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