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_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
34 #include "xfs_rtalloc.h"
35 #include "xfs_fsops.h"
36 #include "xfs_error.h"
38 #include "xfs_inode_item.h"
39 #include "xfs_trans_space.h"
40 #include "xfs_utils.h"
41 #include "xfs_trace.h"
46 * Prototypes for internal functions.
50 STATIC
int xfs_rtallocate_range(xfs_mount_t
*, xfs_trans_t
*, xfs_rtblock_t
,
51 xfs_extlen_t
, xfs_buf_t
**, xfs_fsblock_t
*);
52 STATIC
int xfs_rtany_summary(xfs_mount_t
*, xfs_trans_t
*, int, int,
53 xfs_rtblock_t
, xfs_buf_t
**, xfs_fsblock_t
*, int *);
54 STATIC
int xfs_rtcheck_range(xfs_mount_t
*, xfs_trans_t
*, xfs_rtblock_t
,
55 xfs_extlen_t
, int, xfs_rtblock_t
*, int *);
56 STATIC
int xfs_rtfind_back(xfs_mount_t
*, xfs_trans_t
*, xfs_rtblock_t
,
57 xfs_rtblock_t
, xfs_rtblock_t
*);
58 STATIC
int xfs_rtfind_forw(xfs_mount_t
*, xfs_trans_t
*, xfs_rtblock_t
,
59 xfs_rtblock_t
, xfs_rtblock_t
*);
60 STATIC
int xfs_rtget_summary( xfs_mount_t
*, xfs_trans_t
*, int,
61 xfs_rtblock_t
, xfs_buf_t
**, xfs_fsblock_t
*, xfs_suminfo_t
*);
62 STATIC
int xfs_rtmodify_range(xfs_mount_t
*, xfs_trans_t
*, xfs_rtblock_t
,
64 STATIC
int xfs_rtmodify_summary(xfs_mount_t
*, xfs_trans_t
*, int,
65 xfs_rtblock_t
, int, xfs_buf_t
**, xfs_fsblock_t
*);
72 * Allocate space to the bitmap or summary file, and zero it, for growfs.
74 STATIC
int /* error */
76 xfs_mount_t
*mp
, /* file system mount point */
77 xfs_extlen_t oblocks
, /* old count of blocks */
78 xfs_extlen_t nblocks
, /* new count of blocks */
79 xfs_ino_t ino
) /* inode number (bitmap/summary) */
81 xfs_fileoff_t bno
; /* block number in file */
82 xfs_buf_t
*bp
; /* temporary buffer for zeroing */
83 int committed
; /* transaction committed flag */
84 xfs_daddr_t d
; /* disk block address */
85 int error
; /* error return value */
86 xfs_fsblock_t firstblock
; /* first block allocated in xaction */
87 xfs_bmap_free_t flist
; /* list of freed blocks */
88 xfs_fsblock_t fsbno
; /* filesystem block for bno */
89 xfs_inode_t
*ip
; /* pointer to incore inode */
90 xfs_bmbt_irec_t map
; /* block map output */
91 int nmap
; /* number of block maps */
92 int resblks
; /* space reservation */
95 * Allocate space to the file, as necessary.
97 while (oblocks
< nblocks
) {
101 tp
= xfs_trans_alloc(mp
, XFS_TRANS_GROWFSRT_ALLOC
);
102 resblks
= XFS_GROWFSRT_SPACE_RES(mp
, nblocks
- oblocks
);
104 * Reserve space & log for one extent added to the file.
106 if ((error
= xfs_trans_reserve(tp
, resblks
,
107 XFS_GROWRTALLOC_LOG_RES(mp
), 0,
108 XFS_TRANS_PERM_LOG_RES
,
109 XFS_DEFAULT_PERM_LOG_COUNT
)))
111 cancelflags
= XFS_TRANS_RELEASE_LOG_RES
;
115 if ((error
= xfs_trans_iget(mp
, tp
, ino
, 0,
116 XFS_ILOCK_EXCL
, &ip
)))
118 xfs_bmap_init(&flist
, &firstblock
);
120 * Allocate blocks to the bitmap file.
123 cancelflags
|= XFS_TRANS_ABORT
;
124 error
= xfs_bmapi(tp
, ip
, oblocks
, nblocks
- oblocks
,
125 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
, &firstblock
,
126 resblks
, &map
, &nmap
, &flist
);
127 if (!error
&& nmap
< 1)
128 error
= XFS_ERROR(ENOSPC
);
132 * Free any blocks freed up in the transaction, then commit.
134 error
= xfs_bmap_finish(&tp
, &flist
, &committed
);
137 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
141 * Now we need to clear the allocated blocks.
142 * Do this one block per transaction, to keep it simple.
145 for (bno
= map
.br_startoff
, fsbno
= map
.br_startblock
;
146 bno
< map
.br_startoff
+ map
.br_blockcount
;
148 tp
= xfs_trans_alloc(mp
, XFS_TRANS_GROWFSRT_ZERO
);
150 * Reserve log for one block zeroing.
152 if ((error
= xfs_trans_reserve(tp
, 0,
153 XFS_GROWRTZERO_LOG_RES(mp
), 0, 0, 0)))
156 * Lock the bitmap inode.
158 if ((error
= xfs_trans_iget(mp
, tp
, ino
, 0,
159 XFS_ILOCK_EXCL
, &ip
)))
162 * Get a buffer for the block.
164 d
= XFS_FSB_TO_DADDR(mp
, fsbno
);
165 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
168 error
= XFS_ERROR(EIO
);
170 xfs_trans_cancel(tp
, cancelflags
);
173 memset(XFS_BUF_PTR(bp
), 0, mp
->m_sb
.sb_blocksize
);
174 xfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
176 * Commit the transaction.
178 error
= xfs_trans_commit(tp
, 0);
183 * Go on to the next extent, if any.
185 oblocks
= map
.br_startoff
+ map
.br_blockcount
;
193 * Attempt to allocate an extent minlen<=len<=maxlen starting from
194 * bitmap block bbno. If we don't get maxlen then use prod to trim
195 * the length, if given. Returns error; returns starting block in *rtblock.
196 * The lengths are all in rtextents.
198 STATIC
int /* error */
199 xfs_rtallocate_extent_block(
200 xfs_mount_t
*mp
, /* file system mount point */
201 xfs_trans_t
*tp
, /* transaction pointer */
202 xfs_rtblock_t bbno
, /* bitmap block number */
203 xfs_extlen_t minlen
, /* minimum length to allocate */
204 xfs_extlen_t maxlen
, /* maximum length to allocate */
205 xfs_extlen_t
*len
, /* out: actual length allocated */
206 xfs_rtblock_t
*nextp
, /* out: next block to try */
207 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
208 xfs_fsblock_t
*rsb
, /* in/out: summary block number */
209 xfs_extlen_t prod
, /* extent product factor */
210 xfs_rtblock_t
*rtblock
) /* out: start block allocated */
212 xfs_rtblock_t besti
; /* best rtblock found so far */
213 xfs_rtblock_t bestlen
; /* best length found so far */
214 xfs_rtblock_t end
; /* last rtblock in chunk */
215 int error
; /* error value */
216 xfs_rtblock_t i
; /* current rtblock trying */
217 xfs_rtblock_t next
; /* next rtblock to try */
218 int stat
; /* status from internal calls */
221 * Loop over all the extents starting in this bitmap block,
222 * looking for one that's long enough.
224 for (i
= XFS_BLOCKTOBIT(mp
, bbno
), besti
= -1, bestlen
= 0,
225 end
= XFS_BLOCKTOBIT(mp
, bbno
+ 1) - 1;
229 * See if there's a free extent of maxlen starting at i.
230 * If it's not so then next will contain the first non-free.
232 error
= xfs_rtcheck_range(mp
, tp
, i
, maxlen
, 1, &next
, &stat
);
238 * i for maxlen is all free, allocate and return that.
240 error
= xfs_rtallocate_range(mp
, tp
, i
, maxlen
, rbpp
,
250 * In the case where we have a variable-sized allocation
251 * request, figure out how big this free piece is,
252 * and if it's big enough for the minimum, and the best
253 * so far, remember it.
255 if (minlen
< maxlen
) {
256 xfs_rtblock_t thislen
; /* this extent size */
259 if (thislen
>= minlen
&& thislen
> bestlen
) {
265 * If not done yet, find the start of the next free space.
268 error
= xfs_rtfind_forw(mp
, tp
, next
, end
, &i
);
276 * Searched the whole thing & didn't find a maxlen free extent.
278 if (minlen
< maxlen
&& besti
!= -1) {
279 xfs_extlen_t p
; /* amount to trim length by */
282 * If size should be a multiple of prod, make that so.
284 if (prod
> 1 && (p
= do_mod(bestlen
, prod
)))
287 * Allocate besti for bestlen & return that.
289 error
= xfs_rtallocate_range(mp
, tp
, besti
, bestlen
, rbpp
, rsb
);
298 * Allocation failed. Set *nextp to the next block to try.
301 *rtblock
= NULLRTBLOCK
;
306 * Allocate an extent of length minlen<=len<=maxlen, starting at block
307 * bno. If we don't get maxlen then use prod to trim the length, if given.
308 * Returns error; returns starting block in *rtblock.
309 * The lengths are all in rtextents.
311 STATIC
int /* error */
312 xfs_rtallocate_extent_exact(
313 xfs_mount_t
*mp
, /* file system mount point */
314 xfs_trans_t
*tp
, /* transaction pointer */
315 xfs_rtblock_t bno
, /* starting block number to allocate */
316 xfs_extlen_t minlen
, /* minimum length to allocate */
317 xfs_extlen_t maxlen
, /* maximum length to allocate */
318 xfs_extlen_t
*len
, /* out: actual length allocated */
319 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
320 xfs_fsblock_t
*rsb
, /* in/out: summary block number */
321 xfs_extlen_t prod
, /* extent product factor */
322 xfs_rtblock_t
*rtblock
) /* out: start block allocated */
324 int error
; /* error value */
325 xfs_extlen_t i
; /* extent length trimmed due to prod */
326 int isfree
; /* extent is free */
327 xfs_rtblock_t next
; /* next block to try (dummy) */
329 ASSERT(minlen
% prod
== 0 && maxlen
% prod
== 0);
331 * Check if the range in question (for maxlen) is free.
333 error
= xfs_rtcheck_range(mp
, tp
, bno
, maxlen
, 1, &next
, &isfree
);
339 * If it is, allocate it and return success.
341 error
= xfs_rtallocate_range(mp
, tp
, bno
, maxlen
, rbpp
, rsb
);
350 * If not, allocate what there is, if it's at least minlen.
353 if (maxlen
< minlen
) {
355 * Failed, return failure status.
357 *rtblock
= NULLRTBLOCK
;
361 * Trim off tail of extent, if prod is specified.
363 if (prod
> 1 && (i
= maxlen
% prod
)) {
365 if (maxlen
< minlen
) {
367 * Now we can't do it, return failure status.
369 *rtblock
= NULLRTBLOCK
;
374 * Allocate what we can and return it.
376 error
= xfs_rtallocate_range(mp
, tp
, bno
, maxlen
, rbpp
, rsb
);
386 * Allocate an extent of length minlen<=len<=maxlen, starting as near
387 * to bno as possible. If we don't get maxlen then use prod to trim
388 * the length, if given. The lengths are all in rtextents.
390 STATIC
int /* error */
391 xfs_rtallocate_extent_near(
392 xfs_mount_t
*mp
, /* file system mount point */
393 xfs_trans_t
*tp
, /* transaction pointer */
394 xfs_rtblock_t bno
, /* starting block number to allocate */
395 xfs_extlen_t minlen
, /* minimum length to allocate */
396 xfs_extlen_t maxlen
, /* maximum length to allocate */
397 xfs_extlen_t
*len
, /* out: actual length allocated */
398 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
399 xfs_fsblock_t
*rsb
, /* in/out: summary block number */
400 xfs_extlen_t prod
, /* extent product factor */
401 xfs_rtblock_t
*rtblock
) /* out: start block allocated */
403 int any
; /* any useful extents from summary */
404 xfs_rtblock_t bbno
; /* bitmap block number */
405 int error
; /* error value */
406 int i
; /* bitmap block offset (loop control) */
407 int j
; /* secondary loop control */
408 int log2len
; /* log2 of minlen */
409 xfs_rtblock_t n
; /* next block to try */
410 xfs_rtblock_t r
; /* result block */
412 ASSERT(minlen
% prod
== 0 && maxlen
% prod
== 0);
414 * If the block number given is off the end, silently set it to
417 if (bno
>= mp
->m_sb
.sb_rextents
)
418 bno
= mp
->m_sb
.sb_rextents
- 1;
420 * Try the exact allocation first.
422 error
= xfs_rtallocate_extent_exact(mp
, tp
, bno
, minlen
, maxlen
, len
,
423 rbpp
, rsb
, prod
, &r
);
428 * If the exact allocation worked, return that.
430 if (r
!= NULLRTBLOCK
) {
434 bbno
= XFS_BITTOBLOCK(mp
, bno
);
437 log2len
= xfs_highbit32(minlen
);
439 * Loop over all bitmap blocks (bbno + i is current block).
443 * Get summary information of extents of all useful levels
444 * starting in this bitmap block.
446 error
= xfs_rtany_summary(mp
, tp
, log2len
, mp
->m_rsumlevels
- 1,
447 bbno
+ i
, rbpp
, rsb
, &any
);
452 * If there are any useful extents starting here, try
457 * On the positive side of the starting location.
461 * Try to allocate an extent starting in
464 error
= xfs_rtallocate_extent_block(mp
, tp
,
465 bbno
+ i
, minlen
, maxlen
, len
, &n
, rbpp
,
471 * If it worked, return it.
473 if (r
!= NULLRTBLOCK
) {
479 * On the negative side of the starting location.
483 * Loop backwards through the bitmap blocks from
484 * the starting point-1 up to where we are now.
485 * There should be an extent which ends in this
486 * bitmap block and is long enough.
488 for (j
= -1; j
> i
; j
--) {
490 * Grab the summary information for
493 error
= xfs_rtany_summary(mp
, tp
,
494 log2len
, mp
->m_rsumlevels
- 1,
495 bbno
+ j
, rbpp
, rsb
, &any
);
500 * If there's no extent given in the
501 * summary that means the extent we
502 * found must carry over from an
503 * earlier block. If there is an
504 * extent given, we've already tried
505 * that allocation, don't do it again.
509 error
= xfs_rtallocate_extent_block(mp
,
510 tp
, bbno
+ j
, minlen
, maxlen
,
511 len
, &n
, rbpp
, rsb
, prod
, &r
);
516 * If it works, return the extent.
518 if (r
!= NULLRTBLOCK
) {
524 * There weren't intervening bitmap blocks
525 * with a long enough extent, or the
526 * allocation didn't work for some reason
527 * (i.e. it's a little * too short).
528 * Try to allocate from the summary block
531 error
= xfs_rtallocate_extent_block(mp
, tp
,
532 bbno
+ i
, minlen
, maxlen
, len
, &n
, rbpp
,
538 * If it works, return the extent.
540 if (r
!= NULLRTBLOCK
) {
547 * Loop control. If we were on the positive side, and there's
548 * still more blocks on the negative side, go there.
550 if (i
> 0 && (int)bbno
- i
>= 0)
553 * If positive, and no more negative, but there are more
554 * positive, go there.
556 else if (i
> 0 && (int)bbno
+ i
< mp
->m_sb
.sb_rbmblocks
- 1)
559 * If negative or 0 (just started), and there are positive
560 * blocks to go, go there. The 0 case moves to block 1.
562 else if (i
<= 0 && (int)bbno
- i
< mp
->m_sb
.sb_rbmblocks
- 1)
565 * If negative or 0 and there are more negative blocks,
568 else if (i
<= 0 && (int)bbno
+ i
> 0)
571 * Must be done. Return failure.
576 *rtblock
= NULLRTBLOCK
;
581 * Allocate an extent of length minlen<=len<=maxlen, with no position
582 * specified. If we don't get maxlen then use prod to trim
583 * the length, if given. The lengths are all in rtextents.
585 STATIC
int /* error */
586 xfs_rtallocate_extent_size(
587 xfs_mount_t
*mp
, /* file system mount point */
588 xfs_trans_t
*tp
, /* transaction pointer */
589 xfs_extlen_t minlen
, /* minimum length to allocate */
590 xfs_extlen_t maxlen
, /* maximum length to allocate */
591 xfs_extlen_t
*len
, /* out: actual length allocated */
592 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
593 xfs_fsblock_t
*rsb
, /* in/out: summary block number */
594 xfs_extlen_t prod
, /* extent product factor */
595 xfs_rtblock_t
*rtblock
) /* out: start block allocated */
597 int error
; /* error value */
598 int i
; /* bitmap block number */
599 int l
; /* level number (loop control) */
600 xfs_rtblock_t n
; /* next block to be tried */
601 xfs_rtblock_t r
; /* result block number */
602 xfs_suminfo_t sum
; /* summary information for extents */
604 ASSERT(minlen
% prod
== 0 && maxlen
% prod
== 0);
608 * Loop over all the levels starting with maxlen.
609 * At each level, look at all the bitmap blocks, to see if there
610 * are extents starting there that are long enough (>= maxlen).
611 * Note, only on the initial level can the allocation fail if
612 * the summary says there's an extent.
614 for (l
= xfs_highbit32(maxlen
); l
< mp
->m_rsumlevels
; l
++) {
616 * Loop over all the bitmap blocks.
618 for (i
= 0; i
< mp
->m_sb
.sb_rbmblocks
; i
++) {
620 * Get the summary for this level/block.
622 error
= xfs_rtget_summary(mp
, tp
, l
, i
, rbpp
, rsb
,
628 * Nothing there, on to the next block.
633 * Try allocating the extent.
635 error
= xfs_rtallocate_extent_block(mp
, tp
, i
, maxlen
,
636 maxlen
, len
, &n
, rbpp
, rsb
, prod
, &r
);
641 * If it worked, return that.
643 if (r
!= NULLRTBLOCK
) {
648 * If the "next block to try" returned from the
649 * allocator is beyond the next bitmap block,
650 * skip to that bitmap block.
652 if (XFS_BITTOBLOCK(mp
, n
) > i
+ 1)
653 i
= XFS_BITTOBLOCK(mp
, n
) - 1;
657 * Didn't find any maxlen blocks. Try smaller ones, unless
658 * we're asking for a fixed size extent.
660 if (minlen
> --maxlen
) {
661 *rtblock
= NULLRTBLOCK
;
668 * Loop over sizes, from maxlen down to minlen.
669 * This time, when we do the allocations, allow smaller ones
672 for (l
= xfs_highbit32(maxlen
); l
>= xfs_highbit32(minlen
); l
--) {
674 * Loop over all the bitmap blocks, try an allocation
675 * starting in that block.
677 for (i
= 0; i
< mp
->m_sb
.sb_rbmblocks
; i
++) {
679 * Get the summary information for this level/block.
681 error
= xfs_rtget_summary(mp
, tp
, l
, i
, rbpp
, rsb
,
687 * If nothing there, go on to next.
692 * Try the allocation. Make sure the specified
693 * minlen/maxlen are in the possible range for
694 * this summary level.
696 error
= xfs_rtallocate_extent_block(mp
, tp
, i
,
697 XFS_RTMAX(minlen
, 1 << l
),
698 XFS_RTMIN(maxlen
, (1 << (l
+ 1)) - 1),
699 len
, &n
, rbpp
, rsb
, prod
, &r
);
704 * If it worked, return that extent.
706 if (r
!= NULLRTBLOCK
) {
711 * If the "next block to try" returned from the
712 * allocator is beyond the next bitmap block,
713 * skip to that bitmap block.
715 if (XFS_BITTOBLOCK(mp
, n
) > i
+ 1)
716 i
= XFS_BITTOBLOCK(mp
, n
) - 1;
720 * Got nothing, return failure.
722 *rtblock
= NULLRTBLOCK
;
727 * Mark an extent specified by start and len allocated.
728 * Updates all the summary information as well as the bitmap.
730 STATIC
int /* error */
731 xfs_rtallocate_range(
732 xfs_mount_t
*mp
, /* file system mount point */
733 xfs_trans_t
*tp
, /* transaction pointer */
734 xfs_rtblock_t start
, /* start block to allocate */
735 xfs_extlen_t len
, /* length to allocate */
736 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
737 xfs_fsblock_t
*rsb
) /* in/out: summary block number */
739 xfs_rtblock_t end
; /* end of the allocated extent */
740 int error
; /* error value */
741 xfs_rtblock_t postblock
; /* first block allocated > end */
742 xfs_rtblock_t preblock
; /* first block allocated < start */
744 end
= start
+ len
- 1;
746 * Assume we're allocating out of the middle of a free extent.
747 * We need to find the beginning and end of the extent so we can
748 * properly update the summary.
750 error
= xfs_rtfind_back(mp
, tp
, start
, 0, &preblock
);
755 * Find the next allocated block (end of free extent).
757 error
= xfs_rtfind_forw(mp
, tp
, end
, mp
->m_sb
.sb_rextents
- 1,
763 * Decrement the summary information corresponding to the entire
766 error
= xfs_rtmodify_summary(mp
, tp
,
767 XFS_RTBLOCKLOG(postblock
+ 1 - preblock
),
768 XFS_BITTOBLOCK(mp
, preblock
), -1, rbpp
, rsb
);
773 * If there are blocks not being allocated at the front of the
774 * old extent, add summary data for them to be free.
776 if (preblock
< start
) {
777 error
= xfs_rtmodify_summary(mp
, tp
,
778 XFS_RTBLOCKLOG(start
- preblock
),
779 XFS_BITTOBLOCK(mp
, preblock
), 1, rbpp
, rsb
);
785 * If there are blocks not being allocated at the end of the
786 * old extent, add summary data for them to be free.
788 if (postblock
> end
) {
789 error
= xfs_rtmodify_summary(mp
, tp
,
790 XFS_RTBLOCKLOG(postblock
- end
),
791 XFS_BITTOBLOCK(mp
, end
+ 1), 1, rbpp
, rsb
);
797 * Modify the bitmap to mark this extent allocated.
799 error
= xfs_rtmodify_range(mp
, tp
, start
, len
, 0);
804 * Return whether there are any free extents in the size range given
805 * by low and high, for the bitmap block bbno.
807 STATIC
int /* error */
809 xfs_mount_t
*mp
, /* file system mount structure */
810 xfs_trans_t
*tp
, /* transaction pointer */
811 int low
, /* low log2 extent size */
812 int high
, /* high log2 extent size */
813 xfs_rtblock_t bbno
, /* bitmap block number */
814 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
815 xfs_fsblock_t
*rsb
, /* in/out: summary block number */
816 int *stat
) /* out: any good extents here? */
818 int error
; /* error value */
819 int log
; /* loop counter, log2 of ext. size */
820 xfs_suminfo_t sum
; /* summary data */
823 * Loop over logs of extent sizes. Order is irrelevant.
825 for (log
= low
; log
<= high
; log
++) {
827 * Get one summary datum.
829 error
= xfs_rtget_summary(mp
, tp
, log
, bbno
, rbpp
, rsb
, &sum
);
834 * If there are any, return success.
842 * Found nothing, return failure.
849 * Get a buffer for the bitmap or summary file block specified.
850 * The buffer is returned read and locked.
852 STATIC
int /* error */
854 xfs_mount_t
*mp
, /* file system mount structure */
855 xfs_trans_t
*tp
, /* transaction pointer */
856 xfs_rtblock_t block
, /* block number in bitmap or summary */
857 int issum
, /* is summary not bitmap */
858 xfs_buf_t
**bpp
) /* output: buffer for the block */
860 xfs_buf_t
*bp
; /* block buffer, result */
861 xfs_daddr_t d
; /* disk addr of block */
862 int error
; /* error value */
863 xfs_fsblock_t fsb
; /* fs block number for block */
864 xfs_inode_t
*ip
; /* bitmap or summary inode */
866 ip
= issum
? mp
->m_rsumip
: mp
->m_rbmip
;
868 * Map from the file offset (block) and inode number to the
871 error
= xfs_bmapi_single(tp
, ip
, XFS_DATA_FORK
, &fsb
, block
);
875 ASSERT(fsb
!= NULLFSBLOCK
);
877 * Convert to disk address for buffer cache.
879 d
= XFS_FSB_TO_DADDR(mp
, fsb
);
883 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
,
884 mp
->m_bsize
, 0, &bp
);
888 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
895 * Check that the given extent (block range) is allocated already.
897 STATIC
int /* error */
898 xfs_rtcheck_alloc_range(
899 xfs_mount_t
*mp
, /* file system mount point */
900 xfs_trans_t
*tp
, /* transaction pointer */
901 xfs_rtblock_t bno
, /* starting block number of extent */
902 xfs_extlen_t len
, /* length of extent */
903 int *stat
) /* out: 1 for allocated, 0 for not */
905 xfs_rtblock_t
new; /* dummy for xfs_rtcheck_range */
907 return xfs_rtcheck_range(mp
, tp
, bno
, len
, 0, &new, stat
);
912 * Check that the given range is either all allocated (val = 0) or
913 * all free (val = 1).
915 STATIC
int /* error */
917 xfs_mount_t
*mp
, /* file system mount point */
918 xfs_trans_t
*tp
, /* transaction pointer */
919 xfs_rtblock_t start
, /* starting block number of extent */
920 xfs_extlen_t len
, /* length of extent */
921 int val
, /* 1 for free, 0 for allocated */
922 xfs_rtblock_t
*new, /* out: first block not matching */
923 int *stat
) /* out: 1 for matches, 0 for not */
925 xfs_rtword_t
*b
; /* current word in buffer */
926 int bit
; /* bit number in the word */
927 xfs_rtblock_t block
; /* bitmap block number */
928 xfs_buf_t
*bp
; /* buf for the block */
929 xfs_rtword_t
*bufp
; /* starting word in buffer */
930 int error
; /* error value */
931 xfs_rtblock_t i
; /* current bit number rel. to start */
932 xfs_rtblock_t lastbit
; /* last useful bit in word */
933 xfs_rtword_t mask
; /* mask of relevant bits for value */
934 xfs_rtword_t wdiff
; /* difference from wanted value */
935 int word
; /* word number in the buffer */
938 * Compute starting bitmap block number
940 block
= XFS_BITTOBLOCK(mp
, start
);
942 * Read the bitmap block.
944 error
= xfs_rtbuf_get(mp
, tp
, block
, 0, &bp
);
948 bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
950 * Compute the starting word's address, and starting bit.
952 word
= XFS_BITTOWORD(mp
, start
);
954 bit
= (int)(start
& (XFS_NBWORD
- 1));
956 * 0 (allocated) => all zero's; 1 (free) => all one's.
960 * If not starting on a word boundary, deal with the first
965 * Compute first bit not examined.
967 lastbit
= XFS_RTMIN(bit
+ len
, XFS_NBWORD
);
969 * Mask of relevant bits.
971 mask
= (((xfs_rtword_t
)1 << (lastbit
- bit
)) - 1) << bit
;
973 * Compute difference between actual and desired value.
975 if ((wdiff
= (*b
^ val
) & mask
)) {
977 * Different, compute first wrong bit and return.
979 xfs_trans_brelse(tp
, bp
);
980 i
= XFS_RTLOBIT(wdiff
) - bit
;
987 * Go on to next block if that's where the next word is
988 * and we need the next word.
990 if (++word
== XFS_BLOCKWSIZE(mp
) && i
< len
) {
992 * If done with this block, get the next one.
994 xfs_trans_brelse(tp
, bp
);
995 error
= xfs_rtbuf_get(mp
, tp
, ++block
, 0, &bp
);
999 b
= bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1003 * Go on to the next word in the buffer.
1009 * Starting on a word boundary, no partial word.
1014 * Loop over whole words in buffers. When we use up one buffer
1015 * we move on to the next one.
1017 while (len
- i
>= XFS_NBWORD
) {
1019 * Compute difference between actual and desired value.
1021 if ((wdiff
= *b
^ val
)) {
1023 * Different, compute first wrong bit and return.
1025 xfs_trans_brelse(tp
, bp
);
1026 i
+= XFS_RTLOBIT(wdiff
);
1033 * Go on to next block if that's where the next word is
1034 * and we need the next word.
1036 if (++word
== XFS_BLOCKWSIZE(mp
) && i
< len
) {
1038 * If done with this block, get the next one.
1040 xfs_trans_brelse(tp
, bp
);
1041 error
= xfs_rtbuf_get(mp
, tp
, ++block
, 0, &bp
);
1045 b
= bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1049 * Go on to the next word in the buffer.
1055 * If not ending on a word boundary, deal with the last
1058 if ((lastbit
= len
- i
)) {
1060 * Mask of relevant bits.
1062 mask
= ((xfs_rtword_t
)1 << lastbit
) - 1;
1064 * Compute difference between actual and desired value.
1066 if ((wdiff
= (*b
^ val
) & mask
)) {
1068 * Different, compute first wrong bit and return.
1070 xfs_trans_brelse(tp
, bp
);
1071 i
+= XFS_RTLOBIT(wdiff
);
1079 * Successful, return.
1081 xfs_trans_brelse(tp
, bp
);
1088 * Copy and transform the summary file, given the old and new
1089 * parameters in the mount structures.
1091 STATIC
int /* error */
1093 xfs_mount_t
*omp
, /* old file system mount point */
1094 xfs_mount_t
*nmp
, /* new file system mount point */
1095 xfs_trans_t
*tp
) /* transaction pointer */
1097 xfs_rtblock_t bbno
; /* bitmap block number */
1098 xfs_buf_t
*bp
; /* summary buffer */
1099 int error
; /* error return value */
1100 int log
; /* summary level number (log length) */
1101 xfs_suminfo_t sum
; /* summary data */
1102 xfs_fsblock_t sumbno
; /* summary block number */
1105 for (log
= omp
->m_rsumlevels
- 1; log
>= 0; log
--) {
1106 for (bbno
= omp
->m_sb
.sb_rbmblocks
- 1;
1107 (xfs_srtblock_t
)bbno
>= 0;
1109 error
= xfs_rtget_summary(omp
, tp
, log
, bbno
, &bp
,
1115 error
= xfs_rtmodify_summary(omp
, tp
, log
, bbno
, -sum
,
1119 error
= xfs_rtmodify_summary(nmp
, tp
, log
, bbno
, sum
,
1130 * Searching backward from start to limit, find the first block whose
1131 * allocated/free state is different from start's.
1133 STATIC
int /* error */
1135 xfs_mount_t
*mp
, /* file system mount point */
1136 xfs_trans_t
*tp
, /* transaction pointer */
1137 xfs_rtblock_t start
, /* starting block to look at */
1138 xfs_rtblock_t limit
, /* last block to look at */
1139 xfs_rtblock_t
*rtblock
) /* out: start block found */
1141 xfs_rtword_t
*b
; /* current word in buffer */
1142 int bit
; /* bit number in the word */
1143 xfs_rtblock_t block
; /* bitmap block number */
1144 xfs_buf_t
*bp
; /* buf for the block */
1145 xfs_rtword_t
*bufp
; /* starting word in buffer */
1146 int error
; /* error value */
1147 xfs_rtblock_t firstbit
; /* first useful bit in the word */
1148 xfs_rtblock_t i
; /* current bit number rel. to start */
1149 xfs_rtblock_t len
; /* length of inspected area */
1150 xfs_rtword_t mask
; /* mask of relevant bits for value */
1151 xfs_rtword_t want
; /* mask for "good" values */
1152 xfs_rtword_t wdiff
; /* difference from wanted value */
1153 int word
; /* word number in the buffer */
1156 * Compute and read in starting bitmap block for starting block.
1158 block
= XFS_BITTOBLOCK(mp
, start
);
1159 error
= xfs_rtbuf_get(mp
, tp
, block
, 0, &bp
);
1163 bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1165 * Get the first word's index & point to it.
1167 word
= XFS_BITTOWORD(mp
, start
);
1169 bit
= (int)(start
& (XFS_NBWORD
- 1));
1170 len
= start
- limit
+ 1;
1172 * Compute match value, based on the bit at start: if 1 (free)
1173 * then all-ones, else all-zeroes.
1175 want
= (*b
& ((xfs_rtword_t
)1 << bit
)) ? -1 : 0;
1177 * If the starting position is not word-aligned, deal with the
1180 if (bit
< XFS_NBWORD
- 1) {
1182 * Calculate first (leftmost) bit number to look at,
1183 * and mask for all the relevant bits in this word.
1185 firstbit
= XFS_RTMAX((xfs_srtblock_t
)(bit
- len
+ 1), 0);
1186 mask
= (((xfs_rtword_t
)1 << (bit
- firstbit
+ 1)) - 1) <<
1189 * Calculate the difference between the value there
1190 * and what we're looking for.
1192 if ((wdiff
= (*b
^ want
) & mask
)) {
1194 * Different. Mark where we are and return.
1196 xfs_trans_brelse(tp
, bp
);
1197 i
= bit
- XFS_RTHIBIT(wdiff
);
1198 *rtblock
= start
- i
+ 1;
1201 i
= bit
- firstbit
+ 1;
1203 * Go on to previous block if that's where the previous word is
1204 * and we need the previous word.
1206 if (--word
== -1 && i
< len
) {
1208 * If done with this block, get the previous one.
1210 xfs_trans_brelse(tp
, bp
);
1211 error
= xfs_rtbuf_get(mp
, tp
, --block
, 0, &bp
);
1215 bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1216 word
= XFS_BLOCKWMASK(mp
);
1220 * Go on to the previous word in the buffer.
1226 * Starting on a word boundary, no partial word.
1231 * Loop over whole words in buffers. When we use up one buffer
1232 * we move on to the previous one.
1234 while (len
- i
>= XFS_NBWORD
) {
1236 * Compute difference between actual and desired value.
1238 if ((wdiff
= *b
^ want
)) {
1240 * Different, mark where we are and return.
1242 xfs_trans_brelse(tp
, bp
);
1243 i
+= XFS_NBWORD
- 1 - XFS_RTHIBIT(wdiff
);
1244 *rtblock
= start
- i
+ 1;
1249 * Go on to previous block if that's where the previous word is
1250 * and we need the previous word.
1252 if (--word
== -1 && i
< len
) {
1254 * If done with this block, get the previous one.
1256 xfs_trans_brelse(tp
, bp
);
1257 error
= xfs_rtbuf_get(mp
, tp
, --block
, 0, &bp
);
1261 bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1262 word
= XFS_BLOCKWMASK(mp
);
1266 * Go on to the previous word in the buffer.
1272 * If not ending on a word boundary, deal with the last
1277 * Calculate first (leftmost) bit number to look at,
1278 * and mask for all the relevant bits in this word.
1280 firstbit
= XFS_NBWORD
- (len
- i
);
1281 mask
= (((xfs_rtword_t
)1 << (len
- i
)) - 1) << firstbit
;
1283 * Compute difference between actual and desired value.
1285 if ((wdiff
= (*b
^ want
) & mask
)) {
1287 * Different, mark where we are and return.
1289 xfs_trans_brelse(tp
, bp
);
1290 i
+= XFS_NBWORD
- 1 - XFS_RTHIBIT(wdiff
);
1291 *rtblock
= start
- i
+ 1;
1297 * No match, return that we scanned the whole area.
1299 xfs_trans_brelse(tp
, bp
);
1300 *rtblock
= start
- i
+ 1;
1305 * Searching forward from start to limit, find the first block whose
1306 * allocated/free state is different from start's.
1308 STATIC
int /* error */
1310 xfs_mount_t
*mp
, /* file system mount point */
1311 xfs_trans_t
*tp
, /* transaction pointer */
1312 xfs_rtblock_t start
, /* starting block to look at */
1313 xfs_rtblock_t limit
, /* last block to look at */
1314 xfs_rtblock_t
*rtblock
) /* out: start block found */
1316 xfs_rtword_t
*b
; /* current word in buffer */
1317 int bit
; /* bit number in the word */
1318 xfs_rtblock_t block
; /* bitmap block number */
1319 xfs_buf_t
*bp
; /* buf for the block */
1320 xfs_rtword_t
*bufp
; /* starting word in buffer */
1321 int error
; /* error value */
1322 xfs_rtblock_t i
; /* current bit number rel. to start */
1323 xfs_rtblock_t lastbit
; /* last useful bit in the word */
1324 xfs_rtblock_t len
; /* length of inspected area */
1325 xfs_rtword_t mask
; /* mask of relevant bits for value */
1326 xfs_rtword_t want
; /* mask for "good" values */
1327 xfs_rtword_t wdiff
; /* difference from wanted value */
1328 int word
; /* word number in the buffer */
1331 * Compute and read in starting bitmap block for starting block.
1333 block
= XFS_BITTOBLOCK(mp
, start
);
1334 error
= xfs_rtbuf_get(mp
, tp
, block
, 0, &bp
);
1338 bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1340 * Get the first word's index & point to it.
1342 word
= XFS_BITTOWORD(mp
, start
);
1344 bit
= (int)(start
& (XFS_NBWORD
- 1));
1345 len
= limit
- start
+ 1;
1347 * Compute match value, based on the bit at start: if 1 (free)
1348 * then all-ones, else all-zeroes.
1350 want
= (*b
& ((xfs_rtword_t
)1 << bit
)) ? -1 : 0;
1352 * If the starting position is not word-aligned, deal with the
1357 * Calculate last (rightmost) bit number to look at,
1358 * and mask for all the relevant bits in this word.
1360 lastbit
= XFS_RTMIN(bit
+ len
, XFS_NBWORD
);
1361 mask
= (((xfs_rtword_t
)1 << (lastbit
- bit
)) - 1) << bit
;
1363 * Calculate the difference between the value there
1364 * and what we're looking for.
1366 if ((wdiff
= (*b
^ want
) & mask
)) {
1368 * Different. Mark where we are and return.
1370 xfs_trans_brelse(tp
, bp
);
1371 i
= XFS_RTLOBIT(wdiff
) - bit
;
1372 *rtblock
= start
+ i
- 1;
1377 * Go on to next block if that's where the next word is
1378 * and we need the next word.
1380 if (++word
== XFS_BLOCKWSIZE(mp
) && i
< len
) {
1382 * If done with this block, get the previous one.
1384 xfs_trans_brelse(tp
, bp
);
1385 error
= xfs_rtbuf_get(mp
, tp
, ++block
, 0, &bp
);
1389 b
= bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1393 * Go on to the previous word in the buffer.
1399 * Starting on a word boundary, no partial word.
1404 * Loop over whole words in buffers. When we use up one buffer
1405 * we move on to the next one.
1407 while (len
- i
>= XFS_NBWORD
) {
1409 * Compute difference between actual and desired value.
1411 if ((wdiff
= *b
^ want
)) {
1413 * Different, mark where we are and return.
1415 xfs_trans_brelse(tp
, bp
);
1416 i
+= XFS_RTLOBIT(wdiff
);
1417 *rtblock
= start
+ i
- 1;
1422 * Go on to next block if that's where the next word is
1423 * and we need the next word.
1425 if (++word
== XFS_BLOCKWSIZE(mp
) && i
< len
) {
1427 * If done with this block, get the next one.
1429 xfs_trans_brelse(tp
, bp
);
1430 error
= xfs_rtbuf_get(mp
, tp
, ++block
, 0, &bp
);
1434 b
= bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1438 * Go on to the next word in the buffer.
1444 * If not ending on a word boundary, deal with the last
1447 if ((lastbit
= len
- i
)) {
1449 * Calculate mask for all the relevant bits in this word.
1451 mask
= ((xfs_rtword_t
)1 << lastbit
) - 1;
1453 * Compute difference between actual and desired value.
1455 if ((wdiff
= (*b
^ want
) & mask
)) {
1457 * Different, mark where we are and return.
1459 xfs_trans_brelse(tp
, bp
);
1460 i
+= XFS_RTLOBIT(wdiff
);
1461 *rtblock
= start
+ i
- 1;
1467 * No match, return that we scanned the whole area.
1469 xfs_trans_brelse(tp
, bp
);
1470 *rtblock
= start
+ i
- 1;
1475 * Mark an extent specified by start and len freed.
1476 * Updates all the summary information as well as the bitmap.
1478 STATIC
int /* error */
1480 xfs_mount_t
*mp
, /* file system mount point */
1481 xfs_trans_t
*tp
, /* transaction pointer */
1482 xfs_rtblock_t start
, /* starting block to free */
1483 xfs_extlen_t len
, /* length to free */
1484 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
1485 xfs_fsblock_t
*rsb
) /* in/out: summary block number */
1487 xfs_rtblock_t end
; /* end of the freed extent */
1488 int error
; /* error value */
1489 xfs_rtblock_t postblock
; /* first block freed > end */
1490 xfs_rtblock_t preblock
; /* first block freed < start */
1492 end
= start
+ len
- 1;
1494 * Modify the bitmap to mark this extent freed.
1496 error
= xfs_rtmodify_range(mp
, tp
, start
, len
, 1);
1501 * Assume we're freeing out of the middle of an allocated extent.
1502 * We need to find the beginning and end of the extent so we can
1503 * properly update the summary.
1505 error
= xfs_rtfind_back(mp
, tp
, start
, 0, &preblock
);
1510 * Find the next allocated block (end of allocated extent).
1512 error
= xfs_rtfind_forw(mp
, tp
, end
, mp
->m_sb
.sb_rextents
- 1,
1517 * If there are blocks not being freed at the front of the
1518 * old extent, add summary data for them to be allocated.
1520 if (preblock
< start
) {
1521 error
= xfs_rtmodify_summary(mp
, tp
,
1522 XFS_RTBLOCKLOG(start
- preblock
),
1523 XFS_BITTOBLOCK(mp
, preblock
), -1, rbpp
, rsb
);
1529 * If there are blocks not being freed at the end of the
1530 * old extent, add summary data for them to be allocated.
1532 if (postblock
> end
) {
1533 error
= xfs_rtmodify_summary(mp
, tp
,
1534 XFS_RTBLOCKLOG(postblock
- end
),
1535 XFS_BITTOBLOCK(mp
, end
+ 1), -1, rbpp
, rsb
);
1541 * Increment the summary information corresponding to the entire
1542 * (new) free extent.
1544 error
= xfs_rtmodify_summary(mp
, tp
,
1545 XFS_RTBLOCKLOG(postblock
+ 1 - preblock
),
1546 XFS_BITTOBLOCK(mp
, preblock
), 1, rbpp
, rsb
);
1551 * Read and return the summary information for a given extent size,
1552 * bitmap block combination.
1553 * Keeps track of a current summary block, so we don't keep reading
1554 * it from the buffer cache.
1556 STATIC
int /* error */
1558 xfs_mount_t
*mp
, /* file system mount structure */
1559 xfs_trans_t
*tp
, /* transaction pointer */
1560 int log
, /* log2 of extent size */
1561 xfs_rtblock_t bbno
, /* bitmap block number */
1562 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
1563 xfs_fsblock_t
*rsb
, /* in/out: summary block number */
1564 xfs_suminfo_t
*sum
) /* out: summary info for this block */
1566 xfs_buf_t
*bp
; /* buffer for summary block */
1567 int error
; /* error value */
1568 xfs_fsblock_t sb
; /* summary fsblock */
1569 int so
; /* index into the summary file */
1570 xfs_suminfo_t
*sp
; /* pointer to returned data */
1573 * Compute entry number in the summary file.
1575 so
= XFS_SUMOFFS(mp
, log
, bbno
);
1577 * Compute the block number in the summary file.
1579 sb
= XFS_SUMOFFSTOBLOCK(mp
, so
);
1581 * If we have an old buffer, and the block number matches, use that.
1583 if (rbpp
&& *rbpp
&& *rsb
== sb
)
1586 * Otherwise we have to get the buffer.
1590 * If there was an old one, get rid of it first.
1593 xfs_trans_brelse(tp
, *rbpp
);
1594 error
= xfs_rtbuf_get(mp
, tp
, sb
, 1, &bp
);
1599 * Remember this buffer and block for the next call.
1607 * Point to the summary information & copy it out.
1609 sp
= XFS_SUMPTR(mp
, bp
, so
);
1612 * Drop the buffer if we're not asked to remember it.
1615 xfs_trans_brelse(tp
, bp
);
1620 * Set the given range of bitmap bits to the given value.
1621 * Do whatever I/O and logging is required.
1623 STATIC
int /* error */
1625 xfs_mount_t
*mp
, /* file system mount point */
1626 xfs_trans_t
*tp
, /* transaction pointer */
1627 xfs_rtblock_t start
, /* starting block to modify */
1628 xfs_extlen_t len
, /* length of extent to modify */
1629 int val
) /* 1 for free, 0 for allocated */
1631 xfs_rtword_t
*b
; /* current word in buffer */
1632 int bit
; /* bit number in the word */
1633 xfs_rtblock_t block
; /* bitmap block number */
1634 xfs_buf_t
*bp
; /* buf for the block */
1635 xfs_rtword_t
*bufp
; /* starting word in buffer */
1636 int error
; /* error value */
1637 xfs_rtword_t
*first
; /* first used word in the buffer */
1638 int i
; /* current bit number rel. to start */
1639 int lastbit
; /* last useful bit in word */
1640 xfs_rtword_t mask
; /* mask o frelevant bits for value */
1641 int word
; /* word number in the buffer */
1644 * Compute starting bitmap block number.
1646 block
= XFS_BITTOBLOCK(mp
, start
);
1648 * Read the bitmap block, and point to its data.
1650 error
= xfs_rtbuf_get(mp
, tp
, block
, 0, &bp
);
1654 bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1656 * Compute the starting word's address, and starting bit.
1658 word
= XFS_BITTOWORD(mp
, start
);
1659 first
= b
= &bufp
[word
];
1660 bit
= (int)(start
& (XFS_NBWORD
- 1));
1662 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1666 * If not starting on a word boundary, deal with the first
1671 * Compute first bit not changed and mask of relevant bits.
1673 lastbit
= XFS_RTMIN(bit
+ len
, XFS_NBWORD
);
1674 mask
= (((xfs_rtword_t
)1 << (lastbit
- bit
)) - 1) << bit
;
1676 * Set/clear the active bits.
1684 * Go on to the next block if that's where the next word is
1685 * and we need the next word.
1687 if (++word
== XFS_BLOCKWSIZE(mp
) && i
< len
) {
1689 * Log the changed part of this block.
1692 xfs_trans_log_buf(tp
, bp
,
1693 (uint
)((char *)first
- (char *)bufp
),
1694 (uint
)((char *)b
- (char *)bufp
));
1695 error
= xfs_rtbuf_get(mp
, tp
, ++block
, 0, &bp
);
1699 first
= b
= bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1703 * Go on to the next word in the buffer
1709 * Starting on a word boundary, no partial word.
1714 * Loop over whole words in buffers. When we use up one buffer
1715 * we move on to the next one.
1717 while (len
- i
>= XFS_NBWORD
) {
1719 * Set the word value correctly.
1724 * Go on to the next block if that's where the next word is
1725 * and we need the next word.
1727 if (++word
== XFS_BLOCKWSIZE(mp
) && i
< len
) {
1729 * Log the changed part of this block.
1732 xfs_trans_log_buf(tp
, bp
,
1733 (uint
)((char *)first
- (char *)bufp
),
1734 (uint
)((char *)b
- (char *)bufp
));
1735 error
= xfs_rtbuf_get(mp
, tp
, ++block
, 0, &bp
);
1739 first
= b
= bufp
= (xfs_rtword_t
*)XFS_BUF_PTR(bp
);
1743 * Go on to the next word in the buffer
1749 * If not ending on a word boundary, deal with the last
1752 if ((lastbit
= len
- i
)) {
1754 * Compute a mask of relevant bits.
1757 mask
= ((xfs_rtword_t
)1 << lastbit
) - 1;
1759 * Set/clear the active bits.
1768 * Log any remaining changed bytes.
1771 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)first
- (char *)bufp
),
1772 (uint
)((char *)b
- (char *)bufp
- 1));
1777 * Read and modify the summary information for a given extent size,
1778 * bitmap block combination.
1779 * Keeps track of a current summary block, so we don't keep reading
1780 * it from the buffer cache.
1782 STATIC
int /* error */
1783 xfs_rtmodify_summary(
1784 xfs_mount_t
*mp
, /* file system mount point */
1785 xfs_trans_t
*tp
, /* transaction pointer */
1786 int log
, /* log2 of extent size */
1787 xfs_rtblock_t bbno
, /* bitmap block number */
1788 int delta
, /* change to make to summary info */
1789 xfs_buf_t
**rbpp
, /* in/out: summary block buffer */
1790 xfs_fsblock_t
*rsb
) /* in/out: summary block number */
1792 xfs_buf_t
*bp
; /* buffer for the summary block */
1793 int error
; /* error value */
1794 xfs_fsblock_t sb
; /* summary fsblock */
1795 int so
; /* index into the summary file */
1796 xfs_suminfo_t
*sp
; /* pointer to returned data */
1799 * Compute entry number in the summary file.
1801 so
= XFS_SUMOFFS(mp
, log
, bbno
);
1803 * Compute the block number in the summary file.
1805 sb
= XFS_SUMOFFSTOBLOCK(mp
, so
);
1807 * If we have an old buffer, and the block number matches, use that.
1809 if (rbpp
&& *rbpp
&& *rsb
== sb
)
1812 * Otherwise we have to get the buffer.
1816 * If there was an old one, get rid of it first.
1819 xfs_trans_brelse(tp
, *rbpp
);
1820 error
= xfs_rtbuf_get(mp
, tp
, sb
, 1, &bp
);
1825 * Remember this buffer and block for the next call.
1833 * Point to the summary information, modify and log it.
1835 sp
= XFS_SUMPTR(mp
, bp
, so
);
1837 xfs_trans_log_buf(tp
, bp
, (uint
)((char *)sp
- (char *)XFS_BUF_PTR(bp
)),
1838 (uint
)((char *)sp
- (char *)XFS_BUF_PTR(bp
) + sizeof(*sp
) - 1));
1843 * Visible (exported) functions.
1847 * Grow the realtime area of the filesystem.
1851 xfs_mount_t
*mp
, /* mount point for filesystem */
1852 xfs_growfs_rt_t
*in
) /* growfs rt input struct */
1854 xfs_rtblock_t bmbno
; /* bitmap block number */
1855 xfs_buf_t
*bp
; /* temporary buffer */
1856 int error
; /* error return value */
1857 xfs_inode_t
*ip
; /* bitmap inode, used as lock */
1858 xfs_mount_t
*nmp
; /* new (fake) mount structure */
1859 xfs_drfsbno_t nrblocks
; /* new number of realtime blocks */
1860 xfs_extlen_t nrbmblocks
; /* new number of rt bitmap blocks */
1861 xfs_drtbno_t nrextents
; /* new number of realtime extents */
1862 uint8_t nrextslog
; /* new log2 of sb_rextents */
1863 xfs_extlen_t nrsumblocks
; /* new number of summary blocks */
1864 uint nrsumlevels
; /* new rt summary levels */
1865 uint nrsumsize
; /* new size of rt summary, bytes */
1866 xfs_sb_t
*nsbp
; /* new superblock */
1867 xfs_extlen_t rbmblocks
; /* current number of rt bitmap blocks */
1868 xfs_extlen_t rsumblocks
; /* current number of rt summary blks */
1869 xfs_sb_t
*sbp
; /* old superblock */
1870 xfs_fsblock_t sumbno
; /* summary block number */
1874 * Initial error checking.
1876 if (!capable(CAP_SYS_ADMIN
))
1877 return XFS_ERROR(EPERM
);
1878 if (mp
->m_rtdev_targp
== NULL
|| mp
->m_rbmip
== NULL
||
1879 (nrblocks
= in
->newblocks
) <= sbp
->sb_rblocks
||
1880 (sbp
->sb_rblocks
&& (in
->extsize
!= sbp
->sb_rextsize
)))
1881 return XFS_ERROR(EINVAL
);
1882 if ((error
= xfs_sb_validate_fsb_count(sbp
, nrblocks
)))
1885 * Read in the last block of the device, make sure it exists.
1887 bp
= xfs_buf_read_uncached(mp
, mp
->m_rtdev_targp
,
1888 XFS_FSB_TO_BB(mp
, nrblocks
- 1),
1889 XFS_FSB_TO_B(mp
, 1), 0);
1895 * Calculate new parameters. These are the final values to be reached.
1897 nrextents
= nrblocks
;
1898 do_div(nrextents
, in
->extsize
);
1899 nrbmblocks
= howmany_64(nrextents
, NBBY
* sbp
->sb_blocksize
);
1900 nrextslog
= xfs_highbit32(nrextents
);
1901 nrsumlevels
= nrextslog
+ 1;
1902 nrsumsize
= (uint
)sizeof(xfs_suminfo_t
) * nrsumlevels
* nrbmblocks
;
1903 nrsumblocks
= XFS_B_TO_FSB(mp
, nrsumsize
);
1904 nrsumsize
= XFS_FSB_TO_B(mp
, nrsumblocks
);
1906 * New summary size can't be more than half the size of
1907 * the log. This prevents us from getting a log overflow,
1908 * since we'll log basically the whole summary file at once.
1910 if (nrsumblocks
> (mp
->m_sb
.sb_logblocks
>> 1))
1911 return XFS_ERROR(EINVAL
);
1913 * Get the old block counts for bitmap and summary inodes.
1914 * These can't change since other growfs callers are locked out.
1916 rbmblocks
= XFS_B_TO_FSB(mp
, mp
->m_rbmip
->i_d
.di_size
);
1917 rsumblocks
= XFS_B_TO_FSB(mp
, mp
->m_rsumip
->i_d
.di_size
);
1919 * Allocate space to the bitmap and summary files, as necessary.
1921 if ((error
= xfs_growfs_rt_alloc(mp
, rbmblocks
, nrbmblocks
,
1922 mp
->m_sb
.sb_rbmino
)))
1924 if ((error
= xfs_growfs_rt_alloc(mp
, rsumblocks
, nrsumblocks
,
1925 mp
->m_sb
.sb_rsumino
)))
1928 * Allocate a new (fake) mount/sb.
1930 nmp
= kmem_alloc(sizeof(*nmp
), KM_SLEEP
);
1932 * Loop over the bitmap blocks.
1933 * We will do everything one bitmap block at a time.
1934 * Skip the current block if it is exactly full.
1935 * This also deals with the case where there were no rtextents before.
1937 for (bmbno
= sbp
->sb_rbmblocks
-
1938 ((sbp
->sb_rextents
& ((1 << mp
->m_blkbit_log
) - 1)) != 0);
1942 int cancelflags
= 0;
1947 * Calculate new sb and mount fields for this round.
1949 nsbp
->sb_rextsize
= in
->extsize
;
1950 nsbp
->sb_rbmblocks
= bmbno
+ 1;
1953 nsbp
->sb_rbmblocks
* NBBY
*
1954 nsbp
->sb_blocksize
* nsbp
->sb_rextsize
);
1955 nsbp
->sb_rextents
= nsbp
->sb_rblocks
;
1956 do_div(nsbp
->sb_rextents
, nsbp
->sb_rextsize
);
1957 ASSERT(nsbp
->sb_rextents
!= 0);
1958 nsbp
->sb_rextslog
= xfs_highbit32(nsbp
->sb_rextents
);
1959 nrsumlevels
= nmp
->m_rsumlevels
= nsbp
->sb_rextslog
+ 1;
1961 (uint
)sizeof(xfs_suminfo_t
) * nrsumlevels
*
1963 nrsumblocks
= XFS_B_TO_FSB(mp
, nrsumsize
);
1964 nmp
->m_rsumsize
= nrsumsize
= XFS_FSB_TO_B(mp
, nrsumblocks
);
1966 * Start a transaction, get the log reservation.
1968 tp
= xfs_trans_alloc(mp
, XFS_TRANS_GROWFSRT_FREE
);
1969 if ((error
= xfs_trans_reserve(tp
, 0,
1970 XFS_GROWRTFREE_LOG_RES(nmp
), 0, 0, 0)))
1973 * Lock out other callers by grabbing the bitmap inode lock.
1975 if ((error
= xfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0,
1976 XFS_ILOCK_EXCL
, &ip
)))
1978 ASSERT(ip
== mp
->m_rbmip
);
1980 * Update the bitmap inode's size.
1982 mp
->m_rbmip
->i_d
.di_size
=
1983 nsbp
->sb_rbmblocks
* nsbp
->sb_blocksize
;
1984 xfs_trans_log_inode(tp
, mp
->m_rbmip
, XFS_ILOG_CORE
);
1985 cancelflags
|= XFS_TRANS_ABORT
;
1987 * Get the summary inode into the transaction.
1989 if ((error
= xfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0,
1990 XFS_ILOCK_EXCL
, &ip
)))
1992 ASSERT(ip
== mp
->m_rsumip
);
1994 * Update the summary inode's size.
1996 mp
->m_rsumip
->i_d
.di_size
= nmp
->m_rsumsize
;
1997 xfs_trans_log_inode(tp
, mp
->m_rsumip
, XFS_ILOG_CORE
);
1999 * Copy summary data from old to new sizes.
2000 * Do this when the real size (not block-aligned) changes.
2002 if (sbp
->sb_rbmblocks
!= nsbp
->sb_rbmblocks
||
2003 mp
->m_rsumlevels
!= nmp
->m_rsumlevels
) {
2004 error
= xfs_rtcopy_summary(mp
, nmp
, tp
);
2009 * Update superblock fields.
2011 if (nsbp
->sb_rextsize
!= sbp
->sb_rextsize
)
2012 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_REXTSIZE
,
2013 nsbp
->sb_rextsize
- sbp
->sb_rextsize
);
2014 if (nsbp
->sb_rbmblocks
!= sbp
->sb_rbmblocks
)
2015 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RBMBLOCKS
,
2016 nsbp
->sb_rbmblocks
- sbp
->sb_rbmblocks
);
2017 if (nsbp
->sb_rblocks
!= sbp
->sb_rblocks
)
2018 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RBLOCKS
,
2019 nsbp
->sb_rblocks
- sbp
->sb_rblocks
);
2020 if (nsbp
->sb_rextents
!= sbp
->sb_rextents
)
2021 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_REXTENTS
,
2022 nsbp
->sb_rextents
- sbp
->sb_rextents
);
2023 if (nsbp
->sb_rextslog
!= sbp
->sb_rextslog
)
2024 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_REXTSLOG
,
2025 nsbp
->sb_rextslog
- sbp
->sb_rextslog
);
2030 error
= xfs_rtfree_range(nmp
, tp
, sbp
->sb_rextents
,
2031 nsbp
->sb_rextents
- sbp
->sb_rextents
, &bp
, &sumbno
);
2034 xfs_trans_cancel(tp
, cancelflags
);
2038 * Mark more blocks free in the superblock.
2040 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FREXTENTS
,
2041 nsbp
->sb_rextents
- sbp
->sb_rextents
);
2043 * Update mp values into the real mp structure.
2045 mp
->m_rsumlevels
= nrsumlevels
;
2046 mp
->m_rsumsize
= nrsumsize
;
2048 error
= xfs_trans_commit(tp
, 0);
2054 * Free the fake mp structure.
2062 * Allocate an extent in the realtime subvolume, with the usual allocation
2063 * parameters. The length units are all in realtime extents, as is the
2064 * result block number.
2067 xfs_rtallocate_extent(
2068 xfs_trans_t
*tp
, /* transaction pointer */
2069 xfs_rtblock_t bno
, /* starting block number to allocate */
2070 xfs_extlen_t minlen
, /* minimum length to allocate */
2071 xfs_extlen_t maxlen
, /* maximum length to allocate */
2072 xfs_extlen_t
*len
, /* out: actual length allocated */
2073 xfs_alloctype_t type
, /* allocation type XFS_ALLOCTYPE... */
2074 int wasdel
, /* was a delayed allocation extent */
2075 xfs_extlen_t prod
, /* extent product factor */
2076 xfs_rtblock_t
*rtblock
) /* out: start block allocated */
2078 int error
; /* error value */
2079 xfs_inode_t
*ip
; /* inode for bitmap file */
2080 xfs_mount_t
*mp
; /* file system mount structure */
2081 xfs_rtblock_t r
; /* result allocated block */
2082 xfs_fsblock_t sb
; /* summary file block number */
2083 xfs_buf_t
*sumbp
; /* summary file block buffer */
2085 ASSERT(minlen
> 0 && minlen
<= maxlen
);
2088 * If prod is set then figure out what to do to minlen and maxlen.
2093 if ((i
= maxlen
% prod
))
2095 if ((i
= minlen
% prod
))
2097 if (maxlen
< minlen
) {
2098 *rtblock
= NULLRTBLOCK
;
2103 * Lock out other callers by grabbing the bitmap inode lock.
2105 if ((error
= xfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0,
2106 XFS_ILOCK_EXCL
, &ip
)))
2110 * Allocate by size, or near another block, or exactly at some block.
2113 case XFS_ALLOCTYPE_ANY_AG
:
2114 error
= xfs_rtallocate_extent_size(mp
, tp
, minlen
, maxlen
, len
,
2115 &sumbp
, &sb
, prod
, &r
);
2117 case XFS_ALLOCTYPE_NEAR_BNO
:
2118 error
= xfs_rtallocate_extent_near(mp
, tp
, bno
, minlen
, maxlen
,
2119 len
, &sumbp
, &sb
, prod
, &r
);
2121 case XFS_ALLOCTYPE_THIS_BNO
:
2122 error
= xfs_rtallocate_extent_exact(mp
, tp
, bno
, minlen
, maxlen
,
2123 len
, &sumbp
, &sb
, prod
, &r
);
2132 * If it worked, update the superblock.
2134 if (r
!= NULLRTBLOCK
) {
2135 long slen
= (long)*len
;
2137 ASSERT(*len
>= minlen
&& *len
<= maxlen
);
2139 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_RES_FREXTENTS
, -slen
);
2141 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FREXTENTS
, -slen
);
2148 * Free an extent in the realtime subvolume. Length is expressed in
2149 * realtime extents, as is the block number.
2153 xfs_trans_t
*tp
, /* transaction pointer */
2154 xfs_rtblock_t bno
, /* starting block number to free */
2155 xfs_extlen_t len
) /* length of extent freed */
2157 int error
; /* error value */
2158 xfs_inode_t
*ip
; /* bitmap file inode */
2159 xfs_mount_t
*mp
; /* file system mount structure */
2160 xfs_fsblock_t sb
; /* summary file block number */
2161 xfs_buf_t
*sumbp
; /* summary file block buffer */
2165 * Synchronize by locking the bitmap inode.
2167 if ((error
= xfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0,
2168 XFS_ILOCK_EXCL
, &ip
)))
2170 #if defined(__KERNEL__) && defined(DEBUG)
2172 * Check to see that this whole range is currently allocated.
2175 int stat
; /* result from checking range */
2177 error
= xfs_rtcheck_alloc_range(mp
, tp
, bno
, len
, &stat
);
2186 * Free the range of realtime blocks.
2188 error
= xfs_rtfree_range(mp
, tp
, bno
, len
, &sumbp
, &sb
);
2193 * Mark more blocks free in the superblock.
2195 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_FREXTENTS
, (long)len
);
2197 * If we've now freed all the blocks, reset the file sequence
2200 if (tp
->t_frextents_delta
+ mp
->m_sb
.sb_frextents
==
2201 mp
->m_sb
.sb_rextents
) {
2202 if (!(ip
->i_d
.di_flags
& XFS_DIFLAG_NEWRTBM
))
2203 ip
->i_d
.di_flags
|= XFS_DIFLAG_NEWRTBM
;
2204 *(__uint64_t
*)&ip
->i_d
.di_atime
= 0;
2205 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2211 * Initialize realtime fields in the mount structure.
2215 xfs_mount_t
*mp
) /* file system mount structure */
2217 xfs_buf_t
*bp
; /* buffer for last block of subvolume */
2218 xfs_daddr_t d
; /* address of last block of subvolume */
2219 xfs_sb_t
*sbp
; /* filesystem superblock copy in mount */
2222 if (sbp
->sb_rblocks
== 0)
2224 if (mp
->m_rtdev_targp
== NULL
) {
2226 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2227 return XFS_ERROR(ENODEV
);
2229 mp
->m_rsumlevels
= sbp
->sb_rextslog
+ 1;
2231 (uint
)sizeof(xfs_suminfo_t
) * mp
->m_rsumlevels
*
2233 mp
->m_rsumsize
= roundup(mp
->m_rsumsize
, sbp
->sb_blocksize
);
2234 mp
->m_rbmip
= mp
->m_rsumip
= NULL
;
2236 * Check that the realtime section is an ok size.
2238 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_rblocks
);
2239 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_rblocks
) {
2240 cmn_err(CE_WARN
, "XFS: realtime mount -- %llu != %llu",
2241 (unsigned long long) XFS_BB_TO_FSB(mp
, d
),
2242 (unsigned long long) mp
->m_sb
.sb_rblocks
);
2243 return XFS_ERROR(EFBIG
);
2245 bp
= xfs_buf_read_uncached(mp
, mp
->m_rtdev_targp
,
2246 d
- XFS_FSB_TO_BB(mp
, 1),
2247 XFS_FSB_TO_B(mp
, 1), 0);
2249 cmn_err(CE_WARN
, "XFS: realtime device size check failed");
2257 * Get the bitmap and summary inodes into the mount structure
2262 xfs_mount_t
*mp
) /* file system mount structure */
2264 int error
; /* error return value */
2268 if (sbp
->sb_rbmino
== NULLFSINO
)
2270 error
= xfs_iget(mp
, NULL
, sbp
->sb_rbmino
, 0, 0, &mp
->m_rbmip
);
2273 ASSERT(mp
->m_rbmip
!= NULL
);
2274 ASSERT(sbp
->sb_rsumino
!= NULLFSINO
);
2275 error
= xfs_iget(mp
, NULL
, sbp
->sb_rsumino
, 0, 0, &mp
->m_rsumip
);
2280 ASSERT(mp
->m_rsumip
!= NULL
);
2285 xfs_rtunmount_inodes(
2286 struct xfs_mount
*mp
)
2291 IRELE(mp
->m_rsumip
);
2295 * Pick an extent for allocation at the start of a new realtime file.
2296 * Use the sequence number stored in the atime field of the bitmap inode.
2297 * Translate this to a fraction of the rtextents, and return the product
2298 * of rtextents and the fraction.
2299 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2303 xfs_mount_t
*mp
, /* file system mount point */
2304 xfs_trans_t
*tp
, /* transaction pointer */
2305 xfs_extlen_t len
, /* allocation length (rtextents) */
2306 xfs_rtblock_t
*pick
) /* result rt extent */
2308 xfs_rtblock_t b
; /* result block */
2309 int error
; /* error return value */
2310 xfs_inode_t
*ip
; /* bitmap incore inode */
2311 int log2
; /* log of sequence number */
2312 __uint64_t resid
; /* residual after log removed */
2313 __uint64_t seq
; /* sequence number of file creation */
2314 __uint64_t
*seqp
; /* pointer to seqno in inode */
2316 if ((error
= xfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0,
2317 XFS_ILOCK_EXCL
, &ip
)))
2319 ASSERT(ip
== mp
->m_rbmip
);
2320 seqp
= (__uint64_t
*)&ip
->i_d
.di_atime
;
2321 if (!(ip
->i_d
.di_flags
& XFS_DIFLAG_NEWRTBM
)) {
2322 ip
->i_d
.di_flags
|= XFS_DIFLAG_NEWRTBM
;
2326 if ((log2
= xfs_highbit64(seq
)) == -1)
2329 resid
= seq
- (1ULL << log2
);
2330 b
= (mp
->m_sb
.sb_rextents
* ((resid
<< 1) + 1ULL)) >>
2332 if (b
>= mp
->m_sb
.sb_rextents
)
2333 b
= do_mod(b
, mp
->m_sb
.sb_rextents
);
2334 if (b
+ len
> mp
->m_sb
.sb_rextents
)
2335 b
= mp
->m_sb
.sb_rextents
- len
;
2338 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);