2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_alloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_fsops.h"
45 #include "xfs_error.h"
47 #include "xfs_inode_item.h"
48 #include "xfs_trans_space.h"
52 * Prototypes for internal functions.
56 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
57 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
58 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
59 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
60 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
61 xfs_extlen_t, int, xfs_rtblock_t *, int *);
62 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
63 xfs_rtblock_t, xfs_rtblock_t *);
64 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
65 xfs_rtblock_t, xfs_rtblock_t *);
66 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
67 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
68 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
70 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
71 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
78 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
90 * Allocate space to the bitmap or summary file, and zero it, for growfs.
92 STATIC int /* error */
94 xfs_mount_t *mp, /* file system mount point */
95 xfs_extlen_t oblocks, /* old count of blocks */
96 xfs_extlen_t nblocks, /* new count of blocks */
97 xfs_ino_t ino) /* inode number (bitmap/summary) */
99 xfs_fileoff_t bno; /* block number in file */
100 xfs_buf_t *bp; /* temporary buffer for zeroing */
101 int cancelflags; /* flags for xfs_trans_cancel */
102 int committed; /* transaction committed flag */
103 xfs_daddr_t d; /* disk block address */
104 int error; /* error return value */
105 xfs_fsblock_t firstblock; /* first block allocated in xaction */
106 xfs_bmap_free_t flist; /* list of freed blocks */
107 xfs_fsblock_t fsbno; /* filesystem block for bno */
108 xfs_inode_t *ip; /* pointer to incore inode */
109 xfs_bmbt_irec_t map; /* block map output */
110 int nmap; /* number of block maps */
111 int resblks; /* space reservation */
112 xfs_trans_t *tp; /* transaction pointer */
115 * Allocate space to the file, as necessary.
117 while (oblocks < nblocks) {
118 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
119 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
122 * Reserve space & log for one extent added to the file.
124 if ((error = xfs_trans_reserve(tp, resblks,
125 XFS_GROWRTALLOC_LOG_RES(mp), 0,
126 XFS_TRANS_PERM_LOG_RES,
127 XFS_DEFAULT_PERM_LOG_COUNT)))
129 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
133 if ((error = xfs_trans_iget(mp, tp, ino, 0,
134 XFS_ILOCK_EXCL, &ip)))
136 XFS_BMAP_INIT(&flist, &firstblock);
138 * Allocate blocks to the bitmap file.
141 cancelflags |= XFS_TRANS_ABORT;
142 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
143 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
144 resblks, &map, &nmap, &flist);
145 if (!error && nmap < 1)
146 error = XFS_ERROR(ENOSPC);
150 * Free any blocks freed up in the transaction, then commit.
152 error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
155 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
157 * Now we need to clear the allocated blocks.
158 * Do this one block per transaction, to keep it simple.
161 for (bno = map.br_startoff, fsbno = map.br_startblock;
162 bno < map.br_startoff + map.br_blockcount;
164 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
166 * Reserve log for one block zeroing.
168 if ((error = xfs_trans_reserve(tp, 0,
169 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
172 * Lock the bitmap inode.
174 if ((error = xfs_trans_iget(mp, tp, ino, 0,
175 XFS_ILOCK_EXCL, &ip)))
178 * Get a buffer for the block.
180 d = XFS_FSB_TO_DADDR(mp, fsbno);
181 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
184 error = XFS_ERROR(EIO);
187 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
188 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
190 * Commit the transaction.
192 xfs_trans_commit(tp, 0, NULL);
195 * Go on to the next extent, if any.
197 oblocks = map.br_startoff + map.br_blockcount;
201 xfs_trans_cancel(tp, cancelflags);
206 * Attempt to allocate an extent minlen<=len<=maxlen starting from
207 * bitmap block bbno. If we don't get maxlen then use prod to trim
208 * the length, if given. Returns error; returns starting block in *rtblock.
209 * The lengths are all in rtextents.
211 STATIC int /* error */
212 xfs_rtallocate_extent_block(
213 xfs_mount_t *mp, /* file system mount point */
214 xfs_trans_t *tp, /* transaction pointer */
215 xfs_rtblock_t bbno, /* bitmap block number */
216 xfs_extlen_t minlen, /* minimum length to allocate */
217 xfs_extlen_t maxlen, /* maximum length to allocate */
218 xfs_extlen_t *len, /* out: actual length allocated */
219 xfs_rtblock_t *nextp, /* out: next block to try */
220 xfs_buf_t **rbpp, /* in/out: summary block buffer */
221 xfs_fsblock_t *rsb, /* in/out: summary block number */
222 xfs_extlen_t prod, /* extent product factor */
223 xfs_rtblock_t *rtblock) /* out: start block allocated */
225 xfs_rtblock_t besti; /* best rtblock found so far */
226 xfs_rtblock_t bestlen; /* best length found so far */
227 xfs_rtblock_t end; /* last rtblock in chunk */
228 int error; /* error value */
229 xfs_rtblock_t i; /* current rtblock trying */
230 xfs_rtblock_t next; /* next rtblock to try */
231 int stat; /* status from internal calls */
234 * Loop over all the extents starting in this bitmap block,
235 * looking for one that's long enough.
237 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
238 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
242 * See if there's a free extent of maxlen starting at i.
243 * If it's not so then next will contain the first non-free.
245 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
251 * i for maxlen is all free, allocate and return that.
253 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
263 * In the case where we have a variable-sized allocation
264 * request, figure out how big this free piece is,
265 * and if it's big enough for the minimum, and the best
266 * so far, remember it.
268 if (minlen < maxlen) {
269 xfs_rtblock_t thislen; /* this extent size */
272 if (thislen >= minlen && thislen > bestlen) {
278 * If not done yet, find the start of the next free space.
281 error = xfs_rtfind_forw(mp, tp, next, end, &i);
289 * Searched the whole thing & didn't find a maxlen free extent.
291 if (minlen < maxlen && besti != -1) {
292 xfs_extlen_t p; /* amount to trim length by */
295 * If size should be a multiple of prod, make that so.
297 if (prod > 1 && (p = do_mod(bestlen, prod)))
300 * Allocate besti for bestlen & return that.
302 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
311 * Allocation failed. Set *nextp to the next block to try.
314 *rtblock = NULLRTBLOCK;
319 * Allocate an extent of length minlen<=len<=maxlen, starting at block
320 * bno. If we don't get maxlen then use prod to trim the length, if given.
321 * Returns error; returns starting block in *rtblock.
322 * The lengths are all in rtextents.
324 STATIC int /* error */
325 xfs_rtallocate_extent_exact(
326 xfs_mount_t *mp, /* file system mount point */
327 xfs_trans_t *tp, /* transaction pointer */
328 xfs_rtblock_t bno, /* starting block number to allocate */
329 xfs_extlen_t minlen, /* minimum length to allocate */
330 xfs_extlen_t maxlen, /* maximum length to allocate */
331 xfs_extlen_t *len, /* out: actual length allocated */
332 xfs_buf_t **rbpp, /* in/out: summary block buffer */
333 xfs_fsblock_t *rsb, /* in/out: summary block number */
334 xfs_extlen_t prod, /* extent product factor */
335 xfs_rtblock_t *rtblock) /* out: start block allocated */
337 int error; /* error value */
338 xfs_extlen_t i; /* extent length trimmed due to prod */
339 int isfree; /* extent is free */
340 xfs_rtblock_t next; /* next block to try (dummy) */
342 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
344 * Check if the range in question (for maxlen) is free.
346 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
352 * If it is, allocate it and return success.
354 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
363 * If not, allocate what there is, if it's at least minlen.
366 if (maxlen < minlen) {
368 * Failed, return failure status.
370 *rtblock = NULLRTBLOCK;
374 * Trim off tail of extent, if prod is specified.
376 if (prod > 1 && (i = maxlen % prod)) {
378 if (maxlen < minlen) {
380 * Now we can't do it, return failure status.
382 *rtblock = NULLRTBLOCK;
387 * Allocate what we can and return it.
389 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
399 * Allocate an extent of length minlen<=len<=maxlen, starting as near
400 * to bno as possible. If we don't get maxlen then use prod to trim
401 * the length, if given. The lengths are all in rtextents.
403 STATIC int /* error */
404 xfs_rtallocate_extent_near(
405 xfs_mount_t *mp, /* file system mount point */
406 xfs_trans_t *tp, /* transaction pointer */
407 xfs_rtblock_t bno, /* starting block number to allocate */
408 xfs_extlen_t minlen, /* minimum length to allocate */
409 xfs_extlen_t maxlen, /* maximum length to allocate */
410 xfs_extlen_t *len, /* out: actual length allocated */
411 xfs_buf_t **rbpp, /* in/out: summary block buffer */
412 xfs_fsblock_t *rsb, /* in/out: summary block number */
413 xfs_extlen_t prod, /* extent product factor */
414 xfs_rtblock_t *rtblock) /* out: start block allocated */
416 int any; /* any useful extents from summary */
417 xfs_rtblock_t bbno; /* bitmap block number */
418 int error; /* error value */
419 int i; /* bitmap block offset (loop control) */
420 int j; /* secondary loop control */
421 int log2len; /* log2 of minlen */
422 xfs_rtblock_t n; /* next block to try */
423 xfs_rtblock_t r; /* result block */
425 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
427 * If the block number given is off the end, silently set it to
430 if (bno >= mp->m_sb.sb_rextents)
431 bno = mp->m_sb.sb_rextents - 1;
433 * Try the exact allocation first.
435 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
436 rbpp, rsb, prod, &r);
441 * If the exact allocation worked, return that.
443 if (r != NULLRTBLOCK) {
447 bbno = XFS_BITTOBLOCK(mp, bno);
449 log2len = xfs_highbit32(minlen);
451 * Loop over all bitmap blocks (bbno + i is current block).
455 * Get summary information of extents of all useful levels
456 * starting in this bitmap block.
458 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
459 bbno + i, rbpp, rsb, &any);
464 * If there are any useful extents starting here, try
469 * On the positive side of the starting location.
473 * Try to allocate an extent starting in
476 error = xfs_rtallocate_extent_block(mp, tp,
477 bbno + i, minlen, maxlen, len, &n, rbpp,
483 * If it worked, return it.
485 if (r != NULLRTBLOCK) {
491 * On the negative side of the starting location.
495 * Loop backwards through the bitmap blocks from
496 * the starting point-1 up to where we are now.
497 * There should be an extent which ends in this
498 * bitmap block and is long enough.
500 for (j = -1; j > i; j--) {
502 * Grab the summary information for
505 error = xfs_rtany_summary(mp, tp,
506 log2len, mp->m_rsumlevels - 1,
507 bbno + j, rbpp, rsb, &any);
512 * If there's no extent given in the
513 * summary that means the extent we
514 * found must carry over from an
515 * earlier block. If there is an
516 * extent given, we've already tried
517 * that allocation, don't do it again.
521 error = xfs_rtallocate_extent_block(mp,
522 tp, bbno + j, minlen, maxlen,
523 len, &n, rbpp, rsb, prod, &r);
528 * If it works, return the extent.
530 if (r != NULLRTBLOCK) {
536 * There weren't intervening bitmap blocks
537 * with a long enough extent, or the
538 * allocation didn't work for some reason
539 * (i.e. it's a little * too short).
540 * Try to allocate from the summary block
543 error = xfs_rtallocate_extent_block(mp, tp,
544 bbno + i, minlen, maxlen, len, &n, rbpp,
550 * If it works, return the extent.
552 if (r != NULLRTBLOCK) {
559 * Loop control. If we were on the positive side, and there's
560 * still more blocks on the negative side, go there.
562 if (i > 0 && (int)bbno - i >= 0)
565 * If positive, and no more negative, but there are more
566 * positive, go there.
568 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
571 * If negative or 0 (just started), and there are positive
572 * blocks to go, go there. The 0 case moves to block 1.
574 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
577 * If negative or 0 and there are more negative blocks,
580 else if (i <= 0 && (int)bbno + i > 0)
583 * Must be done. Return failure.
588 *rtblock = NULLRTBLOCK;
593 * Allocate an extent of length minlen<=len<=maxlen, with no position
594 * specified. If we don't get maxlen then use prod to trim
595 * the length, if given. The lengths are all in rtextents.
597 STATIC int /* error */
598 xfs_rtallocate_extent_size(
599 xfs_mount_t *mp, /* file system mount point */
600 xfs_trans_t *tp, /* transaction pointer */
601 xfs_extlen_t minlen, /* minimum length to allocate */
602 xfs_extlen_t maxlen, /* maximum length to allocate */
603 xfs_extlen_t *len, /* out: actual length allocated */
604 xfs_buf_t **rbpp, /* in/out: summary block buffer */
605 xfs_fsblock_t *rsb, /* in/out: summary block number */
606 xfs_extlen_t prod, /* extent product factor */
607 xfs_rtblock_t *rtblock) /* out: start block allocated */
609 int error; /* error value */
610 int i; /* bitmap block number */
611 int l; /* level number (loop control) */
612 xfs_rtblock_t n; /* next block to be tried */
613 xfs_rtblock_t r; /* result block number */
614 xfs_suminfo_t sum; /* summary information for extents */
616 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
618 * Loop over all the levels starting with maxlen.
619 * At each level, look at all the bitmap blocks, to see if there
620 * are extents starting there that are long enough (>= maxlen).
621 * Note, only on the initial level can the allocation fail if
622 * the summary says there's an extent.
624 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
626 * Loop over all the bitmap blocks.
628 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
630 * Get the summary for this level/block.
632 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
638 * Nothing there, on to the next block.
643 * Try allocating the extent.
645 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
646 maxlen, len, &n, rbpp, rsb, prod, &r);
651 * If it worked, return that.
653 if (r != NULLRTBLOCK) {
658 * If the "next block to try" returned from the
659 * allocator is beyond the next bitmap block,
660 * skip to that bitmap block.
662 if (XFS_BITTOBLOCK(mp, n) > i + 1)
663 i = XFS_BITTOBLOCK(mp, n) - 1;
667 * Didn't find any maxlen blocks. Try smaller ones, unless
668 * we're asking for a fixed size extent.
670 if (minlen > --maxlen) {
671 *rtblock = NULLRTBLOCK;
675 * Loop over sizes, from maxlen down to minlen.
676 * This time, when we do the allocations, allow smaller ones
679 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
681 * Loop over all the bitmap blocks, try an allocation
682 * starting in that block.
684 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
686 * Get the summary information for this level/block.
688 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
694 * If nothing there, go on to next.
699 * Try the allocation. Make sure the specified
700 * minlen/maxlen are in the possible range for
701 * this summary level.
703 error = xfs_rtallocate_extent_block(mp, tp, i,
704 XFS_RTMAX(minlen, 1 << l),
705 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
706 len, &n, rbpp, rsb, prod, &r);
711 * If it worked, return that extent.
713 if (r != NULLRTBLOCK) {
718 * If the "next block to try" returned from the
719 * allocator is beyond the next bitmap block,
720 * skip to that bitmap block.
722 if (XFS_BITTOBLOCK(mp, n) > i + 1)
723 i = XFS_BITTOBLOCK(mp, n) - 1;
727 * Got nothing, return failure.
729 *rtblock = NULLRTBLOCK;
734 * Mark an extent specified by start and len allocated.
735 * Updates all the summary information as well as the bitmap.
737 STATIC int /* error */
738 xfs_rtallocate_range(
739 xfs_mount_t *mp, /* file system mount point */
740 xfs_trans_t *tp, /* transaction pointer */
741 xfs_rtblock_t start, /* start block to allocate */
742 xfs_extlen_t len, /* length to allocate */
743 xfs_buf_t **rbpp, /* in/out: summary block buffer */
744 xfs_fsblock_t *rsb) /* in/out: summary block number */
746 xfs_rtblock_t end; /* end of the allocated extent */
747 int error; /* error value */
748 xfs_rtblock_t postblock; /* first block allocated > end */
749 xfs_rtblock_t preblock; /* first block allocated < start */
751 end = start + len - 1;
753 * Assume we're allocating out of the middle of a free extent.
754 * We need to find the beginning and end of the extent so we can
755 * properly update the summary.
757 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
762 * Find the next allocated block (end of free extent).
764 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
770 * Decrement the summary information corresponding to the entire
773 error = xfs_rtmodify_summary(mp, tp,
774 XFS_RTBLOCKLOG(postblock + 1 - preblock),
775 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
780 * If there are blocks not being allocated at the front of the
781 * old extent, add summary data for them to be free.
783 if (preblock < start) {
784 error = xfs_rtmodify_summary(mp, tp,
785 XFS_RTBLOCKLOG(start - preblock),
786 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
792 * If there are blocks not being allocated at the end of the
793 * old extent, add summary data for them to be free.
795 if (postblock > end) {
796 error = xfs_rtmodify_summary(mp, tp,
797 XFS_RTBLOCKLOG(postblock - end),
798 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
804 * Modify the bitmap to mark this extent allocated.
806 error = xfs_rtmodify_range(mp, tp, start, len, 0);
811 * Return whether there are any free extents in the size range given
812 * by low and high, for the bitmap block bbno.
814 STATIC int /* error */
816 xfs_mount_t *mp, /* file system mount structure */
817 xfs_trans_t *tp, /* transaction pointer */
818 int low, /* low log2 extent size */
819 int high, /* high log2 extent size */
820 xfs_rtblock_t bbno, /* bitmap block number */
821 xfs_buf_t **rbpp, /* in/out: summary block buffer */
822 xfs_fsblock_t *rsb, /* in/out: summary block number */
823 int *stat) /* out: any good extents here? */
825 int error; /* error value */
826 int log; /* loop counter, log2 of ext. size */
827 xfs_suminfo_t sum; /* summary data */
830 * Loop over logs of extent sizes. Order is irrelevant.
832 for (log = low; log <= high; log++) {
834 * Get one summary datum.
836 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
841 * If there are any, return success.
849 * Found nothing, return failure.
856 * Get a buffer for the bitmap or summary file block specified.
857 * The buffer is returned read and locked.
859 STATIC int /* error */
861 xfs_mount_t *mp, /* file system mount structure */
862 xfs_trans_t *tp, /* transaction pointer */
863 xfs_rtblock_t block, /* block number in bitmap or summary */
864 int issum, /* is summary not bitmap */
865 xfs_buf_t **bpp) /* output: buffer for the block */
867 xfs_buf_t *bp; /* block buffer, result */
868 xfs_daddr_t d; /* disk addr of block */
869 int error; /* error value */
870 xfs_fsblock_t fsb; /* fs block number for block */
871 xfs_inode_t *ip; /* bitmap or summary inode */
873 ip = issum ? mp->m_rsumip : mp->m_rbmip;
875 * Map from the file offset (block) and inode number to the
878 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
882 ASSERT(fsb != NULLFSBLOCK);
884 * Convert to disk address for buffer cache.
886 d = XFS_FSB_TO_DADDR(mp, fsb);
890 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
891 mp->m_bsize, 0, &bp);
895 ASSERT(bp && !XFS_BUF_GETERROR(bp));
902 * Check that the given extent (block range) is allocated already.
904 STATIC int /* error */
905 xfs_rtcheck_alloc_range(
906 xfs_mount_t *mp, /* file system mount point */
907 xfs_trans_t *tp, /* transaction pointer */
908 xfs_rtblock_t bno, /* starting block number of extent */
909 xfs_extlen_t len, /* length of extent */
910 int *stat) /* out: 1 for allocated, 0 for not */
912 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
914 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
920 * Check whether the given block in the bitmap has the given value.
922 STATIC int /* 1 for matches, 0 for not */
924 xfs_mount_t *mp, /* file system mount structure */
925 xfs_trans_t *tp, /* transaction pointer */
926 xfs_rtblock_t start, /* bit (block) to check */
927 int val) /* 1 for free, 0 for allocated */
929 int bit; /* bit number in the word */
930 xfs_rtblock_t block; /* bitmap block number */
931 xfs_buf_t *bp; /* buf for the block */
932 xfs_rtword_t *bufp; /* pointer into the buffer */
934 int error; /* error value */
935 xfs_rtword_t wdiff; /* difference between bit & expected */
936 int word; /* word number in the buffer */
937 xfs_rtword_t wval; /* word value from buffer */
939 block = XFS_BITTOBLOCK(mp, start);
940 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
941 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
942 word = XFS_BITTOWORD(mp, start);
943 bit = (int)(start & (XFS_NBWORD - 1));
945 xfs_trans_brelse(tp, bp);
946 wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
953 * Check that the given extent (block range) is free already.
955 STATIC int /* error */
956 xfs_rtcheck_free_range(
957 xfs_mount_t *mp, /* file system mount point */
958 xfs_trans_t *tp, /* transaction pointer */
959 xfs_rtblock_t bno, /* starting block number of extent */
960 xfs_extlen_t len, /* length of extent */
961 int *stat) /* out: 1 for free, 0 for not */
963 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
965 return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
970 * Check that the given range is either all allocated (val = 0) or
971 * all free (val = 1).
973 STATIC int /* error */
975 xfs_mount_t *mp, /* file system mount point */
976 xfs_trans_t *tp, /* transaction pointer */
977 xfs_rtblock_t start, /* starting block number of extent */
978 xfs_extlen_t len, /* length of extent */
979 int val, /* 1 for free, 0 for allocated */
980 xfs_rtblock_t *new, /* out: first block not matching */
981 int *stat) /* out: 1 for matches, 0 for not */
983 xfs_rtword_t *b; /* current word in buffer */
984 int bit; /* bit number in the word */
985 xfs_rtblock_t block; /* bitmap block number */
986 xfs_buf_t *bp; /* buf for the block */
987 xfs_rtword_t *bufp; /* starting word in buffer */
988 int error; /* error value */
989 xfs_rtblock_t i; /* current bit number rel. to start */
990 xfs_rtblock_t lastbit; /* last useful bit in word */
991 xfs_rtword_t mask; /* mask of relevant bits for value */
992 xfs_rtword_t wdiff; /* difference from wanted value */
993 int word; /* word number in the buffer */
996 * Compute starting bitmap block number
998 block = XFS_BITTOBLOCK(mp, start);
1000 * Read the bitmap block.
1002 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1006 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1008 * Compute the starting word's address, and starting bit.
1010 word = XFS_BITTOWORD(mp, start);
1012 bit = (int)(start & (XFS_NBWORD - 1));
1014 * 0 (allocated) => all zero's; 1 (free) => all one's.
1018 * If not starting on a word boundary, deal with the first
1023 * Compute first bit not examined.
1025 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1027 * Mask of relevant bits.
1029 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1031 * Compute difference between actual and desired value.
1033 if ((wdiff = (*b ^ val) & mask)) {
1035 * Different, compute first wrong bit and return.
1037 xfs_trans_brelse(tp, bp);
1038 i = XFS_RTLOBIT(wdiff) - bit;
1045 * Go on to next block if that's where the next word is
1046 * and we need the next word.
1048 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1050 * If done with this block, get the next one.
1052 xfs_trans_brelse(tp, bp);
1053 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1057 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1061 * Go on to the next word in the buffer.
1067 * Starting on a word boundary, no partial word.
1072 * Loop over whole words in buffers. When we use up one buffer
1073 * we move on to the next one.
1075 while (len - i >= XFS_NBWORD) {
1077 * Compute difference between actual and desired value.
1079 if ((wdiff = *b ^ val)) {
1081 * Different, compute first wrong bit and return.
1083 xfs_trans_brelse(tp, bp);
1084 i += XFS_RTLOBIT(wdiff);
1091 * Go on to next block if that's where the next word is
1092 * and we need the next word.
1094 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1096 * If done with this block, get the next one.
1098 xfs_trans_brelse(tp, bp);
1099 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1103 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1107 * Go on to the next word in the buffer.
1113 * If not ending on a word boundary, deal with the last
1116 if ((lastbit = len - i)) {
1118 * Mask of relevant bits.
1120 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1122 * Compute difference between actual and desired value.
1124 if ((wdiff = (*b ^ val) & mask)) {
1126 * Different, compute first wrong bit and return.
1128 xfs_trans_brelse(tp, bp);
1129 i += XFS_RTLOBIT(wdiff);
1137 * Successful, return.
1139 xfs_trans_brelse(tp, bp);
1146 * Copy and transform the summary file, given the old and new
1147 * parameters in the mount structures.
1149 STATIC int /* error */
1151 xfs_mount_t *omp, /* old file system mount point */
1152 xfs_mount_t *nmp, /* new file system mount point */
1153 xfs_trans_t *tp) /* transaction pointer */
1155 xfs_rtblock_t bbno; /* bitmap block number */
1156 xfs_buf_t *bp; /* summary buffer */
1157 int error; /* error return value */
1158 int log; /* summary level number (log length) */
1159 xfs_suminfo_t sum; /* summary data */
1160 xfs_fsblock_t sumbno; /* summary block number */
1163 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1164 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1165 (xfs_srtblock_t)bbno >= 0;
1167 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1173 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1177 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1188 * Searching backward from start to limit, find the first block whose
1189 * allocated/free state is different from start's.
1191 STATIC int /* error */
1193 xfs_mount_t *mp, /* file system mount point */
1194 xfs_trans_t *tp, /* transaction pointer */
1195 xfs_rtblock_t start, /* starting block to look at */
1196 xfs_rtblock_t limit, /* last block to look at */
1197 xfs_rtblock_t *rtblock) /* out: start block found */
1199 xfs_rtword_t *b; /* current word in buffer */
1200 int bit; /* bit number in the word */
1201 xfs_rtblock_t block; /* bitmap block number */
1202 xfs_buf_t *bp; /* buf for the block */
1203 xfs_rtword_t *bufp; /* starting word in buffer */
1204 int error; /* error value */
1205 xfs_rtblock_t firstbit; /* first useful bit in the word */
1206 xfs_rtblock_t i; /* current bit number rel. to start */
1207 xfs_rtblock_t len; /* length of inspected area */
1208 xfs_rtword_t mask; /* mask of relevant bits for value */
1209 xfs_rtword_t want; /* mask for "good" values */
1210 xfs_rtword_t wdiff; /* difference from wanted value */
1211 int word; /* word number in the buffer */
1214 * Compute and read in starting bitmap block for starting block.
1216 block = XFS_BITTOBLOCK(mp, start);
1217 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1221 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1223 * Get the first word's index & point to it.
1225 word = XFS_BITTOWORD(mp, start);
1227 bit = (int)(start & (XFS_NBWORD - 1));
1228 len = start - limit + 1;
1230 * Compute match value, based on the bit at start: if 1 (free)
1231 * then all-ones, else all-zeroes.
1233 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1235 * If the starting position is not word-aligned, deal with the
1238 if (bit < XFS_NBWORD - 1) {
1240 * Calculate first (leftmost) bit number to look at,
1241 * and mask for all the relevant bits in this word.
1243 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1244 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1247 * Calculate the difference between the value there
1248 * and what we're looking for.
1250 if ((wdiff = (*b ^ want) & mask)) {
1252 * Different. Mark where we are and return.
1254 xfs_trans_brelse(tp, bp);
1255 i = bit - XFS_RTHIBIT(wdiff);
1256 *rtblock = start - i + 1;
1259 i = bit - firstbit + 1;
1261 * Go on to previous block if that's where the previous word is
1262 * and we need the previous word.
1264 if (--word == -1 && i < len) {
1266 * If done with this block, get the previous one.
1268 xfs_trans_brelse(tp, bp);
1269 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1273 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1274 word = XFS_BLOCKWMASK(mp);
1278 * Go on to the previous word in the buffer.
1284 * Starting on a word boundary, no partial word.
1289 * Loop over whole words in buffers. When we use up one buffer
1290 * we move on to the previous one.
1292 while (len - i >= XFS_NBWORD) {
1294 * Compute difference between actual and desired value.
1296 if ((wdiff = *b ^ want)) {
1298 * Different, mark where we are and return.
1300 xfs_trans_brelse(tp, bp);
1301 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1302 *rtblock = start - i + 1;
1307 * Go on to previous block if that's where the previous word is
1308 * and we need the previous word.
1310 if (--word == -1 && i < len) {
1312 * If done with this block, get the previous one.
1314 xfs_trans_brelse(tp, bp);
1315 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1319 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1320 word = XFS_BLOCKWMASK(mp);
1324 * Go on to the previous word in the buffer.
1330 * If not ending on a word boundary, deal with the last
1335 * Calculate first (leftmost) bit number to look at,
1336 * and mask for all the relevant bits in this word.
1338 firstbit = XFS_NBWORD - (len - i);
1339 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1341 * Compute difference between actual and desired value.
1343 if ((wdiff = (*b ^ want) & mask)) {
1345 * Different, mark where we are and return.
1347 xfs_trans_brelse(tp, bp);
1348 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1349 *rtblock = start - i + 1;
1355 * No match, return that we scanned the whole area.
1357 xfs_trans_brelse(tp, bp);
1358 *rtblock = start - i + 1;
1363 * Searching forward from start to limit, find the first block whose
1364 * allocated/free state is different from start's.
1366 STATIC int /* error */
1368 xfs_mount_t *mp, /* file system mount point */
1369 xfs_trans_t *tp, /* transaction pointer */
1370 xfs_rtblock_t start, /* starting block to look at */
1371 xfs_rtblock_t limit, /* last block to look at */
1372 xfs_rtblock_t *rtblock) /* out: start block found */
1374 xfs_rtword_t *b; /* current word in buffer */
1375 int bit; /* bit number in the word */
1376 xfs_rtblock_t block; /* bitmap block number */
1377 xfs_buf_t *bp; /* buf for the block */
1378 xfs_rtword_t *bufp; /* starting word in buffer */
1379 int error; /* error value */
1380 xfs_rtblock_t i; /* current bit number rel. to start */
1381 xfs_rtblock_t lastbit; /* last useful bit in the word */
1382 xfs_rtblock_t len; /* length of inspected area */
1383 xfs_rtword_t mask; /* mask of relevant bits for value */
1384 xfs_rtword_t want; /* mask for "good" values */
1385 xfs_rtword_t wdiff; /* difference from wanted value */
1386 int word; /* word number in the buffer */
1389 * Compute and read in starting bitmap block for starting block.
1391 block = XFS_BITTOBLOCK(mp, start);
1392 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1396 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1398 * Get the first word's index & point to it.
1400 word = XFS_BITTOWORD(mp, start);
1402 bit = (int)(start & (XFS_NBWORD - 1));
1403 len = limit - start + 1;
1405 * Compute match value, based on the bit at start: if 1 (free)
1406 * then all-ones, else all-zeroes.
1408 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1410 * If the starting position is not word-aligned, deal with the
1415 * Calculate last (rightmost) bit number to look at,
1416 * and mask for all the relevant bits in this word.
1418 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1419 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1421 * Calculate the difference between the value there
1422 * and what we're looking for.
1424 if ((wdiff = (*b ^ want) & mask)) {
1426 * Different. Mark where we are and return.
1428 xfs_trans_brelse(tp, bp);
1429 i = XFS_RTLOBIT(wdiff) - bit;
1430 *rtblock = start + i - 1;
1435 * Go on to next block if that's where the next word is
1436 * and we need the next word.
1438 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1440 * If done with this block, get the previous one.
1442 xfs_trans_brelse(tp, bp);
1443 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1447 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1451 * Go on to the previous word in the buffer.
1457 * Starting on a word boundary, no partial word.
1462 * Loop over whole words in buffers. When we use up one buffer
1463 * we move on to the next one.
1465 while (len - i >= XFS_NBWORD) {
1467 * Compute difference between actual and desired value.
1469 if ((wdiff = *b ^ want)) {
1471 * Different, mark where we are and return.
1473 xfs_trans_brelse(tp, bp);
1474 i += XFS_RTLOBIT(wdiff);
1475 *rtblock = start + i - 1;
1480 * Go on to next block if that's where the next word is
1481 * and we need the next word.
1483 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1485 * If done with this block, get the next one.
1487 xfs_trans_brelse(tp, bp);
1488 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1492 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1496 * Go on to the next word in the buffer.
1502 * If not ending on a word boundary, deal with the last
1505 if ((lastbit = len - i)) {
1507 * Calculate mask for all the relevant bits in this word.
1509 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1511 * Compute difference between actual and desired value.
1513 if ((wdiff = (*b ^ want) & mask)) {
1515 * Different, mark where we are and return.
1517 xfs_trans_brelse(tp, bp);
1518 i += XFS_RTLOBIT(wdiff);
1519 *rtblock = start + i - 1;
1525 * No match, return that we scanned the whole area.
1527 xfs_trans_brelse(tp, bp);
1528 *rtblock = start + i - 1;
1533 * Mark an extent specified by start and len freed.
1534 * Updates all the summary information as well as the bitmap.
1536 STATIC int /* error */
1538 xfs_mount_t *mp, /* file system mount point */
1539 xfs_trans_t *tp, /* transaction pointer */
1540 xfs_rtblock_t start, /* starting block to free */
1541 xfs_extlen_t len, /* length to free */
1542 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1543 xfs_fsblock_t *rsb) /* in/out: summary block number */
1545 xfs_rtblock_t end; /* end of the freed extent */
1546 int error; /* error value */
1547 xfs_rtblock_t postblock; /* first block freed > end */
1548 xfs_rtblock_t preblock; /* first block freed < start */
1550 end = start + len - 1;
1552 * Modify the bitmap to mark this extent freed.
1554 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1559 * Assume we're freeing out of the middle of an allocated extent.
1560 * We need to find the beginning and end of the extent so we can
1561 * properly update the summary.
1563 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1568 * Find the next allocated block (end of allocated extent).
1570 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1573 * If there are blocks not being freed at the front of the
1574 * old extent, add summary data for them to be allocated.
1576 if (preblock < start) {
1577 error = xfs_rtmodify_summary(mp, tp,
1578 XFS_RTBLOCKLOG(start - preblock),
1579 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1585 * If there are blocks not being freed at the end of the
1586 * old extent, add summary data for them to be allocated.
1588 if (postblock > end) {
1589 error = xfs_rtmodify_summary(mp, tp,
1590 XFS_RTBLOCKLOG(postblock - end),
1591 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1597 * Increment the summary information corresponding to the entire
1598 * (new) free extent.
1600 error = xfs_rtmodify_summary(mp, tp,
1601 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1602 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1607 * Read and return the summary information for a given extent size,
1608 * bitmap block combination.
1609 * Keeps track of a current summary block, so we don't keep reading
1610 * it from the buffer cache.
1612 STATIC int /* error */
1614 xfs_mount_t *mp, /* file system mount structure */
1615 xfs_trans_t *tp, /* transaction pointer */
1616 int log, /* log2 of extent size */
1617 xfs_rtblock_t bbno, /* bitmap block number */
1618 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1619 xfs_fsblock_t *rsb, /* in/out: summary block number */
1620 xfs_suminfo_t *sum) /* out: summary info for this block */
1622 xfs_buf_t *bp; /* buffer for summary block */
1623 int error; /* error value */
1624 xfs_fsblock_t sb; /* summary fsblock */
1625 int so; /* index into the summary file */
1626 xfs_suminfo_t *sp; /* pointer to returned data */
1629 * Compute entry number in the summary file.
1631 so = XFS_SUMOFFS(mp, log, bbno);
1633 * Compute the block number in the summary file.
1635 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1637 * If we have an old buffer, and the block number matches, use that.
1639 if (rbpp && *rbpp && *rsb == sb)
1642 * Otherwise we have to get the buffer.
1646 * If there was an old one, get rid of it first.
1649 xfs_trans_brelse(tp, *rbpp);
1650 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1655 * Remember this buffer and block for the next call.
1663 * Point to the summary information & copy it out.
1665 sp = XFS_SUMPTR(mp, bp, so);
1668 * Drop the buffer if we're not asked to remember it.
1671 xfs_trans_brelse(tp, bp);
1676 * Set the given range of bitmap bits to the given value.
1677 * Do whatever I/O and logging is required.
1679 STATIC int /* error */
1681 xfs_mount_t *mp, /* file system mount point */
1682 xfs_trans_t *tp, /* transaction pointer */
1683 xfs_rtblock_t start, /* starting block to modify */
1684 xfs_extlen_t len, /* length of extent to modify */
1685 int val) /* 1 for free, 0 for allocated */
1687 xfs_rtword_t *b; /* current word in buffer */
1688 int bit; /* bit number in the word */
1689 xfs_rtblock_t block; /* bitmap block number */
1690 xfs_buf_t *bp; /* buf for the block */
1691 xfs_rtword_t *bufp; /* starting word in buffer */
1692 int error; /* error value */
1693 xfs_rtword_t *first; /* first used word in the buffer */
1694 int i; /* current bit number rel. to start */
1695 int lastbit; /* last useful bit in word */
1696 xfs_rtword_t mask; /* mask o frelevant bits for value */
1697 int word; /* word number in the buffer */
1700 * Compute starting bitmap block number.
1702 block = XFS_BITTOBLOCK(mp, start);
1704 * Read the bitmap block, and point to its data.
1706 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1710 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1712 * Compute the starting word's address, and starting bit.
1714 word = XFS_BITTOWORD(mp, start);
1715 first = b = &bufp[word];
1716 bit = (int)(start & (XFS_NBWORD - 1));
1718 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1722 * If not starting on a word boundary, deal with the first
1727 * Compute first bit not changed and mask of relevant bits.
1729 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1730 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1732 * Set/clear the active bits.
1740 * Go on to the next block if that's where the next word is
1741 * and we need the next word.
1743 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1745 * Log the changed part of this block.
1748 xfs_trans_log_buf(tp, bp,
1749 (uint)((char *)first - (char *)bufp),
1750 (uint)((char *)b - (char *)bufp));
1751 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1755 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1759 * Go on to the next word in the buffer
1765 * Starting on a word boundary, no partial word.
1770 * Loop over whole words in buffers. When we use up one buffer
1771 * we move on to the next one.
1773 while (len - i >= XFS_NBWORD) {
1775 * Set the word value correctly.
1780 * Go on to the next block if that's where the next word is
1781 * and we need the next word.
1783 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1785 * Log the changed part of this block.
1788 xfs_trans_log_buf(tp, bp,
1789 (uint)((char *)first - (char *)bufp),
1790 (uint)((char *)b - (char *)bufp));
1791 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1795 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1799 * Go on to the next word in the buffer
1805 * If not ending on a word boundary, deal with the last
1808 if ((lastbit = len - i)) {
1810 * Compute a mask of relevant bits.
1813 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1815 * Set/clear the active bits.
1824 * Log any remaining changed bytes.
1827 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1828 (uint)((char *)b - (char *)bufp - 1));
1833 * Read and modify the summary information for a given extent size,
1834 * bitmap block combination.
1835 * Keeps track of a current summary block, so we don't keep reading
1836 * it from the buffer cache.
1838 STATIC int /* error */
1839 xfs_rtmodify_summary(
1840 xfs_mount_t *mp, /* file system mount point */
1841 xfs_trans_t *tp, /* transaction pointer */
1842 int log, /* log2 of extent size */
1843 xfs_rtblock_t bbno, /* bitmap block number */
1844 int delta, /* change to make to summary info */
1845 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1846 xfs_fsblock_t *rsb) /* in/out: summary block number */
1848 xfs_buf_t *bp; /* buffer for the summary block */
1849 int error; /* error value */
1850 xfs_fsblock_t sb; /* summary fsblock */
1851 int so; /* index into the summary file */
1852 xfs_suminfo_t *sp; /* pointer to returned data */
1855 * Compute entry number in the summary file.
1857 so = XFS_SUMOFFS(mp, log, bbno);
1859 * Compute the block number in the summary file.
1861 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1863 * If we have an old buffer, and the block number matches, use that.
1865 if (rbpp && *rbpp && *rsb == sb)
1868 * Otherwise we have to get the buffer.
1872 * If there was an old one, get rid of it first.
1875 xfs_trans_brelse(tp, *rbpp);
1876 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1881 * Remember this buffer and block for the next call.
1889 * Point to the summary information, modify and log it.
1891 sp = XFS_SUMPTR(mp, bp, so);
1893 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1894 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1899 * Visible (exported) functions.
1903 * Grow the realtime area of the filesystem.
1907 xfs_mount_t *mp, /* mount point for filesystem */
1908 xfs_growfs_rt_t *in) /* growfs rt input struct */
1910 xfs_rtblock_t bmbno; /* bitmap block number */
1911 xfs_buf_t *bp; /* temporary buffer */
1912 int cancelflags; /* flags for xfs_trans_cancel */
1913 int error; /* error return value */
1914 xfs_inode_t *ip; /* bitmap inode, used as lock */
1915 xfs_mount_t *nmp; /* new (fake) mount structure */
1916 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1917 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1918 xfs_drtbno_t nrextents; /* new number of realtime extents */
1919 uint8_t nrextslog; /* new log2 of sb_rextents */
1920 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1921 uint nrsumlevels; /* new rt summary levels */
1922 uint nrsumsize; /* new size of rt summary, bytes */
1923 xfs_sb_t *nsbp; /* new superblock */
1924 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1925 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1926 xfs_sb_t *sbp; /* old superblock */
1927 xfs_fsblock_t sumbno; /* summary block number */
1928 xfs_trans_t *tp; /* transaction pointer */
1932 * Initial error checking.
1934 if (mp->m_rtdev_targp || mp->m_rbmip == NULL ||
1935 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1936 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1937 return XFS_ERROR(EINVAL);
1939 * Read in the last block of the device, make sure it exists.
1941 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1942 XFS_FSB_TO_BB(mp, in->newblocks - 1),
1943 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1949 * Calculate new parameters. These are the final values to be reached.
1951 nrextents = nrblocks;
1952 do_div(nrextents, in->extsize);
1953 nrbmblocks = roundup_64(nrextents, NBBY * sbp->sb_blocksize);
1954 nrextslog = xfs_highbit32(nrextents);
1955 nrsumlevels = nrextslog + 1;
1956 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1957 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1958 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1960 * New summary size can't be more than half the size of
1961 * the log. This prevents us from getting a log overflow,
1962 * since we'll log basically the whole summary file at once.
1964 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1965 return XFS_ERROR(EINVAL);
1967 * Get the old block counts for bitmap and summary inodes.
1968 * These can't change since other growfs callers are locked out.
1970 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1971 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1973 * Allocate space to the bitmap and summary files, as necessary.
1975 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1976 mp->m_sb.sb_rbmino)))
1978 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1979 mp->m_sb.sb_rsumino)))
1983 * Loop over the bitmap blocks.
1984 * We will do everything one bitmap block at a time.
1985 * Skip the current block if it is exactly full.
1986 * This also deals with the case where there were no rtextents before.
1988 for (bmbno = sbp->sb_rbmblocks -
1989 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1993 * Allocate a new (fake) mount/sb.
1995 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1999 * Calculate new sb and mount fields for this round.
2001 nsbp->sb_rextsize = in->extsize;
2002 nsbp->sb_rbmblocks = bmbno + 1;
2005 nsbp->sb_rbmblocks * NBBY *
2006 nsbp->sb_blocksize * nsbp->sb_rextsize);
2007 nsbp->sb_rextents = nsbp->sb_rblocks;
2008 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
2009 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2010 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2012 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2014 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2015 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2017 * Start a transaction, get the log reservation.
2019 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2021 if ((error = xfs_trans_reserve(tp, 0,
2022 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2025 * Lock out other callers by grabbing the bitmap inode lock.
2027 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2028 XFS_ILOCK_EXCL, &ip)))
2030 ASSERT(ip == mp->m_rbmip);
2032 * Update the bitmap inode's size.
2034 mp->m_rbmip->i_d.di_size =
2035 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2036 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2037 cancelflags |= XFS_TRANS_ABORT;
2039 * Get the summary inode into the transaction.
2041 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
2042 XFS_ILOCK_EXCL, &ip)))
2044 ASSERT(ip == mp->m_rsumip);
2046 * Update the summary inode's size.
2048 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2049 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2051 * Copy summary data from old to new sizes.
2052 * Do this when the real size (not block-aligned) changes.
2054 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2055 mp->m_rsumlevels != nmp->m_rsumlevels) {
2056 error = xfs_rtcopy_summary(mp, nmp, tp);
2061 * Update superblock fields.
2063 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2064 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2065 nsbp->sb_rextsize - sbp->sb_rextsize);
2066 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2067 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2068 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2069 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2070 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2071 nsbp->sb_rblocks - sbp->sb_rblocks);
2072 if (nsbp->sb_rextents != sbp->sb_rextents)
2073 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2074 nsbp->sb_rextents - sbp->sb_rextents);
2075 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2076 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2077 nsbp->sb_rextslog - sbp->sb_rextslog);
2082 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2083 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2087 * Mark more blocks free in the superblock.
2089 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2090 nsbp->sb_rextents - sbp->sb_rextents);
2092 * Free the fake mp structure.
2094 kmem_free(nmp, sizeof(*nmp));
2097 * Update mp values into the real mp structure.
2099 mp->m_rsumlevels = nrsumlevels;
2100 mp->m_rsumsize = nrsumsize;
2102 * Commit the transaction.
2104 xfs_trans_commit(tp, 0, NULL);
2109 * Error paths come here.
2113 kmem_free(nmp, sizeof(*nmp));
2114 xfs_trans_cancel(tp, cancelflags);
2119 * Allocate an extent in the realtime subvolume, with the usual allocation
2120 * parameters. The length units are all in realtime extents, as is the
2121 * result block number.
2124 xfs_rtallocate_extent(
2125 xfs_trans_t *tp, /* transaction pointer */
2126 xfs_rtblock_t bno, /* starting block number to allocate */
2127 xfs_extlen_t minlen, /* minimum length to allocate */
2128 xfs_extlen_t maxlen, /* maximum length to allocate */
2129 xfs_extlen_t *len, /* out: actual length allocated */
2130 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2131 int wasdel, /* was a delayed allocation extent */
2132 xfs_extlen_t prod, /* extent product factor */
2133 xfs_rtblock_t *rtblock) /* out: start block allocated */
2135 int error; /* error value */
2136 xfs_inode_t *ip; /* inode for bitmap file */
2137 xfs_mount_t *mp; /* file system mount structure */
2138 xfs_rtblock_t r; /* result allocated block */
2139 xfs_fsblock_t sb; /* summary file block number */
2140 xfs_buf_t *sumbp; /* summary file block buffer */
2142 ASSERT(minlen > 0 && minlen <= maxlen);
2145 * If prod is set then figure out what to do to minlen and maxlen.
2150 if ((i = maxlen % prod))
2152 if ((i = minlen % prod))
2154 if (maxlen < minlen) {
2155 *rtblock = NULLRTBLOCK;
2160 * Lock out other callers by grabbing the bitmap inode lock.
2162 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2163 XFS_ILOCK_EXCL, &ip)))
2167 * Allocate by size, or near another block, or exactly at some block.
2170 case XFS_ALLOCTYPE_ANY_AG:
2171 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2172 &sumbp, &sb, prod, &r);
2174 case XFS_ALLOCTYPE_NEAR_BNO:
2175 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2176 len, &sumbp, &sb, prod, &r);
2178 case XFS_ALLOCTYPE_THIS_BNO:
2179 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2180 len, &sumbp, &sb, prod, &r);
2189 * If it worked, update the superblock.
2191 if (r != NULLRTBLOCK) {
2192 long slen = (long)*len;
2194 ASSERT(*len >= minlen && *len <= maxlen);
2196 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2198 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2205 * Free an extent in the realtime subvolume. Length is expressed in
2206 * realtime extents, as is the block number.
2210 xfs_trans_t *tp, /* transaction pointer */
2211 xfs_rtblock_t bno, /* starting block number to free */
2212 xfs_extlen_t len) /* length of extent freed */
2214 int error; /* error value */
2215 xfs_inode_t *ip; /* bitmap file inode */
2216 xfs_mount_t *mp; /* file system mount structure */
2217 xfs_fsblock_t sb; /* summary file block number */
2218 xfs_buf_t *sumbp; /* summary file block buffer */
2222 * Synchronize by locking the bitmap inode.
2224 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2225 XFS_ILOCK_EXCL, &ip)))
2227 #if defined(__KERNEL__) && defined(DEBUG)
2229 * Check to see that this whole range is currently allocated.
2232 int stat; /* result from checking range */
2234 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2243 * Free the range of realtime blocks.
2245 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2250 * Mark more blocks free in the superblock.
2252 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2254 * If we've now freed all the blocks, reset the file sequence
2257 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2258 mp->m_sb.sb_rextents) {
2259 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2260 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2261 *(__uint64_t *)&ip->i_d.di_atime = 0;
2262 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2268 * Initialize realtime fields in the mount structure.
2272 xfs_mount_t *mp) /* file system mount structure */
2274 xfs_buf_t *bp; /* buffer for last block of subvolume */
2275 xfs_daddr_t d; /* address of last block of subvolume */
2276 int error; /* error return value */
2277 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2280 if (sbp->sb_rblocks == 0)
2282 if (mp->m_rtdev_targp == NULL) {
2284 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2285 return XFS_ERROR(ENODEV);
2287 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2289 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2291 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2292 mp->m_rbmip = mp->m_rsumip = NULL;
2294 * Check that the realtime section is an ok size.
2296 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2297 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2298 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2299 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2300 (unsigned long long) mp->m_sb.sb_rblocks);
2301 return XFS_ERROR(E2BIG);
2303 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2304 d - XFS_FSB_TO_BB(mp, 1),
2305 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2308 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2309 if (error == ENOSPC)
2310 return XFS_ERROR(E2BIG);
2318 * Get the bitmap and summary inodes into the mount structure
2323 xfs_mount_t *mp) /* file system mount structure */
2325 int error; /* error return value */
2329 if (sbp->sb_rbmino == NULLFSINO)
2331 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2334 ASSERT(mp->m_rbmip != NULL);
2335 ASSERT(sbp->sb_rsumino != NULLFSINO);
2336 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2338 VN_RELE(XFS_ITOV(mp->m_rbmip));
2341 ASSERT(mp->m_rsumip != NULL);
2346 * Pick an extent for allocation at the start of a new realtime file.
2347 * Use the sequence number stored in the atime field of the bitmap inode.
2348 * Translate this to a fraction of the rtextents, and return the product
2349 * of rtextents and the fraction.
2350 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2354 xfs_mount_t *mp, /* file system mount point */
2355 xfs_trans_t *tp, /* transaction pointer */
2356 xfs_extlen_t len, /* allocation length (rtextents) */
2357 xfs_rtblock_t *pick) /* result rt extent */
2359 xfs_rtblock_t b; /* result block */
2360 int error; /* error return value */
2361 xfs_inode_t *ip; /* bitmap incore inode */
2362 int log2; /* log of sequence number */
2363 __uint64_t resid; /* residual after log removed */
2364 __uint64_t seq; /* sequence number of file creation */
2365 __uint64_t *seqp; /* pointer to seqno in inode */
2367 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2368 XFS_ILOCK_EXCL, &ip)))
2370 ASSERT(ip == mp->m_rbmip);
2371 seqp = (__uint64_t *)&ip->i_d.di_atime;
2372 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2373 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2377 if ((log2 = xfs_highbit64(seq)) == -1)
2380 resid = seq - (1ULL << log2);
2381 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2383 if (b >= mp->m_sb.sb_rextents)
2384 b = do_mod(b, mp->m_sb.sb_rextents);
2385 if (b + len > mp->m_sb.sb_rextents)
2386 b = mp->m_sb.sb_rextents - len;
2389 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2396 * Debug code: print out the value of a range in the bitmap.
2400 xfs_mount_t *mp, /* file system mount structure */
2401 xfs_trans_t *tp, /* transaction pointer */
2402 xfs_rtblock_t start, /* starting block to print */
2403 xfs_extlen_t len) /* length to print */
2405 xfs_extlen_t i; /* block number in the extent */
2407 printk("%Ld: ", (long long)start);
2408 for (i = 0; i < len; i++)
2409 printk("%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2414 * Debug code: print the summary file.
2417 xfs_rtprint_summary(
2418 xfs_mount_t *mp, /* file system mount structure */
2419 xfs_trans_t *tp) /* transaction pointer */
2421 xfs_suminfo_t c; /* summary data */
2422 xfs_rtblock_t i; /* bitmap block number */
2423 int l; /* summary information level */
2424 int p; /* flag for printed anything */
2425 xfs_fsblock_t sb; /* summary block number */
2426 xfs_buf_t *sumbp; /* summary block buffer */
2429 for (l = 0; l < mp->m_rsumlevels; l++) {
2430 for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2431 (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2434 printk("%Ld-%Ld:", 1LL << l,
2435 XFS_RTMIN((1LL << l) +
2437 mp->m_sb.sb_rextents));
2440 printk(" %Ld:%d", (long long)i, c);
2447 xfs_trans_brelse(tp, sumbp);