aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Chinner <dchinner@redhat.com>2013-10-14 18:17:56 -0400
committerBen Myers <bpm@sgi.com>2013-10-23 18:16:32 -0400
commitc963c6193a5adaec58044e238ef23516d04e5a74 (patch)
tree0e785abb7cafe10276ee8d46ecfbde5b409ba63f
parenta4fbe6ab1e7abecf42b75e9c73701ed33b4ab03b (diff)
xfs: split xfs_rtalloc.c for userspace sanity
xfs_rtalloc.c is partially shared with userspace. Split the file up into two parts - one that is kernel private and the other which is wholly shared with userspace. Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Ben Myers <bpm@sgi.com>
-rw-r--r--fs/xfs/Makefile6
-rw-r--r--fs/xfs/xfs_rtalloc.c1537
-rw-r--r--fs/xfs/xfs_rtalloc.h24
-rw-r--r--fs/xfs/xfs_rtbitmap.c973
4 files changed, 1292 insertions, 1248 deletions
diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile
index 08f11bbf56ae..33a69fabfd83 100644
--- a/fs/xfs/Makefile
+++ b/fs/xfs/Makefile
@@ -104,7 +104,11 @@ xfs-$(CONFIG_XFS_QUOTA) += xfs_dquot.o \
104 xfs_qm_bhv.o \ 104 xfs_qm_bhv.o \
105 xfs_qm.o \ 105 xfs_qm.o \
106 xfs_quotaops.o 106 xfs_quotaops.o
107xfs-$(CONFIG_XFS_RT) += xfs_rtalloc.o 107
108# xfs_rtbitmap is shared with libxfs
109xfs-$(CONFIG_XFS_RT) += xfs_rtalloc.o \
110 xfs_rtbitmap.o
111
108xfs-$(CONFIG_XFS_POSIX_ACL) += xfs_acl.o 112xfs-$(CONFIG_XFS_POSIX_ACL) += xfs_acl.o
109xfs-$(CONFIG_PROC_FS) += xfs_stats.o 113xfs-$(CONFIG_PROC_FS) += xfs_stats.o
110xfs-$(CONFIG_SYSCTL) += xfs_sysctl.o 114xfs-$(CONFIG_SYSCTL) += xfs_sysctl.o
diff --git a/fs/xfs/xfs_rtalloc.c b/fs/xfs/xfs_rtalloc.c
index d5da15578185..a6a76b2b6a85 100644
--- a/fs/xfs/xfs_rtalloc.c
+++ b/fs/xfs/xfs_rtalloc.c
@@ -37,151 +37,240 @@
37#include "xfs_buf.h" 37#include "xfs_buf.h"
38#include "xfs_icache.h" 38#include "xfs_icache.h"
39#include "xfs_dinode.h" 39#include "xfs_dinode.h"
40#include "xfs_rtalloc.h"
40 41
41 42
42/* 43/*
43 * Prototypes for internal functions. 44 * Read and return the summary information for a given extent size,
45 * bitmap block combination.
46 * Keeps track of a current summary block, so we don't keep reading
47 * it from the buffer cache.
44 */ 48 */
49STATIC int /* error */
50xfs_rtget_summary(
51 xfs_mount_t *mp, /* file system mount structure */
52 xfs_trans_t *tp, /* transaction pointer */
53 int log, /* log2 of extent size */
54 xfs_rtblock_t bbno, /* bitmap block number */
55 xfs_buf_t **rbpp, /* in/out: summary block buffer */
56 xfs_fsblock_t *rsb, /* in/out: summary block number */
57 xfs_suminfo_t *sum) /* out: summary info for this block */
58{
59 xfs_buf_t *bp; /* buffer for summary block */
60 int error; /* error value */
61 xfs_fsblock_t sb; /* summary fsblock */
62 int so; /* index into the summary file */
63 xfs_suminfo_t *sp; /* pointer to returned data */
45 64
65 /*
66 * Compute entry number in the summary file.
67 */
68 so = XFS_SUMOFFS(mp, log, bbno);
69 /*
70 * Compute the block number in the summary file.
71 */
72 sb = XFS_SUMOFFSTOBLOCK(mp, so);
73 /*
74 * If we have an old buffer, and the block number matches, use that.
75 */
76 if (rbpp && *rbpp && *rsb == sb)
77 bp = *rbpp;
78 /*
79 * Otherwise we have to get the buffer.
80 */
81 else {
82 /*
83 * If there was an old one, get rid of it first.
84 */
85 if (rbpp && *rbpp)
86 xfs_trans_brelse(tp, *rbpp);
87 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
88 if (error) {
89 return error;
90 }
91 /*
92 * Remember this buffer and block for the next call.
93 */
94 if (rbpp) {
95 *rbpp = bp;
96 *rsb = sb;
97 }
98 }
99 /*
100 * Point to the summary information & copy it out.
101 */
102 sp = XFS_SUMPTR(mp, bp, so);
103 *sum = *sp;
104 /*
105 * Drop the buffer if we're not asked to remember it.
106 */
107 if (!rbpp)
108 xfs_trans_brelse(tp, bp);
109 return 0;
110}
46 111
47STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
48 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
49STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
50 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
51STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
52 xfs_extlen_t, int, xfs_rtblock_t *, int *);
53STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
54 xfs_rtblock_t, xfs_rtblock_t *);
55STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
56 xfs_rtblock_t, xfs_rtblock_t *);
57STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
58 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
59STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
60 xfs_extlen_t, int);
61STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
62 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
63
64/*
65 * Internal functions.
66 */
67 112
68/* 113/*
69 * Allocate space to the bitmap or summary file, and zero it, for growfs. 114 * Return whether there are any free extents in the size range given
115 * by low and high, for the bitmap block bbno.
70 */ 116 */
71STATIC int /* error */ 117STATIC int /* error */
72xfs_growfs_rt_alloc( 118xfs_rtany_summary(
73 xfs_mount_t *mp, /* file system mount point */ 119 xfs_mount_t *mp, /* file system mount structure */
74 xfs_extlen_t oblocks, /* old count of blocks */ 120 xfs_trans_t *tp, /* transaction pointer */
75 xfs_extlen_t nblocks, /* new count of blocks */ 121 int low, /* low log2 extent size */
76 xfs_inode_t *ip) /* inode (bitmap/summary) */ 122 int high, /* high log2 extent size */
123 xfs_rtblock_t bbno, /* bitmap block number */
124 xfs_buf_t **rbpp, /* in/out: summary block buffer */
125 xfs_fsblock_t *rsb, /* in/out: summary block number */
126 int *stat) /* out: any good extents here? */
77{ 127{
78 xfs_fileoff_t bno; /* block number in file */ 128 int error; /* error value */
79 xfs_buf_t *bp; /* temporary buffer for zeroing */ 129 int log; /* loop counter, log2 of ext. size */
80 int committed; /* transaction committed flag */ 130 xfs_suminfo_t sum; /* summary data */
81 xfs_daddr_t d; /* disk block address */
82 int error; /* error return value */
83 xfs_fsblock_t firstblock; /* first block allocated in xaction */
84 xfs_bmap_free_t flist; /* list of freed blocks */
85 xfs_fsblock_t fsbno; /* filesystem block for bno */
86 xfs_bmbt_irec_t map; /* block map output */
87 int nmap; /* number of block maps */
88 int resblks; /* space reservation */
89 131
90 /* 132 /*
91 * Allocate space to the file, as necessary. 133 * Loop over logs of extent sizes. Order is irrelevant.
92 */ 134 */
93 while (oblocks < nblocks) { 135 for (log = low; log <= high; log++) {
94 int cancelflags = 0;
95 xfs_trans_t *tp;
96
97 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
98 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
99 /* 136 /*
100 * Reserve space & log for one extent added to the file. 137 * Get one summary datum.
101 */ 138 */
102 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growdata, 139 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
103 resblks, 0); 140 if (error) {
104 if (error) 141 return error;
105 goto error_cancel; 142 }
106 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
107 /* 143 /*
108 * Lock the inode. 144 * If there are any, return success.
109 */ 145 */
110 xfs_ilock(ip, XFS_ILOCK_EXCL); 146 if (sum) {
111 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 147 *stat = 1;
148 return 0;
149 }
150 }
151 /*
152 * Found nothing, return failure.
153 */
154 *stat = 0;
155 return 0;
156}
112 157
113 xfs_bmap_init(&flist, &firstblock); 158
114 /* 159/*
115 * Allocate blocks to the bitmap file. 160 * Copy and transform the summary file, given the old and new
116 */ 161 * parameters in the mount structures.
117 nmap = 1; 162 */
118 cancelflags |= XFS_TRANS_ABORT; 163STATIC int /* error */
119 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks, 164xfs_rtcopy_summary(
120 XFS_BMAPI_METADATA, &firstblock, 165 xfs_mount_t *omp, /* old file system mount point */
121 resblks, &map, &nmap, &flist); 166 xfs_mount_t *nmp, /* new file system mount point */
122 if (!error && nmap < 1) 167 xfs_trans_t *tp) /* transaction pointer */
123 error = XFS_ERROR(ENOSPC); 168{
124 if (error) 169 xfs_rtblock_t bbno; /* bitmap block number */
125 goto error_cancel; 170 xfs_buf_t *bp; /* summary buffer */
126 /* 171 int error; /* error return value */
127 * Free any blocks freed up in the transaction, then commit. 172 int log; /* summary level number (log length) */
128 */ 173 xfs_suminfo_t sum; /* summary data */
129 error = xfs_bmap_finish(&tp, &flist, &committed); 174 xfs_fsblock_t sumbno; /* summary block number */
130 if (error) 175
131 goto error_cancel; 176 bp = NULL;
132 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); 177 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
133 if (error) 178 for (bbno = omp->m_sb.sb_rbmblocks - 1;
134 goto error; 179 (xfs_srtblock_t)bbno >= 0;
135 /* 180 bbno--) {
136 * Now we need to clear the allocated blocks. 181 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
137 * Do this one block per transaction, to keep it simple. 182 &sumbno, &sum);
138 */
139 cancelflags = 0;
140 for (bno = map.br_startoff, fsbno = map.br_startblock;
141 bno < map.br_startoff + map.br_blockcount;
142 bno++, fsbno++) {
143 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
144 /*
145 * Reserve log for one block zeroing.
146 */
147 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
148 0, 0);
149 if (error) 183 if (error)
150 goto error_cancel; 184 return error;
151 /* 185 if (sum == 0)
152 * Lock the bitmap inode. 186 continue;
153 */ 187 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
154 xfs_ilock(ip, XFS_ILOCK_EXCL); 188 &bp, &sumbno);
155 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
156 /*
157 * Get a buffer for the block.
158 */
159 d = XFS_FSB_TO_DADDR(mp, fsbno);
160 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
161 mp->m_bsize, 0);
162 if (bp == NULL) {
163 error = XFS_ERROR(EIO);
164error_cancel:
165 xfs_trans_cancel(tp, cancelflags);
166 goto error;
167 }
168 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
169 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
170 /*
171 * Commit the transaction.
172 */
173 error = xfs_trans_commit(tp, 0);
174 if (error) 189 if (error)
175 goto error; 190 return error;
191 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
192 &bp, &sumbno);
193 if (error)
194 return error;
195 ASSERT(sum > 0);
176 } 196 }
177 /*
178 * Go on to the next extent, if any.
179 */
180 oblocks = map.br_startoff + map.br_blockcount;
181 } 197 }
182 return 0; 198 return 0;
199}
200/*
201 * Mark an extent specified by start and len allocated.
202 * Updates all the summary information as well as the bitmap.
203 */
204STATIC int /* error */
205xfs_rtallocate_range(
206 xfs_mount_t *mp, /* file system mount point */
207 xfs_trans_t *tp, /* transaction pointer */
208 xfs_rtblock_t start, /* start block to allocate */
209 xfs_extlen_t len, /* length to allocate */
210 xfs_buf_t **rbpp, /* in/out: summary block buffer */
211 xfs_fsblock_t *rsb) /* in/out: summary block number */
212{
213 xfs_rtblock_t end; /* end of the allocated extent */
214 int error; /* error value */
215 xfs_rtblock_t postblock = 0; /* first block allocated > end */
216 xfs_rtblock_t preblock = 0; /* first block allocated < start */
183 217
184error: 218 end = start + len - 1;
219 /*
220 * Assume we're allocating out of the middle of a free extent.
221 * We need to find the beginning and end of the extent so we can
222 * properly update the summary.
223 */
224 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
225 if (error) {
226 return error;
227 }
228 /*
229 * Find the next allocated block (end of free extent).
230 */
231 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
232 &postblock);
233 if (error) {
234 return error;
235 }
236 /*
237 * Decrement the summary information corresponding to the entire
238 * (old) free extent.
239 */
240 error = xfs_rtmodify_summary(mp, tp,
241 XFS_RTBLOCKLOG(postblock + 1 - preblock),
242 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
243 if (error) {
244 return error;
245 }
246 /*
247 * If there are blocks not being allocated at the front of the
248 * old extent, add summary data for them to be free.
249 */
250 if (preblock < start) {
251 error = xfs_rtmodify_summary(mp, tp,
252 XFS_RTBLOCKLOG(start - preblock),
253 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
254 if (error) {
255 return error;
256 }
257 }
258 /*
259 * If there are blocks not being allocated at the end of the
260 * old extent, add summary data for them to be free.
261 */
262 if (postblock > end) {
263 error = xfs_rtmodify_summary(mp, tp,
264 XFS_RTBLOCKLOG(postblock - end),
265 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
266 if (error) {
267 return error;
268 }
269 }
270 /*
271 * Modify the bitmap to mark this extent allocated.
272 */
273 error = xfs_rtmodify_range(mp, tp, start, len, 0);
185 return error; 274 return error;
186} 275}
187 276
@@ -720,1112 +809,126 @@ xfs_rtallocate_extent_size(
720} 809}
721 810
722/* 811/*
723 * Mark an extent specified by start and len allocated. 812 * Allocate space to the bitmap or summary file, and zero it, for growfs.
724 * Updates all the summary information as well as the bitmap.
725 */ 813 */
726STATIC int /* error */ 814STATIC int /* error */
727xfs_rtallocate_range( 815xfs_growfs_rt_alloc(
728 xfs_mount_t *mp, /* file system mount point */ 816 xfs_mount_t *mp, /* file system mount point */
729 xfs_trans_t *tp, /* transaction pointer */ 817 xfs_extlen_t oblocks, /* old count of blocks */
730 xfs_rtblock_t start, /* start block to allocate */ 818 xfs_extlen_t nblocks, /* new count of blocks */
731 xfs_extlen_t len, /* length to allocate */ 819 xfs_inode_t *ip) /* inode (bitmap/summary) */
732 xfs_buf_t **rbpp, /* in/out: summary block buffer */
733 xfs_fsblock_t *rsb) /* in/out: summary block number */
734{ 820{
735 xfs_rtblock_t end; /* end of the allocated extent */ 821 xfs_fileoff_t bno; /* block number in file */
736 int error; /* error value */ 822 xfs_buf_t *bp; /* temporary buffer for zeroing */
737 xfs_rtblock_t postblock = 0; /* first block allocated > end */ 823 int committed; /* transaction committed flag */
738 xfs_rtblock_t preblock = 0; /* first block allocated < start */ 824 xfs_daddr_t d; /* disk block address */
825 int error; /* error return value */
826 xfs_fsblock_t firstblock; /* first block allocated in xaction */
827 xfs_bmap_free_t flist; /* list of freed blocks */
828 xfs_fsblock_t fsbno; /* filesystem block for bno */
829 xfs_bmbt_irec_t map; /* block map output */
830 int nmap; /* number of block maps */
831 int resblks; /* space reservation */
739 832
740 end = start + len - 1;
741 /*
742 * Assume we're allocating out of the middle of a free extent.
743 * We need to find the beginning and end of the extent so we can
744 * properly update the summary.
745 */
746 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
747 if (error) {
748 return error;
749 }
750 /*
751 * Find the next allocated block (end of free extent).
752 */
753 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
754 &postblock);
755 if (error) {
756 return error;
757 }
758 /*
759 * Decrement the summary information corresponding to the entire
760 * (old) free extent.
761 */
762 error = xfs_rtmodify_summary(mp, tp,
763 XFS_RTBLOCKLOG(postblock + 1 - preblock),
764 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
765 if (error) {
766 return error;
767 }
768 /* 833 /*
769 * If there are blocks not being allocated at the front of the 834 * Allocate space to the file, as necessary.
770 * old extent, add summary data for them to be free.
771 */
772 if (preblock < start) {
773 error = xfs_rtmodify_summary(mp, tp,
774 XFS_RTBLOCKLOG(start - preblock),
775 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
776 if (error) {
777 return error;
778 }
779 }
780 /*
781 * If there are blocks not being allocated at the end of the
782 * old extent, add summary data for them to be free.
783 */
784 if (postblock > end) {
785 error = xfs_rtmodify_summary(mp, tp,
786 XFS_RTBLOCKLOG(postblock - end),
787 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
788 if (error) {
789 return error;
790 }
791 }
792 /*
793 * Modify the bitmap to mark this extent allocated.
794 */ 835 */
795 error = xfs_rtmodify_range(mp, tp, start, len, 0); 836 while (oblocks < nblocks) {
796 return error; 837 int cancelflags = 0;
797} 838 xfs_trans_t *tp;
798
799/*
800 * Return whether there are any free extents in the size range given
801 * by low and high, for the bitmap block bbno.
802 */
803STATIC int /* error */
804xfs_rtany_summary(
805 xfs_mount_t *mp, /* file system mount structure */
806 xfs_trans_t *tp, /* transaction pointer */
807 int low, /* low log2 extent size */
808 int high, /* high log2 extent size */
809 xfs_rtblock_t bbno, /* bitmap block number */
810 xfs_buf_t **rbpp, /* in/out: summary block buffer */
811 xfs_fsblock_t *rsb, /* in/out: summary block number */
812 int *stat) /* out: any good extents here? */
813{
814 int error; /* error value */
815 int log; /* loop counter, log2 of ext. size */
816 xfs_suminfo_t sum; /* summary data */
817 839
818 /* 840 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
819 * Loop over logs of extent sizes. Order is irrelevant. 841 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
820 */
821 for (log = low; log <= high; log++) {
822 /* 842 /*
823 * Get one summary datum. 843 * Reserve space & log for one extent added to the file.
824 */ 844 */
825 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum); 845 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growdata,
826 if (error) { 846 resblks, 0);
827 return error; 847 if (error)
828 } 848 goto error_cancel;
849 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
829 /* 850 /*
830 * If there are any, return success. 851 * Lock the inode.
831 */ 852 */
832 if (sum) { 853 xfs_ilock(ip, XFS_ILOCK_EXCL);
833 *stat = 1; 854 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
834 return 0;
835 }
836 }
837 /*
838 * Found nothing, return failure.
839 */
840 *stat = 0;
841 return 0;
842}
843
844/*
845 * Get a buffer for the bitmap or summary file block specified.
846 * The buffer is returned read and locked.
847 */
848STATIC int /* error */
849xfs_rtbuf_get(
850 xfs_mount_t *mp, /* file system mount structure */
851 xfs_trans_t *tp, /* transaction pointer */
852 xfs_rtblock_t block, /* block number in bitmap or summary */
853 int issum, /* is summary not bitmap */
854 xfs_buf_t **bpp) /* output: buffer for the block */
855{
856 xfs_buf_t *bp; /* block buffer, result */
857 xfs_inode_t *ip; /* bitmap or summary inode */
858 xfs_bmbt_irec_t map;
859 int nmap = 1;
860 int error; /* error value */
861
862 ip = issum ? mp->m_rsumip : mp->m_rbmip;
863
864 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
865 if (error)
866 return error;
867
868 ASSERT(map.br_startblock != NULLFSBLOCK);
869 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
870 XFS_FSB_TO_DADDR(mp, map.br_startblock),
871 mp->m_bsize, 0, &bp, NULL);
872 if (error)
873 return error;
874 ASSERT(!xfs_buf_geterror(bp));
875 *bpp = bp;
876 return 0;
877}
878
879#ifdef DEBUG
880/*
881 * Check that the given extent (block range) is allocated already.
882 */
883STATIC int /* error */
884xfs_rtcheck_alloc_range(
885 xfs_mount_t *mp, /* file system mount point */
886 xfs_trans_t *tp, /* transaction pointer */
887 xfs_rtblock_t bno, /* starting block number of extent */
888 xfs_extlen_t len, /* length of extent */
889 int *stat) /* out: 1 for allocated, 0 for not */
890{
891 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
892
893 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
894}
895#endif
896
897/*
898 * Check that the given range is either all allocated (val = 0) or
899 * all free (val = 1).
900 */
901STATIC int /* error */
902xfs_rtcheck_range(
903 xfs_mount_t *mp, /* file system mount point */
904 xfs_trans_t *tp, /* transaction pointer */
905 xfs_rtblock_t start, /* starting block number of extent */
906 xfs_extlen_t len, /* length of extent */
907 int val, /* 1 for free, 0 for allocated */
908 xfs_rtblock_t *new, /* out: first block not matching */
909 int *stat) /* out: 1 for matches, 0 for not */
910{
911 xfs_rtword_t *b; /* current word in buffer */
912 int bit; /* bit number in the word */
913 xfs_rtblock_t block; /* bitmap block number */
914 xfs_buf_t *bp; /* buf for the block */
915 xfs_rtword_t *bufp; /* starting word in buffer */
916 int error; /* error value */
917 xfs_rtblock_t i; /* current bit number rel. to start */
918 xfs_rtblock_t lastbit; /* last useful bit in word */
919 xfs_rtword_t mask; /* mask of relevant bits for value */
920 xfs_rtword_t wdiff; /* difference from wanted value */
921 int word; /* word number in the buffer */
922 855
923 /* 856 xfs_bmap_init(&flist, &firstblock);
924 * Compute starting bitmap block number
925 */
926 block = XFS_BITTOBLOCK(mp, start);
927 /*
928 * Read the bitmap block.
929 */
930 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
931 if (error) {
932 return error;
933 }
934 bufp = bp->b_addr;
935 /*
936 * Compute the starting word's address, and starting bit.
937 */
938 word = XFS_BITTOWORD(mp, start);
939 b = &bufp[word];
940 bit = (int)(start & (XFS_NBWORD - 1));
941 /*
942 * 0 (allocated) => all zero's; 1 (free) => all one's.
943 */
944 val = -val;
945 /*
946 * If not starting on a word boundary, deal with the first
947 * (partial) word.
948 */
949 if (bit) {
950 /*
951 * Compute first bit not examined.
952 */
953 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
954 /*
955 * Mask of relevant bits.
956 */
957 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
958 /*
959 * Compute difference between actual and desired value.
960 */
961 if ((wdiff = (*b ^ val) & mask)) {
962 /*
963 * Different, compute first wrong bit and return.
964 */
965 xfs_trans_brelse(tp, bp);
966 i = XFS_RTLOBIT(wdiff) - bit;
967 *new = start + i;
968 *stat = 0;
969 return 0;
970 }
971 i = lastbit - bit;
972 /*
973 * Go on to next block if that's where the next word is
974 * and we need the next word.
975 */
976 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
977 /*
978 * If done with this block, get the next one.
979 */
980 xfs_trans_brelse(tp, bp);
981 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
982 if (error) {
983 return error;
984 }
985 b = bufp = bp->b_addr;
986 word = 0;
987 } else {
988 /*
989 * Go on to the next word in the buffer.
990 */
991 b++;
992 }
993 } else {
994 /*
995 * Starting on a word boundary, no partial word.
996 */
997 i = 0;
998 }
999 /*
1000 * Loop over whole words in buffers. When we use up one buffer
1001 * we move on to the next one.
1002 */
1003 while (len - i >= XFS_NBWORD) {
1004 /*
1005 * Compute difference between actual and desired value.
1006 */
1007 if ((wdiff = *b ^ val)) {
1008 /*
1009 * Different, compute first wrong bit and return.
1010 */
1011 xfs_trans_brelse(tp, bp);
1012 i += XFS_RTLOBIT(wdiff);
1013 *new = start + i;
1014 *stat = 0;
1015 return 0;
1016 }
1017 i += XFS_NBWORD;
1018 /* 857 /*
1019 * Go on to next block if that's where the next word is 858 * Allocate blocks to the bitmap file.
1020 * and we need the next word.
1021 */ 859 */
1022 if (++word == XFS_BLOCKWSIZE(mp) && i < len) { 860 nmap = 1;
1023 /* 861 cancelflags |= XFS_TRANS_ABORT;
1024 * If done with this block, get the next one. 862 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
1025 */ 863 XFS_BMAPI_METADATA, &firstblock,
1026 xfs_trans_brelse(tp, bp); 864 resblks, &map, &nmap, &flist);
1027 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); 865 if (!error && nmap < 1)
1028 if (error) { 866 error = XFS_ERROR(ENOSPC);
1029 return error; 867 if (error)
1030 } 868 goto error_cancel;
1031 b = bufp = bp->b_addr;
1032 word = 0;
1033 } else {
1034 /*
1035 * Go on to the next word in the buffer.
1036 */
1037 b++;
1038 }
1039 }
1040 /*
1041 * If not ending on a word boundary, deal with the last
1042 * (partial) word.
1043 */
1044 if ((lastbit = len - i)) {
1045 /* 869 /*
1046 * Mask of relevant bits. 870 * Free any blocks freed up in the transaction, then commit.
1047 */ 871 */
1048 mask = ((xfs_rtword_t)1 << lastbit) - 1; 872 error = xfs_bmap_finish(&tp, &flist, &committed);
873 if (error)
874 goto error_cancel;
875 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
876 if (error)
877 goto error;
1049 /* 878 /*
1050 * Compute difference between actual and desired value. 879 * Now we need to clear the allocated blocks.
880 * Do this one block per transaction, to keep it simple.
1051 */ 881 */
1052 if ((wdiff = (*b ^ val) & mask)) { 882 cancelflags = 0;
883 for (bno = map.br_startoff, fsbno = map.br_startblock;
884 bno < map.br_startoff + map.br_blockcount;
885 bno++, fsbno++) {
886 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
1053 /* 887 /*
1054 * Different, compute first wrong bit and return. 888 * Reserve log for one block zeroing.
1055 */ 889 */
1056 xfs_trans_brelse(tp, bp); 890 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
1057 i += XFS_RTLOBIT(wdiff); 891 0, 0);
1058 *new = start + i;
1059 *stat = 0;
1060 return 0;
1061 } else
1062 i = len;
1063 }
1064 /*
1065 * Successful, return.
1066 */
1067 xfs_trans_brelse(tp, bp);
1068 *new = start + i;
1069 *stat = 1;
1070 return 0;
1071}
1072
1073/*
1074 * Copy and transform the summary file, given the old and new
1075 * parameters in the mount structures.
1076 */
1077STATIC int /* error */
1078xfs_rtcopy_summary(
1079 xfs_mount_t *omp, /* old file system mount point */
1080 xfs_mount_t *nmp, /* new file system mount point */
1081 xfs_trans_t *tp) /* transaction pointer */
1082{
1083 xfs_rtblock_t bbno; /* bitmap block number */
1084 xfs_buf_t *bp; /* summary buffer */
1085 int error; /* error return value */
1086 int log; /* summary level number (log length) */
1087 xfs_suminfo_t sum; /* summary data */
1088 xfs_fsblock_t sumbno; /* summary block number */
1089
1090 bp = NULL;
1091 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1092 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1093 (xfs_srtblock_t)bbno >= 0;
1094 bbno--) {
1095 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1096 &sumbno, &sum);
1097 if (error)
1098 return error;
1099 if (sum == 0)
1100 continue;
1101 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1102 &bp, &sumbno);
1103 if (error)
1104 return error;
1105 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1106 &bp, &sumbno);
1107 if (error) 892 if (error)
1108 return error; 893 goto error_cancel;
1109 ASSERT(sum > 0);
1110 }
1111 }
1112 return 0;
1113}
1114
1115/*
1116 * Searching backward from start to limit, find the first block whose
1117 * allocated/free state is different from start's.
1118 */
1119STATIC int /* error */
1120xfs_rtfind_back(
1121 xfs_mount_t *mp, /* file system mount point */
1122 xfs_trans_t *tp, /* transaction pointer */
1123 xfs_rtblock_t start, /* starting block to look at */
1124 xfs_rtblock_t limit, /* last block to look at */
1125 xfs_rtblock_t *rtblock) /* out: start block found */
1126{
1127 xfs_rtword_t *b; /* current word in buffer */
1128 int bit; /* bit number in the word */
1129 xfs_rtblock_t block; /* bitmap block number */
1130 xfs_buf_t *bp; /* buf for the block */
1131 xfs_rtword_t *bufp; /* starting word in buffer */
1132 int error; /* error value */
1133 xfs_rtblock_t firstbit; /* first useful bit in the word */
1134 xfs_rtblock_t i; /* current bit number rel. to start */
1135 xfs_rtblock_t len; /* length of inspected area */
1136 xfs_rtword_t mask; /* mask of relevant bits for value */
1137 xfs_rtword_t want; /* mask for "good" values */
1138 xfs_rtword_t wdiff; /* difference from wanted value */
1139 int word; /* word number in the buffer */
1140
1141 /*
1142 * Compute and read in starting bitmap block for starting block.
1143 */
1144 block = XFS_BITTOBLOCK(mp, start);
1145 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1146 if (error) {
1147 return error;
1148 }
1149 bufp = bp->b_addr;
1150 /*
1151 * Get the first word's index & point to it.
1152 */
1153 word = XFS_BITTOWORD(mp, start);
1154 b = &bufp[word];
1155 bit = (int)(start & (XFS_NBWORD - 1));
1156 len = start - limit + 1;
1157 /*
1158 * Compute match value, based on the bit at start: if 1 (free)
1159 * then all-ones, else all-zeroes.
1160 */
1161 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1162 /*
1163 * If the starting position is not word-aligned, deal with the
1164 * partial word.
1165 */
1166 if (bit < XFS_NBWORD - 1) {
1167 /*
1168 * Calculate first (leftmost) bit number to look at,
1169 * and mask for all the relevant bits in this word.
1170 */
1171 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1172 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1173 firstbit;
1174 /*
1175 * Calculate the difference between the value there
1176 * and what we're looking for.
1177 */
1178 if ((wdiff = (*b ^ want) & mask)) {
1179 /*
1180 * Different. Mark where we are and return.
1181 */
1182 xfs_trans_brelse(tp, bp);
1183 i = bit - XFS_RTHIBIT(wdiff);
1184 *rtblock = start - i + 1;
1185 return 0;
1186 }
1187 i = bit - firstbit + 1;
1188 /*
1189 * Go on to previous block if that's where the previous word is
1190 * and we need the previous word.
1191 */
1192 if (--word == -1 && i < len) {
1193 /*
1194 * If done with this block, get the previous one.
1195 */
1196 xfs_trans_brelse(tp, bp);
1197 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1198 if (error) {
1199 return error;
1200 }
1201 bufp = bp->b_addr;
1202 word = XFS_BLOCKWMASK(mp);
1203 b = &bufp[word];
1204 } else {
1205 /*
1206 * Go on to the previous word in the buffer.
1207 */
1208 b--;
1209 }
1210 } else {
1211 /*
1212 * Starting on a word boundary, no partial word.
1213 */
1214 i = 0;
1215 }
1216 /*
1217 * Loop over whole words in buffers. When we use up one buffer
1218 * we move on to the previous one.
1219 */
1220 while (len - i >= XFS_NBWORD) {
1221 /*
1222 * Compute difference between actual and desired value.
1223 */
1224 if ((wdiff = *b ^ want)) {
1225 /*
1226 * Different, mark where we are and return.
1227 */
1228 xfs_trans_brelse(tp, bp);
1229 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1230 *rtblock = start - i + 1;
1231 return 0;
1232 }
1233 i += XFS_NBWORD;
1234 /*
1235 * Go on to previous block if that's where the previous word is
1236 * and we need the previous word.
1237 */
1238 if (--word == -1 && i < len) {
1239 /*
1240 * If done with this block, get the previous one.
1241 */
1242 xfs_trans_brelse(tp, bp);
1243 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1244 if (error) {
1245 return error;
1246 }
1247 bufp = bp->b_addr;
1248 word = XFS_BLOCKWMASK(mp);
1249 b = &bufp[word];
1250 } else {
1251 /*
1252 * Go on to the previous word in the buffer.
1253 */
1254 b--;
1255 }
1256 }
1257 /*
1258 * If not ending on a word boundary, deal with the last
1259 * (partial) word.
1260 */
1261 if (len - i) {
1262 /*
1263 * Calculate first (leftmost) bit number to look at,
1264 * and mask for all the relevant bits in this word.
1265 */
1266 firstbit = XFS_NBWORD - (len - i);
1267 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1268 /*
1269 * Compute difference between actual and desired value.
1270 */
1271 if ((wdiff = (*b ^ want) & mask)) {
1272 /*
1273 * Different, mark where we are and return.
1274 */
1275 xfs_trans_brelse(tp, bp);
1276 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1277 *rtblock = start - i + 1;
1278 return 0;
1279 } else
1280 i = len;
1281 }
1282 /*
1283 * No match, return that we scanned the whole area.
1284 */
1285 xfs_trans_brelse(tp, bp);
1286 *rtblock = start - i + 1;
1287 return 0;
1288}
1289
1290/*
1291 * Searching forward from start to limit, find the first block whose
1292 * allocated/free state is different from start's.
1293 */
1294STATIC int /* error */
1295xfs_rtfind_forw(
1296 xfs_mount_t *mp, /* file system mount point */
1297 xfs_trans_t *tp, /* transaction pointer */
1298 xfs_rtblock_t start, /* starting block to look at */
1299 xfs_rtblock_t limit, /* last block to look at */
1300 xfs_rtblock_t *rtblock) /* out: start block found */
1301{
1302 xfs_rtword_t *b; /* current word in buffer */
1303 int bit; /* bit number in the word */
1304 xfs_rtblock_t block; /* bitmap block number */
1305 xfs_buf_t *bp; /* buf for the block */
1306 xfs_rtword_t *bufp; /* starting word in buffer */
1307 int error; /* error value */
1308 xfs_rtblock_t i; /* current bit number rel. to start */
1309 xfs_rtblock_t lastbit; /* last useful bit in the word */
1310 xfs_rtblock_t len; /* length of inspected area */
1311 xfs_rtword_t mask; /* mask of relevant bits for value */
1312 xfs_rtword_t want; /* mask for "good" values */
1313 xfs_rtword_t wdiff; /* difference from wanted value */
1314 int word; /* word number in the buffer */
1315
1316 /*
1317 * Compute and read in starting bitmap block for starting block.
1318 */
1319 block = XFS_BITTOBLOCK(mp, start);
1320 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1321 if (error) {
1322 return error;
1323 }
1324 bufp = bp->b_addr;
1325 /*
1326 * Get the first word's index & point to it.
1327 */
1328 word = XFS_BITTOWORD(mp, start);
1329 b = &bufp[word];
1330 bit = (int)(start & (XFS_NBWORD - 1));
1331 len = limit - start + 1;
1332 /*
1333 * Compute match value, based on the bit at start: if 1 (free)
1334 * then all-ones, else all-zeroes.
1335 */
1336 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1337 /*
1338 * If the starting position is not word-aligned, deal with the
1339 * partial word.
1340 */
1341 if (bit) {
1342 /*
1343 * Calculate last (rightmost) bit number to look at,
1344 * and mask for all the relevant bits in this word.
1345 */
1346 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1347 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1348 /*
1349 * Calculate the difference between the value there
1350 * and what we're looking for.
1351 */
1352 if ((wdiff = (*b ^ want) & mask)) {
1353 /*
1354 * Different. Mark where we are and return.
1355 */
1356 xfs_trans_brelse(tp, bp);
1357 i = XFS_RTLOBIT(wdiff) - bit;
1358 *rtblock = start + i - 1;
1359 return 0;
1360 }
1361 i = lastbit - bit;
1362 /*
1363 * Go on to next block if that's where the next word is
1364 * and we need the next word.
1365 */
1366 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1367 /*
1368 * If done with this block, get the previous one.
1369 */
1370 xfs_trans_brelse(tp, bp);
1371 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1372 if (error) {
1373 return error;
1374 }
1375 b = bufp = bp->b_addr;
1376 word = 0;
1377 } else {
1378 /*
1379 * Go on to the previous word in the buffer.
1380 */
1381 b++;
1382 }
1383 } else {
1384 /*
1385 * Starting on a word boundary, no partial word.
1386 */
1387 i = 0;
1388 }
1389 /*
1390 * Loop over whole words in buffers. When we use up one buffer
1391 * we move on to the next one.
1392 */
1393 while (len - i >= XFS_NBWORD) {
1394 /*
1395 * Compute difference between actual and desired value.
1396 */
1397 if ((wdiff = *b ^ want)) {
1398 /* 894 /*
1399 * Different, mark where we are and return. 895 * Lock the bitmap inode.
1400 */ 896 */
1401 xfs_trans_brelse(tp, bp); 897 xfs_ilock(ip, XFS_ILOCK_EXCL);
1402 i += XFS_RTLOBIT(wdiff); 898 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1403 *rtblock = start + i - 1;
1404 return 0;
1405 }
1406 i += XFS_NBWORD;
1407 /*
1408 * Go on to next block if that's where the next word is
1409 * and we need the next word.
1410 */
1411 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1412 /* 899 /*
1413 * If done with this block, get the next one. 900 * Get a buffer for the block.
1414 */ 901 */
1415 xfs_trans_brelse(tp, bp); 902 d = XFS_FSB_TO_DADDR(mp, fsbno);
1416 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp); 903 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1417 if (error) { 904 mp->m_bsize, 0);
1418 return error; 905 if (bp == NULL) {
906 error = XFS_ERROR(EIO);
907error_cancel:
908 xfs_trans_cancel(tp, cancelflags);
909 goto error;
1419 } 910 }
1420 b = bufp = bp->b_addr; 911 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
1421 word = 0; 912 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
1422 } else {
1423 /* 913 /*
1424 * Go on to the next word in the buffer. 914 * Commit the transaction.
1425 */ 915 */
1426 b++; 916 error = xfs_trans_commit(tp, 0);
917 if (error)
918 goto error;
1427 } 919 }
1428 }
1429 /*
1430 * If not ending on a word boundary, deal with the last
1431 * (partial) word.
1432 */
1433 if ((lastbit = len - i)) {
1434 /*
1435 * Calculate mask for all the relevant bits in this word.
1436 */
1437 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1438 /* 920 /*
1439 * Compute difference between actual and desired value. 921 * Go on to the next extent, if any.
1440 */ 922 */
1441 if ((wdiff = (*b ^ want) & mask)) { 923 oblocks = map.br_startoff + map.br_blockcount;
1442 /*
1443 * Different, mark where we are and return.
1444 */
1445 xfs_trans_brelse(tp, bp);
1446 i += XFS_RTLOBIT(wdiff);
1447 *rtblock = start + i - 1;
1448 return 0;
1449 } else
1450 i = len;
1451 } 924 }
1452 /*
1453 * No match, return that we scanned the whole area.
1454 */
1455 xfs_trans_brelse(tp, bp);
1456 *rtblock = start + i - 1;
1457 return 0; 925 return 0;
1458}
1459 926
1460/* 927error:
1461 * Mark an extent specified by start and len freed.
1462 * Updates all the summary information as well as the bitmap.
1463 */
1464STATIC int /* error */
1465xfs_rtfree_range(
1466 xfs_mount_t *mp, /* file system mount point */
1467 xfs_trans_t *tp, /* transaction pointer */
1468 xfs_rtblock_t start, /* starting block to free */
1469 xfs_extlen_t len, /* length to free */
1470 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1471 xfs_fsblock_t *rsb) /* in/out: summary block number */
1472{
1473 xfs_rtblock_t end; /* end of the freed extent */
1474 int error; /* error value */
1475 xfs_rtblock_t postblock; /* first block freed > end */
1476 xfs_rtblock_t preblock; /* first block freed < start */
1477
1478 end = start + len - 1;
1479 /*
1480 * Modify the bitmap to mark this extent freed.
1481 */
1482 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1483 if (error) {
1484 return error;
1485 }
1486 /*
1487 * Assume we're freeing out of the middle of an allocated extent.
1488 * We need to find the beginning and end of the extent so we can
1489 * properly update the summary.
1490 */
1491 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1492 if (error) {
1493 return error;
1494 }
1495 /*
1496 * Find the next allocated block (end of allocated extent).
1497 */
1498 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1499 &postblock);
1500 if (error)
1501 return error;
1502 /*
1503 * If there are blocks not being freed at the front of the
1504 * old extent, add summary data for them to be allocated.
1505 */
1506 if (preblock < start) {
1507 error = xfs_rtmodify_summary(mp, tp,
1508 XFS_RTBLOCKLOG(start - preblock),
1509 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1510 if (error) {
1511 return error;
1512 }
1513 }
1514 /*
1515 * If there are blocks not being freed at the end of the
1516 * old extent, add summary data for them to be allocated.
1517 */
1518 if (postblock > end) {
1519 error = xfs_rtmodify_summary(mp, tp,
1520 XFS_RTBLOCKLOG(postblock - end),
1521 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1522 if (error) {
1523 return error;
1524 }
1525 }
1526 /*
1527 * Increment the summary information corresponding to the entire
1528 * (new) free extent.
1529 */
1530 error = xfs_rtmodify_summary(mp, tp,
1531 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1532 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1533 return error; 928 return error;
1534} 929}
1535 930
1536/* 931/*
1537 * Read and return the summary information for a given extent size,
1538 * bitmap block combination.
1539 * Keeps track of a current summary block, so we don't keep reading
1540 * it from the buffer cache.
1541 */
1542STATIC int /* error */
1543xfs_rtget_summary(
1544 xfs_mount_t *mp, /* file system mount structure */
1545 xfs_trans_t *tp, /* transaction pointer */
1546 int log, /* log2 of extent size */
1547 xfs_rtblock_t bbno, /* bitmap block number */
1548 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1549 xfs_fsblock_t *rsb, /* in/out: summary block number */
1550 xfs_suminfo_t *sum) /* out: summary info for this block */
1551{
1552 xfs_buf_t *bp; /* buffer for summary block */
1553 int error; /* error value */
1554 xfs_fsblock_t sb; /* summary fsblock */
1555 int so; /* index into the summary file */
1556 xfs_suminfo_t *sp; /* pointer to returned data */
1557
1558 /*
1559 * Compute entry number in the summary file.
1560 */
1561 so = XFS_SUMOFFS(mp, log, bbno);
1562 /*
1563 * Compute the block number in the summary file.
1564 */
1565 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1566 /*
1567 * If we have an old buffer, and the block number matches, use that.
1568 */
1569 if (rbpp && *rbpp && *rsb == sb)
1570 bp = *rbpp;
1571 /*
1572 * Otherwise we have to get the buffer.
1573 */
1574 else {
1575 /*
1576 * If there was an old one, get rid of it first.
1577 */
1578 if (rbpp && *rbpp)
1579 xfs_trans_brelse(tp, *rbpp);
1580 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1581 if (error) {
1582 return error;
1583 }
1584 /*
1585 * Remember this buffer and block for the next call.
1586 */
1587 if (rbpp) {
1588 *rbpp = bp;
1589 *rsb = sb;
1590 }
1591 }
1592 /*
1593 * Point to the summary information & copy it out.
1594 */
1595 sp = XFS_SUMPTR(mp, bp, so);
1596 *sum = *sp;
1597 /*
1598 * Drop the buffer if we're not asked to remember it.
1599 */
1600 if (!rbpp)
1601 xfs_trans_brelse(tp, bp);
1602 return 0;
1603}
1604
1605/*
1606 * Set the given range of bitmap bits to the given value.
1607 * Do whatever I/O and logging is required.
1608 */
1609STATIC int /* error */
1610xfs_rtmodify_range(
1611 xfs_mount_t *mp, /* file system mount point */
1612 xfs_trans_t *tp, /* transaction pointer */
1613 xfs_rtblock_t start, /* starting block to modify */
1614 xfs_extlen_t len, /* length of extent to modify */
1615 int val) /* 1 for free, 0 for allocated */
1616{
1617 xfs_rtword_t *b; /* current word in buffer */
1618 int bit; /* bit number in the word */
1619 xfs_rtblock_t block; /* bitmap block number */
1620 xfs_buf_t *bp; /* buf for the block */
1621 xfs_rtword_t *bufp; /* starting word in buffer */
1622 int error; /* error value */
1623 xfs_rtword_t *first; /* first used word in the buffer */
1624 int i; /* current bit number rel. to start */
1625 int lastbit; /* last useful bit in word */
1626 xfs_rtword_t mask; /* mask o frelevant bits for value */
1627 int word; /* word number in the buffer */
1628
1629 /*
1630 * Compute starting bitmap block number.
1631 */
1632 block = XFS_BITTOBLOCK(mp, start);
1633 /*
1634 * Read the bitmap block, and point to its data.
1635 */
1636 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1637 if (error) {
1638 return error;
1639 }
1640 bufp = bp->b_addr;
1641 /*
1642 * Compute the starting word's address, and starting bit.
1643 */
1644 word = XFS_BITTOWORD(mp, start);
1645 first = b = &bufp[word];
1646 bit = (int)(start & (XFS_NBWORD - 1));
1647 /*
1648 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1649 */
1650 val = -val;
1651 /*
1652 * If not starting on a word boundary, deal with the first
1653 * (partial) word.
1654 */
1655 if (bit) {
1656 /*
1657 * Compute first bit not changed and mask of relevant bits.
1658 */
1659 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1660 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1661 /*
1662 * Set/clear the active bits.
1663 */
1664 if (val)
1665 *b |= mask;
1666 else
1667 *b &= ~mask;
1668 i = lastbit - bit;
1669 /*
1670 * Go on to the next block if that's where the next word is
1671 * and we need the next word.
1672 */
1673 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1674 /*
1675 * Log the changed part of this block.
1676 * Get the next one.
1677 */
1678 xfs_trans_log_buf(tp, bp,
1679 (uint)((char *)first - (char *)bufp),
1680 (uint)((char *)b - (char *)bufp));
1681 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1682 if (error) {
1683 return error;
1684 }
1685 first = b = bufp = bp->b_addr;
1686 word = 0;
1687 } else {
1688 /*
1689 * Go on to the next word in the buffer
1690 */
1691 b++;
1692 }
1693 } else {
1694 /*
1695 * Starting on a word boundary, no partial word.
1696 */
1697 i = 0;
1698 }
1699 /*
1700 * Loop over whole words in buffers. When we use up one buffer
1701 * we move on to the next one.
1702 */
1703 while (len - i >= XFS_NBWORD) {
1704 /*
1705 * Set the word value correctly.
1706 */
1707 *b = val;
1708 i += XFS_NBWORD;
1709 /*
1710 * Go on to the next block if that's where the next word is
1711 * and we need the next word.
1712 */
1713 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1714 /*
1715 * Log the changed part of this block.
1716 * Get the next one.
1717 */
1718 xfs_trans_log_buf(tp, bp,
1719 (uint)((char *)first - (char *)bufp),
1720 (uint)((char *)b - (char *)bufp));
1721 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1722 if (error) {
1723 return error;
1724 }
1725 first = b = bufp = bp->b_addr;
1726 word = 0;
1727 } else {
1728 /*
1729 * Go on to the next word in the buffer
1730 */
1731 b++;
1732 }
1733 }
1734 /*
1735 * If not ending on a word boundary, deal with the last
1736 * (partial) word.
1737 */
1738 if ((lastbit = len - i)) {
1739 /*
1740 * Compute a mask of relevant bits.
1741 */
1742 bit = 0;
1743 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1744 /*
1745 * Set/clear the active bits.
1746 */
1747 if (val)
1748 *b |= mask;
1749 else
1750 *b &= ~mask;
1751 b++;
1752 }
1753 /*
1754 * Log any remaining changed bytes.
1755 */
1756 if (b > first)
1757 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1758 (uint)((char *)b - (char *)bufp - 1));
1759 return 0;
1760}
1761
1762/*
1763 * Read and modify the summary information for a given extent size,
1764 * bitmap block combination.
1765 * Keeps track of a current summary block, so we don't keep reading
1766 * it from the buffer cache.
1767 */
1768STATIC int /* error */
1769xfs_rtmodify_summary(
1770 xfs_mount_t *mp, /* file system mount point */
1771 xfs_trans_t *tp, /* transaction pointer */
1772 int log, /* log2 of extent size */
1773 xfs_rtblock_t bbno, /* bitmap block number */
1774 int delta, /* change to make to summary info */
1775 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1776 xfs_fsblock_t *rsb) /* in/out: summary block number */
1777{
1778 xfs_buf_t *bp; /* buffer for the summary block */
1779 int error; /* error value */
1780 xfs_fsblock_t sb; /* summary fsblock */
1781 int so; /* index into the summary file */
1782 xfs_suminfo_t *sp; /* pointer to returned data */
1783
1784 /*
1785 * Compute entry number in the summary file.
1786 */
1787 so = XFS_SUMOFFS(mp, log, bbno);
1788 /*
1789 * Compute the block number in the summary file.
1790 */
1791 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1792 /*
1793 * If we have an old buffer, and the block number matches, use that.
1794 */
1795 if (rbpp && *rbpp && *rsb == sb)
1796 bp = *rbpp;
1797 /*
1798 * Otherwise we have to get the buffer.
1799 */
1800 else {
1801 /*
1802 * If there was an old one, get rid of it first.
1803 */
1804 if (rbpp && *rbpp)
1805 xfs_trans_brelse(tp, *rbpp);
1806 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1807 if (error) {
1808 return error;
1809 }
1810 /*
1811 * Remember this buffer and block for the next call.
1812 */
1813 if (rbpp) {
1814 *rbpp = bp;
1815 *rsb = sb;
1816 }
1817 }
1818 /*
1819 * Point to the summary information, modify and log it.
1820 */
1821 sp = XFS_SUMPTR(mp, bp, so);
1822 *sp += delta;
1823 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
1824 (uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
1825 return 0;
1826}
1827
1828/*
1829 * Visible (exported) functions. 932 * Visible (exported) functions.
1830 */ 933 */
1831 934
@@ -2128,66 +1231,6 @@ xfs_rtallocate_extent(
2128} 1231}
2129 1232
2130/* 1233/*
2131 * Free an extent in the realtime subvolume. Length is expressed in
2132 * realtime extents, as is the block number.
2133 */
2134int /* error */
2135xfs_rtfree_extent(
2136 xfs_trans_t *tp, /* transaction pointer */
2137 xfs_rtblock_t bno, /* starting block number to free */
2138 xfs_extlen_t len) /* length of extent freed */
2139{
2140 int error; /* error value */
2141 xfs_mount_t *mp; /* file system mount structure */
2142 xfs_fsblock_t sb; /* summary file block number */
2143 xfs_buf_t *sumbp; /* summary file block buffer */
2144
2145 mp = tp->t_mountp;
2146
2147 ASSERT(mp->m_rbmip->i_itemp != NULL);
2148 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
2149
2150#ifdef DEBUG
2151 /*
2152 * Check to see that this whole range is currently allocated.
2153 */
2154 {
2155 int stat; /* result from checking range */
2156
2157 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2158 if (error) {
2159 return error;
2160 }
2161 ASSERT(stat);
2162 }
2163#endif
2164 sumbp = NULL;
2165 /*
2166 * Free the range of realtime blocks.
2167 */
2168 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2169 if (error) {
2170 return error;
2171 }
2172 /*
2173 * Mark more blocks free in the superblock.
2174 */
2175 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2176 /*
2177 * If we've now freed all the blocks, reset the file sequence
2178 * number to 0.
2179 */
2180 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2181 mp->m_sb.sb_rextents) {
2182 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2183 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2184 *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
2185 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2186 }
2187 return 0;
2188}
2189
2190/*
2191 * Initialize realtime fields in the mount structure. 1234 * Initialize realtime fields in the mount structure.
2192 */ 1235 */
2193int /* error */ 1236int /* error */
diff --git a/fs/xfs/xfs_rtalloc.h b/fs/xfs/xfs_rtalloc.h
index b2a1a24c0e2f..752b63d10300 100644
--- a/fs/xfs/xfs_rtalloc.h
+++ b/fs/xfs/xfs_rtalloc.h
@@ -95,6 +95,30 @@ xfs_growfs_rt(
95 struct xfs_mount *mp, /* file system mount structure */ 95 struct xfs_mount *mp, /* file system mount structure */
96 xfs_growfs_rt_t *in); /* user supplied growfs struct */ 96 xfs_growfs_rt_t *in); /* user supplied growfs struct */
97 97
98/*
99 * From xfs_rtbitmap.c
100 */
101int xfs_rtbuf_get(struct xfs_mount *mp, struct xfs_trans *tp,
102 xfs_rtblock_t block, int issum, struct xfs_buf **bpp);
103int xfs_rtcheck_range(struct xfs_mount *mp, struct xfs_trans *tp,
104 xfs_rtblock_t start, xfs_extlen_t len, int val,
105 xfs_rtblock_t *new, int *stat);
106int xfs_rtfind_back(struct xfs_mount *mp, struct xfs_trans *tp,
107 xfs_rtblock_t start, xfs_rtblock_t limit,
108 xfs_rtblock_t *rtblock);
109int xfs_rtfind_forw(struct xfs_mount *mp, struct xfs_trans *tp,
110 xfs_rtblock_t start, xfs_rtblock_t limit,
111 xfs_rtblock_t *rtblock);
112int xfs_rtmodify_range(struct xfs_mount *mp, struct xfs_trans *tp,
113 xfs_rtblock_t start, xfs_extlen_t len, int val);
114int xfs_rtmodify_summary(struct xfs_mount *mp, struct xfs_trans *tp, int log,
115 xfs_rtblock_t bbno, int delta, xfs_buf_t **rbpp,
116 xfs_fsblock_t *rsb);
117int xfs_rtfree_range(struct xfs_mount *mp, struct xfs_trans *tp,
118 xfs_rtblock_t start, xfs_extlen_t len,
119 struct xfs_buf **rbpp, xfs_fsblock_t *rsb);
120
121
98#else 122#else
99# define xfs_rtallocate_extent(t,b,min,max,l,a,f,p,rb) (ENOSYS) 123# define xfs_rtallocate_extent(t,b,min,max,l,a,f,p,rb) (ENOSYS)
100# define xfs_rtfree_extent(t,b,l) (ENOSYS) 124# define xfs_rtfree_extent(t,b,l) (ENOSYS)
diff --git a/fs/xfs/xfs_rtbitmap.c b/fs/xfs/xfs_rtbitmap.c
new file mode 100644
index 000000000000..e30efe8be5e6
--- /dev/null
+++ b/fs/xfs/xfs_rtbitmap.c
@@ -0,0 +1,973 @@
1/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_shared.h"
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
24#include "xfs_bit.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
28#include "xfs_inode.h"
29#include "xfs_bmap.h"
30#include "xfs_bmap_util.h"
31#include "xfs_bmap_btree.h"
32#include "xfs_alloc.h"
33#include "xfs_error.h"
34#include "xfs_trans.h"
35#include "xfs_trans_space.h"
36#include "xfs_trace.h"
37#include "xfs_buf.h"
38#include "xfs_icache.h"
39#include "xfs_dinode.h"
40
41
42/*
43 * Realtime allocator bitmap functions shared with userspace.
44 */
45
46/*
47 * Get a buffer for the bitmap or summary file block specified.
48 * The buffer is returned read and locked.
49 */
50int
51xfs_rtbuf_get(
52 xfs_mount_t *mp, /* file system mount structure */
53 xfs_trans_t *tp, /* transaction pointer */
54 xfs_rtblock_t block, /* block number in bitmap or summary */
55 int issum, /* is summary not bitmap */
56 xfs_buf_t **bpp) /* output: buffer for the block */
57{
58 xfs_buf_t *bp; /* block buffer, result */
59 xfs_inode_t *ip; /* bitmap or summary inode */
60 xfs_bmbt_irec_t map;
61 int nmap = 1;
62 int error; /* error value */
63
64 ip = issum ? mp->m_rsumip : mp->m_rbmip;
65
66 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
67 if (error)
68 return error;
69
70 ASSERT(map.br_startblock != NULLFSBLOCK);
71 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
72 XFS_FSB_TO_DADDR(mp, map.br_startblock),
73 mp->m_bsize, 0, &bp, NULL);
74 if (error)
75 return error;
76 ASSERT(!xfs_buf_geterror(bp));
77 *bpp = bp;
78 return 0;
79}
80
81/*
82 * Searching backward from start to limit, find the first block whose
83 * allocated/free state is different from start's.
84 */
85int
86xfs_rtfind_back(
87 xfs_mount_t *mp, /* file system mount point */
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_rtblock_t start, /* starting block to look at */
90 xfs_rtblock_t limit, /* last block to look at */
91 xfs_rtblock_t *rtblock) /* out: start block found */
92{
93 xfs_rtword_t *b; /* current word in buffer */
94 int bit; /* bit number in the word */
95 xfs_rtblock_t block; /* bitmap block number */
96 xfs_buf_t *bp; /* buf for the block */
97 xfs_rtword_t *bufp; /* starting word in buffer */
98 int error; /* error value */
99 xfs_rtblock_t firstbit; /* first useful bit in the word */
100 xfs_rtblock_t i; /* current bit number rel. to start */
101 xfs_rtblock_t len; /* length of inspected area */
102 xfs_rtword_t mask; /* mask of relevant bits for value */
103 xfs_rtword_t want; /* mask for "good" values */
104 xfs_rtword_t wdiff; /* difference from wanted value */
105 int word; /* word number in the buffer */
106
107 /*
108 * Compute and read in starting bitmap block for starting block.
109 */
110 block = XFS_BITTOBLOCK(mp, start);
111 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
112 if (error) {
113 return error;
114 }
115 bufp = bp->b_addr;
116 /*
117 * Get the first word's index & point to it.
118 */
119 word = XFS_BITTOWORD(mp, start);
120 b = &bufp[word];
121 bit = (int)(start & (XFS_NBWORD - 1));
122 len = start - limit + 1;
123 /*
124 * Compute match value, based on the bit at start: if 1 (free)
125 * then all-ones, else all-zeroes.
126 */
127 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
128 /*
129 * If the starting position is not word-aligned, deal with the
130 * partial word.
131 */
132 if (bit < XFS_NBWORD - 1) {
133 /*
134 * Calculate first (leftmost) bit number to look at,
135 * and mask for all the relevant bits in this word.
136 */
137 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
138 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
139 firstbit;
140 /*
141 * Calculate the difference between the value there
142 * and what we're looking for.
143 */
144 if ((wdiff = (*b ^ want) & mask)) {
145 /*
146 * Different. Mark where we are and return.
147 */
148 xfs_trans_brelse(tp, bp);
149 i = bit - XFS_RTHIBIT(wdiff);
150 *rtblock = start - i + 1;
151 return 0;
152 }
153 i = bit - firstbit + 1;
154 /*
155 * Go on to previous block if that's where the previous word is
156 * and we need the previous word.
157 */
158 if (--word == -1 && i < len) {
159 /*
160 * If done with this block, get the previous one.
161 */
162 xfs_trans_brelse(tp, bp);
163 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
164 if (error) {
165 return error;
166 }
167 bufp = bp->b_addr;
168 word = XFS_BLOCKWMASK(mp);
169 b = &bufp[word];
170 } else {
171 /*
172 * Go on to the previous word in the buffer.
173 */
174 b--;
175 }
176 } else {
177 /*
178 * Starting on a word boundary, no partial word.
179 */
180 i = 0;
181 }
182 /*
183 * Loop over whole words in buffers. When we use up one buffer
184 * we move on to the previous one.
185 */
186 while (len - i >= XFS_NBWORD) {
187 /*
188 * Compute difference between actual and desired value.
189 */
190 if ((wdiff = *b ^ want)) {
191 /*
192 * Different, mark where we are and return.
193 */
194 xfs_trans_brelse(tp, bp);
195 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
196 *rtblock = start - i + 1;
197 return 0;
198 }
199 i += XFS_NBWORD;
200 /*
201 * Go on to previous block if that's where the previous word is
202 * and we need the previous word.
203 */
204 if (--word == -1 && i < len) {
205 /*
206 * If done with this block, get the previous one.
207 */
208 xfs_trans_brelse(tp, bp);
209 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
210 if (error) {
211 return error;
212 }
213 bufp = bp->b_addr;
214 word = XFS_BLOCKWMASK(mp);
215 b = &bufp[word];
216 } else {
217 /*
218 * Go on to the previous word in the buffer.
219 */
220 b--;
221 }
222 }
223 /*
224 * If not ending on a word boundary, deal with the last
225 * (partial) word.
226 */
227 if (len - i) {
228 /*
229 * Calculate first (leftmost) bit number to look at,
230 * and mask for all the relevant bits in this word.
231 */
232 firstbit = XFS_NBWORD - (len - i);
233 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
234 /*
235 * Compute difference between actual and desired value.
236 */
237 if ((wdiff = (*b ^ want) & mask)) {
238 /*
239 * Different, mark where we are and return.
240 */
241 xfs_trans_brelse(tp, bp);
242 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
243 *rtblock = start - i + 1;
244 return 0;
245 } else
246 i = len;
247 }
248 /*
249 * No match, return that we scanned the whole area.
250 */
251 xfs_trans_brelse(tp, bp);
252 *rtblock = start - i + 1;
253 return 0;
254}
255
256/*
257 * Searching forward from start to limit, find the first block whose
258 * allocated/free state is different from start's.
259 */
260int
261xfs_rtfind_forw(
262 xfs_mount_t *mp, /* file system mount point */
263 xfs_trans_t *tp, /* transaction pointer */
264 xfs_rtblock_t start, /* starting block to look at */
265 xfs_rtblock_t limit, /* last block to look at */
266 xfs_rtblock_t *rtblock) /* out: start block found */
267{
268 xfs_rtword_t *b; /* current word in buffer */
269 int bit; /* bit number in the word */
270 xfs_rtblock_t block; /* bitmap block number */
271 xfs_buf_t *bp; /* buf for the block */
272 xfs_rtword_t *bufp; /* starting word in buffer */
273 int error; /* error value */
274 xfs_rtblock_t i; /* current bit number rel. to start */
275 xfs_rtblock_t lastbit; /* last useful bit in the word */
276 xfs_rtblock_t len; /* length of inspected area */
277 xfs_rtword_t mask; /* mask of relevant bits for value */
278 xfs_rtword_t want; /* mask for "good" values */
279 xfs_rtword_t wdiff; /* difference from wanted value */
280 int word; /* word number in the buffer */
281
282 /*
283 * Compute and read in starting bitmap block for starting block.
284 */
285 block = XFS_BITTOBLOCK(mp, start);
286 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
287 if (error) {
288 return error;
289 }
290 bufp = bp->b_addr;
291 /*
292 * Get the first word's index & point to it.
293 */
294 word = XFS_BITTOWORD(mp, start);
295 b = &bufp[word];
296 bit = (int)(start & (XFS_NBWORD - 1));
297 len = limit - start + 1;
298 /*
299 * Compute match value, based on the bit at start: if 1 (free)
300 * then all-ones, else all-zeroes.
301 */
302 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
303 /*
304 * If the starting position is not word-aligned, deal with the
305 * partial word.
306 */
307 if (bit) {
308 /*
309 * Calculate last (rightmost) bit number to look at,
310 * and mask for all the relevant bits in this word.
311 */
312 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
313 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
314 /*
315 * Calculate the difference between the value there
316 * and what we're looking for.
317 */
318 if ((wdiff = (*b ^ want) & mask)) {
319 /*
320 * Different. Mark where we are and return.
321 */
322 xfs_trans_brelse(tp, bp);
323 i = XFS_RTLOBIT(wdiff) - bit;
324 *rtblock = start + i - 1;
325 return 0;
326 }
327 i = lastbit - bit;
328 /*
329 * Go on to next block if that's where the next word is
330 * and we need the next word.
331 */
332 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
333 /*
334 * If done with this block, get the previous one.
335 */
336 xfs_trans_brelse(tp, bp);
337 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
338 if (error) {
339 return error;
340 }
341 b = bufp = bp->b_addr;
342 word = 0;
343 } else {
344 /*
345 * Go on to the previous word in the buffer.
346 */
347 b++;
348 }
349 } else {
350 /*
351 * Starting on a word boundary, no partial word.
352 */
353 i = 0;
354 }
355 /*
356 * Loop over whole words in buffers. When we use up one buffer
357 * we move on to the next one.
358 */
359 while (len - i >= XFS_NBWORD) {
360 /*
361 * Compute difference between actual and desired value.
362 */
363 if ((wdiff = *b ^ want)) {
364 /*
365 * Different, mark where we are and return.
366 */
367 xfs_trans_brelse(tp, bp);
368 i += XFS_RTLOBIT(wdiff);
369 *rtblock = start + i - 1;
370 return 0;
371 }
372 i += XFS_NBWORD;
373 /*
374 * Go on to next block if that's where the next word is
375 * and we need the next word.
376 */
377 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
378 /*
379 * If done with this block, get the next one.
380 */
381 xfs_trans_brelse(tp, bp);
382 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
383 if (error) {
384 return error;
385 }
386 b = bufp = bp->b_addr;
387 word = 0;
388 } else {
389 /*
390 * Go on to the next word in the buffer.
391 */
392 b++;
393 }
394 }
395 /*
396 * If not ending on a word boundary, deal with the last
397 * (partial) word.
398 */
399 if ((lastbit = len - i)) {
400 /*
401 * Calculate mask for all the relevant bits in this word.
402 */
403 mask = ((xfs_rtword_t)1 << lastbit) - 1;
404 /*
405 * Compute difference between actual and desired value.
406 */
407 if ((wdiff = (*b ^ want) & mask)) {
408 /*
409 * Different, mark where we are and return.
410 */
411 xfs_trans_brelse(tp, bp);
412 i += XFS_RTLOBIT(wdiff);
413 *rtblock = start + i - 1;
414 return 0;
415 } else
416 i = len;
417 }
418 /*
419 * No match, return that we scanned the whole area.
420 */
421 xfs_trans_brelse(tp, bp);
422 *rtblock = start + i - 1;
423 return 0;
424}
425
426/*
427 * Read and modify the summary information for a given extent size,
428 * bitmap block combination.
429 * Keeps track of a current summary block, so we don't keep reading
430 * it from the buffer cache.
431 */
432int
433xfs_rtmodify_summary(
434 xfs_mount_t *mp, /* file system mount point */
435 xfs_trans_t *tp, /* transaction pointer */
436 int log, /* log2 of extent size */
437 xfs_rtblock_t bbno, /* bitmap block number */
438 int delta, /* change to make to summary info */
439 xfs_buf_t **rbpp, /* in/out: summary block buffer */
440 xfs_fsblock_t *rsb) /* in/out: summary block number */
441{
442 xfs_buf_t *bp; /* buffer for the summary block */
443 int error; /* error value */
444 xfs_fsblock_t sb; /* summary fsblock */
445 int so; /* index into the summary file */
446 xfs_suminfo_t *sp; /* pointer to returned data */
447
448 /*
449 * Compute entry number in the summary file.
450 */
451 so = XFS_SUMOFFS(mp, log, bbno);
452 /*
453 * Compute the block number in the summary file.
454 */
455 sb = XFS_SUMOFFSTOBLOCK(mp, so);
456 /*
457 * If we have an old buffer, and the block number matches, use that.
458 */
459 if (rbpp && *rbpp && *rsb == sb)
460 bp = *rbpp;
461 /*
462 * Otherwise we have to get the buffer.
463 */
464 else {
465 /*
466 * If there was an old one, get rid of it first.
467 */
468 if (rbpp && *rbpp)
469 xfs_trans_brelse(tp, *rbpp);
470 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
471 if (error) {
472 return error;
473 }
474 /*
475 * Remember this buffer and block for the next call.
476 */
477 if (rbpp) {
478 *rbpp = bp;
479 *rsb = sb;
480 }
481 }
482 /*
483 * Point to the summary information, modify and log it.
484 */
485 sp = XFS_SUMPTR(mp, bp, so);
486 *sp += delta;
487 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
488 (uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
489 return 0;
490}
491
492/*
493 * Set the given range of bitmap bits to the given value.
494 * Do whatever I/O and logging is required.
495 */
496int
497xfs_rtmodify_range(
498 xfs_mount_t *mp, /* file system mount point */
499 xfs_trans_t *tp, /* transaction pointer */
500 xfs_rtblock_t start, /* starting block to modify */
501 xfs_extlen_t len, /* length of extent to modify */
502 int val) /* 1 for free, 0 for allocated */
503{
504 xfs_rtword_t *b; /* current word in buffer */
505 int bit; /* bit number in the word */
506 xfs_rtblock_t block; /* bitmap block number */
507 xfs_buf_t *bp; /* buf for the block */
508 xfs_rtword_t *bufp; /* starting word in buffer */
509 int error; /* error value */
510 xfs_rtword_t *first; /* first used word in the buffer */
511 int i; /* current bit number rel. to start */
512 int lastbit; /* last useful bit in word */
513 xfs_rtword_t mask; /* mask o frelevant bits for value */
514 int word; /* word number in the buffer */
515
516 /*
517 * Compute starting bitmap block number.
518 */
519 block = XFS_BITTOBLOCK(mp, start);
520 /*
521 * Read the bitmap block, and point to its data.
522 */
523 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
524 if (error) {
525 return error;
526 }
527 bufp = bp->b_addr;
528 /*
529 * Compute the starting word's address, and starting bit.
530 */
531 word = XFS_BITTOWORD(mp, start);
532 first = b = &bufp[word];
533 bit = (int)(start & (XFS_NBWORD - 1));
534 /*
535 * 0 (allocated) => all zeroes; 1 (free) => all ones.
536 */
537 val = -val;
538 /*
539 * If not starting on a word boundary, deal with the first
540 * (partial) word.
541 */
542 if (bit) {
543 /*
544 * Compute first bit not changed and mask of relevant bits.
545 */
546 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
547 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
548 /*
549 * Set/clear the active bits.
550 */
551 if (val)
552 *b |= mask;
553 else
554 *b &= ~mask;
555 i = lastbit - bit;
556 /*
557 * Go on to the next block if that's where the next word is
558 * and we need the next word.
559 */
560 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
561 /*
562 * Log the changed part of this block.
563 * Get the next one.
564 */
565 xfs_trans_log_buf(tp, bp,
566 (uint)((char *)first - (char *)bufp),
567 (uint)((char *)b - (char *)bufp));
568 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
569 if (error) {
570 return error;
571 }
572 first = b = bufp = bp->b_addr;
573 word = 0;
574 } else {
575 /*
576 * Go on to the next word in the buffer
577 */
578 b++;
579 }
580 } else {
581 /*
582 * Starting on a word boundary, no partial word.
583 */
584 i = 0;
585 }
586 /*
587 * Loop over whole words in buffers. When we use up one buffer
588 * we move on to the next one.
589 */
590 while (len - i >= XFS_NBWORD) {
591 /*
592 * Set the word value correctly.
593 */
594 *b = val;
595 i += XFS_NBWORD;
596 /*
597 * Go on to the next block if that's where the next word is
598 * and we need the next word.
599 */
600 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
601 /*
602 * Log the changed part of this block.
603 * Get the next one.
604 */
605 xfs_trans_log_buf(tp, bp,
606 (uint)((char *)first - (char *)bufp),
607 (uint)((char *)b - (char *)bufp));
608 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
609 if (error) {
610 return error;
611 }
612 first = b = bufp = bp->b_addr;
613 word = 0;
614 } else {
615 /*
616 * Go on to the next word in the buffer
617 */
618 b++;
619 }
620 }
621 /*
622 * If not ending on a word boundary, deal with the last
623 * (partial) word.
624 */
625 if ((lastbit = len - i)) {
626 /*
627 * Compute a mask of relevant bits.
628 */
629 bit = 0;
630 mask = ((xfs_rtword_t)1 << lastbit) - 1;
631 /*
632 * Set/clear the active bits.
633 */
634 if (val)
635 *b |= mask;
636 else
637 *b &= ~mask;
638 b++;
639 }
640 /*
641 * Log any remaining changed bytes.
642 */
643 if (b > first)
644 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
645 (uint)((char *)b - (char *)bufp - 1));
646 return 0;
647}
648
649/*
650 * Mark an extent specified by start and len freed.
651 * Updates all the summary information as well as the bitmap.
652 */
653int
654xfs_rtfree_range(
655 xfs_mount_t *mp, /* file system mount point */
656 xfs_trans_t *tp, /* transaction pointer */
657 xfs_rtblock_t start, /* starting block to free */
658 xfs_extlen_t len, /* length to free */
659 xfs_buf_t **rbpp, /* in/out: summary block buffer */
660 xfs_fsblock_t *rsb) /* in/out: summary block number */
661{
662 xfs_rtblock_t end; /* end of the freed extent */
663 int error; /* error value */
664 xfs_rtblock_t postblock; /* first block freed > end */
665 xfs_rtblock_t preblock; /* first block freed < start */
666
667 end = start + len - 1;
668 /*
669 * Modify the bitmap to mark this extent freed.
670 */
671 error = xfs_rtmodify_range(mp, tp, start, len, 1);
672 if (error) {
673 return error;
674 }
675 /*
676 * Assume we're freeing out of the middle of an allocated extent.
677 * We need to find the beginning and end of the extent so we can
678 * properly update the summary.
679 */
680 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
681 if (error) {
682 return error;
683 }
684 /*
685 * Find the next allocated block (end of allocated extent).
686 */
687 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
688 &postblock);
689 if (error)
690 return error;
691 /*
692 * If there are blocks not being freed at the front of the
693 * old extent, add summary data for them to be allocated.
694 */
695 if (preblock < start) {
696 error = xfs_rtmodify_summary(mp, tp,
697 XFS_RTBLOCKLOG(start - preblock),
698 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
699 if (error) {
700 return error;
701 }
702 }
703 /*
704 * If there are blocks not being freed at the end of the
705 * old extent, add summary data for them to be allocated.
706 */
707 if (postblock > end) {
708 error = xfs_rtmodify_summary(mp, tp,
709 XFS_RTBLOCKLOG(postblock - end),
710 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
711 if (error) {
712 return error;
713 }
714 }
715 /*
716 * Increment the summary information corresponding to the entire
717 * (new) free extent.
718 */
719 error = xfs_rtmodify_summary(mp, tp,
720 XFS_RTBLOCKLOG(postblock + 1 - preblock),
721 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
722 return error;
723}
724
725/*
726 * Check that the given range is either all allocated (val = 0) or
727 * all free (val = 1).
728 */
729int
730xfs_rtcheck_range(
731 xfs_mount_t *mp, /* file system mount point */
732 xfs_trans_t *tp, /* transaction pointer */
733 xfs_rtblock_t start, /* starting block number of extent */
734 xfs_extlen_t len, /* length of extent */
735 int val, /* 1 for free, 0 for allocated */
736 xfs_rtblock_t *new, /* out: first block not matching */
737 int *stat) /* out: 1 for matches, 0 for not */
738{
739 xfs_rtword_t *b; /* current word in buffer */
740 int bit; /* bit number in the word */
741 xfs_rtblock_t block; /* bitmap block number */
742 xfs_buf_t *bp; /* buf for the block */
743 xfs_rtword_t *bufp; /* starting word in buffer */
744 int error; /* error value */
745 xfs_rtblock_t i; /* current bit number rel. to start */
746 xfs_rtblock_t lastbit; /* last useful bit in word */
747 xfs_rtword_t mask; /* mask of relevant bits for value */
748 xfs_rtword_t wdiff; /* difference from wanted value */
749 int word; /* word number in the buffer */
750
751 /*
752 * Compute starting bitmap block number
753 */
754 block = XFS_BITTOBLOCK(mp, start);
755 /*
756 * Read the bitmap block.
757 */
758 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
759 if (error) {
760 return error;
761 }
762 bufp = bp->b_addr;
763 /*
764 * Compute the starting word's address, and starting bit.
765 */
766 word = XFS_BITTOWORD(mp, start);
767 b = &bufp[word];
768 bit = (int)(start & (XFS_NBWORD - 1));
769 /*
770 * 0 (allocated) => all zero's; 1 (free) => all one's.
771 */
772 val = -val;
773 /*
774 * If not starting on a word boundary, deal with the first
775 * (partial) word.
776 */
777 if (bit) {
778 /*
779 * Compute first bit not examined.
780 */
781 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
782 /*
783 * Mask of relevant bits.
784 */
785 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
786 /*
787 * Compute difference between actual and desired value.
788 */
789 if ((wdiff = (*b ^ val) & mask)) {
790 /*
791 * Different, compute first wrong bit and return.
792 */
793 xfs_trans_brelse(tp, bp);
794 i = XFS_RTLOBIT(wdiff) - bit;
795 *new = start + i;
796 *stat = 0;
797 return 0;
798 }
799 i = lastbit - bit;
800 /*
801 * Go on to next block if that's where the next word is
802 * and we need the next word.
803 */
804 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
805 /*
806 * If done with this block, get the next one.
807 */
808 xfs_trans_brelse(tp, bp);
809 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
810 if (error) {
811 return error;
812 }
813 b = bufp = bp->b_addr;
814 word = 0;
815 } else {
816 /*
817 * Go on to the next word in the buffer.
818 */
819 b++;
820 }
821 } else {
822 /*
823 * Starting on a word boundary, no partial word.
824 */
825 i = 0;
826 }
827 /*
828 * Loop over whole words in buffers. When we use up one buffer
829 * we move on to the next one.
830 */
831 while (len - i >= XFS_NBWORD) {
832 /*
833 * Compute difference between actual and desired value.
834 */
835 if ((wdiff = *b ^ val)) {
836 /*
837 * Different, compute first wrong bit and return.
838 */
839 xfs_trans_brelse(tp, bp);
840 i += XFS_RTLOBIT(wdiff);
841 *new = start + i;
842 *stat = 0;
843 return 0;
844 }
845 i += XFS_NBWORD;
846 /*
847 * Go on to next block if that's where the next word is
848 * and we need the next word.
849 */
850 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
851 /*
852 * If done with this block, get the next one.
853 */
854 xfs_trans_brelse(tp, bp);
855 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
856 if (error) {
857 return error;
858 }
859 b = bufp = bp->b_addr;
860 word = 0;
861 } else {
862 /*
863 * Go on to the next word in the buffer.
864 */
865 b++;
866 }
867 }
868 /*
869 * If not ending on a word boundary, deal with the last
870 * (partial) word.
871 */
872 if ((lastbit = len - i)) {
873 /*
874 * Mask of relevant bits.
875 */
876 mask = ((xfs_rtword_t)1 << lastbit) - 1;
877 /*
878 * Compute difference between actual and desired value.
879 */
880 if ((wdiff = (*b ^ val) & mask)) {
881 /*
882 * Different, compute first wrong bit and return.
883 */
884 xfs_trans_brelse(tp, bp);
885 i += XFS_RTLOBIT(wdiff);
886 *new = start + i;
887 *stat = 0;
888 return 0;
889 } else
890 i = len;
891 }
892 /*
893 * Successful, return.
894 */
895 xfs_trans_brelse(tp, bp);
896 *new = start + i;
897 *stat = 1;
898 return 0;
899}
900
901#ifdef DEBUG
902/*
903 * Check that the given extent (block range) is allocated already.
904 */
905STATIC int /* error */
906xfs_rtcheck_alloc_range(
907 xfs_mount_t *mp, /* file system mount point */
908 xfs_trans_t *tp, /* transaction pointer */
909 xfs_rtblock_t bno, /* starting block number of extent */
910 xfs_extlen_t len) /* length of extent */
911{
912 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
913 int stat;
914 int error;
915
916 error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
917 if (error)
918 return error;
919 ASSERT(stat);
920 return 0;
921}
922#else
923#define xfs_rtcheck_alloc_range(m,t,b,l) (0)
924#endif
925/*
926 * Free an extent in the realtime subvolume. Length is expressed in
927 * realtime extents, as is the block number.
928 */
929int /* error */
930xfs_rtfree_extent(
931 xfs_trans_t *tp, /* transaction pointer */
932 xfs_rtblock_t bno, /* starting block number to free */
933 xfs_extlen_t len) /* length of extent freed */
934{
935 int error; /* error value */
936 xfs_mount_t *mp; /* file system mount structure */
937 xfs_fsblock_t sb; /* summary file block number */
938 xfs_buf_t *sumbp = NULL; /* summary file block buffer */
939
940 mp = tp->t_mountp;
941
942 ASSERT(mp->m_rbmip->i_itemp != NULL);
943 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
944
945 error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
946 if (error)
947 return error;
948
949 /*
950 * Free the range of realtime blocks.
951 */
952 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
953 if (error) {
954 return error;
955 }
956 /*
957 * Mark more blocks free in the superblock.
958 */
959 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
960 /*
961 * If we've now freed all the blocks, reset the file sequence
962 * number to 0.
963 */
964 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
965 mp->m_sb.sb_rextents) {
966 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
967 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
968 *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
969 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
970 }
971 return 0;
972}
973