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