2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * Free realtime space allocation for XFS.
38 #include "xfs_macros.h"
39 #include "xfs_types.h"
42 #include "xfs_trans.h"
47 #include "xfs_dmapi.h"
48 #include "xfs_mount.h"
49 #include "xfs_alloc_btree.h"
50 #include "xfs_bmap_btree.h"
51 #include "xfs_ialloc_btree.h"
52 #include "xfs_btree.h"
53 #include "xfs_ialloc.h"
54 #include "xfs_attr_sf.h"
55 #include "xfs_dir_sf.h"
56 #include "xfs_dir2_sf.h"
57 #include "xfs_dinode.h"
58 #include "xfs_inode.h"
59 #include "xfs_alloc.h"
62 #include "xfs_rtalloc.h"
63 #include "xfs_fsops.h"
64 #include "xfs_error.h"
66 #include "xfs_inode_item.h"
67 #include "xfs_trans_space.h"
71 * Prototypes for internal functions.
75 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
76 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
77 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
78 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
79 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
80 xfs_extlen_t, int, xfs_rtblock_t *, int *);
81 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
82 xfs_rtblock_t, xfs_rtblock_t *);
83 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
84 xfs_rtblock_t, xfs_rtblock_t *);
85 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
86 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
87 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
89 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
90 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
97 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
107 * Allocate space to the bitmap or summary file, and zero it, for growfs.
109 STATIC int /* error */
111 xfs_mount_t *mp, /* file system mount point */
112 xfs_extlen_t oblocks, /* old count of blocks */
113 xfs_extlen_t nblocks, /* new count of blocks */
114 xfs_ino_t ino) /* inode number (bitmap/summary) */
116 xfs_fileoff_t bno; /* block number in file */
117 xfs_buf_t *bp; /* temporary buffer for zeroing */
118 int cancelflags; /* flags for xfs_trans_cancel */
119 int committed; /* transaction committed flag */
120 xfs_daddr_t d; /* disk block address */
121 int error; /* error return value */
122 xfs_fsblock_t firstblock; /* first block allocated in xaction */
123 xfs_bmap_free_t flist; /* list of freed blocks */
124 xfs_fsblock_t fsbno; /* filesystem block for bno */
125 xfs_inode_t *ip; /* pointer to incore inode */
126 xfs_bmbt_irec_t map; /* block map output */
127 int nmap; /* number of block maps */
128 int resblks; /* space reservation */
129 xfs_trans_t *tp; /* transaction pointer */
132 * Allocate space to the file, as necessary.
134 while (oblocks < nblocks) {
135 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
136 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
139 * Reserve space & log for one extent added to the file.
141 if ((error = xfs_trans_reserve(tp, resblks,
142 XFS_GROWRTALLOC_LOG_RES(mp), 0,
143 XFS_TRANS_PERM_LOG_RES,
144 XFS_DEFAULT_PERM_LOG_COUNT)))
146 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
150 if ((error = xfs_trans_iget(mp, tp, ino, XFS_ILOCK_EXCL, &ip)))
152 XFS_BMAP_INIT(&flist, &firstblock);
154 * Allocate blocks to the bitmap file.
157 cancelflags |= XFS_TRANS_ABORT;
158 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
159 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
160 resblks, &map, &nmap, &flist);
161 if (!error && nmap < 1)
162 error = XFS_ERROR(ENOSPC);
166 * Free any blocks freed up in the transaction, then commit.
168 error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
171 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
173 * Now we need to clear the allocated blocks.
174 * Do this one block per transaction, to keep it simple.
177 for (bno = map.br_startoff, fsbno = map.br_startblock;
178 bno < map.br_startoff + map.br_blockcount;
180 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
182 * Reserve log for one block zeroing.
184 if ((error = xfs_trans_reserve(tp, 0,
185 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
188 * Lock the bitmap inode.
190 if ((error = xfs_trans_iget(mp, tp, ino, XFS_ILOCK_EXCL,
194 * Get a buffer for the block.
196 d = XFS_FSB_TO_DADDR(mp, fsbno);
197 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
200 error = XFS_ERROR(EIO);
203 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
204 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
206 * Commit the transaction.
208 xfs_trans_commit(tp, 0, NULL);
211 * Go on to the next extent, if any.
213 oblocks = map.br_startoff + map.br_blockcount;
217 xfs_trans_cancel(tp, cancelflags);
222 * Attempt to allocate an extent minlen<=len<=maxlen starting from
223 * bitmap block bbno. If we don't get maxlen then use prod to trim
224 * the length, if given. Returns error; returns starting block in *rtblock.
225 * The lengths are all in rtextents.
227 STATIC int /* error */
228 xfs_rtallocate_extent_block(
229 xfs_mount_t *mp, /* file system mount point */
230 xfs_trans_t *tp, /* transaction pointer */
231 xfs_rtblock_t bbno, /* bitmap block number */
232 xfs_extlen_t minlen, /* minimum length to allocate */
233 xfs_extlen_t maxlen, /* maximum length to allocate */
234 xfs_extlen_t *len, /* out: actual length allocated */
235 xfs_rtblock_t *nextp, /* out: next block to try */
236 xfs_buf_t **rbpp, /* in/out: summary block buffer */
237 xfs_fsblock_t *rsb, /* in/out: summary block number */
238 xfs_extlen_t prod, /* extent product factor */
239 xfs_rtblock_t *rtblock) /* out: start block allocated */
241 xfs_rtblock_t besti; /* best rtblock found so far */
242 xfs_rtblock_t bestlen; /* best length found so far */
243 xfs_rtblock_t end; /* last rtblock in chunk */
244 int error; /* error value */
245 xfs_rtblock_t i; /* current rtblock trying */
246 xfs_rtblock_t next; /* next rtblock to try */
247 int stat; /* status from internal calls */
250 * Loop over all the extents starting in this bitmap block,
251 * looking for one that's long enough.
253 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
254 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
258 * See if there's a free extent of maxlen starting at i.
259 * If it's not so then next will contain the first non-free.
261 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
267 * i for maxlen is all free, allocate and return that.
269 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
279 * In the case where we have a variable-sized allocation
280 * request, figure out how big this free piece is,
281 * and if it's big enough for the minimum, and the best
282 * so far, remember it.
284 if (minlen < maxlen) {
285 xfs_rtblock_t thislen; /* this extent size */
288 if (thislen >= minlen && thislen > bestlen) {
294 * If not done yet, find the start of the next free space.
297 error = xfs_rtfind_forw(mp, tp, next, end, &i);
305 * Searched the whole thing & didn't find a maxlen free extent.
307 if (minlen < maxlen && besti != -1) {
308 xfs_extlen_t p; /* amount to trim length by */
311 * If size should be a multiple of prod, make that so.
313 if (prod > 1 && (p = do_mod(bestlen, prod)))
316 * Allocate besti for bestlen & return that.
318 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
327 * Allocation failed. Set *nextp to the next block to try.
330 *rtblock = NULLRTBLOCK;
335 * Allocate an extent of length minlen<=len<=maxlen, starting at block
336 * bno. If we don't get maxlen then use prod to trim the length, if given.
337 * Returns error; returns starting block in *rtblock.
338 * The lengths are all in rtextents.
340 STATIC int /* error */
341 xfs_rtallocate_extent_exact(
342 xfs_mount_t *mp, /* file system mount point */
343 xfs_trans_t *tp, /* transaction pointer */
344 xfs_rtblock_t bno, /* starting block number to allocate */
345 xfs_extlen_t minlen, /* minimum length to allocate */
346 xfs_extlen_t maxlen, /* maximum length to allocate */
347 xfs_extlen_t *len, /* out: actual length allocated */
348 xfs_buf_t **rbpp, /* in/out: summary block buffer */
349 xfs_fsblock_t *rsb, /* in/out: summary block number */
350 xfs_extlen_t prod, /* extent product factor */
351 xfs_rtblock_t *rtblock) /* out: start block allocated */
353 int error; /* error value */
354 xfs_extlen_t i; /* extent length trimmed due to prod */
355 int isfree; /* extent is free */
356 xfs_rtblock_t next; /* next block to try (dummy) */
358 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
360 * Check if the range in question (for maxlen) is free.
362 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
368 * If it is, allocate it and return success.
370 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
379 * If not, allocate what there is, if it's at least minlen.
382 if (maxlen < minlen) {
384 * Failed, return failure status.
386 *rtblock = NULLRTBLOCK;
390 * Trim off tail of extent, if prod is specified.
392 if (prod > 1 && (i = maxlen % prod)) {
394 if (maxlen < minlen) {
396 * Now we can't do it, return failure status.
398 *rtblock = NULLRTBLOCK;
403 * Allocate what we can and return it.
405 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
415 * Allocate an extent of length minlen<=len<=maxlen, starting as near
416 * to bno as possible. If we don't get maxlen then use prod to trim
417 * the length, if given. The lengths are all in rtextents.
419 STATIC int /* error */
420 xfs_rtallocate_extent_near(
421 xfs_mount_t *mp, /* file system mount point */
422 xfs_trans_t *tp, /* transaction pointer */
423 xfs_rtblock_t bno, /* starting block number to allocate */
424 xfs_extlen_t minlen, /* minimum length to allocate */
425 xfs_extlen_t maxlen, /* maximum length to allocate */
426 xfs_extlen_t *len, /* out: actual length allocated */
427 xfs_buf_t **rbpp, /* in/out: summary block buffer */
428 xfs_fsblock_t *rsb, /* in/out: summary block number */
429 xfs_extlen_t prod, /* extent product factor */
430 xfs_rtblock_t *rtblock) /* out: start block allocated */
432 int any; /* any useful extents from summary */
433 xfs_rtblock_t bbno; /* bitmap block number */
434 int error; /* error value */
435 int i; /* bitmap block offset (loop control) */
436 int j; /* secondary loop control */
437 int log2len; /* log2 of minlen */
438 xfs_rtblock_t n; /* next block to try */
439 xfs_rtblock_t r; /* result block */
441 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
443 * If the block number given is off the end, silently set it to
446 if (bno >= mp->m_sb.sb_rextents)
447 bno = mp->m_sb.sb_rextents - 1;
449 * Try the exact allocation first.
451 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
452 rbpp, rsb, prod, &r);
457 * If the exact allocation worked, return that.
459 if (r != NULLRTBLOCK) {
463 bbno = XFS_BITTOBLOCK(mp, bno);
465 log2len = xfs_highbit32(minlen);
467 * Loop over all bitmap blocks (bbno + i is current block).
471 * Get summary information of extents of all useful levels
472 * starting in this bitmap block.
474 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
475 bbno + i, rbpp, rsb, &any);
480 * If there are any useful extents starting here, try
485 * On the positive side of the starting location.
489 * Try to allocate an extent starting in
492 error = xfs_rtallocate_extent_block(mp, tp,
493 bbno + i, minlen, maxlen, len, &n, rbpp,
499 * If it worked, return it.
501 if (r != NULLRTBLOCK) {
507 * On the negative side of the starting location.
511 * Loop backwards through the bitmap blocks from
512 * the starting point-1 up to where we are now.
513 * There should be an extent which ends in this
514 * bitmap block and is long enough.
516 for (j = -1; j > i; j--) {
518 * Grab the summary information for
521 error = xfs_rtany_summary(mp, tp,
522 log2len, mp->m_rsumlevels - 1,
523 bbno + j, rbpp, rsb, &any);
528 * If there's no extent given in the
529 * summary that means the extent we
530 * found must carry over from an
531 * earlier block. If there is an
532 * extent given, we've already tried
533 * that allocation, don't do it again.
537 error = xfs_rtallocate_extent_block(mp,
538 tp, bbno + j, minlen, maxlen,
539 len, &n, rbpp, rsb, prod, &r);
544 * If it works, return the extent.
546 if (r != NULLRTBLOCK) {
552 * There weren't intervening bitmap blocks
553 * with a long enough extent, or the
554 * allocation didn't work for some reason
555 * (i.e. it's a little * too short).
556 * Try to allocate from the summary block
559 error = xfs_rtallocate_extent_block(mp, tp,
560 bbno + i, minlen, maxlen, len, &n, rbpp,
566 * If it works, return the extent.
568 if (r != NULLRTBLOCK) {
575 * Loop control. If we were on the positive side, and there's
576 * still more blocks on the negative side, go there.
578 if (i > 0 && (int)bbno - i >= 0)
581 * If positive, and no more negative, but there are more
582 * positive, go there.
584 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
587 * If negative or 0 (just started), and there are positive
588 * blocks to go, go there. The 0 case moves to block 1.
590 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
593 * If negative or 0 and there are more negative blocks,
596 else if (i <= 0 && (int)bbno + i > 0)
599 * Must be done. Return failure.
604 *rtblock = NULLRTBLOCK;
609 * Allocate an extent of length minlen<=len<=maxlen, with no position
610 * specified. If we don't get maxlen then use prod to trim
611 * the length, if given. The lengths are all in rtextents.
613 STATIC int /* error */
614 xfs_rtallocate_extent_size(
615 xfs_mount_t *mp, /* file system mount point */
616 xfs_trans_t *tp, /* transaction pointer */
617 xfs_extlen_t minlen, /* minimum length to allocate */
618 xfs_extlen_t maxlen, /* maximum length to allocate */
619 xfs_extlen_t *len, /* out: actual length allocated */
620 xfs_buf_t **rbpp, /* in/out: summary block buffer */
621 xfs_fsblock_t *rsb, /* in/out: summary block number */
622 xfs_extlen_t prod, /* extent product factor */
623 xfs_rtblock_t *rtblock) /* out: start block allocated */
625 int error; /* error value */
626 int i; /* bitmap block number */
627 int l; /* level number (loop control) */
628 xfs_rtblock_t n; /* next block to be tried */
629 xfs_rtblock_t r; /* result block number */
630 xfs_suminfo_t sum; /* summary information for extents */
632 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
634 * Loop over all the levels starting with maxlen.
635 * At each level, look at all the bitmap blocks, to see if there
636 * are extents starting there that are long enough (>= maxlen).
637 * Note, only on the initial level can the allocation fail if
638 * the summary says there's an extent.
640 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
642 * Loop over all the bitmap blocks.
644 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
646 * Get the summary for this level/block.
648 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
654 * Nothing there, on to the next block.
659 * Try allocating the extent.
661 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
662 maxlen, len, &n, rbpp, rsb, prod, &r);
667 * If it worked, return that.
669 if (r != NULLRTBLOCK) {
674 * If the "next block to try" returned from the
675 * allocator is beyond the next bitmap block,
676 * skip to that bitmap block.
678 if (XFS_BITTOBLOCK(mp, n) > i + 1)
679 i = XFS_BITTOBLOCK(mp, n) - 1;
683 * Didn't find any maxlen blocks. Try smaller ones, unless
684 * we're asking for a fixed size extent.
686 if (minlen > --maxlen) {
687 *rtblock = NULLRTBLOCK;
691 * Loop over sizes, from maxlen down to minlen.
692 * This time, when we do the allocations, allow smaller ones
695 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
697 * Loop over all the bitmap blocks, try an allocation
698 * starting in that block.
700 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
702 * Get the summary information for this level/block.
704 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
710 * If nothing there, go on to next.
715 * Try the allocation. Make sure the specified
716 * minlen/maxlen are in the possible range for
717 * this summary level.
719 error = xfs_rtallocate_extent_block(mp, tp, i,
720 XFS_RTMAX(minlen, 1 << l),
721 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
722 len, &n, rbpp, rsb, prod, &r);
727 * If it worked, return that extent.
729 if (r != NULLRTBLOCK) {
734 * If the "next block to try" returned from the
735 * allocator is beyond the next bitmap block,
736 * skip to that bitmap block.
738 if (XFS_BITTOBLOCK(mp, n) > i + 1)
739 i = XFS_BITTOBLOCK(mp, n) - 1;
743 * Got nothing, return failure.
745 *rtblock = NULLRTBLOCK;
750 * Mark an extent specified by start and len allocated.
751 * Updates all the summary information as well as the bitmap.
753 STATIC int /* error */
754 xfs_rtallocate_range(
755 xfs_mount_t *mp, /* file system mount point */
756 xfs_trans_t *tp, /* transaction pointer */
757 xfs_rtblock_t start, /* start block to allocate */
758 xfs_extlen_t len, /* length to allocate */
759 xfs_buf_t **rbpp, /* in/out: summary block buffer */
760 xfs_fsblock_t *rsb) /* in/out: summary block number */
762 xfs_rtblock_t end; /* end of the allocated extent */
763 int error; /* error value */
764 xfs_rtblock_t postblock; /* first block allocated > end */
765 xfs_rtblock_t preblock; /* first block allocated < start */
767 end = start + len - 1;
769 * Assume we're allocating out of the middle of a free extent.
770 * We need to find the beginning and end of the extent so we can
771 * properly update the summary.
773 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
778 * Find the next allocated block (end of free extent).
780 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
786 * Decrement the summary information corresponding to the entire
789 error = xfs_rtmodify_summary(mp, tp,
790 XFS_RTBLOCKLOG(postblock + 1 - preblock),
791 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
796 * If there are blocks not being allocated at the front of the
797 * old extent, add summary data for them to be free.
799 if (preblock < start) {
800 error = xfs_rtmodify_summary(mp, tp,
801 XFS_RTBLOCKLOG(start - preblock),
802 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
808 * If there are blocks not being allocated at the end of the
809 * old extent, add summary data for them to be free.
811 if (postblock > end) {
812 error = xfs_rtmodify_summary(mp, tp,
813 XFS_RTBLOCKLOG(postblock - end),
814 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
820 * Modify the bitmap to mark this extent allocated.
822 error = xfs_rtmodify_range(mp, tp, start, len, 0);
827 * Return whether there are any free extents in the size range given
828 * by low and high, for the bitmap block bbno.
830 STATIC int /* error */
832 xfs_mount_t *mp, /* file system mount structure */
833 xfs_trans_t *tp, /* transaction pointer */
834 int low, /* low log2 extent size */
835 int high, /* high log2 extent size */
836 xfs_rtblock_t bbno, /* bitmap block number */
837 xfs_buf_t **rbpp, /* in/out: summary block buffer */
838 xfs_fsblock_t *rsb, /* in/out: summary block number */
839 int *stat) /* out: any good extents here? */
841 int error; /* error value */
842 int log; /* loop counter, log2 of ext. size */
843 xfs_suminfo_t sum; /* summary data */
846 * Loop over logs of extent sizes. Order is irrelevant.
848 for (log = low; log <= high; log++) {
850 * Get one summary datum.
852 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
857 * If there are any, return success.
865 * Found nothing, return failure.
872 * Get a buffer for the bitmap or summary file block specified.
873 * The buffer is returned read and locked.
875 STATIC int /* error */
877 xfs_mount_t *mp, /* file system mount structure */
878 xfs_trans_t *tp, /* transaction pointer */
879 xfs_rtblock_t block, /* block number in bitmap or summary */
880 int issum, /* is summary not bitmap */
881 xfs_buf_t **bpp) /* output: buffer for the block */
883 xfs_buf_t *bp; /* block buffer, result */
884 xfs_daddr_t d; /* disk addr of block */
885 int error; /* error value */
886 xfs_fsblock_t fsb; /* fs block number for block */
887 xfs_inode_t *ip; /* bitmap or summary inode */
889 ip = issum ? mp->m_rsumip : mp->m_rbmip;
891 * Map from the file offset (block) and inode number to the
894 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
898 ASSERT(fsb != NULLFSBLOCK);
900 * Convert to disk address for buffer cache.
902 d = XFS_FSB_TO_DADDR(mp, fsb);
906 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
907 mp->m_bsize, 0, &bp);
911 ASSERT(bp && !XFS_BUF_GETERROR(bp));
918 * Check that the given extent (block range) is allocated already.
920 STATIC int /* error */
921 xfs_rtcheck_alloc_range(
922 xfs_mount_t *mp, /* file system mount point */
923 xfs_trans_t *tp, /* transaction pointer */
924 xfs_rtblock_t bno, /* starting block number of extent */
925 xfs_extlen_t len, /* length of extent */
926 int *stat) /* out: 1 for allocated, 0 for not */
928 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
930 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
936 * Check whether the given block in the bitmap has the given value.
938 STATIC int /* 1 for matches, 0 for not */
940 xfs_mount_t *mp, /* file system mount structure */
941 xfs_trans_t *tp, /* transaction pointer */
942 xfs_rtblock_t start, /* bit (block) to check */
943 int val) /* 1 for free, 0 for allocated */
945 int bit; /* bit number in the word */
946 xfs_rtblock_t block; /* bitmap block number */
947 xfs_buf_t *bp; /* buf for the block */
948 xfs_rtword_t *bufp; /* pointer into the buffer */
950 int error; /* error value */
951 xfs_rtword_t wdiff; /* difference between bit & expected */
952 int word; /* word number in the buffer */
953 xfs_rtword_t wval; /* word value from buffer */
955 block = XFS_BITTOBLOCK(mp, start);
956 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
957 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
958 word = XFS_BITTOWORD(mp, start);
959 bit = (int)(start & (XFS_NBWORD - 1));
961 xfs_trans_brelse(tp, bp);
962 wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
969 * Check that the given extent (block range) is free already.
971 STATIC int /* error */
972 xfs_rtcheck_free_range(
973 xfs_mount_t *mp, /* file system mount point */
974 xfs_trans_t *tp, /* transaction pointer */
975 xfs_rtblock_t bno, /* starting block number of extent */
976 xfs_extlen_t len, /* length of extent */
977 int *stat) /* out: 1 for free, 0 for not */
979 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
981 return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
986 * Check that the given range is either all allocated (val = 0) or
987 * all free (val = 1).
989 STATIC int /* error */
991 xfs_mount_t *mp, /* file system mount point */
992 xfs_trans_t *tp, /* transaction pointer */
993 xfs_rtblock_t start, /* starting block number of extent */
994 xfs_extlen_t len, /* length of extent */
995 int val, /* 1 for free, 0 for allocated */
996 xfs_rtblock_t *new, /* out: first block not matching */
997 int *stat) /* out: 1 for matches, 0 for not */
999 xfs_rtword_t *b; /* current word in buffer */
1000 int bit; /* bit number in the word */
1001 xfs_rtblock_t block; /* bitmap block number */
1002 xfs_buf_t *bp; /* buf for the block */
1003 xfs_rtword_t *bufp; /* starting word in buffer */
1004 int error; /* error value */
1005 xfs_rtblock_t i; /* current bit number rel. to start */
1006 xfs_rtblock_t lastbit; /* last useful bit in word */
1007 xfs_rtword_t mask; /* mask of relevant bits for value */
1008 xfs_rtword_t wdiff; /* difference from wanted value */
1009 int word; /* word number in the buffer */
1012 * Compute starting bitmap block number
1014 block = XFS_BITTOBLOCK(mp, start);
1016 * Read the bitmap block.
1018 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1022 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1024 * Compute the starting word's address, and starting bit.
1026 word = XFS_BITTOWORD(mp, start);
1028 bit = (int)(start & (XFS_NBWORD - 1));
1030 * 0 (allocated) => all zero's; 1 (free) => all one's.
1034 * If not starting on a word boundary, deal with the first
1039 * Compute first bit not examined.
1041 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1043 * Mask of relevant bits.
1045 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1047 * Compute difference between actual and desired value.
1049 if ((wdiff = (*b ^ val) & mask)) {
1051 * Different, compute first wrong bit and return.
1053 xfs_trans_brelse(tp, bp);
1054 i = XFS_RTLOBIT(wdiff) - bit;
1061 * Go on to next block if that's where the next word is
1062 * and we need the next word.
1064 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1066 * If done with this block, get the next one.
1068 xfs_trans_brelse(tp, bp);
1069 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1073 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1077 * Go on to the next word in the buffer.
1083 * Starting on a word boundary, no partial word.
1088 * Loop over whole words in buffers. When we use up one buffer
1089 * we move on to the next one.
1091 while (len - i >= XFS_NBWORD) {
1093 * Compute difference between actual and desired value.
1095 if ((wdiff = *b ^ val)) {
1097 * Different, compute first wrong bit and return.
1099 xfs_trans_brelse(tp, bp);
1100 i += XFS_RTLOBIT(wdiff);
1107 * Go on to next block if that's where the next word is
1108 * and we need the next word.
1110 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1112 * If done with this block, get the next one.
1114 xfs_trans_brelse(tp, bp);
1115 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1119 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1123 * Go on to the next word in the buffer.
1129 * If not ending on a word boundary, deal with the last
1132 if ((lastbit = len - i)) {
1134 * Mask of relevant bits.
1136 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1138 * Compute difference between actual and desired value.
1140 if ((wdiff = (*b ^ val) & mask)) {
1142 * Different, compute first wrong bit and return.
1144 xfs_trans_brelse(tp, bp);
1145 i += XFS_RTLOBIT(wdiff);
1153 * Successful, return.
1155 xfs_trans_brelse(tp, bp);
1162 * Copy and transform the summary file, given the old and new
1163 * parameters in the mount structures.
1165 STATIC int /* error */
1167 xfs_mount_t *omp, /* old file system mount point */
1168 xfs_mount_t *nmp, /* new file system mount point */
1169 xfs_trans_t *tp) /* transaction pointer */
1171 xfs_rtblock_t bbno; /* bitmap block number */
1172 xfs_buf_t *bp; /* summary buffer */
1173 int error; /* error return value */
1174 int log; /* summary level number (log length) */
1175 xfs_suminfo_t sum; /* summary data */
1176 xfs_fsblock_t sumbno; /* summary block number */
1179 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1180 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1181 (xfs_srtblock_t)bbno >= 0;
1183 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1189 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1193 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1204 * Searching backward from start to limit, find the first block whose
1205 * allocated/free state is different from start's.
1207 STATIC int /* error */
1209 xfs_mount_t *mp, /* file system mount point */
1210 xfs_trans_t *tp, /* transaction pointer */
1211 xfs_rtblock_t start, /* starting block to look at */
1212 xfs_rtblock_t limit, /* last block to look at */
1213 xfs_rtblock_t *rtblock) /* out: start block found */
1215 xfs_rtword_t *b; /* current word in buffer */
1216 int bit; /* bit number in the word */
1217 xfs_rtblock_t block; /* bitmap block number */
1218 xfs_buf_t *bp; /* buf for the block */
1219 xfs_rtword_t *bufp; /* starting word in buffer */
1220 int error; /* error value */
1221 xfs_rtblock_t firstbit; /* first useful bit in the word */
1222 xfs_rtblock_t i; /* current bit number rel. to start */
1223 xfs_rtblock_t len; /* length of inspected area */
1224 xfs_rtword_t mask; /* mask of relevant bits for value */
1225 xfs_rtword_t want; /* mask for "good" values */
1226 xfs_rtword_t wdiff; /* difference from wanted value */
1227 int word; /* word number in the buffer */
1230 * Compute and read in starting bitmap block for starting block.
1232 block = XFS_BITTOBLOCK(mp, start);
1233 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1237 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1239 * Get the first word's index & point to it.
1241 word = XFS_BITTOWORD(mp, start);
1243 bit = (int)(start & (XFS_NBWORD - 1));
1244 len = start - limit + 1;
1246 * Compute match value, based on the bit at start: if 1 (free)
1247 * then all-ones, else all-zeroes.
1249 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1251 * If the starting position is not word-aligned, deal with the
1254 if (bit < XFS_NBWORD - 1) {
1256 * Calculate first (leftmost) bit number to look at,
1257 * and mask for all the relevant bits in this word.
1259 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1260 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1263 * Calculate the difference between the value there
1264 * and what we're looking for.
1266 if ((wdiff = (*b ^ want) & mask)) {
1268 * Different. Mark where we are and return.
1270 xfs_trans_brelse(tp, bp);
1271 i = bit - XFS_RTHIBIT(wdiff);
1272 *rtblock = start - i + 1;
1275 i = bit - firstbit + 1;
1277 * Go on to previous block if that's where the previous word is
1278 * and we need the previous word.
1280 if (--word == -1 && i < len) {
1282 * If done with this block, get the previous one.
1284 xfs_trans_brelse(tp, bp);
1285 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1289 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1290 word = XFS_BLOCKWMASK(mp);
1294 * Go on to the previous word in the buffer.
1300 * Starting on a word boundary, no partial word.
1305 * Loop over whole words in buffers. When we use up one buffer
1306 * we move on to the previous one.
1308 while (len - i >= XFS_NBWORD) {
1310 * Compute difference between actual and desired value.
1312 if ((wdiff = *b ^ want)) {
1314 * Different, mark where we are and return.
1316 xfs_trans_brelse(tp, bp);
1317 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1318 *rtblock = start - i + 1;
1323 * Go on to previous block if that's where the previous word is
1324 * and we need the previous word.
1326 if (--word == -1 && i < len) {
1328 * If done with this block, get the previous one.
1330 xfs_trans_brelse(tp, bp);
1331 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1335 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1336 word = XFS_BLOCKWMASK(mp);
1340 * Go on to the previous word in the buffer.
1346 * If not ending on a word boundary, deal with the last
1351 * Calculate first (leftmost) bit number to look at,
1352 * and mask for all the relevant bits in this word.
1354 firstbit = XFS_NBWORD - (len - i);
1355 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1357 * Compute difference between actual and desired value.
1359 if ((wdiff = (*b ^ want) & mask)) {
1361 * Different, mark where we are and return.
1363 xfs_trans_brelse(tp, bp);
1364 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1365 *rtblock = start - i + 1;
1371 * No match, return that we scanned the whole area.
1373 xfs_trans_brelse(tp, bp);
1374 *rtblock = start - i + 1;
1379 * Searching forward from start to limit, find the first block whose
1380 * allocated/free state is different from start's.
1382 STATIC int /* error */
1384 xfs_mount_t *mp, /* file system mount point */
1385 xfs_trans_t *tp, /* transaction pointer */
1386 xfs_rtblock_t start, /* starting block to look at */
1387 xfs_rtblock_t limit, /* last block to look at */
1388 xfs_rtblock_t *rtblock) /* out: start block found */
1390 xfs_rtword_t *b; /* current word in buffer */
1391 int bit; /* bit number in the word */
1392 xfs_rtblock_t block; /* bitmap block number */
1393 xfs_buf_t *bp; /* buf for the block */
1394 xfs_rtword_t *bufp; /* starting word in buffer */
1395 int error; /* error value */
1396 xfs_rtblock_t i; /* current bit number rel. to start */
1397 xfs_rtblock_t lastbit; /* last useful bit in the word */
1398 xfs_rtblock_t len; /* length of inspected area */
1399 xfs_rtword_t mask; /* mask of relevant bits for value */
1400 xfs_rtword_t want; /* mask for "good" values */
1401 xfs_rtword_t wdiff; /* difference from wanted value */
1402 int word; /* word number in the buffer */
1405 * Compute and read in starting bitmap block for starting block.
1407 block = XFS_BITTOBLOCK(mp, start);
1408 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1412 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1414 * Get the first word's index & point to it.
1416 word = XFS_BITTOWORD(mp, start);
1418 bit = (int)(start & (XFS_NBWORD - 1));
1419 len = limit - start + 1;
1421 * Compute match value, based on the bit at start: if 1 (free)
1422 * then all-ones, else all-zeroes.
1424 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1426 * If the starting position is not word-aligned, deal with the
1431 * Calculate last (rightmost) bit number to look at,
1432 * and mask for all the relevant bits in this word.
1434 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1435 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1437 * Calculate the difference between the value there
1438 * and what we're looking for.
1440 if ((wdiff = (*b ^ want) & mask)) {
1442 * Different. Mark where we are and return.
1444 xfs_trans_brelse(tp, bp);
1445 i = XFS_RTLOBIT(wdiff) - bit;
1446 *rtblock = start + i - 1;
1451 * Go on to next block if that's where the next word is
1452 * and we need the next word.
1454 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1456 * If done with this block, get the previous one.
1458 xfs_trans_brelse(tp, bp);
1459 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1463 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1467 * Go on to the previous word in the buffer.
1473 * Starting on a word boundary, no partial word.
1478 * Loop over whole words in buffers. When we use up one buffer
1479 * we move on to the next one.
1481 while (len - i >= XFS_NBWORD) {
1483 * Compute difference between actual and desired value.
1485 if ((wdiff = *b ^ want)) {
1487 * Different, mark where we are and return.
1489 xfs_trans_brelse(tp, bp);
1490 i += XFS_RTLOBIT(wdiff);
1491 *rtblock = start + i - 1;
1496 * Go on to next block if that's where the next word is
1497 * and we need the next word.
1499 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1501 * If done with this block, get the next one.
1503 xfs_trans_brelse(tp, bp);
1504 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1508 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1512 * Go on to the next word in the buffer.
1518 * If not ending on a word boundary, deal with the last
1521 if ((lastbit = len - i)) {
1523 * Calculate mask for all the relevant bits in this word.
1525 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1527 * Compute difference between actual and desired value.
1529 if ((wdiff = (*b ^ want) & mask)) {
1531 * Different, mark where we are and return.
1533 xfs_trans_brelse(tp, bp);
1534 i += XFS_RTLOBIT(wdiff);
1535 *rtblock = start + i - 1;
1541 * No match, return that we scanned the whole area.
1543 xfs_trans_brelse(tp, bp);
1544 *rtblock = start + i - 1;
1549 * Mark an extent specified by start and len freed.
1550 * Updates all the summary information as well as the bitmap.
1552 STATIC int /* error */
1554 xfs_mount_t *mp, /* file system mount point */
1555 xfs_trans_t *tp, /* transaction pointer */
1556 xfs_rtblock_t start, /* starting block to free */
1557 xfs_extlen_t len, /* length to free */
1558 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1559 xfs_fsblock_t *rsb) /* in/out: summary block number */
1561 xfs_rtblock_t end; /* end of the freed extent */
1562 int error; /* error value */
1563 xfs_rtblock_t postblock; /* first block freed > end */
1564 xfs_rtblock_t preblock; /* first block freed < start */
1566 end = start + len - 1;
1568 * Modify the bitmap to mark this extent freed.
1570 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1575 * Assume we're freeing out of the middle of an allocated extent.
1576 * We need to find the beginning and end of the extent so we can
1577 * properly update the summary.
1579 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1584 * Find the next allocated block (end of allocated extent).
1586 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1589 * If there are blocks not being freed at the front of the
1590 * old extent, add summary data for them to be allocated.
1592 if (preblock < start) {
1593 error = xfs_rtmodify_summary(mp, tp,
1594 XFS_RTBLOCKLOG(start - preblock),
1595 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1601 * If there are blocks not being freed at the end of the
1602 * old extent, add summary data for them to be allocated.
1604 if (postblock > end) {
1605 error = xfs_rtmodify_summary(mp, tp,
1606 XFS_RTBLOCKLOG(postblock - end),
1607 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1613 * Increment the summary information corresponding to the entire
1614 * (new) free extent.
1616 error = xfs_rtmodify_summary(mp, tp,
1617 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1618 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1623 * Read and return the summary information for a given extent size,
1624 * bitmap block combination.
1625 * Keeps track of a current summary block, so we don't keep reading
1626 * it from the buffer cache.
1628 STATIC int /* error */
1630 xfs_mount_t *mp, /* file system mount structure */
1631 xfs_trans_t *tp, /* transaction pointer */
1632 int log, /* log2 of extent size */
1633 xfs_rtblock_t bbno, /* bitmap block number */
1634 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1635 xfs_fsblock_t *rsb, /* in/out: summary block number */
1636 xfs_suminfo_t *sum) /* out: summary info for this block */
1638 xfs_buf_t *bp; /* buffer for summary block */
1639 int error; /* error value */
1640 xfs_fsblock_t sb; /* summary fsblock */
1641 int so; /* index into the summary file */
1642 xfs_suminfo_t *sp; /* pointer to returned data */
1645 * Compute entry number in the summary file.
1647 so = XFS_SUMOFFS(mp, log, bbno);
1649 * Compute the block number in the summary file.
1651 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1653 * If we have an old buffer, and the block number matches, use that.
1655 if (rbpp && *rbpp && *rsb == sb)
1658 * Otherwise we have to get the buffer.
1662 * If there was an old one, get rid of it first.
1665 xfs_trans_brelse(tp, *rbpp);
1666 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1671 * Remember this buffer and block for the next call.
1679 * Point to the summary information & copy it out.
1681 sp = XFS_SUMPTR(mp, bp, so);
1684 * Drop the buffer if we're not asked to remember it.
1687 xfs_trans_brelse(tp, bp);
1692 * Set the given range of bitmap bits to the given value.
1693 * Do whatever I/O and logging is required.
1695 STATIC int /* error */
1697 xfs_mount_t *mp, /* file system mount point */
1698 xfs_trans_t *tp, /* transaction pointer */
1699 xfs_rtblock_t start, /* starting block to modify */
1700 xfs_extlen_t len, /* length of extent to modify */
1701 int val) /* 1 for free, 0 for allocated */
1703 xfs_rtword_t *b; /* current word in buffer */
1704 int bit; /* bit number in the word */
1705 xfs_rtblock_t block; /* bitmap block number */
1706 xfs_buf_t *bp; /* buf for the block */
1707 xfs_rtword_t *bufp; /* starting word in buffer */
1708 int error; /* error value */
1709 xfs_rtword_t *first; /* first used word in the buffer */
1710 int i; /* current bit number rel. to start */
1711 int lastbit; /* last useful bit in word */
1712 xfs_rtword_t mask; /* mask o frelevant bits for value */
1713 int word; /* word number in the buffer */
1716 * Compute starting bitmap block number.
1718 block = XFS_BITTOBLOCK(mp, start);
1720 * Read the bitmap block, and point to its data.
1722 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1726 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1728 * Compute the starting word's address, and starting bit.
1730 word = XFS_BITTOWORD(mp, start);
1731 first = b = &bufp[word];
1732 bit = (int)(start & (XFS_NBWORD - 1));
1734 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1738 * If not starting on a word boundary, deal with the first
1743 * Compute first bit not changed and mask of relevant bits.
1745 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1746 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1748 * Set/clear the active bits.
1756 * Go on to the next block if that's where the next word is
1757 * and we need the next word.
1759 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1761 * Log the changed part of this block.
1764 xfs_trans_log_buf(tp, bp,
1765 (uint)((char *)first - (char *)bufp),
1766 (uint)((char *)b - (char *)bufp));
1767 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1771 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1775 * Go on to the next word in the buffer
1781 * Starting on a word boundary, no partial word.
1786 * Loop over whole words in buffers. When we use up one buffer
1787 * we move on to the next one.
1789 while (len - i >= XFS_NBWORD) {
1791 * Set the word value correctly.
1796 * Go on to the next block if that's where the next word is
1797 * and we need the next word.
1799 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1801 * Log the changed part of this block.
1804 xfs_trans_log_buf(tp, bp,
1805 (uint)((char *)first - (char *)bufp),
1806 (uint)((char *)b - (char *)bufp));
1807 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1811 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1815 * Go on to the next word in the buffer
1821 * If not ending on a word boundary, deal with the last
1824 if ((lastbit = len - i)) {
1826 * Compute a mask of relevant bits.
1829 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1831 * Set/clear the active bits.
1840 * Log any remaining changed bytes.
1843 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1844 (uint)((char *)b - (char *)bufp - 1));
1849 * Read and modify the summary information for a given extent size,
1850 * bitmap block combination.
1851 * Keeps track of a current summary block, so we don't keep reading
1852 * it from the buffer cache.
1854 STATIC int /* error */
1855 xfs_rtmodify_summary(
1856 xfs_mount_t *mp, /* file system mount point */
1857 xfs_trans_t *tp, /* transaction pointer */
1858 int log, /* log2 of extent size */
1859 xfs_rtblock_t bbno, /* bitmap block number */
1860 int delta, /* change to make to summary info */
1861 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1862 xfs_fsblock_t *rsb) /* in/out: summary block number */
1864 xfs_buf_t *bp; /* buffer for the summary block */
1865 int error; /* error value */
1866 xfs_fsblock_t sb; /* summary fsblock */
1867 int so; /* index into the summary file */
1868 xfs_suminfo_t *sp; /* pointer to returned data */
1871 * Compute entry number in the summary file.
1873 so = XFS_SUMOFFS(mp, log, bbno);
1875 * Compute the block number in the summary file.
1877 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1879 * If we have an old buffer, and the block number matches, use that.
1881 if (rbpp && *rbpp && *rsb == sb)
1884 * Otherwise we have to get the buffer.
1888 * If there was an old one, get rid of it first.
1891 xfs_trans_brelse(tp, *rbpp);
1892 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1897 * Remember this buffer and block for the next call.
1905 * Point to the summary information, modify and log it.
1907 sp = XFS_SUMPTR(mp, bp, so);
1909 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1910 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1915 * Visible (exported) functions.
1919 * Grow the realtime area of the filesystem.
1923 xfs_mount_t *mp, /* mount point for filesystem */
1924 xfs_growfs_rt_t *in) /* growfs rt input struct */
1926 xfs_rtblock_t bmbno; /* bitmap block number */
1927 xfs_buf_t *bp; /* temporary buffer */
1928 int cancelflags; /* flags for xfs_trans_cancel */
1929 int error; /* error return value */
1930 xfs_inode_t *ip; /* bitmap inode, used as lock */
1931 xfs_mount_t *nmp; /* new (fake) mount structure */
1932 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1933 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1934 xfs_drtbno_t nrextents; /* new number of realtime extents */
1935 uint8_t nrextslog; /* new log2 of sb_rextents */
1936 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1937 uint nrsumlevels; /* new rt summary levels */
1938 uint nrsumsize; /* new size of rt summary, bytes */
1939 xfs_sb_t *nsbp; /* new superblock */
1940 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1941 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1942 xfs_sb_t *sbp; /* old superblock */
1943 xfs_fsblock_t sumbno; /* summary block number */
1944 xfs_trans_t *tp; /* transaction pointer */
1948 * Initial error checking.
1950 if (mp->m_rtdev_targp || mp->m_rbmip == NULL ||
1951 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1952 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1953 return XFS_ERROR(EINVAL);
1955 * Read in the last block of the device, make sure it exists.
1957 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1958 XFS_FSB_TO_BB(mp, in->newblocks - 1),
1959 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1965 * Calculate new parameters. These are the final values to be reached.
1967 nrextents = do_div(nrblocks, in->extsize);
1968 nrbmblocks = roundup_64(nrextents, NBBY * sbp->sb_blocksize);
1969 nrextslog = xfs_highbit32(nrextents);
1970 nrsumlevels = nrextslog + 1;
1971 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1972 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1973 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1975 * New summary size can't be more than half the size of
1976 * the log. This prevents us from getting a log overflow,
1977 * since we'll log basically the whole summary file at once.
1979 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1980 return XFS_ERROR(EINVAL);
1982 * Get the old block counts for bitmap and summary inodes.
1983 * These can't change since other growfs callers are locked out.
1985 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1986 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1988 * Allocate space to the bitmap and summary files, as necessary.
1990 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1991 mp->m_sb.sb_rbmino)))
1993 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1994 mp->m_sb.sb_rsumino)))
1998 * Loop over the bitmap blocks.
1999 * We will do everything one bitmap block at a time.
2000 * Skip the current block if it is exactly full.
2001 * This also deals with the case where there were no rtextents before.
2003 for (bmbno = sbp->sb_rbmblocks -
2004 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
2008 * Allocate a new (fake) mount/sb.
2010 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
2014 * Calculate new sb and mount fields for this round.
2016 nsbp->sb_rextsize = in->extsize;
2017 nsbp->sb_rbmblocks = bmbno + 1;
2020 nsbp->sb_rbmblocks * NBBY *
2021 nsbp->sb_blocksize * nsbp->sb_rextsize);
2022 nsbp->sb_rextents = do_div(nsbp->sb_rblocks, nsbp->sb_rextsize);
2023 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2024 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2026 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2028 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2029 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2031 * Start a transaction, get the log reservation.
2033 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2035 if ((error = xfs_trans_reserve(tp, 0,
2036 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2039 * Lock out other callers by grabbing the bitmap inode lock.
2041 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino,
2042 XFS_ILOCK_EXCL, &ip)))
2044 ASSERT(ip == mp->m_rbmip);
2046 * Update the bitmap inode's size.
2048 mp->m_rbmip->i_d.di_size =
2049 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2050 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2051 cancelflags |= XFS_TRANS_ABORT;
2053 * Get the summary inode into the transaction.
2055 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino,
2056 XFS_ILOCK_EXCL, &ip)))
2058 ASSERT(ip == mp->m_rsumip);
2060 * Update the summary inode's size.
2062 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2063 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2065 * Copy summary data from old to new sizes.
2066 * Do this when the real size (not block-aligned) changes.
2068 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2069 mp->m_rsumlevels != nmp->m_rsumlevels) {
2070 error = xfs_rtcopy_summary(mp, nmp, tp);
2075 * Update superblock fields.
2077 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2078 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2079 nsbp->sb_rextsize - sbp->sb_rextsize);
2080 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2081 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2082 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2083 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2084 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2085 nsbp->sb_rblocks - sbp->sb_rblocks);
2086 if (nsbp->sb_rextents != sbp->sb_rextents)
2087 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2088 nsbp->sb_rextents - sbp->sb_rextents);
2089 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2090 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2091 nsbp->sb_rextslog - sbp->sb_rextslog);
2096 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2097 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2101 * Mark more blocks free in the superblock.
2103 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2104 nsbp->sb_rextents - sbp->sb_rextents);
2106 * Free the fake mp structure.
2108 kmem_free(nmp, sizeof(*nmp));
2111 * Update mp values into the real mp structure.
2113 mp->m_rsumlevels = nrsumlevels;
2114 mp->m_rsumsize = nrsumsize;
2116 * Commit the transaction.
2118 xfs_trans_commit(tp, 0, NULL);
2123 * Error paths come here.
2127 kmem_free(nmp, sizeof(*nmp));
2128 xfs_trans_cancel(tp, cancelflags);
2133 * Allocate an extent in the realtime subvolume, with the usual allocation
2134 * parameters. The length units are all in realtime extents, as is the
2135 * result block number.
2138 xfs_rtallocate_extent(
2139 xfs_trans_t *tp, /* transaction pointer */
2140 xfs_rtblock_t bno, /* starting block number to allocate */
2141 xfs_extlen_t minlen, /* minimum length to allocate */
2142 xfs_extlen_t maxlen, /* maximum length to allocate */
2143 xfs_extlen_t *len, /* out: actual length allocated */
2144 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2145 int wasdel, /* was a delayed allocation extent */
2146 xfs_extlen_t prod, /* extent product factor */
2147 xfs_rtblock_t *rtblock) /* out: start block allocated */
2149 int error; /* error value */
2150 xfs_inode_t *ip; /* inode for bitmap file */
2151 xfs_mount_t *mp; /* file system mount structure */
2152 xfs_rtblock_t r; /* result allocated block */
2153 xfs_fsblock_t sb; /* summary file block number */
2154 xfs_buf_t *sumbp; /* summary file block buffer */
2156 ASSERT(minlen > 0 && minlen <= maxlen);
2159 * If prod is set then figure out what to do to minlen and maxlen.
2164 if ((i = maxlen % prod))
2166 if ((i = minlen % prod))
2168 if (maxlen < minlen) {
2169 *rtblock = NULLRTBLOCK;
2174 * Lock out other callers by grabbing the bitmap inode lock.
2176 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, XFS_ILOCK_EXCL, &ip);
2182 * Allocate by size, or near another block, or exactly at some block.
2185 case XFS_ALLOCTYPE_ANY_AG:
2186 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2187 &sumbp, &sb, prod, &r);
2189 case XFS_ALLOCTYPE_NEAR_BNO:
2190 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2191 len, &sumbp, &sb, prod, &r);
2193 case XFS_ALLOCTYPE_THIS_BNO:
2194 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2195 len, &sumbp, &sb, prod, &r);
2204 * If it worked, update the superblock.
2206 if (r != NULLRTBLOCK) {
2207 long slen = (long)*len;
2209 ASSERT(*len >= minlen && *len <= maxlen);
2211 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2213 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2220 * Free an extent in the realtime subvolume. Length is expressed in
2221 * realtime extents, as is the block number.
2225 xfs_trans_t *tp, /* transaction pointer */
2226 xfs_rtblock_t bno, /* starting block number to free */
2227 xfs_extlen_t len) /* length of extent freed */
2229 int error; /* error value */
2230 xfs_inode_t *ip; /* bitmap file inode */
2231 xfs_mount_t *mp; /* file system mount structure */
2232 xfs_fsblock_t sb; /* summary file block number */
2233 xfs_buf_t *sumbp; /* summary file block buffer */
2237 * Synchronize by locking the bitmap inode.
2239 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, XFS_ILOCK_EXCL, &ip);
2243 #if defined(__KERNEL__) && defined(DEBUG)
2245 * Check to see that this whole range is currently allocated.
2248 int stat; /* result from checking range */
2250 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2259 * Free the range of realtime blocks.
2261 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2266 * Mark more blocks free in the superblock.
2268 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2270 * If we've now freed all the blocks, reset the file sequence
2273 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2274 mp->m_sb.sb_rextents) {
2275 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2276 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2277 *(__uint64_t *)&ip->i_d.di_atime = 0;
2278 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2284 * Initialize realtime fields in the mount structure.
2288 xfs_mount_t *mp) /* file system mount structure */
2290 xfs_buf_t *bp; /* buffer for last block of subvolume */
2291 xfs_daddr_t d; /* address of last block of subvolume */
2292 int error; /* error return value */
2293 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2296 if (sbp->sb_rblocks == 0)
2298 if (mp->m_rtdev_targp == NULL) {
2300 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2301 return XFS_ERROR(ENODEV);
2303 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2305 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2307 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2308 mp->m_rbmip = mp->m_rsumip = NULL;
2310 * Check that the realtime section is an ok size.
2312 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2313 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2314 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2315 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2316 (unsigned long long) mp->m_sb.sb_rblocks);
2317 return XFS_ERROR(E2BIG);
2319 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2320 d - XFS_FSB_TO_BB(mp, 1),
2321 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2324 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2325 if (error == ENOSPC)
2326 return XFS_ERROR(E2BIG);
2334 * Get the bitmap and summary inodes into the mount structure
2339 xfs_mount_t *mp) /* file system mount structure */
2341 int error; /* error return value */
2345 if (sbp->sb_rbmino == NULLFSINO)
2347 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, &mp->m_rbmip, 0);
2350 ASSERT(mp->m_rbmip != NULL);
2351 ASSERT(sbp->sb_rsumino != NULLFSINO);
2352 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, &mp->m_rsumip, 0);
2354 vnode_t *rbmvp; /* vnode for bitmap file */
2355 vmap_t vmap; /* vmap to delete vnode */
2357 rbmvp = XFS_ITOV(mp->m_rbmip);
2360 vn_purge(rbmvp, &vmap);
2363 ASSERT(mp->m_rsumip != NULL);
2368 * Pick an extent for allocation at the start of a new realtime file.
2369 * Use the sequence number stored in the atime field of the bitmap inode.
2370 * Translate this to a fraction of the rtextents, and return the product
2371 * of rtextents and the fraction.
2372 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2376 xfs_mount_t *mp, /* file system mount point */
2377 xfs_trans_t *tp, /* transaction pointer */
2378 xfs_extlen_t len, /* allocation length (rtextents) */
2379 xfs_rtblock_t *pick) /* result rt extent */
2381 xfs_rtblock_t b; /* result block */
2382 int error; /* error return value */
2383 xfs_inode_t *ip; /* bitmap incore inode */
2384 int log2; /* log of sequence number */
2385 __uint64_t resid; /* residual after log removed */
2386 __uint64_t seq; /* sequence number of file creation */
2387 __uint64_t *seqp; /* pointer to seqno in inode */
2389 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, XFS_ILOCK_EXCL, &ip);
2392 ASSERT(ip == mp->m_rbmip);
2393 seqp = (__uint64_t *)&ip->i_d.di_atime;
2394 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2395 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2399 if ((log2 = xfs_highbit64(seq)) == -1)
2402 resid = seq - (1ULL << log2);
2403 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2405 if (b >= mp->m_sb.sb_rextents)
2406 b = do_mod(b, mp->m_sb.sb_rextents);
2407 if (b + len > mp->m_sb.sb_rextents)
2408 b = mp->m_sb.sb_rextents - len;
2411 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2418 * Debug code: print out the value of a range in the bitmap.
2422 xfs_mount_t *mp, /* file system mount structure */
2423 xfs_trans_t *tp, /* transaction pointer */
2424 xfs_rtblock_t start, /* starting block to print */
2425 xfs_extlen_t len) /* length to print */
2427 xfs_extlen_t i; /* block number in the extent */
2429 printk("%Ld: ", (long long)start);
2430 for (i = 0; i < len; i++)
2431 printk("%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2436 * Debug code: print the summary file.
2439 xfs_rtprint_summary(
2440 xfs_mount_t *mp, /* file system mount structure */
2441 xfs_trans_t *tp) /* transaction pointer */
2443 xfs_suminfo_t c; /* summary data */
2444 xfs_rtblock_t i; /* bitmap block number */
2445 int l; /* summary information level */
2446 int p; /* flag for printed anything */
2447 xfs_fsblock_t sb; /* summary block number */
2448 xfs_buf_t *sumbp; /* summary block buffer */
2451 for (l = 0; l < mp->m_rsumlevels; l++) {
2452 for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2453 (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2456 printk("%Ld-%Ld:", 1LL << l,
2457 XFS_RTMIN((1LL << l) +
2459 mp->m_sb.sb_rextents));
2462 printk(" %Ld:%d", (long long)i, c);
2469 xfs_trans_brelse(tp, sumbp);