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"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_fsops.h"
43 #include "xfs_error.h"
45 #include "xfs_inode_item.h"
46 #include "xfs_trans_space.h"
50 * Prototypes for internal functions.
54 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
55 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
56 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
57 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
58 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
59 xfs_extlen_t, int, xfs_rtblock_t *, int *);
60 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
61 xfs_rtblock_t, xfs_rtblock_t *);
62 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
63 xfs_rtblock_t, xfs_rtblock_t *);
64 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
65 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
66 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
68 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
69 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
76 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
88 * Allocate space to the bitmap or summary file, and zero it, for growfs.
90 STATIC int /* error */
92 xfs_mount_t *mp, /* file system mount point */
93 xfs_extlen_t oblocks, /* old count of blocks */
94 xfs_extlen_t nblocks, /* new count of blocks */
95 xfs_ino_t ino) /* inode number (bitmap/summary) */
97 xfs_fileoff_t bno; /* block number in file */
98 xfs_buf_t *bp; /* temporary buffer for zeroing */
99 int cancelflags; /* flags for xfs_trans_cancel */
100 int committed; /* transaction committed flag */
101 xfs_daddr_t d; /* disk block address */
102 int error; /* error return value */
103 xfs_fsblock_t firstblock; /* first block allocated in xaction */
104 xfs_bmap_free_t flist; /* list of freed blocks */
105 xfs_fsblock_t fsbno; /* filesystem block for bno */
106 xfs_inode_t *ip; /* pointer to incore inode */
107 xfs_bmbt_irec_t map; /* block map output */
108 int nmap; /* number of block maps */
109 int resblks; /* space reservation */
110 xfs_trans_t *tp; /* transaction pointer */
113 * Allocate space to the file, as necessary.
115 while (oblocks < nblocks) {
116 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
117 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
120 * Reserve space & log for one extent added to the file.
122 if ((error = xfs_trans_reserve(tp, resblks,
123 XFS_GROWRTALLOC_LOG_RES(mp), 0,
124 XFS_TRANS_PERM_LOG_RES,
125 XFS_DEFAULT_PERM_LOG_COUNT)))
127 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
131 if ((error = xfs_trans_iget(mp, tp, ino, 0,
132 XFS_ILOCK_EXCL, &ip)))
134 XFS_BMAP_INIT(&flist, &firstblock);
136 * Allocate blocks to the bitmap file.
139 cancelflags |= XFS_TRANS_ABORT;
140 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
141 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
142 resblks, &map, &nmap, &flist, NULL);
143 if (!error && nmap < 1)
144 error = XFS_ERROR(ENOSPC);
148 * Free any blocks freed up in the transaction, then commit.
150 error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
153 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
155 * Now we need to clear the allocated blocks.
156 * Do this one block per transaction, to keep it simple.
159 for (bno = map.br_startoff, fsbno = map.br_startblock;
160 bno < map.br_startoff + map.br_blockcount;
162 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
164 * Reserve log for one block zeroing.
166 if ((error = xfs_trans_reserve(tp, 0,
167 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
170 * Lock the bitmap inode.
172 if ((error = xfs_trans_iget(mp, tp, ino, 0,
173 XFS_ILOCK_EXCL, &ip)))
176 * Get a buffer for the block.
178 d = XFS_FSB_TO_DADDR(mp, fsbno);
179 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
182 error = XFS_ERROR(EIO);
185 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
186 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
188 * Commit the transaction.
190 xfs_trans_commit(tp, 0, NULL);
193 * Go on to the next extent, if any.
195 oblocks = map.br_startoff + map.br_blockcount;
199 xfs_trans_cancel(tp, cancelflags);
204 * Attempt to allocate an extent minlen<=len<=maxlen starting from
205 * bitmap block bbno. If we don't get maxlen then use prod to trim
206 * the length, if given. Returns error; returns starting block in *rtblock.
207 * The lengths are all in rtextents.
209 STATIC int /* error */
210 xfs_rtallocate_extent_block(
211 xfs_mount_t *mp, /* file system mount point */
212 xfs_trans_t *tp, /* transaction pointer */
213 xfs_rtblock_t bbno, /* bitmap block number */
214 xfs_extlen_t minlen, /* minimum length to allocate */
215 xfs_extlen_t maxlen, /* maximum length to allocate */
216 xfs_extlen_t *len, /* out: actual length allocated */
217 xfs_rtblock_t *nextp, /* out: next block to try */
218 xfs_buf_t **rbpp, /* in/out: summary block buffer */
219 xfs_fsblock_t *rsb, /* in/out: summary block number */
220 xfs_extlen_t prod, /* extent product factor */
221 xfs_rtblock_t *rtblock) /* out: start block allocated */
223 xfs_rtblock_t besti; /* best rtblock found so far */
224 xfs_rtblock_t bestlen; /* best length found so far */
225 xfs_rtblock_t end; /* last rtblock in chunk */
226 int error; /* error value */
227 xfs_rtblock_t i; /* current rtblock trying */
228 xfs_rtblock_t next; /* next rtblock to try */
229 int stat; /* status from internal calls */
232 * Loop over all the extents starting in this bitmap block,
233 * looking for one that's long enough.
235 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
236 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
240 * See if there's a free extent of maxlen starting at i.
241 * If it's not so then next will contain the first non-free.
243 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
249 * i for maxlen is all free, allocate and return that.
251 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
261 * In the case where we have a variable-sized allocation
262 * request, figure out how big this free piece is,
263 * and if it's big enough for the minimum, and the best
264 * so far, remember it.
266 if (minlen < maxlen) {
267 xfs_rtblock_t thislen; /* this extent size */
270 if (thislen >= minlen && thislen > bestlen) {
276 * If not done yet, find the start of the next free space.
279 error = xfs_rtfind_forw(mp, tp, next, end, &i);
287 * Searched the whole thing & didn't find a maxlen free extent.
289 if (minlen < maxlen && besti != -1) {
290 xfs_extlen_t p; /* amount to trim length by */
293 * If size should be a multiple of prod, make that so.
295 if (prod > 1 && (p = do_mod(bestlen, prod)))
298 * Allocate besti for bestlen & return that.
300 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
309 * Allocation failed. Set *nextp to the next block to try.
312 *rtblock = NULLRTBLOCK;
317 * Allocate an extent of length minlen<=len<=maxlen, starting at block
318 * bno. If we don't get maxlen then use prod to trim the length, if given.
319 * Returns error; returns starting block in *rtblock.
320 * The lengths are all in rtextents.
322 STATIC int /* error */
323 xfs_rtallocate_extent_exact(
324 xfs_mount_t *mp, /* file system mount point */
325 xfs_trans_t *tp, /* transaction pointer */
326 xfs_rtblock_t bno, /* starting block number to allocate */
327 xfs_extlen_t minlen, /* minimum length to allocate */
328 xfs_extlen_t maxlen, /* maximum length to allocate */
329 xfs_extlen_t *len, /* out: actual length allocated */
330 xfs_buf_t **rbpp, /* in/out: summary block buffer */
331 xfs_fsblock_t *rsb, /* in/out: summary block number */
332 xfs_extlen_t prod, /* extent product factor */
333 xfs_rtblock_t *rtblock) /* out: start block allocated */
335 int error; /* error value */
336 xfs_extlen_t i; /* extent length trimmed due to prod */
337 int isfree; /* extent is free */
338 xfs_rtblock_t next; /* next block to try (dummy) */
340 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
342 * Check if the range in question (for maxlen) is free.
344 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
350 * If it is, allocate it and return success.
352 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
361 * If not, allocate what there is, if it's at least minlen.
364 if (maxlen < minlen) {
366 * Failed, return failure status.
368 *rtblock = NULLRTBLOCK;
372 * Trim off tail of extent, if prod is specified.
374 if (prod > 1 && (i = maxlen % prod)) {
376 if (maxlen < minlen) {
378 * Now we can't do it, return failure status.
380 *rtblock = NULLRTBLOCK;
385 * Allocate what we can and return it.
387 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
397 * Allocate an extent of length minlen<=len<=maxlen, starting as near
398 * to bno as possible. If we don't get maxlen then use prod to trim
399 * the length, if given. The lengths are all in rtextents.
401 STATIC int /* error */
402 xfs_rtallocate_extent_near(
403 xfs_mount_t *mp, /* file system mount point */
404 xfs_trans_t *tp, /* transaction pointer */
405 xfs_rtblock_t bno, /* starting block number to allocate */
406 xfs_extlen_t minlen, /* minimum length to allocate */
407 xfs_extlen_t maxlen, /* maximum length to allocate */
408 xfs_extlen_t *len, /* out: actual length allocated */
409 xfs_buf_t **rbpp, /* in/out: summary block buffer */
410 xfs_fsblock_t *rsb, /* in/out: summary block number */
411 xfs_extlen_t prod, /* extent product factor */
412 xfs_rtblock_t *rtblock) /* out: start block allocated */
414 int any; /* any useful extents from summary */
415 xfs_rtblock_t bbno; /* bitmap block number */
416 int error; /* error value */
417 int i; /* bitmap block offset (loop control) */
418 int j; /* secondary loop control */
419 int log2len; /* log2 of minlen */
420 xfs_rtblock_t n; /* next block to try */
421 xfs_rtblock_t r; /* result block */
423 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
425 * If the block number given is off the end, silently set it to
428 if (bno >= mp->m_sb.sb_rextents)
429 bno = mp->m_sb.sb_rextents - 1;
431 * Try the exact allocation first.
433 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
434 rbpp, rsb, prod, &r);
439 * If the exact allocation worked, return that.
441 if (r != NULLRTBLOCK) {
445 bbno = XFS_BITTOBLOCK(mp, bno);
447 log2len = xfs_highbit32(minlen);
449 * Loop over all bitmap blocks (bbno + i is current block).
453 * Get summary information of extents of all useful levels
454 * starting in this bitmap block.
456 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
457 bbno + i, rbpp, rsb, &any);
462 * If there are any useful extents starting here, try
467 * On the positive side of the starting location.
471 * Try to allocate an extent starting in
474 error = xfs_rtallocate_extent_block(mp, tp,
475 bbno + i, minlen, maxlen, len, &n, rbpp,
481 * If it worked, return it.
483 if (r != NULLRTBLOCK) {
489 * On the negative side of the starting location.
493 * Loop backwards through the bitmap blocks from
494 * the starting point-1 up to where we are now.
495 * There should be an extent which ends in this
496 * bitmap block and is long enough.
498 for (j = -1; j > i; j--) {
500 * Grab the summary information for
503 error = xfs_rtany_summary(mp, tp,
504 log2len, mp->m_rsumlevels - 1,
505 bbno + j, rbpp, rsb, &any);
510 * If there's no extent given in the
511 * summary that means the extent we
512 * found must carry over from an
513 * earlier block. If there is an
514 * extent given, we've already tried
515 * that allocation, don't do it again.
519 error = xfs_rtallocate_extent_block(mp,
520 tp, bbno + j, minlen, maxlen,
521 len, &n, rbpp, rsb, prod, &r);
526 * If it works, return the extent.
528 if (r != NULLRTBLOCK) {
534 * There weren't intervening bitmap blocks
535 * with a long enough extent, or the
536 * allocation didn't work for some reason
537 * (i.e. it's a little * too short).
538 * Try to allocate from the summary block
541 error = xfs_rtallocate_extent_block(mp, tp,
542 bbno + i, minlen, maxlen, len, &n, rbpp,
548 * If it works, return the extent.
550 if (r != NULLRTBLOCK) {
557 * Loop control. If we were on the positive side, and there's
558 * still more blocks on the negative side, go there.
560 if (i > 0 && (int)bbno - i >= 0)
563 * If positive, and no more negative, but there are more
564 * positive, go there.
566 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
569 * If negative or 0 (just started), and there are positive
570 * blocks to go, go there. The 0 case moves to block 1.
572 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
575 * If negative or 0 and there are more negative blocks,
578 else if (i <= 0 && (int)bbno + i > 0)
581 * Must be done. Return failure.
586 *rtblock = NULLRTBLOCK;
591 * Allocate an extent of length minlen<=len<=maxlen, with no position
592 * specified. If we don't get maxlen then use prod to trim
593 * the length, if given. The lengths are all in rtextents.
595 STATIC int /* error */
596 xfs_rtallocate_extent_size(
597 xfs_mount_t *mp, /* file system mount point */
598 xfs_trans_t *tp, /* transaction pointer */
599 xfs_extlen_t minlen, /* minimum length to allocate */
600 xfs_extlen_t maxlen, /* maximum length to allocate */
601 xfs_extlen_t *len, /* out: actual length allocated */
602 xfs_buf_t **rbpp, /* in/out: summary block buffer */
603 xfs_fsblock_t *rsb, /* in/out: summary block number */
604 xfs_extlen_t prod, /* extent product factor */
605 xfs_rtblock_t *rtblock) /* out: start block allocated */
607 int error; /* error value */
608 int i; /* bitmap block number */
609 int l; /* level number (loop control) */
610 xfs_rtblock_t n; /* next block to be tried */
611 xfs_rtblock_t r; /* result block number */
612 xfs_suminfo_t sum; /* summary information for extents */
614 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
616 * Loop over all the levels starting with maxlen.
617 * At each level, look at all the bitmap blocks, to see if there
618 * are extents starting there that are long enough (>= maxlen).
619 * Note, only on the initial level can the allocation fail if
620 * the summary says there's an extent.
622 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
624 * Loop over all the bitmap blocks.
626 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
628 * Get the summary for this level/block.
630 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
636 * Nothing there, on to the next block.
641 * Try allocating the extent.
643 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
644 maxlen, len, &n, rbpp, rsb, prod, &r);
649 * If it worked, return that.
651 if (r != NULLRTBLOCK) {
656 * If the "next block to try" returned from the
657 * allocator is beyond the next bitmap block,
658 * skip to that bitmap block.
660 if (XFS_BITTOBLOCK(mp, n) > i + 1)
661 i = XFS_BITTOBLOCK(mp, n) - 1;
665 * Didn't find any maxlen blocks. Try smaller ones, unless
666 * we're asking for a fixed size extent.
668 if (minlen > --maxlen) {
669 *rtblock = NULLRTBLOCK;
673 * Loop over sizes, from maxlen down to minlen.
674 * This time, when we do the allocations, allow smaller ones
677 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
679 * Loop over all the bitmap blocks, try an allocation
680 * starting in that block.
682 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
684 * Get the summary information for this level/block.
686 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
692 * If nothing there, go on to next.
697 * Try the allocation. Make sure the specified
698 * minlen/maxlen are in the possible range for
699 * this summary level.
701 error = xfs_rtallocate_extent_block(mp, tp, i,
702 XFS_RTMAX(minlen, 1 << l),
703 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
704 len, &n, rbpp, rsb, prod, &r);
709 * If it worked, return that extent.
711 if (r != NULLRTBLOCK) {
716 * If the "next block to try" returned from the
717 * allocator is beyond the next bitmap block,
718 * skip to that bitmap block.
720 if (XFS_BITTOBLOCK(mp, n) > i + 1)
721 i = XFS_BITTOBLOCK(mp, n) - 1;
725 * Got nothing, return failure.
727 *rtblock = NULLRTBLOCK;
732 * Mark an extent specified by start and len allocated.
733 * Updates all the summary information as well as the bitmap.
735 STATIC int /* error */
736 xfs_rtallocate_range(
737 xfs_mount_t *mp, /* file system mount point */
738 xfs_trans_t *tp, /* transaction pointer */
739 xfs_rtblock_t start, /* start block to allocate */
740 xfs_extlen_t len, /* length to allocate */
741 xfs_buf_t **rbpp, /* in/out: summary block buffer */
742 xfs_fsblock_t *rsb) /* in/out: summary block number */
744 xfs_rtblock_t end; /* end of the allocated extent */
745 int error; /* error value */
746 xfs_rtblock_t postblock; /* first block allocated > end */
747 xfs_rtblock_t preblock; /* first block allocated < start */
749 end = start + len - 1;
751 * Assume we're allocating out of the middle of a free extent.
752 * We need to find the beginning and end of the extent so we can
753 * properly update the summary.
755 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
760 * Find the next allocated block (end of free extent).
762 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
768 * Decrement the summary information corresponding to the entire
771 error = xfs_rtmodify_summary(mp, tp,
772 XFS_RTBLOCKLOG(postblock + 1 - preblock),
773 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
778 * If there are blocks not being allocated at the front of the
779 * old extent, add summary data for them to be free.
781 if (preblock < start) {
782 error = xfs_rtmodify_summary(mp, tp,
783 XFS_RTBLOCKLOG(start - preblock),
784 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
790 * If there are blocks not being allocated at the end of the
791 * old extent, add summary data for them to be free.
793 if (postblock > end) {
794 error = xfs_rtmodify_summary(mp, tp,
795 XFS_RTBLOCKLOG(postblock - end),
796 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
802 * Modify the bitmap to mark this extent allocated.
804 error = xfs_rtmodify_range(mp, tp, start, len, 0);
809 * Return whether there are any free extents in the size range given
810 * by low and high, for the bitmap block bbno.
812 STATIC int /* error */
814 xfs_mount_t *mp, /* file system mount structure */
815 xfs_trans_t *tp, /* transaction pointer */
816 int low, /* low log2 extent size */
817 int high, /* high log2 extent size */
818 xfs_rtblock_t bbno, /* bitmap block number */
819 xfs_buf_t **rbpp, /* in/out: summary block buffer */
820 xfs_fsblock_t *rsb, /* in/out: summary block number */
821 int *stat) /* out: any good extents here? */
823 int error; /* error value */
824 int log; /* loop counter, log2 of ext. size */
825 xfs_suminfo_t sum; /* summary data */
828 * Loop over logs of extent sizes. Order is irrelevant.
830 for (log = low; log <= high; log++) {
832 * Get one summary datum.
834 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
839 * If there are any, return success.
847 * Found nothing, return failure.
854 * Get a buffer for the bitmap or summary file block specified.
855 * The buffer is returned read and locked.
857 STATIC int /* error */
859 xfs_mount_t *mp, /* file system mount structure */
860 xfs_trans_t *tp, /* transaction pointer */
861 xfs_rtblock_t block, /* block number in bitmap or summary */
862 int issum, /* is summary not bitmap */
863 xfs_buf_t **bpp) /* output: buffer for the block */
865 xfs_buf_t *bp; /* block buffer, result */
866 xfs_daddr_t d; /* disk addr of block */
867 int error; /* error value */
868 xfs_fsblock_t fsb; /* fs block number for block */
869 xfs_inode_t *ip; /* bitmap or summary inode */
871 ip = issum ? mp->m_rsumip : mp->m_rbmip;
873 * Map from the file offset (block) and inode number to the
876 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
880 ASSERT(fsb != NULLFSBLOCK);
882 * Convert to disk address for buffer cache.
884 d = XFS_FSB_TO_DADDR(mp, fsb);
888 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
889 mp->m_bsize, 0, &bp);
893 ASSERT(bp && !XFS_BUF_GETERROR(bp));
900 * Check that the given extent (block range) is allocated already.
902 STATIC int /* error */
903 xfs_rtcheck_alloc_range(
904 xfs_mount_t *mp, /* file system mount point */
905 xfs_trans_t *tp, /* transaction pointer */
906 xfs_rtblock_t bno, /* starting block number of extent */
907 xfs_extlen_t len, /* length of extent */
908 int *stat) /* out: 1 for allocated, 0 for not */
910 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
912 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
918 * Check whether the given block in the bitmap has the given value.
920 STATIC int /* 1 for matches, 0 for not */
922 xfs_mount_t *mp, /* file system mount structure */
923 xfs_trans_t *tp, /* transaction pointer */
924 xfs_rtblock_t start, /* bit (block) to check */
925 int val) /* 1 for free, 0 for allocated */
927 int bit; /* bit number in the word */
928 xfs_rtblock_t block; /* bitmap block number */
929 xfs_buf_t *bp; /* buf for the block */
930 xfs_rtword_t *bufp; /* pointer into the buffer */
932 int error; /* error value */
933 xfs_rtword_t wdiff; /* difference between bit & expected */
934 int word; /* word number in the buffer */
935 xfs_rtword_t wval; /* word value from buffer */
937 block = XFS_BITTOBLOCK(mp, start);
938 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
939 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
940 word = XFS_BITTOWORD(mp, start);
941 bit = (int)(start & (XFS_NBWORD - 1));
943 xfs_trans_brelse(tp, bp);
944 wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
951 * Check that the given extent (block range) is free already.
953 STATIC int /* error */
954 xfs_rtcheck_free_range(
955 xfs_mount_t *mp, /* file system mount point */
956 xfs_trans_t *tp, /* transaction pointer */
957 xfs_rtblock_t bno, /* starting block number of extent */
958 xfs_extlen_t len, /* length of extent */
959 int *stat) /* out: 1 for free, 0 for not */
961 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
963 return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
968 * Check that the given range is either all allocated (val = 0) or
969 * all free (val = 1).
971 STATIC int /* error */
973 xfs_mount_t *mp, /* file system mount point */
974 xfs_trans_t *tp, /* transaction pointer */
975 xfs_rtblock_t start, /* starting block number of extent */
976 xfs_extlen_t len, /* length of extent */
977 int val, /* 1 for free, 0 for allocated */
978 xfs_rtblock_t *new, /* out: first block not matching */
979 int *stat) /* out: 1 for matches, 0 for not */
981 xfs_rtword_t *b; /* current word in buffer */
982 int bit; /* bit number in the word */
983 xfs_rtblock_t block; /* bitmap block number */
984 xfs_buf_t *bp; /* buf for the block */
985 xfs_rtword_t *bufp; /* starting word in buffer */
986 int error; /* error value */
987 xfs_rtblock_t i; /* current bit number rel. to start */
988 xfs_rtblock_t lastbit; /* last useful bit in word */
989 xfs_rtword_t mask; /* mask of relevant bits for value */
990 xfs_rtword_t wdiff; /* difference from wanted value */
991 int word; /* word number in the buffer */
994 * Compute starting bitmap block number
996 block = XFS_BITTOBLOCK(mp, start);
998 * Read the bitmap block.
1000 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1004 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1006 * Compute the starting word's address, and starting bit.
1008 word = XFS_BITTOWORD(mp, start);
1010 bit = (int)(start & (XFS_NBWORD - 1));
1012 * 0 (allocated) => all zero's; 1 (free) => all one's.
1016 * If not starting on a word boundary, deal with the first
1021 * Compute first bit not examined.
1023 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1025 * Mask of relevant bits.
1027 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1029 * Compute difference between actual and desired value.
1031 if ((wdiff = (*b ^ val) & mask)) {
1033 * Different, compute first wrong bit and return.
1035 xfs_trans_brelse(tp, bp);
1036 i = XFS_RTLOBIT(wdiff) - bit;
1043 * Go on to next block if that's where the next word is
1044 * and we need the next word.
1046 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1048 * If done with this block, get the next one.
1050 xfs_trans_brelse(tp, bp);
1051 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1055 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1059 * Go on to the next word in the buffer.
1065 * Starting on a word boundary, no partial word.
1070 * Loop over whole words in buffers. When we use up one buffer
1071 * we move on to the next one.
1073 while (len - i >= XFS_NBWORD) {
1075 * Compute difference between actual and desired value.
1077 if ((wdiff = *b ^ val)) {
1079 * Different, compute first wrong bit and return.
1081 xfs_trans_brelse(tp, bp);
1082 i += XFS_RTLOBIT(wdiff);
1089 * Go on to next block if that's where the next word is
1090 * and we need the next word.
1092 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1094 * If done with this block, get the next one.
1096 xfs_trans_brelse(tp, bp);
1097 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1101 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1105 * Go on to the next word in the buffer.
1111 * If not ending on a word boundary, deal with the last
1114 if ((lastbit = len - i)) {
1116 * Mask of relevant bits.
1118 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1120 * Compute difference between actual and desired value.
1122 if ((wdiff = (*b ^ val) & mask)) {
1124 * Different, compute first wrong bit and return.
1126 xfs_trans_brelse(tp, bp);
1127 i += XFS_RTLOBIT(wdiff);
1135 * Successful, return.
1137 xfs_trans_brelse(tp, bp);
1144 * Copy and transform the summary file, given the old and new
1145 * parameters in the mount structures.
1147 STATIC int /* error */
1149 xfs_mount_t *omp, /* old file system mount point */
1150 xfs_mount_t *nmp, /* new file system mount point */
1151 xfs_trans_t *tp) /* transaction pointer */
1153 xfs_rtblock_t bbno; /* bitmap block number */
1154 xfs_buf_t *bp; /* summary buffer */
1155 int error; /* error return value */
1156 int log; /* summary level number (log length) */
1157 xfs_suminfo_t sum; /* summary data */
1158 xfs_fsblock_t sumbno; /* summary block number */
1161 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1162 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1163 (xfs_srtblock_t)bbno >= 0;
1165 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1171 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1175 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1186 * Searching backward from start to limit, find the first block whose
1187 * allocated/free state is different from start's.
1189 STATIC int /* error */
1191 xfs_mount_t *mp, /* file system mount point */
1192 xfs_trans_t *tp, /* transaction pointer */
1193 xfs_rtblock_t start, /* starting block to look at */
1194 xfs_rtblock_t limit, /* last block to look at */
1195 xfs_rtblock_t *rtblock) /* out: start block found */
1197 xfs_rtword_t *b; /* current word in buffer */
1198 int bit; /* bit number in the word */
1199 xfs_rtblock_t block; /* bitmap block number */
1200 xfs_buf_t *bp; /* buf for the block */
1201 xfs_rtword_t *bufp; /* starting word in buffer */
1202 int error; /* error value */
1203 xfs_rtblock_t firstbit; /* first useful bit in the word */
1204 xfs_rtblock_t i; /* current bit number rel. to start */
1205 xfs_rtblock_t len; /* length of inspected area */
1206 xfs_rtword_t mask; /* mask of relevant bits for value */
1207 xfs_rtword_t want; /* mask for "good" values */
1208 xfs_rtword_t wdiff; /* difference from wanted value */
1209 int word; /* word number in the buffer */
1212 * Compute and read in starting bitmap block for starting block.
1214 block = XFS_BITTOBLOCK(mp, start);
1215 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1219 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1221 * Get the first word's index & point to it.
1223 word = XFS_BITTOWORD(mp, start);
1225 bit = (int)(start & (XFS_NBWORD - 1));
1226 len = start - limit + 1;
1228 * Compute match value, based on the bit at start: if 1 (free)
1229 * then all-ones, else all-zeroes.
1231 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1233 * If the starting position is not word-aligned, deal with the
1236 if (bit < XFS_NBWORD - 1) {
1238 * Calculate first (leftmost) bit number to look at,
1239 * and mask for all the relevant bits in this word.
1241 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1242 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1245 * Calculate the difference between the value there
1246 * and what we're looking for.
1248 if ((wdiff = (*b ^ want) & mask)) {
1250 * Different. Mark where we are and return.
1252 xfs_trans_brelse(tp, bp);
1253 i = bit - XFS_RTHIBIT(wdiff);
1254 *rtblock = start - i + 1;
1257 i = bit - firstbit + 1;
1259 * Go on to previous block if that's where the previous word is
1260 * and we need the previous word.
1262 if (--word == -1 && i < len) {
1264 * If done with this block, get the previous one.
1266 xfs_trans_brelse(tp, bp);
1267 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1271 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1272 word = XFS_BLOCKWMASK(mp);
1276 * Go on to the previous word in the buffer.
1282 * Starting on a word boundary, no partial word.
1287 * Loop over whole words in buffers. When we use up one buffer
1288 * we move on to the previous one.
1290 while (len - i >= XFS_NBWORD) {
1292 * Compute difference between actual and desired value.
1294 if ((wdiff = *b ^ want)) {
1296 * Different, mark where we are and return.
1298 xfs_trans_brelse(tp, bp);
1299 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1300 *rtblock = start - i + 1;
1305 * Go on to previous block if that's where the previous word is
1306 * and we need the previous word.
1308 if (--word == -1 && i < len) {
1310 * If done with this block, get the previous one.
1312 xfs_trans_brelse(tp, bp);
1313 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1317 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1318 word = XFS_BLOCKWMASK(mp);
1322 * Go on to the previous word in the buffer.
1328 * If not ending on a word boundary, deal with the last
1333 * Calculate first (leftmost) bit number to look at,
1334 * and mask for all the relevant bits in this word.
1336 firstbit = XFS_NBWORD - (len - i);
1337 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1339 * Compute difference between actual and desired value.
1341 if ((wdiff = (*b ^ want) & mask)) {
1343 * Different, mark where we are and return.
1345 xfs_trans_brelse(tp, bp);
1346 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1347 *rtblock = start - i + 1;
1353 * No match, return that we scanned the whole area.
1355 xfs_trans_brelse(tp, bp);
1356 *rtblock = start - i + 1;
1361 * Searching forward from start to limit, find the first block whose
1362 * allocated/free state is different from start's.
1364 STATIC int /* error */
1366 xfs_mount_t *mp, /* file system mount point */
1367 xfs_trans_t *tp, /* transaction pointer */
1368 xfs_rtblock_t start, /* starting block to look at */
1369 xfs_rtblock_t limit, /* last block to look at */
1370 xfs_rtblock_t *rtblock) /* out: start block found */
1372 xfs_rtword_t *b; /* current word in buffer */
1373 int bit; /* bit number in the word */
1374 xfs_rtblock_t block; /* bitmap block number */
1375 xfs_buf_t *bp; /* buf for the block */
1376 xfs_rtword_t *bufp; /* starting word in buffer */
1377 int error; /* error value */
1378 xfs_rtblock_t i; /* current bit number rel. to start */
1379 xfs_rtblock_t lastbit; /* last useful bit in the word */
1380 xfs_rtblock_t len; /* length of inspected area */
1381 xfs_rtword_t mask; /* mask of relevant bits for value */
1382 xfs_rtword_t want; /* mask for "good" values */
1383 xfs_rtword_t wdiff; /* difference from wanted value */
1384 int word; /* word number in the buffer */
1387 * Compute and read in starting bitmap block for starting block.
1389 block = XFS_BITTOBLOCK(mp, start);
1390 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1394 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1396 * Get the first word's index & point to it.
1398 word = XFS_BITTOWORD(mp, start);
1400 bit = (int)(start & (XFS_NBWORD - 1));
1401 len = limit - start + 1;
1403 * Compute match value, based on the bit at start: if 1 (free)
1404 * then all-ones, else all-zeroes.
1406 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1408 * If the starting position is not word-aligned, deal with the
1413 * Calculate last (rightmost) bit number to look at,
1414 * and mask for all the relevant bits in this word.
1416 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1417 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1419 * Calculate the difference between the value there
1420 * and what we're looking for.
1422 if ((wdiff = (*b ^ want) & mask)) {
1424 * Different. Mark where we are and return.
1426 xfs_trans_brelse(tp, bp);
1427 i = XFS_RTLOBIT(wdiff) - bit;
1428 *rtblock = start + i - 1;
1433 * Go on to next block if that's where the next word is
1434 * and we need the next word.
1436 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1438 * If done with this block, get the previous one.
1440 xfs_trans_brelse(tp, bp);
1441 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1445 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1449 * Go on to the previous word in the buffer.
1455 * Starting on a word boundary, no partial word.
1460 * Loop over whole words in buffers. When we use up one buffer
1461 * we move on to the next one.
1463 while (len - i >= XFS_NBWORD) {
1465 * Compute difference between actual and desired value.
1467 if ((wdiff = *b ^ want)) {
1469 * Different, mark where we are and return.
1471 xfs_trans_brelse(tp, bp);
1472 i += XFS_RTLOBIT(wdiff);
1473 *rtblock = start + i - 1;
1478 * Go on to next block if that's where the next word is
1479 * and we need the next word.
1481 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1483 * If done with this block, get the next one.
1485 xfs_trans_brelse(tp, bp);
1486 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1490 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1494 * Go on to the next word in the buffer.
1500 * If not ending on a word boundary, deal with the last
1503 if ((lastbit = len - i)) {
1505 * Calculate mask for all the relevant bits in this word.
1507 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1509 * Compute difference between actual and desired value.
1511 if ((wdiff = (*b ^ want) & mask)) {
1513 * Different, mark where we are and return.
1515 xfs_trans_brelse(tp, bp);
1516 i += XFS_RTLOBIT(wdiff);
1517 *rtblock = start + i - 1;
1523 * No match, return that we scanned the whole area.
1525 xfs_trans_brelse(tp, bp);
1526 *rtblock = start + i - 1;
1531 * Mark an extent specified by start and len freed.
1532 * Updates all the summary information as well as the bitmap.
1534 STATIC int /* error */
1536 xfs_mount_t *mp, /* file system mount point */
1537 xfs_trans_t *tp, /* transaction pointer */
1538 xfs_rtblock_t start, /* starting block to free */
1539 xfs_extlen_t len, /* length to free */
1540 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1541 xfs_fsblock_t *rsb) /* in/out: summary block number */
1543 xfs_rtblock_t end; /* end of the freed extent */
1544 int error; /* error value */
1545 xfs_rtblock_t postblock; /* first block freed > end */
1546 xfs_rtblock_t preblock; /* first block freed < start */
1548 end = start + len - 1;
1550 * Modify the bitmap to mark this extent freed.
1552 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1557 * Assume we're freeing out of the middle of an allocated extent.
1558 * We need to find the beginning and end of the extent so we can
1559 * properly update the summary.
1561 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1566 * Find the next allocated block (end of allocated extent).
1568 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1571 * If there are blocks not being freed at the front of the
1572 * old extent, add summary data for them to be allocated.
1574 if (preblock < start) {
1575 error = xfs_rtmodify_summary(mp, tp,
1576 XFS_RTBLOCKLOG(start - preblock),
1577 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1583 * If there are blocks not being freed at the end of the
1584 * old extent, add summary data for them to be allocated.
1586 if (postblock > end) {
1587 error = xfs_rtmodify_summary(mp, tp,
1588 XFS_RTBLOCKLOG(postblock - end),
1589 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1595 * Increment the summary information corresponding to the entire
1596 * (new) free extent.
1598 error = xfs_rtmodify_summary(mp, tp,
1599 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1600 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1605 * Read and return the summary information for a given extent size,
1606 * bitmap block combination.
1607 * Keeps track of a current summary block, so we don't keep reading
1608 * it from the buffer cache.
1610 STATIC int /* error */
1612 xfs_mount_t *mp, /* file system mount structure */
1613 xfs_trans_t *tp, /* transaction pointer */
1614 int log, /* log2 of extent size */
1615 xfs_rtblock_t bbno, /* bitmap block number */
1616 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1617 xfs_fsblock_t *rsb, /* in/out: summary block number */
1618 xfs_suminfo_t *sum) /* out: summary info for this block */
1620 xfs_buf_t *bp; /* buffer for summary block */
1621 int error; /* error value */
1622 xfs_fsblock_t sb; /* summary fsblock */
1623 int so; /* index into the summary file */
1624 xfs_suminfo_t *sp; /* pointer to returned data */
1627 * Compute entry number in the summary file.
1629 so = XFS_SUMOFFS(mp, log, bbno);
1631 * Compute the block number in the summary file.
1633 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1635 * If we have an old buffer, and the block number matches, use that.
1637 if (rbpp && *rbpp && *rsb == sb)
1640 * Otherwise we have to get the buffer.
1644 * If there was an old one, get rid of it first.
1647 xfs_trans_brelse(tp, *rbpp);
1648 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1653 * Remember this buffer and block for the next call.
1661 * Point to the summary information & copy it out.
1663 sp = XFS_SUMPTR(mp, bp, so);
1666 * Drop the buffer if we're not asked to remember it.
1669 xfs_trans_brelse(tp, bp);
1674 * Set the given range of bitmap bits to the given value.
1675 * Do whatever I/O and logging is required.
1677 STATIC int /* error */
1679 xfs_mount_t *mp, /* file system mount point */
1680 xfs_trans_t *tp, /* transaction pointer */
1681 xfs_rtblock_t start, /* starting block to modify */
1682 xfs_extlen_t len, /* length of extent to modify */
1683 int val) /* 1 for free, 0 for allocated */
1685 xfs_rtword_t *b; /* current word in buffer */
1686 int bit; /* bit number in the word */
1687 xfs_rtblock_t block; /* bitmap block number */
1688 xfs_buf_t *bp; /* buf for the block */
1689 xfs_rtword_t *bufp; /* starting word in buffer */
1690 int error; /* error value */
1691 xfs_rtword_t *first; /* first used word in the buffer */
1692 int i; /* current bit number rel. to start */
1693 int lastbit; /* last useful bit in word */
1694 xfs_rtword_t mask; /* mask o frelevant bits for value */
1695 int word; /* word number in the buffer */
1698 * Compute starting bitmap block number.
1700 block = XFS_BITTOBLOCK(mp, start);
1702 * Read the bitmap block, and point to its data.
1704 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1708 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1710 * Compute the starting word's address, and starting bit.
1712 word = XFS_BITTOWORD(mp, start);
1713 first = b = &bufp[word];
1714 bit = (int)(start & (XFS_NBWORD - 1));
1716 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1720 * If not starting on a word boundary, deal with the first
1725 * Compute first bit not changed and mask of relevant bits.
1727 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1728 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1730 * Set/clear the active bits.
1738 * Go on to the next block if that's where the next word is
1739 * and we need the next word.
1741 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1743 * Log the changed part of this block.
1746 xfs_trans_log_buf(tp, bp,
1747 (uint)((char *)first - (char *)bufp),
1748 (uint)((char *)b - (char *)bufp));
1749 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1753 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1757 * Go on to the next word in the buffer
1763 * Starting on a word boundary, no partial word.
1768 * Loop over whole words in buffers. When we use up one buffer
1769 * we move on to the next one.
1771 while (len - i >= XFS_NBWORD) {
1773 * Set the word value correctly.
1778 * Go on to the next block if that's where the next word is
1779 * and we need the next word.
1781 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1783 * Log the changed part of this block.
1786 xfs_trans_log_buf(tp, bp,
1787 (uint)((char *)first - (char *)bufp),
1788 (uint)((char *)b - (char *)bufp));
1789 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1793 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1797 * Go on to the next word in the buffer
1803 * If not ending on a word boundary, deal with the last
1806 if ((lastbit = len - i)) {
1808 * Compute a mask of relevant bits.
1811 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1813 * Set/clear the active bits.
1822 * Log any remaining changed bytes.
1825 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1826 (uint)((char *)b - (char *)bufp - 1));
1831 * Read and modify the summary information for a given extent size,
1832 * bitmap block combination.
1833 * Keeps track of a current summary block, so we don't keep reading
1834 * it from the buffer cache.
1836 STATIC int /* error */
1837 xfs_rtmodify_summary(
1838 xfs_mount_t *mp, /* file system mount point */
1839 xfs_trans_t *tp, /* transaction pointer */
1840 int log, /* log2 of extent size */
1841 xfs_rtblock_t bbno, /* bitmap block number */
1842 int delta, /* change to make to summary info */
1843 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1844 xfs_fsblock_t *rsb) /* in/out: summary block number */
1846 xfs_buf_t *bp; /* buffer for the summary block */
1847 int error; /* error value */
1848 xfs_fsblock_t sb; /* summary fsblock */
1849 int so; /* index into the summary file */
1850 xfs_suminfo_t *sp; /* pointer to returned data */
1853 * Compute entry number in the summary file.
1855 so = XFS_SUMOFFS(mp, log, bbno);
1857 * Compute the block number in the summary file.
1859 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1861 * If we have an old buffer, and the block number matches, use that.
1863 if (rbpp && *rbpp && *rsb == sb)
1866 * Otherwise we have to get the buffer.
1870 * If there was an old one, get rid of it first.
1873 xfs_trans_brelse(tp, *rbpp);
1874 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1879 * Remember this buffer and block for the next call.
1887 * Point to the summary information, modify and log it.
1889 sp = XFS_SUMPTR(mp, bp, so);
1891 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1892 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1897 * Visible (exported) functions.
1901 * Grow the realtime area of the filesystem.
1905 xfs_mount_t *mp, /* mount point for filesystem */
1906 xfs_growfs_rt_t *in) /* growfs rt input struct */
1908 xfs_rtblock_t bmbno; /* bitmap block number */
1909 xfs_buf_t *bp; /* temporary buffer */
1910 int cancelflags; /* flags for xfs_trans_cancel */
1911 int error; /* error return value */
1912 xfs_inode_t *ip; /* bitmap inode, used as lock */
1913 xfs_mount_t *nmp; /* new (fake) mount structure */
1914 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1915 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1916 xfs_drtbno_t nrextents; /* new number of realtime extents */
1917 uint8_t nrextslog; /* new log2 of sb_rextents */
1918 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1919 uint nrsumlevels; /* new rt summary levels */
1920 uint nrsumsize; /* new size of rt summary, bytes */
1921 xfs_sb_t *nsbp; /* new superblock */
1922 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1923 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1924 xfs_sb_t *sbp; /* old superblock */
1925 xfs_fsblock_t sumbno; /* summary block number */
1926 xfs_trans_t *tp; /* transaction pointer */
1930 * Initial error checking.
1932 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1933 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1934 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1935 return XFS_ERROR(EINVAL);
1937 * Read in the last block of the device, make sure it exists.
1939 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1940 XFS_FSB_TO_BB(mp, in->newblocks - 1),
1941 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1947 * Calculate new parameters. These are the final values to be reached.
1949 nrextents = nrblocks;
1950 do_div(nrextents, in->extsize);
1951 nrbmblocks = roundup_64(nrextents, NBBY * sbp->sb_blocksize);
1952 nrextslog = xfs_highbit32(nrextents);
1953 nrsumlevels = nrextslog + 1;
1954 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1955 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1956 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1958 * New summary size can't be more than half the size of
1959 * the log. This prevents us from getting a log overflow,
1960 * since we'll log basically the whole summary file at once.
1962 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1963 return XFS_ERROR(EINVAL);
1965 * Get the old block counts for bitmap and summary inodes.
1966 * These can't change since other growfs callers are locked out.
1968 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1969 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1971 * Allocate space to the bitmap and summary files, as necessary.
1973 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1974 mp->m_sb.sb_rbmino)))
1976 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1977 mp->m_sb.sb_rsumino)))
1981 * Loop over the bitmap blocks.
1982 * We will do everything one bitmap block at a time.
1983 * Skip the current block if it is exactly full.
1984 * This also deals with the case where there were no rtextents before.
1986 for (bmbno = sbp->sb_rbmblocks -
1987 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1991 * Allocate a new (fake) mount/sb.
1993 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1997 * Calculate new sb and mount fields for this round.
1999 nsbp->sb_rextsize = in->extsize;
2000 nsbp->sb_rbmblocks = bmbno + 1;
2003 nsbp->sb_rbmblocks * NBBY *
2004 nsbp->sb_blocksize * nsbp->sb_rextsize);
2005 nsbp->sb_rextents = nsbp->sb_rblocks;
2006 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
2007 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2008 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2010 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2012 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2013 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2015 * Start a transaction, get the log reservation.
2017 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2019 if ((error = xfs_trans_reserve(tp, 0,
2020 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2023 * Lock out other callers by grabbing the bitmap inode lock.
2025 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2026 XFS_ILOCK_EXCL, &ip)))
2028 ASSERT(ip == mp->m_rbmip);
2030 * Update the bitmap inode's size.
2032 mp->m_rbmip->i_d.di_size =
2033 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2034 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2035 cancelflags |= XFS_TRANS_ABORT;
2037 * Get the summary inode into the transaction.
2039 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
2040 XFS_ILOCK_EXCL, &ip)))
2042 ASSERT(ip == mp->m_rsumip);
2044 * Update the summary inode's size.
2046 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2047 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2049 * Copy summary data from old to new sizes.
2050 * Do this when the real size (not block-aligned) changes.
2052 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2053 mp->m_rsumlevels != nmp->m_rsumlevels) {
2054 error = xfs_rtcopy_summary(mp, nmp, tp);
2059 * Update superblock fields.
2061 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2062 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2063 nsbp->sb_rextsize - sbp->sb_rextsize);
2064 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2065 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2066 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2067 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2068 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2069 nsbp->sb_rblocks - sbp->sb_rblocks);
2070 if (nsbp->sb_rextents != sbp->sb_rextents)
2071 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2072 nsbp->sb_rextents - sbp->sb_rextents);
2073 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2074 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2075 nsbp->sb_rextslog - sbp->sb_rextslog);
2080 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2081 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2085 * Mark more blocks free in the superblock.
2087 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2088 nsbp->sb_rextents - sbp->sb_rextents);
2090 * Free the fake mp structure.
2092 kmem_free(nmp, sizeof(*nmp));
2095 * Update mp values into the real mp structure.
2097 mp->m_rsumlevels = nrsumlevels;
2098 mp->m_rsumsize = nrsumsize;
2100 * Commit the transaction.
2102 xfs_trans_commit(tp, 0, NULL);
2107 * Error paths come here.
2111 kmem_free(nmp, sizeof(*nmp));
2112 xfs_trans_cancel(tp, cancelflags);
2117 * Allocate an extent in the realtime subvolume, with the usual allocation
2118 * parameters. The length units are all in realtime extents, as is the
2119 * result block number.
2122 xfs_rtallocate_extent(
2123 xfs_trans_t *tp, /* transaction pointer */
2124 xfs_rtblock_t bno, /* starting block number to allocate */
2125 xfs_extlen_t minlen, /* minimum length to allocate */
2126 xfs_extlen_t maxlen, /* maximum length to allocate */
2127 xfs_extlen_t *len, /* out: actual length allocated */
2128 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2129 int wasdel, /* was a delayed allocation extent */
2130 xfs_extlen_t prod, /* extent product factor */
2131 xfs_rtblock_t *rtblock) /* out: start block allocated */
2133 int error; /* error value */
2134 xfs_inode_t *ip; /* inode for bitmap file */
2135 xfs_mount_t *mp; /* file system mount structure */
2136 xfs_rtblock_t r; /* result allocated block */
2137 xfs_fsblock_t sb; /* summary file block number */
2138 xfs_buf_t *sumbp; /* summary file block buffer */
2140 ASSERT(minlen > 0 && minlen <= maxlen);
2143 * If prod is set then figure out what to do to minlen and maxlen.
2148 if ((i = maxlen % prod))
2150 if ((i = minlen % prod))
2152 if (maxlen < minlen) {
2153 *rtblock = NULLRTBLOCK;
2158 * Lock out other callers by grabbing the bitmap inode lock.
2160 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2161 XFS_ILOCK_EXCL, &ip)))
2165 * Allocate by size, or near another block, or exactly at some block.
2168 case XFS_ALLOCTYPE_ANY_AG:
2169 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2170 &sumbp, &sb, prod, &r);
2172 case XFS_ALLOCTYPE_NEAR_BNO:
2173 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2174 len, &sumbp, &sb, prod, &r);
2176 case XFS_ALLOCTYPE_THIS_BNO:
2177 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2178 len, &sumbp, &sb, prod, &r);
2187 * If it worked, update the superblock.
2189 if (r != NULLRTBLOCK) {
2190 long slen = (long)*len;
2192 ASSERT(*len >= minlen && *len <= maxlen);
2194 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2196 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2203 * Free an extent in the realtime subvolume. Length is expressed in
2204 * realtime extents, as is the block number.
2208 xfs_trans_t *tp, /* transaction pointer */
2209 xfs_rtblock_t bno, /* starting block number to free */
2210 xfs_extlen_t len) /* length of extent freed */
2212 int error; /* error value */
2213 xfs_inode_t *ip; /* bitmap file inode */
2214 xfs_mount_t *mp; /* file system mount structure */
2215 xfs_fsblock_t sb; /* summary file block number */
2216 xfs_buf_t *sumbp; /* summary file block buffer */
2220 * Synchronize by locking the bitmap inode.
2222 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2223 XFS_ILOCK_EXCL, &ip)))
2225 #if defined(__KERNEL__) && defined(DEBUG)
2227 * Check to see that this whole range is currently allocated.
2230 int stat; /* result from checking range */
2232 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2241 * Free the range of realtime blocks.
2243 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2248 * Mark more blocks free in the superblock.
2250 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2252 * If we've now freed all the blocks, reset the file sequence
2255 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2256 mp->m_sb.sb_rextents) {
2257 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2258 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2259 *(__uint64_t *)&ip->i_d.di_atime = 0;
2260 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2266 * Initialize realtime fields in the mount structure.
2270 xfs_mount_t *mp) /* file system mount structure */
2272 xfs_buf_t *bp; /* buffer for last block of subvolume */
2273 xfs_daddr_t d; /* address of last block of subvolume */
2274 int error; /* error return value */
2275 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2278 if (sbp->sb_rblocks == 0)
2280 if (mp->m_rtdev_targp == NULL) {
2282 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2283 return XFS_ERROR(ENODEV);
2285 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2287 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2289 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2290 mp->m_rbmip = mp->m_rsumip = NULL;
2292 * Check that the realtime section is an ok size.
2294 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2295 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2296 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2297 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2298 (unsigned long long) mp->m_sb.sb_rblocks);
2299 return XFS_ERROR(E2BIG);
2301 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2302 d - XFS_FSB_TO_BB(mp, 1),
2303 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2306 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2307 if (error == ENOSPC)
2308 return XFS_ERROR(E2BIG);
2316 * Get the bitmap and summary inodes into the mount structure
2321 xfs_mount_t *mp) /* file system mount structure */
2323 int error; /* error return value */
2327 if (sbp->sb_rbmino == NULLFSINO)
2329 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2332 ASSERT(mp->m_rbmip != NULL);
2333 ASSERT(sbp->sb_rsumino != NULLFSINO);
2334 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2336 VN_RELE(XFS_ITOV(mp->m_rbmip));
2339 ASSERT(mp->m_rsumip != NULL);
2344 * Pick an extent for allocation at the start of a new realtime file.
2345 * Use the sequence number stored in the atime field of the bitmap inode.
2346 * Translate this to a fraction of the rtextents, and return the product
2347 * of rtextents and the fraction.
2348 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2352 xfs_mount_t *mp, /* file system mount point */
2353 xfs_trans_t *tp, /* transaction pointer */
2354 xfs_extlen_t len, /* allocation length (rtextents) */
2355 xfs_rtblock_t *pick) /* result rt extent */
2357 xfs_rtblock_t b; /* result block */
2358 int error; /* error return value */
2359 xfs_inode_t *ip; /* bitmap incore inode */
2360 int log2; /* log of sequence number */
2361 __uint64_t resid; /* residual after log removed */
2362 __uint64_t seq; /* sequence number of file creation */
2363 __uint64_t *seqp; /* pointer to seqno in inode */
2365 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2366 XFS_ILOCK_EXCL, &ip)))
2368 ASSERT(ip == mp->m_rbmip);
2369 seqp = (__uint64_t *)&ip->i_d.di_atime;
2370 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2371 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2375 if ((log2 = xfs_highbit64(seq)) == -1)
2378 resid = seq - (1ULL << log2);
2379 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2381 if (b >= mp->m_sb.sb_rextents)
2382 b = do_mod(b, mp->m_sb.sb_rextents);
2383 if (b + len > mp->m_sb.sb_rextents)
2384 b = mp->m_sb.sb_rextents - len;
2387 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2394 * Debug code: print out the value of a range in the bitmap.
2398 xfs_mount_t *mp, /* file system mount structure */
2399 xfs_trans_t *tp, /* transaction pointer */
2400 xfs_rtblock_t start, /* starting block to print */
2401 xfs_extlen_t len) /* length to print */
2403 xfs_extlen_t i; /* block number in the extent */
2405 cmn_err(CE_DEBUG, "%Ld: ", (long long)start);
2406 for (i = 0; i < len; i++)
2407 cmn_err(CE_DEBUG, "%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2408 cmn_err(CE_DEBUG, "\n");
2412 * Debug code: print the summary file.
2415 xfs_rtprint_summary(
2416 xfs_mount_t *mp, /* file system mount structure */
2417 xfs_trans_t *tp) /* transaction pointer */
2419 xfs_suminfo_t c; /* summary data */
2420 xfs_rtblock_t i; /* bitmap block number */
2421 int l; /* summary information level */
2422 int p; /* flag for printed anything */
2423 xfs_fsblock_t sb; /* summary block number */
2424 xfs_buf_t *sumbp; /* summary block buffer */
2427 for (l = 0; l < mp->m_rsumlevels; l++) {
2428 for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2429 (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2432 cmn_err(CE_DEBUG, "%Ld-%Ld:", 1LL << l,
2433 XFS_RTMIN((1LL << l) +
2435 mp->m_sb.sb_rextents));
2438 cmn_err(CE_DEBUG, " %Ld:%d", (long long)i, c);
2442 cmn_err(CE_DEBUG, "\n");
2445 xfs_trans_brelse(tp, sumbp);