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 * Allocate space to the bitmap or summary file, and zero it, for growfs.
79 STATIC int /* error */
81 xfs_mount_t *mp, /* file system mount point */
82 xfs_extlen_t oblocks, /* old count of blocks */
83 xfs_extlen_t nblocks, /* new count of blocks */
84 xfs_ino_t ino) /* inode number (bitmap/summary) */
86 xfs_fileoff_t bno; /* block number in file */
87 xfs_buf_t *bp; /* temporary buffer for zeroing */
88 int committed; /* transaction committed flag */
89 xfs_daddr_t d; /* disk block address */
90 int error; /* error return value */
91 xfs_fsblock_t firstblock; /* first block allocated in xaction */
92 xfs_bmap_free_t flist; /* list of freed blocks */
93 xfs_fsblock_t fsbno; /* filesystem block for bno */
94 xfs_inode_t *ip; /* pointer to incore inode */
95 xfs_bmbt_irec_t map; /* block map output */
96 int nmap; /* number of block maps */
97 int resblks; /* space reservation */
100 * Allocate space to the file, as necessary.
102 while (oblocks < nblocks) {
106 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
107 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
109 * Reserve space & log for one extent added to the file.
111 if ((error = xfs_trans_reserve(tp, resblks,
112 XFS_GROWRTALLOC_LOG_RES(mp), 0,
113 XFS_TRANS_PERM_LOG_RES,
114 XFS_DEFAULT_PERM_LOG_COUNT)))
116 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
120 if ((error = xfs_trans_iget(mp, tp, ino, 0,
121 XFS_ILOCK_EXCL, &ip)))
123 xfs_bmap_init(&flist, &firstblock);
125 * Allocate blocks to the bitmap file.
128 cancelflags |= XFS_TRANS_ABORT;
129 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
130 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
131 resblks, &map, &nmap, &flist, NULL);
132 if (!error && nmap < 1)
133 error = XFS_ERROR(ENOSPC);
137 * Free any blocks freed up in the transaction, then commit.
139 error = xfs_bmap_finish(&tp, &flist, &committed);
142 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
146 * Now we need to clear the allocated blocks.
147 * Do this one block per transaction, to keep it simple.
150 for (bno = map.br_startoff, fsbno = map.br_startblock;
151 bno < map.br_startoff + map.br_blockcount;
153 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
155 * Reserve log for one block zeroing.
157 if ((error = xfs_trans_reserve(tp, 0,
158 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
161 * Lock the bitmap inode.
163 if ((error = xfs_trans_iget(mp, tp, ino, 0,
164 XFS_ILOCK_EXCL, &ip)))
167 * Get a buffer for the block.
169 d = XFS_FSB_TO_DADDR(mp, fsbno);
170 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
173 error = XFS_ERROR(EIO);
175 xfs_trans_cancel(tp, cancelflags);
178 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
179 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
181 * Commit the transaction.
183 error = xfs_trans_commit(tp, 0);
188 * Go on to the next extent, if any.
190 oblocks = map.br_startoff + map.br_blockcount;
198 * Attempt to allocate an extent minlen<=len<=maxlen starting from
199 * bitmap block bbno. If we don't get maxlen then use prod to trim
200 * the length, if given. Returns error; returns starting block in *rtblock.
201 * The lengths are all in rtextents.
203 STATIC int /* error */
204 xfs_rtallocate_extent_block(
205 xfs_mount_t *mp, /* file system mount point */
206 xfs_trans_t *tp, /* transaction pointer */
207 xfs_rtblock_t bbno, /* bitmap block number */
208 xfs_extlen_t minlen, /* minimum length to allocate */
209 xfs_extlen_t maxlen, /* maximum length to allocate */
210 xfs_extlen_t *len, /* out: actual length allocated */
211 xfs_rtblock_t *nextp, /* out: next block to try */
212 xfs_buf_t **rbpp, /* in/out: summary block buffer */
213 xfs_fsblock_t *rsb, /* in/out: summary block number */
214 xfs_extlen_t prod, /* extent product factor */
215 xfs_rtblock_t *rtblock) /* out: start block allocated */
217 xfs_rtblock_t besti; /* best rtblock found so far */
218 xfs_rtblock_t bestlen; /* best length found so far */
219 xfs_rtblock_t end; /* last rtblock in chunk */
220 int error; /* error value */
221 xfs_rtblock_t i; /* current rtblock trying */
222 xfs_rtblock_t next; /* next rtblock to try */
223 int stat; /* status from internal calls */
226 * Loop over all the extents starting in this bitmap block,
227 * looking for one that's long enough.
229 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
230 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
234 * See if there's a free extent of maxlen starting at i.
235 * If it's not so then next will contain the first non-free.
237 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
243 * i for maxlen is all free, allocate and return that.
245 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
255 * In the case where we have a variable-sized allocation
256 * request, figure out how big this free piece is,
257 * and if it's big enough for the minimum, and the best
258 * so far, remember it.
260 if (minlen < maxlen) {
261 xfs_rtblock_t thislen; /* this extent size */
264 if (thislen >= minlen && thislen > bestlen) {
270 * If not done yet, find the start of the next free space.
273 error = xfs_rtfind_forw(mp, tp, next, end, &i);
281 * Searched the whole thing & didn't find a maxlen free extent.
283 if (minlen < maxlen && besti != -1) {
284 xfs_extlen_t p; /* amount to trim length by */
287 * If size should be a multiple of prod, make that so.
289 if (prod > 1 && (p = do_mod(bestlen, prod)))
292 * Allocate besti for bestlen & return that.
294 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
303 * Allocation failed. Set *nextp to the next block to try.
306 *rtblock = NULLRTBLOCK;
311 * Allocate an extent of length minlen<=len<=maxlen, starting at block
312 * bno. If we don't get maxlen then use prod to trim the length, if given.
313 * Returns error; returns starting block in *rtblock.
314 * The lengths are all in rtextents.
316 STATIC int /* error */
317 xfs_rtallocate_extent_exact(
318 xfs_mount_t *mp, /* file system mount point */
319 xfs_trans_t *tp, /* transaction pointer */
320 xfs_rtblock_t bno, /* starting block number to allocate */
321 xfs_extlen_t minlen, /* minimum length to allocate */
322 xfs_extlen_t maxlen, /* maximum length to allocate */
323 xfs_extlen_t *len, /* out: actual length allocated */
324 xfs_buf_t **rbpp, /* in/out: summary block buffer */
325 xfs_fsblock_t *rsb, /* in/out: summary block number */
326 xfs_extlen_t prod, /* extent product factor */
327 xfs_rtblock_t *rtblock) /* out: start block allocated */
329 int error; /* error value */
330 xfs_extlen_t i; /* extent length trimmed due to prod */
331 int isfree; /* extent is free */
332 xfs_rtblock_t next; /* next block to try (dummy) */
334 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
336 * Check if the range in question (for maxlen) is free.
338 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
344 * If it is, allocate it and return success.
346 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
355 * If not, allocate what there is, if it's at least minlen.
358 if (maxlen < minlen) {
360 * Failed, return failure status.
362 *rtblock = NULLRTBLOCK;
366 * Trim off tail of extent, if prod is specified.
368 if (prod > 1 && (i = maxlen % prod)) {
370 if (maxlen < minlen) {
372 * Now we can't do it, return failure status.
374 *rtblock = NULLRTBLOCK;
379 * Allocate what we can and return it.
381 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
391 * Allocate an extent of length minlen<=len<=maxlen, starting as near
392 * to bno as possible. If we don't get maxlen then use prod to trim
393 * the length, if given. The lengths are all in rtextents.
395 STATIC int /* error */
396 xfs_rtallocate_extent_near(
397 xfs_mount_t *mp, /* file system mount point */
398 xfs_trans_t *tp, /* transaction pointer */
399 xfs_rtblock_t bno, /* starting block number to allocate */
400 xfs_extlen_t minlen, /* minimum length to allocate */
401 xfs_extlen_t maxlen, /* maximum length to allocate */
402 xfs_extlen_t *len, /* out: actual length allocated */
403 xfs_buf_t **rbpp, /* in/out: summary block buffer */
404 xfs_fsblock_t *rsb, /* in/out: summary block number */
405 xfs_extlen_t prod, /* extent product factor */
406 xfs_rtblock_t *rtblock) /* out: start block allocated */
408 int any; /* any useful extents from summary */
409 xfs_rtblock_t bbno; /* bitmap block number */
410 int error; /* error value */
411 int i; /* bitmap block offset (loop control) */
412 int j; /* secondary loop control */
413 int log2len; /* log2 of minlen */
414 xfs_rtblock_t n; /* next block to try */
415 xfs_rtblock_t r; /* result block */
417 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
419 * If the block number given is off the end, silently set it to
422 if (bno >= mp->m_sb.sb_rextents)
423 bno = mp->m_sb.sb_rextents - 1;
425 * Try the exact allocation first.
427 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
428 rbpp, rsb, prod, &r);
433 * If the exact allocation worked, return that.
435 if (r != NULLRTBLOCK) {
439 bbno = XFS_BITTOBLOCK(mp, bno);
442 log2len = xfs_highbit32(minlen);
444 * Loop over all bitmap blocks (bbno + i is current block).
448 * Get summary information of extents of all useful levels
449 * starting in this bitmap block.
451 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
452 bbno + i, rbpp, rsb, &any);
457 * If there are any useful extents starting here, try
462 * On the positive side of the starting location.
466 * Try to allocate an extent starting in
469 error = xfs_rtallocate_extent_block(mp, tp,
470 bbno + i, minlen, maxlen, len, &n, rbpp,
476 * If it worked, return it.
478 if (r != NULLRTBLOCK) {
484 * On the negative side of the starting location.
488 * Loop backwards through the bitmap blocks from
489 * the starting point-1 up to where we are now.
490 * There should be an extent which ends in this
491 * bitmap block and is long enough.
493 for (j = -1; j > i; j--) {
495 * Grab the summary information for
498 error = xfs_rtany_summary(mp, tp,
499 log2len, mp->m_rsumlevels - 1,
500 bbno + j, rbpp, rsb, &any);
505 * If there's no extent given in the
506 * summary that means the extent we
507 * found must carry over from an
508 * earlier block. If there is an
509 * extent given, we've already tried
510 * that allocation, don't do it again.
514 error = xfs_rtallocate_extent_block(mp,
515 tp, bbno + j, minlen, maxlen,
516 len, &n, rbpp, rsb, prod, &r);
521 * If it works, return the extent.
523 if (r != NULLRTBLOCK) {
529 * There weren't intervening bitmap blocks
530 * with a long enough extent, or the
531 * allocation didn't work for some reason
532 * (i.e. it's a little * too short).
533 * Try to allocate from the summary block
536 error = xfs_rtallocate_extent_block(mp, tp,
537 bbno + i, minlen, maxlen, len, &n, rbpp,
543 * If it works, return the extent.
545 if (r != NULLRTBLOCK) {
552 * Loop control. If we were on the positive side, and there's
553 * still more blocks on the negative side, go there.
555 if (i > 0 && (int)bbno - i >= 0)
558 * If positive, and no more negative, but there are more
559 * positive, go there.
561 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
564 * If negative or 0 (just started), and there are positive
565 * blocks to go, go there. The 0 case moves to block 1.
567 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
570 * If negative or 0 and there are more negative blocks,
573 else if (i <= 0 && (int)bbno + i > 0)
576 * Must be done. Return failure.
581 *rtblock = NULLRTBLOCK;
586 * Allocate an extent of length minlen<=len<=maxlen, with no position
587 * specified. If we don't get maxlen then use prod to trim
588 * the length, if given. The lengths are all in rtextents.
590 STATIC int /* error */
591 xfs_rtallocate_extent_size(
592 xfs_mount_t *mp, /* file system mount point */
593 xfs_trans_t *tp, /* transaction pointer */
594 xfs_extlen_t minlen, /* minimum length to allocate */
595 xfs_extlen_t maxlen, /* maximum length to allocate */
596 xfs_extlen_t *len, /* out: actual length allocated */
597 xfs_buf_t **rbpp, /* in/out: summary block buffer */
598 xfs_fsblock_t *rsb, /* in/out: summary block number */
599 xfs_extlen_t prod, /* extent product factor */
600 xfs_rtblock_t *rtblock) /* out: start block allocated */
602 int error; /* error value */
603 int i; /* bitmap block number */
604 int l; /* level number (loop control) */
605 xfs_rtblock_t n; /* next block to be tried */
606 xfs_rtblock_t r; /* result block number */
607 xfs_suminfo_t sum; /* summary information for extents */
609 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
613 * Loop over all the levels starting with maxlen.
614 * At each level, look at all the bitmap blocks, to see if there
615 * are extents starting there that are long enough (>= maxlen).
616 * Note, only on the initial level can the allocation fail if
617 * the summary says there's an extent.
619 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
621 * Loop over all the bitmap blocks.
623 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
625 * Get the summary for this level/block.
627 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
633 * Nothing there, on to the next block.
638 * Try allocating the extent.
640 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
641 maxlen, len, &n, rbpp, rsb, prod, &r);
646 * If it worked, return that.
648 if (r != NULLRTBLOCK) {
653 * If the "next block to try" returned from the
654 * allocator is beyond the next bitmap block,
655 * skip to that bitmap block.
657 if (XFS_BITTOBLOCK(mp, n) > i + 1)
658 i = XFS_BITTOBLOCK(mp, n) - 1;
662 * Didn't find any maxlen blocks. Try smaller ones, unless
663 * we're asking for a fixed size extent.
665 if (minlen > --maxlen) {
666 *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);
917 * Check that the given range is either all allocated (val = 0) or
918 * all free (val = 1).
920 STATIC int /* error */
922 xfs_mount_t *mp, /* file system mount point */
923 xfs_trans_t *tp, /* transaction pointer */
924 xfs_rtblock_t start, /* starting block number of extent */
925 xfs_extlen_t len, /* length of extent */
926 int val, /* 1 for free, 0 for allocated */
927 xfs_rtblock_t *new, /* out: first block not matching */
928 int *stat) /* out: 1 for matches, 0 for not */
930 xfs_rtword_t *b; /* current word in buffer */
931 int bit; /* bit number in the word */
932 xfs_rtblock_t block; /* bitmap block number */
933 xfs_buf_t *bp; /* buf for the block */
934 xfs_rtword_t *bufp; /* starting word in buffer */
935 int error; /* error value */
936 xfs_rtblock_t i; /* current bit number rel. to start */
937 xfs_rtblock_t lastbit; /* last useful bit in word */
938 xfs_rtword_t mask; /* mask of relevant bits for value */
939 xfs_rtword_t wdiff; /* difference from wanted value */
940 int word; /* word number in the buffer */
943 * Compute starting bitmap block number
945 block = XFS_BITTOBLOCK(mp, start);
947 * Read the bitmap block.
949 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
953 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
955 * Compute the starting word's address, and starting bit.
957 word = XFS_BITTOWORD(mp, start);
959 bit = (int)(start & (XFS_NBWORD - 1));
961 * 0 (allocated) => all zero's; 1 (free) => all one's.
965 * If not starting on a word boundary, deal with the first
970 * Compute first bit not examined.
972 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
974 * Mask of relevant bits.
976 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
978 * Compute difference between actual and desired value.
980 if ((wdiff = (*b ^ val) & mask)) {
982 * Different, compute first wrong bit and return.
984 xfs_trans_brelse(tp, bp);
985 i = XFS_RTLOBIT(wdiff) - bit;
992 * Go on to next block if that's where the next word is
993 * and we need the next word.
995 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
997 * If done with this block, get the next one.
999 xfs_trans_brelse(tp, bp);
1000 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1004 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1008 * Go on to the next word in the buffer.
1014 * Starting on a word boundary, no partial word.
1019 * Loop over whole words in buffers. When we use up one buffer
1020 * we move on to the next one.
1022 while (len - i >= XFS_NBWORD) {
1024 * Compute difference between actual and desired value.
1026 if ((wdiff = *b ^ val)) {
1028 * Different, compute first wrong bit and return.
1030 xfs_trans_brelse(tp, bp);
1031 i += XFS_RTLOBIT(wdiff);
1038 * Go on to next block if that's where the next word is
1039 * and we need the next word.
1041 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1043 * If done with this block, get the next one.
1045 xfs_trans_brelse(tp, bp);
1046 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1050 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1054 * Go on to the next word in the buffer.
1060 * If not ending on a word boundary, deal with the last
1063 if ((lastbit = len - i)) {
1065 * Mask of relevant bits.
1067 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1069 * Compute difference between actual and desired value.
1071 if ((wdiff = (*b ^ val) & mask)) {
1073 * Different, compute first wrong bit and return.
1075 xfs_trans_brelse(tp, bp);
1076 i += XFS_RTLOBIT(wdiff);
1084 * Successful, return.
1086 xfs_trans_brelse(tp, bp);
1093 * Copy and transform the summary file, given the old and new
1094 * parameters in the mount structures.
1096 STATIC int /* error */
1098 xfs_mount_t *omp, /* old file system mount point */
1099 xfs_mount_t *nmp, /* new file system mount point */
1100 xfs_trans_t *tp) /* transaction pointer */
1102 xfs_rtblock_t bbno; /* bitmap block number */
1103 xfs_buf_t *bp; /* summary buffer */
1104 int error; /* error return value */
1105 int log; /* summary level number (log length) */
1106 xfs_suminfo_t sum; /* summary data */
1107 xfs_fsblock_t sumbno; /* summary block number */
1110 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1111 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1112 (xfs_srtblock_t)bbno >= 0;
1114 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1120 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1124 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1135 * Searching backward from start to limit, find the first block whose
1136 * allocated/free state is different from start's.
1138 STATIC int /* error */
1140 xfs_mount_t *mp, /* file system mount point */
1141 xfs_trans_t *tp, /* transaction pointer */
1142 xfs_rtblock_t start, /* starting block to look at */
1143 xfs_rtblock_t limit, /* last block to look at */
1144 xfs_rtblock_t *rtblock) /* out: start block found */
1146 xfs_rtword_t *b; /* current word in buffer */
1147 int bit; /* bit number in the word */
1148 xfs_rtblock_t block; /* bitmap block number */
1149 xfs_buf_t *bp; /* buf for the block */
1150 xfs_rtword_t *bufp; /* starting word in buffer */
1151 int error; /* error value */
1152 xfs_rtblock_t firstbit; /* first useful bit in the word */
1153 xfs_rtblock_t i; /* current bit number rel. to start */
1154 xfs_rtblock_t len; /* length of inspected area */
1155 xfs_rtword_t mask; /* mask of relevant bits for value */
1156 xfs_rtword_t want; /* mask for "good" values */
1157 xfs_rtword_t wdiff; /* difference from wanted value */
1158 int word; /* word number in the buffer */
1161 * Compute and read in starting bitmap block for starting block.
1163 block = XFS_BITTOBLOCK(mp, start);
1164 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1168 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1170 * Get the first word's index & point to it.
1172 word = XFS_BITTOWORD(mp, start);
1174 bit = (int)(start & (XFS_NBWORD - 1));
1175 len = start - limit + 1;
1177 * Compute match value, based on the bit at start: if 1 (free)
1178 * then all-ones, else all-zeroes.
1180 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1182 * If the starting position is not word-aligned, deal with the
1185 if (bit < XFS_NBWORD - 1) {
1187 * Calculate first (leftmost) bit number to look at,
1188 * and mask for all the relevant bits in this word.
1190 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1191 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1194 * Calculate the difference between the value there
1195 * and what we're looking for.
1197 if ((wdiff = (*b ^ want) & mask)) {
1199 * Different. Mark where we are and return.
1201 xfs_trans_brelse(tp, bp);
1202 i = bit - XFS_RTHIBIT(wdiff);
1203 *rtblock = start - i + 1;
1206 i = bit - firstbit + 1;
1208 * Go on to previous block if that's where the previous word is
1209 * and we need the previous word.
1211 if (--word == -1 && i < len) {
1213 * If done with this block, get the previous one.
1215 xfs_trans_brelse(tp, bp);
1216 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1220 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1221 word = XFS_BLOCKWMASK(mp);
1225 * Go on to the previous word in the buffer.
1231 * Starting on a word boundary, no partial word.
1236 * Loop over whole words in buffers. When we use up one buffer
1237 * we move on to the previous one.
1239 while (len - i >= XFS_NBWORD) {
1241 * Compute difference between actual and desired value.
1243 if ((wdiff = *b ^ want)) {
1245 * Different, mark where we are and return.
1247 xfs_trans_brelse(tp, bp);
1248 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1249 *rtblock = start - i + 1;
1254 * Go on to previous block if that's where the previous word is
1255 * and we need the previous word.
1257 if (--word == -1 && i < len) {
1259 * If done with this block, get the previous one.
1261 xfs_trans_brelse(tp, bp);
1262 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1266 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1267 word = XFS_BLOCKWMASK(mp);
1271 * Go on to the previous word in the buffer.
1277 * If not ending on a word boundary, deal with the last
1282 * Calculate first (leftmost) bit number to look at,
1283 * and mask for all the relevant bits in this word.
1285 firstbit = XFS_NBWORD - (len - i);
1286 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1288 * Compute difference between actual and desired value.
1290 if ((wdiff = (*b ^ want) & mask)) {
1292 * Different, mark where we are and return.
1294 xfs_trans_brelse(tp, bp);
1295 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1296 *rtblock = start - i + 1;
1302 * No match, return that we scanned the whole area.
1304 xfs_trans_brelse(tp, bp);
1305 *rtblock = start - i + 1;
1310 * Searching forward from start to limit, find the first block whose
1311 * allocated/free state is different from start's.
1313 STATIC int /* error */
1315 xfs_mount_t *mp, /* file system mount point */
1316 xfs_trans_t *tp, /* transaction pointer */
1317 xfs_rtblock_t start, /* starting block to look at */
1318 xfs_rtblock_t limit, /* last block to look at */
1319 xfs_rtblock_t *rtblock) /* out: start block found */
1321 xfs_rtword_t *b; /* current word in buffer */
1322 int bit; /* bit number in the word */
1323 xfs_rtblock_t block; /* bitmap block number */
1324 xfs_buf_t *bp; /* buf for the block */
1325 xfs_rtword_t *bufp; /* starting word in buffer */
1326 int error; /* error value */
1327 xfs_rtblock_t i; /* current bit number rel. to start */
1328 xfs_rtblock_t lastbit; /* last useful bit in the word */
1329 xfs_rtblock_t len; /* length of inspected area */
1330 xfs_rtword_t mask; /* mask of relevant bits for value */
1331 xfs_rtword_t want; /* mask for "good" values */
1332 xfs_rtword_t wdiff; /* difference from wanted value */
1333 int word; /* word number in the buffer */
1336 * Compute and read in starting bitmap block for starting block.
1338 block = XFS_BITTOBLOCK(mp, start);
1339 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1343 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1345 * Get the first word's index & point to it.
1347 word = XFS_BITTOWORD(mp, start);
1349 bit = (int)(start & (XFS_NBWORD - 1));
1350 len = limit - start + 1;
1352 * Compute match value, based on the bit at start: if 1 (free)
1353 * then all-ones, else all-zeroes.
1355 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1357 * If the starting position is not word-aligned, deal with the
1362 * Calculate last (rightmost) bit number to look at,
1363 * and mask for all the relevant bits in this word.
1365 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1366 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1368 * Calculate the difference between the value there
1369 * and what we're looking for.
1371 if ((wdiff = (*b ^ want) & mask)) {
1373 * Different. Mark where we are and return.
1375 xfs_trans_brelse(tp, bp);
1376 i = XFS_RTLOBIT(wdiff) - bit;
1377 *rtblock = start + i - 1;
1382 * Go on to next block if that's where the next word is
1383 * and we need the next word.
1385 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1387 * If done with this block, get the previous one.
1389 xfs_trans_brelse(tp, bp);
1390 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1394 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1398 * Go on to the previous word in the buffer.
1404 * Starting on a word boundary, no partial word.
1409 * Loop over whole words in buffers. When we use up one buffer
1410 * we move on to the next one.
1412 while (len - i >= XFS_NBWORD) {
1414 * Compute difference between actual and desired value.
1416 if ((wdiff = *b ^ want)) {
1418 * Different, mark where we are and return.
1420 xfs_trans_brelse(tp, bp);
1421 i += XFS_RTLOBIT(wdiff);
1422 *rtblock = start + i - 1;
1427 * Go on to next block if that's where the next word is
1428 * and we need the next word.
1430 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1432 * If done with this block, get the next one.
1434 xfs_trans_brelse(tp, bp);
1435 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1439 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1443 * Go on to the next word in the buffer.
1449 * If not ending on a word boundary, deal with the last
1452 if ((lastbit = len - i)) {
1454 * Calculate mask for all the relevant bits in this word.
1456 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1458 * Compute difference between actual and desired value.
1460 if ((wdiff = (*b ^ want) & mask)) {
1462 * Different, mark where we are and return.
1464 xfs_trans_brelse(tp, bp);
1465 i += XFS_RTLOBIT(wdiff);
1466 *rtblock = start + i - 1;
1472 * No match, return that we scanned the whole area.
1474 xfs_trans_brelse(tp, bp);
1475 *rtblock = start + i - 1;
1480 * Mark an extent specified by start and len freed.
1481 * Updates all the summary information as well as the bitmap.
1483 STATIC int /* error */
1485 xfs_mount_t *mp, /* file system mount point */
1486 xfs_trans_t *tp, /* transaction pointer */
1487 xfs_rtblock_t start, /* starting block to free */
1488 xfs_extlen_t len, /* length to free */
1489 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1490 xfs_fsblock_t *rsb) /* in/out: summary block number */
1492 xfs_rtblock_t end; /* end of the freed extent */
1493 int error; /* error value */
1494 xfs_rtblock_t postblock; /* first block freed > end */
1495 xfs_rtblock_t preblock; /* first block freed < start */
1497 end = start + len - 1;
1499 * Modify the bitmap to mark this extent freed.
1501 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1506 * Assume we're freeing out of the middle of an allocated extent.
1507 * We need to find the beginning and end of the extent so we can
1508 * properly update the summary.
1510 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1515 * Find the next allocated block (end of allocated extent).
1517 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1520 * If there are blocks not being freed at the front of the
1521 * old extent, add summary data for them to be allocated.
1523 if (preblock < start) {
1524 error = xfs_rtmodify_summary(mp, tp,
1525 XFS_RTBLOCKLOG(start - preblock),
1526 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1532 * If there are blocks not being freed at the end of the
1533 * old extent, add summary data for them to be allocated.
1535 if (postblock > end) {
1536 error = xfs_rtmodify_summary(mp, tp,
1537 XFS_RTBLOCKLOG(postblock - end),
1538 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1544 * Increment the summary information corresponding to the entire
1545 * (new) free extent.
1547 error = xfs_rtmodify_summary(mp, tp,
1548 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1549 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1554 * Read and return the summary information for a given extent size,
1555 * bitmap block combination.
1556 * Keeps track of a current summary block, so we don't keep reading
1557 * it from the buffer cache.
1559 STATIC int /* error */
1561 xfs_mount_t *mp, /* file system mount structure */
1562 xfs_trans_t *tp, /* transaction pointer */
1563 int log, /* log2 of extent size */
1564 xfs_rtblock_t bbno, /* bitmap block number */
1565 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1566 xfs_fsblock_t *rsb, /* in/out: summary block number */
1567 xfs_suminfo_t *sum) /* out: summary info for this block */
1569 xfs_buf_t *bp; /* buffer for summary block */
1570 int error; /* error value */
1571 xfs_fsblock_t sb; /* summary fsblock */
1572 int so; /* index into the summary file */
1573 xfs_suminfo_t *sp; /* pointer to returned data */
1576 * Compute entry number in the summary file.
1578 so = XFS_SUMOFFS(mp, log, bbno);
1580 * Compute the block number in the summary file.
1582 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1584 * If we have an old buffer, and the block number matches, use that.
1586 if (rbpp && *rbpp && *rsb == sb)
1589 * Otherwise we have to get the buffer.
1593 * If there was an old one, get rid of it first.
1596 xfs_trans_brelse(tp, *rbpp);
1597 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1602 * Remember this buffer and block for the next call.
1610 * Point to the summary information & copy it out.
1612 sp = XFS_SUMPTR(mp, bp, so);
1615 * Drop the buffer if we're not asked to remember it.
1618 xfs_trans_brelse(tp, bp);
1623 * Set the given range of bitmap bits to the given value.
1624 * Do whatever I/O and logging is required.
1626 STATIC int /* error */
1628 xfs_mount_t *mp, /* file system mount point */
1629 xfs_trans_t *tp, /* transaction pointer */
1630 xfs_rtblock_t start, /* starting block to modify */
1631 xfs_extlen_t len, /* length of extent to modify */
1632 int val) /* 1 for free, 0 for allocated */
1634 xfs_rtword_t *b; /* current word in buffer */
1635 int bit; /* bit number in the word */
1636 xfs_rtblock_t block; /* bitmap block number */
1637 xfs_buf_t *bp; /* buf for the block */
1638 xfs_rtword_t *bufp; /* starting word in buffer */
1639 int error; /* error value */
1640 xfs_rtword_t *first; /* first used word in the buffer */
1641 int i; /* current bit number rel. to start */
1642 int lastbit; /* last useful bit in word */
1643 xfs_rtword_t mask; /* mask o frelevant bits for value */
1644 int word; /* word number in the buffer */
1647 * Compute starting bitmap block number.
1649 block = XFS_BITTOBLOCK(mp, start);
1651 * Read the bitmap block, and point to its data.
1653 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1657 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1659 * Compute the starting word's address, and starting bit.
1661 word = XFS_BITTOWORD(mp, start);
1662 first = b = &bufp[word];
1663 bit = (int)(start & (XFS_NBWORD - 1));
1665 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1669 * If not starting on a word boundary, deal with the first
1674 * Compute first bit not changed and mask of relevant bits.
1676 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1677 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1679 * Set/clear the active bits.
1687 * Go on to the next block if that's where the next word is
1688 * and we need the next word.
1690 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1692 * Log the changed part of this block.
1695 xfs_trans_log_buf(tp, bp,
1696 (uint)((char *)first - (char *)bufp),
1697 (uint)((char *)b - (char *)bufp));
1698 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1702 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1706 * Go on to the next word in the buffer
1712 * Starting on a word boundary, no partial word.
1717 * Loop over whole words in buffers. When we use up one buffer
1718 * we move on to the next one.
1720 while (len - i >= XFS_NBWORD) {
1722 * Set the word value correctly.
1727 * Go on to the next block if that's where the next word is
1728 * and we need the next word.
1730 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1732 * Log the changed part of this block.
1735 xfs_trans_log_buf(tp, bp,
1736 (uint)((char *)first - (char *)bufp),
1737 (uint)((char *)b - (char *)bufp));
1738 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1742 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1746 * Go on to the next word in the buffer
1752 * If not ending on a word boundary, deal with the last
1755 if ((lastbit = len - i)) {
1757 * Compute a mask of relevant bits.
1760 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1762 * Set/clear the active bits.
1771 * Log any remaining changed bytes.
1774 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1775 (uint)((char *)b - (char *)bufp - 1));
1780 * Read and modify the summary information for a given extent size,
1781 * bitmap block combination.
1782 * Keeps track of a current summary block, so we don't keep reading
1783 * it from the buffer cache.
1785 STATIC int /* error */
1786 xfs_rtmodify_summary(
1787 xfs_mount_t *mp, /* file system mount point */
1788 xfs_trans_t *tp, /* transaction pointer */
1789 int log, /* log2 of extent size */
1790 xfs_rtblock_t bbno, /* bitmap block number */
1791 int delta, /* change to make to summary info */
1792 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1793 xfs_fsblock_t *rsb) /* in/out: summary block number */
1795 xfs_buf_t *bp; /* buffer for the summary block */
1796 int error; /* error value */
1797 xfs_fsblock_t sb; /* summary fsblock */
1798 int so; /* index into the summary file */
1799 xfs_suminfo_t *sp; /* pointer to returned data */
1802 * Compute entry number in the summary file.
1804 so = XFS_SUMOFFS(mp, log, bbno);
1806 * Compute the block number in the summary file.
1808 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1810 * If we have an old buffer, and the block number matches, use that.
1812 if (rbpp && *rbpp && *rsb == sb)
1815 * Otherwise we have to get the buffer.
1819 * If there was an old one, get rid of it first.
1822 xfs_trans_brelse(tp, *rbpp);
1823 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1828 * Remember this buffer and block for the next call.
1836 * Point to the summary information, modify and log it.
1838 sp = XFS_SUMPTR(mp, bp, so);
1840 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1841 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1846 * Visible (exported) functions.
1850 * Grow the realtime area of the filesystem.
1854 xfs_mount_t *mp, /* mount point for filesystem */
1855 xfs_growfs_rt_t *in) /* growfs rt input struct */
1857 xfs_rtblock_t bmbno; /* bitmap block number */
1858 xfs_buf_t *bp; /* temporary buffer */
1859 int error; /* error return value */
1860 xfs_inode_t *ip; /* bitmap inode, used as lock */
1861 xfs_mount_t *nmp; /* new (fake) mount structure */
1862 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1863 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1864 xfs_drtbno_t nrextents; /* new number of realtime extents */
1865 uint8_t nrextslog; /* new log2 of sb_rextents */
1866 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1867 uint nrsumlevels; /* new rt summary levels */
1868 uint nrsumsize; /* new size of rt summary, bytes */
1869 xfs_sb_t *nsbp; /* new superblock */
1870 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1871 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1872 xfs_sb_t *sbp; /* old superblock */
1873 xfs_fsblock_t sumbno; /* summary block number */
1877 * Initial error checking.
1879 if (!capable(CAP_SYS_ADMIN))
1880 return XFS_ERROR(EPERM);
1881 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1882 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1883 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1884 return XFS_ERROR(EINVAL);
1885 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1888 * Read in the last block of the device, make sure it exists.
1890 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1891 XFS_FSB_TO_BB(mp, nrblocks - 1),
1892 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1898 * Calculate new parameters. These are the final values to be reached.
1900 nrextents = nrblocks;
1901 do_div(nrextents, in->extsize);
1902 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1903 nrextslog = xfs_highbit32(nrextents);
1904 nrsumlevels = nrextslog + 1;
1905 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1906 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1907 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1909 * New summary size can't be more than half the size of
1910 * the log. This prevents us from getting a log overflow,
1911 * since we'll log basically the whole summary file at once.
1913 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1914 return XFS_ERROR(EINVAL);
1916 * Get the old block counts for bitmap and summary inodes.
1917 * These can't change since other growfs callers are locked out.
1919 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1920 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1922 * Allocate space to the bitmap and summary files, as necessary.
1924 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1925 mp->m_sb.sb_rbmino)))
1927 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1928 mp->m_sb.sb_rsumino)))
1931 * Allocate a new (fake) mount/sb.
1933 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1935 * Loop over the bitmap blocks.
1936 * We will do everything one bitmap block at a time.
1937 * Skip the current block if it is exactly full.
1938 * This also deals with the case where there were no rtextents before.
1940 for (bmbno = sbp->sb_rbmblocks -
1941 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1945 int cancelflags = 0;
1950 * Calculate new sb and mount fields for this round.
1952 nsbp->sb_rextsize = in->extsize;
1953 nsbp->sb_rbmblocks = bmbno + 1;
1956 nsbp->sb_rbmblocks * NBBY *
1957 nsbp->sb_blocksize * nsbp->sb_rextsize);
1958 nsbp->sb_rextents = nsbp->sb_rblocks;
1959 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1960 ASSERT(nsbp->sb_rextents != 0);
1961 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1962 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1964 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1966 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1967 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1969 * Start a transaction, get the log reservation.
1971 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1972 if ((error = xfs_trans_reserve(tp, 0,
1973 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1976 * Lock out other callers by grabbing the bitmap inode lock.
1978 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1979 XFS_ILOCK_EXCL, &ip)))
1981 ASSERT(ip == mp->m_rbmip);
1983 * Update the bitmap inode's size.
1985 mp->m_rbmip->i_d.di_size =
1986 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1987 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1988 cancelflags |= XFS_TRANS_ABORT;
1990 * Get the summary inode into the transaction.
1992 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1993 XFS_ILOCK_EXCL, &ip)))
1995 ASSERT(ip == mp->m_rsumip);
1997 * Update the summary inode's size.
1999 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2000 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2002 * Copy summary data from old to new sizes.
2003 * Do this when the real size (not block-aligned) changes.
2005 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2006 mp->m_rsumlevels != nmp->m_rsumlevels) {
2007 error = xfs_rtcopy_summary(mp, nmp, tp);
2012 * Update superblock fields.
2014 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2015 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2016 nsbp->sb_rextsize - sbp->sb_rextsize);
2017 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2018 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2019 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2020 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2021 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2022 nsbp->sb_rblocks - sbp->sb_rblocks);
2023 if (nsbp->sb_rextents != sbp->sb_rextents)
2024 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2025 nsbp->sb_rextents - sbp->sb_rextents);
2026 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2027 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2028 nsbp->sb_rextslog - sbp->sb_rextslog);
2033 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2034 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2037 xfs_trans_cancel(tp, cancelflags);
2041 * Mark more blocks free in the superblock.
2043 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2044 nsbp->sb_rextents - sbp->sb_rextents);
2046 * Update mp values into the real mp structure.
2048 mp->m_rsumlevels = nrsumlevels;
2049 mp->m_rsumsize = nrsumsize;
2051 error = xfs_trans_commit(tp, 0);
2057 * Free the fake mp structure.
2065 * Allocate an extent in the realtime subvolume, with the usual allocation
2066 * parameters. The length units are all in realtime extents, as is the
2067 * result block number.
2070 xfs_rtallocate_extent(
2071 xfs_trans_t *tp, /* transaction pointer */
2072 xfs_rtblock_t bno, /* starting block number to allocate */
2073 xfs_extlen_t minlen, /* minimum length to allocate */
2074 xfs_extlen_t maxlen, /* maximum length to allocate */
2075 xfs_extlen_t *len, /* out: actual length allocated */
2076 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2077 int wasdel, /* was a delayed allocation extent */
2078 xfs_extlen_t prod, /* extent product factor */
2079 xfs_rtblock_t *rtblock) /* out: start block allocated */
2081 int error; /* error value */
2082 xfs_inode_t *ip; /* inode for bitmap file */
2083 xfs_mount_t *mp; /* file system mount structure */
2084 xfs_rtblock_t r; /* result allocated block */
2085 xfs_fsblock_t sb; /* summary file block number */
2086 xfs_buf_t *sumbp; /* summary file block buffer */
2088 ASSERT(minlen > 0 && minlen <= maxlen);
2091 * If prod is set then figure out what to do to minlen and maxlen.
2096 if ((i = maxlen % prod))
2098 if ((i = minlen % prod))
2100 if (maxlen < minlen) {
2101 *rtblock = NULLRTBLOCK;
2106 * Lock out other callers by grabbing the bitmap inode lock.
2108 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2109 XFS_ILOCK_EXCL, &ip)))
2113 * Allocate by size, or near another block, or exactly at some block.
2116 case XFS_ALLOCTYPE_ANY_AG:
2117 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2118 &sumbp, &sb, prod, &r);
2120 case XFS_ALLOCTYPE_NEAR_BNO:
2121 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2122 len, &sumbp, &sb, prod, &r);
2124 case XFS_ALLOCTYPE_THIS_BNO:
2125 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2126 len, &sumbp, &sb, prod, &r);
2135 * If it worked, update the superblock.
2137 if (r != NULLRTBLOCK) {
2138 long slen = (long)*len;
2140 ASSERT(*len >= minlen && *len <= maxlen);
2142 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2144 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2151 * Free an extent in the realtime subvolume. Length is expressed in
2152 * realtime extents, as is the block number.
2156 xfs_trans_t *tp, /* transaction pointer */
2157 xfs_rtblock_t bno, /* starting block number to free */
2158 xfs_extlen_t len) /* length of extent freed */
2160 int error; /* error value */
2161 xfs_inode_t *ip; /* bitmap file inode */
2162 xfs_mount_t *mp; /* file system mount structure */
2163 xfs_fsblock_t sb; /* summary file block number */
2164 xfs_buf_t *sumbp; /* summary file block buffer */
2168 * Synchronize by locking the bitmap inode.
2170 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2171 XFS_ILOCK_EXCL, &ip)))
2173 #if defined(__KERNEL__) && defined(DEBUG)
2175 * Check to see that this whole range is currently allocated.
2178 int stat; /* result from checking range */
2180 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2189 * Free the range of realtime blocks.
2191 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2196 * Mark more blocks free in the superblock.
2198 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2200 * If we've now freed all the blocks, reset the file sequence
2203 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2204 mp->m_sb.sb_rextents) {
2205 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2206 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2207 *(__uint64_t *)&ip->i_d.di_atime = 0;
2208 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2214 * Initialize realtime fields in the mount structure.
2218 xfs_mount_t *mp) /* file system mount structure */
2220 xfs_buf_t *bp; /* buffer for last block of subvolume */
2221 xfs_daddr_t d; /* address of last block of subvolume */
2222 int error; /* error return value */
2223 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2226 if (sbp->sb_rblocks == 0)
2228 if (mp->m_rtdev_targp == NULL) {
2230 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2231 return XFS_ERROR(ENODEV);
2233 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2235 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2237 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2238 mp->m_rbmip = mp->m_rsumip = NULL;
2240 * Check that the realtime section is an ok size.
2242 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2243 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2244 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2245 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2246 (unsigned long long) mp->m_sb.sb_rblocks);
2247 return XFS_ERROR(E2BIG);
2249 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2250 d - XFS_FSB_TO_BB(mp, 1),
2251 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2254 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2255 if (error == ENOSPC)
2256 return XFS_ERROR(E2BIG);
2264 * Get the bitmap and summary inodes into the mount structure
2269 xfs_mount_t *mp) /* file system mount structure */
2271 int error; /* error return value */
2275 if (sbp->sb_rbmino == NULLFSINO)
2277 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2280 ASSERT(mp->m_rbmip != NULL);
2281 ASSERT(sbp->sb_rsumino != NULLFSINO);
2282 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2287 ASSERT(mp->m_rsumip != NULL);
2292 * Pick an extent for allocation at the start of a new realtime file.
2293 * Use the sequence number stored in the atime field of the bitmap inode.
2294 * Translate this to a fraction of the rtextents, and return the product
2295 * of rtextents and the fraction.
2296 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2300 xfs_mount_t *mp, /* file system mount point */
2301 xfs_trans_t *tp, /* transaction pointer */
2302 xfs_extlen_t len, /* allocation length (rtextents) */
2303 xfs_rtblock_t *pick) /* result rt extent */
2305 xfs_rtblock_t b; /* result block */
2306 int error; /* error return value */
2307 xfs_inode_t *ip; /* bitmap incore inode */
2308 int log2; /* log of sequence number */
2309 __uint64_t resid; /* residual after log removed */
2310 __uint64_t seq; /* sequence number of file creation */
2311 __uint64_t *seqp; /* pointer to seqno in inode */
2313 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2314 XFS_ILOCK_EXCL, &ip)))
2316 ASSERT(ip == mp->m_rbmip);
2317 seqp = (__uint64_t *)&ip->i_d.di_atime;
2318 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2319 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2323 if ((log2 = xfs_highbit64(seq)) == -1)
2326 resid = seq - (1ULL << log2);
2327 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2329 if (b >= mp->m_sb.sb_rextents)
2330 b = do_mod(b, mp->m_sb.sb_rextents);
2331 if (b + len > mp->m_sb.sb_rextents)
2332 b = mp->m_sb.sb_rextents - len;
2335 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);