ACPI: thinkpad-acpi: rename pci HID constant
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_rtalloc.c
blob880c73271c05fac890f3e0dcdd53a4805ae26be0
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_bmap.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_fsops.h"
43 #include "xfs_error.h"
44 #include "xfs_rw.h"
45 #include "xfs_inode_item.h"
46 #include "xfs_trans_space.h"
50 * Prototypes for internal functions.
54 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
55 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
56 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
57 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
58 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
59 xfs_extlen_t, int, xfs_rtblock_t *, int *);
60 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
61 xfs_rtblock_t, xfs_rtblock_t *);
62 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
63 xfs_rtblock_t, xfs_rtblock_t *);
64 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
65 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
66 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
67 xfs_extlen_t, int);
68 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
69 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
72 * Internal functions.
76 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
78 STATIC int
79 xfs_lowbit32(
80 __uint32_t v)
82 if (v)
83 return ffs(v) - 1;
84 return -1;
88 * Allocate space to the bitmap or summary file, and zero it, for growfs.
90 STATIC int /* error */
91 xfs_growfs_rt_alloc(
92 xfs_mount_t *mp, /* file system mount point */
93 xfs_extlen_t oblocks, /* old count of blocks */
94 xfs_extlen_t nblocks, /* new count of blocks */
95 xfs_ino_t ino) /* inode number (bitmap/summary) */
97 xfs_fileoff_t bno; /* block number in file */
98 xfs_buf_t *bp; /* temporary buffer for zeroing */
99 int cancelflags; /* flags for xfs_trans_cancel */
100 int committed; /* transaction committed flag */
101 xfs_daddr_t d; /* disk block address */
102 int error; /* error return value */
103 xfs_fsblock_t firstblock; /* first block allocated in xaction */
104 xfs_bmap_free_t flist; /* list of freed blocks */
105 xfs_fsblock_t fsbno; /* filesystem block for bno */
106 xfs_inode_t *ip; /* pointer to incore inode */
107 xfs_bmbt_irec_t map; /* block map output */
108 int nmap; /* number of block maps */
109 int resblks; /* space reservation */
110 xfs_trans_t *tp; /* transaction pointer */
113 * Allocate space to the file, as necessary.
115 while (oblocks < nblocks) {
116 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
117 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
118 cancelflags = 0;
120 * Reserve space & log for one extent added to the file.
122 if ((error = xfs_trans_reserve(tp, resblks,
123 XFS_GROWRTALLOC_LOG_RES(mp), 0,
124 XFS_TRANS_PERM_LOG_RES,
125 XFS_DEFAULT_PERM_LOG_COUNT)))
126 goto error_exit;
127 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
129 * Lock the inode.
131 if ((error = xfs_trans_iget(mp, tp, ino, 0,
132 XFS_ILOCK_EXCL, &ip)))
133 goto error_exit;
134 XFS_BMAP_INIT(&flist, &firstblock);
136 * Allocate blocks to the bitmap file.
138 nmap = 1;
139 cancelflags |= XFS_TRANS_ABORT;
140 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
141 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
142 resblks, &map, &nmap, &flist, NULL);
143 if (!error && nmap < 1)
144 error = XFS_ERROR(ENOSPC);
145 if (error)
146 goto error_exit;
148 * Free any blocks freed up in the transaction, then commit.
150 error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
151 if (error)
152 goto error_exit;
153 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
155 * Now we need to clear the allocated blocks.
156 * Do this one block per transaction, to keep it simple.
158 cancelflags = 0;
159 for (bno = map.br_startoff, fsbno = map.br_startblock;
160 bno < map.br_startoff + map.br_blockcount;
161 bno++, fsbno++) {
162 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
164 * Reserve log for one block zeroing.
166 if ((error = xfs_trans_reserve(tp, 0,
167 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
168 goto error_exit;
170 * Lock the bitmap inode.
172 if ((error = xfs_trans_iget(mp, tp, ino, 0,
173 XFS_ILOCK_EXCL, &ip)))
174 goto error_exit;
176 * Get a buffer for the block.
178 d = XFS_FSB_TO_DADDR(mp, fsbno);
179 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
180 mp->m_bsize, 0);
181 if (bp == NULL) {
182 error = XFS_ERROR(EIO);
183 goto error_exit;
185 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
186 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
188 * Commit the transaction.
190 xfs_trans_commit(tp, 0, NULL);
193 * Go on to the next extent, if any.
195 oblocks = map.br_startoff + map.br_blockcount;
197 return 0;
198 error_exit:
199 xfs_trans_cancel(tp, cancelflags);
200 return error;
204 * Attempt to allocate an extent minlen<=len<=maxlen starting from
205 * bitmap block bbno. If we don't get maxlen then use prod to trim
206 * the length, if given. Returns error; returns starting block in *rtblock.
207 * The lengths are all in rtextents.
209 STATIC int /* error */
210 xfs_rtallocate_extent_block(
211 xfs_mount_t *mp, /* file system mount point */
212 xfs_trans_t *tp, /* transaction pointer */
213 xfs_rtblock_t bbno, /* bitmap block number */
214 xfs_extlen_t minlen, /* minimum length to allocate */
215 xfs_extlen_t maxlen, /* maximum length to allocate */
216 xfs_extlen_t *len, /* out: actual length allocated */
217 xfs_rtblock_t *nextp, /* out: next block to try */
218 xfs_buf_t **rbpp, /* in/out: summary block buffer */
219 xfs_fsblock_t *rsb, /* in/out: summary block number */
220 xfs_extlen_t prod, /* extent product factor */
221 xfs_rtblock_t *rtblock) /* out: start block allocated */
223 xfs_rtblock_t besti; /* best rtblock found so far */
224 xfs_rtblock_t bestlen; /* best length found so far */
225 xfs_rtblock_t end; /* last rtblock in chunk */
226 int error; /* error value */
227 xfs_rtblock_t i; /* current rtblock trying */
228 xfs_rtblock_t next; /* next rtblock to try */
229 int stat; /* status from internal calls */
232 * Loop over all the extents starting in this bitmap block,
233 * looking for one that's long enough.
235 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
236 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
237 i <= end;
238 i++) {
240 * See if there's a free extent of maxlen starting at i.
241 * If it's not so then next will contain the first non-free.
243 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
244 if (error) {
245 return error;
247 if (stat) {
249 * i for maxlen is all free, allocate and return that.
251 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
252 rsb);
253 if (error) {
254 return error;
256 *len = maxlen;
257 *rtblock = i;
258 return 0;
261 * In the case where we have a variable-sized allocation
262 * request, figure out how big this free piece is,
263 * and if it's big enough for the minimum, and the best
264 * so far, remember it.
266 if (minlen < maxlen) {
267 xfs_rtblock_t thislen; /* this extent size */
269 thislen = next - i;
270 if (thislen >= minlen && thislen > bestlen) {
271 besti = i;
272 bestlen = thislen;
276 * If not done yet, find the start of the next free space.
278 if (next < end) {
279 error = xfs_rtfind_forw(mp, tp, next, end, &i);
280 if (error) {
281 return error;
283 } else
284 break;
287 * Searched the whole thing & didn't find a maxlen free extent.
289 if (minlen < maxlen && besti != -1) {
290 xfs_extlen_t p; /* amount to trim length by */
293 * If size should be a multiple of prod, make that so.
295 if (prod > 1 && (p = do_mod(bestlen, prod)))
296 bestlen -= p;
298 * Allocate besti for bestlen & return that.
300 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
301 if (error) {
302 return error;
304 *len = bestlen;
305 *rtblock = besti;
306 return 0;
309 * Allocation failed. Set *nextp to the next block to try.
311 *nextp = next;
312 *rtblock = NULLRTBLOCK;
313 return 0;
317 * Allocate an extent of length minlen<=len<=maxlen, starting at block
318 * bno. If we don't get maxlen then use prod to trim the length, if given.
319 * Returns error; returns starting block in *rtblock.
320 * The lengths are all in rtextents.
322 STATIC int /* error */
323 xfs_rtallocate_extent_exact(
324 xfs_mount_t *mp, /* file system mount point */
325 xfs_trans_t *tp, /* transaction pointer */
326 xfs_rtblock_t bno, /* starting block number to allocate */
327 xfs_extlen_t minlen, /* minimum length to allocate */
328 xfs_extlen_t maxlen, /* maximum length to allocate */
329 xfs_extlen_t *len, /* out: actual length allocated */
330 xfs_buf_t **rbpp, /* in/out: summary block buffer */
331 xfs_fsblock_t *rsb, /* in/out: summary block number */
332 xfs_extlen_t prod, /* extent product factor */
333 xfs_rtblock_t *rtblock) /* out: start block allocated */
335 int error; /* error value */
336 xfs_extlen_t i; /* extent length trimmed due to prod */
337 int isfree; /* extent is free */
338 xfs_rtblock_t next; /* next block to try (dummy) */
340 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
342 * Check if the range in question (for maxlen) is free.
344 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
345 if (error) {
346 return error;
348 if (isfree) {
350 * If it is, allocate it and return success.
352 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
353 if (error) {
354 return error;
356 *len = maxlen;
357 *rtblock = bno;
358 return 0;
361 * If not, allocate what there is, if it's at least minlen.
363 maxlen = next - bno;
364 if (maxlen < minlen) {
366 * Failed, return failure status.
368 *rtblock = NULLRTBLOCK;
369 return 0;
372 * Trim off tail of extent, if prod is specified.
374 if (prod > 1 && (i = maxlen % prod)) {
375 maxlen -= i;
376 if (maxlen < minlen) {
378 * Now we can't do it, return failure status.
380 *rtblock = NULLRTBLOCK;
381 return 0;
385 * Allocate what we can and return it.
387 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
388 if (error) {
389 return error;
391 *len = maxlen;
392 *rtblock = bno;
393 return 0;
397 * Allocate an extent of length minlen<=len<=maxlen, starting as near
398 * to bno as possible. If we don't get maxlen then use prod to trim
399 * the length, if given. The lengths are all in rtextents.
401 STATIC int /* error */
402 xfs_rtallocate_extent_near(
403 xfs_mount_t *mp, /* file system mount point */
404 xfs_trans_t *tp, /* transaction pointer */
405 xfs_rtblock_t bno, /* starting block number to allocate */
406 xfs_extlen_t minlen, /* minimum length to allocate */
407 xfs_extlen_t maxlen, /* maximum length to allocate */
408 xfs_extlen_t *len, /* out: actual length allocated */
409 xfs_buf_t **rbpp, /* in/out: summary block buffer */
410 xfs_fsblock_t *rsb, /* in/out: summary block number */
411 xfs_extlen_t prod, /* extent product factor */
412 xfs_rtblock_t *rtblock) /* out: start block allocated */
414 int any; /* any useful extents from summary */
415 xfs_rtblock_t bbno; /* bitmap block number */
416 int error; /* error value */
417 int i; /* bitmap block offset (loop control) */
418 int j; /* secondary loop control */
419 int log2len; /* log2 of minlen */
420 xfs_rtblock_t n; /* next block to try */
421 xfs_rtblock_t r; /* result block */
423 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
425 * If the block number given is off the end, silently set it to
426 * the last block.
428 if (bno >= mp->m_sb.sb_rextents)
429 bno = mp->m_sb.sb_rextents - 1;
431 * Try the exact allocation first.
433 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
434 rbpp, rsb, prod, &r);
435 if (error) {
436 return error;
439 * If the exact allocation worked, return that.
441 if (r != NULLRTBLOCK) {
442 *rtblock = r;
443 return 0;
445 bbno = XFS_BITTOBLOCK(mp, bno);
446 i = 0;
447 log2len = xfs_highbit32(minlen);
449 * Loop over all bitmap blocks (bbno + i is current block).
451 for (;;) {
453 * Get summary information of extents of all useful levels
454 * starting in this bitmap block.
456 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
457 bbno + i, rbpp, rsb, &any);
458 if (error) {
459 return error;
462 * If there are any useful extents starting here, try
463 * allocating one.
465 if (any) {
467 * On the positive side of the starting location.
469 if (i >= 0) {
471 * Try to allocate an extent starting in
472 * this block.
474 error = xfs_rtallocate_extent_block(mp, tp,
475 bbno + i, minlen, maxlen, len, &n, rbpp,
476 rsb, prod, &r);
477 if (error) {
478 return error;
481 * If it worked, return it.
483 if (r != NULLRTBLOCK) {
484 *rtblock = r;
485 return 0;
489 * On the negative side of the starting location.
491 else { /* i < 0 */
493 * Loop backwards through the bitmap blocks from
494 * the starting point-1 up to where we are now.
495 * There should be an extent which ends in this
496 * bitmap block and is long enough.
498 for (j = -1; j > i; j--) {
500 * Grab the summary information for
501 * this bitmap block.
503 error = xfs_rtany_summary(mp, tp,
504 log2len, mp->m_rsumlevels - 1,
505 bbno + j, rbpp, rsb, &any);
506 if (error) {
507 return error;
510 * If there's no extent given in the
511 * summary that means the extent we
512 * found must carry over from an
513 * earlier block. If there is an
514 * extent given, we've already tried
515 * that allocation, don't do it again.
517 if (any)
518 continue;
519 error = xfs_rtallocate_extent_block(mp,
520 tp, bbno + j, minlen, maxlen,
521 len, &n, rbpp, rsb, prod, &r);
522 if (error) {
523 return error;
526 * If it works, return the extent.
528 if (r != NULLRTBLOCK) {
529 *rtblock = r;
530 return 0;
534 * There weren't intervening bitmap blocks
535 * with a long enough extent, or the
536 * allocation didn't work for some reason
537 * (i.e. it's a little * too short).
538 * Try to allocate from the summary block
539 * that we found.
541 error = xfs_rtallocate_extent_block(mp, tp,
542 bbno + i, minlen, maxlen, len, &n, rbpp,
543 rsb, prod, &r);
544 if (error) {
545 return error;
548 * If it works, return the extent.
550 if (r != NULLRTBLOCK) {
551 *rtblock = r;
552 return 0;
557 * Loop control. If we were on the positive side, and there's
558 * still more blocks on the negative side, go there.
560 if (i > 0 && (int)bbno - i >= 0)
561 i = -i;
563 * If positive, and no more negative, but there are more
564 * positive, go there.
566 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
567 i++;
569 * If negative or 0 (just started), and there are positive
570 * blocks to go, go there. The 0 case moves to block 1.
572 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
573 i = 1 - i;
575 * If negative or 0 and there are more negative blocks,
576 * go there.
578 else if (i <= 0 && (int)bbno + i > 0)
579 i--;
581 * Must be done. Return failure.
583 else
584 break;
586 *rtblock = NULLRTBLOCK;
587 return 0;
591 * Allocate an extent of length minlen<=len<=maxlen, with no position
592 * specified. If we don't get maxlen then use prod to trim
593 * the length, if given. The lengths are all in rtextents.
595 STATIC int /* error */
596 xfs_rtallocate_extent_size(
597 xfs_mount_t *mp, /* file system mount point */
598 xfs_trans_t *tp, /* transaction pointer */
599 xfs_extlen_t minlen, /* minimum length to allocate */
600 xfs_extlen_t maxlen, /* maximum length to allocate */
601 xfs_extlen_t *len, /* out: actual length allocated */
602 xfs_buf_t **rbpp, /* in/out: summary block buffer */
603 xfs_fsblock_t *rsb, /* in/out: summary block number */
604 xfs_extlen_t prod, /* extent product factor */
605 xfs_rtblock_t *rtblock) /* out: start block allocated */
607 int error; /* error value */
608 int i; /* bitmap block number */
609 int l; /* level number (loop control) */
610 xfs_rtblock_t n; /* next block to be tried */
611 xfs_rtblock_t r; /* result block number */
612 xfs_suminfo_t sum; /* summary information for extents */
614 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
616 * Loop over all the levels starting with maxlen.
617 * At each level, look at all the bitmap blocks, to see if there
618 * are extents starting there that are long enough (>= maxlen).
619 * Note, only on the initial level can the allocation fail if
620 * the summary says there's an extent.
622 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
624 * Loop over all the bitmap blocks.
626 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
628 * Get the summary for this level/block.
630 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
631 &sum);
632 if (error) {
633 return error;
636 * Nothing there, on to the next block.
638 if (!sum)
639 continue;
641 * Try allocating the extent.
643 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
644 maxlen, len, &n, rbpp, rsb, prod, &r);
645 if (error) {
646 return error;
649 * If it worked, return that.
651 if (r != NULLRTBLOCK) {
652 *rtblock = r;
653 return 0;
656 * If the "next block to try" returned from the
657 * allocator is beyond the next bitmap block,
658 * skip to that bitmap block.
660 if (XFS_BITTOBLOCK(mp, n) > i + 1)
661 i = XFS_BITTOBLOCK(mp, n) - 1;
665 * Didn't find any maxlen blocks. Try smaller ones, unless
666 * we're asking for a fixed size extent.
668 if (minlen > --maxlen) {
669 *rtblock = NULLRTBLOCK;
670 return 0;
673 * Loop over sizes, from maxlen down to minlen.
674 * This time, when we do the allocations, allow smaller ones
675 * to succeed.
677 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
679 * Loop over all the bitmap blocks, try an allocation
680 * starting in that block.
682 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
684 * Get the summary information for this level/block.
686 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
687 &sum);
688 if (error) {
689 return error;
692 * If nothing there, go on to next.
694 if (!sum)
695 continue;
697 * Try the allocation. Make sure the specified
698 * minlen/maxlen are in the possible range for
699 * this summary level.
701 error = xfs_rtallocate_extent_block(mp, tp, i,
702 XFS_RTMAX(minlen, 1 << l),
703 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
704 len, &n, rbpp, rsb, prod, &r);
705 if (error) {
706 return error;
709 * If it worked, return that extent.
711 if (r != NULLRTBLOCK) {
712 *rtblock = r;
713 return 0;
716 * If the "next block to try" returned from the
717 * allocator is beyond the next bitmap block,
718 * skip to that bitmap block.
720 if (XFS_BITTOBLOCK(mp, n) > i + 1)
721 i = XFS_BITTOBLOCK(mp, n) - 1;
725 * Got nothing, return failure.
727 *rtblock = NULLRTBLOCK;
728 return 0;
732 * Mark an extent specified by start and len allocated.
733 * Updates all the summary information as well as the bitmap.
735 STATIC int /* error */
736 xfs_rtallocate_range(
737 xfs_mount_t *mp, /* file system mount point */
738 xfs_trans_t *tp, /* transaction pointer */
739 xfs_rtblock_t start, /* start block to allocate */
740 xfs_extlen_t len, /* length to allocate */
741 xfs_buf_t **rbpp, /* in/out: summary block buffer */
742 xfs_fsblock_t *rsb) /* in/out: summary block number */
744 xfs_rtblock_t end; /* end of the allocated extent */
745 int error; /* error value */
746 xfs_rtblock_t postblock; /* first block allocated > end */
747 xfs_rtblock_t preblock; /* first block allocated < start */
749 end = start + len - 1;
751 * Assume we're allocating out of the middle of a free extent.
752 * We need to find the beginning and end of the extent so we can
753 * properly update the summary.
755 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
756 if (error) {
757 return error;
760 * Find the next allocated block (end of free extent).
762 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
763 &postblock);
764 if (error) {
765 return error;
768 * Decrement the summary information corresponding to the entire
769 * (old) free extent.
771 error = xfs_rtmodify_summary(mp, tp,
772 XFS_RTBLOCKLOG(postblock + 1 - preblock),
773 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
774 if (error) {
775 return error;
778 * If there are blocks not being allocated at the front of the
779 * old extent, add summary data for them to be free.
781 if (preblock < start) {
782 error = xfs_rtmodify_summary(mp, tp,
783 XFS_RTBLOCKLOG(start - preblock),
784 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
785 if (error) {
786 return error;
790 * If there are blocks not being allocated at the end of the
791 * old extent, add summary data for them to be free.
793 if (postblock > end) {
794 error = xfs_rtmodify_summary(mp, tp,
795 XFS_RTBLOCKLOG(postblock - end),
796 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
797 if (error) {
798 return error;
802 * Modify the bitmap to mark this extent allocated.
804 error = xfs_rtmodify_range(mp, tp, start, len, 0);
805 return error;
809 * Return whether there are any free extents in the size range given
810 * by low and high, for the bitmap block bbno.
812 STATIC int /* error */
813 xfs_rtany_summary(
814 xfs_mount_t *mp, /* file system mount structure */
815 xfs_trans_t *tp, /* transaction pointer */
816 int low, /* low log2 extent size */
817 int high, /* high log2 extent size */
818 xfs_rtblock_t bbno, /* bitmap block number */
819 xfs_buf_t **rbpp, /* in/out: summary block buffer */
820 xfs_fsblock_t *rsb, /* in/out: summary block number */
821 int *stat) /* out: any good extents here? */
823 int error; /* error value */
824 int log; /* loop counter, log2 of ext. size */
825 xfs_suminfo_t sum; /* summary data */
828 * Loop over logs of extent sizes. Order is irrelevant.
830 for (log = low; log <= high; log++) {
832 * Get one summary datum.
834 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
835 if (error) {
836 return error;
839 * If there are any, return success.
841 if (sum) {
842 *stat = 1;
843 return 0;
847 * Found nothing, return failure.
849 *stat = 0;
850 return 0;
854 * Get a buffer for the bitmap or summary file block specified.
855 * The buffer is returned read and locked.
857 STATIC int /* error */
858 xfs_rtbuf_get(
859 xfs_mount_t *mp, /* file system mount structure */
860 xfs_trans_t *tp, /* transaction pointer */
861 xfs_rtblock_t block, /* block number in bitmap or summary */
862 int issum, /* is summary not bitmap */
863 xfs_buf_t **bpp) /* output: buffer for the block */
865 xfs_buf_t *bp; /* block buffer, result */
866 xfs_daddr_t d; /* disk addr of block */
867 int error; /* error value */
868 xfs_fsblock_t fsb; /* fs block number for block */
869 xfs_inode_t *ip; /* bitmap or summary inode */
871 ip = issum ? mp->m_rsumip : mp->m_rbmip;
873 * Map from the file offset (block) and inode number to the
874 * file system block.
876 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
877 if (error) {
878 return error;
880 ASSERT(fsb != NULLFSBLOCK);
882 * Convert to disk address for buffer cache.
884 d = XFS_FSB_TO_DADDR(mp, fsb);
886 * Read the buffer.
888 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
889 mp->m_bsize, 0, &bp);
890 if (error) {
891 return error;
893 ASSERT(bp && !XFS_BUF_GETERROR(bp));
894 *bpp = bp;
895 return 0;
898 #ifdef DEBUG
900 * Check that the given extent (block range) is allocated already.
902 STATIC int /* error */
903 xfs_rtcheck_alloc_range(
904 xfs_mount_t *mp, /* file system mount point */
905 xfs_trans_t *tp, /* transaction pointer */
906 xfs_rtblock_t bno, /* starting block number of extent */
907 xfs_extlen_t len, /* length of extent */
908 int *stat) /* out: 1 for allocated, 0 for not */
910 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
912 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
914 #endif
916 #ifdef DEBUG
918 * Check whether the given block in the bitmap has the given value.
920 STATIC int /* 1 for matches, 0 for not */
921 xfs_rtcheck_bit(
922 xfs_mount_t *mp, /* file system mount structure */
923 xfs_trans_t *tp, /* transaction pointer */
924 xfs_rtblock_t start, /* bit (block) to check */
925 int val) /* 1 for free, 0 for allocated */
927 int bit; /* bit number in the word */
928 xfs_rtblock_t block; /* bitmap block number */
929 xfs_buf_t *bp; /* buf for the block */
930 xfs_rtword_t *bufp; /* pointer into the buffer */
931 /* REFERENCED */
932 int error; /* error value */
933 xfs_rtword_t wdiff; /* difference between bit & expected */
934 int word; /* word number in the buffer */
935 xfs_rtword_t wval; /* word value from buffer */
937 block = XFS_BITTOBLOCK(mp, start);
938 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
939 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
940 word = XFS_BITTOWORD(mp, start);
941 bit = (int)(start & (XFS_NBWORD - 1));
942 wval = bufp[word];
943 xfs_trans_brelse(tp, bp);
944 wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
945 return !wdiff;
947 #endif /* DEBUG */
949 #if 0
951 * Check that the given extent (block range) is free already.
953 STATIC int /* error */
954 xfs_rtcheck_free_range(
955 xfs_mount_t *mp, /* file system mount point */
956 xfs_trans_t *tp, /* transaction pointer */
957 xfs_rtblock_t bno, /* starting block number of extent */
958 xfs_extlen_t len, /* length of extent */
959 int *stat) /* out: 1 for free, 0 for not */
961 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
963 return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
965 #endif
968 * Check that the given range is either all allocated (val = 0) or
969 * all free (val = 1).
971 STATIC int /* error */
972 xfs_rtcheck_range(
973 xfs_mount_t *mp, /* file system mount point */
974 xfs_trans_t *tp, /* transaction pointer */
975 xfs_rtblock_t start, /* starting block number of extent */
976 xfs_extlen_t len, /* length of extent */
977 int val, /* 1 for free, 0 for allocated */
978 xfs_rtblock_t *new, /* out: first block not matching */
979 int *stat) /* out: 1 for matches, 0 for not */
981 xfs_rtword_t *b; /* current word in buffer */
982 int bit; /* bit number in the word */
983 xfs_rtblock_t block; /* bitmap block number */
984 xfs_buf_t *bp; /* buf for the block */
985 xfs_rtword_t *bufp; /* starting word in buffer */
986 int error; /* error value */
987 xfs_rtblock_t i; /* current bit number rel. to start */
988 xfs_rtblock_t lastbit; /* last useful bit in word */
989 xfs_rtword_t mask; /* mask of relevant bits for value */
990 xfs_rtword_t wdiff; /* difference from wanted value */
991 int word; /* word number in the buffer */
994 * Compute starting bitmap block number
996 block = XFS_BITTOBLOCK(mp, start);
998 * Read the bitmap block.
1000 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1001 if (error) {
1002 return error;
1004 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1006 * Compute the starting word's address, and starting bit.
1008 word = XFS_BITTOWORD(mp, start);
1009 b = &bufp[word];
1010 bit = (int)(start & (XFS_NBWORD - 1));
1012 * 0 (allocated) => all zero's; 1 (free) => all one's.
1014 val = -val;
1016 * If not starting on a word boundary, deal with the first
1017 * (partial) word.
1019 if (bit) {
1021 * Compute first bit not examined.
1023 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1025 * Mask of relevant bits.
1027 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1029 * Compute difference between actual and desired value.
1031 if ((wdiff = (*b ^ val) & mask)) {
1033 * Different, compute first wrong bit and return.
1035 xfs_trans_brelse(tp, bp);
1036 i = XFS_RTLOBIT(wdiff) - bit;
1037 *new = start + i;
1038 *stat = 0;
1039 return 0;
1041 i = lastbit - bit;
1043 * Go on to next block if that's where the next word is
1044 * and we need the next word.
1046 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1048 * If done with this block, get the next one.
1050 xfs_trans_brelse(tp, bp);
1051 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1052 if (error) {
1053 return error;
1055 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1056 word = 0;
1057 } else {
1059 * Go on to the next word in the buffer.
1061 b++;
1063 } else {
1065 * Starting on a word boundary, no partial word.
1067 i = 0;
1070 * Loop over whole words in buffers. When we use up one buffer
1071 * we move on to the next one.
1073 while (len - i >= XFS_NBWORD) {
1075 * Compute difference between actual and desired value.
1077 if ((wdiff = *b ^ val)) {
1079 * Different, compute first wrong bit and return.
1081 xfs_trans_brelse(tp, bp);
1082 i += XFS_RTLOBIT(wdiff);
1083 *new = start + i;
1084 *stat = 0;
1085 return 0;
1087 i += XFS_NBWORD;
1089 * Go on to next block if that's where the next word is
1090 * and we need the next word.
1092 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1094 * If done with this block, get the next one.
1096 xfs_trans_brelse(tp, bp);
1097 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1098 if (error) {
1099 return error;
1101 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1102 word = 0;
1103 } else {
1105 * Go on to the next word in the buffer.
1107 b++;
1111 * If not ending on a word boundary, deal with the last
1112 * (partial) word.
1114 if ((lastbit = len - i)) {
1116 * Mask of relevant bits.
1118 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1120 * Compute difference between actual and desired value.
1122 if ((wdiff = (*b ^ val) & mask)) {
1124 * Different, compute first wrong bit and return.
1126 xfs_trans_brelse(tp, bp);
1127 i += XFS_RTLOBIT(wdiff);
1128 *new = start + i;
1129 *stat = 0;
1130 return 0;
1131 } else
1132 i = len;
1135 * Successful, return.
1137 xfs_trans_brelse(tp, bp);
1138 *new = start + i;
1139 *stat = 1;
1140 return 0;
1144 * Copy and transform the summary file, given the old and new
1145 * parameters in the mount structures.
1147 STATIC int /* error */
1148 xfs_rtcopy_summary(
1149 xfs_mount_t *omp, /* old file system mount point */
1150 xfs_mount_t *nmp, /* new file system mount point */
1151 xfs_trans_t *tp) /* transaction pointer */
1153 xfs_rtblock_t bbno; /* bitmap block number */
1154 xfs_buf_t *bp; /* summary buffer */
1155 int error; /* error return value */
1156 int log; /* summary level number (log length) */
1157 xfs_suminfo_t sum; /* summary data */
1158 xfs_fsblock_t sumbno; /* summary block number */
1160 bp = NULL;
1161 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1162 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1163 (xfs_srtblock_t)bbno >= 0;
1164 bbno--) {
1165 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1166 &sumbno, &sum);
1167 if (error)
1168 return error;
1169 if (sum == 0)
1170 continue;
1171 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1172 &bp, &sumbno);
1173 if (error)
1174 return error;
1175 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1176 &bp, &sumbno);
1177 if (error)
1178 return error;
1179 ASSERT(sum > 0);
1182 return 0;
1186 * Searching backward from start to limit, find the first block whose
1187 * allocated/free state is different from start's.
1189 STATIC int /* error */
1190 xfs_rtfind_back(
1191 xfs_mount_t *mp, /* file system mount point */
1192 xfs_trans_t *tp, /* transaction pointer */
1193 xfs_rtblock_t start, /* starting block to look at */
1194 xfs_rtblock_t limit, /* last block to look at */
1195 xfs_rtblock_t *rtblock) /* out: start block found */
1197 xfs_rtword_t *b; /* current word in buffer */
1198 int bit; /* bit number in the word */
1199 xfs_rtblock_t block; /* bitmap block number */
1200 xfs_buf_t *bp; /* buf for the block */
1201 xfs_rtword_t *bufp; /* starting word in buffer */
1202 int error; /* error value */
1203 xfs_rtblock_t firstbit; /* first useful bit in the word */
1204 xfs_rtblock_t i; /* current bit number rel. to start */
1205 xfs_rtblock_t len; /* length of inspected area */
1206 xfs_rtword_t mask; /* mask of relevant bits for value */
1207 xfs_rtword_t want; /* mask for "good" values */
1208 xfs_rtword_t wdiff; /* difference from wanted value */
1209 int word; /* word number in the buffer */
1212 * Compute and read in starting bitmap block for starting block.
1214 block = XFS_BITTOBLOCK(mp, start);
1215 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1216 if (error) {
1217 return error;
1219 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1221 * Get the first word's index & point to it.
1223 word = XFS_BITTOWORD(mp, start);
1224 b = &bufp[word];
1225 bit = (int)(start & (XFS_NBWORD - 1));
1226 len = start - limit + 1;
1228 * Compute match value, based on the bit at start: if 1 (free)
1229 * then all-ones, else all-zeroes.
1231 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1233 * If the starting position is not word-aligned, deal with the
1234 * partial word.
1236 if (bit < XFS_NBWORD - 1) {
1238 * Calculate first (leftmost) bit number to look at,
1239 * and mask for all the relevant bits in this word.
1241 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1242 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1243 firstbit;
1245 * Calculate the difference between the value there
1246 * and what we're looking for.
1248 if ((wdiff = (*b ^ want) & mask)) {
1250 * Different. Mark where we are and return.
1252 xfs_trans_brelse(tp, bp);
1253 i = bit - XFS_RTHIBIT(wdiff);
1254 *rtblock = start - i + 1;
1255 return 0;
1257 i = bit - firstbit + 1;
1259 * Go on to previous block if that's where the previous word is
1260 * and we need the previous word.
1262 if (--word == -1 && i < len) {
1264 * If done with this block, get the previous one.
1266 xfs_trans_brelse(tp, bp);
1267 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1268 if (error) {
1269 return error;
1271 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1272 word = XFS_BLOCKWMASK(mp);
1273 b = &bufp[word];
1274 } else {
1276 * Go on to the previous word in the buffer.
1278 b--;
1280 } else {
1282 * Starting on a word boundary, no partial word.
1284 i = 0;
1287 * Loop over whole words in buffers. When we use up one buffer
1288 * we move on to the previous one.
1290 while (len - i >= XFS_NBWORD) {
1292 * Compute difference between actual and desired value.
1294 if ((wdiff = *b ^ want)) {
1296 * Different, mark where we are and return.
1298 xfs_trans_brelse(tp, bp);
1299 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1300 *rtblock = start - i + 1;
1301 return 0;
1303 i += XFS_NBWORD;
1305 * Go on to previous block if that's where the previous word is
1306 * and we need the previous word.
1308 if (--word == -1 && i < len) {
1310 * If done with this block, get the previous one.
1312 xfs_trans_brelse(tp, bp);
1313 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1314 if (error) {
1315 return error;
1317 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1318 word = XFS_BLOCKWMASK(mp);
1319 b = &bufp[word];
1320 } else {
1322 * Go on to the previous word in the buffer.
1324 b--;
1328 * If not ending on a word boundary, deal with the last
1329 * (partial) word.
1331 if (len - i) {
1333 * Calculate first (leftmost) bit number to look at,
1334 * and mask for all the relevant bits in this word.
1336 firstbit = XFS_NBWORD - (len - i);
1337 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1339 * Compute difference between actual and desired value.
1341 if ((wdiff = (*b ^ want) & mask)) {
1343 * Different, mark where we are and return.
1345 xfs_trans_brelse(tp, bp);
1346 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1347 *rtblock = start - i + 1;
1348 return 0;
1349 } else
1350 i = len;
1353 * No match, return that we scanned the whole area.
1355 xfs_trans_brelse(tp, bp);
1356 *rtblock = start - i + 1;
1357 return 0;
1361 * Searching forward from start to limit, find the first block whose
1362 * allocated/free state is different from start's.
1364 STATIC int /* error */
1365 xfs_rtfind_forw(
1366 xfs_mount_t *mp, /* file system mount point */
1367 xfs_trans_t *tp, /* transaction pointer */
1368 xfs_rtblock_t start, /* starting block to look at */
1369 xfs_rtblock_t limit, /* last block to look at */
1370 xfs_rtblock_t *rtblock) /* out: start block found */
1372 xfs_rtword_t *b; /* current word in buffer */
1373 int bit; /* bit number in the word */
1374 xfs_rtblock_t block; /* bitmap block number */
1375 xfs_buf_t *bp; /* buf for the block */
1376 xfs_rtword_t *bufp; /* starting word in buffer */
1377 int error; /* error value */
1378 xfs_rtblock_t i; /* current bit number rel. to start */
1379 xfs_rtblock_t lastbit; /* last useful bit in the word */
1380 xfs_rtblock_t len; /* length of inspected area */
1381 xfs_rtword_t mask; /* mask of relevant bits for value */
1382 xfs_rtword_t want; /* mask for "good" values */
1383 xfs_rtword_t wdiff; /* difference from wanted value */
1384 int word; /* word number in the buffer */
1387 * Compute and read in starting bitmap block for starting block.
1389 block = XFS_BITTOBLOCK(mp, start);
1390 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1391 if (error) {
1392 return error;
1394 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1396 * Get the first word's index & point to it.
1398 word = XFS_BITTOWORD(mp, start);
1399 b = &bufp[word];
1400 bit = (int)(start & (XFS_NBWORD - 1));
1401 len = limit - start + 1;
1403 * Compute match value, based on the bit at start: if 1 (free)
1404 * then all-ones, else all-zeroes.
1406 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1408 * If the starting position is not word-aligned, deal with the
1409 * partial word.
1411 if (bit) {
1413 * Calculate last (rightmost) bit number to look at,
1414 * and mask for all the relevant bits in this word.
1416 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1417 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1419 * Calculate the difference between the value there
1420 * and what we're looking for.
1422 if ((wdiff = (*b ^ want) & mask)) {
1424 * Different. Mark where we are and return.
1426 xfs_trans_brelse(tp, bp);
1427 i = XFS_RTLOBIT(wdiff) - bit;
1428 *rtblock = start + i - 1;
1429 return 0;
1431 i = lastbit - bit;
1433 * Go on to next block if that's where the next word is
1434 * and we need the next word.
1436 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1438 * If done with this block, get the previous one.
1440 xfs_trans_brelse(tp, bp);
1441 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1442 if (error) {
1443 return error;
1445 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1446 word = 0;
1447 } else {
1449 * Go on to the previous word in the buffer.
1451 b++;
1453 } else {
1455 * Starting on a word boundary, no partial word.
1457 i = 0;
1460 * Loop over whole words in buffers. When we use up one buffer
1461 * we move on to the next one.
1463 while (len - i >= XFS_NBWORD) {
1465 * Compute difference between actual and desired value.
1467 if ((wdiff = *b ^ want)) {
1469 * Different, mark where we are and return.
1471 xfs_trans_brelse(tp, bp);
1472 i += XFS_RTLOBIT(wdiff);
1473 *rtblock = start + i - 1;
1474 return 0;
1476 i += XFS_NBWORD;
1478 * Go on to next block if that's where the next word is
1479 * and we need the next word.
1481 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1483 * If done with this block, get the next one.
1485 xfs_trans_brelse(tp, bp);
1486 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1487 if (error) {
1488 return error;
1490 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1491 word = 0;
1492 } else {
1494 * Go on to the next word in the buffer.
1496 b++;
1500 * If not ending on a word boundary, deal with the last
1501 * (partial) word.
1503 if ((lastbit = len - i)) {
1505 * Calculate mask for all the relevant bits in this word.
1507 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1509 * Compute difference between actual and desired value.
1511 if ((wdiff = (*b ^ want) & mask)) {
1513 * Different, mark where we are and return.
1515 xfs_trans_brelse(tp, bp);
1516 i += XFS_RTLOBIT(wdiff);
1517 *rtblock = start + i - 1;
1518 return 0;
1519 } else
1520 i = len;
1523 * No match, return that we scanned the whole area.
1525 xfs_trans_brelse(tp, bp);
1526 *rtblock = start + i - 1;
1527 return 0;
1531 * Mark an extent specified by start and len freed.
1532 * Updates all the summary information as well as the bitmap.
1534 STATIC int /* error */
1535 xfs_rtfree_range(
1536 xfs_mount_t *mp, /* file system mount point */
1537 xfs_trans_t *tp, /* transaction pointer */
1538 xfs_rtblock_t start, /* starting block to free */
1539 xfs_extlen_t len, /* length to free */
1540 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1541 xfs_fsblock_t *rsb) /* in/out: summary block number */
1543 xfs_rtblock_t end; /* end of the freed extent */
1544 int error; /* error value */
1545 xfs_rtblock_t postblock; /* first block freed > end */
1546 xfs_rtblock_t preblock; /* first block freed < start */
1548 end = start + len - 1;
1550 * Modify the bitmap to mark this extent freed.
1552 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1553 if (error) {
1554 return error;
1557 * Assume we're freeing out of the middle of an allocated extent.
1558 * We need to find the beginning and end of the extent so we can
1559 * properly update the summary.
1561 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1562 if (error) {
1563 return error;
1566 * Find the next allocated block (end of allocated extent).
1568 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1569 &postblock);
1571 * If there are blocks not being freed at the front of the
1572 * old extent, add summary data for them to be allocated.
1574 if (preblock < start) {
1575 error = xfs_rtmodify_summary(mp, tp,
1576 XFS_RTBLOCKLOG(start - preblock),
1577 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1578 if (error) {
1579 return error;
1583 * If there are blocks not being freed at the end of the
1584 * old extent, add summary data for them to be allocated.
1586 if (postblock > end) {
1587 error = xfs_rtmodify_summary(mp, tp,
1588 XFS_RTBLOCKLOG(postblock - end),
1589 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1590 if (error) {
1591 return error;
1595 * Increment the summary information corresponding to the entire
1596 * (new) free extent.
1598 error = xfs_rtmodify_summary(mp, tp,
1599 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1600 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1601 return error;
1605 * Read and return the summary information for a given extent size,
1606 * bitmap block combination.
1607 * Keeps track of a current summary block, so we don't keep reading
1608 * it from the buffer cache.
1610 STATIC int /* error */
1611 xfs_rtget_summary(
1612 xfs_mount_t *mp, /* file system mount structure */
1613 xfs_trans_t *tp, /* transaction pointer */
1614 int log, /* log2 of extent size */
1615 xfs_rtblock_t bbno, /* bitmap block number */
1616 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1617 xfs_fsblock_t *rsb, /* in/out: summary block number */
1618 xfs_suminfo_t *sum) /* out: summary info for this block */
1620 xfs_buf_t *bp; /* buffer for summary block */
1621 int error; /* error value */
1622 xfs_fsblock_t sb; /* summary fsblock */
1623 int so; /* index into the summary file */
1624 xfs_suminfo_t *sp; /* pointer to returned data */
1627 * Compute entry number in the summary file.
1629 so = XFS_SUMOFFS(mp, log, bbno);
1631 * Compute the block number in the summary file.
1633 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1635 * If we have an old buffer, and the block number matches, use that.
1637 if (rbpp && *rbpp && *rsb == sb)
1638 bp = *rbpp;
1640 * Otherwise we have to get the buffer.
1642 else {
1644 * If there was an old one, get rid of it first.
1646 if (rbpp && *rbpp)
1647 xfs_trans_brelse(tp, *rbpp);
1648 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1649 if (error) {
1650 return error;
1653 * Remember this buffer and block for the next call.
1655 if (rbpp) {
1656 *rbpp = bp;
1657 *rsb = sb;
1661 * Point to the summary information & copy it out.
1663 sp = XFS_SUMPTR(mp, bp, so);
1664 *sum = *sp;
1666 * Drop the buffer if we're not asked to remember it.
1668 if (!rbpp)
1669 xfs_trans_brelse(tp, bp);
1670 return 0;
1674 * Set the given range of bitmap bits to the given value.
1675 * Do whatever I/O and logging is required.
1677 STATIC int /* error */
1678 xfs_rtmodify_range(
1679 xfs_mount_t *mp, /* file system mount point */
1680 xfs_trans_t *tp, /* transaction pointer */
1681 xfs_rtblock_t start, /* starting block to modify */
1682 xfs_extlen_t len, /* length of extent to modify */
1683 int val) /* 1 for free, 0 for allocated */
1685 xfs_rtword_t *b; /* current word in buffer */
1686 int bit; /* bit number in the word */
1687 xfs_rtblock_t block; /* bitmap block number */
1688 xfs_buf_t *bp; /* buf for the block */
1689 xfs_rtword_t *bufp; /* starting word in buffer */
1690 int error; /* error value */
1691 xfs_rtword_t *first; /* first used word in the buffer */
1692 int i; /* current bit number rel. to start */
1693 int lastbit; /* last useful bit in word */
1694 xfs_rtword_t mask; /* mask o frelevant bits for value */
1695 int word; /* word number in the buffer */
1698 * Compute starting bitmap block number.
1700 block = XFS_BITTOBLOCK(mp, start);
1702 * Read the bitmap block, and point to its data.
1704 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1705 if (error) {
1706 return error;
1708 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1710 * Compute the starting word's address, and starting bit.
1712 word = XFS_BITTOWORD(mp, start);
1713 first = b = &bufp[word];
1714 bit = (int)(start & (XFS_NBWORD - 1));
1716 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1718 val = -val;
1720 * If not starting on a word boundary, deal with the first
1721 * (partial) word.
1723 if (bit) {
1725 * Compute first bit not changed and mask of relevant bits.
1727 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1728 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1730 * Set/clear the active bits.
1732 if (val)
1733 *b |= mask;
1734 else
1735 *b &= ~mask;
1736 i = lastbit - bit;
1738 * Go on to the next block if that's where the next word is
1739 * and we need the next word.
1741 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1743 * Log the changed part of this block.
1744 * Get the next one.
1746 xfs_trans_log_buf(tp, bp,
1747 (uint)((char *)first - (char *)bufp),
1748 (uint)((char *)b - (char *)bufp));
1749 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1750 if (error) {
1751 return error;
1753 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1754 word = 0;
1755 } else {
1757 * Go on to the next word in the buffer
1759 b++;
1761 } else {
1763 * Starting on a word boundary, no partial word.
1765 i = 0;
1768 * Loop over whole words in buffers. When we use up one buffer
1769 * we move on to the next one.
1771 while (len - i >= XFS_NBWORD) {
1773 * Set the word value correctly.
1775 *b = val;
1776 i += XFS_NBWORD;
1778 * Go on to the next block if that's where the next word is
1779 * and we need the next word.
1781 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1783 * Log the changed part of this block.
1784 * Get the next one.
1786 xfs_trans_log_buf(tp, bp,
1787 (uint)((char *)first - (char *)bufp),
1788 (uint)((char *)b - (char *)bufp));
1789 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1790 if (error) {
1791 return error;
1793 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1794 word = 0;
1795 } else {
1797 * Go on to the next word in the buffer
1799 b++;
1803 * If not ending on a word boundary, deal with the last
1804 * (partial) word.
1806 if ((lastbit = len - i)) {
1808 * Compute a mask of relevant bits.
1810 bit = 0;
1811 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1813 * Set/clear the active bits.
1815 if (val)
1816 *b |= mask;
1817 else
1818 *b &= ~mask;
1819 b++;
1822 * Log any remaining changed bytes.
1824 if (b > first)
1825 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1826 (uint)((char *)b - (char *)bufp - 1));
1827 return 0;
1831 * Read and modify the summary information for a given extent size,
1832 * bitmap block combination.
1833 * Keeps track of a current summary block, so we don't keep reading
1834 * it from the buffer cache.
1836 STATIC int /* error */
1837 xfs_rtmodify_summary(
1838 xfs_mount_t *mp, /* file system mount point */
1839 xfs_trans_t *tp, /* transaction pointer */
1840 int log, /* log2 of extent size */
1841 xfs_rtblock_t bbno, /* bitmap block number */
1842 int delta, /* change to make to summary info */
1843 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1844 xfs_fsblock_t *rsb) /* in/out: summary block number */
1846 xfs_buf_t *bp; /* buffer for the summary block */
1847 int error; /* error value */
1848 xfs_fsblock_t sb; /* summary fsblock */
1849 int so; /* index into the summary file */
1850 xfs_suminfo_t *sp; /* pointer to returned data */
1853 * Compute entry number in the summary file.
1855 so = XFS_SUMOFFS(mp, log, bbno);
1857 * Compute the block number in the summary file.
1859 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1861 * If we have an old buffer, and the block number matches, use that.
1863 if (rbpp && *rbpp && *rsb == sb)
1864 bp = *rbpp;
1866 * Otherwise we have to get the buffer.
1868 else {
1870 * If there was an old one, get rid of it first.
1872 if (rbpp && *rbpp)
1873 xfs_trans_brelse(tp, *rbpp);
1874 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1875 if (error) {
1876 return error;
1879 * Remember this buffer and block for the next call.
1881 if (rbpp) {
1882 *rbpp = bp;
1883 *rsb = sb;
1887 * Point to the summary information, modify and log it.
1889 sp = XFS_SUMPTR(mp, bp, so);
1890 *sp += delta;
1891 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1892 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1893 return 0;
1897 * Visible (exported) functions.
1901 * Grow the realtime area of the filesystem.
1904 xfs_growfs_rt(
1905 xfs_mount_t *mp, /* mount point for filesystem */
1906 xfs_growfs_rt_t *in) /* growfs rt input struct */
1908 xfs_rtblock_t bmbno; /* bitmap block number */
1909 xfs_buf_t *bp; /* temporary buffer */
1910 int cancelflags; /* flags for xfs_trans_cancel */
1911 int error; /* error return value */
1912 xfs_inode_t *ip; /* bitmap inode, used as lock */
1913 xfs_mount_t *nmp; /* new (fake) mount structure */
1914 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1915 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1916 xfs_drtbno_t nrextents; /* new number of realtime extents */
1917 uint8_t nrextslog; /* new log2 of sb_rextents */
1918 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1919 uint nrsumlevels; /* new rt summary levels */
1920 uint nrsumsize; /* new size of rt summary, bytes */
1921 xfs_sb_t *nsbp; /* new superblock */
1922 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1923 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1924 xfs_sb_t *sbp; /* old superblock */
1925 xfs_fsblock_t sumbno; /* summary block number */
1926 xfs_trans_t *tp; /* transaction pointer */
1928 sbp = &mp->m_sb;
1930 * Initial error checking.
1932 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1933 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1934 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1935 return XFS_ERROR(EINVAL);
1937 * Read in the last block of the device, make sure it exists.
1939 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1940 XFS_FSB_TO_BB(mp, in->newblocks - 1),
1941 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1942 if (error)
1943 return error;
1944 ASSERT(bp);
1945 xfs_buf_relse(bp);
1947 * Calculate new parameters. These are the final values to be reached.
1949 nrextents = nrblocks;
1950 do_div(nrextents, in->extsize);
1951 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1952 nrextslog = xfs_highbit32(nrextents);
1953 nrsumlevels = nrextslog + 1;
1954 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1955 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1956 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1958 * New summary size can't be more than half the size of
1959 * the log. This prevents us from getting a log overflow,
1960 * since we'll log basically the whole summary file at once.
1962 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1963 return XFS_ERROR(EINVAL);
1965 * Get the old block counts for bitmap and summary inodes.
1966 * These can't change since other growfs callers are locked out.
1968 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1969 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1971 * Allocate space to the bitmap and summary files, as necessary.
1973 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1974 mp->m_sb.sb_rbmino)))
1975 return error;
1976 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1977 mp->m_sb.sb_rsumino)))
1978 return error;
1980 * Allocate a new (fake) mount/sb.
1982 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1984 * Loop over the bitmap blocks.
1985 * We will do everything one bitmap block at a time.
1986 * Skip the current block if it is exactly full.
1987 * This also deals with the case where there were no rtextents before.
1989 for (bmbno = sbp->sb_rbmblocks -
1990 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1991 bmbno < nrbmblocks;
1992 bmbno++) {
1993 *nmp = *mp;
1994 nsbp = &nmp->m_sb;
1996 * Calculate new sb and mount fields for this round.
1998 nsbp->sb_rextsize = in->extsize;
1999 nsbp->sb_rbmblocks = bmbno + 1;
2000 nsbp->sb_rblocks =
2001 XFS_RTMIN(nrblocks,
2002 nsbp->sb_rbmblocks * NBBY *
2003 nsbp->sb_blocksize * nsbp->sb_rextsize);
2004 nsbp->sb_rextents = nsbp->sb_rblocks;
2005 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
2006 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2007 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2008 nrsumsize =
2009 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2010 nsbp->sb_rbmblocks;
2011 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2012 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2014 * Start a transaction, get the log reservation.
2016 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2017 cancelflags = 0;
2018 if ((error = xfs_trans_reserve(tp, 0,
2019 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2020 break;
2022 * Lock out other callers by grabbing the bitmap inode lock.
2024 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2025 XFS_ILOCK_EXCL, &ip)))
2026 break;
2027 ASSERT(ip == mp->m_rbmip);
2029 * Update the bitmap inode's size.
2031 mp->m_rbmip->i_d.di_size =
2032 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2033 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2034 cancelflags |= XFS_TRANS_ABORT;
2036 * Get the summary inode into the transaction.
2038 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
2039 XFS_ILOCK_EXCL, &ip)))
2040 break;
2041 ASSERT(ip == mp->m_rsumip);
2043 * Update the summary inode's size.
2045 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2046 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2048 * Copy summary data from old to new sizes.
2049 * Do this when the real size (not block-aligned) changes.
2051 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2052 mp->m_rsumlevels != nmp->m_rsumlevels) {
2053 error = xfs_rtcopy_summary(mp, nmp, tp);
2054 if (error)
2055 break;
2058 * Update superblock fields.
2060 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2061 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2062 nsbp->sb_rextsize - sbp->sb_rextsize);
2063 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2064 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2065 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2066 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2067 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2068 nsbp->sb_rblocks - sbp->sb_rblocks);
2069 if (nsbp->sb_rextents != sbp->sb_rextents)
2070 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2071 nsbp->sb_rextents - sbp->sb_rextents);
2072 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2073 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2074 nsbp->sb_rextslog - sbp->sb_rextslog);
2076 * Free new extent.
2078 bp = NULL;
2079 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2080 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2081 if (error)
2082 break;
2084 * Mark more blocks free in the superblock.
2086 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2087 nsbp->sb_rextents - sbp->sb_rextents);
2089 * Update mp values into the real mp structure.
2091 mp->m_rsumlevels = nrsumlevels;
2092 mp->m_rsumsize = nrsumsize;
2094 * Commit the transaction.
2096 xfs_trans_commit(tp, 0, NULL);
2099 if (error)
2100 xfs_trans_cancel(tp, cancelflags);
2103 * Free the fake mp structure.
2105 kmem_free(nmp, sizeof(*nmp));
2107 return error;
2111 * Allocate an extent in the realtime subvolume, with the usual allocation
2112 * parameters. The length units are all in realtime extents, as is the
2113 * result block number.
2115 int /* error */
2116 xfs_rtallocate_extent(
2117 xfs_trans_t *tp, /* transaction pointer */
2118 xfs_rtblock_t bno, /* starting block number to allocate */
2119 xfs_extlen_t minlen, /* minimum length to allocate */
2120 xfs_extlen_t maxlen, /* maximum length to allocate */
2121 xfs_extlen_t *len, /* out: actual length allocated */
2122 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2123 int wasdel, /* was a delayed allocation extent */
2124 xfs_extlen_t prod, /* extent product factor */
2125 xfs_rtblock_t *rtblock) /* out: start block allocated */
2127 int error; /* error value */
2128 xfs_inode_t *ip; /* inode for bitmap file */
2129 xfs_mount_t *mp; /* file system mount structure */
2130 xfs_rtblock_t r; /* result allocated block */
2131 xfs_fsblock_t sb; /* summary file block number */
2132 xfs_buf_t *sumbp; /* summary file block buffer */
2134 ASSERT(minlen > 0 && minlen <= maxlen);
2135 mp = tp->t_mountp;
2137 * If prod is set then figure out what to do to minlen and maxlen.
2139 if (prod > 1) {
2140 xfs_extlen_t i;
2142 if ((i = maxlen % prod))
2143 maxlen -= i;
2144 if ((i = minlen % prod))
2145 minlen += prod - i;
2146 if (maxlen < minlen) {
2147 *rtblock = NULLRTBLOCK;
2148 return 0;
2152 * Lock out other callers by grabbing the bitmap inode lock.
2154 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2155 XFS_ILOCK_EXCL, &ip)))
2156 return error;
2157 sumbp = NULL;
2159 * Allocate by size, or near another block, or exactly at some block.
2161 switch (type) {
2162 case XFS_ALLOCTYPE_ANY_AG:
2163 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2164 &sumbp, &sb, prod, &r);
2165 break;
2166 case XFS_ALLOCTYPE_NEAR_BNO:
2167 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2168 len, &sumbp, &sb, prod, &r);
2169 break;
2170 case XFS_ALLOCTYPE_THIS_BNO:
2171 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2172 len, &sumbp, &sb, prod, &r);
2173 break;
2174 default:
2175 ASSERT(0);
2177 if (error) {
2178 return error;
2181 * If it worked, update the superblock.
2183 if (r != NULLRTBLOCK) {
2184 long slen = (long)*len;
2186 ASSERT(*len >= minlen && *len <= maxlen);
2187 if (wasdel)
2188 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2189 else
2190 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2192 *rtblock = r;
2193 return 0;
2197 * Free an extent in the realtime subvolume. Length is expressed in
2198 * realtime extents, as is the block number.
2200 int /* error */
2201 xfs_rtfree_extent(
2202 xfs_trans_t *tp, /* transaction pointer */
2203 xfs_rtblock_t bno, /* starting block number to free */
2204 xfs_extlen_t len) /* length of extent freed */
2206 int error; /* error value */
2207 xfs_inode_t *ip; /* bitmap file inode */
2208 xfs_mount_t *mp; /* file system mount structure */
2209 xfs_fsblock_t sb; /* summary file block number */
2210 xfs_buf_t *sumbp; /* summary file block buffer */
2212 mp = tp->t_mountp;
2214 * Synchronize by locking the bitmap inode.
2216 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2217 XFS_ILOCK_EXCL, &ip)))
2218 return error;
2219 #if defined(__KERNEL__) && defined(DEBUG)
2221 * Check to see that this whole range is currently allocated.
2224 int stat; /* result from checking range */
2226 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2227 if (error) {
2228 return error;
2230 ASSERT(stat);
2232 #endif
2233 sumbp = NULL;
2235 * Free the range of realtime blocks.
2237 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2238 if (error) {
2239 return error;
2242 * Mark more blocks free in the superblock.
2244 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2246 * If we've now freed all the blocks, reset the file sequence
2247 * number to 0.
2249 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2250 mp->m_sb.sb_rextents) {
2251 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2252 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2253 *(__uint64_t *)&ip->i_d.di_atime = 0;
2254 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2256 return 0;
2260 * Initialize realtime fields in the mount structure.
2262 int /* error */
2263 xfs_rtmount_init(
2264 xfs_mount_t *mp) /* file system mount structure */
2266 xfs_buf_t *bp; /* buffer for last block of subvolume */
2267 xfs_daddr_t d; /* address of last block of subvolume */
2268 int error; /* error return value */
2269 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2271 sbp = &mp->m_sb;
2272 if (sbp->sb_rblocks == 0)
2273 return 0;
2274 if (mp->m_rtdev_targp == NULL) {
2275 cmn_err(CE_WARN,
2276 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2277 return XFS_ERROR(ENODEV);
2279 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2280 mp->m_rsumsize =
2281 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2282 sbp->sb_rbmblocks;
2283 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2284 mp->m_rbmip = mp->m_rsumip = NULL;
2286 * Check that the realtime section is an ok size.
2288 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2289 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2290 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2291 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2292 (unsigned long long) mp->m_sb.sb_rblocks);
2293 return XFS_ERROR(E2BIG);
2295 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2296 d - XFS_FSB_TO_BB(mp, 1),
2297 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2298 if (error) {
2299 cmn_err(CE_WARN,
2300 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2301 if (error == ENOSPC)
2302 return XFS_ERROR(E2BIG);
2303 return error;
2305 xfs_buf_relse(bp);
2306 return 0;
2310 * Get the bitmap and summary inodes into the mount structure
2311 * at mount time.
2313 int /* error */
2314 xfs_rtmount_inodes(
2315 xfs_mount_t *mp) /* file system mount structure */
2317 int error; /* error return value */
2318 xfs_sb_t *sbp;
2320 sbp = &mp->m_sb;
2321 if (sbp->sb_rbmino == NULLFSINO)
2322 return 0;
2323 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2324 if (error)
2325 return error;
2326 ASSERT(mp->m_rbmip != NULL);
2327 ASSERT(sbp->sb_rsumino != NULLFSINO);
2328 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2329 if (error) {
2330 VN_RELE(XFS_ITOV(mp->m_rbmip));
2331 return error;
2333 ASSERT(mp->m_rsumip != NULL);
2334 return 0;
2338 * Pick an extent for allocation at the start of a new realtime file.
2339 * Use the sequence number stored in the atime field of the bitmap inode.
2340 * Translate this to a fraction of the rtextents, and return the product
2341 * of rtextents and the fraction.
2342 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2344 int /* error */
2345 xfs_rtpick_extent(
2346 xfs_mount_t *mp, /* file system mount point */
2347 xfs_trans_t *tp, /* transaction pointer */
2348 xfs_extlen_t len, /* allocation length (rtextents) */
2349 xfs_rtblock_t *pick) /* result rt extent */
2351 xfs_rtblock_t b; /* result block */
2352 int error; /* error return value */
2353 xfs_inode_t *ip; /* bitmap incore inode */
2354 int log2; /* log of sequence number */
2355 __uint64_t resid; /* residual after log removed */
2356 __uint64_t seq; /* sequence number of file creation */
2357 __uint64_t *seqp; /* pointer to seqno in inode */
2359 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2360 XFS_ILOCK_EXCL, &ip)))
2361 return error;
2362 ASSERT(ip == mp->m_rbmip);
2363 seqp = (__uint64_t *)&ip->i_d.di_atime;
2364 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2365 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2366 *seqp = 0;
2368 seq = *seqp;
2369 if ((log2 = xfs_highbit64(seq)) == -1)
2370 b = 0;
2371 else {
2372 resid = seq - (1ULL << log2);
2373 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2374 (log2 + 1);
2375 if (b >= mp->m_sb.sb_rextents)
2376 b = do_mod(b, mp->m_sb.sb_rextents);
2377 if (b + len > mp->m_sb.sb_rextents)
2378 b = mp->m_sb.sb_rextents - len;
2380 *seqp = seq + 1;
2381 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2382 *pick = b;
2383 return 0;
2386 #ifdef DEBUG
2388 * Debug code: print out the value of a range in the bitmap.
2390 void
2391 xfs_rtprint_range(
2392 xfs_mount_t *mp, /* file system mount structure */
2393 xfs_trans_t *tp, /* transaction pointer */
2394 xfs_rtblock_t start, /* starting block to print */
2395 xfs_extlen_t len) /* length to print */
2397 xfs_extlen_t i; /* block number in the extent */
2399 cmn_err(CE_DEBUG, "%Ld: ", (long long)start);
2400 for (i = 0; i < len; i++)
2401 cmn_err(CE_DEBUG, "%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2402 cmn_err(CE_DEBUG, "\n");
2406 * Debug code: print the summary file.
2408 void
2409 xfs_rtprint_summary(
2410 xfs_mount_t *mp, /* file system mount structure */
2411 xfs_trans_t *tp) /* transaction pointer */
2413 xfs_suminfo_t c; /* summary data */
2414 xfs_rtblock_t i; /* bitmap block number */
2415 int l; /* summary information level */
2416 int p; /* flag for printed anything */
2417 xfs_fsblock_t sb; /* summary block number */
2418 xfs_buf_t *sumbp; /* summary block buffer */
2420 sumbp = NULL;
2421 for (l = 0; l < mp->m_rsumlevels; l++) {
2422 for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2423 (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2424 if (c) {
2425 if (!p) {
2426 cmn_err(CE_DEBUG, "%Ld-%Ld:", 1LL << l,
2427 XFS_RTMIN((1LL << l) +
2428 ((1LL << l) - 1LL),
2429 mp->m_sb.sb_rextents));
2430 p = 1;
2432 cmn_err(CE_DEBUG, " %Ld:%d", (long long)i, c);
2435 if (p)
2436 cmn_err(CE_DEBUG, "\n");
2438 if (sumbp)
2439 xfs_trans_brelse(tp, sumbp);
2441 #endif /* DEBUG */