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"
47 #include "xfs_utils.h"
51 * Prototypes for internal functions.
55 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
56 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
57 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
58 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
59 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
60 xfs_extlen_t, int, xfs_rtblock_t *, int *);
61 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
62 xfs_rtblock_t, xfs_rtblock_t *);
63 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
64 xfs_rtblock_t, xfs_rtblock_t *);
65 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
66 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
67 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
69 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
70 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
77 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
89 * Allocate space to the bitmap or summary file, and zero it, for growfs.
91 STATIC int /* error */
93 xfs_mount_t *mp, /* file system mount point */
94 xfs_extlen_t oblocks, /* old count of blocks */
95 xfs_extlen_t nblocks, /* new count of blocks */
96 xfs_ino_t ino) /* inode number (bitmap/summary) */
98 xfs_fileoff_t bno; /* block number in file */
99 xfs_buf_t *bp; /* temporary buffer for zeroing */
100 int cancelflags; /* flags for xfs_trans_cancel */
101 int committed; /* transaction committed flag */
102 xfs_daddr_t d; /* disk block address */
103 int error; /* error return value */
104 xfs_fsblock_t firstblock; /* first block allocated in xaction */
105 xfs_bmap_free_t flist; /* list of freed blocks */
106 xfs_fsblock_t fsbno; /* filesystem block for bno */
107 xfs_inode_t *ip; /* pointer to incore inode */
108 xfs_bmbt_irec_t map; /* block map output */
109 int nmap; /* number of block maps */
110 int resblks; /* space reservation */
111 xfs_trans_t *tp; /* transaction pointer */
114 * Allocate space to the file, as necessary.
116 while (oblocks < nblocks) {
117 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
118 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
121 * Reserve space & log for one extent added to the file.
123 if ((error = xfs_trans_reserve(tp, resblks,
124 XFS_GROWRTALLOC_LOG_RES(mp), 0,
125 XFS_TRANS_PERM_LOG_RES,
126 XFS_DEFAULT_PERM_LOG_COUNT)))
128 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
132 if ((error = xfs_trans_iget(mp, tp, ino, 0,
133 XFS_ILOCK_EXCL, &ip)))
135 XFS_BMAP_INIT(&flist, &firstblock);
137 * Allocate blocks to the bitmap file.
140 cancelflags |= XFS_TRANS_ABORT;
141 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
142 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
143 resblks, &map, &nmap, &flist, NULL);
144 if (!error && nmap < 1)
145 error = XFS_ERROR(ENOSPC);
149 * Free any blocks freed up in the transaction, then commit.
151 error = xfs_bmap_finish(&tp, &flist, &committed);
154 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
158 * Now we need to clear the allocated blocks.
159 * Do this one block per transaction, to keep it simple.
162 for (bno = map.br_startoff, fsbno = map.br_startblock;
163 bno < map.br_startoff + map.br_blockcount;
165 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
167 * Reserve log for one block zeroing.
169 if ((error = xfs_trans_reserve(tp, 0,
170 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
173 * Lock the bitmap inode.
175 if ((error = xfs_trans_iget(mp, tp, ino, 0,
176 XFS_ILOCK_EXCL, &ip)))
179 * Get a buffer for the block.
181 d = XFS_FSB_TO_DADDR(mp, fsbno);
182 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
185 error = XFS_ERROR(EIO);
188 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
189 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
191 * Commit the transaction.
193 error = xfs_trans_commit(tp, 0);
198 * Go on to the next extent, if any.
200 oblocks = map.br_startoff + map.br_blockcount;
204 xfs_trans_cancel(tp, cancelflags);
210 * Attempt to allocate an extent minlen<=len<=maxlen starting from
211 * bitmap block bbno. If we don't get maxlen then use prod to trim
212 * the length, if given. Returns error; returns starting block in *rtblock.
213 * The lengths are all in rtextents.
215 STATIC int /* error */
216 xfs_rtallocate_extent_block(
217 xfs_mount_t *mp, /* file system mount point */
218 xfs_trans_t *tp, /* transaction pointer */
219 xfs_rtblock_t bbno, /* bitmap block number */
220 xfs_extlen_t minlen, /* minimum length to allocate */
221 xfs_extlen_t maxlen, /* maximum length to allocate */
222 xfs_extlen_t *len, /* out: actual length allocated */
223 xfs_rtblock_t *nextp, /* out: next block to try */
224 xfs_buf_t **rbpp, /* in/out: summary block buffer */
225 xfs_fsblock_t *rsb, /* in/out: summary block number */
226 xfs_extlen_t prod, /* extent product factor */
227 xfs_rtblock_t *rtblock) /* out: start block allocated */
229 xfs_rtblock_t besti; /* best rtblock found so far */
230 xfs_rtblock_t bestlen; /* best length found so far */
231 xfs_rtblock_t end; /* last rtblock in chunk */
232 int error; /* error value */
233 xfs_rtblock_t i; /* current rtblock trying */
234 xfs_rtblock_t next; /* next rtblock to try */
235 int stat; /* status from internal calls */
238 * Loop over all the extents starting in this bitmap block,
239 * looking for one that's long enough.
241 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
242 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
246 * See if there's a free extent of maxlen starting at i.
247 * If it's not so then next will contain the first non-free.
249 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
255 * i for maxlen is all free, allocate and return that.
257 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
267 * In the case where we have a variable-sized allocation
268 * request, figure out how big this free piece is,
269 * and if it's big enough for the minimum, and the best
270 * so far, remember it.
272 if (minlen < maxlen) {
273 xfs_rtblock_t thislen; /* this extent size */
276 if (thislen >= minlen && thislen > bestlen) {
282 * If not done yet, find the start of the next free space.
285 error = xfs_rtfind_forw(mp, tp, next, end, &i);
293 * Searched the whole thing & didn't find a maxlen free extent.
295 if (minlen < maxlen && besti != -1) {
296 xfs_extlen_t p; /* amount to trim length by */
299 * If size should be a multiple of prod, make that so.
301 if (prod > 1 && (p = do_mod(bestlen, prod)))
304 * Allocate besti for bestlen & return that.
306 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
315 * Allocation failed. Set *nextp to the next block to try.
318 *rtblock = NULLRTBLOCK;
323 * Allocate an extent of length minlen<=len<=maxlen, starting at block
324 * bno. If we don't get maxlen then use prod to trim the length, if given.
325 * Returns error; returns starting block in *rtblock.
326 * The lengths are all in rtextents.
328 STATIC int /* error */
329 xfs_rtallocate_extent_exact(
330 xfs_mount_t *mp, /* file system mount point */
331 xfs_trans_t *tp, /* transaction pointer */
332 xfs_rtblock_t bno, /* starting block number to allocate */
333 xfs_extlen_t minlen, /* minimum length to allocate */
334 xfs_extlen_t maxlen, /* maximum length to allocate */
335 xfs_extlen_t *len, /* out: actual length allocated */
336 xfs_buf_t **rbpp, /* in/out: summary block buffer */
337 xfs_fsblock_t *rsb, /* in/out: summary block number */
338 xfs_extlen_t prod, /* extent product factor */
339 xfs_rtblock_t *rtblock) /* out: start block allocated */
341 int error; /* error value */
342 xfs_extlen_t i; /* extent length trimmed due to prod */
343 int isfree; /* extent is free */
344 xfs_rtblock_t next; /* next block to try (dummy) */
346 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
348 * Check if the range in question (for maxlen) is free.
350 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
356 * If it is, allocate it and return success.
358 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
367 * If not, allocate what there is, if it's at least minlen.
370 if (maxlen < minlen) {
372 * Failed, return failure status.
374 *rtblock = NULLRTBLOCK;
378 * Trim off tail of extent, if prod is specified.
380 if (prod > 1 && (i = maxlen % prod)) {
382 if (maxlen < minlen) {
384 * Now we can't do it, return failure status.
386 *rtblock = NULLRTBLOCK;
391 * Allocate what we can and return it.
393 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
403 * Allocate an extent of length minlen<=len<=maxlen, starting as near
404 * to bno as possible. If we don't get maxlen then use prod to trim
405 * the length, if given. The lengths are all in rtextents.
407 STATIC int /* error */
408 xfs_rtallocate_extent_near(
409 xfs_mount_t *mp, /* file system mount point */
410 xfs_trans_t *tp, /* transaction pointer */
411 xfs_rtblock_t bno, /* starting block number to allocate */
412 xfs_extlen_t minlen, /* minimum length to allocate */
413 xfs_extlen_t maxlen, /* maximum length to allocate */
414 xfs_extlen_t *len, /* out: actual length allocated */
415 xfs_buf_t **rbpp, /* in/out: summary block buffer */
416 xfs_fsblock_t *rsb, /* in/out: summary block number */
417 xfs_extlen_t prod, /* extent product factor */
418 xfs_rtblock_t *rtblock) /* out: start block allocated */
420 int any; /* any useful extents from summary */
421 xfs_rtblock_t bbno; /* bitmap block number */
422 int error; /* error value */
423 int i; /* bitmap block offset (loop control) */
424 int j; /* secondary loop control */
425 int log2len; /* log2 of minlen */
426 xfs_rtblock_t n; /* next block to try */
427 xfs_rtblock_t r; /* result block */
429 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
431 * If the block number given is off the end, silently set it to
434 if (bno >= mp->m_sb.sb_rextents)
435 bno = mp->m_sb.sb_rextents - 1;
437 * Try the exact allocation first.
439 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
440 rbpp, rsb, prod, &r);
445 * If the exact allocation worked, return that.
447 if (r != NULLRTBLOCK) {
451 bbno = XFS_BITTOBLOCK(mp, bno);
453 log2len = xfs_highbit32(minlen);
455 * Loop over all bitmap blocks (bbno + i is current block).
459 * Get summary information of extents of all useful levels
460 * starting in this bitmap block.
462 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
463 bbno + i, rbpp, rsb, &any);
468 * If there are any useful extents starting here, try
473 * On the positive side of the starting location.
477 * Try to allocate an extent starting in
480 error = xfs_rtallocate_extent_block(mp, tp,
481 bbno + i, minlen, maxlen, len, &n, rbpp,
487 * If it worked, return it.
489 if (r != NULLRTBLOCK) {
495 * On the negative side of the starting location.
499 * Loop backwards through the bitmap blocks from
500 * the starting point-1 up to where we are now.
501 * There should be an extent which ends in this
502 * bitmap block and is long enough.
504 for (j = -1; j > i; j--) {
506 * Grab the summary information for
509 error = xfs_rtany_summary(mp, tp,
510 log2len, mp->m_rsumlevels - 1,
511 bbno + j, rbpp, rsb, &any);
516 * If there's no extent given in the
517 * summary that means the extent we
518 * found must carry over from an
519 * earlier block. If there is an
520 * extent given, we've already tried
521 * that allocation, don't do it again.
525 error = xfs_rtallocate_extent_block(mp,
526 tp, bbno + j, minlen, maxlen,
527 len, &n, rbpp, rsb, prod, &r);
532 * If it works, return the extent.
534 if (r != NULLRTBLOCK) {
540 * There weren't intervening bitmap blocks
541 * with a long enough extent, or the
542 * allocation didn't work for some reason
543 * (i.e. it's a little * too short).
544 * Try to allocate from the summary block
547 error = xfs_rtallocate_extent_block(mp, tp,
548 bbno + i, minlen, maxlen, len, &n, rbpp,
554 * If it works, return the extent.
556 if (r != NULLRTBLOCK) {
563 * Loop control. If we were on the positive side, and there's
564 * still more blocks on the negative side, go there.
566 if (i > 0 && (int)bbno - i >= 0)
569 * If positive, and no more negative, but there are more
570 * positive, go there.
572 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
575 * If negative or 0 (just started), and there are positive
576 * blocks to go, go there. The 0 case moves to block 1.
578 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
581 * If negative or 0 and there are more negative blocks,
584 else if (i <= 0 && (int)bbno + i > 0)
587 * Must be done. Return failure.
592 *rtblock = NULLRTBLOCK;
597 * Allocate an extent of length minlen<=len<=maxlen, with no position
598 * specified. If we don't get maxlen then use prod to trim
599 * the length, if given. The lengths are all in rtextents.
601 STATIC int /* error */
602 xfs_rtallocate_extent_size(
603 xfs_mount_t *mp, /* file system mount point */
604 xfs_trans_t *tp, /* transaction pointer */
605 xfs_extlen_t minlen, /* minimum length to allocate */
606 xfs_extlen_t maxlen, /* maximum length to allocate */
607 xfs_extlen_t *len, /* out: actual length allocated */
608 xfs_buf_t **rbpp, /* in/out: summary block buffer */
609 xfs_fsblock_t *rsb, /* in/out: summary block number */
610 xfs_extlen_t prod, /* extent product factor */
611 xfs_rtblock_t *rtblock) /* out: start block allocated */
613 int error; /* error value */
614 int i; /* bitmap block number */
615 int l; /* level number (loop control) */
616 xfs_rtblock_t n; /* next block to be tried */
617 xfs_rtblock_t r; /* result block number */
618 xfs_suminfo_t sum; /* summary information for extents */
620 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
622 * Loop over all the levels starting with maxlen.
623 * At each level, look at all the bitmap blocks, to see if there
624 * are extents starting there that are long enough (>= maxlen).
625 * Note, only on the initial level can the allocation fail if
626 * the summary says there's an extent.
628 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
630 * Loop over all the bitmap blocks.
632 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
634 * Get the summary for this level/block.
636 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
642 * Nothing there, on to the next block.
647 * Try allocating the extent.
649 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
650 maxlen, len, &n, rbpp, rsb, prod, &r);
655 * If it worked, return that.
657 if (r != NULLRTBLOCK) {
662 * If the "next block to try" returned from the
663 * allocator is beyond the next bitmap block,
664 * skip to that bitmap block.
666 if (XFS_BITTOBLOCK(mp, n) > i + 1)
667 i = XFS_BITTOBLOCK(mp, n) - 1;
671 * Didn't find any maxlen blocks. Try smaller ones, unless
672 * we're asking for a fixed size extent.
674 if (minlen > --maxlen) {
675 *rtblock = NULLRTBLOCK;
679 * Loop over sizes, from maxlen down to minlen.
680 * This time, when we do the allocations, allow smaller ones
683 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
685 * Loop over all the bitmap blocks, try an allocation
686 * starting in that block.
688 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
690 * Get the summary information for this level/block.
692 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
698 * If nothing there, go on to next.
703 * Try the allocation. Make sure the specified
704 * minlen/maxlen are in the possible range for
705 * this summary level.
707 error = xfs_rtallocate_extent_block(mp, tp, i,
708 XFS_RTMAX(minlen, 1 << l),
709 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
710 len, &n, rbpp, rsb, prod, &r);
715 * If it worked, return that extent.
717 if (r != NULLRTBLOCK) {
722 * If the "next block to try" returned from the
723 * allocator is beyond the next bitmap block,
724 * skip to that bitmap block.
726 if (XFS_BITTOBLOCK(mp, n) > i + 1)
727 i = XFS_BITTOBLOCK(mp, n) - 1;
731 * Got nothing, return failure.
733 *rtblock = NULLRTBLOCK;
738 * Mark an extent specified by start and len allocated.
739 * Updates all the summary information as well as the bitmap.
741 STATIC int /* error */
742 xfs_rtallocate_range(
743 xfs_mount_t *mp, /* file system mount point */
744 xfs_trans_t *tp, /* transaction pointer */
745 xfs_rtblock_t start, /* start block to allocate */
746 xfs_extlen_t len, /* length to allocate */
747 xfs_buf_t **rbpp, /* in/out: summary block buffer */
748 xfs_fsblock_t *rsb) /* in/out: summary block number */
750 xfs_rtblock_t end; /* end of the allocated extent */
751 int error; /* error value */
752 xfs_rtblock_t postblock; /* first block allocated > end */
753 xfs_rtblock_t preblock; /* first block allocated < start */
755 end = start + len - 1;
757 * Assume we're allocating out of the middle of a free extent.
758 * We need to find the beginning and end of the extent so we can
759 * properly update the summary.
761 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
766 * Find the next allocated block (end of free extent).
768 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
774 * Decrement the summary information corresponding to the entire
777 error = xfs_rtmodify_summary(mp, tp,
778 XFS_RTBLOCKLOG(postblock + 1 - preblock),
779 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
784 * If there are blocks not being allocated at the front of the
785 * old extent, add summary data for them to be free.
787 if (preblock < start) {
788 error = xfs_rtmodify_summary(mp, tp,
789 XFS_RTBLOCKLOG(start - preblock),
790 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
796 * If there are blocks not being allocated at the end of the
797 * old extent, add summary data for them to be free.
799 if (postblock > end) {
800 error = xfs_rtmodify_summary(mp, tp,
801 XFS_RTBLOCKLOG(postblock - end),
802 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
808 * Modify the bitmap to mark this extent allocated.
810 error = xfs_rtmodify_range(mp, tp, start, len, 0);
815 * Return whether there are any free extents in the size range given
816 * by low and high, for the bitmap block bbno.
818 STATIC int /* error */
820 xfs_mount_t *mp, /* file system mount structure */
821 xfs_trans_t *tp, /* transaction pointer */
822 int low, /* low log2 extent size */
823 int high, /* high log2 extent size */
824 xfs_rtblock_t bbno, /* bitmap block number */
825 xfs_buf_t **rbpp, /* in/out: summary block buffer */
826 xfs_fsblock_t *rsb, /* in/out: summary block number */
827 int *stat) /* out: any good extents here? */
829 int error; /* error value */
830 int log; /* loop counter, log2 of ext. size */
831 xfs_suminfo_t sum; /* summary data */
834 * Loop over logs of extent sizes. Order is irrelevant.
836 for (log = low; log <= high; log++) {
838 * Get one summary datum.
840 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
845 * If there are any, return success.
853 * Found nothing, return failure.
860 * Get a buffer for the bitmap or summary file block specified.
861 * The buffer is returned read and locked.
863 STATIC int /* error */
865 xfs_mount_t *mp, /* file system mount structure */
866 xfs_trans_t *tp, /* transaction pointer */
867 xfs_rtblock_t block, /* block number in bitmap or summary */
868 int issum, /* is summary not bitmap */
869 xfs_buf_t **bpp) /* output: buffer for the block */
871 xfs_buf_t *bp; /* block buffer, result */
872 xfs_daddr_t d; /* disk addr of block */
873 int error; /* error value */
874 xfs_fsblock_t fsb; /* fs block number for block */
875 xfs_inode_t *ip; /* bitmap or summary inode */
877 ip = issum ? mp->m_rsumip : mp->m_rbmip;
879 * Map from the file offset (block) and inode number to the
882 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
886 ASSERT(fsb != NULLFSBLOCK);
888 * Convert to disk address for buffer cache.
890 d = XFS_FSB_TO_DADDR(mp, fsb);
894 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
895 mp->m_bsize, 0, &bp);
899 ASSERT(bp && !XFS_BUF_GETERROR(bp));
906 * Check that the given extent (block range) is allocated already.
908 STATIC int /* error */
909 xfs_rtcheck_alloc_range(
910 xfs_mount_t *mp, /* file system mount point */
911 xfs_trans_t *tp, /* transaction pointer */
912 xfs_rtblock_t bno, /* starting block number of extent */
913 xfs_extlen_t len, /* length of extent */
914 int *stat) /* out: 1 for allocated, 0 for not */
916 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
918 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
923 * Check that the given range is either all allocated (val = 0) or
924 * all free (val = 1).
926 STATIC int /* error */
928 xfs_mount_t *mp, /* file system mount point */
929 xfs_trans_t *tp, /* transaction pointer */
930 xfs_rtblock_t start, /* starting block number of extent */
931 xfs_extlen_t len, /* length of extent */
932 int val, /* 1 for free, 0 for allocated */
933 xfs_rtblock_t *new, /* out: first block not matching */
934 int *stat) /* out: 1 for matches, 0 for not */
936 xfs_rtword_t *b; /* current word in buffer */
937 int bit; /* bit number in the word */
938 xfs_rtblock_t block; /* bitmap block number */
939 xfs_buf_t *bp; /* buf for the block */
940 xfs_rtword_t *bufp; /* starting word in buffer */
941 int error; /* error value */
942 xfs_rtblock_t i; /* current bit number rel. to start */
943 xfs_rtblock_t lastbit; /* last useful bit in word */
944 xfs_rtword_t mask; /* mask of relevant bits for value */
945 xfs_rtword_t wdiff; /* difference from wanted value */
946 int word; /* word number in the buffer */
949 * Compute starting bitmap block number
951 block = XFS_BITTOBLOCK(mp, start);
953 * Read the bitmap block.
955 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
959 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
961 * Compute the starting word's address, and starting bit.
963 word = XFS_BITTOWORD(mp, start);
965 bit = (int)(start & (XFS_NBWORD - 1));
967 * 0 (allocated) => all zero's; 1 (free) => all one's.
971 * If not starting on a word boundary, deal with the first
976 * Compute first bit not examined.
978 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
980 * Mask of relevant bits.
982 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
984 * Compute difference between actual and desired value.
986 if ((wdiff = (*b ^ val) & mask)) {
988 * Different, compute first wrong bit and return.
990 xfs_trans_brelse(tp, bp);
991 i = XFS_RTLOBIT(wdiff) - bit;
998 * Go on to next block if that's where the next word is
999 * and we need the next word.
1001 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1003 * If done with this block, get the next one.
1005 xfs_trans_brelse(tp, bp);
1006 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1010 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1014 * Go on to the next word in the buffer.
1020 * Starting on a word boundary, no partial word.
1025 * Loop over whole words in buffers. When we use up one buffer
1026 * we move on to the next one.
1028 while (len - i >= XFS_NBWORD) {
1030 * Compute difference between actual and desired value.
1032 if ((wdiff = *b ^ val)) {
1034 * Different, compute first wrong bit and return.
1036 xfs_trans_brelse(tp, bp);
1037 i += XFS_RTLOBIT(wdiff);
1044 * Go on to next block if that's where the next word is
1045 * and we need the next word.
1047 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1049 * If done with this block, get the next one.
1051 xfs_trans_brelse(tp, bp);
1052 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1056 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1060 * Go on to the next word in the buffer.
1066 * If not ending on a word boundary, deal with the last
1069 if ((lastbit = len - i)) {
1071 * Mask of relevant bits.
1073 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1075 * Compute difference between actual and desired value.
1077 if ((wdiff = (*b ^ val) & mask)) {
1079 * Different, compute first wrong bit and return.
1081 xfs_trans_brelse(tp, bp);
1082 i += XFS_RTLOBIT(wdiff);
1090 * Successful, return.
1092 xfs_trans_brelse(tp, bp);
1099 * Copy and transform the summary file, given the old and new
1100 * parameters in the mount structures.
1102 STATIC int /* error */
1104 xfs_mount_t *omp, /* old file system mount point */
1105 xfs_mount_t *nmp, /* new file system mount point */
1106 xfs_trans_t *tp) /* transaction pointer */
1108 xfs_rtblock_t bbno; /* bitmap block number */
1109 xfs_buf_t *bp; /* summary buffer */
1110 int error; /* error return value */
1111 int log; /* summary level number (log length) */
1112 xfs_suminfo_t sum; /* summary data */
1113 xfs_fsblock_t sumbno; /* summary block number */
1116 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1117 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1118 (xfs_srtblock_t)bbno >= 0;
1120 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1126 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1130 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1141 * Searching backward from start to limit, find the first block whose
1142 * allocated/free state is different from start's.
1144 STATIC int /* error */
1146 xfs_mount_t *mp, /* file system mount point */
1147 xfs_trans_t *tp, /* transaction pointer */
1148 xfs_rtblock_t start, /* starting block to look at */
1149 xfs_rtblock_t limit, /* last block to look at */
1150 xfs_rtblock_t *rtblock) /* out: start block found */
1152 xfs_rtword_t *b; /* current word in buffer */
1153 int bit; /* bit number in the word */
1154 xfs_rtblock_t block; /* bitmap block number */
1155 xfs_buf_t *bp; /* buf for the block */
1156 xfs_rtword_t *bufp; /* starting word in buffer */
1157 int error; /* error value */
1158 xfs_rtblock_t firstbit; /* first useful bit in the word */
1159 xfs_rtblock_t i; /* current bit number rel. to start */
1160 xfs_rtblock_t len; /* length of inspected area */
1161 xfs_rtword_t mask; /* mask of relevant bits for value */
1162 xfs_rtword_t want; /* mask for "good" values */
1163 xfs_rtword_t wdiff; /* difference from wanted value */
1164 int word; /* word number in the buffer */
1167 * Compute and read in starting bitmap block for starting block.
1169 block = XFS_BITTOBLOCK(mp, start);
1170 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1174 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1176 * Get the first word's index & point to it.
1178 word = XFS_BITTOWORD(mp, start);
1180 bit = (int)(start & (XFS_NBWORD - 1));
1181 len = start - limit + 1;
1183 * Compute match value, based on the bit at start: if 1 (free)
1184 * then all-ones, else all-zeroes.
1186 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1188 * If the starting position is not word-aligned, deal with the
1191 if (bit < XFS_NBWORD - 1) {
1193 * Calculate first (leftmost) bit number to look at,
1194 * and mask for all the relevant bits in this word.
1196 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1197 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1200 * Calculate the difference between the value there
1201 * and what we're looking for.
1203 if ((wdiff = (*b ^ want) & mask)) {
1205 * Different. Mark where we are and return.
1207 xfs_trans_brelse(tp, bp);
1208 i = bit - XFS_RTHIBIT(wdiff);
1209 *rtblock = start - i + 1;
1212 i = bit - firstbit + 1;
1214 * Go on to previous block if that's where the previous word is
1215 * and we need the previous word.
1217 if (--word == -1 && i < len) {
1219 * If done with this block, get the previous one.
1221 xfs_trans_brelse(tp, bp);
1222 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1226 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1227 word = XFS_BLOCKWMASK(mp);
1231 * Go on to the previous word in the buffer.
1237 * Starting on a word boundary, no partial word.
1242 * Loop over whole words in buffers. When we use up one buffer
1243 * we move on to the previous one.
1245 while (len - i >= XFS_NBWORD) {
1247 * Compute difference between actual and desired value.
1249 if ((wdiff = *b ^ want)) {
1251 * Different, mark where we are and return.
1253 xfs_trans_brelse(tp, bp);
1254 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1255 *rtblock = start - i + 1;
1260 * Go on to previous block if that's where the previous word is
1261 * and we need the previous word.
1263 if (--word == -1 && i < len) {
1265 * If done with this block, get the previous one.
1267 xfs_trans_brelse(tp, bp);
1268 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1272 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1273 word = XFS_BLOCKWMASK(mp);
1277 * Go on to the previous word in the buffer.
1283 * If not ending on a word boundary, deal with the last
1288 * Calculate first (leftmost) bit number to look at,
1289 * and mask for all the relevant bits in this word.
1291 firstbit = XFS_NBWORD - (len - i);
1292 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1294 * Compute difference between actual and desired value.
1296 if ((wdiff = (*b ^ want) & mask)) {
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;
1308 * No match, return that we scanned the whole area.
1310 xfs_trans_brelse(tp, bp);
1311 *rtblock = start - i + 1;
1316 * Searching forward from start to limit, find the first block whose
1317 * allocated/free state is different from start's.
1319 STATIC int /* error */
1321 xfs_mount_t *mp, /* file system mount point */
1322 xfs_trans_t *tp, /* transaction pointer */
1323 xfs_rtblock_t start, /* starting block to look at */
1324 xfs_rtblock_t limit, /* last block to look at */
1325 xfs_rtblock_t *rtblock) /* out: start block found */
1327 xfs_rtword_t *b; /* current word in buffer */
1328 int bit; /* bit number in the word */
1329 xfs_rtblock_t block; /* bitmap block number */
1330 xfs_buf_t *bp; /* buf for the block */
1331 xfs_rtword_t *bufp; /* starting word in buffer */
1332 int error; /* error value */
1333 xfs_rtblock_t i; /* current bit number rel. to start */
1334 xfs_rtblock_t lastbit; /* last useful bit in the word */
1335 xfs_rtblock_t len; /* length of inspected area */
1336 xfs_rtword_t mask; /* mask of relevant bits for value */
1337 xfs_rtword_t want; /* mask for "good" values */
1338 xfs_rtword_t wdiff; /* difference from wanted value */
1339 int word; /* word number in the buffer */
1342 * Compute and read in starting bitmap block for starting block.
1344 block = XFS_BITTOBLOCK(mp, start);
1345 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1349 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1351 * Get the first word's index & point to it.
1353 word = XFS_BITTOWORD(mp, start);
1355 bit = (int)(start & (XFS_NBWORD - 1));
1356 len = limit - start + 1;
1358 * Compute match value, based on the bit at start: if 1 (free)
1359 * then all-ones, else all-zeroes.
1361 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1363 * If the starting position is not word-aligned, deal with the
1368 * Calculate last (rightmost) bit number to look at,
1369 * and mask for all the relevant bits in this word.
1371 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1372 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1374 * Calculate the difference between the value there
1375 * and what we're looking for.
1377 if ((wdiff = (*b ^ want) & mask)) {
1379 * Different. Mark where we are and return.
1381 xfs_trans_brelse(tp, bp);
1382 i = XFS_RTLOBIT(wdiff) - bit;
1383 *rtblock = start + i - 1;
1388 * Go on to next block if that's where the next word is
1389 * and we need the next word.
1391 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1393 * If done with this block, get the previous one.
1395 xfs_trans_brelse(tp, bp);
1396 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1400 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1404 * Go on to the previous word in the buffer.
1410 * Starting on a word boundary, no partial word.
1415 * Loop over whole words in buffers. When we use up one buffer
1416 * we move on to the next one.
1418 while (len - i >= XFS_NBWORD) {
1420 * Compute difference between actual and desired value.
1422 if ((wdiff = *b ^ want)) {
1424 * Different, mark where we are and return.
1426 xfs_trans_brelse(tp, bp);
1427 i += XFS_RTLOBIT(wdiff);
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 next 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 next word in the buffer.
1455 * If not ending on a word boundary, deal with the last
1458 if ((lastbit = len - i)) {
1460 * Calculate mask for all the relevant bits in this word.
1462 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1464 * Compute difference between actual and desired value.
1466 if ((wdiff = (*b ^ want) & mask)) {
1468 * Different, mark where we are and return.
1470 xfs_trans_brelse(tp, bp);
1471 i += XFS_RTLOBIT(wdiff);
1472 *rtblock = start + i - 1;
1478 * No match, return that we scanned the whole area.
1480 xfs_trans_brelse(tp, bp);
1481 *rtblock = start + i - 1;
1486 * Mark an extent specified by start and len freed.
1487 * Updates all the summary information as well as the bitmap.
1489 STATIC int /* error */
1491 xfs_mount_t *mp, /* file system mount point */
1492 xfs_trans_t *tp, /* transaction pointer */
1493 xfs_rtblock_t start, /* starting block to free */
1494 xfs_extlen_t len, /* length to free */
1495 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1496 xfs_fsblock_t *rsb) /* in/out: summary block number */
1498 xfs_rtblock_t end; /* end of the freed extent */
1499 int error; /* error value */
1500 xfs_rtblock_t postblock; /* first block freed > end */
1501 xfs_rtblock_t preblock; /* first block freed < start */
1503 end = start + len - 1;
1505 * Modify the bitmap to mark this extent freed.
1507 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1512 * Assume we're freeing out of the middle of an allocated extent.
1513 * We need to find the beginning and end of the extent so we can
1514 * properly update the summary.
1516 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1521 * Find the next allocated block (end of allocated extent).
1523 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1526 * If there are blocks not being freed at the front of the
1527 * old extent, add summary data for them to be allocated.
1529 if (preblock < start) {
1530 error = xfs_rtmodify_summary(mp, tp,
1531 XFS_RTBLOCKLOG(start - preblock),
1532 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1538 * If there are blocks not being freed at the end of the
1539 * old extent, add summary data for them to be allocated.
1541 if (postblock > end) {
1542 error = xfs_rtmodify_summary(mp, tp,
1543 XFS_RTBLOCKLOG(postblock - end),
1544 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1550 * Increment the summary information corresponding to the entire
1551 * (new) free extent.
1553 error = xfs_rtmodify_summary(mp, tp,
1554 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1555 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1560 * Read and return the summary information for a given extent size,
1561 * bitmap block combination.
1562 * Keeps track of a current summary block, so we don't keep reading
1563 * it from the buffer cache.
1565 STATIC int /* error */
1567 xfs_mount_t *mp, /* file system mount structure */
1568 xfs_trans_t *tp, /* transaction pointer */
1569 int log, /* log2 of extent size */
1570 xfs_rtblock_t bbno, /* bitmap block number */
1571 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1572 xfs_fsblock_t *rsb, /* in/out: summary block number */
1573 xfs_suminfo_t *sum) /* out: summary info for this block */
1575 xfs_buf_t *bp; /* buffer for summary block */
1576 int error; /* error value */
1577 xfs_fsblock_t sb; /* summary fsblock */
1578 int so; /* index into the summary file */
1579 xfs_suminfo_t *sp; /* pointer to returned data */
1582 * Compute entry number in the summary file.
1584 so = XFS_SUMOFFS(mp, log, bbno);
1586 * Compute the block number in the summary file.
1588 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1590 * If we have an old buffer, and the block number matches, use that.
1592 if (rbpp && *rbpp && *rsb == sb)
1595 * Otherwise we have to get the buffer.
1599 * If there was an old one, get rid of it first.
1602 xfs_trans_brelse(tp, *rbpp);
1603 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1608 * Remember this buffer and block for the next call.
1616 * Point to the summary information & copy it out.
1618 sp = XFS_SUMPTR(mp, bp, so);
1621 * Drop the buffer if we're not asked to remember it.
1624 xfs_trans_brelse(tp, bp);
1629 * Set the given range of bitmap bits to the given value.
1630 * Do whatever I/O and logging is required.
1632 STATIC int /* error */
1634 xfs_mount_t *mp, /* file system mount point */
1635 xfs_trans_t *tp, /* transaction pointer */
1636 xfs_rtblock_t start, /* starting block to modify */
1637 xfs_extlen_t len, /* length of extent to modify */
1638 int val) /* 1 for free, 0 for allocated */
1640 xfs_rtword_t *b; /* current word in buffer */
1641 int bit; /* bit number in the word */
1642 xfs_rtblock_t block; /* bitmap block number */
1643 xfs_buf_t *bp; /* buf for the block */
1644 xfs_rtword_t *bufp; /* starting word in buffer */
1645 int error; /* error value */
1646 xfs_rtword_t *first; /* first used word in the buffer */
1647 int i; /* current bit number rel. to start */
1648 int lastbit; /* last useful bit in word */
1649 xfs_rtword_t mask; /* mask o frelevant bits for value */
1650 int word; /* word number in the buffer */
1653 * Compute starting bitmap block number.
1655 block = XFS_BITTOBLOCK(mp, start);
1657 * Read the bitmap block, and point to its data.
1659 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1663 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1665 * Compute the starting word's address, and starting bit.
1667 word = XFS_BITTOWORD(mp, start);
1668 first = b = &bufp[word];
1669 bit = (int)(start & (XFS_NBWORD - 1));
1671 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1675 * If not starting on a word boundary, deal with the first
1680 * Compute first bit not changed and mask of relevant bits.
1682 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1683 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1685 * Set/clear the active bits.
1693 * Go on to the next block if that's where the next word is
1694 * and we need the next word.
1696 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1698 * Log the changed part of this block.
1701 xfs_trans_log_buf(tp, bp,
1702 (uint)((char *)first - (char *)bufp),
1703 (uint)((char *)b - (char *)bufp));
1704 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1708 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1712 * Go on to the next word in the buffer
1718 * Starting on a word boundary, no partial word.
1723 * Loop over whole words in buffers. When we use up one buffer
1724 * we move on to the next one.
1726 while (len - i >= XFS_NBWORD) {
1728 * Set the word value correctly.
1733 * Go on to the next block if that's where the next word is
1734 * and we need the next word.
1736 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1738 * Log the changed part of this block.
1741 xfs_trans_log_buf(tp, bp,
1742 (uint)((char *)first - (char *)bufp),
1743 (uint)((char *)b - (char *)bufp));
1744 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1748 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1752 * Go on to the next word in the buffer
1758 * If not ending on a word boundary, deal with the last
1761 if ((lastbit = len - i)) {
1763 * Compute a mask of relevant bits.
1766 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1768 * Set/clear the active bits.
1777 * Log any remaining changed bytes.
1780 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1781 (uint)((char *)b - (char *)bufp - 1));
1786 * Read and modify the summary information for a given extent size,
1787 * bitmap block combination.
1788 * Keeps track of a current summary block, so we don't keep reading
1789 * it from the buffer cache.
1791 STATIC int /* error */
1792 xfs_rtmodify_summary(
1793 xfs_mount_t *mp, /* file system mount point */
1794 xfs_trans_t *tp, /* transaction pointer */
1795 int log, /* log2 of extent size */
1796 xfs_rtblock_t bbno, /* bitmap block number */
1797 int delta, /* change to make to summary info */
1798 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1799 xfs_fsblock_t *rsb) /* in/out: summary block number */
1801 xfs_buf_t *bp; /* buffer for the summary block */
1802 int error; /* error value */
1803 xfs_fsblock_t sb; /* summary fsblock */
1804 int so; /* index into the summary file */
1805 xfs_suminfo_t *sp; /* pointer to returned data */
1808 * Compute entry number in the summary file.
1810 so = XFS_SUMOFFS(mp, log, bbno);
1812 * Compute the block number in the summary file.
1814 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1816 * If we have an old buffer, and the block number matches, use that.
1818 if (rbpp && *rbpp && *rsb == sb)
1821 * Otherwise we have to get the buffer.
1825 * If there was an old one, get rid of it first.
1828 xfs_trans_brelse(tp, *rbpp);
1829 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1834 * Remember this buffer and block for the next call.
1842 * Point to the summary information, modify and log it.
1844 sp = XFS_SUMPTR(mp, bp, so);
1846 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1847 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1852 * Visible (exported) functions.
1856 * Grow the realtime area of the filesystem.
1860 xfs_mount_t *mp, /* mount point for filesystem */
1861 xfs_growfs_rt_t *in) /* growfs rt input struct */
1863 xfs_rtblock_t bmbno; /* bitmap block number */
1864 xfs_buf_t *bp; /* temporary buffer */
1865 int cancelflags; /* flags for xfs_trans_cancel */
1866 int error; /* error return value */
1867 xfs_inode_t *ip; /* bitmap inode, used as lock */
1868 xfs_mount_t *nmp; /* new (fake) mount structure */
1869 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1870 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1871 xfs_drtbno_t nrextents; /* new number of realtime extents */
1872 uint8_t nrextslog; /* new log2 of sb_rextents */
1873 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1874 uint nrsumlevels; /* new rt summary levels */
1875 uint nrsumsize; /* new size of rt summary, bytes */
1876 xfs_sb_t *nsbp; /* new superblock */
1877 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1878 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1879 xfs_sb_t *sbp; /* old superblock */
1880 xfs_fsblock_t sumbno; /* summary block number */
1881 xfs_trans_t *tp; /* transaction pointer */
1886 * Initial error checking.
1888 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1889 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1890 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1891 return XFS_ERROR(EINVAL);
1892 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1895 * Read in the last block of the device, make sure it exists.
1897 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1898 XFS_FSB_TO_BB(mp, nrblocks - 1),
1899 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1905 * Calculate new parameters. These are the final values to be reached.
1907 nrextents = nrblocks;
1908 do_div(nrextents, in->extsize);
1909 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1910 nrextslog = xfs_highbit32(nrextents);
1911 nrsumlevels = nrextslog + 1;
1912 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1913 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1914 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1916 * New summary size can't be more than half the size of
1917 * the log. This prevents us from getting a log overflow,
1918 * since we'll log basically the whole summary file at once.
1920 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1921 return XFS_ERROR(EINVAL);
1923 * Get the old block counts for bitmap and summary inodes.
1924 * These can't change since other growfs callers are locked out.
1926 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1927 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1929 * Allocate space to the bitmap and summary files, as necessary.
1931 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1932 mp->m_sb.sb_rbmino)))
1934 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1935 mp->m_sb.sb_rsumino)))
1938 * Allocate a new (fake) mount/sb.
1940 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1942 * Loop over the bitmap blocks.
1943 * We will do everything one bitmap block at a time.
1944 * Skip the current block if it is exactly full.
1945 * This also deals with the case where there were no rtextents before.
1947 for (bmbno = sbp->sb_rbmblocks -
1948 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1954 * Calculate new sb and mount fields for this round.
1956 nsbp->sb_rextsize = in->extsize;
1957 nsbp->sb_rbmblocks = bmbno + 1;
1960 nsbp->sb_rbmblocks * NBBY *
1961 nsbp->sb_blocksize * nsbp->sb_rextsize);
1962 nsbp->sb_rextents = nsbp->sb_rblocks;
1963 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1964 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1965 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1967 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1969 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1970 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1972 * Start a transaction, get the log reservation.
1974 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1976 if ((error = xfs_trans_reserve(tp, 0,
1977 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1980 * Lock out other callers by grabbing the bitmap inode lock.
1982 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1983 XFS_ILOCK_EXCL, &ip)))
1985 ASSERT(ip == mp->m_rbmip);
1987 * Update the bitmap inode's size.
1989 mp->m_rbmip->i_d.di_size =
1990 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1991 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1992 cancelflags |= XFS_TRANS_ABORT;
1994 * Get the summary inode into the transaction.
1996 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1997 XFS_ILOCK_EXCL, &ip)))
1999 ASSERT(ip == mp->m_rsumip);
2001 * Update the summary inode's size.
2003 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2004 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2006 * Copy summary data from old to new sizes.
2007 * Do this when the real size (not block-aligned) changes.
2009 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2010 mp->m_rsumlevels != nmp->m_rsumlevels) {
2011 error = xfs_rtcopy_summary(mp, nmp, tp);
2016 * Update superblock fields.
2018 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2019 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2020 nsbp->sb_rextsize - sbp->sb_rextsize);
2021 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2022 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2023 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2024 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2025 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2026 nsbp->sb_rblocks - sbp->sb_rblocks);
2027 if (nsbp->sb_rextents != sbp->sb_rextents)
2028 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2029 nsbp->sb_rextents - sbp->sb_rextents);
2030 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2031 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2032 nsbp->sb_rextslog - sbp->sb_rextslog);
2037 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2038 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2042 * Mark more blocks free in the superblock.
2044 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2045 nsbp->sb_rextents - sbp->sb_rextents);
2047 * Update mp values into the real mp structure.
2049 mp->m_rsumlevels = nrsumlevels;
2050 mp->m_rsumsize = nrsumsize;
2052 error = xfs_trans_commit(tp, 0);
2060 xfs_trans_cancel(tp, cancelflags);
2063 * Free the fake mp structure.
2071 * Allocate an extent in the realtime subvolume, with the usual allocation
2072 * parameters. The length units are all in realtime extents, as is the
2073 * result block number.
2076 xfs_rtallocate_extent(
2077 xfs_trans_t *tp, /* transaction pointer */
2078 xfs_rtblock_t bno, /* starting block number to allocate */
2079 xfs_extlen_t minlen, /* minimum length to allocate */
2080 xfs_extlen_t maxlen, /* maximum length to allocate */
2081 xfs_extlen_t *len, /* out: actual length allocated */
2082 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2083 int wasdel, /* was a delayed allocation extent */
2084 xfs_extlen_t prod, /* extent product factor */
2085 xfs_rtblock_t *rtblock) /* out: start block allocated */
2087 int error; /* error value */
2088 xfs_inode_t *ip; /* inode for bitmap file */
2089 xfs_mount_t *mp; /* file system mount structure */
2090 xfs_rtblock_t r; /* result allocated block */
2091 xfs_fsblock_t sb; /* summary file block number */
2092 xfs_buf_t *sumbp; /* summary file block buffer */
2094 ASSERT(minlen > 0 && minlen <= maxlen);
2097 * If prod is set then figure out what to do to minlen and maxlen.
2102 if ((i = maxlen % prod))
2104 if ((i = minlen % prod))
2106 if (maxlen < minlen) {
2107 *rtblock = NULLRTBLOCK;
2112 * Lock out other callers by grabbing the bitmap inode lock.
2114 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2115 XFS_ILOCK_EXCL, &ip)))
2119 * Allocate by size, or near another block, or exactly at some block.
2122 case XFS_ALLOCTYPE_ANY_AG:
2123 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2124 &sumbp, &sb, prod, &r);
2126 case XFS_ALLOCTYPE_NEAR_BNO:
2127 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2128 len, &sumbp, &sb, prod, &r);
2130 case XFS_ALLOCTYPE_THIS_BNO:
2131 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2132 len, &sumbp, &sb, prod, &r);
2141 * If it worked, update the superblock.
2143 if (r != NULLRTBLOCK) {
2144 long slen = (long)*len;
2146 ASSERT(*len >= minlen && *len <= maxlen);
2148 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2150 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2157 * Free an extent in the realtime subvolume. Length is expressed in
2158 * realtime extents, as is the block number.
2162 xfs_trans_t *tp, /* transaction pointer */
2163 xfs_rtblock_t bno, /* starting block number to free */
2164 xfs_extlen_t len) /* length of extent freed */
2166 int error; /* error value */
2167 xfs_inode_t *ip; /* bitmap file inode */
2168 xfs_mount_t *mp; /* file system mount structure */
2169 xfs_fsblock_t sb; /* summary file block number */
2170 xfs_buf_t *sumbp; /* summary file block buffer */
2174 * Synchronize by locking the bitmap inode.
2176 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2177 XFS_ILOCK_EXCL, &ip)))
2179 #if defined(__KERNEL__) && defined(DEBUG)
2181 * Check to see that this whole range is currently allocated.
2184 int stat; /* result from checking range */
2186 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2195 * Free the range of realtime blocks.
2197 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2202 * Mark more blocks free in the superblock.
2204 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2206 * If we've now freed all the blocks, reset the file sequence
2209 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2210 mp->m_sb.sb_rextents) {
2211 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2212 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2213 *(__uint64_t *)&ip->i_d.di_atime = 0;
2214 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2220 * Initialize realtime fields in the mount structure.
2224 xfs_mount_t *mp) /* file system mount structure */
2226 xfs_buf_t *bp; /* buffer for last block of subvolume */
2227 xfs_daddr_t d; /* address of last block of subvolume */
2228 int error; /* error return value */
2229 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2232 if (sbp->sb_rblocks == 0)
2234 if (mp->m_rtdev_targp == NULL) {
2236 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2237 return XFS_ERROR(ENODEV);
2239 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2241 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2243 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2244 mp->m_rbmip = mp->m_rsumip = NULL;
2246 * Check that the realtime section is an ok size.
2248 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2249 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2250 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2251 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2252 (unsigned long long) mp->m_sb.sb_rblocks);
2253 return XFS_ERROR(E2BIG);
2255 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2256 d - XFS_FSB_TO_BB(mp, 1),
2257 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2260 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2261 if (error == ENOSPC)
2262 return XFS_ERROR(E2BIG);
2270 * Get the bitmap and summary inodes into the mount structure
2275 xfs_mount_t *mp) /* file system mount structure */
2277 int error; /* error return value */
2281 if (sbp->sb_rbmino == NULLFSINO)
2283 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2286 ASSERT(mp->m_rbmip != NULL);
2287 ASSERT(sbp->sb_rsumino != NULLFSINO);
2288 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2293 ASSERT(mp->m_rsumip != NULL);
2298 * Pick an extent for allocation at the start of a new realtime file.
2299 * Use the sequence number stored in the atime field of the bitmap inode.
2300 * Translate this to a fraction of the rtextents, and return the product
2301 * of rtextents and the fraction.
2302 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2306 xfs_mount_t *mp, /* file system mount point */
2307 xfs_trans_t *tp, /* transaction pointer */
2308 xfs_extlen_t len, /* allocation length (rtextents) */
2309 xfs_rtblock_t *pick) /* result rt extent */
2311 xfs_rtblock_t b; /* result block */
2312 int error; /* error return value */
2313 xfs_inode_t *ip; /* bitmap incore inode */
2314 int log2; /* log of sequence number */
2315 __uint64_t resid; /* residual after log removed */
2316 __uint64_t seq; /* sequence number of file creation */
2317 __uint64_t *seqp; /* pointer to seqno in inode */
2319 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2320 XFS_ILOCK_EXCL, &ip)))
2322 ASSERT(ip == mp->m_rbmip);
2323 seqp = (__uint64_t *)&ip->i_d.di_atime;
2324 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2325 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2329 if ((log2 = xfs_highbit64(seq)) == -1)
2332 resid = seq - (1ULL << log2);
2333 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2335 if (b >= mp->m_sb.sb_rextents)
2336 b = do_mod(b, mp->m_sb.sb_rextents);
2337 if (b + len > mp->m_sb.sb_rextents)
2338 b = mp->m_sb.sb_rextents - len;
2341 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);