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