fs/partition/msdos: fix unusable extended partition for > 512B sector
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_rtalloc.c
blob6be05f756d59d544c82576d307266ce5b739f893
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"
47 #include "xfs_utils.h"
48 #include "xfs_trace.h"
52 * Prototypes for internal functions.
56 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
57 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
58 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
59 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
60 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
61 xfs_extlen_t, int, xfs_rtblock_t *, int *);
62 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
63 xfs_rtblock_t, xfs_rtblock_t *);
64 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
65 xfs_rtblock_t, xfs_rtblock_t *);
66 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
67 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
68 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
69 xfs_extlen_t, int);
70 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
71 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
74 * Internal functions.
78 * Allocate space to the bitmap or summary file, and zero it, for growfs.
80 STATIC int /* error */
81 xfs_growfs_rt_alloc(
82 xfs_mount_t *mp, /* file system mount point */
83 xfs_extlen_t oblocks, /* old count of blocks */
84 xfs_extlen_t nblocks, /* new count of blocks */
85 xfs_ino_t ino) /* inode number (bitmap/summary) */
87 xfs_fileoff_t bno; /* block number in file */
88 xfs_buf_t *bp; /* temporary buffer for zeroing */
89 int committed; /* transaction committed flag */
90 xfs_daddr_t d; /* disk block address */
91 int error; /* error return value */
92 xfs_fsblock_t firstblock; /* first block allocated in xaction */
93 xfs_bmap_free_t flist; /* list of freed blocks */
94 xfs_fsblock_t fsbno; /* filesystem block for bno */
95 xfs_inode_t *ip; /* pointer to incore inode */
96 xfs_bmbt_irec_t map; /* block map output */
97 int nmap; /* number of block maps */
98 int resblks; /* space reservation */
101 * Allocate space to the file, as necessary.
103 while (oblocks < nblocks) {
104 int cancelflags = 0;
105 xfs_trans_t *tp;
107 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
108 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
110 * Reserve space & log for one extent added to the file.
112 if ((error = xfs_trans_reserve(tp, resblks,
113 XFS_GROWRTALLOC_LOG_RES(mp), 0,
114 XFS_TRANS_PERM_LOG_RES,
115 XFS_DEFAULT_PERM_LOG_COUNT)))
116 goto error_cancel;
117 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
119 * Lock the inode.
121 if ((error = xfs_trans_iget(mp, tp, ino, 0,
122 XFS_ILOCK_EXCL, &ip)))
123 goto error_cancel;
124 xfs_bmap_init(&flist, &firstblock);
126 * Allocate blocks to the bitmap file.
128 nmap = 1;
129 cancelflags |= XFS_TRANS_ABORT;
130 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
131 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
132 resblks, &map, &nmap, &flist, NULL);
133 if (!error && nmap < 1)
134 error = XFS_ERROR(ENOSPC);
135 if (error)
136 goto error_cancel;
138 * Free any blocks freed up in the transaction, then commit.
140 error = xfs_bmap_finish(&tp, &flist, &committed);
141 if (error)
142 goto error_cancel;
143 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
144 if (error)
145 goto error;
147 * Now we need to clear the allocated blocks.
148 * Do this one block per transaction, to keep it simple.
150 cancelflags = 0;
151 for (bno = map.br_startoff, fsbno = map.br_startblock;
152 bno < map.br_startoff + map.br_blockcount;
153 bno++, fsbno++) {
154 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
156 * Reserve log for one block zeroing.
158 if ((error = xfs_trans_reserve(tp, 0,
159 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
160 goto error_cancel;
162 * Lock the bitmap inode.
164 if ((error = xfs_trans_iget(mp, tp, ino, 0,
165 XFS_ILOCK_EXCL, &ip)))
166 goto error_cancel;
168 * Get a buffer for the block.
170 d = XFS_FSB_TO_DADDR(mp, fsbno);
171 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
172 mp->m_bsize, 0);
173 if (bp == NULL) {
174 error = XFS_ERROR(EIO);
175 error_cancel:
176 xfs_trans_cancel(tp, cancelflags);
177 goto error;
179 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
180 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
182 * Commit the transaction.
184 error = xfs_trans_commit(tp, 0);
185 if (error)
186 goto error;
189 * Go on to the next extent, if any.
191 oblocks = map.br_startoff + map.br_blockcount;
193 return 0;
194 error:
195 return error;
199 * Attempt to allocate an extent minlen<=len<=maxlen starting from
200 * bitmap block bbno. If we don't get maxlen then use prod to trim
201 * the length, if given. Returns error; returns starting block in *rtblock.
202 * The lengths are all in rtextents.
204 STATIC int /* error */
205 xfs_rtallocate_extent_block(
206 xfs_mount_t *mp, /* file system mount point */
207 xfs_trans_t *tp, /* transaction pointer */
208 xfs_rtblock_t bbno, /* bitmap block number */
209 xfs_extlen_t minlen, /* minimum length to allocate */
210 xfs_extlen_t maxlen, /* maximum length to allocate */
211 xfs_extlen_t *len, /* out: actual length allocated */
212 xfs_rtblock_t *nextp, /* out: next block to try */
213 xfs_buf_t **rbpp, /* in/out: summary block buffer */
214 xfs_fsblock_t *rsb, /* in/out: summary block number */
215 xfs_extlen_t prod, /* extent product factor */
216 xfs_rtblock_t *rtblock) /* out: start block allocated */
218 xfs_rtblock_t besti; /* best rtblock found so far */
219 xfs_rtblock_t bestlen; /* best length found so far */
220 xfs_rtblock_t end; /* last rtblock in chunk */
221 int error; /* error value */
222 xfs_rtblock_t i; /* current rtblock trying */
223 xfs_rtblock_t next; /* next rtblock to try */
224 int stat; /* status from internal calls */
227 * Loop over all the extents starting in this bitmap block,
228 * looking for one that's long enough.
230 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
231 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
232 i <= end;
233 i++) {
235 * See if there's a free extent of maxlen starting at i.
236 * If it's not so then next will contain the first non-free.
238 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
239 if (error) {
240 return error;
242 if (stat) {
244 * i for maxlen is all free, allocate and return that.
246 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
247 rsb);
248 if (error) {
249 return error;
251 *len = maxlen;
252 *rtblock = i;
253 return 0;
256 * In the case where we have a variable-sized allocation
257 * request, figure out how big this free piece is,
258 * and if it's big enough for the minimum, and the best
259 * so far, remember it.
261 if (minlen < maxlen) {
262 xfs_rtblock_t thislen; /* this extent size */
264 thislen = next - i;
265 if (thislen >= minlen && thislen > bestlen) {
266 besti = i;
267 bestlen = thislen;
271 * If not done yet, find the start of the next free space.
273 if (next < end) {
274 error = xfs_rtfind_forw(mp, tp, next, end, &i);
275 if (error) {
276 return error;
278 } else
279 break;
282 * Searched the whole thing & didn't find a maxlen free extent.
284 if (minlen < maxlen && besti != -1) {
285 xfs_extlen_t p; /* amount to trim length by */
288 * If size should be a multiple of prod, make that so.
290 if (prod > 1 && (p = do_mod(bestlen, prod)))
291 bestlen -= p;
293 * Allocate besti for bestlen & return that.
295 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
296 if (error) {
297 return error;
299 *len = bestlen;
300 *rtblock = besti;
301 return 0;
304 * Allocation failed. Set *nextp to the next block to try.
306 *nextp = next;
307 *rtblock = NULLRTBLOCK;
308 return 0;
312 * Allocate an extent of length minlen<=len<=maxlen, starting at block
313 * bno. If we don't get maxlen then use prod to trim the length, if given.
314 * Returns error; returns starting block in *rtblock.
315 * The lengths are all in rtextents.
317 STATIC int /* error */
318 xfs_rtallocate_extent_exact(
319 xfs_mount_t *mp, /* file system mount point */
320 xfs_trans_t *tp, /* transaction pointer */
321 xfs_rtblock_t bno, /* starting block number to allocate */
322 xfs_extlen_t minlen, /* minimum length to allocate */
323 xfs_extlen_t maxlen, /* maximum length to allocate */
324 xfs_extlen_t *len, /* out: actual length allocated */
325 xfs_buf_t **rbpp, /* in/out: summary block buffer */
326 xfs_fsblock_t *rsb, /* in/out: summary block number */
327 xfs_extlen_t prod, /* extent product factor */
328 xfs_rtblock_t *rtblock) /* out: start block allocated */
330 int error; /* error value */
331 xfs_extlen_t i; /* extent length trimmed due to prod */
332 int isfree; /* extent is free */
333 xfs_rtblock_t next; /* next block to try (dummy) */
335 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
337 * Check if the range in question (for maxlen) is free.
339 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
340 if (error) {
341 return error;
343 if (isfree) {
345 * If it is, allocate it and return success.
347 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
348 if (error) {
349 return error;
351 *len = maxlen;
352 *rtblock = bno;
353 return 0;
356 * If not, allocate what there is, if it's at least minlen.
358 maxlen = next - bno;
359 if (maxlen < minlen) {
361 * Failed, return failure status.
363 *rtblock = NULLRTBLOCK;
364 return 0;
367 * Trim off tail of extent, if prod is specified.
369 if (prod > 1 && (i = maxlen % prod)) {
370 maxlen -= i;
371 if (maxlen < minlen) {
373 * Now we can't do it, return failure status.
375 *rtblock = NULLRTBLOCK;
376 return 0;
380 * Allocate what we can and return it.
382 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
383 if (error) {
384 return error;
386 *len = maxlen;
387 *rtblock = bno;
388 return 0;
392 * Allocate an extent of length minlen<=len<=maxlen, starting as near
393 * to bno as possible. If we don't get maxlen then use prod to trim
394 * the length, if given. The lengths are all in rtextents.
396 STATIC int /* error */
397 xfs_rtallocate_extent_near(
398 xfs_mount_t *mp, /* file system mount point */
399 xfs_trans_t *tp, /* transaction pointer */
400 xfs_rtblock_t bno, /* starting block number to allocate */
401 xfs_extlen_t minlen, /* minimum length to allocate */
402 xfs_extlen_t maxlen, /* maximum length to allocate */
403 xfs_extlen_t *len, /* out: actual length allocated */
404 xfs_buf_t **rbpp, /* in/out: summary block buffer */
405 xfs_fsblock_t *rsb, /* in/out: summary block number */
406 xfs_extlen_t prod, /* extent product factor */
407 xfs_rtblock_t *rtblock) /* out: start block allocated */
409 int any; /* any useful extents from summary */
410 xfs_rtblock_t bbno; /* bitmap block number */
411 int error; /* error value */
412 int i; /* bitmap block offset (loop control) */
413 int j; /* secondary loop control */
414 int log2len; /* log2 of minlen */
415 xfs_rtblock_t n; /* next block to try */
416 xfs_rtblock_t r; /* result block */
418 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
420 * If the block number given is off the end, silently set it to
421 * the last block.
423 if (bno >= mp->m_sb.sb_rextents)
424 bno = mp->m_sb.sb_rextents - 1;
426 * Try the exact allocation first.
428 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
429 rbpp, rsb, prod, &r);
430 if (error) {
431 return error;
434 * If the exact allocation worked, return that.
436 if (r != NULLRTBLOCK) {
437 *rtblock = r;
438 return 0;
440 bbno = XFS_BITTOBLOCK(mp, bno);
441 i = 0;
442 ASSERT(minlen != 0);
443 log2len = xfs_highbit32(minlen);
445 * Loop over all bitmap blocks (bbno + i is current block).
447 for (;;) {
449 * Get summary information of extents of all useful levels
450 * starting in this bitmap block.
452 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
453 bbno + i, rbpp, rsb, &any);
454 if (error) {
455 return error;
458 * If there are any useful extents starting here, try
459 * allocating one.
461 if (any) {
463 * On the positive side of the starting location.
465 if (i >= 0) {
467 * Try to allocate an extent starting in
468 * this block.
470 error = xfs_rtallocate_extent_block(mp, tp,
471 bbno + i, minlen, maxlen, len, &n, rbpp,
472 rsb, prod, &r);
473 if (error) {
474 return error;
477 * If it worked, return it.
479 if (r != NULLRTBLOCK) {
480 *rtblock = r;
481 return 0;
485 * On the negative side of the starting location.
487 else { /* i < 0 */
489 * Loop backwards through the bitmap blocks from
490 * the starting point-1 up to where we are now.
491 * There should be an extent which ends in this
492 * bitmap block and is long enough.
494 for (j = -1; j > i; j--) {
496 * Grab the summary information for
497 * this bitmap block.
499 error = xfs_rtany_summary(mp, tp,
500 log2len, mp->m_rsumlevels - 1,
501 bbno + j, rbpp, rsb, &any);
502 if (error) {
503 return error;
506 * If there's no extent given in the
507 * summary that means the extent we
508 * found must carry over from an
509 * earlier block. If there is an
510 * extent given, we've already tried
511 * that allocation, don't do it again.
513 if (any)
514 continue;
515 error = xfs_rtallocate_extent_block(mp,
516 tp, bbno + j, minlen, maxlen,
517 len, &n, rbpp, rsb, prod, &r);
518 if (error) {
519 return error;
522 * If it works, return the extent.
524 if (r != NULLRTBLOCK) {
525 *rtblock = r;
526 return 0;
530 * There weren't intervening bitmap blocks
531 * with a long enough extent, or the
532 * allocation didn't work for some reason
533 * (i.e. it's a little * too short).
534 * Try to allocate from the summary block
535 * that we found.
537 error = xfs_rtallocate_extent_block(mp, tp,
538 bbno + i, minlen, maxlen, len, &n, rbpp,
539 rsb, prod, &r);
540 if (error) {
541 return error;
544 * If it works, return the extent.
546 if (r != NULLRTBLOCK) {
547 *rtblock = r;
548 return 0;
553 * Loop control. If we were on the positive side, and there's
554 * still more blocks on the negative side, go there.
556 if (i > 0 && (int)bbno - i >= 0)
557 i = -i;
559 * If positive, and no more negative, but there are more
560 * positive, go there.
562 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
563 i++;
565 * If negative or 0 (just started), and there are positive
566 * blocks to go, go there. The 0 case moves to block 1.
568 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
569 i = 1 - i;
571 * If negative or 0 and there are more negative blocks,
572 * go there.
574 else if (i <= 0 && (int)bbno + i > 0)
575 i--;
577 * Must be done. Return failure.
579 else
580 break;
582 *rtblock = NULLRTBLOCK;
583 return 0;
587 * Allocate an extent of length minlen<=len<=maxlen, with no position
588 * specified. If we don't get maxlen then use prod to trim
589 * the length, if given. The lengths are all in rtextents.
591 STATIC int /* error */
592 xfs_rtallocate_extent_size(
593 xfs_mount_t *mp, /* file system mount point */
594 xfs_trans_t *tp, /* transaction pointer */
595 xfs_extlen_t minlen, /* minimum length to allocate */
596 xfs_extlen_t maxlen, /* maximum length to allocate */
597 xfs_extlen_t *len, /* out: actual length allocated */
598 xfs_buf_t **rbpp, /* in/out: summary block buffer */
599 xfs_fsblock_t *rsb, /* in/out: summary block number */
600 xfs_extlen_t prod, /* extent product factor */
601 xfs_rtblock_t *rtblock) /* out: start block allocated */
603 int error; /* error value */
604 int i; /* bitmap block number */
605 int l; /* level number (loop control) */
606 xfs_rtblock_t n; /* next block to be tried */
607 xfs_rtblock_t r; /* result block number */
608 xfs_suminfo_t sum; /* summary information for extents */
610 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
611 ASSERT(maxlen != 0);
614 * Loop over all the levels starting with maxlen.
615 * At each level, look at all the bitmap blocks, to see if there
616 * are extents starting there that are long enough (>= maxlen).
617 * Note, only on the initial level can the allocation fail if
618 * the summary says there's an extent.
620 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
622 * Loop over all the bitmap blocks.
624 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
626 * Get the summary for this level/block.
628 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
629 &sum);
630 if (error) {
631 return error;
634 * Nothing there, on to the next block.
636 if (!sum)
637 continue;
639 * Try allocating the extent.
641 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
642 maxlen, len, &n, rbpp, rsb, prod, &r);
643 if (error) {
644 return error;
647 * If it worked, return that.
649 if (r != NULLRTBLOCK) {
650 *rtblock = r;
651 return 0;
654 * If the "next block to try" returned from the
655 * allocator is beyond the next bitmap block,
656 * skip to that bitmap block.
658 if (XFS_BITTOBLOCK(mp, n) > i + 1)
659 i = XFS_BITTOBLOCK(mp, n) - 1;
663 * Didn't find any maxlen blocks. Try smaller ones, unless
664 * we're asking for a fixed size extent.
666 if (minlen > --maxlen) {
667 *rtblock = NULLRTBLOCK;
668 return 0;
670 ASSERT(minlen != 0);
671 ASSERT(maxlen != 0);
674 * Loop over sizes, from maxlen down to minlen.
675 * This time, when we do the allocations, allow smaller ones
676 * to succeed.
678 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
680 * Loop over all the bitmap blocks, try an allocation
681 * starting in that block.
683 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
685 * Get the summary information for this level/block.
687 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
688 &sum);
689 if (error) {
690 return error;
693 * If nothing there, go on to next.
695 if (!sum)
696 continue;
698 * Try the allocation. Make sure the specified
699 * minlen/maxlen are in the possible range for
700 * this summary level.
702 error = xfs_rtallocate_extent_block(mp, tp, i,
703 XFS_RTMAX(minlen, 1 << l),
704 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
705 len, &n, rbpp, rsb, prod, &r);
706 if (error) {
707 return error;
710 * If it worked, return that extent.
712 if (r != NULLRTBLOCK) {
713 *rtblock = r;
714 return 0;
717 * If the "next block to try" returned from the
718 * allocator is beyond the next bitmap block,
719 * skip to that bitmap block.
721 if (XFS_BITTOBLOCK(mp, n) > i + 1)
722 i = XFS_BITTOBLOCK(mp, n) - 1;
726 * Got nothing, return failure.
728 *rtblock = NULLRTBLOCK;
729 return 0;
733 * Mark an extent specified by start and len allocated.
734 * Updates all the summary information as well as the bitmap.
736 STATIC int /* error */
737 xfs_rtallocate_range(
738 xfs_mount_t *mp, /* file system mount point */
739 xfs_trans_t *tp, /* transaction pointer */
740 xfs_rtblock_t start, /* start block to allocate */
741 xfs_extlen_t len, /* length to allocate */
742 xfs_buf_t **rbpp, /* in/out: summary block buffer */
743 xfs_fsblock_t *rsb) /* in/out: summary block number */
745 xfs_rtblock_t end; /* end of the allocated extent */
746 int error; /* error value */
747 xfs_rtblock_t postblock; /* first block allocated > end */
748 xfs_rtblock_t preblock; /* first block allocated < start */
750 end = start + len - 1;
752 * Assume we're allocating out of the middle of a free extent.
753 * We need to find the beginning and end of the extent so we can
754 * properly update the summary.
756 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
757 if (error) {
758 return error;
761 * Find the next allocated block (end of free extent).
763 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
764 &postblock);
765 if (error) {
766 return error;
769 * Decrement the summary information corresponding to the entire
770 * (old) free extent.
772 error = xfs_rtmodify_summary(mp, tp,
773 XFS_RTBLOCKLOG(postblock + 1 - preblock),
774 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
775 if (error) {
776 return error;
779 * If there are blocks not being allocated at the front of the
780 * old extent, add summary data for them to be free.
782 if (preblock < start) {
783 error = xfs_rtmodify_summary(mp, tp,
784 XFS_RTBLOCKLOG(start - preblock),
785 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
786 if (error) {
787 return error;
791 * If there are blocks not being allocated at the end of the
792 * old extent, add summary data for them to be free.
794 if (postblock > end) {
795 error = xfs_rtmodify_summary(mp, tp,
796 XFS_RTBLOCKLOG(postblock - end),
797 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
798 if (error) {
799 return error;
803 * Modify the bitmap to mark this extent allocated.
805 error = xfs_rtmodify_range(mp, tp, start, len, 0);
806 return error;
810 * Return whether there are any free extents in the size range given
811 * by low and high, for the bitmap block bbno.
813 STATIC int /* error */
814 xfs_rtany_summary(
815 xfs_mount_t *mp, /* file system mount structure */
816 xfs_trans_t *tp, /* transaction pointer */
817 int low, /* low log2 extent size */
818 int high, /* high log2 extent size */
819 xfs_rtblock_t bbno, /* bitmap block number */
820 xfs_buf_t **rbpp, /* in/out: summary block buffer */
821 xfs_fsblock_t *rsb, /* in/out: summary block number */
822 int *stat) /* out: any good extents here? */
824 int error; /* error value */
825 int log; /* loop counter, log2 of ext. size */
826 xfs_suminfo_t sum; /* summary data */
829 * Loop over logs of extent sizes. Order is irrelevant.
831 for (log = low; log <= high; log++) {
833 * Get one summary datum.
835 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
836 if (error) {
837 return error;
840 * If there are any, return success.
842 if (sum) {
843 *stat = 1;
844 return 0;
848 * Found nothing, return failure.
850 *stat = 0;
851 return 0;
855 * Get a buffer for the bitmap or summary file block specified.
856 * The buffer is returned read and locked.
858 STATIC int /* error */
859 xfs_rtbuf_get(
860 xfs_mount_t *mp, /* file system mount structure */
861 xfs_trans_t *tp, /* transaction pointer */
862 xfs_rtblock_t block, /* block number in bitmap or summary */
863 int issum, /* is summary not bitmap */
864 xfs_buf_t **bpp) /* output: buffer for the block */
866 xfs_buf_t *bp; /* block buffer, result */
867 xfs_daddr_t d; /* disk addr of block */
868 int error; /* error value */
869 xfs_fsblock_t fsb; /* fs block number for block */
870 xfs_inode_t *ip; /* bitmap or summary inode */
872 ip = issum ? mp->m_rsumip : mp->m_rbmip;
874 * Map from the file offset (block) and inode number to the
875 * file system block.
877 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
878 if (error) {
879 return error;
881 ASSERT(fsb != NULLFSBLOCK);
883 * Convert to disk address for buffer cache.
885 d = XFS_FSB_TO_DADDR(mp, fsb);
887 * Read the buffer.
889 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
890 mp->m_bsize, 0, &bp);
891 if (error) {
892 return error;
894 ASSERT(bp && !XFS_BUF_GETERROR(bp));
895 *bpp = bp;
896 return 0;
899 #ifdef DEBUG
901 * Check that the given extent (block range) is allocated already.
903 STATIC int /* error */
904 xfs_rtcheck_alloc_range(
905 xfs_mount_t *mp, /* file system mount point */
906 xfs_trans_t *tp, /* transaction pointer */
907 xfs_rtblock_t bno, /* starting block number of extent */
908 xfs_extlen_t len, /* length of extent */
909 int *stat) /* out: 1 for allocated, 0 for not */
911 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
913 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
915 #endif
918 * Check that the given range is either all allocated (val = 0) or
919 * all free (val = 1).
921 STATIC int /* error */
922 xfs_rtcheck_range(
923 xfs_mount_t *mp, /* file system mount point */
924 xfs_trans_t *tp, /* transaction pointer */
925 xfs_rtblock_t start, /* starting block number of extent */
926 xfs_extlen_t len, /* length of extent */
927 int val, /* 1 for free, 0 for allocated */
928 xfs_rtblock_t *new, /* out: first block not matching */
929 int *stat) /* out: 1 for matches, 0 for not */
931 xfs_rtword_t *b; /* current word in buffer */
932 int bit; /* bit number in the word */
933 xfs_rtblock_t block; /* bitmap block number */
934 xfs_buf_t *bp; /* buf for the block */
935 xfs_rtword_t *bufp; /* starting word in buffer */
936 int error; /* error value */
937 xfs_rtblock_t i; /* current bit number rel. to start */
938 xfs_rtblock_t lastbit; /* last useful bit in word */
939 xfs_rtword_t mask; /* mask of relevant bits for value */
940 xfs_rtword_t wdiff; /* difference from wanted value */
941 int word; /* word number in the buffer */
944 * Compute starting bitmap block number
946 block = XFS_BITTOBLOCK(mp, start);
948 * Read the bitmap block.
950 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
951 if (error) {
952 return error;
954 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
956 * Compute the starting word's address, and starting bit.
958 word = XFS_BITTOWORD(mp, start);
959 b = &bufp[word];
960 bit = (int)(start & (XFS_NBWORD - 1));
962 * 0 (allocated) => all zero's; 1 (free) => all one's.
964 val = -val;
966 * If not starting on a word boundary, deal with the first
967 * (partial) word.
969 if (bit) {
971 * Compute first bit not examined.
973 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
975 * Mask of relevant bits.
977 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
979 * Compute difference between actual and desired value.
981 if ((wdiff = (*b ^ val) & mask)) {
983 * Different, compute first wrong bit and return.
985 xfs_trans_brelse(tp, bp);
986 i = XFS_RTLOBIT(wdiff) - bit;
987 *new = start + i;
988 *stat = 0;
989 return 0;
991 i = lastbit - bit;
993 * Go on to next block if that's where the next word is
994 * and we need the next word.
996 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
998 * If done with this block, get the next one.
1000 xfs_trans_brelse(tp, bp);
1001 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1002 if (error) {
1003 return error;
1005 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1006 word = 0;
1007 } else {
1009 * Go on to the next word in the buffer.
1011 b++;
1013 } else {
1015 * Starting on a word boundary, no partial word.
1017 i = 0;
1020 * Loop over whole words in buffers. When we use up one buffer
1021 * we move on to the next one.
1023 while (len - i >= XFS_NBWORD) {
1025 * Compute difference between actual and desired value.
1027 if ((wdiff = *b ^ val)) {
1029 * Different, compute first wrong bit and return.
1031 xfs_trans_brelse(tp, bp);
1032 i += XFS_RTLOBIT(wdiff);
1033 *new = start + i;
1034 *stat = 0;
1035 return 0;
1037 i += XFS_NBWORD;
1039 * Go on to next block if that's where the next word is
1040 * and we need the next word.
1042 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1044 * If done with this block, get the next one.
1046 xfs_trans_brelse(tp, bp);
1047 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1048 if (error) {
1049 return error;
1051 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1052 word = 0;
1053 } else {
1055 * Go on to the next word in the buffer.
1057 b++;
1061 * If not ending on a word boundary, deal with the last
1062 * (partial) word.
1064 if ((lastbit = len - i)) {
1066 * Mask of relevant bits.
1068 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1070 * Compute difference between actual and desired value.
1072 if ((wdiff = (*b ^ val) & mask)) {
1074 * Different, compute first wrong bit and return.
1076 xfs_trans_brelse(tp, bp);
1077 i += XFS_RTLOBIT(wdiff);
1078 *new = start + i;
1079 *stat = 0;
1080 return 0;
1081 } else
1082 i = len;
1085 * Successful, return.
1087 xfs_trans_brelse(tp, bp);
1088 *new = start + i;
1089 *stat = 1;
1090 return 0;
1094 * Copy and transform the summary file, given the old and new
1095 * parameters in the mount structures.
1097 STATIC int /* error */
1098 xfs_rtcopy_summary(
1099 xfs_mount_t *omp, /* old file system mount point */
1100 xfs_mount_t *nmp, /* new file system mount point */
1101 xfs_trans_t *tp) /* transaction pointer */
1103 xfs_rtblock_t bbno; /* bitmap block number */
1104 xfs_buf_t *bp; /* summary buffer */
1105 int error; /* error return value */
1106 int log; /* summary level number (log length) */
1107 xfs_suminfo_t sum; /* summary data */
1108 xfs_fsblock_t sumbno; /* summary block number */
1110 bp = NULL;
1111 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1112 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1113 (xfs_srtblock_t)bbno >= 0;
1114 bbno--) {
1115 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1116 &sumbno, &sum);
1117 if (error)
1118 return error;
1119 if (sum == 0)
1120 continue;
1121 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1122 &bp, &sumbno);
1123 if (error)
1124 return error;
1125 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1126 &bp, &sumbno);
1127 if (error)
1128 return error;
1129 ASSERT(sum > 0);
1132 return 0;
1136 * Searching backward from start to limit, find the first block whose
1137 * allocated/free state is different from start's.
1139 STATIC int /* error */
1140 xfs_rtfind_back(
1141 xfs_mount_t *mp, /* file system mount point */
1142 xfs_trans_t *tp, /* transaction pointer */
1143 xfs_rtblock_t start, /* starting block to look at */
1144 xfs_rtblock_t limit, /* last block to look at */
1145 xfs_rtblock_t *rtblock) /* out: start block found */
1147 xfs_rtword_t *b; /* current word in buffer */
1148 int bit; /* bit number in the word */
1149 xfs_rtblock_t block; /* bitmap block number */
1150 xfs_buf_t *bp; /* buf for the block */
1151 xfs_rtword_t *bufp; /* starting word in buffer */
1152 int error; /* error value */
1153 xfs_rtblock_t firstbit; /* first useful bit in the word */
1154 xfs_rtblock_t i; /* current bit number rel. to start */
1155 xfs_rtblock_t len; /* length of inspected area */
1156 xfs_rtword_t mask; /* mask of relevant bits for value */
1157 xfs_rtword_t want; /* mask for "good" values */
1158 xfs_rtword_t wdiff; /* difference from wanted value */
1159 int word; /* word number in the buffer */
1162 * Compute and read in starting bitmap block for starting block.
1164 block = XFS_BITTOBLOCK(mp, start);
1165 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1166 if (error) {
1167 return error;
1169 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1171 * Get the first word's index & point to it.
1173 word = XFS_BITTOWORD(mp, start);
1174 b = &bufp[word];
1175 bit = (int)(start & (XFS_NBWORD - 1));
1176 len = start - limit + 1;
1178 * Compute match value, based on the bit at start: if 1 (free)
1179 * then all-ones, else all-zeroes.
1181 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1183 * If the starting position is not word-aligned, deal with the
1184 * partial word.
1186 if (bit < XFS_NBWORD - 1) {
1188 * Calculate first (leftmost) bit number to look at,
1189 * and mask for all the relevant bits in this word.
1191 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1192 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1193 firstbit;
1195 * Calculate the difference between the value there
1196 * and what we're looking for.
1198 if ((wdiff = (*b ^ want) & mask)) {
1200 * Different. Mark where we are and return.
1202 xfs_trans_brelse(tp, bp);
1203 i = bit - XFS_RTHIBIT(wdiff);
1204 *rtblock = start - i + 1;
1205 return 0;
1207 i = bit - firstbit + 1;
1209 * Go on to previous block if that's where the previous word is
1210 * and we need the previous word.
1212 if (--word == -1 && i < len) {
1214 * If done with this block, get the previous one.
1216 xfs_trans_brelse(tp, bp);
1217 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1218 if (error) {
1219 return error;
1221 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1222 word = XFS_BLOCKWMASK(mp);
1223 b = &bufp[word];
1224 } else {
1226 * Go on to the previous word in the buffer.
1228 b--;
1230 } else {
1232 * Starting on a word boundary, no partial word.
1234 i = 0;
1237 * Loop over whole words in buffers. When we use up one buffer
1238 * we move on to the previous one.
1240 while (len - i >= XFS_NBWORD) {
1242 * Compute difference between actual and desired value.
1244 if ((wdiff = *b ^ want)) {
1246 * Different, mark where we are and return.
1248 xfs_trans_brelse(tp, bp);
1249 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1250 *rtblock = start - i + 1;
1251 return 0;
1253 i += XFS_NBWORD;
1255 * Go on to previous block if that's where the previous word is
1256 * and we need the previous word.
1258 if (--word == -1 && i < len) {
1260 * If done with this block, get the previous one.
1262 xfs_trans_brelse(tp, bp);
1263 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1264 if (error) {
1265 return error;
1267 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1268 word = XFS_BLOCKWMASK(mp);
1269 b = &bufp[word];
1270 } else {
1272 * Go on to the previous word in the buffer.
1274 b--;
1278 * If not ending on a word boundary, deal with the last
1279 * (partial) word.
1281 if (len - i) {
1283 * Calculate first (leftmost) bit number to look at,
1284 * and mask for all the relevant bits in this word.
1286 firstbit = XFS_NBWORD - (len - i);
1287 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1289 * Compute difference between actual and desired value.
1291 if ((wdiff = (*b ^ want) & mask)) {
1293 * Different, mark where we are and return.
1295 xfs_trans_brelse(tp, bp);
1296 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1297 *rtblock = start - i + 1;
1298 return 0;
1299 } else
1300 i = len;
1303 * No match, return that we scanned the whole area.
1305 xfs_trans_brelse(tp, bp);
1306 *rtblock = start - i + 1;
1307 return 0;
1311 * Searching forward from start to limit, find the first block whose
1312 * allocated/free state is different from start's.
1314 STATIC int /* error */
1315 xfs_rtfind_forw(
1316 xfs_mount_t *mp, /* file system mount point */
1317 xfs_trans_t *tp, /* transaction pointer */
1318 xfs_rtblock_t start, /* starting block to look at */
1319 xfs_rtblock_t limit, /* last block to look at */
1320 xfs_rtblock_t *rtblock) /* out: start block found */
1322 xfs_rtword_t *b; /* current word in buffer */
1323 int bit; /* bit number in the word */
1324 xfs_rtblock_t block; /* bitmap block number */
1325 xfs_buf_t *bp; /* buf for the block */
1326 xfs_rtword_t *bufp; /* starting word in buffer */
1327 int error; /* error value */
1328 xfs_rtblock_t i; /* current bit number rel. to start */
1329 xfs_rtblock_t lastbit; /* last useful bit in the word */
1330 xfs_rtblock_t len; /* length of inspected area */
1331 xfs_rtword_t mask; /* mask of relevant bits for value */
1332 xfs_rtword_t want; /* mask for "good" values */
1333 xfs_rtword_t wdiff; /* difference from wanted value */
1334 int word; /* word number in the buffer */
1337 * Compute and read in starting bitmap block for starting block.
1339 block = XFS_BITTOBLOCK(mp, start);
1340 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1341 if (error) {
1342 return error;
1344 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1346 * Get the first word's index & point to it.
1348 word = XFS_BITTOWORD(mp, start);
1349 b = &bufp[word];
1350 bit = (int)(start & (XFS_NBWORD - 1));
1351 len = limit - start + 1;
1353 * Compute match value, based on the bit at start: if 1 (free)
1354 * then all-ones, else all-zeroes.
1356 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1358 * If the starting position is not word-aligned, deal with the
1359 * partial word.
1361 if (bit) {
1363 * Calculate last (rightmost) bit number to look at,
1364 * and mask for all the relevant bits in this word.
1366 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1367 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1369 * Calculate the difference between the value there
1370 * and what we're looking for.
1372 if ((wdiff = (*b ^ want) & mask)) {
1374 * Different. Mark where we are and return.
1376 xfs_trans_brelse(tp, bp);
1377 i = XFS_RTLOBIT(wdiff) - bit;
1378 *rtblock = start + i - 1;
1379 return 0;
1381 i = lastbit - bit;
1383 * Go on to next block if that's where the next word is
1384 * and we need the next word.
1386 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1388 * If done with this block, get the previous one.
1390 xfs_trans_brelse(tp, bp);
1391 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1392 if (error) {
1393 return error;
1395 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1396 word = 0;
1397 } else {
1399 * Go on to the previous word in the buffer.
1401 b++;
1403 } else {
1405 * Starting on a word boundary, no partial word.
1407 i = 0;
1410 * Loop over whole words in buffers. When we use up one buffer
1411 * we move on to the next one.
1413 while (len - i >= XFS_NBWORD) {
1415 * Compute difference between actual and desired value.
1417 if ((wdiff = *b ^ want)) {
1419 * Different, mark where we are and return.
1421 xfs_trans_brelse(tp, bp);
1422 i += XFS_RTLOBIT(wdiff);
1423 *rtblock = start + i - 1;
1424 return 0;
1426 i += XFS_NBWORD;
1428 * Go on to next block if that's where the next word is
1429 * and we need the next word.
1431 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1433 * If done with this block, get the next one.
1435 xfs_trans_brelse(tp, bp);
1436 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1437 if (error) {
1438 return error;
1440 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1441 word = 0;
1442 } else {
1444 * Go on to the next word in the buffer.
1446 b++;
1450 * If not ending on a word boundary, deal with the last
1451 * (partial) word.
1453 if ((lastbit = len - i)) {
1455 * Calculate mask for all the relevant bits in this word.
1457 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1459 * Compute difference between actual and desired value.
1461 if ((wdiff = (*b ^ want) & mask)) {
1463 * Different, mark where we are and return.
1465 xfs_trans_brelse(tp, bp);
1466 i += XFS_RTLOBIT(wdiff);
1467 *rtblock = start + i - 1;
1468 return 0;
1469 } else
1470 i = len;
1473 * No match, return that we scanned the whole area.
1475 xfs_trans_brelse(tp, bp);
1476 *rtblock = start + i - 1;
1477 return 0;
1481 * Mark an extent specified by start and len freed.
1482 * Updates all the summary information as well as the bitmap.
1484 STATIC int /* error */
1485 xfs_rtfree_range(
1486 xfs_mount_t *mp, /* file system mount point */
1487 xfs_trans_t *tp, /* transaction pointer */
1488 xfs_rtblock_t start, /* starting block to free */
1489 xfs_extlen_t len, /* length to free */
1490 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1491 xfs_fsblock_t *rsb) /* in/out: summary block number */
1493 xfs_rtblock_t end; /* end of the freed extent */
1494 int error; /* error value */
1495 xfs_rtblock_t postblock; /* first block freed > end */
1496 xfs_rtblock_t preblock; /* first block freed < start */
1498 end = start + len - 1;
1500 * Modify the bitmap to mark this extent freed.
1502 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1503 if (error) {
1504 return error;
1507 * Assume we're freeing out of the middle of an allocated extent.
1508 * We need to find the beginning and end of the extent so we can
1509 * properly update the summary.
1511 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1512 if (error) {
1513 return error;
1516 * Find the next allocated block (end of allocated extent).
1518 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1519 &postblock);
1520 if (error)
1521 return error;
1523 * If there are blocks not being freed at the front of the
1524 * old extent, add summary data for them to be allocated.
1526 if (preblock < start) {
1527 error = xfs_rtmodify_summary(mp, tp,
1528 XFS_RTBLOCKLOG(start - preblock),
1529 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1530 if (error) {
1531 return error;
1535 * If there are blocks not being freed at the end of the
1536 * old extent, add summary data for them to be allocated.
1538 if (postblock > end) {
1539 error = xfs_rtmodify_summary(mp, tp,
1540 XFS_RTBLOCKLOG(postblock - end),
1541 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1542 if (error) {
1543 return error;
1547 * Increment the summary information corresponding to the entire
1548 * (new) free extent.
1550 error = xfs_rtmodify_summary(mp, tp,
1551 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1552 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1553 return error;
1557 * Read and return the summary information for a given extent size,
1558 * bitmap block combination.
1559 * Keeps track of a current summary block, so we don't keep reading
1560 * it from the buffer cache.
1562 STATIC int /* error */
1563 xfs_rtget_summary(
1564 xfs_mount_t *mp, /* file system mount structure */
1565 xfs_trans_t *tp, /* transaction pointer */
1566 int log, /* log2 of extent size */
1567 xfs_rtblock_t bbno, /* bitmap block number */
1568 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1569 xfs_fsblock_t *rsb, /* in/out: summary block number */
1570 xfs_suminfo_t *sum) /* out: summary info for this block */
1572 xfs_buf_t *bp; /* buffer for summary block */
1573 int error; /* error value */
1574 xfs_fsblock_t sb; /* summary fsblock */
1575 int so; /* index into the summary file */
1576 xfs_suminfo_t *sp; /* pointer to returned data */
1579 * Compute entry number in the summary file.
1581 so = XFS_SUMOFFS(mp, log, bbno);
1583 * Compute the block number in the summary file.
1585 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1587 * If we have an old buffer, and the block number matches, use that.
1589 if (rbpp && *rbpp && *rsb == sb)
1590 bp = *rbpp;
1592 * Otherwise we have to get the buffer.
1594 else {
1596 * If there was an old one, get rid of it first.
1598 if (rbpp && *rbpp)
1599 xfs_trans_brelse(tp, *rbpp);
1600 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1601 if (error) {
1602 return error;
1605 * Remember this buffer and block for the next call.
1607 if (rbpp) {
1608 *rbpp = bp;
1609 *rsb = sb;
1613 * Point to the summary information & copy it out.
1615 sp = XFS_SUMPTR(mp, bp, so);
1616 *sum = *sp;
1618 * Drop the buffer if we're not asked to remember it.
1620 if (!rbpp)
1621 xfs_trans_brelse(tp, bp);
1622 return 0;
1626 * Set the given range of bitmap bits to the given value.
1627 * Do whatever I/O and logging is required.
1629 STATIC int /* error */
1630 xfs_rtmodify_range(
1631 xfs_mount_t *mp, /* file system mount point */
1632 xfs_trans_t *tp, /* transaction pointer */
1633 xfs_rtblock_t start, /* starting block to modify */
1634 xfs_extlen_t len, /* length of extent to modify */
1635 int val) /* 1 for free, 0 for allocated */
1637 xfs_rtword_t *b; /* current word in buffer */
1638 int bit; /* bit number in the word */
1639 xfs_rtblock_t block; /* bitmap block number */
1640 xfs_buf_t *bp; /* buf for the block */
1641 xfs_rtword_t *bufp; /* starting word in buffer */
1642 int error; /* error value */
1643 xfs_rtword_t *first; /* first used word in the buffer */
1644 int i; /* current bit number rel. to start */
1645 int lastbit; /* last useful bit in word */
1646 xfs_rtword_t mask; /* mask o frelevant bits for value */
1647 int word; /* word number in the buffer */
1650 * Compute starting bitmap block number.
1652 block = XFS_BITTOBLOCK(mp, start);
1654 * Read the bitmap block, and point to its data.
1656 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1657 if (error) {
1658 return error;
1660 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1662 * Compute the starting word's address, and starting bit.
1664 word = XFS_BITTOWORD(mp, start);
1665 first = b = &bufp[word];
1666 bit = (int)(start & (XFS_NBWORD - 1));
1668 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1670 val = -val;
1672 * If not starting on a word boundary, deal with the first
1673 * (partial) word.
1675 if (bit) {
1677 * Compute first bit not changed and mask of relevant bits.
1679 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1680 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1682 * Set/clear the active bits.
1684 if (val)
1685 *b |= mask;
1686 else
1687 *b &= ~mask;
1688 i = lastbit - bit;
1690 * Go on to the next block if that's where the next word is
1691 * and we need the next word.
1693 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1695 * Log the changed part of this block.
1696 * Get the next one.
1698 xfs_trans_log_buf(tp, bp,
1699 (uint)((char *)first - (char *)bufp),
1700 (uint)((char *)b - (char *)bufp));
1701 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1702 if (error) {
1703 return error;
1705 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1706 word = 0;
1707 } else {
1709 * Go on to the next word in the buffer
1711 b++;
1713 } else {
1715 * Starting on a word boundary, no partial word.
1717 i = 0;
1720 * Loop over whole words in buffers. When we use up one buffer
1721 * we move on to the next one.
1723 while (len - i >= XFS_NBWORD) {
1725 * Set the word value correctly.
1727 *b = val;
1728 i += XFS_NBWORD;
1730 * Go on to the next block if that's where the next word is
1731 * and we need the next word.
1733 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1735 * Log the changed part of this block.
1736 * Get the next one.
1738 xfs_trans_log_buf(tp, bp,
1739 (uint)((char *)first - (char *)bufp),
1740 (uint)((char *)b - (char *)bufp));
1741 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1742 if (error) {
1743 return error;
1745 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1746 word = 0;
1747 } else {
1749 * Go on to the next word in the buffer
1751 b++;
1755 * If not ending on a word boundary, deal with the last
1756 * (partial) word.
1758 if ((lastbit = len - i)) {
1760 * Compute a mask of relevant bits.
1762 bit = 0;
1763 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1765 * Set/clear the active bits.
1767 if (val)
1768 *b |= mask;
1769 else
1770 *b &= ~mask;
1771 b++;
1774 * Log any remaining changed bytes.
1776 if (b > first)
1777 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1778 (uint)((char *)b - (char *)bufp - 1));
1779 return 0;
1783 * Read and modify the summary information for a given extent size,
1784 * bitmap block combination.
1785 * Keeps track of a current summary block, so we don't keep reading
1786 * it from the buffer cache.
1788 STATIC int /* error */
1789 xfs_rtmodify_summary(
1790 xfs_mount_t *mp, /* file system mount point */
1791 xfs_trans_t *tp, /* transaction pointer */
1792 int log, /* log2 of extent size */
1793 xfs_rtblock_t bbno, /* bitmap block number */
1794 int delta, /* change to make to summary info */
1795 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1796 xfs_fsblock_t *rsb) /* in/out: summary block number */
1798 xfs_buf_t *bp; /* buffer for the summary block */
1799 int error; /* error value */
1800 xfs_fsblock_t sb; /* summary fsblock */
1801 int so; /* index into the summary file */
1802 xfs_suminfo_t *sp; /* pointer to returned data */
1805 * Compute entry number in the summary file.
1807 so = XFS_SUMOFFS(mp, log, bbno);
1809 * Compute the block number in the summary file.
1811 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1813 * If we have an old buffer, and the block number matches, use that.
1815 if (rbpp && *rbpp && *rsb == sb)
1816 bp = *rbpp;
1818 * Otherwise we have to get the buffer.
1820 else {
1822 * If there was an old one, get rid of it first.
1824 if (rbpp && *rbpp)
1825 xfs_trans_brelse(tp, *rbpp);
1826 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1827 if (error) {
1828 return error;
1831 * Remember this buffer and block for the next call.
1833 if (rbpp) {
1834 *rbpp = bp;
1835 *rsb = sb;
1839 * Point to the summary information, modify and log it.
1841 sp = XFS_SUMPTR(mp, bp, so);
1842 *sp += delta;
1843 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1844 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1845 return 0;
1849 * Visible (exported) functions.
1853 * Grow the realtime area of the filesystem.
1856 xfs_growfs_rt(
1857 xfs_mount_t *mp, /* mount point for filesystem */
1858 xfs_growfs_rt_t *in) /* growfs rt input struct */
1860 xfs_rtblock_t bmbno; /* bitmap block number */
1861 xfs_buf_t *bp; /* temporary buffer */
1862 int error; /* error return value */
1863 xfs_inode_t *ip; /* bitmap inode, used as lock */
1864 xfs_mount_t *nmp; /* new (fake) mount structure */
1865 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1866 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1867 xfs_drtbno_t nrextents; /* new number of realtime extents */
1868 uint8_t nrextslog; /* new log2 of sb_rextents */
1869 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1870 uint nrsumlevels; /* new rt summary levels */
1871 uint nrsumsize; /* new size of rt summary, bytes */
1872 xfs_sb_t *nsbp; /* new superblock */
1873 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1874 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1875 xfs_sb_t *sbp; /* old superblock */
1876 xfs_fsblock_t sumbno; /* summary block number */
1878 sbp = &mp->m_sb;
1880 * Initial error checking.
1882 if (!capable(CAP_SYS_ADMIN))
1883 return XFS_ERROR(EPERM);
1884 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1885 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1886 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1887 return XFS_ERROR(EINVAL);
1888 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1889 return error;
1891 * Read in the last block of the device, make sure it exists.
1893 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1894 XFS_FSB_TO_BB(mp, nrblocks - 1),
1895 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1896 if (error)
1897 return error;
1898 ASSERT(bp);
1899 xfs_buf_relse(bp);
1901 * Calculate new parameters. These are the final values to be reached.
1903 nrextents = nrblocks;
1904 do_div(nrextents, in->extsize);
1905 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1906 nrextslog = xfs_highbit32(nrextents);
1907 nrsumlevels = nrextslog + 1;
1908 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1909 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1910 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1912 * New summary size can't be more than half the size of
1913 * the log. This prevents us from getting a log overflow,
1914 * since we'll log basically the whole summary file at once.
1916 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1917 return XFS_ERROR(EINVAL);
1919 * Get the old block counts for bitmap and summary inodes.
1920 * These can't change since other growfs callers are locked out.
1922 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1923 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1925 * Allocate space to the bitmap and summary files, as necessary.
1927 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1928 mp->m_sb.sb_rbmino)))
1929 return error;
1930 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1931 mp->m_sb.sb_rsumino)))
1932 return error;
1934 * Allocate a new (fake) mount/sb.
1936 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1938 * Loop over the bitmap blocks.
1939 * We will do everything one bitmap block at a time.
1940 * Skip the current block if it is exactly full.
1941 * This also deals with the case where there were no rtextents before.
1943 for (bmbno = sbp->sb_rbmblocks -
1944 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1945 bmbno < nrbmblocks;
1946 bmbno++) {
1947 xfs_trans_t *tp;
1948 int cancelflags = 0;
1950 *nmp = *mp;
1951 nsbp = &nmp->m_sb;
1953 * Calculate new sb and mount fields for this round.
1955 nsbp->sb_rextsize = in->extsize;
1956 nsbp->sb_rbmblocks = bmbno + 1;
1957 nsbp->sb_rblocks =
1958 XFS_RTMIN(nrblocks,
1959 nsbp->sb_rbmblocks * NBBY *
1960 nsbp->sb_blocksize * nsbp->sb_rextsize);
1961 nsbp->sb_rextents = nsbp->sb_rblocks;
1962 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1963 ASSERT(nsbp->sb_rextents != 0);
1964 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1965 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1966 nrsumsize =
1967 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1968 nsbp->sb_rbmblocks;
1969 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1970 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1972 * Start a transaction, get the log reservation.
1974 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1975 if ((error = xfs_trans_reserve(tp, 0,
1976 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
1977 goto error_cancel;
1979 * Lock out other callers by grabbing the bitmap inode lock.
1981 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
1982 XFS_ILOCK_EXCL, &ip)))
1983 goto error_cancel;
1984 ASSERT(ip == mp->m_rbmip);
1986 * Update the bitmap inode's size.
1988 mp->m_rbmip->i_d.di_size =
1989 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1990 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1991 cancelflags |= XFS_TRANS_ABORT;
1993 * Get the summary inode into the transaction.
1995 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
1996 XFS_ILOCK_EXCL, &ip)))
1997 goto error_cancel;
1998 ASSERT(ip == mp->m_rsumip);
2000 * Update the summary inode's size.
2002 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2003 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2005 * Copy summary data from old to new sizes.
2006 * Do this when the real size (not block-aligned) changes.
2008 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2009 mp->m_rsumlevels != nmp->m_rsumlevels) {
2010 error = xfs_rtcopy_summary(mp, nmp, tp);
2011 if (error)
2012 goto error_cancel;
2015 * Update superblock fields.
2017 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2018 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2019 nsbp->sb_rextsize - sbp->sb_rextsize);
2020 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2021 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2022 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2023 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2024 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2025 nsbp->sb_rblocks - sbp->sb_rblocks);
2026 if (nsbp->sb_rextents != sbp->sb_rextents)
2027 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2028 nsbp->sb_rextents - sbp->sb_rextents);
2029 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2030 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2031 nsbp->sb_rextslog - sbp->sb_rextslog);
2033 * Free new extent.
2035 bp = NULL;
2036 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2037 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2038 if (error) {
2039 error_cancel:
2040 xfs_trans_cancel(tp, cancelflags);
2041 break;
2044 * Mark more blocks free in the superblock.
2046 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2047 nsbp->sb_rextents - sbp->sb_rextents);
2049 * Update mp values into the real mp structure.
2051 mp->m_rsumlevels = nrsumlevels;
2052 mp->m_rsumsize = nrsumsize;
2054 error = xfs_trans_commit(tp, 0);
2055 if (error)
2056 break;
2060 * Free the fake mp structure.
2062 kmem_free(nmp);
2064 return error;
2068 * Allocate an extent in the realtime subvolume, with the usual allocation
2069 * parameters. The length units are all in realtime extents, as is the
2070 * result block number.
2072 int /* error */
2073 xfs_rtallocate_extent(
2074 xfs_trans_t *tp, /* transaction pointer */
2075 xfs_rtblock_t bno, /* starting block number to allocate */
2076 xfs_extlen_t minlen, /* minimum length to allocate */
2077 xfs_extlen_t maxlen, /* maximum length to allocate */
2078 xfs_extlen_t *len, /* out: actual length allocated */
2079 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2080 int wasdel, /* was a delayed allocation extent */
2081 xfs_extlen_t prod, /* extent product factor */
2082 xfs_rtblock_t *rtblock) /* out: start block allocated */
2084 int error; /* error value */
2085 xfs_inode_t *ip; /* inode for bitmap file */
2086 xfs_mount_t *mp; /* file system mount structure */
2087 xfs_rtblock_t r; /* result allocated block */
2088 xfs_fsblock_t sb; /* summary file block number */
2089 xfs_buf_t *sumbp; /* summary file block buffer */
2091 ASSERT(minlen > 0 && minlen <= maxlen);
2092 mp = tp->t_mountp;
2094 * If prod is set then figure out what to do to minlen and maxlen.
2096 if (prod > 1) {
2097 xfs_extlen_t i;
2099 if ((i = maxlen % prod))
2100 maxlen -= i;
2101 if ((i = minlen % prod))
2102 minlen += prod - i;
2103 if (maxlen < minlen) {
2104 *rtblock = NULLRTBLOCK;
2105 return 0;
2109 * Lock out other callers by grabbing the bitmap inode lock.
2111 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2112 XFS_ILOCK_EXCL, &ip)))
2113 return error;
2114 sumbp = NULL;
2116 * Allocate by size, or near another block, or exactly at some block.
2118 switch (type) {
2119 case XFS_ALLOCTYPE_ANY_AG:
2120 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2121 &sumbp, &sb, prod, &r);
2122 break;
2123 case XFS_ALLOCTYPE_NEAR_BNO:
2124 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2125 len, &sumbp, &sb, prod, &r);
2126 break;
2127 case XFS_ALLOCTYPE_THIS_BNO:
2128 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2129 len, &sumbp, &sb, prod, &r);
2130 break;
2131 default:
2132 ASSERT(0);
2134 if (error) {
2135 return error;
2138 * If it worked, update the superblock.
2140 if (r != NULLRTBLOCK) {
2141 long slen = (long)*len;
2143 ASSERT(*len >= minlen && *len <= maxlen);
2144 if (wasdel)
2145 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2146 else
2147 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2149 *rtblock = r;
2150 return 0;
2154 * Free an extent in the realtime subvolume. Length is expressed in
2155 * realtime extents, as is the block number.
2157 int /* error */
2158 xfs_rtfree_extent(
2159 xfs_trans_t *tp, /* transaction pointer */
2160 xfs_rtblock_t bno, /* starting block number to free */
2161 xfs_extlen_t len) /* length of extent freed */
2163 int error; /* error value */
2164 xfs_inode_t *ip; /* bitmap file inode */
2165 xfs_mount_t *mp; /* file system mount structure */
2166 xfs_fsblock_t sb; /* summary file block number */
2167 xfs_buf_t *sumbp; /* summary file block buffer */
2169 mp = tp->t_mountp;
2171 * Synchronize by locking the bitmap inode.
2173 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2174 XFS_ILOCK_EXCL, &ip)))
2175 return error;
2176 #if defined(__KERNEL__) && defined(DEBUG)
2178 * Check to see that this whole range is currently allocated.
2181 int stat; /* result from checking range */
2183 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2184 if (error) {
2185 return error;
2187 ASSERT(stat);
2189 #endif
2190 sumbp = NULL;
2192 * Free the range of realtime blocks.
2194 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2195 if (error) {
2196 return error;
2199 * Mark more blocks free in the superblock.
2201 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2203 * If we've now freed all the blocks, reset the file sequence
2204 * number to 0.
2206 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2207 mp->m_sb.sb_rextents) {
2208 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2209 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2210 *(__uint64_t *)&ip->i_d.di_atime = 0;
2211 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2213 return 0;
2217 * Initialize realtime fields in the mount structure.
2219 int /* error */
2220 xfs_rtmount_init(
2221 xfs_mount_t *mp) /* file system mount structure */
2223 xfs_buf_t *bp; /* buffer for last block of subvolume */
2224 xfs_daddr_t d; /* address of last block of subvolume */
2225 int error; /* error return value */
2226 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2228 sbp = &mp->m_sb;
2229 if (sbp->sb_rblocks == 0)
2230 return 0;
2231 if (mp->m_rtdev_targp == NULL) {
2232 cmn_err(CE_WARN,
2233 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2234 return XFS_ERROR(ENODEV);
2236 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2237 mp->m_rsumsize =
2238 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2239 sbp->sb_rbmblocks;
2240 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2241 mp->m_rbmip = mp->m_rsumip = NULL;
2243 * Check that the realtime section is an ok size.
2245 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2246 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2247 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2248 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2249 (unsigned long long) mp->m_sb.sb_rblocks);
2250 return XFS_ERROR(E2BIG);
2252 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2253 d - XFS_FSB_TO_BB(mp, 1),
2254 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2255 if (error) {
2256 cmn_err(CE_WARN,
2257 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2258 if (error == ENOSPC)
2259 return XFS_ERROR(E2BIG);
2260 return error;
2262 xfs_buf_relse(bp);
2263 return 0;
2267 * Get the bitmap and summary inodes into the mount structure
2268 * at mount time.
2270 int /* error */
2271 xfs_rtmount_inodes(
2272 xfs_mount_t *mp) /* file system mount structure */
2274 int error; /* error return value */
2275 xfs_sb_t *sbp;
2277 sbp = &mp->m_sb;
2278 if (sbp->sb_rbmino == NULLFSINO)
2279 return 0;
2280 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2281 if (error)
2282 return error;
2283 ASSERT(mp->m_rbmip != NULL);
2284 ASSERT(sbp->sb_rsumino != NULLFSINO);
2285 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2286 if (error) {
2287 IRELE(mp->m_rbmip);
2288 return error;
2290 ASSERT(mp->m_rsumip != NULL);
2291 return 0;
2294 void
2295 xfs_rtunmount_inodes(
2296 struct xfs_mount *mp)
2298 if (mp->m_rbmip)
2299 IRELE(mp->m_rbmip);
2300 if (mp->m_rsumip)
2301 IRELE(mp->m_rsumip);
2305 * Pick an extent for allocation at the start of a new realtime file.
2306 * Use the sequence number stored in the atime field of the bitmap inode.
2307 * Translate this to a fraction of the rtextents, and return the product
2308 * of rtextents and the fraction.
2309 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2311 int /* error */
2312 xfs_rtpick_extent(
2313 xfs_mount_t *mp, /* file system mount point */
2314 xfs_trans_t *tp, /* transaction pointer */
2315 xfs_extlen_t len, /* allocation length (rtextents) */
2316 xfs_rtblock_t *pick) /* result rt extent */
2318 xfs_rtblock_t b; /* result block */
2319 int error; /* error return value */
2320 xfs_inode_t *ip; /* bitmap incore inode */
2321 int log2; /* log of sequence number */
2322 __uint64_t resid; /* residual after log removed */
2323 __uint64_t seq; /* sequence number of file creation */
2324 __uint64_t *seqp; /* pointer to seqno in inode */
2326 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
2327 XFS_ILOCK_EXCL, &ip)))
2328 return error;
2329 ASSERT(ip == mp->m_rbmip);
2330 seqp = (__uint64_t *)&ip->i_d.di_atime;
2331 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2332 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2333 *seqp = 0;
2335 seq = *seqp;
2336 if ((log2 = xfs_highbit64(seq)) == -1)
2337 b = 0;
2338 else {
2339 resid = seq - (1ULL << log2);
2340 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2341 (log2 + 1);
2342 if (b >= mp->m_sb.sb_rextents)
2343 b = do_mod(b, mp->m_sb.sb_rextents);
2344 if (b + len > mp->m_sb.sb_rextents)
2345 b = mp->m_sb.sb_rextents - len;
2347 *seqp = seq + 1;
2348 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2349 *pick = b;
2350 return 0;