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