aboutsummaryrefslogtreecommitdiffstats
path: root/fs/xfs/xfs_rtalloc.c
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 /fs/xfs/xfs_rtalloc.c
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>
Diffstat (limited to 'fs/xfs/xfs_rtalloc.c')
-rw-r--r--fs/xfs/xfs_rtalloc.c1537
1 files changed, 290 insertions, 1247 deletions
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 */