pid: fix mips irix emulation pid usage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_rtalloc.c
blobca83ddf72af4569ab8cfa1b3cdfcbbf16c80ffb6
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 * Allocate space to the bitmap or summary file, and zero it, for growfs.
78 STATIC int /* error */
79 xfs_growfs_rt_alloc(
80 xfs_mount_t *mp, /* file system mount point */
81 xfs_extlen_t oblocks, /* old count of blocks */
82 xfs_extlen_t nblocks, /* new count of blocks */
83 xfs_ino_t ino) /* inode number (bitmap/summary) */
85 xfs_fileoff_t bno; /* block number in file */
86 xfs_buf_t *bp; /* temporary buffer for zeroing */
87 int cancelflags; /* flags for xfs_trans_cancel */
88 int committed; /* transaction committed flag */
89 xfs_daddr_t d; /* disk block address */
90 int error; /* error return value */
91 xfs_fsblock_t firstblock; /* first block allocated in xaction */
92 xfs_bmap_free_t flist; /* list of freed blocks */
93 xfs_fsblock_t fsbno; /* filesystem block for bno */
94 xfs_inode_t *ip; /* pointer to incore inode */
95 xfs_bmbt_irec_t map; /* block map output */
96 int nmap; /* number of block maps */
97 int resblks; /* space reservation */
98 xfs_trans_t *tp; /* transaction pointer */
101 * Allocate space to the file, as necessary.
103 while (oblocks < nblocks) {
104 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
105 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
106 cancelflags = 0;
108 * Reserve space & log for one extent added to the file.
110 if ((error = xfs_trans_reserve(tp, resblks,
111 XFS_GROWRTALLOC_LOG_RES(mp), 0,
112 XFS_TRANS_PERM_LOG_RES,
113 XFS_DEFAULT_PERM_LOG_COUNT)))
114 goto error_exit;
115 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
117 * Lock the inode.
119 if ((error = xfs_trans_iget(mp, tp, ino, 0,
120 XFS_ILOCK_EXCL, &ip)))
121 goto error_exit;
122 XFS_BMAP_INIT(&flist, &firstblock);
124 * Allocate blocks to the bitmap file.
126 nmap = 1;
127 cancelflags |= XFS_TRANS_ABORT;
128 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
129 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
130 resblks, &map, &nmap, &flist, NULL);
131 if (!error && nmap < 1)
132 error = XFS_ERROR(ENOSPC);
133 if (error)
134 goto error_exit;
136 * Free any blocks freed up in the transaction, then commit.
138 error = xfs_bmap_finish(&tp, &flist, &committed);
139 if (error)
140 goto error_exit;
141 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
143 * Now we need to clear the allocated blocks.
144 * Do this one block per transaction, to keep it simple.
146 cancelflags = 0;
147 for (bno = map.br_startoff, fsbno = map.br_startblock;
148 bno < map.br_startoff + map.br_blockcount;
149 bno++, fsbno++) {
150 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
152 * Reserve log for one block zeroing.
154 if ((error = xfs_trans_reserve(tp, 0,
155 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
156 goto error_exit;
158 * Lock the bitmap inode.
160 if ((error = xfs_trans_iget(mp, tp, ino, 0,
161 XFS_ILOCK_EXCL, &ip)))
162 goto error_exit;
164 * Get a buffer for the block.
166 d = XFS_FSB_TO_DADDR(mp, fsbno);
167 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
168 mp->m_bsize, 0);
169 if (bp == NULL) {
170 error = XFS_ERROR(EIO);
171 goto error_exit;
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 xfs_trans_commit(tp, 0);
181 * Go on to the next extent, if any.
183 oblocks = map.br_startoff + map.br_blockcount;
185 return 0;
186 error_exit:
187 xfs_trans_cancel(tp, cancelflags);
188 return error;
192 * Attempt to allocate an extent minlen<=len<=maxlen starting from
193 * bitmap block bbno. If we don't get maxlen then use prod to trim
194 * the length, if given. Returns error; returns starting block in *rtblock.
195 * The lengths are all in rtextents.
197 STATIC int /* error */
198 xfs_rtallocate_extent_block(
199 xfs_mount_t *mp, /* file system mount point */
200 xfs_trans_t *tp, /* transaction pointer */
201 xfs_rtblock_t bbno, /* bitmap block number */
202 xfs_extlen_t minlen, /* minimum length to allocate */
203 xfs_extlen_t maxlen, /* maximum length to allocate */
204 xfs_extlen_t *len, /* out: actual length allocated */
205 xfs_rtblock_t *nextp, /* out: next block to try */
206 xfs_buf_t **rbpp, /* in/out: summary block buffer */
207 xfs_fsblock_t *rsb, /* in/out: summary block number */
208 xfs_extlen_t prod, /* extent product factor */
209 xfs_rtblock_t *rtblock) /* out: start block allocated */
211 xfs_rtblock_t besti; /* best rtblock found so far */
212 xfs_rtblock_t bestlen; /* best length found so far */
213 xfs_rtblock_t end; /* last rtblock in chunk */
214 int error; /* error value */
215 xfs_rtblock_t i; /* current rtblock trying */
216 xfs_rtblock_t next; /* next rtblock to try */
217 int stat; /* status from internal calls */
220 * Loop over all the extents starting in this bitmap block,
221 * looking for one that's long enough.
223 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
224 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
225 i <= end;
226 i++) {
228 * See if there's a free extent of maxlen starting at i.
229 * If it's not so then next will contain the first non-free.
231 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
232 if (error) {
233 return error;
235 if (stat) {
237 * i for maxlen is all free, allocate and return that.
239 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
240 rsb);
241 if (error) {
242 return error;
244 *len = maxlen;
245 *rtblock = i;
246 return 0;
249 * In the case where we have a variable-sized allocation
250 * request, figure out how big this free piece is,
251 * and if it's big enough for the minimum, and the best
252 * so far, remember it.
254 if (minlen < maxlen) {
255 xfs_rtblock_t thislen; /* this extent size */
257 thislen = next - i;
258 if (thislen >= minlen && thislen > bestlen) {
259 besti = i;
260 bestlen = thislen;
264 * If not done yet, find the start of the next free space.
266 if (next < end) {
267 error = xfs_rtfind_forw(mp, tp, next, end, &i);
268 if (error) {
269 return error;
271 } else
272 break;
275 * Searched the whole thing & didn't find a maxlen free extent.
277 if (minlen < maxlen && besti != -1) {
278 xfs_extlen_t p; /* amount to trim length by */
281 * If size should be a multiple of prod, make that so.
283 if (prod > 1 && (p = do_mod(bestlen, prod)))
284 bestlen -= p;
286 * Allocate besti for bestlen & return that.
288 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
289 if (error) {
290 return error;
292 *len = bestlen;
293 *rtblock = besti;
294 return 0;
297 * Allocation failed. Set *nextp to the next block to try.
299 *nextp = next;
300 *rtblock = NULLRTBLOCK;
301 return 0;
305 * Allocate an extent of length minlen<=len<=maxlen, starting at block
306 * bno. If we don't get maxlen then use prod to trim the length, if given.
307 * Returns error; returns starting block in *rtblock.
308 * The lengths are all in rtextents.
310 STATIC int /* error */
311 xfs_rtallocate_extent_exact(
312 xfs_mount_t *mp, /* file system mount point */
313 xfs_trans_t *tp, /* transaction pointer */
314 xfs_rtblock_t bno, /* starting block number to allocate */
315 xfs_extlen_t minlen, /* minimum length to allocate */
316 xfs_extlen_t maxlen, /* maximum length to allocate */
317 xfs_extlen_t *len, /* out: actual length allocated */
318 xfs_buf_t **rbpp, /* in/out: summary block buffer */
319 xfs_fsblock_t *rsb, /* in/out: summary block number */
320 xfs_extlen_t prod, /* extent product factor */
321 xfs_rtblock_t *rtblock) /* out: start block allocated */
323 int error; /* error value */
324 xfs_extlen_t i; /* extent length trimmed due to prod */
325 int isfree; /* extent is free */
326 xfs_rtblock_t next; /* next block to try (dummy) */
328 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
330 * Check if the range in question (for maxlen) is free.
332 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
333 if (error) {
334 return error;
336 if (isfree) {
338 * If it is, allocate it and return success.
340 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
341 if (error) {
342 return error;
344 *len = maxlen;
345 *rtblock = bno;
346 return 0;
349 * If not, allocate what there is, if it's at least minlen.
351 maxlen = next - bno;
352 if (maxlen < minlen) {
354 * Failed, return failure status.
356 *rtblock = NULLRTBLOCK;
357 return 0;
360 * Trim off tail of extent, if prod is specified.
362 if (prod > 1 && (i = maxlen % prod)) {
363 maxlen -= i;
364 if (maxlen < minlen) {
366 * Now we can't do it, return failure status.
368 *rtblock = NULLRTBLOCK;
369 return 0;
373 * Allocate what we can and return it.
375 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
376 if (error) {
377 return error;
379 *len = maxlen;
380 *rtblock = bno;
381 return 0;
385 * Allocate an extent of length minlen<=len<=maxlen, starting as near
386 * to bno as possible. If we don't get maxlen then use prod to trim
387 * the length, if given. The lengths are all in rtextents.
389 STATIC int /* error */
390 xfs_rtallocate_extent_near(
391 xfs_mount_t *mp, /* file system mount point */
392 xfs_trans_t *tp, /* transaction pointer */
393 xfs_rtblock_t bno, /* starting block number to allocate */
394 xfs_extlen_t minlen, /* minimum length to allocate */
395 xfs_extlen_t maxlen, /* maximum length to allocate */
396 xfs_extlen_t *len, /* out: actual length allocated */
397 xfs_buf_t **rbpp, /* in/out: summary block buffer */
398 xfs_fsblock_t *rsb, /* in/out: summary block number */
399 xfs_extlen_t prod, /* extent product factor */
400 xfs_rtblock_t *rtblock) /* out: start block allocated */
402 int any; /* any useful extents from summary */
403 xfs_rtblock_t bbno; /* bitmap block number */
404 int error; /* error value */
405 int i; /* bitmap block offset (loop control) */
406 int j; /* secondary loop control */
407 int log2len; /* log2 of minlen */
408 xfs_rtblock_t n; /* next block to try */
409 xfs_rtblock_t r; /* result block */
411 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
413 * If the block number given is off the end, silently set it to
414 * the last block.
416 if (bno >= mp->m_sb.sb_rextents)
417 bno = mp->m_sb.sb_rextents - 1;
419 * Try the exact allocation first.
421 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
422 rbpp, rsb, prod, &r);
423 if (error) {
424 return error;
427 * If the exact allocation worked, return that.
429 if (r != NULLRTBLOCK) {
430 *rtblock = r;
431 return 0;
433 bbno = XFS_BITTOBLOCK(mp, bno);
434 i = 0;
435 ASSERT(minlen != 0);
436 log2len = xfs_highbit32(minlen);
438 * Loop over all bitmap blocks (bbno + i is current block).
440 for (;;) {
442 * Get summary information of extents of all useful levels
443 * starting in this bitmap block.
445 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
446 bbno + i, rbpp, rsb, &any);
447 if (error) {
448 return error;
451 * If there are any useful extents starting here, try
452 * allocating one.
454 if (any) {
456 * On the positive side of the starting location.
458 if (i >= 0) {
460 * Try to allocate an extent starting in
461 * this block.
463 error = xfs_rtallocate_extent_block(mp, tp,
464 bbno + i, minlen, maxlen, len, &n, rbpp,
465 rsb, prod, &r);
466 if (error) {
467 return error;
470 * If it worked, return it.
472 if (r != NULLRTBLOCK) {
473 *rtblock = r;
474 return 0;
478 * On the negative side of the starting location.
480 else { /* i < 0 */
482 * Loop backwards through the bitmap blocks from
483 * the starting point-1 up to where we are now.
484 * There should be an extent which ends in this
485 * bitmap block and is long enough.
487 for (j = -1; j > i; j--) {
489 * Grab the summary information for
490 * this bitmap block.
492 error = xfs_rtany_summary(mp, tp,
493 log2len, mp->m_rsumlevels - 1,
494 bbno + j, rbpp, rsb, &any);
495 if (error) {
496 return error;
499 * If there's no extent given in the
500 * summary that means the extent we
501 * found must carry over from an
502 * earlier block. If there is an
503 * extent given, we've already tried
504 * that allocation, don't do it again.
506 if (any)
507 continue;
508 error = xfs_rtallocate_extent_block(mp,
509 tp, bbno + j, minlen, maxlen,
510 len, &n, rbpp, rsb, prod, &r);
511 if (error) {
512 return error;
515 * If it works, return the extent.
517 if (r != NULLRTBLOCK) {
518 *rtblock = r;
519 return 0;
523 * There weren't intervening bitmap blocks
524 * with a long enough extent, or the
525 * allocation didn't work for some reason
526 * (i.e. it's a little * too short).
527 * Try to allocate from the summary block
528 * that we found.
530 error = xfs_rtallocate_extent_block(mp, tp,
531 bbno + i, minlen, maxlen, len, &n, rbpp,
532 rsb, prod, &r);
533 if (error) {
534 return error;
537 * If it works, return the extent.
539 if (r != NULLRTBLOCK) {
540 *rtblock = r;
541 return 0;
546 * Loop control. If we were on the positive side, and there's
547 * still more blocks on the negative side, go there.
549 if (i > 0 && (int)bbno - i >= 0)
550 i = -i;
552 * If positive, and no more negative, but there are more
553 * positive, go there.
555 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
556 i++;
558 * If negative or 0 (just started), and there are positive
559 * blocks to go, go there. The 0 case moves to block 1.
561 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
562 i = 1 - i;
564 * If negative or 0 and there are more negative blocks,
565 * go there.
567 else if (i <= 0 && (int)bbno + i > 0)
568 i--;
570 * Must be done. Return failure.
572 else
573 break;
575 *rtblock = NULLRTBLOCK;
576 return 0;
580 * Allocate an extent of length minlen<=len<=maxlen, with no position
581 * specified. If we don't get maxlen then use prod to trim
582 * the length, if given. The lengths are all in rtextents.
584 STATIC int /* error */
585 xfs_rtallocate_extent_size(
586 xfs_mount_t *mp, /* file system mount point */
587 xfs_trans_t *tp, /* transaction pointer */
588 xfs_extlen_t minlen, /* minimum length to allocate */
589 xfs_extlen_t maxlen, /* maximum length to allocate */
590 xfs_extlen_t *len, /* out: actual length allocated */
591 xfs_buf_t **rbpp, /* in/out: summary block buffer */
592 xfs_fsblock_t *rsb, /* in/out: summary block number */
593 xfs_extlen_t prod, /* extent product factor */
594 xfs_rtblock_t *rtblock) /* out: start block allocated */
596 int error; /* error value */
597 int i; /* bitmap block number */
598 int l; /* level number (loop control) */
599 xfs_rtblock_t n; /* next block to be tried */
600 xfs_rtblock_t r; /* result block number */
601 xfs_suminfo_t sum; /* summary information for extents */
603 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
604 ASSERT(maxlen != 0);
607 * Loop over all the levels starting with maxlen.
608 * At each level, look at all the bitmap blocks, to see if there
609 * are extents starting there that are long enough (>= maxlen).
610 * Note, only on the initial level can the allocation fail if
611 * the summary says there's an extent.
613 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
615 * Loop over all the bitmap blocks.
617 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
619 * Get the summary for this level/block.
621 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
622 &sum);
623 if (error) {
624 return error;
627 * Nothing there, on to the next block.
629 if (!sum)
630 continue;
632 * Try allocating the extent.
634 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
635 maxlen, len, &n, rbpp, rsb, prod, &r);
636 if (error) {
637 return error;
640 * If it worked, return that.
642 if (r != NULLRTBLOCK) {
643 *rtblock = r;
644 return 0;
647 * If the "next block to try" returned from the
648 * allocator is beyond the next bitmap block,
649 * skip to that bitmap block.
651 if (XFS_BITTOBLOCK(mp, n) > i + 1)
652 i = XFS_BITTOBLOCK(mp, n) - 1;
656 * Didn't find any maxlen blocks. Try smaller ones, unless
657 * we're asking for a fixed size extent.
659 if (minlen > --maxlen) {
660 *rtblock = NULLRTBLOCK;
661 return 0;
663 ASSERT(minlen != 0);
664 ASSERT(maxlen != 0);
667 * Loop over sizes, from maxlen down to minlen.
668 * This time, when we do the allocations, allow smaller ones
669 * to succeed.
671 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
673 * Loop over all the bitmap blocks, try an allocation
674 * starting in that block.
676 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
678 * Get the summary information for this level/block.
680 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
681 &sum);
682 if (error) {
683 return error;
686 * If nothing there, go on to next.
688 if (!sum)
689 continue;
691 * Try the allocation. Make sure the specified
692 * minlen/maxlen are in the possible range for
693 * this summary level.
695 error = xfs_rtallocate_extent_block(mp, tp, i,
696 XFS_RTMAX(minlen, 1 << l),
697 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
698 len, &n, rbpp, rsb, prod, &r);
699 if (error) {
700 return error;
703 * If it worked, return that extent.
705 if (r != NULLRTBLOCK) {
706 *rtblock = r;
707 return 0;
710 * If the "next block to try" returned from the
711 * allocator is beyond the next bitmap block,
712 * skip to that bitmap block.
714 if (XFS_BITTOBLOCK(mp, n) > i + 1)
715 i = XFS_BITTOBLOCK(mp, n) - 1;
719 * Got nothing, return failure.
721 *rtblock = NULLRTBLOCK;
722 return 0;
726 * Mark an extent specified by start and len allocated.
727 * Updates all the summary information as well as the bitmap.
729 STATIC int /* error */
730 xfs_rtallocate_range(
731 xfs_mount_t *mp, /* file system mount point */
732 xfs_trans_t *tp, /* transaction pointer */
733 xfs_rtblock_t start, /* start block to allocate */
734 xfs_extlen_t len, /* length to allocate */
735 xfs_buf_t **rbpp, /* in/out: summary block buffer */
736 xfs_fsblock_t *rsb) /* in/out: summary block number */
738 xfs_rtblock_t end; /* end of the allocated extent */
739 int error; /* error value */
740 xfs_rtblock_t postblock; /* first block allocated > end */
741 xfs_rtblock_t preblock; /* first block allocated < start */
743 end = start + len - 1;
745 * Assume we're allocating out of the middle of a free extent.
746 * We need to find the beginning and end of the extent so we can
747 * properly update the summary.
749 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
750 if (error) {
751 return error;
754 * Find the next allocated block (end of free extent).
756 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
757 &postblock);
758 if (error) {
759 return error;
762 * Decrement the summary information corresponding to the entire
763 * (old) free extent.
765 error = xfs_rtmodify_summary(mp, tp,
766 XFS_RTBLOCKLOG(postblock + 1 - preblock),
767 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
768 if (error) {
769 return error;
772 * If there are blocks not being allocated at the front of the
773 * old extent, add summary data for them to be free.
775 if (preblock < start) {
776 error = xfs_rtmodify_summary(mp, tp,
777 XFS_RTBLOCKLOG(start - preblock),
778 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
779 if (error) {
780 return error;
784 * If there are blocks not being allocated at the end of the
785 * old extent, add summary data for them to be free.
787 if (postblock > end) {
788 error = xfs_rtmodify_summary(mp, tp,
789 XFS_RTBLOCKLOG(postblock - end),
790 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
791 if (error) {
792 return error;
796 * Modify the bitmap to mark this extent allocated.
798 error = xfs_rtmodify_range(mp, tp, start, len, 0);
799 return error;
803 * Return whether there are any free extents in the size range given
804 * by low and high, for the bitmap block bbno.
806 STATIC int /* error */
807 xfs_rtany_summary(
808 xfs_mount_t *mp, /* file system mount structure */
809 xfs_trans_t *tp, /* transaction pointer */
810 int low, /* low log2 extent size */
811 int high, /* high log2 extent size */
812 xfs_rtblock_t bbno, /* bitmap block number */
813 xfs_buf_t **rbpp, /* in/out: summary block buffer */
814 xfs_fsblock_t *rsb, /* in/out: summary block number */
815 int *stat) /* out: any good extents here? */
817 int error; /* error value */
818 int log; /* loop counter, log2 of ext. size */
819 xfs_suminfo_t sum; /* summary data */
822 * Loop over logs of extent sizes. Order is irrelevant.
824 for (log = low; log <= high; log++) {
826 * Get one summary datum.
828 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
829 if (error) {
830 return error;
833 * If there are any, return success.
835 if (sum) {
836 *stat = 1;
837 return 0;
841 * Found nothing, return failure.
843 *stat = 0;
844 return 0;
848 * Get a buffer for the bitmap or summary file block specified.
849 * The buffer is returned read and locked.
851 STATIC int /* error */
852 xfs_rtbuf_get(
853 xfs_mount_t *mp, /* file system mount structure */
854 xfs_trans_t *tp, /* transaction pointer */
855 xfs_rtblock_t block, /* block number in bitmap or summary */
856 int issum, /* is summary not bitmap */
857 xfs_buf_t **bpp) /* output: buffer for the block */
859 xfs_buf_t *bp; /* block buffer, result */
860 xfs_daddr_t d; /* disk addr of block */
861 int error; /* error value */
862 xfs_fsblock_t fsb; /* fs block number for block */
863 xfs_inode_t *ip; /* bitmap or summary inode */
865 ip = issum ? mp->m_rsumip : mp->m_rbmip;
867 * Map from the file offset (block) and inode number to the
868 * file system block.
870 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
871 if (error) {
872 return error;
874 ASSERT(fsb != NULLFSBLOCK);
876 * Convert to disk address for buffer cache.
878 d = XFS_FSB_TO_DADDR(mp, fsb);
880 * Read the buffer.
882 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
883 mp->m_bsize, 0, &bp);
884 if (error) {
885 return error;
887 ASSERT(bp && !XFS_BUF_GETERROR(bp));
888 *bpp = bp;
889 return 0;
892 #ifdef DEBUG
894 * Check that the given extent (block range) is allocated already.
896 STATIC int /* error */
897 xfs_rtcheck_alloc_range(
898 xfs_mount_t *mp, /* file system mount point */
899 xfs_trans_t *tp, /* transaction pointer */
900 xfs_rtblock_t bno, /* starting block number of extent */
901 xfs_extlen_t len, /* length of extent */
902 int *stat) /* out: 1 for allocated, 0 for not */
904 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
906 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
908 #endif
911 * Check that the given range is either all allocated (val = 0) or
912 * all free (val = 1).
914 STATIC int /* error */
915 xfs_rtcheck_range(
916 xfs_mount_t *mp, /* file system mount point */
917 xfs_trans_t *tp, /* transaction pointer */
918 xfs_rtblock_t start, /* starting block number of extent */
919 xfs_extlen_t len, /* length of extent */
920 int val, /* 1 for free, 0 for allocated */
921 xfs_rtblock_t *new, /* out: first block not matching */
922 int *stat) /* out: 1 for matches, 0 for not */
924 xfs_rtword_t *b; /* current word in buffer */
925 int bit; /* bit number in the word */
926 xfs_rtblock_t block; /* bitmap block number */
927 xfs_buf_t *bp; /* buf for the block */
928 xfs_rtword_t *bufp; /* starting word in buffer */
929 int error; /* error value */
930 xfs_rtblock_t i; /* current bit number rel. to start */
931 xfs_rtblock_t lastbit; /* last useful bit in word */
932 xfs_rtword_t mask; /* mask of relevant bits for value */
933 xfs_rtword_t wdiff; /* difference from wanted value */
934 int word; /* word number in the buffer */
937 * Compute starting bitmap block number
939 block = XFS_BITTOBLOCK(mp, start);
941 * Read the bitmap block.
943 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
944 if (error) {
945 return error;
947 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
949 * Compute the starting word's address, and starting bit.
951 word = XFS_BITTOWORD(mp, start);
952 b = &bufp[word];
953 bit = (int)(start & (XFS_NBWORD - 1));
955 * 0 (allocated) => all zero's; 1 (free) => all one's.
957 val = -val;
959 * If not starting on a word boundary, deal with the first
960 * (partial) word.
962 if (bit) {
964 * Compute first bit not examined.
966 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
968 * Mask of relevant bits.
970 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
972 * Compute difference between actual and desired value.
974 if ((wdiff = (*b ^ val) & mask)) {
976 * Different, compute first wrong bit and return.
978 xfs_trans_brelse(tp, bp);
979 i = XFS_RTLOBIT(wdiff) - bit;
980 *new = start + i;
981 *stat = 0;
982 return 0;
984 i = lastbit - bit;
986 * Go on to next block if that's where the next word is
987 * and we need the next word.
989 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
991 * If done with this block, get the next one.
993 xfs_trans_brelse(tp, bp);
994 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
995 if (error) {
996 return error;
998 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
999 word = 0;
1000 } else {
1002 * Go on to the next word in the buffer.
1004 b++;
1006 } else {
1008 * Starting on a word boundary, no partial word.
1010 i = 0;
1013 * Loop over whole words in buffers. When we use up one buffer
1014 * we move on to the next one.
1016 while (len - i >= XFS_NBWORD) {
1018 * Compute difference between actual and desired value.
1020 if ((wdiff = *b ^ val)) {
1022 * Different, compute first wrong bit and return.
1024 xfs_trans_brelse(tp, bp);
1025 i += XFS_RTLOBIT(wdiff);
1026 *new = start + i;
1027 *stat = 0;
1028 return 0;
1030 i += XFS_NBWORD;
1032 * Go on to next block if that's where the next word is
1033 * and we need the next word.
1035 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1037 * If done with this block, get the next one.
1039 xfs_trans_brelse(tp, bp);
1040 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1041 if (error) {
1042 return error;
1044 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1045 word = 0;
1046 } else {
1048 * Go on to the next word in the buffer.
1050 b++;
1054 * If not ending on a word boundary, deal with the last
1055 * (partial) word.
1057 if ((lastbit = len - i)) {
1059 * Mask of relevant bits.
1061 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1063 * Compute difference between actual and desired value.
1065 if ((wdiff = (*b ^ val) & mask)) {
1067 * Different, compute first wrong bit and return.
1069 xfs_trans_brelse(tp, bp);
1070 i += XFS_RTLOBIT(wdiff);
1071 *new = start + i;
1072 *stat = 0;
1073 return 0;
1074 } else
1075 i = len;
1078 * Successful, return.
1080 xfs_trans_brelse(tp, bp);
1081 *new = start + i;
1082 *stat = 1;
1083 return 0;
1087 * Copy and transform the summary file, given the old and new
1088 * parameters in the mount structures.
1090 STATIC int /* error */
1091 xfs_rtcopy_summary(
1092 xfs_mount_t *omp, /* old file system mount point */
1093 xfs_mount_t *nmp, /* new file system mount point */
1094 xfs_trans_t *tp) /* transaction pointer */
1096 xfs_rtblock_t bbno; /* bitmap block number */
1097 xfs_buf_t *bp; /* summary buffer */
1098 int error; /* error return value */
1099 int log; /* summary level number (log length) */
1100 xfs_suminfo_t sum; /* summary data */
1101 xfs_fsblock_t sumbno; /* summary block number */
1103 bp = NULL;
1104 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1105 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1106 (xfs_srtblock_t)bbno >= 0;
1107 bbno--) {
1108 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1109 &sumbno, &sum);
1110 if (error)
1111 return error;
1112 if (sum == 0)
1113 continue;
1114 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1115 &bp, &sumbno);
1116 if (error)
1117 return error;
1118 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1119 &bp, &sumbno);
1120 if (error)
1121 return error;
1122 ASSERT(sum > 0);
1125 return 0;
1129 * Searching backward from start to limit, find the first block whose
1130 * allocated/free state is different from start's.
1132 STATIC int /* error */
1133 xfs_rtfind_back(
1134 xfs_mount_t *mp, /* file system mount point */
1135 xfs_trans_t *tp, /* transaction pointer */
1136 xfs_rtblock_t start, /* starting block to look at */
1137 xfs_rtblock_t limit, /* last block to look at */
1138 xfs_rtblock_t *rtblock) /* out: start block found */
1140 xfs_rtword_t *b; /* current word in buffer */
1141 int bit; /* bit number in the word */
1142 xfs_rtblock_t block; /* bitmap block number */
1143 xfs_buf_t *bp; /* buf for the block */
1144 xfs_rtword_t *bufp; /* starting word in buffer */
1145 int error; /* error value */
1146 xfs_rtblock_t firstbit; /* first useful bit in the word */
1147 xfs_rtblock_t i; /* current bit number rel. to start */
1148 xfs_rtblock_t len; /* length of inspected area */
1149 xfs_rtword_t mask; /* mask of relevant bits for value */
1150 xfs_rtword_t want; /* mask for "good" values */
1151 xfs_rtword_t wdiff; /* difference from wanted value */
1152 int word; /* word number in the buffer */
1155 * Compute and read in starting bitmap block for starting block.
1157 block = XFS_BITTOBLOCK(mp, start);
1158 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1159 if (error) {
1160 return error;
1162 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1164 * Get the first word's index & point to it.
1166 word = XFS_BITTOWORD(mp, start);
1167 b = &bufp[word];
1168 bit = (int)(start & (XFS_NBWORD - 1));
1169 len = start - limit + 1;
1171 * Compute match value, based on the bit at start: if 1 (free)
1172 * then all-ones, else all-zeroes.
1174 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1176 * If the starting position is not word-aligned, deal with the
1177 * partial word.
1179 if (bit < XFS_NBWORD - 1) {
1181 * Calculate first (leftmost) bit number to look at,
1182 * and mask for all the relevant bits in this word.
1184 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1185 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1186 firstbit;
1188 * Calculate the difference between the value there
1189 * and what we're looking for.
1191 if ((wdiff = (*b ^ want) & mask)) {
1193 * Different. Mark where we are and return.
1195 xfs_trans_brelse(tp, bp);
1196 i = bit - XFS_RTHIBIT(wdiff);
1197 *rtblock = start - i + 1;
1198 return 0;
1200 i = bit - firstbit + 1;
1202 * Go on to previous block if that's where the previous word is
1203 * and we need the previous word.
1205 if (--word == -1 && i < len) {
1207 * If done with this block, get the previous one.
1209 xfs_trans_brelse(tp, bp);
1210 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1211 if (error) {
1212 return error;
1214 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1215 word = XFS_BLOCKWMASK(mp);
1216 b = &bufp[word];
1217 } else {
1219 * Go on to the previous word in the buffer.
1221 b--;
1223 } else {
1225 * Starting on a word boundary, no partial word.
1227 i = 0;
1230 * Loop over whole words in buffers. When we use up one buffer
1231 * we move on to the previous one.
1233 while (len - i >= XFS_NBWORD) {
1235 * Compute difference between actual and desired value.
1237 if ((wdiff = *b ^ want)) {
1239 * Different, mark where we are and return.
1241 xfs_trans_brelse(tp, bp);
1242 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1243 *rtblock = start - i + 1;
1244 return 0;
1246 i += XFS_NBWORD;
1248 * Go on to previous block if that's where the previous word is
1249 * and we need the previous word.
1251 if (--word == -1 && i < len) {
1253 * If done with this block, get the previous one.
1255 xfs_trans_brelse(tp, bp);
1256 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1257 if (error) {
1258 return error;
1260 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1261 word = XFS_BLOCKWMASK(mp);
1262 b = &bufp[word];
1263 } else {
1265 * Go on to the previous word in the buffer.
1267 b--;
1271 * If not ending on a word boundary, deal with the last
1272 * (partial) word.
1274 if (len - i) {
1276 * Calculate first (leftmost) bit number to look at,
1277 * and mask for all the relevant bits in this word.
1279 firstbit = XFS_NBWORD - (len - i);
1280 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1282 * Compute difference between actual and desired value.
1284 if ((wdiff = (*b ^ want) & mask)) {
1286 * Different, mark where we are and return.
1288 xfs_trans_brelse(tp, bp);
1289 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1290 *rtblock = start - i + 1;
1291 return 0;
1292 } else
1293 i = len;
1296 * No match, return that we scanned the whole area.
1298 xfs_trans_brelse(tp, bp);
1299 *rtblock = start - i + 1;
1300 return 0;
1304 * Searching forward from start to limit, find the first block whose
1305 * allocated/free state is different from start's.
1307 STATIC int /* error */
1308 xfs_rtfind_forw(
1309 xfs_mount_t *mp, /* file system mount point */
1310 xfs_trans_t *tp, /* transaction pointer */
1311 xfs_rtblock_t start, /* starting block to look at */
1312 xfs_rtblock_t limit, /* last block to look at */
1313 xfs_rtblock_t *rtblock) /* out: start block found */
1315 xfs_rtword_t *b; /* current word in buffer */
1316 int bit; /* bit number in the word */
1317 xfs_rtblock_t block; /* bitmap block number */
1318 xfs_buf_t *bp; /* buf for the block */
1319 xfs_rtword_t *bufp; /* starting word in buffer */
1320 int error; /* error value */
1321 xfs_rtblock_t i; /* current bit number rel. to start */
1322 xfs_rtblock_t lastbit; /* last useful bit in the word */
1323 xfs_rtblock_t len; /* length of inspected area */
1324 xfs_rtword_t mask; /* mask of relevant bits for value */
1325 xfs_rtword_t want; /* mask for "good" values */
1326 xfs_rtword_t wdiff; /* difference from wanted value */
1327 int word; /* word number in the buffer */
1330 * Compute and read in starting bitmap block for starting block.
1332 block = XFS_BITTOBLOCK(mp, start);
1333 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1334 if (error) {
1335 return error;
1337 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1339 * Get the first word's index & point to it.
1341 word = XFS_BITTOWORD(mp, start);
1342 b = &bufp[word];
1343 bit = (int)(start & (XFS_NBWORD - 1));
1344 len = limit - start + 1;
1346 * Compute match value, based on the bit at start: if 1 (free)
1347 * then all-ones, else all-zeroes.
1349 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1351 * If the starting position is not word-aligned, deal with the
1352 * partial word.
1354 if (bit) {
1356 * Calculate last (rightmost) bit number to look at,
1357 * and mask for all the relevant bits in this word.
1359 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1360 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1362 * Calculate the difference between the value there
1363 * and what we're looking for.
1365 if ((wdiff = (*b ^ want) & mask)) {
1367 * Different. Mark where we are and return.
1369 xfs_trans_brelse(tp, bp);
1370 i = XFS_RTLOBIT(wdiff) - bit;
1371 *rtblock = start + i - 1;
1372 return 0;
1374 i = lastbit - bit;
1376 * Go on to next block if that's where the next word is
1377 * and we need the next word.
1379 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1381 * If done with this block, get the previous one.
1383 xfs_trans_brelse(tp, bp);
1384 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1385 if (error) {
1386 return error;
1388 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1389 word = 0;
1390 } else {
1392 * Go on to the previous word in the buffer.
1394 b++;
1396 } else {
1398 * Starting on a word boundary, no partial word.
1400 i = 0;
1403 * Loop over whole words in buffers. When we use up one buffer
1404 * we move on to the next one.
1406 while (len - i >= XFS_NBWORD) {
1408 * Compute difference between actual and desired value.
1410 if ((wdiff = *b ^ want)) {
1412 * Different, mark where we are and return.
1414 xfs_trans_brelse(tp, bp);
1415 i += XFS_RTLOBIT(wdiff);
1416 *rtblock = start + i - 1;
1417 return 0;
1419 i += XFS_NBWORD;
1421 * Go on to next block if that's where the next word is
1422 * and we need the next word.
1424 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1426 * If done with this block, get the next one.
1428 xfs_trans_brelse(tp, bp);
1429 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1430 if (error) {
1431 return error;
1433 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1434 word = 0;
1435 } else {
1437 * Go on to the next word in the buffer.
1439 b++;
1443 * If not ending on a word boundary, deal with the last
1444 * (partial) word.
1446 if ((lastbit = len - i)) {
1448 * Calculate mask for all the relevant bits in this word.
1450 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1452 * Compute difference between actual and desired value.
1454 if ((wdiff = (*b ^ want) & mask)) {
1456 * Different, mark where we are and return.
1458 xfs_trans_brelse(tp, bp);
1459 i += XFS_RTLOBIT(wdiff);
1460 *rtblock = start + i - 1;
1461 return 0;
1462 } else
1463 i = len;
1466 * No match, return that we scanned the whole area.
1468 xfs_trans_brelse(tp, bp);
1469 *rtblock = start + i - 1;
1470 return 0;
1474 * Mark an extent specified by start and len freed.
1475 * Updates all the summary information as well as the bitmap.
1477 STATIC int /* error */
1478 xfs_rtfree_range(
1479 xfs_mount_t *mp, /* file system mount point */
1480 xfs_trans_t *tp, /* transaction pointer */
1481 xfs_rtblock_t start, /* starting block to free */
1482 xfs_extlen_t len, /* length to free */
1483 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1484 xfs_fsblock_t *rsb) /* in/out: summary block number */
1486 xfs_rtblock_t end; /* end of the freed extent */
1487 int error; /* error value */
1488 xfs_rtblock_t postblock; /* first block freed > end */
1489 xfs_rtblock_t preblock; /* first block freed < start */
1491 end = start + len - 1;
1493 * Modify the bitmap to mark this extent freed.
1495 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1496 if (error) {
1497 return error;
1500 * Assume we're freeing out of the middle of an allocated extent.
1501 * We need to find the beginning and end of the extent so we can
1502 * properly update the summary.
1504 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1505 if (error) {
1506 return error;
1509 * Find the next allocated block (end of allocated extent).
1511 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1512 &postblock);
1514 * If there are blocks not being freed at the front of the
1515 * old extent, add summary data for them to be allocated.
1517 if (preblock < start) {
1518 error = xfs_rtmodify_summary(mp, tp,
1519 XFS_RTBLOCKLOG(start - preblock),
1520 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1521 if (error) {
1522 return error;
1526 * If there are blocks not being freed at the end of the
1527 * old extent, add summary data for them to be allocated.
1529 if (postblock > end) {
1530 error = xfs_rtmodify_summary(mp, tp,
1531 XFS_RTBLOCKLOG(postblock - end),
1532 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1533 if (error) {
1534 return error;
1538 * Increment the summary information corresponding to the entire
1539 * (new) free extent.
1541 error = xfs_rtmodify_summary(mp, tp,
1542 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1543 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1544 return error;
1548 * Read and return the summary information for a given extent size,
1549 * bitmap block combination.
1550 * Keeps track of a current summary block, so we don't keep reading
1551 * it from the buffer cache.
1553 STATIC int /* error */
1554 xfs_rtget_summary(
1555 xfs_mount_t *mp, /* file system mount structure */
1556 xfs_trans_t *tp, /* transaction pointer */
1557 int log, /* log2 of extent size */
1558 xfs_rtblock_t bbno, /* bitmap block number */
1559 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1560 xfs_fsblock_t *rsb, /* in/out: summary block number */
1561 xfs_suminfo_t *sum) /* out: summary info for this block */
1563 xfs_buf_t *bp; /* buffer for summary block */
1564 int error; /* error value */
1565 xfs_fsblock_t sb; /* summary fsblock */
1566 int so; /* index into the summary file */
1567 xfs_suminfo_t *sp; /* pointer to returned data */
1570 * Compute entry number in the summary file.
1572 so = XFS_SUMOFFS(mp, log, bbno);
1574 * Compute the block number in the summary file.
1576 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1578 * If we have an old buffer, and the block number matches, use that.
1580 if (rbpp && *rbpp && *rsb == sb)
1581 bp = *rbpp;
1583 * Otherwise we have to get the buffer.
1585 else {
1587 * If there was an old one, get rid of it first.
1589 if (rbpp && *rbpp)
1590 xfs_trans_brelse(tp, *rbpp);
1591 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1592 if (error) {
1593 return error;
1596 * Remember this buffer and block for the next call.
1598 if (rbpp) {
1599 *rbpp = bp;
1600 *rsb = sb;
1604 * Point to the summary information & copy it out.
1606 sp = XFS_SUMPTR(mp, bp, so);
1607 *sum = *sp;
1609 * Drop the buffer if we're not asked to remember it.
1611 if (!rbpp)
1612 xfs_trans_brelse(tp, bp);
1613 return 0;
1617 * Set the given range of bitmap bits to the given value.
1618 * Do whatever I/O and logging is required.
1620 STATIC int /* error */
1621 xfs_rtmodify_range(
1622 xfs_mount_t *mp, /* file system mount point */
1623 xfs_trans_t *tp, /* transaction pointer */
1624 xfs_rtblock_t start, /* starting block to modify */
1625 xfs_extlen_t len, /* length of extent to modify */
1626 int val) /* 1 for free, 0 for allocated */
1628 xfs_rtword_t *b; /* current word in buffer */
1629 int bit; /* bit number in the word */
1630 xfs_rtblock_t block; /* bitmap block number */
1631 xfs_buf_t *bp; /* buf for the block */
1632 xfs_rtword_t *bufp; /* starting word in buffer */
1633 int error; /* error value */
1634 xfs_rtword_t *first; /* first used word in the buffer */
1635 int i; /* current bit number rel. to start */
1636 int lastbit; /* last useful bit in word */
1637 xfs_rtword_t mask; /* mask o frelevant bits for value */
1638 int word; /* word number in the buffer */
1641 * Compute starting bitmap block number.
1643 block = XFS_BITTOBLOCK(mp, start);
1645 * Read the bitmap block, and point to its data.
1647 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1648 if (error) {
1649 return error;
1651 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1653 * Compute the starting word's address, and starting bit.
1655 word = XFS_BITTOWORD(mp, start);
1656 first = b = &bufp[word];
1657 bit = (int)(start & (XFS_NBWORD - 1));
1659 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1661 val = -val;
1663 * If not starting on a word boundary, deal with the first
1664 * (partial) word.
1666 if (bit) {
1668 * Compute first bit not changed and mask of relevant bits.
1670 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1671 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1673 * Set/clear the active bits.
1675 if (val)
1676 *b |= mask;
1677 else
1678 *b &= ~mask;
1679 i = lastbit - bit;
1681 * Go on to the next block if that's where the next word is
1682 * and we need the next word.
1684 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1686 * Log the changed part of this block.
1687 * Get the next one.
1689 xfs_trans_log_buf(tp, bp,
1690 (uint)((char *)first - (char *)bufp),
1691 (uint)((char *)b - (char *)bufp));
1692 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1693 if (error) {
1694 return error;
1696 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1697 word = 0;
1698 } else {
1700 * Go on to the next word in the buffer
1702 b++;
1704 } else {
1706 * Starting on a word boundary, no partial word.
1708 i = 0;
1711 * Loop over whole words in buffers. When we use up one buffer
1712 * we move on to the next one.
1714 while (len - i >= XFS_NBWORD) {
1716 * Set the word value correctly.
1718 *b = val;
1719 i += XFS_NBWORD;
1721 * Go on to the next block if that's where the next word is
1722 * and we need the next word.
1724 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1726 * Log the changed part of this block.
1727 * Get the next one.
1729 xfs_trans_log_buf(tp, bp,
1730 (uint)((char *)first - (char *)bufp),
1731 (uint)((char *)b - (char *)bufp));
1732 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1733 if (error) {
1734 return error;
1736 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1737 word = 0;
1738 } else {
1740 * Go on to the next word in the buffer
1742 b++;
1746 * If not ending on a word boundary, deal with the last
1747 * (partial) word.
1749 if ((lastbit = len - i)) {
1751 * Compute a mask of relevant bits.
1753 bit = 0;
1754 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1756 * Set/clear the active bits.
1758 if (val)
1759 *b |= mask;
1760 else
1761 *b &= ~mask;
1762 b++;
1765 * Log any remaining changed bytes.
1767 if (b > first)
1768 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1769 (uint)((char *)b - (char *)bufp - 1));
1770 return 0;
1774 * Read and modify the summary information for a given extent size,
1775 * bitmap block combination.
1776 * Keeps track of a current summary block, so we don't keep reading
1777 * it from the buffer cache.
1779 STATIC int /* error */
1780 xfs_rtmodify_summary(
1781 xfs_mount_t *mp, /* file system mount point */
1782 xfs_trans_t *tp, /* transaction pointer */
1783 int log, /* log2 of extent size */
1784 xfs_rtblock_t bbno, /* bitmap block number */
1785 int delta, /* change to make to summary info */
1786 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1787 xfs_fsblock_t *rsb) /* in/out: summary block number */
1789 xfs_buf_t *bp; /* buffer for the summary block */
1790 int error; /* error value */
1791 xfs_fsblock_t sb; /* summary fsblock */
1792 int so; /* index into the summary file */
1793 xfs_suminfo_t *sp; /* pointer to returned data */
1796 * Compute entry number in the summary file.
1798 so = XFS_SUMOFFS(mp, log, bbno);
1800 * Compute the block number in the summary file.
1802 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1804 * If we have an old buffer, and the block number matches, use that.
1806 if (rbpp && *rbpp && *rsb == sb)
1807 bp = *rbpp;
1809 * Otherwise we have to get the buffer.
1811 else {
1813 * If there was an old one, get rid of it first.
1815 if (rbpp && *rbpp)
1816 xfs_trans_brelse(tp, *rbpp);
1817 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1818 if (error) {
1819 return error;
1822 * Remember this buffer and block for the next call.
1824 if (rbpp) {
1825 *rbpp = bp;
1826 *rsb = sb;
1830 * Point to the summary information, modify and log it.
1832 sp = XFS_SUMPTR(mp, bp, so);
1833 *sp += delta;
1834 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1835 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1836 return 0;
1840 * Visible (exported) functions.
1844 * Grow the realtime area of the filesystem.
1847 xfs_growfs_rt(
1848 xfs_mount_t *mp, /* mount point for filesystem */
1849 xfs_growfs_rt_t *in) /* growfs rt input struct */
1851 xfs_rtblock_t bmbno; /* bitmap block number */
1852 xfs_buf_t *bp; /* temporary buffer */
1853 int cancelflags; /* flags for xfs_trans_cancel */
1854 int error; /* error return value */
1855 xfs_inode_t *ip; /* bitmap inode, used as lock */
1856 xfs_mount_t *nmp; /* new (fake) mount structure */
1857 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1858 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1859 xfs_drtbno_t nrextents; /* new number of realtime extents */
1860 uint8_t nrextslog; /* new log2 of sb_rextents */
1861 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1862 uint nrsumlevels; /* new rt summary levels */
1863 uint nrsumsize; /* new size of rt summary, bytes */
1864 xfs_sb_t *nsbp; /* new superblock */
1865 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1866 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1867 xfs_sb_t *sbp; /* old superblock */
1868 xfs_fsblock_t sumbno; /* summary block number */
1869 xfs_trans_t *tp; /* transaction pointer */
1871 sbp = &mp->m_sb;
1873 * Initial error checking.
1875 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1876 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1877 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1878 return XFS_ERROR(EINVAL);
1879 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1880 return error;
1882 * Read in the last block of the device, make sure it exists.
1884 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1885 XFS_FSB_TO_BB(mp, nrblocks - 1),
1886 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1887 if (error)
1888 return error;
1889 ASSERT(bp);
1890 xfs_buf_relse(bp);
1892 * Calculate new parameters. These are the final values to be reached.
1894 nrextents = nrblocks;
1895 do_div(nrextents, in->extsize);
1896 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1897 nrextslog = xfs_highbit32(nrextents);
1898 nrsumlevels = nrextslog + 1;
1899 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1900 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1901 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1903 * New summary size can't be more than half the size of
1904 * the log. This prevents us from getting a log overflow,
1905 * since we'll log basically the whole summary file at once.
1907 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1908 return XFS_ERROR(EINVAL);
1910 * Get the old block counts for bitmap and summary inodes.
1911 * These can't change since other growfs callers are locked out.
1913 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1914 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1916 * Allocate space to the bitmap and summary files, as necessary.
1918 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1919 mp->m_sb.sb_rbmino)))
1920 return error;
1921 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1922 mp->m_sb.sb_rsumino)))
1923 return error;
1925 * Allocate a new (fake) mount/sb.
1927 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1929 * Loop over the bitmap blocks.
1930 * We will do everything one bitmap block at a time.
1931 * Skip the current block if it is exactly full.
1932 * This also deals with the case where there were no rtextents before.
1934 for (bmbno = sbp->sb_rbmblocks -
1935 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1936 bmbno < nrbmblocks;
1937 bmbno++) {
1938 *nmp = *mp;
1939 nsbp = &nmp->m_sb;
1941 * Calculate new sb and mount fields for this round.
1943 nsbp->sb_rextsize = in->extsize;
1944 nsbp->sb_rbmblocks = bmbno + 1;
1945 nsbp->sb_rblocks =
1946 XFS_RTMIN(nrblocks,
1947 nsbp->sb_rbmblocks * NBBY *
1948 nsbp->sb_blocksize * nsbp->sb_rextsize);
1949 nsbp->sb_rextents = nsbp->sb_rblocks;
1950 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1951 ASSERT(nsbp->sb_rextents != 0);
1952 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1953 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1954 nrsumsize =
1955 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1956 nsbp->sb_rbmblocks;
1957 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1958 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1960 * Start a transaction, get the log reservation.
1962 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1963 cancelflags = 0;
1964 if ((error = xfs_trans_reserve(tp, 0,
1965 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1966 break;
1968 * Lock out other callers by grabbing the bitmap inode lock.
1970 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1971 XFS_ILOCK_EXCL, &ip)))
1972 break;
1973 ASSERT(ip == mp->m_rbmip);
1975 * Update the bitmap inode's size.
1977 mp->m_rbmip->i_d.di_size =
1978 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1979 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1980 cancelflags |= XFS_TRANS_ABORT;
1982 * Get the summary inode into the transaction.
1984 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1985 XFS_ILOCK_EXCL, &ip)))
1986 break;
1987 ASSERT(ip == mp->m_rsumip);
1989 * Update the summary inode's size.
1991 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1992 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1994 * Copy summary data from old to new sizes.
1995 * Do this when the real size (not block-aligned) changes.
1997 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1998 mp->m_rsumlevels != nmp->m_rsumlevels) {
1999 error = xfs_rtcopy_summary(mp, nmp, tp);
2000 if (error)
2001 break;
2004 * Update superblock fields.
2006 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2007 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2008 nsbp->sb_rextsize - sbp->sb_rextsize);
2009 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2010 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2011 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2012 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2013 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2014 nsbp->sb_rblocks - sbp->sb_rblocks);
2015 if (nsbp->sb_rextents != sbp->sb_rextents)
2016 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2017 nsbp->sb_rextents - sbp->sb_rextents);
2018 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2019 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2020 nsbp->sb_rextslog - sbp->sb_rextslog);
2022 * Free new extent.
2024 bp = NULL;
2025 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2026 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2027 if (error)
2028 break;
2030 * Mark more blocks free in the superblock.
2032 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2033 nsbp->sb_rextents - sbp->sb_rextents);
2035 * Update mp values into the real mp structure.
2037 mp->m_rsumlevels = nrsumlevels;
2038 mp->m_rsumsize = nrsumsize;
2040 * Commit the transaction.
2042 xfs_trans_commit(tp, 0);
2045 if (error)
2046 xfs_trans_cancel(tp, cancelflags);
2049 * Free the fake mp structure.
2051 kmem_free(nmp, sizeof(*nmp));
2053 return error;
2057 * Allocate an extent in the realtime subvolume, with the usual allocation
2058 * parameters. The length units are all in realtime extents, as is the
2059 * result block number.
2061 int /* error */
2062 xfs_rtallocate_extent(
2063 xfs_trans_t *tp, /* transaction pointer */
2064 xfs_rtblock_t bno, /* starting block number to allocate */
2065 xfs_extlen_t minlen, /* minimum length to allocate */
2066 xfs_extlen_t maxlen, /* maximum length to allocate */
2067 xfs_extlen_t *len, /* out: actual length allocated */
2068 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2069 int wasdel, /* was a delayed allocation extent */
2070 xfs_extlen_t prod, /* extent product factor */
2071 xfs_rtblock_t *rtblock) /* out: start block allocated */
2073 int error; /* error value */
2074 xfs_inode_t *ip; /* inode for bitmap file */
2075 xfs_mount_t *mp; /* file system mount structure */
2076 xfs_rtblock_t r; /* result allocated block */
2077 xfs_fsblock_t sb; /* summary file block number */
2078 xfs_buf_t *sumbp; /* summary file block buffer */
2080 ASSERT(minlen > 0 && minlen <= maxlen);
2081 mp = tp->t_mountp;
2083 * If prod is set then figure out what to do to minlen and maxlen.
2085 if (prod > 1) {
2086 xfs_extlen_t i;
2088 if ((i = maxlen % prod))
2089 maxlen -= i;
2090 if ((i = minlen % prod))
2091 minlen += prod - i;
2092 if (maxlen < minlen) {
2093 *rtblock = NULLRTBLOCK;
2094 return 0;
2098 * Lock out other callers by grabbing the bitmap inode lock.
2100 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2101 XFS_ILOCK_EXCL, &ip)))
2102 return error;
2103 sumbp = NULL;
2105 * Allocate by size, or near another block, or exactly at some block.
2107 switch (type) {
2108 case XFS_ALLOCTYPE_ANY_AG:
2109 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2110 &sumbp, &sb, prod, &r);
2111 break;
2112 case XFS_ALLOCTYPE_NEAR_BNO:
2113 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2114 len, &sumbp, &sb, prod, &r);
2115 break;
2116 case XFS_ALLOCTYPE_THIS_BNO:
2117 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2118 len, &sumbp, &sb, prod, &r);
2119 break;
2120 default:
2121 ASSERT(0);
2123 if (error) {
2124 return error;
2127 * If it worked, update the superblock.
2129 if (r != NULLRTBLOCK) {
2130 long slen = (long)*len;
2132 ASSERT(*len >= minlen && *len <= maxlen);
2133 if (wasdel)
2134 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2135 else
2136 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2138 *rtblock = r;
2139 return 0;
2143 * Free an extent in the realtime subvolume. Length is expressed in
2144 * realtime extents, as is the block number.
2146 int /* error */
2147 xfs_rtfree_extent(
2148 xfs_trans_t *tp, /* transaction pointer */
2149 xfs_rtblock_t bno, /* starting block number to free */
2150 xfs_extlen_t len) /* length of extent freed */
2152 int error; /* error value */
2153 xfs_inode_t *ip; /* bitmap file inode */
2154 xfs_mount_t *mp; /* file system mount structure */
2155 xfs_fsblock_t sb; /* summary file block number */
2156 xfs_buf_t *sumbp; /* summary file block buffer */
2158 mp = tp->t_mountp;
2160 * Synchronize by locking the bitmap inode.
2162 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2163 XFS_ILOCK_EXCL, &ip)))
2164 return error;
2165 #if defined(__KERNEL__) && defined(DEBUG)
2167 * Check to see that this whole range is currently allocated.
2170 int stat; /* result from checking range */
2172 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2173 if (error) {
2174 return error;
2176 ASSERT(stat);
2178 #endif
2179 sumbp = NULL;
2181 * Free the range of realtime blocks.
2183 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2184 if (error) {
2185 return error;
2188 * Mark more blocks free in the superblock.
2190 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2192 * If we've now freed all the blocks, reset the file sequence
2193 * number to 0.
2195 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2196 mp->m_sb.sb_rextents) {
2197 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2198 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2199 *(__uint64_t *)&ip->i_d.di_atime = 0;
2200 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2202 return 0;
2206 * Initialize realtime fields in the mount structure.
2208 int /* error */
2209 xfs_rtmount_init(
2210 xfs_mount_t *mp) /* file system mount structure */
2212 xfs_buf_t *bp; /* buffer for last block of subvolume */
2213 xfs_daddr_t d; /* address of last block of subvolume */
2214 int error; /* error return value */
2215 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2217 sbp = &mp->m_sb;
2218 if (sbp->sb_rblocks == 0)
2219 return 0;
2220 if (mp->m_rtdev_targp == NULL) {
2221 cmn_err(CE_WARN,
2222 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2223 return XFS_ERROR(ENODEV);
2225 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2226 mp->m_rsumsize =
2227 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2228 sbp->sb_rbmblocks;
2229 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2230 mp->m_rbmip = mp->m_rsumip = NULL;
2232 * Check that the realtime section is an ok size.
2234 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2235 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2236 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2237 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2238 (unsigned long long) mp->m_sb.sb_rblocks);
2239 return XFS_ERROR(E2BIG);
2241 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2242 d - XFS_FSB_TO_BB(mp, 1),
2243 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2244 if (error) {
2245 cmn_err(CE_WARN,
2246 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2247 if (error == ENOSPC)
2248 return XFS_ERROR(E2BIG);
2249 return error;
2251 xfs_buf_relse(bp);
2252 return 0;
2256 * Get the bitmap and summary inodes into the mount structure
2257 * at mount time.
2259 int /* error */
2260 xfs_rtmount_inodes(
2261 xfs_mount_t *mp) /* file system mount structure */
2263 int error; /* error return value */
2264 xfs_sb_t *sbp;
2266 sbp = &mp->m_sb;
2267 if (sbp->sb_rbmino == NULLFSINO)
2268 return 0;
2269 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2270 if (error)
2271 return error;
2272 ASSERT(mp->m_rbmip != NULL);
2273 ASSERT(sbp->sb_rsumino != NULLFSINO);
2274 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2275 if (error) {
2276 VN_RELE(XFS_ITOV(mp->m_rbmip));
2277 return error;
2279 ASSERT(mp->m_rsumip != NULL);
2280 return 0;
2284 * Pick an extent for allocation at the start of a new realtime file.
2285 * Use the sequence number stored in the atime field of the bitmap inode.
2286 * Translate this to a fraction of the rtextents, and return the product
2287 * of rtextents and the fraction.
2288 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2290 int /* error */
2291 xfs_rtpick_extent(
2292 xfs_mount_t *mp, /* file system mount point */
2293 xfs_trans_t *tp, /* transaction pointer */
2294 xfs_extlen_t len, /* allocation length (rtextents) */
2295 xfs_rtblock_t *pick) /* result rt extent */
2297 xfs_rtblock_t b; /* result block */
2298 int error; /* error return value */
2299 xfs_inode_t *ip; /* bitmap incore inode */
2300 int log2; /* log of sequence number */
2301 __uint64_t resid; /* residual after log removed */
2302 __uint64_t seq; /* sequence number of file creation */
2303 __uint64_t *seqp; /* pointer to seqno in inode */
2305 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2306 XFS_ILOCK_EXCL, &ip)))
2307 return error;
2308 ASSERT(ip == mp->m_rbmip);
2309 seqp = (__uint64_t *)&ip->i_d.di_atime;
2310 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2311 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2312 *seqp = 0;
2314 seq = *seqp;
2315 if ((log2 = xfs_highbit64(seq)) == -1)
2316 b = 0;
2317 else {
2318 resid = seq - (1ULL << log2);
2319 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2320 (log2 + 1);
2321 if (b >= mp->m_sb.sb_rextents)
2322 b = do_mod(b, mp->m_sb.sb_rextents);
2323 if (b + len > mp->m_sb.sb_rextents)
2324 b = mp->m_sb.sb_rextents - len;
2326 *seqp = seq + 1;
2327 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2328 *pick = b;
2329 return 0;