blob: d1160cce5dad80c91a4f7604ea0423408e2145b7 [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"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100030#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_bmap_btree.h"
32#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner1922c942010-09-22 10:47:20 +100037#include "xfs_buf.h"
Dave Chinner33479e02012-10-08 21:56:11 +110038#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_dinode.h"
Dave Chinnerc963c612013-10-15 09:17:56 +110040#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
43/*
Dave Chinnerc963c612013-10-15 09:17:56 +110044 * Read and return the summary information for a given extent size,
45 * bitmap block combination.
46 * Keeps track of a current summary block, so we don't keep reading
47 * it from the buffer cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Eric Sandeenafabfd32014-09-09 11:58:42 +100049int
Dave Chinnerc963c612013-10-15 09:17:56 +110050xfs_rtget_summary(
51 xfs_mount_t *mp, /* file system mount structure */
52 xfs_trans_t *tp, /* transaction pointer */
53 int log, /* log2 of extent size */
54 xfs_rtblock_t bbno, /* bitmap block number */
55 xfs_buf_t **rbpp, /* in/out: summary block buffer */
56 xfs_fsblock_t *rsb, /* in/out: summary block number */
57 xfs_suminfo_t *sum) /* out: summary info for this block */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Eric Sandeenafabfd32014-09-09 11:58:42 +100059 return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
Dave Chinnerc963c612013-10-15 09:17:56 +110060}
61
Dave Chinnerc963c612013-10-15 09:17:56 +110062/*
63 * Return whether there are any free extents in the size range given
64 * by low and high, for the bitmap block bbno.
65 */
66STATIC int /* error */
67xfs_rtany_summary(
68 xfs_mount_t *mp, /* file system mount structure */
69 xfs_trans_t *tp, /* transaction pointer */
70 int low, /* low log2 extent size */
71 int high, /* high log2 extent size */
72 xfs_rtblock_t bbno, /* bitmap block number */
73 xfs_buf_t **rbpp, /* in/out: summary block buffer */
74 xfs_fsblock_t *rsb, /* in/out: summary block number */
75 int *stat) /* out: any good extents here? */
76{
77 int error; /* error value */
78 int log; /* loop counter, log2 of ext. size */
79 xfs_suminfo_t sum; /* summary data */
80
81 /*
82 * Loop over logs of extent sizes. Order is irrelevant.
83 */
84 for (log = low; log <= high; log++) {
85 /*
86 * Get one summary datum.
87 */
88 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
89 if (error) {
90 return error;
91 }
92 /*
93 * If there are any, return success.
94 */
95 if (sum) {
96 *stat = 1;
97 return 0;
98 }
99 }
100 /*
101 * Found nothing, return failure.
102 */
103 *stat = 0;
104 return 0;
105}
106
107
108/*
109 * Copy and transform the summary file, given the old and new
110 * parameters in the mount structures.
111 */
112STATIC int /* error */
113xfs_rtcopy_summary(
114 xfs_mount_t *omp, /* old file system mount point */
115 xfs_mount_t *nmp, /* new file system mount point */
116 xfs_trans_t *tp) /* transaction pointer */
117{
118 xfs_rtblock_t bbno; /* bitmap block number */
119 xfs_buf_t *bp; /* summary buffer */
120 int error; /* error return value */
121 int log; /* summary level number (log length) */
122 xfs_suminfo_t sum; /* summary data */
123 xfs_fsblock_t sumbno; /* summary block number */
124
125 bp = NULL;
126 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
127 for (bbno = omp->m_sb.sb_rbmblocks - 1;
128 (xfs_srtblock_t)bbno >= 0;
129 bbno--) {
130 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
131 &sumbno, &sum);
132 if (error)
133 return error;
134 if (sum == 0)
135 continue;
136 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
137 &bp, &sumbno);
138 if (error)
139 return error;
140 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
141 &bp, &sumbno);
142 if (error)
143 return error;
144 ASSERT(sum > 0);
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147 return 0;
Dave Chinnerc963c612013-10-15 09:17:56 +1100148}
149/*
150 * Mark an extent specified by start and len allocated.
151 * Updates all the summary information as well as the bitmap.
152 */
153STATIC int /* error */
154xfs_rtallocate_range(
155 xfs_mount_t *mp, /* file system mount point */
156 xfs_trans_t *tp, /* transaction pointer */
157 xfs_rtblock_t start, /* start block to allocate */
158 xfs_extlen_t len, /* length to allocate */
159 xfs_buf_t **rbpp, /* in/out: summary block buffer */
160 xfs_fsblock_t *rsb) /* in/out: summary block number */
161{
162 xfs_rtblock_t end; /* end of the allocated extent */
163 int error; /* error value */
164 xfs_rtblock_t postblock = 0; /* first block allocated > end */
165 xfs_rtblock_t preblock = 0; /* first block allocated < start */
Kamal Dasu5575acc2012-02-23 00:41:39 +0000166
Dave Chinnerc963c612013-10-15 09:17:56 +1100167 end = start + len - 1;
168 /*
169 * Assume we're allocating out of the middle of a free extent.
170 * We need to find the beginning and end of the extent so we can
171 * properly update the summary.
172 */
173 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
174 if (error) {
175 return error;
176 }
177 /*
178 * Find the next allocated block (end of free extent).
179 */
180 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
181 &postblock);
182 if (error) {
183 return error;
184 }
185 /*
186 * Decrement the summary information corresponding to the entire
187 * (old) free extent.
188 */
189 error = xfs_rtmodify_summary(mp, tp,
190 XFS_RTBLOCKLOG(postblock + 1 - preblock),
191 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
192 if (error) {
193 return error;
194 }
195 /*
196 * If there are blocks not being allocated at the front of the
197 * old extent, add summary data for them to be free.
198 */
199 if (preblock < start) {
200 error = xfs_rtmodify_summary(mp, tp,
201 XFS_RTBLOCKLOG(start - preblock),
202 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
203 if (error) {
204 return error;
205 }
206 }
207 /*
208 * If there are blocks not being allocated at the end of the
209 * old extent, add summary data for them to be free.
210 */
211 if (postblock > end) {
212 error = xfs_rtmodify_summary(mp, tp,
213 XFS_RTBLOCKLOG(postblock - end),
214 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
215 if (error) {
216 return error;
217 }
218 }
219 /*
220 * Modify the bitmap to mark this extent allocated.
221 */
222 error = xfs_rtmodify_range(mp, tp, start, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return error;
224}
225
226/*
227 * Attempt to allocate an extent minlen<=len<=maxlen starting from
228 * bitmap block bbno. If we don't get maxlen then use prod to trim
229 * the length, if given. Returns error; returns starting block in *rtblock.
230 * The lengths are all in rtextents.
231 */
232STATIC int /* error */
233xfs_rtallocate_extent_block(
234 xfs_mount_t *mp, /* file system mount point */
235 xfs_trans_t *tp, /* transaction pointer */
236 xfs_rtblock_t bbno, /* bitmap block number */
237 xfs_extlen_t minlen, /* minimum length to allocate */
238 xfs_extlen_t maxlen, /* maximum length to allocate */
239 xfs_extlen_t *len, /* out: actual length allocated */
240 xfs_rtblock_t *nextp, /* out: next block to try */
241 xfs_buf_t **rbpp, /* in/out: summary block buffer */
242 xfs_fsblock_t *rsb, /* in/out: summary block number */
243 xfs_extlen_t prod, /* extent product factor */
244 xfs_rtblock_t *rtblock) /* out: start block allocated */
245{
246 xfs_rtblock_t besti; /* best rtblock found so far */
247 xfs_rtblock_t bestlen; /* best length found so far */
248 xfs_rtblock_t end; /* last rtblock in chunk */
249 int error; /* error value */
250 xfs_rtblock_t i; /* current rtblock trying */
251 xfs_rtblock_t next; /* next rtblock to try */
252 int stat; /* status from internal calls */
253
254 /*
255 * Loop over all the extents starting in this bitmap block,
256 * looking for one that's long enough.
257 */
258 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
259 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
260 i <= end;
261 i++) {
262 /*
263 * See if there's a free extent of maxlen starting at i.
264 * If it's not so then next will contain the first non-free.
265 */
266 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
267 if (error) {
268 return error;
269 }
270 if (stat) {
271 /*
272 * i for maxlen is all free, allocate and return that.
273 */
274 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
275 rsb);
276 if (error) {
277 return error;
278 }
279 *len = maxlen;
280 *rtblock = i;
281 return 0;
282 }
283 /*
284 * In the case where we have a variable-sized allocation
285 * request, figure out how big this free piece is,
286 * and if it's big enough for the minimum, and the best
287 * so far, remember it.
288 */
289 if (minlen < maxlen) {
290 xfs_rtblock_t thislen; /* this extent size */
291
292 thislen = next - i;
293 if (thislen >= minlen && thislen > bestlen) {
294 besti = i;
295 bestlen = thislen;
296 }
297 }
298 /*
299 * If not done yet, find the start of the next free space.
300 */
301 if (next < end) {
302 error = xfs_rtfind_forw(mp, tp, next, end, &i);
303 if (error) {
304 return error;
305 }
306 } else
307 break;
308 }
309 /*
310 * Searched the whole thing & didn't find a maxlen free extent.
311 */
312 if (minlen < maxlen && besti != -1) {
313 xfs_extlen_t p; /* amount to trim length by */
314
315 /*
316 * If size should be a multiple of prod, make that so.
317 */
318 if (prod > 1 && (p = do_mod(bestlen, prod)))
319 bestlen -= p;
320 /*
321 * Allocate besti for bestlen & return that.
322 */
323 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
324 if (error) {
325 return error;
326 }
327 *len = bestlen;
328 *rtblock = besti;
329 return 0;
330 }
331 /*
332 * Allocation failed. Set *nextp to the next block to try.
333 */
334 *nextp = next;
335 *rtblock = NULLRTBLOCK;
336 return 0;
337}
338
339/*
340 * Allocate an extent of length minlen<=len<=maxlen, starting at block
341 * bno. If we don't get maxlen then use prod to trim the length, if given.
342 * Returns error; returns starting block in *rtblock.
343 * The lengths are all in rtextents.
344 */
345STATIC int /* error */
346xfs_rtallocate_extent_exact(
347 xfs_mount_t *mp, /* file system mount point */
348 xfs_trans_t *tp, /* transaction pointer */
349 xfs_rtblock_t bno, /* starting block number to allocate */
350 xfs_extlen_t minlen, /* minimum length to allocate */
351 xfs_extlen_t maxlen, /* maximum length to allocate */
352 xfs_extlen_t *len, /* out: actual length allocated */
353 xfs_buf_t **rbpp, /* in/out: summary block buffer */
354 xfs_fsblock_t *rsb, /* in/out: summary block number */
355 xfs_extlen_t prod, /* extent product factor */
356 xfs_rtblock_t *rtblock) /* out: start block allocated */
357{
358 int error; /* error value */
359 xfs_extlen_t i; /* extent length trimmed due to prod */
360 int isfree; /* extent is free */
361 xfs_rtblock_t next; /* next block to try (dummy) */
362
363 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
364 /*
365 * Check if the range in question (for maxlen) is free.
366 */
367 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
368 if (error) {
369 return error;
370 }
371 if (isfree) {
372 /*
373 * If it is, allocate it and return success.
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 * If not, allocate what there is, if it's at least minlen.
385 */
386 maxlen = next - bno;
387 if (maxlen < minlen) {
388 /*
389 * Failed, return failure status.
390 */
391 *rtblock = NULLRTBLOCK;
392 return 0;
393 }
394 /*
395 * Trim off tail of extent, if prod is specified.
396 */
397 if (prod > 1 && (i = maxlen % prod)) {
398 maxlen -= i;
399 if (maxlen < minlen) {
400 /*
401 * Now we can't do it, return failure status.
402 */
403 *rtblock = NULLRTBLOCK;
404 return 0;
405 }
406 }
407 /*
408 * Allocate what we can and return it.
409 */
410 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
411 if (error) {
412 return error;
413 }
414 *len = maxlen;
415 *rtblock = bno;
416 return 0;
417}
418
419/*
420 * Allocate an extent of length minlen<=len<=maxlen, starting as near
421 * to bno as possible. If we don't get maxlen then use prod to trim
422 * the length, if given. The lengths are all in rtextents.
423 */
424STATIC int /* error */
425xfs_rtallocate_extent_near(
426 xfs_mount_t *mp, /* file system mount point */
427 xfs_trans_t *tp, /* transaction pointer */
428 xfs_rtblock_t bno, /* starting block number to allocate */
429 xfs_extlen_t minlen, /* minimum length to allocate */
430 xfs_extlen_t maxlen, /* maximum length to allocate */
431 xfs_extlen_t *len, /* out: actual length allocated */
432 xfs_buf_t **rbpp, /* in/out: summary block buffer */
433 xfs_fsblock_t *rsb, /* in/out: summary block number */
434 xfs_extlen_t prod, /* extent product factor */
435 xfs_rtblock_t *rtblock) /* out: start block allocated */
436{
437 int any; /* any useful extents from summary */
438 xfs_rtblock_t bbno; /* bitmap block number */
439 int error; /* error value */
440 int i; /* bitmap block offset (loop control) */
441 int j; /* secondary loop control */
442 int log2len; /* log2 of minlen */
443 xfs_rtblock_t n; /* next block to try */
444 xfs_rtblock_t r; /* result block */
445
446 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
447 /*
448 * If the block number given is off the end, silently set it to
449 * the last block.
450 */
451 if (bno >= mp->m_sb.sb_rextents)
452 bno = mp->m_sb.sb_rextents - 1;
453 /*
454 * Try the exact allocation first.
455 */
456 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
457 rbpp, rsb, prod, &r);
458 if (error) {
459 return error;
460 }
461 /*
462 * If the exact allocation worked, return that.
463 */
464 if (r != NULLRTBLOCK) {
465 *rtblock = r;
466 return 0;
467 }
468 bbno = XFS_BITTOBLOCK(mp, bno);
469 i = 0;
David Chinner79071eb2008-08-13 15:41:12 +1000470 ASSERT(minlen != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 log2len = xfs_highbit32(minlen);
472 /*
473 * Loop over all bitmap blocks (bbno + i is current block).
474 */
475 for (;;) {
476 /*
477 * Get summary information of extents of all useful levels
478 * starting in this bitmap block.
479 */
480 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
481 bbno + i, rbpp, rsb, &any);
482 if (error) {
483 return error;
484 }
485 /*
486 * If there are any useful extents starting here, try
487 * allocating one.
488 */
489 if (any) {
490 /*
491 * On the positive side of the starting location.
492 */
493 if (i >= 0) {
494 /*
495 * Try to allocate an extent starting in
496 * this block.
497 */
498 error = xfs_rtallocate_extent_block(mp, tp,
499 bbno + i, minlen, maxlen, len, &n, rbpp,
500 rsb, prod, &r);
501 if (error) {
502 return error;
503 }
504 /*
505 * If it worked, return it.
506 */
507 if (r != NULLRTBLOCK) {
508 *rtblock = r;
509 return 0;
510 }
511 }
512 /*
513 * On the negative side of the starting location.
514 */
515 else { /* i < 0 */
516 /*
517 * Loop backwards through the bitmap blocks from
518 * the starting point-1 up to where we are now.
519 * There should be an extent which ends in this
520 * bitmap block and is long enough.
521 */
522 for (j = -1; j > i; j--) {
523 /*
524 * Grab the summary information for
525 * this bitmap block.
526 */
527 error = xfs_rtany_summary(mp, tp,
528 log2len, mp->m_rsumlevels - 1,
529 bbno + j, rbpp, rsb, &any);
530 if (error) {
531 return error;
532 }
533 /*
534 * If there's no extent given in the
535 * summary that means the extent we
536 * found must carry over from an
537 * earlier block. If there is an
538 * extent given, we've already tried
539 * that allocation, don't do it again.
540 */
541 if (any)
542 continue;
543 error = xfs_rtallocate_extent_block(mp,
544 tp, bbno + j, minlen, maxlen,
545 len, &n, rbpp, rsb, prod, &r);
546 if (error) {
547 return error;
548 }
549 /*
550 * If it works, return the extent.
551 */
552 if (r != NULLRTBLOCK) {
553 *rtblock = r;
554 return 0;
555 }
556 }
557 /*
558 * There weren't intervening bitmap blocks
559 * with a long enough extent, or the
560 * allocation didn't work for some reason
561 * (i.e. it's a little * too short).
562 * Try to allocate from the summary block
563 * that we found.
564 */
565 error = xfs_rtallocate_extent_block(mp, tp,
566 bbno + i, minlen, maxlen, len, &n, rbpp,
567 rsb, prod, &r);
568 if (error) {
569 return error;
570 }
571 /*
572 * If it works, return the extent.
573 */
574 if (r != NULLRTBLOCK) {
575 *rtblock = r;
576 return 0;
577 }
578 }
579 }
580 /*
581 * Loop control. If we were on the positive side, and there's
582 * still more blocks on the negative side, go there.
583 */
584 if (i > 0 && (int)bbno - i >= 0)
585 i = -i;
586 /*
587 * If positive, and no more negative, but there are more
588 * positive, go there.
589 */
590 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
591 i++;
592 /*
593 * If negative or 0 (just started), and there are positive
594 * blocks to go, go there. The 0 case moves to block 1.
595 */
596 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
597 i = 1 - i;
598 /*
599 * If negative or 0 and there are more negative blocks,
600 * go there.
601 */
602 else if (i <= 0 && (int)bbno + i > 0)
603 i--;
604 /*
605 * Must be done. Return failure.
606 */
607 else
608 break;
609 }
610 *rtblock = NULLRTBLOCK;
611 return 0;
612}
613
614/*
615 * Allocate an extent of length minlen<=len<=maxlen, with no position
616 * specified. If we don't get maxlen then use prod to trim
617 * the length, if given. The lengths are all in rtextents.
618 */
619STATIC int /* error */
620xfs_rtallocate_extent_size(
621 xfs_mount_t *mp, /* file system mount point */
622 xfs_trans_t *tp, /* transaction pointer */
623 xfs_extlen_t minlen, /* minimum length to allocate */
624 xfs_extlen_t maxlen, /* maximum length to allocate */
625 xfs_extlen_t *len, /* out: actual length allocated */
626 xfs_buf_t **rbpp, /* in/out: summary block buffer */
627 xfs_fsblock_t *rsb, /* in/out: summary block number */
628 xfs_extlen_t prod, /* extent product factor */
629 xfs_rtblock_t *rtblock) /* out: start block allocated */
630{
631 int error; /* error value */
632 int i; /* bitmap block number */
633 int l; /* level number (loop control) */
634 xfs_rtblock_t n; /* next block to be tried */
635 xfs_rtblock_t r; /* result block number */
636 xfs_suminfo_t sum; /* summary information for extents */
637
638 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
David Chinner79071eb2008-08-13 15:41:12 +1000639 ASSERT(maxlen != 0);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Loop over all the levels starting with maxlen.
643 * At each level, look at all the bitmap blocks, to see if there
644 * are extents starting there that are long enough (>= maxlen).
645 * Note, only on the initial level can the allocation fail if
646 * the summary says there's an extent.
647 */
648 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
649 /*
650 * Loop over all the bitmap blocks.
651 */
652 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
653 /*
654 * Get the summary for this level/block.
655 */
656 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
657 &sum);
658 if (error) {
659 return error;
660 }
661 /*
662 * Nothing there, on to the next block.
663 */
664 if (!sum)
665 continue;
666 /*
667 * Try allocating the extent.
668 */
669 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
670 maxlen, len, &n, rbpp, rsb, prod, &r);
671 if (error) {
672 return error;
673 }
674 /*
675 * If it worked, return that.
676 */
677 if (r != NULLRTBLOCK) {
678 *rtblock = r;
679 return 0;
680 }
681 /*
682 * If the "next block to try" returned from the
683 * allocator is beyond the next bitmap block,
684 * skip to that bitmap block.
685 */
686 if (XFS_BITTOBLOCK(mp, n) > i + 1)
687 i = XFS_BITTOBLOCK(mp, n) - 1;
688 }
689 }
690 /*
691 * Didn't find any maxlen blocks. Try smaller ones, unless
692 * we're asking for a fixed size extent.
693 */
694 if (minlen > --maxlen) {
695 *rtblock = NULLRTBLOCK;
696 return 0;
697 }
David Chinner79071eb2008-08-13 15:41:12 +1000698 ASSERT(minlen != 0);
699 ASSERT(maxlen != 0);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * Loop over sizes, from maxlen down to minlen.
703 * This time, when we do the allocations, allow smaller ones
704 * to succeed.
705 */
706 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
707 /*
708 * Loop over all the bitmap blocks, try an allocation
709 * starting in that block.
710 */
711 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
712 /*
713 * Get the summary information for this level/block.
714 */
715 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
716 &sum);
717 if (error) {
718 return error;
719 }
720 /*
721 * If nothing there, go on to next.
722 */
723 if (!sum)
724 continue;
725 /*
726 * Try the allocation. Make sure the specified
727 * minlen/maxlen are in the possible range for
728 * this summary level.
729 */
730 error = xfs_rtallocate_extent_block(mp, tp, i,
731 XFS_RTMAX(minlen, 1 << l),
732 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
733 len, &n, rbpp, rsb, prod, &r);
734 if (error) {
735 return error;
736 }
737 /*
738 * If it worked, return that extent.
739 */
740 if (r != NULLRTBLOCK) {
741 *rtblock = r;
742 return 0;
743 }
744 /*
745 * If the "next block to try" returned from the
746 * allocator is beyond the next bitmap block,
747 * skip to that bitmap block.
748 */
749 if (XFS_BITTOBLOCK(mp, n) > i + 1)
750 i = XFS_BITTOBLOCK(mp, n) - 1;
751 }
752 }
753 /*
754 * Got nothing, return failure.
755 */
756 *rtblock = NULLRTBLOCK;
757 return 0;
758}
759
760/*
Dave Chinnerc963c612013-10-15 09:17:56 +1100761 * Allocate space to the bitmap or summary file, and zero it, for growfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
763STATIC int /* error */
Dave Chinnerc963c612013-10-15 09:17:56 +1100764xfs_growfs_rt_alloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 xfs_mount_t *mp, /* file system mount point */
Dave Chinnerc963c612013-10-15 09:17:56 +1100766 xfs_extlen_t oblocks, /* old count of blocks */
767 xfs_extlen_t nblocks, /* new count of blocks */
768 xfs_inode_t *ip) /* inode (bitmap/summary) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Dave Chinnerc963c612013-10-15 09:17:56 +1100770 xfs_fileoff_t bno; /* block number in file */
771 xfs_buf_t *bp; /* temporary buffer for zeroing */
772 int committed; /* transaction committed flag */
773 xfs_daddr_t d; /* disk block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int error; /* error return value */
Dave Chinnerc963c612013-10-15 09:17:56 +1100775 xfs_fsblock_t firstblock; /* first block allocated in xaction */
776 xfs_bmap_free_t flist; /* list of freed blocks */
777 xfs_fsblock_t fsbno; /* filesystem block for bno */
778 xfs_bmbt_irec_t map; /* block map output */
779 int nmap; /* number of block maps */
780 int resblks; /* space reservation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dave Chinnerc963c612013-10-15 09:17:56 +1100782 /*
783 * Allocate space to the file, as necessary.
784 */
785 while (oblocks < nblocks) {
786 int cancelflags = 0;
787 xfs_trans_t *tp;
788
789 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
790 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
791 /*
792 * Reserve space & log for one extent added to the file.
793 */
Brian Foster70bbca072014-02-07 14:53:50 +1100794 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtalloc,
Dave Chinnerc963c612013-10-15 09:17:56 +1100795 resblks, 0);
796 if (error)
797 goto error_cancel;
798 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
799 /*
800 * Lock the inode.
801 */
802 xfs_ilock(ip, XFS_ILOCK_EXCL);
803 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
804
805 xfs_bmap_init(&flist, &firstblock);
806 /*
807 * Allocate blocks to the bitmap file.
808 */
809 nmap = 1;
810 cancelflags |= XFS_TRANS_ABORT;
811 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
812 XFS_BMAPI_METADATA, &firstblock,
813 resblks, &map, &nmap, &flist);
814 if (!error && nmap < 1)
Dave Chinner24513372014-06-25 14:58:08 +1000815 error = -ENOSPC;
Dave Chinnerc963c612013-10-15 09:17:56 +1100816 if (error)
817 goto error_cancel;
818 /*
819 * Free any blocks freed up in the transaction, then commit.
820 */
821 error = xfs_bmap_finish(&tp, &flist, &committed);
822 if (error)
823 goto error_cancel;
824 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
825 if (error)
826 goto error;
827 /*
828 * Now we need to clear the allocated blocks.
829 * Do this one block per transaction, to keep it simple.
830 */
831 cancelflags = 0;
832 for (bno = map.br_startoff, fsbno = map.br_startblock;
833 bno < map.br_startoff + map.br_blockcount;
834 bno++, fsbno++) {
835 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
836 /*
837 * Reserve log for one block zeroing.
838 */
839 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
840 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (error)
Dave Chinnerc963c612013-10-15 09:17:56 +1100842 goto error_cancel;
843 /*
844 * Lock the bitmap inode.
845 */
846 xfs_ilock(ip, XFS_ILOCK_EXCL);
847 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
848 /*
849 * Get a buffer for the block.
850 */
851 d = XFS_FSB_TO_DADDR(mp, fsbno);
852 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
853 mp->m_bsize, 0);
854 if (bp == NULL) {
Dave Chinner24513372014-06-25 14:58:08 +1000855 error = -EIO;
Dave Chinnerc963c612013-10-15 09:17:56 +1100856error_cancel:
857 xfs_trans_cancel(tp, cancelflags);
858 goto error;
859 }
860 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
861 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
862 /*
863 * Commit the transaction.
864 */
865 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (error)
Dave Chinnerc963c612013-10-15 09:17:56 +1100867 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Dave Chinnerc963c612013-10-15 09:17:56 +1100869 /*
870 * Go on to the next extent, if any.
871 */
872 oblocks = map.br_startoff + map.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dave Chinnerc963c612013-10-15 09:17:56 +1100876error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return error;
878}
879
880/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * Visible (exported) functions.
882 */
883
884/*
885 * Grow the realtime area of the filesystem.
886 */
887int
888xfs_growfs_rt(
889 xfs_mount_t *mp, /* mount point for filesystem */
890 xfs_growfs_rt_t *in) /* growfs rt input struct */
891{
892 xfs_rtblock_t bmbno; /* bitmap block number */
893 xfs_buf_t *bp; /* temporary buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 xfs_mount_t *nmp; /* new (fake) mount structure */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000896 xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000898 xfs_rtblock_t nrextents; /* new number of realtime extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 uint8_t nrextslog; /* new log2 of sb_rextents */
900 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
901 uint nrsumlevels; /* new rt summary levels */
902 uint nrsumsize; /* new size of rt summary, bytes */
903 xfs_sb_t *nsbp; /* new superblock */
904 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
905 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
906 xfs_sb_t *sbp; /* old superblock */
907 xfs_fsblock_t sumbno; /* summary block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 sbp = &mp->m_sb;
910 /*
911 * Initial error checking.
912 */
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600913 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000914 return -EPERM;
Eric Sesterhenn73024cf2006-06-28 08:42:26 +1000915 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
917 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
Dave Chinner24513372014-06-25 14:58:08 +1000918 return -EINVAL;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000919 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
920 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /*
922 * Read in the last block of the device, make sure it exists.
923 */
Dave Chinnere70b73f2012-04-23 15:58:49 +1000924 bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +1000925 XFS_FSB_TO_BB(mp, nrblocks - 1),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100926 XFS_FSB_TO_BB(mp, 1), 0, NULL);
Dave Chinner1922c942010-09-22 10:47:20 +1000927 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000928 return -EIO;
Dave Chinnereab4e632012-11-12 22:54:02 +1100929 if (bp->b_error) {
930 error = bp->b_error;
931 xfs_buf_relse(bp);
932 return error;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 xfs_buf_relse(bp);
Dave Chinner1922c942010-09-22 10:47:20 +1000935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /*
937 * Calculate new parameters. These are the final values to be reached.
938 */
939 nrextents = nrblocks;
940 do_div(nrextents, in->extsize);
Nathan Scott68c32712006-09-28 11:03:53 +1000941 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 nrextslog = xfs_highbit32(nrextents);
943 nrsumlevels = nrextslog + 1;
944 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
945 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
946 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
947 /*
948 * New summary size can't be more than half the size of
949 * the log. This prevents us from getting a log overflow,
950 * since we'll log basically the whole summary file at once.
951 */
952 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
Dave Chinner24513372014-06-25 14:58:08 +1000953 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * Get the old block counts for bitmap and summary inodes.
956 * These can't change since other growfs callers are locked out.
957 */
958 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
959 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
960 /*
961 * Allocate space to the bitmap and summary files, as necessary.
962 */
Christoph Hellwig1050c712011-02-13 13:25:31 +0000963 error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
964 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return error;
Christoph Hellwig1050c712011-02-13 13:25:31 +0000966 error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
967 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return error;
Nathan Scottd432c802006-09-28 11:03:44 +1000969 /*
970 * Allocate a new (fake) mount/sb.
971 */
972 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /*
974 * Loop over the bitmap blocks.
975 * We will do everything one bitmap block at a time.
976 * Skip the current block if it is exactly full.
977 * This also deals with the case where there were no rtextents before.
978 */
979 for (bmbno = sbp->sb_rbmblocks -
980 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
981 bmbno < nrbmblocks;
982 bmbno++) {
Dave Chinner0924b582008-11-28 14:23:34 +1100983 xfs_trans_t *tp;
984 int cancelflags = 0;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *nmp = *mp;
987 nsbp = &nmp->m_sb;
988 /*
989 * Calculate new sb and mount fields for this round.
990 */
991 nsbp->sb_rextsize = in->extsize;
992 nsbp->sb_rbmblocks = bmbno + 1;
993 nsbp->sb_rblocks =
994 XFS_RTMIN(nrblocks,
995 nsbp->sb_rbmblocks * NBBY *
996 nsbp->sb_blocksize * nsbp->sb_rextsize);
997 nsbp->sb_rextents = nsbp->sb_rblocks;
998 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
David Chinner79071eb2008-08-13 15:41:12 +1000999 ASSERT(nsbp->sb_rextents != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1001 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1002 nrsumsize =
1003 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1004 nsbp->sb_rbmblocks;
1005 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1006 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1007 /*
1008 * Start a transaction, get the log reservation.
1009 */
1010 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001011 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtfree,
1012 0, 0);
1013 if (error)
Dave Chinner0924b582008-11-28 14:23:34 +11001014 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Lock out other callers by grabbing the bitmap inode lock.
1017 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00001018 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001019 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * Update the bitmap inode's size.
1022 */
1023 mp->m_rbmip->i_d.di_size =
1024 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1025 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1026 cancelflags |= XFS_TRANS_ABORT;
1027 /*
1028 * Get the summary inode into the transaction.
1029 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00001030 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001031 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * Update the summary inode's size.
1034 */
1035 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1036 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1037 /*
1038 * Copy summary data from old to new sizes.
1039 * Do this when the real size (not block-aligned) changes.
1040 */
1041 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1042 mp->m_rsumlevels != nmp->m_rsumlevels) {
1043 error = xfs_rtcopy_summary(mp, nmp, tp);
1044 if (error)
Dave Chinner0924b582008-11-28 14:23:34 +11001045 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047 /*
1048 * Update superblock fields.
1049 */
1050 if (nsbp->sb_rextsize != sbp->sb_rextsize)
1051 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1052 nsbp->sb_rextsize - sbp->sb_rextsize);
1053 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1054 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1055 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1056 if (nsbp->sb_rblocks != sbp->sb_rblocks)
1057 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1058 nsbp->sb_rblocks - sbp->sb_rblocks);
1059 if (nsbp->sb_rextents != sbp->sb_rextents)
1060 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1061 nsbp->sb_rextents - sbp->sb_rextents);
1062 if (nsbp->sb_rextslog != sbp->sb_rextslog)
1063 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1064 nsbp->sb_rextslog - sbp->sb_rextslog);
1065 /*
1066 * Free new extent.
1067 */
1068 bp = NULL;
1069 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1070 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
Dave Chinner0924b582008-11-28 14:23:34 +11001071 if (error) {
1072error_cancel:
1073 xfs_trans_cancel(tp, cancelflags);
Nathan Scottd432c802006-09-28 11:03:44 +10001074 break;
Dave Chinner0924b582008-11-28 14:23:34 +11001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /*
1077 * Mark more blocks free in the superblock.
1078 */
1079 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1080 nsbp->sb_rextents - sbp->sb_rextents);
1081 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * Update mp values into the real mp structure.
1083 */
1084 mp->m_rsumlevels = nrsumlevels;
1085 mp->m_rsumsize = nrsumsize;
David Chinnere5720ee2008-04-10 12:21:18 +10001086
1087 error = xfs_trans_commit(tp, 0);
Dave Chinner0924b582008-11-28 14:23:34 +11001088 if (error)
David Chinnere5720ee2008-04-10 12:21:18 +10001089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Nathan Scottd432c802006-09-28 11:03:44 +10001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
Nathan Scottd432c802006-09-28 11:03:44 +10001093 * Free the fake mp structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001095 kmem_free(nmp);
Nathan Scottd432c802006-09-28 11:03:44 +10001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return error;
1098}
1099
1100/*
1101 * Allocate an extent in the realtime subvolume, with the usual allocation
1102 * parameters. The length units are all in realtime extents, as is the
1103 * result block number.
1104 */
1105int /* error */
1106xfs_rtallocate_extent(
1107 xfs_trans_t *tp, /* transaction pointer */
1108 xfs_rtblock_t bno, /* starting block number to allocate */
1109 xfs_extlen_t minlen, /* minimum length to allocate */
1110 xfs_extlen_t maxlen, /* maximum length to allocate */
1111 xfs_extlen_t *len, /* out: actual length allocated */
1112 xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
1113 int wasdel, /* was a delayed allocation extent */
1114 xfs_extlen_t prod, /* extent product factor */
1115 xfs_rtblock_t *rtblock) /* out: start block allocated */
1116{
Christoph Hellwig04e99452011-01-25 09:06:19 +00001117 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int error; /* error value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 xfs_rtblock_t r; /* result allocated block */
1120 xfs_fsblock_t sb; /* summary file block number */
1121 xfs_buf_t *sumbp; /* summary file block buffer */
1122
Christoph Hellwig04e99452011-01-25 09:06:19 +00001123 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ASSERT(minlen > 0 && minlen <= maxlen);
Christoph Hellwig04e99452011-01-25 09:06:19 +00001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * If prod is set then figure out what to do to minlen and maxlen.
1128 */
1129 if (prod > 1) {
1130 xfs_extlen_t i;
1131
1132 if ((i = maxlen % prod))
1133 maxlen -= i;
1134 if ((i = minlen % prod))
1135 minlen += prod - i;
1136 if (maxlen < minlen) {
1137 *rtblock = NULLRTBLOCK;
1138 return 0;
1139 }
1140 }
Christoph Hellwig04e99452011-01-25 09:06:19 +00001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 sumbp = NULL;
1143 /*
1144 * Allocate by size, or near another block, or exactly at some block.
1145 */
1146 switch (type) {
1147 case XFS_ALLOCTYPE_ANY_AG:
1148 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1149 &sumbp, &sb, prod, &r);
1150 break;
1151 case XFS_ALLOCTYPE_NEAR_BNO:
1152 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1153 len, &sumbp, &sb, prod, &r);
1154 break;
1155 case XFS_ALLOCTYPE_THIS_BNO:
1156 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
1157 len, &sumbp, &sb, prod, &r);
1158 break;
1159 default:
Dave Chinner24513372014-06-25 14:58:08 +10001160 error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ASSERT(0);
1162 }
Christoph Hellwig04e99452011-01-25 09:06:19 +00001163 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return error;
Christoph Hellwig04e99452011-01-25 09:06:19 +00001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /*
1167 * If it worked, update the superblock.
1168 */
1169 if (r != NULLRTBLOCK) {
1170 long slen = (long)*len;
1171
1172 ASSERT(*len >= minlen && *len <= maxlen);
1173 if (wasdel)
1174 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1175 else
1176 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1177 }
1178 *rtblock = r;
1179 return 0;
1180}
1181
1182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 * Initialize realtime fields in the mount structure.
1184 */
1185int /* error */
1186xfs_rtmount_init(
1187 xfs_mount_t *mp) /* file system mount structure */
1188{
1189 xfs_buf_t *bp; /* buffer for last block of subvolume */
1190 xfs_daddr_t d; /* address of last block of subvolume */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
1192
1193 sbp = &mp->m_sb;
1194 if (sbp->sb_rblocks == 0)
1195 return 0;
1196 if (mp->m_rtdev_targp == NULL) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001197 xfs_warn(mp,
1198 "Filesystem has a realtime volume, use rtdev=device option");
Dave Chinner24513372014-06-25 14:58:08 +10001199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 mp->m_rsumlevels = sbp->sb_rextslog + 1;
1202 mp->m_rsumsize =
1203 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1204 sbp->sb_rbmblocks;
1205 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1206 mp->m_rbmip = mp->m_rsumip = NULL;
1207 /*
1208 * Check that the realtime section is an ok size.
1209 */
1210 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1211 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001212 xfs_warn(mp, "realtime mount -- %llu != %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 (unsigned long long) XFS_BB_TO_FSB(mp, d),
1214 (unsigned long long) mp->m_sb.sb_rblocks);
Dave Chinner24513372014-06-25 14:58:08 +10001215 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Dave Chinnere70b73f2012-04-23 15:58:49 +10001217 bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +10001218 d - XFS_FSB_TO_BB(mp, 1),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001219 XFS_FSB_TO_BB(mp, 1), 0, NULL);
Dave Chinnereab4e632012-11-12 22:54:02 +11001220 if (!bp || bp->b_error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001221 xfs_warn(mp, "realtime device size check failed");
Dave Chinnereab4e632012-11-12 22:54:02 +11001222 if (bp)
1223 xfs_buf_relse(bp);
Dave Chinner24513372014-06-25 14:58:08 +10001224 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 xfs_buf_relse(bp);
1227 return 0;
1228}
1229
1230/*
1231 * Get the bitmap and summary inodes into the mount structure
1232 * at mount time.
1233 */
1234int /* error */
1235xfs_rtmount_inodes(
1236 xfs_mount_t *mp) /* file system mount structure */
1237{
1238 int error; /* error return value */
1239 xfs_sb_t *sbp;
1240
1241 sbp = &mp->m_sb;
1242 if (sbp->sb_rbmino == NULLFSINO)
1243 return 0;
Dave Chinner7b6259e2010-06-24 11:35:17 +10001244 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (error)
1246 return error;
1247 ASSERT(mp->m_rbmip != NULL);
1248 ASSERT(sbp->sb_rsumino != NULLFSINO);
Dave Chinner7b6259e2010-06-24 11:35:17 +10001249 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (error) {
Christoph Hellwig43355092008-03-27 18:01:08 +11001251 IRELE(mp->m_rbmip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return error;
1253 }
1254 ASSERT(mp->m_rsumip != NULL);
1255 return 0;
1256}
1257
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001258void
1259xfs_rtunmount_inodes(
1260 struct xfs_mount *mp)
1261{
1262 if (mp->m_rbmip)
1263 IRELE(mp->m_rbmip);
1264 if (mp->m_rsumip)
1265 IRELE(mp->m_rsumip);
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
1269 * Pick an extent for allocation at the start of a new realtime file.
1270 * Use the sequence number stored in the atime field of the bitmap inode.
1271 * Translate this to a fraction of the rtextents, and return the product
1272 * of rtextents and the fraction.
1273 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1274 */
1275int /* error */
1276xfs_rtpick_extent(
1277 xfs_mount_t *mp, /* file system mount point */
1278 xfs_trans_t *tp, /* transaction pointer */
1279 xfs_extlen_t len, /* allocation length (rtextents) */
1280 xfs_rtblock_t *pick) /* result rt extent */
1281{
1282 xfs_rtblock_t b; /* result block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int log2; /* log of sequence number */
1284 __uint64_t resid; /* residual after log removed */
1285 __uint64_t seq; /* sequence number of file creation */
1286 __uint64_t *seqp; /* pointer to seqno in inode */
1287
Christoph Hellwig04e99452011-01-25 09:06:19 +00001288 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1289
1290 seqp = (__uint64_t *)&mp->m_rbmip->i_d.di_atime;
1291 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
1292 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 *seqp = 0;
1294 }
1295 seq = *seqp;
1296 if ((log2 = xfs_highbit64(seq)) == -1)
1297 b = 0;
1298 else {
1299 resid = seq - (1ULL << log2);
1300 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1301 (log2 + 1);
1302 if (b >= mp->m_sb.sb_rextents)
1303 b = do_mod(b, mp->m_sb.sb_rextents);
1304 if (b + len > mp->m_sb.sb_rextents)
1305 b = mp->m_sb.sb_rextents - len;
1306 }
1307 *seqp = seq + 1;
Christoph Hellwig04e99452011-01-25 09:06:19 +00001308 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 *pick = b;
1310 return 0;
1311}