blob: bc471d42a968395dcb7afb36df777e1182de3225 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100014#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100017#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110018#include "xfs_bmap_btree.h"
19#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000023#include "xfs_trace.h"
Dave Chinner1922c942010-09-22 10:47:20 +100024#include "xfs_buf.h"
Dave Chinner33479e02012-10-08 21:56:11 +110025#include "xfs_icache.h"
Dave Chinnerc963c612013-10-15 09:17:56 +110026#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28
29/*
Dave Chinnerc963c612013-10-15 09:17:56 +110030 * Read and return the summary information for a given extent size,
31 * bitmap block combination.
32 * Keeps track of a current summary block, so we don't keep reading
33 * it from the buffer cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
Fengguang Wuea959612014-09-23 16:11:43 +100035static int
Dave Chinnerc963c612013-10-15 09:17:56 +110036xfs_rtget_summary(
37 xfs_mount_t *mp, /* file system mount structure */
38 xfs_trans_t *tp, /* transaction pointer */
39 int log, /* log2 of extent size */
40 xfs_rtblock_t bbno, /* bitmap block number */
41 xfs_buf_t **rbpp, /* in/out: summary block buffer */
42 xfs_fsblock_t *rsb, /* in/out: summary block number */
43 xfs_suminfo_t *sum) /* out: summary info for this block */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Eric Sandeenafabfd32014-09-09 11:58:42 +100045 return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
Dave Chinnerc963c612013-10-15 09:17:56 +110046}
47
Dave Chinnerc963c612013-10-15 09:17:56 +110048/*
49 * Return whether there are any free extents in the size range given
50 * by low and high, for the bitmap block bbno.
51 */
52STATIC int /* error */
53xfs_rtany_summary(
54 xfs_mount_t *mp, /* file system mount structure */
55 xfs_trans_t *tp, /* transaction pointer */
56 int low, /* low log2 extent size */
57 int high, /* high log2 extent size */
58 xfs_rtblock_t bbno, /* bitmap block number */
59 xfs_buf_t **rbpp, /* in/out: summary block buffer */
60 xfs_fsblock_t *rsb, /* in/out: summary block number */
61 int *stat) /* out: any good extents here? */
62{
63 int error; /* error value */
64 int log; /* loop counter, log2 of ext. size */
65 xfs_suminfo_t sum; /* summary data */
66
67 /*
68 * Loop over logs of extent sizes. Order is irrelevant.
69 */
70 for (log = low; log <= high; log++) {
71 /*
72 * Get one summary datum.
73 */
74 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
75 if (error) {
76 return error;
77 }
78 /*
79 * If there are any, return success.
80 */
81 if (sum) {
82 *stat = 1;
83 return 0;
84 }
85 }
86 /*
87 * Found nothing, return failure.
88 */
89 *stat = 0;
90 return 0;
91}
92
93
94/*
95 * Copy and transform the summary file, given the old and new
96 * parameters in the mount structures.
97 */
98STATIC int /* error */
99xfs_rtcopy_summary(
100 xfs_mount_t *omp, /* old file system mount point */
101 xfs_mount_t *nmp, /* new file system mount point */
102 xfs_trans_t *tp) /* transaction pointer */
103{
104 xfs_rtblock_t bbno; /* bitmap block number */
105 xfs_buf_t *bp; /* summary buffer */
106 int error; /* error return value */
107 int log; /* summary level number (log length) */
108 xfs_suminfo_t sum; /* summary data */
109 xfs_fsblock_t sumbno; /* summary block number */
110
111 bp = NULL;
112 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
113 for (bbno = omp->m_sb.sb_rbmblocks - 1;
114 (xfs_srtblock_t)bbno >= 0;
115 bbno--) {
116 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
117 &sumbno, &sum);
118 if (error)
119 return error;
120 if (sum == 0)
121 continue;
122 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
123 &bp, &sumbno);
124 if (error)
125 return error;
126 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
127 &bp, &sumbno);
128 if (error)
129 return error;
130 ASSERT(sum > 0);
131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 return 0;
Dave Chinnerc963c612013-10-15 09:17:56 +1100134}
135/*
136 * Mark an extent specified by start and len allocated.
137 * Updates all the summary information as well as the bitmap.
138 */
139STATIC int /* error */
140xfs_rtallocate_range(
141 xfs_mount_t *mp, /* file system mount point */
142 xfs_trans_t *tp, /* transaction pointer */
143 xfs_rtblock_t start, /* start block to allocate */
144 xfs_extlen_t len, /* length to allocate */
145 xfs_buf_t **rbpp, /* in/out: summary block buffer */
146 xfs_fsblock_t *rsb) /* in/out: summary block number */
147{
148 xfs_rtblock_t end; /* end of the allocated extent */
149 int error; /* error value */
150 xfs_rtblock_t postblock = 0; /* first block allocated > end */
151 xfs_rtblock_t preblock = 0; /* first block allocated < start */
Kamal Dasu5575acc2012-02-23 00:41:39 +0000152
Dave Chinnerc963c612013-10-15 09:17:56 +1100153 end = start + len - 1;
154 /*
155 * Assume we're allocating out of the middle of a free extent.
156 * We need to find the beginning and end of the extent so we can
157 * properly update the summary.
158 */
159 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
160 if (error) {
161 return error;
162 }
163 /*
164 * Find the next allocated block (end of free extent).
165 */
166 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
167 &postblock);
168 if (error) {
169 return error;
170 }
171 /*
172 * Decrement the summary information corresponding to the entire
173 * (old) free extent.
174 */
175 error = xfs_rtmodify_summary(mp, tp,
176 XFS_RTBLOCKLOG(postblock + 1 - preblock),
177 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
178 if (error) {
179 return error;
180 }
181 /*
182 * If there are blocks not being allocated at the front of the
183 * old extent, add summary data for them to be free.
184 */
185 if (preblock < start) {
186 error = xfs_rtmodify_summary(mp, tp,
187 XFS_RTBLOCKLOG(start - preblock),
188 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
189 if (error) {
190 return error;
191 }
192 }
193 /*
194 * If there are blocks not being allocated at the end of the
195 * old extent, add summary data for them to be free.
196 */
197 if (postblock > end) {
198 error = xfs_rtmodify_summary(mp, tp,
199 XFS_RTBLOCKLOG(postblock - end),
200 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
201 if (error) {
202 return error;
203 }
204 }
205 /*
206 * Modify the bitmap to mark this extent allocated.
207 */
208 error = xfs_rtmodify_range(mp, tp, start, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return error;
210}
211
212/*
213 * Attempt to allocate an extent minlen<=len<=maxlen starting from
214 * bitmap block bbno. If we don't get maxlen then use prod to trim
215 * the length, if given. Returns error; returns starting block in *rtblock.
216 * The lengths are all in rtextents.
217 */
218STATIC int /* error */
219xfs_rtallocate_extent_block(
220 xfs_mount_t *mp, /* file system mount point */
221 xfs_trans_t *tp, /* transaction pointer */
222 xfs_rtblock_t bbno, /* bitmap block number */
223 xfs_extlen_t minlen, /* minimum length to allocate */
224 xfs_extlen_t maxlen, /* maximum length to allocate */
225 xfs_extlen_t *len, /* out: actual length allocated */
226 xfs_rtblock_t *nextp, /* out: next block to try */
227 xfs_buf_t **rbpp, /* in/out: summary block buffer */
228 xfs_fsblock_t *rsb, /* in/out: summary block number */
229 xfs_extlen_t prod, /* extent product factor */
230 xfs_rtblock_t *rtblock) /* out: start block allocated */
231{
232 xfs_rtblock_t besti; /* best rtblock found so far */
233 xfs_rtblock_t bestlen; /* best length found so far */
234 xfs_rtblock_t end; /* last rtblock in chunk */
235 int error; /* error value */
236 xfs_rtblock_t i; /* current rtblock trying */
237 xfs_rtblock_t next; /* next rtblock to try */
238 int stat; /* status from internal calls */
239
240 /*
241 * Loop over all the extents starting in this bitmap block,
242 * looking for one that's long enough.
243 */
244 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
245 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
246 i <= end;
247 i++) {
248 /*
249 * See if there's a free extent of maxlen starting at i.
250 * If it's not so then next will contain the first non-free.
251 */
252 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
253 if (error) {
254 return error;
255 }
256 if (stat) {
257 /*
258 * i for maxlen is all free, allocate and return that.
259 */
260 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
261 rsb);
262 if (error) {
263 return error;
264 }
265 *len = maxlen;
266 *rtblock = i;
267 return 0;
268 }
269 /*
270 * In the case where we have a variable-sized allocation
271 * request, figure out how big this free piece is,
272 * and if it's big enough for the minimum, and the best
273 * so far, remember it.
274 */
275 if (minlen < maxlen) {
276 xfs_rtblock_t thislen; /* this extent size */
277
278 thislen = next - i;
279 if (thislen >= minlen && thislen > bestlen) {
280 besti = i;
281 bestlen = thislen;
282 }
283 }
284 /*
285 * If not done yet, find the start of the next free space.
286 */
287 if (next < end) {
288 error = xfs_rtfind_forw(mp, tp, next, end, &i);
289 if (error) {
290 return error;
291 }
292 } else
293 break;
294 }
295 /*
296 * Searched the whole thing & didn't find a maxlen free extent.
297 */
298 if (minlen < maxlen && besti != -1) {
299 xfs_extlen_t p; /* amount to trim length by */
300
301 /*
302 * If size should be a multiple of prod, make that so.
303 */
Dave Chinner0703a8e2018-06-08 09:54:22 -0700304 if (prod > 1) {
305 div_u64_rem(bestlen, prod, &p);
306 if (p)
307 bestlen -= p;
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * Allocate besti for bestlen & return that.
312 */
313 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
314 if (error) {
315 return error;
316 }
317 *len = bestlen;
318 *rtblock = besti;
319 return 0;
320 }
321 /*
322 * Allocation failed. Set *nextp to the next block to try.
323 */
324 *nextp = next;
325 *rtblock = NULLRTBLOCK;
326 return 0;
327}
328
329/*
330 * Allocate an extent of length minlen<=len<=maxlen, starting at block
331 * bno. If we don't get maxlen then use prod to trim the length, if given.
332 * Returns error; returns starting block in *rtblock.
333 * The lengths are all in rtextents.
334 */
335STATIC int /* error */
336xfs_rtallocate_extent_exact(
337 xfs_mount_t *mp, /* file system mount point */
338 xfs_trans_t *tp, /* transaction pointer */
339 xfs_rtblock_t bno, /* starting block number to allocate */
340 xfs_extlen_t minlen, /* minimum length to allocate */
341 xfs_extlen_t maxlen, /* maximum length to allocate */
342 xfs_extlen_t *len, /* out: actual length allocated */
343 xfs_buf_t **rbpp, /* in/out: summary block buffer */
344 xfs_fsblock_t *rsb, /* in/out: summary block number */
345 xfs_extlen_t prod, /* extent product factor */
346 xfs_rtblock_t *rtblock) /* out: start block allocated */
347{
348 int error; /* error value */
349 xfs_extlen_t i; /* extent length trimmed due to prod */
350 int isfree; /* extent is free */
351 xfs_rtblock_t next; /* next block to try (dummy) */
352
353 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
354 /*
355 * Check if the range in question (for maxlen) is free.
356 */
357 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
358 if (error) {
359 return error;
360 }
361 if (isfree) {
362 /*
363 * If it is, allocate it and return success.
364 */
365 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
366 if (error) {
367 return error;
368 }
369 *len = maxlen;
370 *rtblock = bno;
371 return 0;
372 }
373 /*
374 * If not, allocate what there is, if it's at least minlen.
375 */
376 maxlen = next - bno;
377 if (maxlen < minlen) {
378 /*
379 * Failed, return failure status.
380 */
381 *rtblock = NULLRTBLOCK;
382 return 0;
383 }
384 /*
385 * Trim off tail of extent, if prod is specified.
386 */
387 if (prod > 1 && (i = maxlen % prod)) {
388 maxlen -= i;
389 if (maxlen < minlen) {
390 /*
391 * Now we can't do it, return failure status.
392 */
393 *rtblock = NULLRTBLOCK;
394 return 0;
395 }
396 }
397 /*
398 * Allocate what we can and return it.
399 */
400 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
401 if (error) {
402 return error;
403 }
404 *len = maxlen;
405 *rtblock = bno;
406 return 0;
407}
408
409/*
410 * Allocate an extent of length minlen<=len<=maxlen, starting as near
411 * to bno as possible. If we don't get maxlen then use prod to trim
412 * the length, if given. The lengths are all in rtextents.
413 */
414STATIC int /* error */
415xfs_rtallocate_extent_near(
416 xfs_mount_t *mp, /* file system mount point */
417 xfs_trans_t *tp, /* transaction pointer */
418 xfs_rtblock_t bno, /* starting block number to allocate */
419 xfs_extlen_t minlen, /* minimum length to allocate */
420 xfs_extlen_t maxlen, /* maximum length to allocate */
421 xfs_extlen_t *len, /* out: actual length allocated */
422 xfs_buf_t **rbpp, /* in/out: summary block buffer */
423 xfs_fsblock_t *rsb, /* in/out: summary block number */
424 xfs_extlen_t prod, /* extent product factor */
425 xfs_rtblock_t *rtblock) /* out: start block allocated */
426{
427 int any; /* any useful extents from summary */
428 xfs_rtblock_t bbno; /* bitmap block number */
429 int error; /* error value */
430 int i; /* bitmap block offset (loop control) */
431 int j; /* secondary loop control */
432 int log2len; /* log2 of minlen */
433 xfs_rtblock_t n; /* next block to try */
434 xfs_rtblock_t r; /* result block */
435
436 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
437 /*
438 * If the block number given is off the end, silently set it to
439 * the last block.
440 */
441 if (bno >= mp->m_sb.sb_rextents)
442 bno = mp->m_sb.sb_rextents - 1;
443 /*
444 * Try the exact allocation first.
445 */
446 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
447 rbpp, rsb, prod, &r);
448 if (error) {
449 return error;
450 }
451 /*
452 * If the exact allocation worked, return that.
453 */
454 if (r != NULLRTBLOCK) {
455 *rtblock = r;
456 return 0;
457 }
458 bbno = XFS_BITTOBLOCK(mp, bno);
459 i = 0;
David Chinner79071eb2008-08-13 15:41:12 +1000460 ASSERT(minlen != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 log2len = xfs_highbit32(minlen);
462 /*
463 * Loop over all bitmap blocks (bbno + i is current block).
464 */
465 for (;;) {
466 /*
467 * Get summary information of extents of all useful levels
468 * starting in this bitmap block.
469 */
470 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
471 bbno + i, rbpp, rsb, &any);
472 if (error) {
473 return error;
474 }
475 /*
476 * If there are any useful extents starting here, try
477 * allocating one.
478 */
479 if (any) {
480 /*
481 * On the positive side of the starting location.
482 */
483 if (i >= 0) {
484 /*
485 * Try to allocate an extent starting in
486 * this block.
487 */
488 error = xfs_rtallocate_extent_block(mp, tp,
489 bbno + i, minlen, maxlen, len, &n, rbpp,
490 rsb, prod, &r);
491 if (error) {
492 return error;
493 }
494 /*
495 * If it worked, return it.
496 */
497 if (r != NULLRTBLOCK) {
498 *rtblock = r;
499 return 0;
500 }
501 }
502 /*
503 * On the negative side of the starting location.
504 */
505 else { /* i < 0 */
506 /*
507 * Loop backwards through the bitmap blocks from
508 * the starting point-1 up to where we are now.
509 * There should be an extent which ends in this
510 * bitmap block and is long enough.
511 */
512 for (j = -1; j > i; j--) {
513 /*
514 * Grab the summary information for
515 * this bitmap block.
516 */
517 error = xfs_rtany_summary(mp, tp,
518 log2len, mp->m_rsumlevels - 1,
519 bbno + j, rbpp, rsb, &any);
520 if (error) {
521 return error;
522 }
523 /*
524 * If there's no extent given in the
525 * summary that means the extent we
526 * found must carry over from an
527 * earlier block. If there is an
528 * extent given, we've already tried
529 * that allocation, don't do it again.
530 */
531 if (any)
532 continue;
533 error = xfs_rtallocate_extent_block(mp,
534 tp, bbno + j, minlen, maxlen,
535 len, &n, rbpp, rsb, prod, &r);
536 if (error) {
537 return error;
538 }
539 /*
540 * If it works, return the extent.
541 */
542 if (r != NULLRTBLOCK) {
543 *rtblock = r;
544 return 0;
545 }
546 }
547 /*
548 * There weren't intervening bitmap blocks
549 * with a long enough extent, or the
550 * allocation didn't work for some reason
551 * (i.e. it's a little * too short).
552 * Try to allocate from the summary block
553 * that we found.
554 */
555 error = xfs_rtallocate_extent_block(mp, tp,
556 bbno + i, minlen, maxlen, len, &n, rbpp,
557 rsb, prod, &r);
558 if (error) {
559 return error;
560 }
561 /*
562 * If it works, return the extent.
563 */
564 if (r != NULLRTBLOCK) {
565 *rtblock = r;
566 return 0;
567 }
568 }
569 }
570 /*
571 * Loop control. If we were on the positive side, and there's
572 * still more blocks on the negative side, go there.
573 */
574 if (i > 0 && (int)bbno - i >= 0)
575 i = -i;
576 /*
577 * If positive, and no more negative, but there are more
578 * positive, go there.
579 */
580 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
581 i++;
582 /*
583 * If negative or 0 (just started), and there are positive
584 * blocks to go, go there. The 0 case moves to block 1.
585 */
586 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
587 i = 1 - i;
588 /*
589 * If negative or 0 and there are more negative blocks,
590 * go there.
591 */
592 else if (i <= 0 && (int)bbno + i > 0)
593 i--;
594 /*
595 * Must be done. Return failure.
596 */
597 else
598 break;
599 }
600 *rtblock = NULLRTBLOCK;
601 return 0;
602}
603
604/*
605 * Allocate an extent of length minlen<=len<=maxlen, with no position
606 * specified. If we don't get maxlen then use prod to trim
607 * the length, if given. The lengths are all in rtextents.
608 */
609STATIC int /* error */
610xfs_rtallocate_extent_size(
611 xfs_mount_t *mp, /* file system mount point */
612 xfs_trans_t *tp, /* transaction pointer */
613 xfs_extlen_t minlen, /* minimum length to allocate */
614 xfs_extlen_t maxlen, /* maximum length to allocate */
615 xfs_extlen_t *len, /* out: actual length allocated */
616 xfs_buf_t **rbpp, /* in/out: summary block buffer */
617 xfs_fsblock_t *rsb, /* in/out: summary block number */
618 xfs_extlen_t prod, /* extent product factor */
619 xfs_rtblock_t *rtblock) /* out: start block allocated */
620{
621 int error; /* error value */
622 int i; /* bitmap block number */
623 int l; /* level number (loop control) */
624 xfs_rtblock_t n; /* next block to be tried */
625 xfs_rtblock_t r; /* result block number */
626 xfs_suminfo_t sum; /* summary information for extents */
627
628 ASSERT(minlen % prod == 0 && maxlen % prod == 0);
David Chinner79071eb2008-08-13 15:41:12 +1000629 ASSERT(maxlen != 0);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * Loop over all the levels starting with maxlen.
633 * At each level, look at all the bitmap blocks, to see if there
634 * are extents starting there that are long enough (>= maxlen).
635 * Note, only on the initial level can the allocation fail if
636 * the summary says there's an extent.
637 */
638 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
639 /*
640 * Loop over all the bitmap blocks.
641 */
642 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
643 /*
644 * Get the summary for this level/block.
645 */
646 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
647 &sum);
648 if (error) {
649 return error;
650 }
651 /*
652 * Nothing there, on to the next block.
653 */
654 if (!sum)
655 continue;
656 /*
657 * Try allocating the extent.
658 */
659 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
660 maxlen, len, &n, rbpp, rsb, prod, &r);
661 if (error) {
662 return error;
663 }
664 /*
665 * If it worked, return that.
666 */
667 if (r != NULLRTBLOCK) {
668 *rtblock = r;
669 return 0;
670 }
671 /*
672 * If the "next block to try" returned from the
673 * allocator is beyond the next bitmap block,
674 * skip to that bitmap block.
675 */
676 if (XFS_BITTOBLOCK(mp, n) > i + 1)
677 i = XFS_BITTOBLOCK(mp, n) - 1;
678 }
679 }
680 /*
681 * Didn't find any maxlen blocks. Try smaller ones, unless
682 * we're asking for a fixed size extent.
683 */
684 if (minlen > --maxlen) {
685 *rtblock = NULLRTBLOCK;
686 return 0;
687 }
David Chinner79071eb2008-08-13 15:41:12 +1000688 ASSERT(minlen != 0);
689 ASSERT(maxlen != 0);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Loop over sizes, from maxlen down to minlen.
693 * This time, when we do the allocations, allow smaller ones
694 * to succeed.
695 */
696 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
697 /*
698 * Loop over all the bitmap blocks, try an allocation
699 * starting in that block.
700 */
701 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
702 /*
703 * Get the summary information for this level/block.
704 */
705 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
706 &sum);
707 if (error) {
708 return error;
709 }
710 /*
711 * If nothing there, go on to next.
712 */
713 if (!sum)
714 continue;
715 /*
716 * Try the allocation. Make sure the specified
717 * minlen/maxlen are in the possible range for
718 * this summary level.
719 */
720 error = xfs_rtallocate_extent_block(mp, tp, i,
721 XFS_RTMAX(minlen, 1 << l),
722 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
723 len, &n, rbpp, rsb, prod, &r);
724 if (error) {
725 return error;
726 }
727 /*
728 * If it worked, return that extent.
729 */
730 if (r != NULLRTBLOCK) {
731 *rtblock = r;
732 return 0;
733 }
734 /*
735 * If the "next block to try" returned from the
736 * allocator is beyond the next bitmap block,
737 * skip to that bitmap block.
738 */
739 if (XFS_BITTOBLOCK(mp, n) > i + 1)
740 i = XFS_BITTOBLOCK(mp, n) - 1;
741 }
742 }
743 /*
744 * Got nothing, return failure.
745 */
746 *rtblock = NULLRTBLOCK;
747 return 0;
748}
749
750/*
Dave Chinnerc963c612013-10-15 09:17:56 +1100751 * Allocate space to the bitmap or summary file, and zero it, for growfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Brian Fosterd4a97a02015-08-19 10:01:40 +1000753STATIC int
Dave Chinnerc963c612013-10-15 09:17:56 +1100754xfs_growfs_rt_alloc(
Brian Fosterd4a97a02015-08-19 10:01:40 +1000755 struct xfs_mount *mp, /* file system mount point */
756 xfs_extlen_t oblocks, /* old count of blocks */
757 xfs_extlen_t nblocks, /* new count of blocks */
758 struct xfs_inode *ip) /* inode (bitmap/summary) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Brian Fosterd4a97a02015-08-19 10:01:40 +1000760 xfs_fileoff_t bno; /* block number in file */
761 struct xfs_buf *bp; /* temporary buffer for zeroing */
Brian Fosterd4a97a02015-08-19 10:01:40 +1000762 xfs_daddr_t d; /* disk block address */
763 int error; /* error return value */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000764 struct xfs_defer_ops dfops; /* list of freed blocks */
Brian Fosterd4a97a02015-08-19 10:01:40 +1000765 xfs_fsblock_t fsbno; /* filesystem block for bno */
766 struct xfs_bmbt_irec map; /* block map output */
767 int nmap; /* number of block maps */
768 int resblks; /* space reservation */
769 struct xfs_trans *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Dave Chinnerc963c612013-10-15 09:17:56 +1100771 /*
772 * Allocate space to the file, as necessary.
773 */
774 while (oblocks < nblocks) {
Dave Chinnerc963c612013-10-15 09:17:56 +1100775 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
776 /*
777 * Reserve space & log for one extent added to the file.
778 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000779 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
780 0, 0, &tp);
Dave Chinnerc963c612013-10-15 09:17:56 +1100781 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000782 return error;
Dave Chinnerc963c612013-10-15 09:17:56 +1100783 /*
784 * Lock the inode.
785 */
786 xfs_ilock(ip, XFS_ILOCK_EXCL);
787 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
788
Brian Foster5fdd9792018-07-11 22:26:33 -0700789 xfs_defer_init(tp, &dfops);
Dave Chinnerc963c612013-10-15 09:17:56 +1100790 /*
791 * Allocate blocks to the bitmap file.
792 */
793 nmap = 1;
Dave Chinnerc963c612013-10-15 09:17:56 +1100794 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
Brian Fostera7beabe2018-07-11 22:26:25 -0700795 XFS_BMAPI_METADATA, resblks, &map,
796 &nmap);
Dave Chinnerc963c612013-10-15 09:17:56 +1100797 if (!error && nmap < 1)
Dave Chinner24513372014-06-25 14:58:08 +1000798 error = -ENOSPC;
Dave Chinnerc963c612013-10-15 09:17:56 +1100799 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +1000800 goto out_bmap_cancel;
Dave Chinnerc963c612013-10-15 09:17:56 +1100801 /*
802 * Free any blocks freed up in the transaction, then commit.
803 */
Brian Foster175d1a02018-07-11 22:26:12 -0700804 error = xfs_defer_finish(&tp, tp->t_dfops);
Dave Chinnerc963c612013-10-15 09:17:56 +1100805 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +1000806 goto out_bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +1000807 error = xfs_trans_commit(tp);
Dave Chinnerc963c612013-10-15 09:17:56 +1100808 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +1000809 return error;
Dave Chinnerc963c612013-10-15 09:17:56 +1100810 /*
811 * Now we need to clear the allocated blocks.
812 * Do this one block per transaction, to keep it simple.
813 */
Dave Chinnerc963c612013-10-15 09:17:56 +1100814 for (bno = map.br_startoff, fsbno = map.br_startblock;
815 bno < map.br_startoff + map.br_blockcount;
816 bno++, fsbno++) {
Dave Chinnerc963c612013-10-15 09:17:56 +1100817 /*
818 * Reserve log for one block zeroing.
819 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000820 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
821 0, 0, 0, &tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000823 return error;
Dave Chinnerc963c612013-10-15 09:17:56 +1100824 /*
825 * Lock the bitmap inode.
826 */
827 xfs_ilock(ip, XFS_ILOCK_EXCL);
828 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
829 /*
830 * Get a buffer for the block.
831 */
832 d = XFS_FSB_TO_DADDR(mp, fsbno);
833 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
834 mp->m_bsize, 0);
835 if (bp == NULL) {
Dave Chinner24513372014-06-25 14:58:08 +1000836 error = -EIO;
Brian Fosterd4a97a02015-08-19 10:01:40 +1000837 goto out_trans_cancel;
Dave Chinnerc963c612013-10-15 09:17:56 +1100838 }
839 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
840 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
841 /*
842 * Commit the transaction.
843 */
Christoph Hellwig70393312015-06-04 13:48:08 +1000844 error = xfs_trans_commit(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +1000846 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Dave Chinnerc963c612013-10-15 09:17:56 +1100848 /*
849 * Go on to the next extent, if any.
850 */
851 oblocks = map.br_startoff + map.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Brian Fosterd4a97a02015-08-19 10:01:40 +1000853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Brian Fosterd4a97a02015-08-19 10:01:40 +1000856out_bmap_cancel:
Brian Foster175d1a02018-07-11 22:26:12 -0700857 xfs_defer_cancel(tp->t_dfops);
Brian Fosterd4a97a02015-08-19 10:01:40 +1000858out_trans_cancel:
859 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return error;
861}
862
863/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * Visible (exported) functions.
865 */
866
867/*
868 * Grow the realtime area of the filesystem.
869 */
870int
871xfs_growfs_rt(
872 xfs_mount_t *mp, /* mount point for filesystem */
873 xfs_growfs_rt_t *in) /* growfs rt input struct */
874{
875 xfs_rtblock_t bmbno; /* bitmap block number */
876 xfs_buf_t *bp; /* temporary buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 xfs_mount_t *nmp; /* new (fake) mount structure */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000879 xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000881 xfs_rtblock_t nrextents; /* new number of realtime extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 uint8_t nrextslog; /* new log2 of sb_rextents */
883 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
884 uint nrsumlevels; /* new rt summary levels */
885 uint nrsumsize; /* new size of rt summary, bytes */
886 xfs_sb_t *nsbp; /* new superblock */
887 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
888 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
889 xfs_sb_t *sbp; /* old superblock */
890 xfs_fsblock_t sumbno; /* summary block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 sbp = &mp->m_sb;
893 /*
894 * Initial error checking.
895 */
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600896 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000897 return -EPERM;
Eric Sesterhenn73024cf2006-06-28 08:42:26 +1000898 if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
900 (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
Dave Chinner24513372014-06-25 14:58:08 +1000901 return -EINVAL;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000902 if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
903 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * Read in the last block of the device, make sure it exists.
906 */
Dave Chinnerba3726742014-10-02 09:05:32 +1000907 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +1000908 XFS_FSB_TO_BB(mp, nrblocks - 1),
Dave Chinnerba3726742014-10-02 09:05:32 +1000909 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
910 if (error)
Dave Chinnereab4e632012-11-12 22:54:02 +1100911 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 xfs_buf_relse(bp);
Dave Chinner1922c942010-09-22 10:47:20 +1000913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Calculate new parameters. These are the final values to be reached.
916 */
917 nrextents = nrblocks;
918 do_div(nrextents, in->extsize);
Nathan Scott68c32712006-09-28 11:03:53 +1000919 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 nrextslog = xfs_highbit32(nrextents);
921 nrsumlevels = nrextslog + 1;
922 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
923 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
924 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
925 /*
926 * New summary size can't be more than half the size of
927 * the log. This prevents us from getting a log overflow,
928 * since we'll log basically the whole summary file at once.
929 */
930 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
Dave Chinner24513372014-06-25 14:58:08 +1000931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Get the old block counts for bitmap and summary inodes.
934 * These can't change since other growfs callers are locked out.
935 */
936 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
937 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
938 /*
939 * Allocate space to the bitmap and summary files, as necessary.
940 */
Christoph Hellwig1050c712011-02-13 13:25:31 +0000941 error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
942 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return error;
Christoph Hellwig1050c712011-02-13 13:25:31 +0000944 error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
945 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return error;
Nathan Scottd432c802006-09-28 11:03:44 +1000947 /*
948 * Allocate a new (fake) mount/sb.
949 */
950 nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /*
952 * Loop over the bitmap blocks.
953 * We will do everything one bitmap block at a time.
954 * Skip the current block if it is exactly full.
955 * This also deals with the case where there were no rtextents before.
956 */
957 for (bmbno = sbp->sb_rbmblocks -
958 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
959 bmbno < nrbmblocks;
960 bmbno++) {
Dave Chinner0924b582008-11-28 14:23:34 +1100961 xfs_trans_t *tp;
Dave Chinner0924b582008-11-28 14:23:34 +1100962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 *nmp = *mp;
964 nsbp = &nmp->m_sb;
965 /*
966 * Calculate new sb and mount fields for this round.
967 */
968 nsbp->sb_rextsize = in->extsize;
969 nsbp->sb_rbmblocks = bmbno + 1;
970 nsbp->sb_rblocks =
971 XFS_RTMIN(nrblocks,
972 nsbp->sb_rbmblocks * NBBY *
973 nsbp->sb_blocksize * nsbp->sb_rextsize);
974 nsbp->sb_rextents = nsbp->sb_rblocks;
975 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
David Chinner79071eb2008-08-13 15:41:12 +1000976 ASSERT(nsbp->sb_rextents != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
978 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
979 nrsumsize =
980 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
981 nsbp->sb_rbmblocks;
982 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
983 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
984 /*
985 * Start a transaction, get the log reservation.
986 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000987 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
988 &tp);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000989 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +1000990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /*
992 * Lock out other callers by grabbing the bitmap inode lock.
993 */
Christoph Hellwig1050c712011-02-13 13:25:31 +0000994 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000995 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /*
997 * Update the bitmap inode's size.
998 */
999 mp->m_rbmip->i_d.di_size =
1000 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1001 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /*
1003 * Get the summary inode into the transaction.
1004 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00001005 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00001006 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * Update the summary inode's size.
1009 */
1010 mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1011 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1012 /*
1013 * Copy summary data from old to new sizes.
1014 * Do this when the real size (not block-aligned) changes.
1015 */
1016 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1017 mp->m_rsumlevels != nmp->m_rsumlevels) {
1018 error = xfs_rtcopy_summary(mp, nmp, tp);
1019 if (error)
Dave Chinner0924b582008-11-28 14:23:34 +11001020 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 /*
1023 * Update superblock fields.
1024 */
1025 if (nsbp->sb_rextsize != sbp->sb_rextsize)
1026 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1027 nsbp->sb_rextsize - sbp->sb_rextsize);
1028 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1029 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1030 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1031 if (nsbp->sb_rblocks != sbp->sb_rblocks)
1032 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1033 nsbp->sb_rblocks - sbp->sb_rblocks);
1034 if (nsbp->sb_rextents != sbp->sb_rextents)
1035 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1036 nsbp->sb_rextents - sbp->sb_rextents);
1037 if (nsbp->sb_rextslog != sbp->sb_rextslog)
1038 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1039 nsbp->sb_rextslog - sbp->sb_rextslog);
1040 /*
1041 * Free new extent.
1042 */
1043 bp = NULL;
1044 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1045 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
Dave Chinner0924b582008-11-28 14:23:34 +11001046 if (error) {
1047error_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001048 xfs_trans_cancel(tp);
Nathan Scottd432c802006-09-28 11:03:44 +10001049 break;
Dave Chinner0924b582008-11-28 14:23:34 +11001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
1052 * Mark more blocks free in the superblock.
1053 */
1054 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1055 nsbp->sb_rextents - sbp->sb_rextents);
1056 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * Update mp values into the real mp structure.
1058 */
1059 mp->m_rsumlevels = nrsumlevels;
1060 mp->m_rsumsize = nrsumsize;
David Chinnere5720ee2008-04-10 12:21:18 +10001061
Christoph Hellwig70393312015-06-04 13:48:08 +10001062 error = xfs_trans_commit(tp);
Dave Chinner0924b582008-11-28 14:23:34 +11001063 if (error)
David Chinnere5720ee2008-04-10 12:21:18 +10001064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Nathan Scottd432c802006-09-28 11:03:44 +10001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
Nathan Scottd432c802006-09-28 11:03:44 +10001068 * Free the fake mp structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001070 kmem_free(nmp);
Nathan Scottd432c802006-09-28 11:03:44 +10001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return error;
1073}
1074
1075/*
1076 * Allocate an extent in the realtime subvolume, with the usual allocation
1077 * parameters. The length units are all in realtime extents, as is the
1078 * result block number.
1079 */
1080int /* error */
1081xfs_rtallocate_extent(
1082 xfs_trans_t *tp, /* transaction pointer */
1083 xfs_rtblock_t bno, /* starting block number to allocate */
1084 xfs_extlen_t minlen, /* minimum length to allocate */
1085 xfs_extlen_t maxlen, /* maximum length to allocate */
1086 xfs_extlen_t *len, /* out: actual length allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int wasdel, /* was a delayed allocation extent */
1088 xfs_extlen_t prod, /* extent product factor */
1089 xfs_rtblock_t *rtblock) /* out: start block allocated */
1090{
Christoph Hellwig04e99452011-01-25 09:06:19 +00001091 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int error; /* error value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 xfs_rtblock_t r; /* result allocated block */
1094 xfs_fsblock_t sb; /* summary file block number */
1095 xfs_buf_t *sumbp; /* summary file block buffer */
1096
Christoph Hellwig04e99452011-01-25 09:06:19 +00001097 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ASSERT(minlen > 0 && minlen <= maxlen);
Christoph Hellwig04e99452011-01-25 09:06:19 +00001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * If prod is set then figure out what to do to minlen and maxlen.
1102 */
1103 if (prod > 1) {
1104 xfs_extlen_t i;
1105
1106 if ((i = maxlen % prod))
1107 maxlen -= i;
1108 if ((i = minlen % prod))
1109 minlen += prod - i;
1110 if (maxlen < minlen) {
1111 *rtblock = NULLRTBLOCK;
1112 return 0;
1113 }
1114 }
Christoph Hellwig04e99452011-01-25 09:06:19 +00001115
Christoph Hellwig089ec2f2017-02-17 08:21:06 -08001116retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 sumbp = NULL;
Christoph Hellwig089ec2f2017-02-17 08:21:06 -08001118 if (bno == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1120 &sumbp, &sb, prod, &r);
Christoph Hellwig089ec2f2017-02-17 08:21:06 -08001121 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1123 len, &sumbp, &sb, prod, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Christoph Hellwig089ec2f2017-02-17 08:21:06 -08001125
Christoph Hellwig04e99452011-01-25 09:06:19 +00001126 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return error;
Christoph Hellwig04e99452011-01-25 09:06:19 +00001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * If it worked, update the superblock.
1131 */
1132 if (r != NULLRTBLOCK) {
1133 long slen = (long)*len;
1134
1135 ASSERT(*len >= minlen && *len <= maxlen);
1136 if (wasdel)
1137 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1138 else
1139 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
Christoph Hellwig089ec2f2017-02-17 08:21:06 -08001140 } else if (prod > 1) {
1141 prod = 1;
1142 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Christoph Hellwig089ec2f2017-02-17 08:21:06 -08001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 *rtblock = r;
1146 return 0;
1147}
1148
1149/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * Initialize realtime fields in the mount structure.
1151 */
1152int /* error */
1153xfs_rtmount_init(
Dave Chinnerba3726742014-10-02 09:05:32 +10001154 struct xfs_mount *mp) /* file system mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Dave Chinnerba3726742014-10-02 09:05:32 +10001156 struct xfs_buf *bp; /* buffer for last block of subvolume */
1157 struct xfs_sb *sbp; /* filesystem superblock copy in mount */
1158 xfs_daddr_t d; /* address of last block of subvolume */
1159 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 sbp = &mp->m_sb;
1162 if (sbp->sb_rblocks == 0)
1163 return 0;
1164 if (mp->m_rtdev_targp == NULL) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001165 xfs_warn(mp,
1166 "Filesystem has a realtime volume, use rtdev=device option");
Dave Chinner24513372014-06-25 14:58:08 +10001167 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169 mp->m_rsumlevels = sbp->sb_rextslog + 1;
1170 mp->m_rsumsize =
1171 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1172 sbp->sb_rbmblocks;
1173 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1174 mp->m_rbmip = mp->m_rsumip = NULL;
1175 /*
1176 * Check that the realtime section is an ok size.
1177 */
1178 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1179 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001180 xfs_warn(mp, "realtime mount -- %llu != %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 (unsigned long long) XFS_BB_TO_FSB(mp, d),
1182 (unsigned long long) mp->m_sb.sb_rblocks);
Dave Chinner24513372014-06-25 14:58:08 +10001183 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Dave Chinnerba3726742014-10-02 09:05:32 +10001185 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +10001186 d - XFS_FSB_TO_BB(mp, 1),
Dave Chinnerba3726742014-10-02 09:05:32 +10001187 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1188 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001189 xfs_warn(mp, "realtime device size check failed");
Dave Chinnerba3726742014-10-02 09:05:32 +10001190 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192 xfs_buf_relse(bp);
1193 return 0;
1194}
1195
1196/*
1197 * Get the bitmap and summary inodes into the mount structure
1198 * at mount time.
1199 */
1200int /* error */
1201xfs_rtmount_inodes(
1202 xfs_mount_t *mp) /* file system mount structure */
1203{
1204 int error; /* error return value */
1205 xfs_sb_t *sbp;
1206
1207 sbp = &mp->m_sb;
1208 if (sbp->sb_rbmino == NULLFSINO)
1209 return 0;
Dave Chinner7b6259e2010-06-24 11:35:17 +10001210 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (error)
1212 return error;
1213 ASSERT(mp->m_rbmip != NULL);
1214 ASSERT(sbp->sb_rsumino != NULLFSINO);
Dave Chinner7b6259e2010-06-24 11:35:17 +10001215 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (error) {
Christoph Hellwig43355092008-03-27 18:01:08 +11001217 IRELE(mp->m_rbmip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return error;
1219 }
1220 ASSERT(mp->m_rsumip != NULL);
1221 return 0;
1222}
1223
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001224void
1225xfs_rtunmount_inodes(
1226 struct xfs_mount *mp)
1227{
1228 if (mp->m_rbmip)
1229 IRELE(mp->m_rbmip);
1230 if (mp->m_rsumip)
1231 IRELE(mp->m_rsumip);
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/*
1235 * Pick an extent for allocation at the start of a new realtime file.
1236 * Use the sequence number stored in the atime field of the bitmap inode.
1237 * Translate this to a fraction of the rtextents, and return the product
1238 * of rtextents and the fraction.
1239 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1240 */
1241int /* error */
1242xfs_rtpick_extent(
1243 xfs_mount_t *mp, /* file system mount point */
1244 xfs_trans_t *tp, /* transaction pointer */
1245 xfs_extlen_t len, /* allocation length (rtextents) */
1246 xfs_rtblock_t *pick) /* result rt extent */
1247{
1248 xfs_rtblock_t b; /* result block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int log2; /* log of sequence number */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001250 uint64_t resid; /* residual after log removed */
1251 uint64_t seq; /* sequence number of file creation */
1252 uint64_t *seqp; /* pointer to seqno in inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Christoph Hellwig04e99452011-01-25 09:06:19 +00001254 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1255
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001256 seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
Christoph Hellwig04e99452011-01-25 09:06:19 +00001257 if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
1258 mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 *seqp = 0;
1260 }
1261 seq = *seqp;
1262 if ((log2 = xfs_highbit64(seq)) == -1)
1263 b = 0;
1264 else {
1265 resid = seq - (1ULL << log2);
1266 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1267 (log2 + 1);
1268 if (b >= mp->m_sb.sb_rextents)
Dave Chinner0703a8e2018-06-08 09:54:22 -07001269 div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (b + len > mp->m_sb.sb_rextents)
1271 b = mp->m_sb.sb_rextents - len;
1272 }
1273 *seqp = seq + 1;
Christoph Hellwig04e99452011-01-25 09:06:19 +00001274 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 *pick = b;
1276 return 0;
1277}