]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_rtalloc.c
[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[net-next-2.6.git] / fs / xfs / xfs_rtalloc.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
1da177e4 32#include "xfs.h"
a844f451 33#include "xfs_fs.h"
1da177e4 34#include "xfs_types.h"
a844f451 35#include "xfs_bit.h"
1da177e4 36#include "xfs_log.h"
a844f451 37#include "xfs_inum.h"
1da177e4
LT
38#include "xfs_trans.h"
39#include "xfs_sb.h"
40#include "xfs_ag.h"
41#include "xfs_dir.h"
42#include "xfs_dir2.h"
43#include "xfs_dmapi.h"
44#include "xfs_mount.h"
1da177e4 45#include "xfs_bmap_btree.h"
a844f451 46#include "xfs_alloc_btree.h"
1da177e4 47#include "xfs_ialloc_btree.h"
1da177e4
LT
48#include "xfs_dir_sf.h"
49#include "xfs_dir2_sf.h"
a844f451 50#include "xfs_attr_sf.h"
1da177e4
LT
51#include "xfs_dinode.h"
52#include "xfs_inode.h"
a844f451
NS
53#include "xfs_btree.h"
54#include "xfs_ialloc.h"
1da177e4
LT
55#include "xfs_alloc.h"
56#include "xfs_bmap.h"
1da177e4
LT
57#include "xfs_rtalloc.h"
58#include "xfs_fsops.h"
59#include "xfs_error.h"
60#include "xfs_rw.h"
61#include "xfs_inode_item.h"
62#include "xfs_trans_space.h"
63
64
65/*
66 * Prototypes for internal functions.
67 */
68
69
70STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
71 xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
72STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
73 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
74STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
75 xfs_extlen_t, int, xfs_rtblock_t *, int *);
76STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
77 xfs_rtblock_t, xfs_rtblock_t *);
78STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
79 xfs_rtblock_t, xfs_rtblock_t *);
80STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
81 xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
82STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
83 xfs_extlen_t, int);
84STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
85 xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
86
87/*
88 * Internal functions.
89 */
90
91/*
92 * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
93 */
94STATIC int
95xfs_lowbit32(
96 __uint32_t v)
97{
98 if (v)
99 return ffs(v) - 1;
100 return -1;
101}
102
103/*
104 * Allocate space to the bitmap or summary file, and zero it, for growfs.
105 */
106STATIC int /* error */
107xfs_growfs_rt_alloc(
108 xfs_mount_t *mp, /* file system mount point */
109 xfs_extlen_t oblocks, /* old count of blocks */
110 xfs_extlen_t nblocks, /* new count of blocks */
111 xfs_ino_t ino) /* inode number (bitmap/summary) */
112{
113 xfs_fileoff_t bno; /* block number in file */
114 xfs_buf_t *bp; /* temporary buffer for zeroing */
115 int cancelflags; /* flags for xfs_trans_cancel */
116 int committed; /* transaction committed flag */
117 xfs_daddr_t d; /* disk block address */
118 int error; /* error return value */
119 xfs_fsblock_t firstblock; /* first block allocated in xaction */
120 xfs_bmap_free_t flist; /* list of freed blocks */
121 xfs_fsblock_t fsbno; /* filesystem block for bno */
122 xfs_inode_t *ip; /* pointer to incore inode */
123 xfs_bmbt_irec_t map; /* block map output */
124 int nmap; /* number of block maps */
125 int resblks; /* space reservation */
126 xfs_trans_t *tp; /* transaction pointer */
127
128 /*
129 * Allocate space to the file, as necessary.
130 */
131 while (oblocks < nblocks) {
132 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
133 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
134 cancelflags = 0;
135 /*
136 * Reserve space & log for one extent added to the file.
137 */
138 if ((error = xfs_trans_reserve(tp, resblks,
139 XFS_GROWRTALLOC_LOG_RES(mp), 0,
140 XFS_TRANS_PERM_LOG_RES,
141 XFS_DEFAULT_PERM_LOG_COUNT)))
142 goto error_exit;
143 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
144 /*
145 * Lock the inode.
146 */
147 if ((error = xfs_trans_iget(mp, tp, ino, 0, XFS_ILOCK_EXCL, &ip)))
148 goto error_exit;
149 XFS_BMAP_INIT(&flist, &firstblock);
150 /*
151 * Allocate blocks to the bitmap file.
152 */
153 nmap = 1;
154 cancelflags |= XFS_TRANS_ABORT;
155 error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
156 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
157 resblks, &map, &nmap, &flist);
158 if (!error && nmap < 1)
159 error = XFS_ERROR(ENOSPC);
160 if (error)
161 goto error_exit;
162 /*
163 * Free any blocks freed up in the transaction, then commit.
164 */
165 error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
166 if (error)
167 goto error_exit;
168 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
169 /*
170 * Now we need to clear the allocated blocks.
171 * Do this one block per transaction, to keep it simple.
172 */
173 cancelflags = 0;
174 for (bno = map.br_startoff, fsbno = map.br_startblock;
175 bno < map.br_startoff + map.br_blockcount;
176 bno++, fsbno++) {
177 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
178 /*
179 * Reserve log for one block zeroing.
180 */
181 if ((error = xfs_trans_reserve(tp, 0,
182 XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
183 goto error_exit;
184 /*
185 * Lock the bitmap inode.
186 */
187 if ((error = xfs_trans_iget(mp, tp, ino, 0, XFS_ILOCK_EXCL,
188 &ip)))
189 goto error_exit;
190 /*
191 * Get a buffer for the block.
192 */
193 d = XFS_FSB_TO_DADDR(mp, fsbno);
194 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
195 mp->m_bsize, 0);
196 if (bp == NULL) {
197 error = XFS_ERROR(EIO);
198 goto error_exit;
199 }
200 memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
201 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
202 /*
203 * Commit the transaction.
204 */
205 xfs_trans_commit(tp, 0, NULL);
206 }
207 /*
208 * Go on to the next extent, if any.
209 */
210 oblocks = map.br_startoff + map.br_blockcount;
211 }
212 return 0;
213error_exit:
214 xfs_trans_cancel(tp, cancelflags);
215 return error;
216}
217
218/*
219 * Attempt to allocate an extent minlen<=len<=maxlen starting from
220 * bitmap block bbno. If we don't get maxlen then use prod to trim
221 * the length, if given. Returns error; returns starting block in *rtblock.
222 * The lengths are all in rtextents.
223 */
224STATIC int /* error */
225xfs_rtallocate_extent_block(
226 xfs_mount_t *mp, /* file system mount point */
227 xfs_trans_t *tp, /* transaction pointer */
228 xfs_rtblock_t bbno, /* bitmap block number */
229 xfs_extlen_t minlen, /* minimum length to allocate */
230 xfs_extlen_t maxlen, /* maximum length to allocate */
231 xfs_extlen_t *len, /* out: actual length allocated */
232 xfs_rtblock_t *nextp, /* out: next block to try */
233 xfs_buf_t **rbpp, /* in/out: summary block buffer */
234 xfs_fsblock_t *rsb, /* in/out: summary block number */
235 xfs_extlen_t prod, /* extent product factor */
236 xfs_rtblock_t *rtblock) /* out: start block allocated */
237{
238 xfs_rtblock_t besti; /* best rtblock found so far */
239 xfs_rtblock_t bestlen; /* best length found so far */
240 xfs_rtblock_t end; /* last rtblock in chunk */
241 int error; /* error value */
242 xfs_rtblock_t i; /* current rtblock trying */
243 xfs_rtblock_t next; /* next rtblock to try */
244 int stat; /* status from internal calls */
245
246 /*
247 * Loop over all the extents starting in this bitmap block,
248 * looking for one that's long enough.
249 */
250 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
251 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
252 i <= end;
253 i++) {
254 /*
255 * See if there's a free extent of maxlen starting at i.
256 * If it's not so then next will contain the first non-free.
257 */
258 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
259 if (error) {
260 return error;
261 }
262 if (stat) {
263 /*
264 * i for maxlen is all free, allocate and return that.
265 */
266 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
267 rsb);
268 if (error) {
269 return error;
270 }
271 *len = maxlen;
272 *rtblock = i;
273 return 0;
274 }
275 /*
276 * In the case where we have a variable-sized allocation
277 * request, figure out how big this free piece is,
278 * and if it's big enough for the minimum, and the best
279 * so far, remember it.
280 */
281 if (minlen < maxlen) {
282 xfs_rtblock_t thislen; /* this extent size */
283
284 thislen = next - i;
285 if (thislen >= minlen && thislen > bestlen) {
286 besti = i;
287 bestlen = thislen;
288 }
289 }
290 /*
291 * If not done yet, find the start of the next free space.
292 */
293 if (next < end) {
294 error = xfs_rtfind_forw(mp, tp, next, end, &i);
295 if (error) {
296 return error;
297 }
298 } else
299 break;
300 }
301 /*
302 * Searched the whole thing & didn't find a maxlen free extent.
303 */
304 if (minlen < maxlen && besti != -1) {
305 xfs_extlen_t p; /* amount to trim length by */
306
307 /*
308 * If size should be a multiple of prod, make that so.
309 */
310 if (prod > 1 && (p = do_mod(bestlen, prod)))
311 bestlen -= p;
312 /*
313 * Allocate besti for bestlen & return that.
314 */
315 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
316 if (error) {
317 return error;
318 }
319 *len = bestlen;
320 *rtblock = besti;
321 return 0;
322 }
323 /*
324 * Allocation failed. Set *nextp to the next block to try.
325 */
326 *nextp = next;
327 *rtblock = NULLRTBLOCK;
328 return 0;
329}
330
331/*
332 * Allocate an extent of length minlen<=len<=maxlen, starting at block
333 * bno. If we don't get maxlen then use prod to trim the length, if given.
334 * Returns error; returns starting block in *rtblock.
335 * The lengths are all in rtextents.
336 */
337STATIC int /* error */
338xfs_rtallocate_extent_exact(
339 xfs_mount_t *mp, /* file system mount point */
340 xfs_trans_t *tp, /* transaction pointer */
341 xfs_rtblock_t bno, /* starting block number to allocate */
342 xfs_extlen_t minlen, /* minimum length to allocate */
343 xfs_extlen_t maxlen, /* maximum length to allocate */
344 xfs_extlen_t *len, /* out: actual length allocated */
345 xfs_buf_t **rbpp, /* in/out: summary block buffer */
346 xfs_fsblock_t *rsb, /* in/out: summary block number */
347 xfs_extlen_t prod, /* extent product factor */
348 xfs_rtblock_t *rtblock) /* out: start block allocated */
349{
350 int error; /* error value */
351 xfs_extlen_t i; /* extent length trimmed due to prod */
352 int isfree; /* extent is free */
353 xfs_rtblock_t next; /* next block to try (dummy) */
354
355 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
356 /*
357 * Check if the range in question (for maxlen) is free.
358 */
359 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
360 if (error) {
361 return error;
362 }
363 if (isfree) {
364 /*
365 * If it is, allocate it and return success.
366 */
367 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
368 if (error) {
369 return error;
370 }
371 *len = maxlen;
372 *rtblock = bno;
373 return 0;
374 }
375 /*
376 * If not, allocate what there is, if it's at least minlen.
377 */
378 maxlen = next - bno;
379 if (maxlen < minlen) {
380 /*
381 * Failed, return failure status.
382 */
383 *rtblock = NULLRTBLOCK;
384 return 0;
385 }
386 /*
387 * Trim off tail of extent, if prod is specified.
388 */
389 if (prod > 1 && (i = maxlen % prod)) {
390 maxlen -= i;
391 if (maxlen < minlen) {
392 /*
393 * Now we can't do it, return failure status.
394 */
395 *rtblock = NULLRTBLOCK;
396 return 0;
397 }
398 }
399 /*
400 * Allocate what we can and return it.
401 */
402 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
403 if (error) {
404 return error;
405 }
406 *len = maxlen;
407 *rtblock = bno;
408 return 0;
409}
410
411/*
412 * Allocate an extent of length minlen<=len<=maxlen, starting as near
413 * to bno as possible. If we don't get maxlen then use prod to trim
414 * the length, if given. The lengths are all in rtextents.
415 */
416STATIC int /* error */
417xfs_rtallocate_extent_near(
418 xfs_mount_t *mp, /* file system mount point */
419 xfs_trans_t *tp, /* transaction pointer */
420 xfs_rtblock_t bno, /* starting block number to allocate */
421 xfs_extlen_t minlen, /* minimum length to allocate */
422 xfs_extlen_t maxlen, /* maximum length to allocate */
423 xfs_extlen_t *len, /* out: actual length allocated */
424 xfs_buf_t **rbpp, /* in/out: summary block buffer */
425 xfs_fsblock_t *rsb, /* in/out: summary block number */
426 xfs_extlen_t prod, /* extent product factor */
427 xfs_rtblock_t *rtblock) /* out: start block allocated */
428{
429 int any; /* any useful extents from summary */
430 xfs_rtblock_t bbno; /* bitmap block number */
431 int error; /* error value */
432 int i; /* bitmap block offset (loop control) */
433 int j; /* secondary loop control */
434 int log2len; /* log2 of minlen */
435 xfs_rtblock_t n; /* next block to try */
436 xfs_rtblock_t r; /* result block */
437
438 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
439 /*
440 * If the block number given is off the end, silently set it to
441 * the last block.
442 */
443 if (bno >= mp->m_sb.sb_rextents)
444 bno = mp->m_sb.sb_rextents - 1;
445 /*
446 * Try the exact allocation first.
447 */
448 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
449 rbpp, rsb, prod, &r);
450 if (error) {
451 return error;
452 }
453 /*
454 * If the exact allocation worked, return that.
455 */
456 if (r != NULLRTBLOCK) {
457 *rtblock = r;
458 return 0;
459 }
460 bbno = XFS_BITTOBLOCK(mp, bno);
461 i = 0;
462 log2len = xfs_highbit32(minlen);
463 /*
464 * Loop over all bitmap blocks (bbno + i is current block).
465 */
466 for (;;) {
467 /*
468 * Get summary information of extents of all useful levels
469 * starting in this bitmap block.
470 */
471 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
472 bbno + i, rbpp, rsb, &any);
473 if (error) {
474 return error;
475 }
476 /*
477 * If there are any useful extents starting here, try
478 * allocating one.
479 */
480 if (any) {
481 /*
482 * On the positive side of the starting location.
483 */
484 if (i >= 0) {
485 /*
486 * Try to allocate an extent starting in
487 * this block.
488 */
489 error = xfs_rtallocate_extent_block(mp, tp,
490 bbno + i, minlen, maxlen, len, &n, rbpp,
491 rsb, prod, &r);
492 if (error) {
493 return error;
494 }
495 /*
496 * If it worked, return it.
497 */
498 if (r != NULLRTBLOCK) {
499 *rtblock = r;
500 return 0;
501 }
502 }
503 /*
504 * On the negative side of the starting location.
505 */
506 else { /* i < 0 */
507 /*
508 * Loop backwards through the bitmap blocks from
509 * the starting point-1 up to where we are now.
510 * There should be an extent which ends in this
511 * bitmap block and is long enough.
512 */
513 for (j = -1; j > i; j--) {
514 /*
515 * Grab the summary information for
516 * this bitmap block.
517 */
518 error = xfs_rtany_summary(mp, tp,
519 log2len, mp->m_rsumlevels - 1,
520 bbno + j, rbpp, rsb, &any);
521 if (error) {
522 return error;
523 }
524 /*
525 * If there's no extent given in the
526 * summary that means the extent we
527 * found must carry over from an
528 * earlier block. If there is an
529 * extent given, we've already tried
530 * that allocation, don't do it again.
531 */
532 if (any)
533 continue;
534 error = xfs_rtallocate_extent_block(mp,
535 tp, bbno + j, minlen, maxlen,
536 len, &n, rbpp, rsb, prod, &r);
537 if (error) {
538 return error;
539 }
540 /*
541 * If it works, return the extent.
542 */
543 if (r != NULLRTBLOCK) {
544 *rtblock = r;
545 return 0;
546 }
547 }
548 /*
549 * There weren't intervening bitmap blocks
550 * with a long enough extent, or the
551 * allocation didn't work for some reason
552 * (i.e. it's a little * too short).
553 * Try to allocate from the summary block
554 * that we found.
555 */
556 error = xfs_rtallocate_extent_block(mp, tp,
557 bbno + i, minlen, maxlen, len, &n, rbpp,
558 rsb, prod, &r);
559 if (error) {
560 return error;
561 }
562 /*
563 * If it works, return the extent.
564 */
565 if (r != NULLRTBLOCK) {
566 *rtblock = r;
567 return 0;
568 }
569 }
570 }
571 /*
572 * Loop control. If we were on the positive side, and there's
573 * still more blocks on the negative side, go there.
574 */
575 if (i > 0 && (int)bbno - i >= 0)
576 i = -i;
577 /*
578 * If positive, and no more negative, but there are more
579 * positive, go there.
580 */
581 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
582 i++;
583 /*
584 * If negative or 0 (just started), and there are positive
585 * blocks to go, go there. The 0 case moves to block 1.
586 */
587 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
588 i = 1 - i;
589 /*
590 * If negative or 0 and there are more negative blocks,
591 * go there.
592 */
593 else if (i <= 0 && (int)bbno + i > 0)
594 i--;
595 /*
596 * Must be done. Return failure.
597 */
598 else
599 break;
600 }
601 *rtblock = NULLRTBLOCK;
602 return 0;
603}
604
605/*
606 * Allocate an extent of length minlen<=len<=maxlen, with no position
607 * specified. If we don't get maxlen then use prod to trim
608 * the length, if given. The lengths are all in rtextents.
609 */
610STATIC int /* error */
611xfs_rtallocate_extent_size(
612 xfs_mount_t *mp, /* file system mount point */
613 xfs_trans_t *tp, /* transaction pointer */
614 xfs_extlen_t minlen, /* minimum length to allocate */
615 xfs_extlen_t maxlen, /* maximum length to allocate */
616 xfs_extlen_t *len, /* out: actual length allocated */
617 xfs_buf_t **rbpp, /* in/out: summary block buffer */
618 xfs_fsblock_t *rsb, /* in/out: summary block number */
619 xfs_extlen_t prod, /* extent product factor */
620 xfs_rtblock_t *rtblock) /* out: start block allocated */
621{
622 int error; /* error value */
623 int i; /* bitmap block number */
624 int l; /* level number (loop control) */
625 xfs_rtblock_t n; /* next block to be tried */
626 xfs_rtblock_t r; /* result block number */
627 xfs_suminfo_t sum; /* summary information for extents */
628
629 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
630 /*
631 * Loop over all the levels starting with maxlen.
632 * At each level, look at all the bitmap blocks, to see if there
633 * are extents starting there that are long enough (>= maxlen).
634 * Note, only on the initial level can the allocation fail if
635 * the summary says there's an extent.
636 */
637 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
638 /*
639 * Loop over all the bitmap blocks.
640 */
641 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
642 /*
643 * Get the summary for this level/block.
644 */
645 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
646 &sum);
647 if (error) {
648 return error;
649 }
650 /*
651 * Nothing there, on to the next block.
652 */
653 if (!sum)
654 continue;
655 /*
656 * Try allocating the extent.
657 */
658 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
659 maxlen, len, &n, rbpp, rsb, prod, &r);
660 if (error) {
661 return error;
662 }
663 /*
664 * If it worked, return that.
665 */
666 if (r != NULLRTBLOCK) {
667 *rtblock = r;
668 return 0;
669 }
670 /*
671 * If the "next block to try" returned from the
672 * allocator is beyond the next bitmap block,
673 * skip to that bitmap block.
674 */
675 if (XFS_BITTOBLOCK(mp, n) > i + 1)
676 i = XFS_BITTOBLOCK(mp, n) - 1;
677 }
678 }
679 /*
680 * Didn't find any maxlen blocks. Try smaller ones, unless
681 * we're asking for a fixed size extent.
682 */
683 if (minlen > --maxlen) {
684 *rtblock = NULLRTBLOCK;
685 return 0;
686 }
687 /*
688 * Loop over sizes, from maxlen down to minlen.
689 * This time, when we do the allocations, allow smaller ones
690 * to succeed.
691 */
692 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
693 /*
694 * Loop over all the bitmap blocks, try an allocation
695 * starting in that block.
696 */
697 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
698 /*
699 * Get the summary information for this level/block.
700 */
701 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
702 &sum);
703 if (error) {
704 return error;
705 }
706 /*
707 * If nothing there, go on to next.
708 */
709 if (!sum)
710 continue;
711 /*
712 * Try the allocation. Make sure the specified
713 * minlen/maxlen are in the possible range for
714 * this summary level.
715 */
716 error = xfs_rtallocate_extent_block(mp, tp, i,
717 XFS_RTMAX(minlen, 1 << l),
718 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
719 len, &n, rbpp, rsb, prod, &r);
720 if (error) {
721 return error;
722 }
723 /*
724 * If it worked, return that extent.
725 */
726 if (r != NULLRTBLOCK) {
727 *rtblock = r;
728 return 0;
729 }
730 /*
731 * If the "next block to try" returned from the
732 * allocator is beyond the next bitmap block,
733 * skip to that bitmap block.
734 */
735 if (XFS_BITTOBLOCK(mp, n) > i + 1)
736 i = XFS_BITTOBLOCK(mp, n) - 1;
737 }
738 }
739 /*
740 * Got nothing, return failure.
741 */
742 *rtblock = NULLRTBLOCK;
743 return 0;
744}
745
746/*
747 * Mark an extent specified by start and len allocated.
748 * Updates all the summary information as well as the bitmap.
749 */
750STATIC int /* error */
751xfs_rtallocate_range(
752 xfs_mount_t *mp, /* file system mount point */
753 xfs_trans_t *tp, /* transaction pointer */
754 xfs_rtblock_t start, /* start block to allocate */
755 xfs_extlen_t len, /* length to allocate */
756 xfs_buf_t **rbpp, /* in/out: summary block buffer */
757 xfs_fsblock_t *rsb) /* in/out: summary block number */
758{
759 xfs_rtblock_t end; /* end of the allocated extent */
760 int error; /* error value */
761 xfs_rtblock_t postblock; /* first block allocated > end */
762 xfs_rtblock_t preblock; /* first block allocated < start */
763
764 end = start + len - 1;
765 /*
766 * Assume we're allocating out of the middle of a free extent.
767 * We need to find the beginning and end of the extent so we can
768 * properly update the summary.
769 */
770 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
771 if (error) {
772 return error;
773 }
774 /*
775 * Find the next allocated block (end of free extent).
776 */
777 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
778 &postblock);
779 if (error) {
780 return error;
781 }
782 /*
783 * Decrement the summary information corresponding to the entire
784 * (old) free extent.
785 */
786 error = xfs_rtmodify_summary(mp, tp,
787 XFS_RTBLOCKLOG(postblock + 1 - preblock),
788 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
789 if (error) {
790 return error;
791 }
792 /*
793 * If there are blocks not being allocated at the front of the
794 * old extent, add summary data for them to be free.
795 */
796 if (preblock < start) {
797 error = xfs_rtmodify_summary(mp, tp,
798 XFS_RTBLOCKLOG(start - preblock),
799 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
800 if (error) {
801 return error;
802 }
803 }
804 /*
805 * If there are blocks not being allocated at the end of the
806 * old extent, add summary data for them to be free.
807 */
808 if (postblock > end) {
809 error = xfs_rtmodify_summary(mp, tp,
810 XFS_RTBLOCKLOG(postblock - end),
811 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
812 if (error) {
813 return error;
814 }
815 }
816 /*
817 * Modify the bitmap to mark this extent allocated.
818 */
819 error = xfs_rtmodify_range(mp, tp, start, len, 0);
820 return error;
821}
822
823/*
824 * Return whether there are any free extents in the size range given
825 * by low and high, for the bitmap block bbno.
826 */
827STATIC int /* error */
828xfs_rtany_summary(
829 xfs_mount_t *mp, /* file system mount structure */
830 xfs_trans_t *tp, /* transaction pointer */
831 int low, /* low log2 extent size */
832 int high, /* high log2 extent size */
833 xfs_rtblock_t bbno, /* bitmap block number */
834 xfs_buf_t **rbpp, /* in/out: summary block buffer */
835 xfs_fsblock_t *rsb, /* in/out: summary block number */
836 int *stat) /* out: any good extents here? */
837{
838 int error; /* error value */
839 int log; /* loop counter, log2 of ext. size */
840 xfs_suminfo_t sum; /* summary data */
841
842 /*
843 * Loop over logs of extent sizes. Order is irrelevant.
844 */
845 for (log = low; log <= high; log++) {
846 /*
847 * Get one summary datum.
848 */
849 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
850 if (error) {
851 return error;
852 }
853 /*
854 * If there are any, return success.
855 */
856 if (sum) {
857 *stat = 1;
858 return 0;
859 }
860 }
861 /*
862 * Found nothing, return failure.
863 */
864 *stat = 0;
865 return 0;
866}
867
868/*
869 * Get a buffer for the bitmap or summary file block specified.
870 * The buffer is returned read and locked.
871 */
872STATIC int /* error */
873xfs_rtbuf_get(
874 xfs_mount_t *mp, /* file system mount structure */
875 xfs_trans_t *tp, /* transaction pointer */
876 xfs_rtblock_t block, /* block number in bitmap or summary */
877 int issum, /* is summary not bitmap */
878 xfs_buf_t **bpp) /* output: buffer for the block */
879{
880 xfs_buf_t *bp; /* block buffer, result */
881 xfs_daddr_t d; /* disk addr of block */
882 int error; /* error value */
883 xfs_fsblock_t fsb; /* fs block number for block */
884 xfs_inode_t *ip; /* bitmap or summary inode */
885
886 ip = issum ? mp->m_rsumip : mp->m_rbmip;
887 /*
888 * Map from the file offset (block) and inode number to the
889 * file system block.
890 */
891 error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
892 if (error) {
893 return error;
894 }
895 ASSERT(fsb != NULLFSBLOCK);
896 /*
897 * Convert to disk address for buffer cache.
898 */
899 d = XFS_FSB_TO_DADDR(mp, fsb);
900 /*
901 * Read the buffer.
902 */
903 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
904 mp->m_bsize, 0, &bp);
905 if (error) {
906 return error;
907 }
908 ASSERT(bp && !XFS_BUF_GETERROR(bp));
909 *bpp = bp;
910 return 0;
911}
912
913#ifdef DEBUG
914/*
915 * Check that the given extent (block range) is allocated already.
916 */
917STATIC int /* error */
918xfs_rtcheck_alloc_range(
919 xfs_mount_t *mp, /* file system mount point */
920 xfs_trans_t *tp, /* transaction pointer */
921 xfs_rtblock_t bno, /* starting block number of extent */
922 xfs_extlen_t len, /* length of extent */
923 int *stat) /* out: 1 for allocated, 0 for not */
924{
925 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
926
927 return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
928}
929#endif
930
931#ifdef DEBUG
932/*
933 * Check whether the given block in the bitmap has the given value.
934 */
935STATIC int /* 1 for matches, 0 for not */
936xfs_rtcheck_bit(
937 xfs_mount_t *mp, /* file system mount structure */
938 xfs_trans_t *tp, /* transaction pointer */
939 xfs_rtblock_t start, /* bit (block) to check */
940 int val) /* 1 for free, 0 for allocated */
941{
942 int bit; /* bit number in the word */
943 xfs_rtblock_t block; /* bitmap block number */
944 xfs_buf_t *bp; /* buf for the block */
945 xfs_rtword_t *bufp; /* pointer into the buffer */
946 /* REFERENCED */
947 int error; /* error value */
948 xfs_rtword_t wdiff; /* difference between bit & expected */
949 int word; /* word number in the buffer */
950 xfs_rtword_t wval; /* word value from buffer */
951
952 block = XFS_BITTOBLOCK(mp, start);
953 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
954 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
955 word = XFS_BITTOWORD(mp, start);
956 bit = (int)(start & (XFS_NBWORD - 1));
957 wval = bufp[word];
958 xfs_trans_brelse(tp, bp);
959 wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
960 return !wdiff;
961}
962#endif /* DEBUG */
963
964#if 0
965/*
966 * Check that the given extent (block range) is free already.
967 */
968STATIC int /* error */
969xfs_rtcheck_free_range(
970 xfs_mount_t *mp, /* file system mount point */
971 xfs_trans_t *tp, /* transaction pointer */
972 xfs_rtblock_t bno, /* starting block number of extent */
973 xfs_extlen_t len, /* length of extent */
974 int *stat) /* out: 1 for free, 0 for not */
975{
976 xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
977
978 return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
979}
980#endif
981
982/*
983 * Check that the given range is either all allocated (val = 0) or
984 * all free (val = 1).
985 */
986STATIC int /* error */
987xfs_rtcheck_range(
988 xfs_mount_t *mp, /* file system mount point */
989 xfs_trans_t *tp, /* transaction pointer */
990 xfs_rtblock_t start, /* starting block number of extent */
991 xfs_extlen_t len, /* length of extent */
992 int val, /* 1 for free, 0 for allocated */
993 xfs_rtblock_t *new, /* out: first block not matching */
994 int *stat) /* out: 1 for matches, 0 for not */
995{
996 xfs_rtword_t *b; /* current word in buffer */
997 int bit; /* bit number in the word */
998 xfs_rtblock_t block; /* bitmap block number */
999 xfs_buf_t *bp; /* buf for the block */
1000 xfs_rtword_t *bufp; /* starting word in buffer */
1001 int error; /* error value */
1002 xfs_rtblock_t i; /* current bit number rel. to start */
1003 xfs_rtblock_t lastbit; /* last useful bit in word */
1004 xfs_rtword_t mask; /* mask of relevant bits for value */
1005 xfs_rtword_t wdiff; /* difference from wanted value */
1006 int word; /* word number in the buffer */
1007
1008 /*
1009 * Compute starting bitmap block number
1010 */
1011 block = XFS_BITTOBLOCK(mp, start);
1012 /*
1013 * Read the bitmap block.
1014 */
1015 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1016 if (error) {
1017 return error;
1018 }
1019 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1020 /*
1021 * Compute the starting word's address, and starting bit.
1022 */
1023 word = XFS_BITTOWORD(mp, start);
1024 b = &bufp[word];
1025 bit = (int)(start & (XFS_NBWORD - 1));
1026 /*
1027 * 0 (allocated) => all zero's; 1 (free) => all one's.
1028 */
1029 val = -val;
1030 /*
1031 * If not starting on a word boundary, deal with the first
1032 * (partial) word.
1033 */
1034 if (bit) {
1035 /*
1036 * Compute first bit not examined.
1037 */
1038 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1039 /*
1040 * Mask of relevant bits.
1041 */
1042 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1043 /*
1044 * Compute difference between actual and desired value.
1045 */
1046 if ((wdiff = (*b ^ val) & mask)) {
1047 /*
1048 * Different, compute first wrong bit and return.
1049 */
1050 xfs_trans_brelse(tp, bp);
1051 i = XFS_RTLOBIT(wdiff) - bit;
1052 *new = start + i;
1053 *stat = 0;
1054 return 0;
1055 }
1056 i = lastbit - bit;
1057 /*
1058 * Go on to next block if that's where the next word is
1059 * and we need the next word.
1060 */
1061 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1062 /*
1063 * If done with this block, get the next one.
1064 */
1065 xfs_trans_brelse(tp, bp);
1066 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1067 if (error) {
1068 return error;
1069 }
1070 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1071 word = 0;
1072 } else {
1073 /*
1074 * Go on to the next word in the buffer.
1075 */
1076 b++;
1077 }
1078 } else {
1079 /*
1080 * Starting on a word boundary, no partial word.
1081 */
1082 i = 0;
1083 }
1084 /*
1085 * Loop over whole words in buffers. When we use up one buffer
1086 * we move on to the next one.
1087 */
1088 while (len - i >= XFS_NBWORD) {
1089 /*
1090 * Compute difference between actual and desired value.
1091 */
1092 if ((wdiff = *b ^ val)) {
1093 /*
1094 * Different, compute first wrong bit and return.
1095 */
1096 xfs_trans_brelse(tp, bp);
1097 i += XFS_RTLOBIT(wdiff);
1098 *new = start + i;
1099 *stat = 0;
1100 return 0;
1101 }
1102 i += XFS_NBWORD;
1103 /*
1104 * Go on to next block if that's where the next word is
1105 * and we need the next word.
1106 */
1107 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1108 /*
1109 * If done with this block, get the next one.
1110 */
1111 xfs_trans_brelse(tp, bp);
1112 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1113 if (error) {
1114 return error;
1115 }
1116 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1117 word = 0;
1118 } else {
1119 /*
1120 * Go on to the next word in the buffer.
1121 */
1122 b++;
1123 }
1124 }
1125 /*
1126 * If not ending on a word boundary, deal with the last
1127 * (partial) word.
1128 */
1129 if ((lastbit = len - i)) {
1130 /*
1131 * Mask of relevant bits.
1132 */
1133 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1134 /*
1135 * Compute difference between actual and desired value.
1136 */
1137 if ((wdiff = (*b ^ val) & mask)) {
1138 /*
1139 * Different, compute first wrong bit and return.
1140 */
1141 xfs_trans_brelse(tp, bp);
1142 i += XFS_RTLOBIT(wdiff);
1143 *new = start + i;
1144 *stat = 0;
1145 return 0;
1146 } else
1147 i = len;
1148 }
1149 /*
1150 * Successful, return.
1151 */
1152 xfs_trans_brelse(tp, bp);
1153 *new = start + i;
1154 *stat = 1;
1155 return 0;
1156}
1157
1158/*
1159 * Copy and transform the summary file, given the old and new
1160 * parameters in the mount structures.
1161 */
1162STATIC int /* error */
1163xfs_rtcopy_summary(
1164 xfs_mount_t *omp, /* old file system mount point */
1165 xfs_mount_t *nmp, /* new file system mount point */
1166 xfs_trans_t *tp) /* transaction pointer */
1167{
1168 xfs_rtblock_t bbno; /* bitmap block number */
1169 xfs_buf_t *bp; /* summary buffer */
1170 int error; /* error return value */
1171 int log; /* summary level number (log length) */
1172 xfs_suminfo_t sum; /* summary data */
1173 xfs_fsblock_t sumbno; /* summary block number */
1174
1175 bp = NULL;
1176 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1177 for (bbno = omp->m_sb.sb_rbmblocks - 1;
1178 (xfs_srtblock_t)bbno >= 0;
1179 bbno--) {
1180 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1181 &sumbno, &sum);
1182 if (error)
1183 return error;
1184 if (sum == 0)
1185 continue;
1186 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1187 &bp, &sumbno);
1188 if (error)
1189 return error;
1190 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1191 &bp, &sumbno);
1192 if (error)
1193 return error;
1194 ASSERT(sum > 0);
1195 }
1196 }
1197 return 0;
1198}
1199
1200/*
1201 * Searching backward from start to limit, find the first block whose
1202 * allocated/free state is different from start's.
1203 */
1204STATIC int /* error */
1205xfs_rtfind_back(
1206 xfs_mount_t *mp, /* file system mount point */
1207 xfs_trans_t *tp, /* transaction pointer */
1208 xfs_rtblock_t start, /* starting block to look at */
1209 xfs_rtblock_t limit, /* last block to look at */
1210 xfs_rtblock_t *rtblock) /* out: start block found */
1211{
1212 xfs_rtword_t *b; /* current word in buffer */
1213 int bit; /* bit number in the word */
1214 xfs_rtblock_t block; /* bitmap block number */
1215 xfs_buf_t *bp; /* buf for the block */
1216 xfs_rtword_t *bufp; /* starting word in buffer */
1217 int error; /* error value */
1218 xfs_rtblock_t firstbit; /* first useful bit in the word */
1219 xfs_rtblock_t i; /* current bit number rel. to start */
1220 xfs_rtblock_t len; /* length of inspected area */
1221 xfs_rtword_t mask; /* mask of relevant bits for value */
1222 xfs_rtword_t want; /* mask for "good" values */
1223 xfs_rtword_t wdiff; /* difference from wanted value */
1224 int word; /* word number in the buffer */
1225
1226 /*
1227 * Compute and read in starting bitmap block for starting block.
1228 */
1229 block = XFS_BITTOBLOCK(mp, start);
1230 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1231 if (error) {
1232 return error;
1233 }
1234 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1235 /*
1236 * Get the first word's index & point to it.
1237 */
1238 word = XFS_BITTOWORD(mp, start);
1239 b = &bufp[word];
1240 bit = (int)(start & (XFS_NBWORD - 1));
1241 len = start - limit + 1;
1242 /*
1243 * Compute match value, based on the bit at start: if 1 (free)
1244 * then all-ones, else all-zeroes.
1245 */
1246 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1247 /*
1248 * If the starting position is not word-aligned, deal with the
1249 * partial word.
1250 */
1251 if (bit < XFS_NBWORD - 1) {
1252 /*
1253 * Calculate first (leftmost) bit number to look at,
1254 * and mask for all the relevant bits in this word.
1255 */
1256 firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1257 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1258 firstbit;
1259 /*
1260 * Calculate the difference between the value there
1261 * and what we're looking for.
1262 */
1263 if ((wdiff = (*b ^ want) & mask)) {
1264 /*
1265 * Different. Mark where we are and return.
1266 */
1267 xfs_trans_brelse(tp, bp);
1268 i = bit - XFS_RTHIBIT(wdiff);
1269 *rtblock = start - i + 1;
1270 return 0;
1271 }
1272 i = bit - firstbit + 1;
1273 /*
1274 * Go on to previous block if that's where the previous word is
1275 * and we need the previous word.
1276 */
1277 if (--word == -1 && i < len) {
1278 /*
1279 * If done with this block, get the previous one.
1280 */
1281 xfs_trans_brelse(tp, bp);
1282 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1283 if (error) {
1284 return error;
1285 }
1286 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1287 word = XFS_BLOCKWMASK(mp);
1288 b = &bufp[word];
1289 } else {
1290 /*
1291 * Go on to the previous word in the buffer.
1292 */
1293 b--;
1294 }
1295 } else {
1296 /*
1297 * Starting on a word boundary, no partial word.
1298 */
1299 i = 0;
1300 }
1301 /*
1302 * Loop over whole words in buffers. When we use up one buffer
1303 * we move on to the previous one.
1304 */
1305 while (len - i >= XFS_NBWORD) {
1306 /*
1307 * Compute difference between actual and desired value.
1308 */
1309 if ((wdiff = *b ^ want)) {
1310 /*
1311 * Different, mark where we are and return.
1312 */
1313 xfs_trans_brelse(tp, bp);
1314 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1315 *rtblock = start - i + 1;
1316 return 0;
1317 }
1318 i += XFS_NBWORD;
1319 /*
1320 * Go on to previous block if that's where the previous word is
1321 * and we need the previous word.
1322 */
1323 if (--word == -1 && i < len) {
1324 /*
1325 * If done with this block, get the previous one.
1326 */
1327 xfs_trans_brelse(tp, bp);
1328 error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1329 if (error) {
1330 return error;
1331 }
1332 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1333 word = XFS_BLOCKWMASK(mp);
1334 b = &bufp[word];
1335 } else {
1336 /*
1337 * Go on to the previous word in the buffer.
1338 */
1339 b--;
1340 }
1341 }
1342 /*
1343 * If not ending on a word boundary, deal with the last
1344 * (partial) word.
1345 */
1346 if (len - i) {
1347 /*
1348 * Calculate first (leftmost) bit number to look at,
1349 * and mask for all the relevant bits in this word.
1350 */
1351 firstbit = XFS_NBWORD - (len - i);
1352 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1353 /*
1354 * Compute difference between actual and desired value.
1355 */
1356 if ((wdiff = (*b ^ want) & mask)) {
1357 /*
1358 * Different, mark where we are and return.
1359 */
1360 xfs_trans_brelse(tp, bp);
1361 i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1362 *rtblock = start - i + 1;
1363 return 0;
1364 } else
1365 i = len;
1366 }
1367 /*
1368 * No match, return that we scanned the whole area.
1369 */
1370 xfs_trans_brelse(tp, bp);
1371 *rtblock = start - i + 1;
1372 return 0;
1373}
1374
1375/*
1376 * Searching forward from start to limit, find the first block whose
1377 * allocated/free state is different from start's.
1378 */
1379STATIC int /* error */
1380xfs_rtfind_forw(
1381 xfs_mount_t *mp, /* file system mount point */
1382 xfs_trans_t *tp, /* transaction pointer */
1383 xfs_rtblock_t start, /* starting block to look at */
1384 xfs_rtblock_t limit, /* last block to look at */
1385 xfs_rtblock_t *rtblock) /* out: start block found */
1386{
1387 xfs_rtword_t *b; /* current word in buffer */
1388 int bit; /* bit number in the word */
1389 xfs_rtblock_t block; /* bitmap block number */
1390 xfs_buf_t *bp; /* buf for the block */
1391 xfs_rtword_t *bufp; /* starting word in buffer */
1392 int error; /* error value */
1393 xfs_rtblock_t i; /* current bit number rel. to start */
1394 xfs_rtblock_t lastbit; /* last useful bit in the word */
1395 xfs_rtblock_t len; /* length of inspected area */
1396 xfs_rtword_t mask; /* mask of relevant bits for value */
1397 xfs_rtword_t want; /* mask for "good" values */
1398 xfs_rtword_t wdiff; /* difference from wanted value */
1399 int word; /* word number in the buffer */
1400
1401 /*
1402 * Compute and read in starting bitmap block for starting block.
1403 */
1404 block = XFS_BITTOBLOCK(mp, start);
1405 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1406 if (error) {
1407 return error;
1408 }
1409 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1410 /*
1411 * Get the first word's index & point to it.
1412 */
1413 word = XFS_BITTOWORD(mp, start);
1414 b = &bufp[word];
1415 bit = (int)(start & (XFS_NBWORD - 1));
1416 len = limit - start + 1;
1417 /*
1418 * Compute match value, based on the bit at start: if 1 (free)
1419 * then all-ones, else all-zeroes.
1420 */
1421 want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1422 /*
1423 * If the starting position is not word-aligned, deal with the
1424 * partial word.
1425 */
1426 if (bit) {
1427 /*
1428 * Calculate last (rightmost) bit number to look at,
1429 * and mask for all the relevant bits in this word.
1430 */
1431 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1432 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1433 /*
1434 * Calculate the difference between the value there
1435 * and what we're looking for.
1436 */
1437 if ((wdiff = (*b ^ want) & mask)) {
1438 /*
1439 * Different. Mark where we are and return.
1440 */
1441 xfs_trans_brelse(tp, bp);
1442 i = XFS_RTLOBIT(wdiff) - bit;
1443 *rtblock = start + i - 1;
1444 return 0;
1445 }
1446 i = lastbit - bit;
1447 /*
1448 * Go on to next block if that's where the next word is
1449 * and we need the next word.
1450 */
1451 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1452 /*
1453 * If done with this block, get the previous one.
1454 */
1455 xfs_trans_brelse(tp, bp);
1456 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1457 if (error) {
1458 return error;
1459 }
1460 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1461 word = 0;
1462 } else {
1463 /*
1464 * Go on to the previous word in the buffer.
1465 */
1466 b++;
1467 }
1468 } else {
1469 /*
1470 * Starting on a word boundary, no partial word.
1471 */
1472 i = 0;
1473 }
1474 /*
1475 * Loop over whole words in buffers. When we use up one buffer
1476 * we move on to the next one.
1477 */
1478 while (len - i >= XFS_NBWORD) {
1479 /*
1480 * Compute difference between actual and desired value.
1481 */
1482 if ((wdiff = *b ^ want)) {
1483 /*
1484 * Different, mark where we are and return.
1485 */
1486 xfs_trans_brelse(tp, bp);
1487 i += XFS_RTLOBIT(wdiff);
1488 *rtblock = start + i - 1;
1489 return 0;
1490 }
1491 i += XFS_NBWORD;
1492 /*
1493 * Go on to next block if that's where the next word is
1494 * and we need the next word.
1495 */
1496 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1497 /*
1498 * If done with this block, get the next one.
1499 */
1500 xfs_trans_brelse(tp, bp);
1501 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1502 if (error) {
1503 return error;
1504 }
1505 b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1506 word = 0;
1507 } else {
1508 /*
1509 * Go on to the next word in the buffer.
1510 */
1511 b++;
1512 }
1513 }
1514 /*
1515 * If not ending on a word boundary, deal with the last
1516 * (partial) word.
1517 */
1518 if ((lastbit = len - i)) {
1519 /*
1520 * Calculate mask for all the relevant bits in this word.
1521 */
1522 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1523 /*
1524 * Compute difference between actual and desired value.
1525 */
1526 if ((wdiff = (*b ^ want) & mask)) {
1527 /*
1528 * Different, mark where we are and return.
1529 */
1530 xfs_trans_brelse(tp, bp);
1531 i += XFS_RTLOBIT(wdiff);
1532 *rtblock = start + i - 1;
1533 return 0;
1534 } else
1535 i = len;
1536 }
1537 /*
1538 * No match, return that we scanned the whole area.
1539 */
1540 xfs_trans_brelse(tp, bp);
1541 *rtblock = start + i - 1;
1542 return 0;
1543}
1544
1545/*
1546 * Mark an extent specified by start and len freed.
1547 * Updates all the summary information as well as the bitmap.
1548 */
1549STATIC int /* error */
1550xfs_rtfree_range(
1551 xfs_mount_t *mp, /* file system mount point */
1552 xfs_trans_t *tp, /* transaction pointer */
1553 xfs_rtblock_t start, /* starting block to free */
1554 xfs_extlen_t len, /* length to free */
1555 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1556 xfs_fsblock_t *rsb) /* in/out: summary block number */
1557{
1558 xfs_rtblock_t end; /* end of the freed extent */
1559 int error; /* error value */
1560 xfs_rtblock_t postblock; /* first block freed > end */
1561 xfs_rtblock_t preblock; /* first block freed < start */
1562
1563 end = start + len - 1;
1564 /*
1565 * Modify the bitmap to mark this extent freed.
1566 */
1567 error = xfs_rtmodify_range(mp, tp, start, len, 1);
1568 if (error) {
1569 return error;
1570 }
1571 /*
1572 * Assume we're freeing out of the middle of an allocated extent.
1573 * We need to find the beginning and end of the extent so we can
1574 * properly update the summary.
1575 */
1576 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1577 if (error) {
1578 return error;
1579 }
1580 /*
1581 * Find the next allocated block (end of allocated extent).
1582 */
1583 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1584 &postblock);
1585 /*
1586 * If there are blocks not being freed at the front of the
1587 * old extent, add summary data for them to be allocated.
1588 */
1589 if (preblock < start) {
1590 error = xfs_rtmodify_summary(mp, tp,
1591 XFS_RTBLOCKLOG(start - preblock),
1592 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1593 if (error) {
1594 return error;
1595 }
1596 }
1597 /*
1598 * If there are blocks not being freed at the end of the
1599 * old extent, add summary data for them to be allocated.
1600 */
1601 if (postblock > end) {
1602 error = xfs_rtmodify_summary(mp, tp,
1603 XFS_RTBLOCKLOG(postblock - end),
1604 XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1605 if (error) {
1606 return error;
1607 }
1608 }
1609 /*
1610 * Increment the summary information corresponding to the entire
1611 * (new) free extent.
1612 */
1613 error = xfs_rtmodify_summary(mp, tp,
1614 XFS_RTBLOCKLOG(postblock + 1 - preblock),
1615 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1616 return error;
1617}
1618
1619/*
1620 * Read and return the summary information for a given extent size,
1621 * bitmap block combination.
1622 * Keeps track of a current summary block, so we don't keep reading
1623 * it from the buffer cache.
1624 */
1625STATIC int /* error */
1626xfs_rtget_summary(
1627 xfs_mount_t *mp, /* file system mount structure */
1628 xfs_trans_t *tp, /* transaction pointer */
1629 int log, /* log2 of extent size */
1630 xfs_rtblock_t bbno, /* bitmap block number */
1631 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1632 xfs_fsblock_t *rsb, /* in/out: summary block number */
1633 xfs_suminfo_t *sum) /* out: summary info for this block */
1634{
1635 xfs_buf_t *bp; /* buffer for summary block */
1636 int error; /* error value */
1637 xfs_fsblock_t sb; /* summary fsblock */
1638 int so; /* index into the summary file */
1639 xfs_suminfo_t *sp; /* pointer to returned data */
1640
1641 /*
1642 * Compute entry number in the summary file.
1643 */
1644 so = XFS_SUMOFFS(mp, log, bbno);
1645 /*
1646 * Compute the block number in the summary file.
1647 */
1648 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1649 /*
1650 * If we have an old buffer, and the block number matches, use that.
1651 */
1652 if (rbpp && *rbpp && *rsb == sb)
1653 bp = *rbpp;
1654 /*
1655 * Otherwise we have to get the buffer.
1656 */
1657 else {
1658 /*
1659 * If there was an old one, get rid of it first.
1660 */
1661 if (rbpp && *rbpp)
1662 xfs_trans_brelse(tp, *rbpp);
1663 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1664 if (error) {
1665 return error;
1666 }
1667 /*
1668 * Remember this buffer and block for the next call.
1669 */
1670 if (rbpp) {
1671 *rbpp = bp;
1672 *rsb = sb;
1673 }
1674 }
1675 /*
1676 * Point to the summary information & copy it out.
1677 */
1678 sp = XFS_SUMPTR(mp, bp, so);
1679 *sum = *sp;
1680 /*
1681 * Drop the buffer if we're not asked to remember it.
1682 */
1683 if (!rbpp)
1684 xfs_trans_brelse(tp, bp);
1685 return 0;
1686}
1687
1688/*
1689 * Set the given range of bitmap bits to the given value.
1690 * Do whatever I/O and logging is required.
1691 */
1692STATIC int /* error */
1693xfs_rtmodify_range(
1694 xfs_mount_t *mp, /* file system mount point */
1695 xfs_trans_t *tp, /* transaction pointer */
1696 xfs_rtblock_t start, /* starting block to modify */
1697 xfs_extlen_t len, /* length of extent to modify */
1698 int val) /* 1 for free, 0 for allocated */
1699{
1700 xfs_rtword_t *b; /* current word in buffer */
1701 int bit; /* bit number in the word */
1702 xfs_rtblock_t block; /* bitmap block number */
1703 xfs_buf_t *bp; /* buf for the block */
1704 xfs_rtword_t *bufp; /* starting word in buffer */
1705 int error; /* error value */
1706 xfs_rtword_t *first; /* first used word in the buffer */
1707 int i; /* current bit number rel. to start */
1708 int lastbit; /* last useful bit in word */
1709 xfs_rtword_t mask; /* mask o frelevant bits for value */
1710 int word; /* word number in the buffer */
1711
1712 /*
1713 * Compute starting bitmap block number.
1714 */
1715 block = XFS_BITTOBLOCK(mp, start);
1716 /*
1717 * Read the bitmap block, and point to its data.
1718 */
1719 error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1720 if (error) {
1721 return error;
1722 }
1723 bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1724 /*
1725 * Compute the starting word's address, and starting bit.
1726 */
1727 word = XFS_BITTOWORD(mp, start);
1728 first = b = &bufp[word];
1729 bit = (int)(start & (XFS_NBWORD - 1));
1730 /*
1731 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1732 */
1733 val = -val;
1734 /*
1735 * If not starting on a word boundary, deal with the first
1736 * (partial) word.
1737 */
1738 if (bit) {
1739 /*
1740 * Compute first bit not changed and mask of relevant bits.
1741 */
1742 lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1743 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1744 /*
1745 * Set/clear the active bits.
1746 */
1747 if (val)
1748 *b |= mask;
1749 else
1750 *b &= ~mask;
1751 i = lastbit - bit;
1752 /*
1753 * Go on to the next block if that's where the next word is
1754 * and we need the next word.
1755 */
1756 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1757 /*
1758 * Log the changed part of this block.
1759 * Get the next one.
1760 */
1761 xfs_trans_log_buf(tp, bp,
1762 (uint)((char *)first - (char *)bufp),
1763 (uint)((char *)b - (char *)bufp));
1764 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1765 if (error) {
1766 return error;
1767 }
1768 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1769 word = 0;
1770 } else {
1771 /*
1772 * Go on to the next word in the buffer
1773 */
1774 b++;
1775 }
1776 } else {
1777 /*
1778 * Starting on a word boundary, no partial word.
1779 */
1780 i = 0;
1781 }
1782 /*
1783 * Loop over whole words in buffers. When we use up one buffer
1784 * we move on to the next one.
1785 */
1786 while (len - i >= XFS_NBWORD) {
1787 /*
1788 * Set the word value correctly.
1789 */
1790 *b = val;
1791 i += XFS_NBWORD;
1792 /*
1793 * Go on to the next block if that's where the next word is
1794 * and we need the next word.
1795 */
1796 if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1797 /*
1798 * Log the changed part of this block.
1799 * Get the next one.
1800 */
1801 xfs_trans_log_buf(tp, bp,
1802 (uint)((char *)first - (char *)bufp),
1803 (uint)((char *)b - (char *)bufp));
1804 error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1805 if (error) {
1806 return error;
1807 }
1808 first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
1809 word = 0;
1810 } else {
1811 /*
1812 * Go on to the next word in the buffer
1813 */
1814 b++;
1815 }
1816 }
1817 /*
1818 * If not ending on a word boundary, deal with the last
1819 * (partial) word.
1820 */
1821 if ((lastbit = len - i)) {
1822 /*
1823 * Compute a mask of relevant bits.
1824 */
1825 bit = 0;
1826 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1827 /*
1828 * Set/clear the active bits.
1829 */
1830 if (val)
1831 *b |= mask;
1832 else
1833 *b &= ~mask;
1834 b++;
1835 }
1836 /*
1837 * Log any remaining changed bytes.
1838 */
1839 if (b > first)
1840 xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1841 (uint)((char *)b - (char *)bufp - 1));
1842 return 0;
1843}
1844
1845/*
1846 * Read and modify the summary information for a given extent size,
1847 * bitmap block combination.
1848 * Keeps track of a current summary block, so we don't keep reading
1849 * it from the buffer cache.
1850 */
1851STATIC int /* error */
1852xfs_rtmodify_summary(
1853 xfs_mount_t *mp, /* file system mount point */
1854 xfs_trans_t *tp, /* transaction pointer */
1855 int log, /* log2 of extent size */
1856 xfs_rtblock_t bbno, /* bitmap block number */
1857 int delta, /* change to make to summary info */
1858 xfs_buf_t **rbpp, /* in/out: summary block buffer */
1859 xfs_fsblock_t *rsb) /* in/out: summary block number */
1860{
1861 xfs_buf_t *bp; /* buffer for the summary block */
1862 int error; /* error value */
1863 xfs_fsblock_t sb; /* summary fsblock */
1864 int so; /* index into the summary file */
1865 xfs_suminfo_t *sp; /* pointer to returned data */
1866
1867 /*
1868 * Compute entry number in the summary file.
1869 */
1870 so = XFS_SUMOFFS(mp, log, bbno);
1871 /*
1872 * Compute the block number in the summary file.
1873 */
1874 sb = XFS_SUMOFFSTOBLOCK(mp, so);
1875 /*
1876 * If we have an old buffer, and the block number matches, use that.
1877 */
1878 if (rbpp && *rbpp && *rsb == sb)
1879 bp = *rbpp;
1880 /*
1881 * Otherwise we have to get the buffer.
1882 */
1883 else {
1884 /*
1885 * If there was an old one, get rid of it first.
1886 */
1887 if (rbpp && *rbpp)
1888 xfs_trans_brelse(tp, *rbpp);
1889 error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1890 if (error) {
1891 return error;
1892 }
1893 /*
1894 * Remember this buffer and block for the next call.
1895 */
1896 if (rbpp) {
1897 *rbpp = bp;
1898 *rsb = sb;
1899 }
1900 }
1901 /*
1902 * Point to the summary information, modify and log it.
1903 */
1904 sp = XFS_SUMPTR(mp, bp, so);
1905 *sp += delta;
1906 xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
1907 (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
1908 return 0;
1909}
1910
1911/*
1912 * Visible (exported) functions.
1913 */
1914
1915/*
1916 * Grow the realtime area of the filesystem.
1917 */
1918int
1919xfs_growfs_rt(
1920 xfs_mount_t *mp, /* mount point for filesystem */
1921 xfs_growfs_rt_t *in) /* growfs rt input struct */
1922{
1923 xfs_rtblock_t bmbno; /* bitmap block number */
1924 xfs_buf_t *bp; /* temporary buffer */
1925 int cancelflags; /* flags for xfs_trans_cancel */
1926 int error; /* error return value */
1927 xfs_inode_t *ip; /* bitmap inode, used as lock */
1928 xfs_mount_t *nmp; /* new (fake) mount structure */
1929 xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
1930 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
1931 xfs_drtbno_t nrextents; /* new number of realtime extents */
1932 uint8_t nrextslog; /* new log2 of sb_rextents */
1933 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
1934 uint nrsumlevels; /* new rt summary levels */
1935 uint nrsumsize; /* new size of rt summary, bytes */
1936 xfs_sb_t *nsbp; /* new superblock */
1937 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
1938 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
1939 xfs_sb_t *sbp; /* old superblock */
1940 xfs_fsblock_t sumbno; /* summary block number */
1941 xfs_trans_t *tp; /* transaction pointer */
1942
1943 sbp = &mp->m_sb;
1944 /*
1945 * Initial error checking.
1946 */
1947 if (mp->m_rtdev_targp || mp->m_rbmip == NULL ||
1948 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1949 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1950 return XFS_ERROR(EINVAL);
1951 /*
1952 * Read in the last block of the device, make sure it exists.
1953 */
1954 error = xfs_read_buf(mp, mp->m_rtdev_targp,
1955 XFS_FSB_TO_BB(mp, in->newblocks - 1),
1956 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1957 if (error)
1958 return error;
1959 ASSERT(bp);
1960 xfs_buf_relse(bp);
1961 /*
1962 * Calculate new parameters. These are the final values to be reached.
1963 */
1964 nrextents = nrblocks;
1965 do_div(nrextents, in->extsize);
1966 nrbmblocks = roundup_64(nrextents, NBBY * sbp->sb_blocksize);
1967 nrextslog = xfs_highbit32(nrextents);
1968 nrsumlevels = nrextslog + 1;
1969 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1970 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1971 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1972 /*
1973 * New summary size can't be more than half the size of
1974 * the log. This prevents us from getting a log overflow,
1975 * since we'll log basically the whole summary file at once.
1976 */
1977 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1978 return XFS_ERROR(EINVAL);
1979 /*
1980 * Get the old block counts for bitmap and summary inodes.
1981 * These can't change since other growfs callers are locked out.
1982 */
1983 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1984 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1985 /*
1986 * Allocate space to the bitmap and summary files, as necessary.
1987 */
1988 if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
1989 mp->m_sb.sb_rbmino)))
1990 return error;
1991 if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
1992 mp->m_sb.sb_rsumino)))
1993 return error;
1994 nmp = NULL;
1995 /*
1996 * Loop over the bitmap blocks.
1997 * We will do everything one bitmap block at a time.
1998 * Skip the current block if it is exactly full.
1999 * This also deals with the case where there were no rtextents before.
2000 */
2001 for (bmbno = sbp->sb_rbmblocks -
2002 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
2003 bmbno < nrbmblocks;
2004 bmbno++) {
2005 /*
2006 * Allocate a new (fake) mount/sb.
2007 */
2008 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
2009 *nmp = *mp;
2010 nsbp = &nmp->m_sb;
2011 /*
2012 * Calculate new sb and mount fields for this round.
2013 */
2014 nsbp->sb_rextsize = in->extsize;
2015 nsbp->sb_rbmblocks = bmbno + 1;
2016 nsbp->sb_rblocks =
2017 XFS_RTMIN(nrblocks,
2018 nsbp->sb_rbmblocks * NBBY *
2019 nsbp->sb_blocksize * nsbp->sb_rextsize);
2020 nsbp->sb_rextents = nsbp->sb_rblocks;
2021 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
2022 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
2023 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
2024 nrsumsize =
2025 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
2026 nsbp->sb_rbmblocks;
2027 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
2028 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
2029 /*
2030 * Start a transaction, get the log reservation.
2031 */
2032 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
2033 cancelflags = 0;
2034 if ((error = xfs_trans_reserve(tp, 0,
2035 XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
2036 goto error_exit;
2037 /*
2038 * Lock out other callers by grabbing the bitmap inode lock.
2039 */
2040 if ((error = xfs_trans_iget(mp, tp, 0, mp->m_sb.sb_rbmino,
2041 XFS_ILOCK_EXCL, &ip)))
2042 goto error_exit;
2043 ASSERT(ip == mp->m_rbmip);
2044 /*
2045 * Update the bitmap inode's size.
2046 */
2047 mp->m_rbmip->i_d.di_size =
2048 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
2049 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2050 cancelflags |= XFS_TRANS_ABORT;
2051 /*
2052 * Get the summary inode into the transaction.
2053 */
2054 if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino,
2055 0, XFS_ILOCK_EXCL, &ip)))
2056 goto error_exit;
2057 ASSERT(ip == mp->m_rsumip);
2058 /*
2059 * Update the summary inode's size.
2060 */
2061 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
2062 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
2063 /*
2064 * Copy summary data from old to new sizes.
2065 * Do this when the real size (not block-aligned) changes.
2066 */
2067 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
2068 mp->m_rsumlevels != nmp->m_rsumlevels) {
2069 error = xfs_rtcopy_summary(mp, nmp, tp);
2070 if (error)
2071 goto error_exit;
2072 }
2073 /*
2074 * Update superblock fields.
2075 */
2076 if (nsbp->sb_rextsize != sbp->sb_rextsize)
2077 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2078 nsbp->sb_rextsize - sbp->sb_rextsize);
2079 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2080 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2081 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2082 if (nsbp->sb_rblocks != sbp->sb_rblocks)
2083 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2084 nsbp->sb_rblocks - sbp->sb_rblocks);
2085 if (nsbp->sb_rextents != sbp->sb_rextents)
2086 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2087 nsbp->sb_rextents - sbp->sb_rextents);
2088 if (nsbp->sb_rextslog != sbp->sb_rextslog)
2089 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2090 nsbp->sb_rextslog - sbp->sb_rextslog);
2091 /*
2092 * Free new extent.
2093 */
2094 bp = NULL;
2095 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2096 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2097 if (error)
2098 goto error_exit;
2099 /*
2100 * Mark more blocks free in the superblock.
2101 */
2102 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2103 nsbp->sb_rextents - sbp->sb_rextents);
2104 /*
2105 * Free the fake mp structure.
2106 */
2107 kmem_free(nmp, sizeof(*nmp));
2108 nmp = NULL;
2109 /*
2110 * Update mp values into the real mp structure.
2111 */
2112 mp->m_rsumlevels = nrsumlevels;
2113 mp->m_rsumsize = nrsumsize;
2114 /*
2115 * Commit the transaction.
2116 */
2117 xfs_trans_commit(tp, 0, NULL);
2118 }
2119 return 0;
2120
2121 /*
2122 * Error paths come here.
2123 */
2124error_exit:
2125 if (nmp)
2126 kmem_free(nmp, sizeof(*nmp));
2127 xfs_trans_cancel(tp, cancelflags);
2128 return error;
2129}
2130
2131/*
2132 * Allocate an extent in the realtime subvolume, with the usual allocation
2133 * parameters. The length units are all in realtime extents, as is the
2134 * result block number.
2135 */
2136int /* error */
2137xfs_rtallocate_extent(
2138 xfs_trans_t *tp, /* transaction pointer */
2139 xfs_rtblock_t bno, /* starting block number to allocate */
2140 xfs_extlen_t minlen, /* minimum length to allocate */
2141 xfs_extlen_t maxlen, /* maximum length to allocate */
2142 xfs_extlen_t *len, /* out: actual length allocated */
2143 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
2144 int wasdel, /* was a delayed allocation extent */
2145 xfs_extlen_t prod, /* extent product factor */
2146 xfs_rtblock_t *rtblock) /* out: start block allocated */
2147{
2148 int error; /* error value */
2149 xfs_inode_t *ip; /* inode for bitmap file */
2150 xfs_mount_t *mp; /* file system mount structure */
2151 xfs_rtblock_t r; /* result allocated block */
2152 xfs_fsblock_t sb; /* summary file block number */
2153 xfs_buf_t *sumbp; /* summary file block buffer */
2154
2155 ASSERT(minlen > 0 && minlen <= maxlen);
2156 mp = tp->t_mountp;
2157 /*
2158 * If prod is set then figure out what to do to minlen and maxlen.
2159 */
2160 if (prod > 1) {
2161 xfs_extlen_t i;
2162
2163 if ((i = maxlen % prod))
2164 maxlen -= i;
2165 if ((i = minlen % prod))
2166 minlen += prod - i;
2167 if (maxlen < minlen) {
2168 *rtblock = NULLRTBLOCK;
2169 return 0;
2170 }
2171 }
2172 /*
2173 * Lock out other callers by grabbing the bitmap inode lock.
2174 */
2175 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, XFS_ILOCK_EXCL, &ip);
2176 if (error) {
2177 return error;
2178 }
2179 sumbp = NULL;
2180 /*
2181 * Allocate by size, or near another block, or exactly at some block.
2182 */
2183 switch (type) {
2184 case XFS_ALLOCTYPE_ANY_AG:
2185 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2186 &sumbp, &sb, prod, &r);
2187 break;
2188 case XFS_ALLOCTYPE_NEAR_BNO:
2189 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2190 len, &sumbp, &sb, prod, &r);
2191 break;
2192 case XFS_ALLOCTYPE_THIS_BNO:
2193 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2194 len, &sumbp, &sb, prod, &r);
2195 break;
2196 default:
2197 ASSERT(0);
2198 }
2199 if (error) {
2200 return error;
2201 }
2202 /*
2203 * If it worked, update the superblock.
2204 */
2205 if (r != NULLRTBLOCK) {
2206 long slen = (long)*len;
2207
2208 ASSERT(*len >= minlen && *len <= maxlen);
2209 if (wasdel)
2210 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2211 else
2212 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2213 }
2214 *rtblock = r;
2215 return 0;
2216}
2217
2218/*
2219 * Free an extent in the realtime subvolume. Length is expressed in
2220 * realtime extents, as is the block number.
2221 */
2222int /* error */
2223xfs_rtfree_extent(
2224 xfs_trans_t *tp, /* transaction pointer */
2225 xfs_rtblock_t bno, /* starting block number to free */
2226 xfs_extlen_t len) /* length of extent freed */
2227{
2228 int error; /* error value */
2229 xfs_inode_t *ip; /* bitmap file inode */
2230 xfs_mount_t *mp; /* file system mount structure */
2231 xfs_fsblock_t sb; /* summary file block number */
2232 xfs_buf_t *sumbp; /* summary file block buffer */
2233
2234 mp = tp->t_mountp;
2235 /*
2236 * Synchronize by locking the bitmap inode.
2237 */
2238 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, XFS_ILOCK_EXCL, &ip);
2239 if (error) {
2240 return error;
2241 }
2242#if defined(__KERNEL__) && defined(DEBUG)
2243 /*
2244 * Check to see that this whole range is currently allocated.
2245 */
2246 {
2247 int stat; /* result from checking range */
2248
2249 error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2250 if (error) {
2251 return error;
2252 }
2253 ASSERT(stat);
2254 }
2255#endif
2256 sumbp = NULL;
2257 /*
2258 * Free the range of realtime blocks.
2259 */
2260 error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2261 if (error) {
2262 return error;
2263 }
2264 /*
2265 * Mark more blocks free in the superblock.
2266 */
2267 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2268 /*
2269 * If we've now freed all the blocks, reset the file sequence
2270 * number to 0.
2271 */
2272 if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2273 mp->m_sb.sb_rextents) {
2274 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2275 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2276 *(__uint64_t *)&ip->i_d.di_atime = 0;
2277 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2278 }
2279 return 0;
2280}
2281
2282/*
2283 * Initialize realtime fields in the mount structure.
2284 */
2285int /* error */
2286xfs_rtmount_init(
2287 xfs_mount_t *mp) /* file system mount structure */
2288{
2289 xfs_buf_t *bp; /* buffer for last block of subvolume */
2290 xfs_daddr_t d; /* address of last block of subvolume */
2291 int error; /* error return value */
2292 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
2293
2294 sbp = &mp->m_sb;
2295 if (sbp->sb_rblocks == 0)
2296 return 0;
2297 if (mp->m_rtdev_targp == NULL) {
2298 cmn_err(CE_WARN,
2299 "XFS: This filesystem has a realtime volume, use rtdev=device option");
2300 return XFS_ERROR(ENODEV);
2301 }
2302 mp->m_rsumlevels = sbp->sb_rextslog + 1;
2303 mp->m_rsumsize =
2304 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2305 sbp->sb_rbmblocks;
2306 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2307 mp->m_rbmip = mp->m_rsumip = NULL;
2308 /*
2309 * Check that the realtime section is an ok size.
2310 */
2311 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2312 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2313 cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
2314 (unsigned long long) XFS_BB_TO_FSB(mp, d),
2315 (unsigned long long) mp->m_sb.sb_rblocks);
2316 return XFS_ERROR(E2BIG);
2317 }
2318 error = xfs_read_buf(mp, mp->m_rtdev_targp,
2319 d - XFS_FSB_TO_BB(mp, 1),
2320 XFS_FSB_TO_BB(mp, 1), 0, &bp);
2321 if (error) {
2322 cmn_err(CE_WARN,
2323 "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
2324 if (error == ENOSPC)
2325 return XFS_ERROR(E2BIG);
2326 return error;
2327 }
2328 xfs_buf_relse(bp);
2329 return 0;
2330}
2331
2332/*
2333 * Get the bitmap and summary inodes into the mount structure
2334 * at mount time.
2335 */
2336int /* error */
2337xfs_rtmount_inodes(
2338 xfs_mount_t *mp) /* file system mount structure */
2339{
2340 int error; /* error return value */
2341 xfs_sb_t *sbp;
2342
2343 sbp = &mp->m_sb;
2344 if (sbp->sb_rbmino == NULLFSINO)
2345 return 0;
2346 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
2347 if (error)
2348 return error;
2349 ASSERT(mp->m_rbmip != NULL);
2350 ASSERT(sbp->sb_rsumino != NULLFSINO);
2351 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
2352 if (error) {
2353 VN_RELE(XFS_ITOV(mp->m_rbmip));
2354 return error;
2355 }
2356 ASSERT(mp->m_rsumip != NULL);
2357 return 0;
2358}
2359
2360/*
2361 * Pick an extent for allocation at the start of a new realtime file.
2362 * Use the sequence number stored in the atime field of the bitmap inode.
2363 * Translate this to a fraction of the rtextents, and return the product
2364 * of rtextents and the fraction.
2365 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2366 */
2367int /* error */
2368xfs_rtpick_extent(
2369 xfs_mount_t *mp, /* file system mount point */
2370 xfs_trans_t *tp, /* transaction pointer */
2371 xfs_extlen_t len, /* allocation length (rtextents) */
2372 xfs_rtblock_t *pick) /* result rt extent */
2373{
2374 xfs_rtblock_t b; /* result block */
2375 int error; /* error return value */
2376 xfs_inode_t *ip; /* bitmap incore inode */
2377 int log2; /* log of sequence number */
2378 __uint64_t resid; /* residual after log removed */
2379 __uint64_t seq; /* sequence number of file creation */
2380 __uint64_t *seqp; /* pointer to seqno in inode */
2381
2382 error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0, XFS_ILOCK_EXCL, &ip);
2383 if (error)
2384 return error;
2385 ASSERT(ip == mp->m_rbmip);
2386 seqp = (__uint64_t *)&ip->i_d.di_atime;
2387 if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2388 ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2389 *seqp = 0;
2390 }
2391 seq = *seqp;
2392 if ((log2 = xfs_highbit64(seq)) == -1)
2393 b = 0;
2394 else {
2395 resid = seq - (1ULL << log2);
2396 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2397 (log2 + 1);
2398 if (b >= mp->m_sb.sb_rextents)
2399 b = do_mod(b, mp->m_sb.sb_rextents);
2400 if (b + len > mp->m_sb.sb_rextents)
2401 b = mp->m_sb.sb_rextents - len;
2402 }
2403 *seqp = seq + 1;
2404 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2405 *pick = b;
2406 return 0;
2407}
2408
2409#ifdef DEBUG
2410/*
2411 * Debug code: print out the value of a range in the bitmap.
2412 */
2413void
2414xfs_rtprint_range(
2415 xfs_mount_t *mp, /* file system mount structure */
2416 xfs_trans_t *tp, /* transaction pointer */
2417 xfs_rtblock_t start, /* starting block to print */
2418 xfs_extlen_t len) /* length to print */
2419{
2420 xfs_extlen_t i; /* block number in the extent */
2421
2422 printk("%Ld: ", (long long)start);
2423 for (i = 0; i < len; i++)
2424 printk("%d", xfs_rtcheck_bit(mp, tp, start + i, 1));
2425 printk("\n");
2426}
2427
2428/*
2429 * Debug code: print the summary file.
2430 */
2431void
2432xfs_rtprint_summary(
2433 xfs_mount_t *mp, /* file system mount structure */
2434 xfs_trans_t *tp) /* transaction pointer */
2435{
2436 xfs_suminfo_t c; /* summary data */
2437 xfs_rtblock_t i; /* bitmap block number */
2438 int l; /* summary information level */
2439 int p; /* flag for printed anything */
2440 xfs_fsblock_t sb; /* summary block number */
2441 xfs_buf_t *sumbp; /* summary block buffer */
2442
2443 sumbp = NULL;
2444 for (l = 0; l < mp->m_rsumlevels; l++) {
2445 for (p = 0, i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
2446 (void)xfs_rtget_summary(mp, tp, l, i, &sumbp, &sb, &c);
2447 if (c) {
2448 if (!p) {
2449 printk("%Ld-%Ld:", 1LL << l,
2450 XFS_RTMIN((1LL << l) +
2451 ((1LL << l) - 1LL),
2452 mp->m_sb.sb_rextents));
2453 p = 1;
2454 }
2455 printk(" %Ld:%d", (long long)i, c);
2456 }
2457 }
2458 if (p)
2459 printk("\n");
2460 }
2461 if (sumbp)
2462 xfs_trans_brelse(tp, sumbp);
2463}
2464#endif /* DEBUG */