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