1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_bmap.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_trans.h"
18 #include "xfs_trans_space.h"
19 #include "xfs_icache.h"
20 #include "xfs_rtalloc.h"
21 #include "xfs_sb.h"
22 #include "xfs_rtbitmap.h"
23
24 /*
25 * Read and return the summary information for a given extent size,
26 * bitmap block combination.
27 * Keeps track of a current summary block, so we don't keep reading
28 * it from the buffer cache.
29 */
30 static int
xfs_rtget_summary(xfs_mount_t * mp,xfs_trans_t * tp,int log,xfs_rtblock_t bbno,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb,xfs_suminfo_t * sum)31 xfs_rtget_summary(
32 xfs_mount_t *mp, /* file system mount structure */
33 xfs_trans_t *tp, /* transaction pointer */
34 int log, /* log2 of extent size */
35 xfs_rtblock_t bbno, /* bitmap block number */
36 struct xfs_buf **rbpp, /* in/out: summary block buffer */
37 xfs_fsblock_t *rsb, /* in/out: summary block number */
38 xfs_suminfo_t *sum) /* out: summary info for this block */
39 {
40 return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
41 }
42
43 /*
44 * Return whether there are any free extents in the size range given
45 * by low and high, for the bitmap block bbno.
46 */
47 STATIC int /* error */
xfs_rtany_summary(xfs_mount_t * mp,xfs_trans_t * tp,int low,int high,xfs_rtblock_t bbno,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb,int * stat)48 xfs_rtany_summary(
49 xfs_mount_t *mp, /* file system mount structure */
50 xfs_trans_t *tp, /* transaction pointer */
51 int low, /* low log2 extent size */
52 int high, /* high log2 extent size */
53 xfs_rtblock_t bbno, /* bitmap block number */
54 struct xfs_buf **rbpp, /* in/out: summary block buffer */
55 xfs_fsblock_t *rsb, /* in/out: summary block number */
56 int *stat) /* out: any good extents here? */
57 {
58 int error; /* error value */
59 int log; /* loop counter, log2 of ext. size */
60 xfs_suminfo_t sum; /* summary data */
61
62 /* There are no extents at levels < m_rsum_cache[bbno]. */
63 if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
64 low = mp->m_rsum_cache[bbno];
65
66 /*
67 * Loop over logs of extent sizes.
68 */
69 for (log = low; log <= high; log++) {
70 /*
71 * Get one summary datum.
72 */
73 error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
74 if (error) {
75 return error;
76 }
77 /*
78 * If there are any, return success.
79 */
80 if (sum) {
81 *stat = 1;
82 goto out;
83 }
84 }
85 /*
86 * Found nothing, return failure.
87 */
88 *stat = 0;
89 out:
90 /* There were no extents at levels < log. */
91 if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
92 mp->m_rsum_cache[bbno] = log;
93 return 0;
94 }
95
96
97 /*
98 * Copy and transform the summary file, given the old and new
99 * parameters in the mount structures.
100 */
101 STATIC int /* error */
xfs_rtcopy_summary(xfs_mount_t * omp,xfs_mount_t * nmp,xfs_trans_t * tp)102 xfs_rtcopy_summary(
103 xfs_mount_t *omp, /* old file system mount point */
104 xfs_mount_t *nmp, /* new file system mount point */
105 xfs_trans_t *tp) /* transaction pointer */
106 {
107 xfs_rtblock_t bbno; /* bitmap block number */
108 struct xfs_buf *bp; /* summary buffer */
109 int error; /* error return value */
110 int log; /* summary level number (log length) */
111 xfs_suminfo_t sum; /* summary data */
112 xfs_fsblock_t sumbno; /* summary block number */
113
114 bp = NULL;
115 for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
116 for (bbno = omp->m_sb.sb_rbmblocks - 1;
117 (xfs_srtblock_t)bbno >= 0;
118 bbno--) {
119 error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
120 &sumbno, &sum);
121 if (error)
122 return error;
123 if (sum == 0)
124 continue;
125 error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
126 &bp, &sumbno);
127 if (error)
128 return error;
129 error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
130 &bp, &sumbno);
131 if (error)
132 return error;
133 ASSERT(sum > 0);
134 }
135 }
136 return 0;
137 }
138 /*
139 * Mark an extent specified by start and len allocated.
140 * Updates all the summary information as well as the bitmap.
141 */
142 STATIC int /* error */
xfs_rtallocate_range(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t start,xfs_extlen_t len,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb)143 xfs_rtallocate_range(
144 xfs_mount_t *mp, /* file system mount point */
145 xfs_trans_t *tp, /* transaction pointer */
146 xfs_rtblock_t start, /* start block to allocate */
147 xfs_extlen_t len, /* length to allocate */
148 struct xfs_buf **rbpp, /* in/out: summary block buffer */
149 xfs_fsblock_t *rsb) /* in/out: summary block number */
150 {
151 xfs_rtblock_t end; /* end of the allocated extent */
152 int error; /* error value */
153 xfs_rtblock_t postblock = 0; /* first block allocated > end */
154 xfs_rtblock_t preblock = 0; /* first block allocated < start */
155
156 end = start + len - 1;
157 /*
158 * Assume we're allocating out of the middle of a free extent.
159 * We need to find the beginning and end of the extent so we can
160 * properly update the summary.
161 */
162 error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
163 if (error) {
164 return error;
165 }
166 /*
167 * Find the next allocated block (end of free extent).
168 */
169 error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
170 &postblock);
171 if (error) {
172 return error;
173 }
174 /*
175 * Decrement the summary information corresponding to the entire
176 * (old) free extent.
177 */
178 error = xfs_rtmodify_summary(mp, tp,
179 XFS_RTBLOCKLOG(postblock + 1 - preblock),
180 XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
181 if (error) {
182 return error;
183 }
184 /*
185 * If there are blocks not being allocated at the front of the
186 * old extent, add summary data for them to be free.
187 */
188 if (preblock < start) {
189 error = xfs_rtmodify_summary(mp, tp,
190 XFS_RTBLOCKLOG(start - preblock),
191 XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
192 if (error) {
193 return error;
194 }
195 }
196 /*
197 * If there are blocks not being allocated at the end of the
198 * old extent, add summary data for them to be free.
199 */
200 if (postblock > end) {
201 error = xfs_rtmodify_summary(mp, tp,
202 XFS_RTBLOCKLOG(postblock - end),
203 XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
204 if (error) {
205 return error;
206 }
207 }
208 /*
209 * Modify the bitmap to mark this extent allocated.
210 */
211 error = xfs_rtmodify_range(mp, tp, start, len, 0);
212 return error;
213 }
214
215 /*
216 * Make sure we don't run off the end of the rt volume. Be careful that
217 * adjusting maxlen downwards doesn't cause us to fail the alignment checks.
218 */
219 static inline xfs_extlen_t
xfs_rtallocate_clamp_len(struct xfs_mount * mp,xfs_rtblock_t startrtx,xfs_extlen_t rtxlen,xfs_extlen_t prod)220 xfs_rtallocate_clamp_len(
221 struct xfs_mount *mp,
222 xfs_rtblock_t startrtx,
223 xfs_extlen_t rtxlen,
224 xfs_extlen_t prod)
225 {
226 xfs_extlen_t ret;
227
228 ret = min(mp->m_sb.sb_rextents, startrtx + rtxlen) - startrtx;
229 return rounddown(ret, prod);
230 }
231
232 /*
233 * Attempt to allocate an extent minlen<=len<=maxlen starting from
234 * bitmap block bbno. If we don't get maxlen then use prod to trim
235 * the length, if given. Returns error; returns starting block in *rtblock.
236 * The lengths are all in rtextents.
237 */
238 STATIC int /* error */
xfs_rtallocate_extent_block(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t bbno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,xfs_rtblock_t * nextp,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)239 xfs_rtallocate_extent_block(
240 xfs_mount_t *mp, /* file system mount point */
241 xfs_trans_t *tp, /* transaction pointer */
242 xfs_rtblock_t bbno, /* bitmap block number */
243 xfs_extlen_t minlen, /* minimum length to allocate */
244 xfs_extlen_t maxlen, /* maximum length to allocate */
245 xfs_extlen_t *len, /* out: actual length allocated */
246 xfs_rtblock_t *nextp, /* out: next block to try */
247 struct xfs_buf **rbpp, /* in/out: summary block buffer */
248 xfs_fsblock_t *rsb, /* in/out: summary block number */
249 xfs_extlen_t prod, /* extent product factor */
250 xfs_rtblock_t *rtblock) /* out: start block allocated */
251 {
252 xfs_rtblock_t besti; /* best rtblock found so far */
253 xfs_rtblock_t bestlen; /* best length found so far */
254 xfs_rtblock_t end; /* last rtblock in chunk */
255 int error; /* error value */
256 xfs_rtblock_t i; /* current rtblock trying */
257 xfs_rtblock_t next; /* next rtblock to try */
258 int stat; /* status from internal calls */
259
260 /*
261 * Loop over all the extents starting in this bitmap block,
262 * looking for one that's long enough.
263 */
264 for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
265 end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
266 i <= end;
267 i++) {
268 /* Make sure we don't scan off the end of the rt volume. */
269 maxlen = xfs_rtallocate_clamp_len(mp, i, maxlen, prod);
270
271 /*
272 * See if there's a free extent of maxlen starting at i.
273 * If it's not so then next will contain the first non-free.
274 */
275 error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
276 if (error) {
277 return error;
278 }
279 if (stat) {
280 /*
281 * i for maxlen is all free, allocate and return that.
282 */
283 error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
284 rsb);
285 if (error) {
286 return error;
287 }
288 *len = maxlen;
289 *rtblock = i;
290 return 0;
291 }
292 /*
293 * In the case where we have a variable-sized allocation
294 * request, figure out how big this free piece is,
295 * and if it's big enough for the minimum, and the best
296 * so far, remember it.
297 */
298 if (minlen < maxlen) {
299 xfs_rtblock_t thislen; /* this extent size */
300
301 thislen = next - i;
302 if (thislen >= minlen && thislen > bestlen) {
303 besti = i;
304 bestlen = thislen;
305 }
306 }
307 /*
308 * If not done yet, find the start of the next free space.
309 */
310 if (next < end) {
311 error = xfs_rtfind_forw(mp, tp, next, end, &i);
312 if (error) {
313 return error;
314 }
315 } else
316 break;
317 }
318 /*
319 * Searched the whole thing & didn't find a maxlen free extent.
320 */
321 if (minlen <= maxlen && besti != -1) {
322 xfs_extlen_t p; /* amount to trim length by */
323
324 /*
325 * If size should be a multiple of prod, make that so.
326 */
327 if (prod > 1) {
328 div_u64_rem(bestlen, prod, &p);
329 if (p)
330 bestlen -= p;
331 }
332
333 /*
334 * Allocate besti for bestlen & return that.
335 */
336 error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
337 if (error) {
338 return error;
339 }
340 *len = bestlen;
341 *rtblock = besti;
342 return 0;
343 }
344 /*
345 * Allocation failed. Set *nextp to the next block to try.
346 */
347 *nextp = next;
348 *rtblock = NULLRTBLOCK;
349 return 0;
350 }
351
352 /*
353 * Allocate an extent of length minlen<=len<=maxlen, starting at block
354 * bno. If we don't get maxlen then use prod to trim the length, if given.
355 * Returns error; returns starting block in *rtblock.
356 * The lengths are all in rtextents.
357 */
358 STATIC int /* error */
xfs_rtallocate_extent_exact(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t bno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)359 xfs_rtallocate_extent_exact(
360 xfs_mount_t *mp, /* file system mount point */
361 xfs_trans_t *tp, /* transaction pointer */
362 xfs_rtblock_t bno, /* starting block number to allocate */
363 xfs_extlen_t minlen, /* minimum length to allocate */
364 xfs_extlen_t maxlen, /* maximum length to allocate */
365 xfs_extlen_t *len, /* out: actual length allocated */
366 struct xfs_buf **rbpp, /* in/out: summary block buffer */
367 xfs_fsblock_t *rsb, /* in/out: summary block number */
368 xfs_extlen_t prod, /* extent product factor */
369 xfs_rtblock_t *rtblock) /* out: start block allocated */
370 {
371 int error; /* error value */
372 xfs_extlen_t i; /* extent length trimmed due to prod */
373 int isfree; /* extent is free */
374 xfs_rtblock_t next; /* next block to try (dummy) */
375
376 ASSERT(minlen % prod == 0);
377 ASSERT(maxlen % prod == 0);
378 /*
379 * Check if the range in question (for maxlen) is free.
380 */
381 error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
382 if (error) {
383 return error;
384 }
385 if (isfree) {
386 /*
387 * If it is, allocate it and return success.
388 */
389 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
390 if (error) {
391 return error;
392 }
393 *len = maxlen;
394 *rtblock = bno;
395 return 0;
396 }
397 /*
398 * If not, allocate what there is, if it's at least minlen.
399 */
400 maxlen = next - bno;
401 if (maxlen < minlen) {
402 /*
403 * Failed, return failure status.
404 */
405 *rtblock = NULLRTBLOCK;
406 return 0;
407 }
408 /*
409 * Trim off tail of extent, if prod is specified.
410 */
411 if (prod > 1 && (i = maxlen % prod)) {
412 maxlen -= i;
413 if (maxlen < minlen) {
414 /*
415 * Now we can't do it, return failure status.
416 */
417 *rtblock = NULLRTBLOCK;
418 return 0;
419 }
420 }
421 /*
422 * Allocate what we can and return it.
423 */
424 error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
425 if (error) {
426 return error;
427 }
428 *len = maxlen;
429 *rtblock = bno;
430 return 0;
431 }
432
433 /*
434 * Allocate an extent of length minlen<=len<=maxlen, starting as near
435 * to bno as possible. If we don't get maxlen then use prod to trim
436 * the length, if given. The lengths are all in rtextents.
437 */
438 STATIC int /* error */
xfs_rtallocate_extent_near(xfs_mount_t * mp,xfs_trans_t * tp,xfs_rtblock_t bno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)439 xfs_rtallocate_extent_near(
440 xfs_mount_t *mp, /* file system mount point */
441 xfs_trans_t *tp, /* transaction pointer */
442 xfs_rtblock_t bno, /* starting block number to allocate */
443 xfs_extlen_t minlen, /* minimum length to allocate */
444 xfs_extlen_t maxlen, /* maximum length to allocate */
445 xfs_extlen_t *len, /* out: actual length allocated */
446 struct xfs_buf **rbpp, /* in/out: summary block buffer */
447 xfs_fsblock_t *rsb, /* in/out: summary block number */
448 xfs_extlen_t prod, /* extent product factor */
449 xfs_rtblock_t *rtblock) /* out: start block allocated */
450 {
451 int any; /* any useful extents from summary */
452 xfs_rtblock_t bbno; /* bitmap block number */
453 int error; /* error value */
454 int i; /* bitmap block offset (loop control) */
455 int j; /* secondary loop control */
456 int log2len; /* log2 of minlen */
457 xfs_rtblock_t n; /* next block to try */
458 xfs_rtblock_t r; /* result block */
459
460 ASSERT(minlen % prod == 0);
461 ASSERT(maxlen % prod == 0);
462
463 /*
464 * If the block number given is off the end, silently set it to
465 * the last block.
466 */
467 if (bno >= mp->m_sb.sb_rextents)
468 bno = mp->m_sb.sb_rextents - 1;
469
470 /* Make sure we don't run off the end of the rt volume. */
471 maxlen = xfs_rtallocate_clamp_len(mp, bno, maxlen, prod);
472 if (maxlen < minlen) {
473 *rtblock = NULLRTBLOCK;
474 return 0;
475 }
476
477 /*
478 * Try the exact allocation first.
479 */
480 error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
481 rbpp, rsb, prod, &r);
482 if (error) {
483 return error;
484 }
485 /*
486 * If the exact allocation worked, return that.
487 */
488 if (r != NULLRTBLOCK) {
489 *rtblock = r;
490 return 0;
491 }
492 bbno = XFS_BITTOBLOCK(mp, bno);
493 i = 0;
494 ASSERT(minlen != 0);
495 log2len = xfs_highbit32(minlen);
496 /*
497 * Loop over all bitmap blocks (bbno + i is current block).
498 */
499 for (;;) {
500 /*
501 * Get summary information of extents of all useful levels
502 * starting in this bitmap block.
503 */
504 error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
505 bbno + i, rbpp, rsb, &any);
506 if (error) {
507 return error;
508 }
509 /*
510 * If there are any useful extents starting here, try
511 * allocating one.
512 */
513 if (any) {
514 /*
515 * On the positive side of the starting location.
516 */
517 if (i >= 0) {
518 /*
519 * Try to allocate an extent starting in
520 * this block.
521 */
522 error = xfs_rtallocate_extent_block(mp, tp,
523 bbno + i, minlen, maxlen, len, &n, rbpp,
524 rsb, prod, &r);
525 if (error) {
526 return error;
527 }
528 /*
529 * If it worked, return it.
530 */
531 if (r != NULLRTBLOCK) {
532 *rtblock = r;
533 return 0;
534 }
535 }
536 /*
537 * On the negative side of the starting location.
538 */
539 else { /* i < 0 */
540 /*
541 * Loop backwards through the bitmap blocks from
542 * the starting point-1 up to where we are now.
543 * There should be an extent which ends in this
544 * bitmap block and is long enough.
545 */
546 for (j = -1; j > i; j--) {
547 /*
548 * Grab the summary information for
549 * this bitmap block.
550 */
551 error = xfs_rtany_summary(mp, tp,
552 log2len, mp->m_rsumlevels - 1,
553 bbno + j, rbpp, rsb, &any);
554 if (error) {
555 return error;
556 }
557 /*
558 * If there's no extent given in the
559 * summary that means the extent we
560 * found must carry over from an
561 * earlier block. If there is an
562 * extent given, we've already tried
563 * that allocation, don't do it again.
564 */
565 if (any)
566 continue;
567 error = xfs_rtallocate_extent_block(mp,
568 tp, bbno + j, minlen, maxlen,
569 len, &n, rbpp, rsb, prod, &r);
570 if (error) {
571 return error;
572 }
573 /*
574 * If it works, return the extent.
575 */
576 if (r != NULLRTBLOCK) {
577 *rtblock = r;
578 return 0;
579 }
580 }
581 /*
582 * There weren't intervening bitmap blocks
583 * with a long enough extent, or the
584 * allocation didn't work for some reason
585 * (i.e. it's a little * too short).
586 * Try to allocate from the summary block
587 * that we found.
588 */
589 error = xfs_rtallocate_extent_block(mp, tp,
590 bbno + i, minlen, maxlen, len, &n, rbpp,
591 rsb, prod, &r);
592 if (error) {
593 return error;
594 }
595 /*
596 * If it works, return the extent.
597 */
598 if (r != NULLRTBLOCK) {
599 *rtblock = r;
600 return 0;
601 }
602 }
603 }
604 /*
605 * Loop control. If we were on the positive side, and there's
606 * still more blocks on the negative side, go there.
607 */
608 if (i > 0 && (int)bbno - i >= 0)
609 i = -i;
610 /*
611 * If positive, and no more negative, but there are more
612 * positive, go there.
613 */
614 else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
615 i++;
616 /*
617 * If negative or 0 (just started), and there are positive
618 * blocks to go, go there. The 0 case moves to block 1.
619 */
620 else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
621 i = 1 - i;
622 /*
623 * If negative or 0 and there are more negative blocks,
624 * go there.
625 */
626 else if (i <= 0 && (int)bbno + i > 0)
627 i--;
628 /*
629 * Must be done. Return failure.
630 */
631 else
632 break;
633 }
634 *rtblock = NULLRTBLOCK;
635 return 0;
636 }
637
638 /*
639 * Allocate an extent of length minlen<=len<=maxlen, with no position
640 * specified. If we don't get maxlen then use prod to trim
641 * the length, if given. The lengths are all in rtextents.
642 */
643 STATIC int /* error */
xfs_rtallocate_extent_size(xfs_mount_t * mp,xfs_trans_t * tp,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,struct xfs_buf ** rbpp,xfs_fsblock_t * rsb,xfs_extlen_t prod,xfs_rtblock_t * rtblock)644 xfs_rtallocate_extent_size(
645 xfs_mount_t *mp, /* file system mount point */
646 xfs_trans_t *tp, /* transaction pointer */
647 xfs_extlen_t minlen, /* minimum length to allocate */
648 xfs_extlen_t maxlen, /* maximum length to allocate */
649 xfs_extlen_t *len, /* out: actual length allocated */
650 struct xfs_buf **rbpp, /* in/out: summary block buffer */
651 xfs_fsblock_t *rsb, /* in/out: summary block number */
652 xfs_extlen_t prod, /* extent product factor */
653 xfs_rtblock_t *rtblock) /* out: start block allocated */
654 {
655 int error; /* error value */
656 int i; /* bitmap block number */
657 int l; /* level number (loop control) */
658 xfs_rtblock_t n; /* next block to be tried */
659 xfs_rtblock_t r; /* result block number */
660 xfs_suminfo_t sum; /* summary information for extents */
661
662 ASSERT(minlen % prod == 0);
663 ASSERT(maxlen % prod == 0);
664 ASSERT(maxlen != 0);
665
666 /*
667 * Loop over all the levels starting with maxlen.
668 * At each level, look at all the bitmap blocks, to see if there
669 * are extents starting there that are long enough (>= maxlen).
670 * Note, only on the initial level can the allocation fail if
671 * the summary says there's an extent.
672 */
673 for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
674 /*
675 * Loop over all the bitmap blocks.
676 */
677 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
678 /*
679 * Get the summary for this level/block.
680 */
681 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
682 &sum);
683 if (error) {
684 return error;
685 }
686 /*
687 * Nothing there, on to the next block.
688 */
689 if (!sum)
690 continue;
691 /*
692 * Try allocating the extent.
693 */
694 error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
695 maxlen, len, &n, rbpp, rsb, prod, &r);
696 if (error) {
697 return error;
698 }
699 /*
700 * If it worked, return that.
701 */
702 if (r != NULLRTBLOCK) {
703 *rtblock = r;
704 return 0;
705 }
706 /*
707 * If the "next block to try" returned from the
708 * allocator is beyond the next bitmap block,
709 * skip to that bitmap block.
710 */
711 if (XFS_BITTOBLOCK(mp, n) > i + 1)
712 i = XFS_BITTOBLOCK(mp, n) - 1;
713 }
714 }
715 /*
716 * Didn't find any maxlen blocks. Try smaller ones, unless
717 * we're asking for a fixed size extent.
718 */
719 if (minlen > --maxlen) {
720 *rtblock = NULLRTBLOCK;
721 return 0;
722 }
723 ASSERT(minlen != 0);
724 ASSERT(maxlen != 0);
725
726 /*
727 * Loop over sizes, from maxlen down to minlen.
728 * This time, when we do the allocations, allow smaller ones
729 * to succeed.
730 */
731 for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
732 /*
733 * Loop over all the bitmap blocks, try an allocation
734 * starting in that block.
735 */
736 for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
737 /*
738 * Get the summary information for this level/block.
739 */
740 error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
741 &sum);
742 if (error) {
743 return error;
744 }
745 /*
746 * If nothing there, go on to next.
747 */
748 if (!sum)
749 continue;
750 /*
751 * Try the allocation. Make sure the specified
752 * minlen/maxlen are in the possible range for
753 * this summary level.
754 */
755 error = xfs_rtallocate_extent_block(mp, tp, i,
756 XFS_RTMAX(minlen, 1 << l),
757 XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
758 len, &n, rbpp, rsb, prod, &r);
759 if (error) {
760 return error;
761 }
762 /*
763 * If it worked, return that extent.
764 */
765 if (r != NULLRTBLOCK) {
766 *rtblock = r;
767 return 0;
768 }
769 /*
770 * If the "next block to try" returned from the
771 * allocator is beyond the next bitmap block,
772 * skip to that bitmap block.
773 */
774 if (XFS_BITTOBLOCK(mp, n) > i + 1)
775 i = XFS_BITTOBLOCK(mp, n) - 1;
776 }
777 }
778 /*
779 * Got nothing, return failure.
780 */
781 *rtblock = NULLRTBLOCK;
782 return 0;
783 }
784
785 /*
786 * Allocate space to the bitmap or summary file, and zero it, for growfs.
787 */
788 STATIC int
xfs_growfs_rt_alloc(struct xfs_mount * mp,xfs_extlen_t oblocks,xfs_extlen_t nblocks,struct xfs_inode * ip)789 xfs_growfs_rt_alloc(
790 struct xfs_mount *mp, /* file system mount point */
791 xfs_extlen_t oblocks, /* old count of blocks */
792 xfs_extlen_t nblocks, /* new count of blocks */
793 struct xfs_inode *ip) /* inode (bitmap/summary) */
794 {
795 xfs_fileoff_t bno; /* block number in file */
796 struct xfs_buf *bp; /* temporary buffer for zeroing */
797 xfs_daddr_t d; /* disk block address */
798 int error; /* error return value */
799 xfs_fsblock_t fsbno; /* filesystem block for bno */
800 struct xfs_bmbt_irec map; /* block map output */
801 int nmap; /* number of block maps */
802 int resblks; /* space reservation */
803 enum xfs_blft buf_type;
804 struct xfs_trans *tp;
805
806 if (ip == mp->m_rsumip)
807 buf_type = XFS_BLFT_RTSUMMARY_BUF;
808 else
809 buf_type = XFS_BLFT_RTBITMAP_BUF;
810
811 /*
812 * Allocate space to the file, as necessary.
813 */
814 while (oblocks < nblocks) {
815 resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
816 /*
817 * Reserve space & log for one extent added to the file.
818 */
819 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
820 0, 0, &tp);
821 if (error)
822 return error;
823 /*
824 * Lock the inode.
825 */
826 xfs_ilock(ip, XFS_ILOCK_EXCL);
827 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
828
829 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
830 XFS_IEXT_ADD_NOSPLIT_CNT);
831 if (error == -EFBIG)
832 error = xfs_iext_count_upgrade(tp, ip,
833 XFS_IEXT_ADD_NOSPLIT_CNT);
834 if (error)
835 goto out_trans_cancel;
836
837 /*
838 * Allocate blocks to the bitmap file.
839 */
840 nmap = 1;
841 error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
842 XFS_BMAPI_METADATA, 0, &map, &nmap);
843 if (error)
844 goto out_trans_cancel;
845 /*
846 * Free any blocks freed up in the transaction, then commit.
847 */
848 error = xfs_trans_commit(tp);
849 if (error)
850 return error;
851 /*
852 * Now we need to clear the allocated blocks.
853 * Do this one block per transaction, to keep it simple.
854 */
855 for (bno = map.br_startoff, fsbno = map.br_startblock;
856 bno < map.br_startoff + map.br_blockcount;
857 bno++, fsbno++) {
858 /*
859 * Reserve log for one block zeroing.
860 */
861 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
862 0, 0, 0, &tp);
863 if (error)
864 return error;
865 /*
866 * Lock the bitmap inode.
867 */
868 xfs_ilock(ip, XFS_ILOCK_EXCL);
869 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
870 /*
871 * Get a buffer for the block.
872 */
873 d = XFS_FSB_TO_DADDR(mp, fsbno);
874 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
875 mp->m_bsize, 0, &bp);
876 if (error)
877 goto out_trans_cancel;
878
879 xfs_trans_buf_set_type(tp, bp, buf_type);
880 bp->b_ops = &xfs_rtbuf_ops;
881 memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
882 xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
883 /*
884 * Commit the transaction.
885 */
886 error = xfs_trans_commit(tp);
887 if (error)
888 return error;
889 }
890 /*
891 * Go on to the next extent, if any.
892 */
893 oblocks = map.br_startoff + map.br_blockcount;
894 }
895
896 return 0;
897
898 out_trans_cancel:
899 xfs_trans_cancel(tp);
900 return error;
901 }
902
903 static void
xfs_alloc_rsum_cache(xfs_mount_t * mp,xfs_extlen_t rbmblocks)904 xfs_alloc_rsum_cache(
905 xfs_mount_t *mp, /* file system mount structure */
906 xfs_extlen_t rbmblocks) /* number of rt bitmap blocks */
907 {
908 /*
909 * The rsum cache is initialized to all zeroes, which is trivially a
910 * lower bound on the minimum level with any free extents. We can
911 * continue without the cache if it couldn't be allocated.
912 */
913 mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
914 if (!mp->m_rsum_cache)
915 xfs_warn(mp, "could not allocate realtime summary cache");
916 }
917
918 /*
919 * If we changed the rt extent size (meaning there was no rt volume previously)
920 * and the root directory had EXTSZINHERIT and RTINHERIT set, it's possible
921 * that the extent size hint on the root directory is no longer congruent with
922 * the new rt extent size. Log the rootdir inode to fix this.
923 */
924 static int
xfs_growfs_rt_fixup_extsize(struct xfs_mount * mp)925 xfs_growfs_rt_fixup_extsize(
926 struct xfs_mount *mp)
927 {
928 struct xfs_inode *ip = mp->m_rootip;
929 struct xfs_trans *tp;
930 int error = 0;
931
932 xfs_ilock(ip, XFS_IOLOCK_EXCL);
933 if (!(ip->i_diflags & XFS_DIFLAG_RTINHERIT) ||
934 !(ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT))
935 goto out_iolock;
936
937 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_ichange, 0, 0, false,
938 &tp);
939 if (error)
940 goto out_iolock;
941
942 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
943 error = xfs_trans_commit(tp);
944 xfs_iunlock(ip, XFS_ILOCK_EXCL);
945
946 out_iolock:
947 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
948 return error;
949 }
950
951 /*
952 * Visible (exported) functions.
953 */
954
955 /*
956 * Grow the realtime area of the filesystem.
957 */
958 int
xfs_growfs_rt(xfs_mount_t * mp,xfs_growfs_rt_t * in)959 xfs_growfs_rt(
960 xfs_mount_t *mp, /* mount point for filesystem */
961 xfs_growfs_rt_t *in) /* growfs rt input struct */
962 {
963 xfs_rtblock_t bmbno; /* bitmap block number */
964 struct xfs_buf *bp; /* temporary buffer */
965 int error; /* error return value */
966 xfs_mount_t *nmp; /* new (fake) mount structure */
967 xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
968 xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
969 xfs_rtblock_t nrextents; /* new number of realtime extents */
970 uint8_t nrextslog; /* new log2 of sb_rextents */
971 xfs_extlen_t nrsumblocks; /* new number of summary blocks */
972 uint nrsumlevels; /* new rt summary levels */
973 uint nrsumsize; /* new size of rt summary, bytes */
974 xfs_sb_t *nsbp; /* new superblock */
975 xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
976 xfs_extlen_t rsumblocks; /* current number of rt summary blks */
977 xfs_sb_t *sbp; /* old superblock */
978 xfs_fsblock_t sumbno; /* summary block number */
979 uint8_t *rsum_cache; /* old summary cache */
980 xfs_agblock_t old_rextsize = mp->m_sb.sb_rextsize;
981
982 sbp = &mp->m_sb;
983
984 if (!capable(CAP_SYS_ADMIN))
985 return -EPERM;
986
987 /* Needs to have been mounted with an rt device. */
988 if (!XFS_IS_REALTIME_MOUNT(mp))
989 return -EINVAL;
990
991 if (!mutex_trylock(&mp->m_growlock))
992 return -EWOULDBLOCK;
993 /*
994 * Mount should fail if the rt bitmap/summary files don't load, but
995 * we'll check anyway.
996 */
997 error = -EINVAL;
998 if (!mp->m_rbmip || !mp->m_rsumip)
999 goto out_unlock;
1000
1001 /* Shrink not supported. */
1002 if (in->newblocks <= sbp->sb_rblocks)
1003 goto out_unlock;
1004
1005 /* Can only change rt extent size when adding rt volume. */
1006 if (sbp->sb_rblocks > 0 && in->extsize != sbp->sb_rextsize)
1007 goto out_unlock;
1008
1009 /* Range check the extent size. */
1010 if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
1011 XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
1012 goto out_unlock;
1013
1014 /* Unsupported realtime features. */
1015 error = -EOPNOTSUPP;
1016 if (xfs_has_rmapbt(mp) || xfs_has_reflink(mp) || xfs_has_quota(mp))
1017 goto out_unlock;
1018
1019 nrblocks = in->newblocks;
1020 error = xfs_sb_validate_fsb_count(sbp, nrblocks);
1021 if (error)
1022 goto out_unlock;
1023 /*
1024 * Read in the last block of the device, make sure it exists.
1025 */
1026 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1027 XFS_FSB_TO_BB(mp, nrblocks - 1),
1028 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1029 if (error)
1030 goto out_unlock;
1031 xfs_buf_relse(bp);
1032
1033 /*
1034 * Calculate new parameters. These are the final values to be reached.
1035 */
1036 nrextents = nrblocks;
1037 do_div(nrextents, in->extsize);
1038 if (!xfs_validate_rtextents(nrextents)) {
1039 error = -EINVAL;
1040 goto out_unlock;
1041 }
1042 nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1043 nrextslog = xfs_compute_rextslog(nrextents);
1044 nrsumlevels = nrextslog + 1;
1045 nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1046 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1047 nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1048 /*
1049 * New summary size can't be more than half the size of
1050 * the log. This prevents us from getting a log overflow,
1051 * since we'll log basically the whole summary file at once.
1052 */
1053 if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1)) {
1054 error = -EINVAL;
1055 goto out_unlock;
1056 }
1057
1058 /*
1059 * Get the old block counts for bitmap and summary inodes.
1060 * These can't change since other growfs callers are locked out.
1061 */
1062 rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
1063 rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
1064 /*
1065 * Allocate space to the bitmap and summary files, as necessary.
1066 */
1067 error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
1068 if (error)
1069 goto out_unlock;
1070 error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
1071 if (error)
1072 goto out_unlock;
1073
1074 rsum_cache = mp->m_rsum_cache;
1075 if (nrbmblocks != sbp->sb_rbmblocks)
1076 xfs_alloc_rsum_cache(mp, nrbmblocks);
1077
1078 /*
1079 * Allocate a new (fake) mount/sb.
1080 */
1081 nmp = kmem_alloc(sizeof(*nmp), 0);
1082 /*
1083 * Loop over the bitmap blocks.
1084 * We will do everything one bitmap block at a time.
1085 * Skip the current block if it is exactly full.
1086 * This also deals with the case where there were no rtextents before.
1087 */
1088 for (bmbno = sbp->sb_rbmblocks -
1089 ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1090 bmbno < nrbmblocks;
1091 bmbno++) {
1092 struct xfs_trans *tp;
1093 xfs_rfsblock_t nrblocks_step;
1094
1095 *nmp = *mp;
1096 nsbp = &nmp->m_sb;
1097 /*
1098 * Calculate new sb and mount fields for this round.
1099 */
1100 nsbp->sb_rextsize = in->extsize;
1101 nsbp->sb_rbmblocks = bmbno + 1;
1102 nrblocks_step = (bmbno + 1) * NBBY * nsbp->sb_blocksize *
1103 nsbp->sb_rextsize;
1104 nsbp->sb_rblocks = min(nrblocks, nrblocks_step);
1105 nsbp->sb_rextents = nsbp->sb_rblocks;
1106 do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1107 ASSERT(nsbp->sb_rextents != 0);
1108 nsbp->sb_rextslog = xfs_compute_rextslog(nsbp->sb_rextents);
1109 nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1110 nrsumsize =
1111 (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1112 nsbp->sb_rbmblocks;
1113 nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1114 nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1115 /* recompute growfsrt reservation from new rsumsize */
1116 xfs_trans_resv_calc(nmp, &nmp->m_resv);
1117
1118 /*
1119 * Start a transaction, get the log reservation.
1120 */
1121 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
1122 &tp);
1123 if (error)
1124 break;
1125 /*
1126 * Lock out other callers by grabbing the bitmap inode lock.
1127 */
1128 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
1129 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
1130 /*
1131 * Update the bitmap inode's size ondisk and incore. We need
1132 * to update the incore size so that inode inactivation won't
1133 * punch what it thinks are "posteof" blocks.
1134 */
1135 mp->m_rbmip->i_disk_size =
1136 nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1137 i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_disk_size);
1138 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1139 /*
1140 * Get the summary inode into the transaction.
1141 */
1142 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
1143 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1144 /*
1145 * Update the summary inode's size. We need to update the
1146 * incore size so that inode inactivation won't punch what it
1147 * thinks are "posteof" blocks.
1148 */
1149 mp->m_rsumip->i_disk_size = nmp->m_rsumsize;
1150 i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_disk_size);
1151 xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1152 /*
1153 * Copy summary data from old to new sizes.
1154 * Do this when the real size (not block-aligned) changes.
1155 */
1156 if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1157 mp->m_rsumlevels != nmp->m_rsumlevels) {
1158 error = xfs_rtcopy_summary(mp, nmp, tp);
1159 if (error)
1160 goto error_cancel;
1161 }
1162 /*
1163 * Update superblock fields.
1164 */
1165 if (nsbp->sb_rextsize != sbp->sb_rextsize)
1166 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1167 nsbp->sb_rextsize - sbp->sb_rextsize);
1168 if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1169 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1170 nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1171 if (nsbp->sb_rblocks != sbp->sb_rblocks)
1172 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1173 nsbp->sb_rblocks - sbp->sb_rblocks);
1174 if (nsbp->sb_rextents != sbp->sb_rextents)
1175 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1176 nsbp->sb_rextents - sbp->sb_rextents);
1177 if (nsbp->sb_rextslog != sbp->sb_rextslog)
1178 xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1179 nsbp->sb_rextslog - sbp->sb_rextslog);
1180 /*
1181 * Free new extent.
1182 */
1183 bp = NULL;
1184 error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1185 nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
1186 if (error) {
1187 error_cancel:
1188 xfs_trans_cancel(tp);
1189 break;
1190 }
1191 /*
1192 * Mark more blocks free in the superblock.
1193 */
1194 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1195 nsbp->sb_rextents - sbp->sb_rextents);
1196 /*
1197 * Update mp values into the real mp structure.
1198 */
1199 mp->m_rsumlevels = nrsumlevels;
1200 mp->m_rsumsize = nrsumsize;
1201 /* recompute growfsrt reservation from new rsumsize */
1202 xfs_trans_resv_calc(mp, &mp->m_resv);
1203
1204 error = xfs_trans_commit(tp);
1205 if (error)
1206 break;
1207
1208 /* Ensure the mount RT feature flag is now set. */
1209 mp->m_features |= XFS_FEAT_REALTIME;
1210 }
1211 if (error)
1212 goto out_free;
1213
1214 if (old_rextsize != in->extsize) {
1215 error = xfs_growfs_rt_fixup_extsize(mp);
1216 if (error)
1217 goto out_free;
1218 }
1219
1220 /* Update secondary superblocks now the physical grow has completed */
1221 error = xfs_update_secondary_sbs(mp);
1222
1223 out_free:
1224 /*
1225 * Free the fake mp structure.
1226 */
1227 kmem_free(nmp);
1228
1229 /*
1230 * If we had to allocate a new rsum_cache, we either need to free the
1231 * old one (if we succeeded) or free the new one and restore the old one
1232 * (if there was an error).
1233 */
1234 if (rsum_cache != mp->m_rsum_cache) {
1235 if (error) {
1236 kmem_free(mp->m_rsum_cache);
1237 mp->m_rsum_cache = rsum_cache;
1238 } else {
1239 kmem_free(rsum_cache);
1240 }
1241 }
1242
1243 out_unlock:
1244 mutex_unlock(&mp->m_growlock);
1245 return error;
1246 }
1247
1248 /*
1249 * Allocate an extent in the realtime subvolume, with the usual allocation
1250 * parameters. The length units are all in realtime extents, as is the
1251 * result block number.
1252 */
1253 int /* error */
xfs_rtallocate_extent(xfs_trans_t * tp,xfs_rtblock_t bno,xfs_extlen_t minlen,xfs_extlen_t maxlen,xfs_extlen_t * len,int wasdel,xfs_extlen_t prod,xfs_rtblock_t * rtblock)1254 xfs_rtallocate_extent(
1255 xfs_trans_t *tp, /* transaction pointer */
1256 xfs_rtblock_t bno, /* starting block number to allocate */
1257 xfs_extlen_t minlen, /* minimum length to allocate */
1258 xfs_extlen_t maxlen, /* maximum length to allocate */
1259 xfs_extlen_t *len, /* out: actual length allocated */
1260 int wasdel, /* was a delayed allocation extent */
1261 xfs_extlen_t prod, /* extent product factor */
1262 xfs_rtblock_t *rtblock) /* out: start block allocated */
1263 {
1264 xfs_mount_t *mp = tp->t_mountp;
1265 int error; /* error value */
1266 xfs_rtblock_t r; /* result allocated block */
1267 xfs_fsblock_t sb; /* summary file block number */
1268 struct xfs_buf *sumbp; /* summary file block buffer */
1269
1270 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1271 ASSERT(minlen > 0 && minlen <= maxlen);
1272
1273 /*
1274 * If prod is set then figure out what to do to minlen and maxlen.
1275 */
1276 if (prod > 1) {
1277 xfs_extlen_t i;
1278
1279 if ((i = maxlen % prod))
1280 maxlen -= i;
1281 if ((i = minlen % prod))
1282 minlen += prod - i;
1283 if (maxlen < minlen) {
1284 *rtblock = NULLRTBLOCK;
1285 return 0;
1286 }
1287 }
1288
1289 retry:
1290 sumbp = NULL;
1291 if (bno == 0) {
1292 error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1293 &sumbp, &sb, prod, &r);
1294 } else {
1295 error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1296 len, &sumbp, &sb, prod, &r);
1297 }
1298
1299 if (error)
1300 return error;
1301
1302 /*
1303 * If it worked, update the superblock.
1304 */
1305 if (r != NULLRTBLOCK) {
1306 long slen = (long)*len;
1307
1308 ASSERT(*len >= minlen && *len <= maxlen);
1309 if (wasdel)
1310 xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1311 else
1312 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1313 } else if (prod > 1) {
1314 prod = 1;
1315 goto retry;
1316 }
1317
1318 *rtblock = r;
1319 return 0;
1320 }
1321
1322 /*
1323 * Initialize realtime fields in the mount structure.
1324 */
1325 int /* error */
xfs_rtmount_init(struct xfs_mount * mp)1326 xfs_rtmount_init(
1327 struct xfs_mount *mp) /* file system mount structure */
1328 {
1329 struct xfs_buf *bp; /* buffer for last block of subvolume */
1330 struct xfs_sb *sbp; /* filesystem superblock copy in mount */
1331 xfs_daddr_t d; /* address of last block of subvolume */
1332 int error;
1333
1334 sbp = &mp->m_sb;
1335 if (sbp->sb_rblocks == 0)
1336 return 0;
1337 if (mp->m_rtdev_targp == NULL) {
1338 xfs_warn(mp,
1339 "Filesystem has a realtime volume, use rtdev=device option");
1340 return -ENODEV;
1341 }
1342 mp->m_rsumlevels = sbp->sb_rextslog + 1;
1343 mp->m_rsumsize =
1344 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1345 sbp->sb_rbmblocks;
1346 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1347 mp->m_rbmip = mp->m_rsumip = NULL;
1348 /*
1349 * Check that the realtime section is an ok size.
1350 */
1351 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1352 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1353 xfs_warn(mp, "realtime mount -- %llu != %llu",
1354 (unsigned long long) XFS_BB_TO_FSB(mp, d),
1355 (unsigned long long) mp->m_sb.sb_rblocks);
1356 return -EFBIG;
1357 }
1358 error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1359 d - XFS_FSB_TO_BB(mp, 1),
1360 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1361 if (error) {
1362 xfs_warn(mp, "realtime device size check failed");
1363 return error;
1364 }
1365 xfs_buf_relse(bp);
1366 return 0;
1367 }
1368
1369 static int
xfs_rtalloc_count_frextent(struct xfs_mount * mp,struct xfs_trans * tp,const struct xfs_rtalloc_rec * rec,void * priv)1370 xfs_rtalloc_count_frextent(
1371 struct xfs_mount *mp,
1372 struct xfs_trans *tp,
1373 const struct xfs_rtalloc_rec *rec,
1374 void *priv)
1375 {
1376 uint64_t *valp = priv;
1377
1378 *valp += rec->ar_extcount;
1379 return 0;
1380 }
1381
1382 /*
1383 * Reinitialize the number of free realtime extents from the realtime bitmap.
1384 * Callers must ensure that there is no other activity in the filesystem.
1385 */
1386 int
xfs_rtalloc_reinit_frextents(struct xfs_mount * mp)1387 xfs_rtalloc_reinit_frextents(
1388 struct xfs_mount *mp)
1389 {
1390 uint64_t val = 0;
1391 int error;
1392
1393 xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
1394 error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
1395 &val);
1396 xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
1397 if (error)
1398 return error;
1399
1400 spin_lock(&mp->m_sb_lock);
1401 mp->m_sb.sb_frextents = val;
1402 spin_unlock(&mp->m_sb_lock);
1403 percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
1404 return 0;
1405 }
1406
1407 /*
1408 * Read in the bmbt of an rt metadata inode so that we never have to load them
1409 * at runtime. This enables the use of shared ILOCKs for rtbitmap scans. Use
1410 * an empty transaction to avoid deadlocking on loops in the bmbt.
1411 */
1412 static inline int
xfs_rtmount_iread_extents(struct xfs_inode * ip,unsigned int lock_class)1413 xfs_rtmount_iread_extents(
1414 struct xfs_inode *ip,
1415 unsigned int lock_class)
1416 {
1417 struct xfs_trans *tp;
1418 int error;
1419
1420 error = xfs_trans_alloc_empty(ip->i_mount, &tp);
1421 if (error)
1422 return error;
1423
1424 xfs_ilock(ip, XFS_ILOCK_EXCL | lock_class);
1425
1426 error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1427 if (error)
1428 goto out_unlock;
1429
1430 if (xfs_inode_has_attr_fork(ip)) {
1431 error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
1432 if (error)
1433 goto out_unlock;
1434 }
1435
1436 out_unlock:
1437 xfs_iunlock(ip, XFS_ILOCK_EXCL | lock_class);
1438 xfs_trans_cancel(tp);
1439 return error;
1440 }
1441
1442 /*
1443 * Get the bitmap and summary inodes and the summary cache into the mount
1444 * structure at mount time.
1445 */
1446 int /* error */
xfs_rtmount_inodes(xfs_mount_t * mp)1447 xfs_rtmount_inodes(
1448 xfs_mount_t *mp) /* file system mount structure */
1449 {
1450 int error; /* error return value */
1451 xfs_sb_t *sbp;
1452
1453 sbp = &mp->m_sb;
1454 error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1455 if (error)
1456 return error;
1457 ASSERT(mp->m_rbmip != NULL);
1458
1459 error = xfs_rtmount_iread_extents(mp->m_rbmip, XFS_ILOCK_RTBITMAP);
1460 if (error)
1461 goto out_rele_bitmap;
1462
1463 error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1464 if (error)
1465 goto out_rele_bitmap;
1466 ASSERT(mp->m_rsumip != NULL);
1467
1468 error = xfs_rtmount_iread_extents(mp->m_rsumip, XFS_ILOCK_RTSUM);
1469 if (error)
1470 goto out_rele_summary;
1471
1472 xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
1473 return 0;
1474
1475 out_rele_summary:
1476 xfs_irele(mp->m_rsumip);
1477 out_rele_bitmap:
1478 xfs_irele(mp->m_rbmip);
1479 return error;
1480 }
1481
1482 void
xfs_rtunmount_inodes(struct xfs_mount * mp)1483 xfs_rtunmount_inodes(
1484 struct xfs_mount *mp)
1485 {
1486 kmem_free(mp->m_rsum_cache);
1487 if (mp->m_rbmip)
1488 xfs_irele(mp->m_rbmip);
1489 if (mp->m_rsumip)
1490 xfs_irele(mp->m_rsumip);
1491 }
1492
1493 /*
1494 * Pick an extent for allocation at the start of a new realtime file.
1495 * Use the sequence number stored in the atime field of the bitmap inode.
1496 * Translate this to a fraction of the rtextents, and return the product
1497 * of rtextents and the fraction.
1498 * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1499 */
1500 int /* error */
xfs_rtpick_extent(xfs_mount_t * mp,xfs_trans_t * tp,xfs_extlen_t len,xfs_rtblock_t * pick)1501 xfs_rtpick_extent(
1502 xfs_mount_t *mp, /* file system mount point */
1503 xfs_trans_t *tp, /* transaction pointer */
1504 xfs_extlen_t len, /* allocation length (rtextents) */
1505 xfs_rtblock_t *pick) /* result rt extent */
1506 {
1507 xfs_rtblock_t b; /* result block */
1508 int log2; /* log of sequence number */
1509 uint64_t resid; /* residual after log removed */
1510 uint64_t seq; /* sequence number of file creation */
1511 uint64_t *seqp; /* pointer to seqno in inode */
1512
1513 ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1514
1515 seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
1516 if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
1517 mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1518 *seqp = 0;
1519 }
1520 seq = *seqp;
1521 if ((log2 = xfs_highbit64(seq)) == -1)
1522 b = 0;
1523 else {
1524 resid = seq - (1ULL << log2);
1525 b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1526 (log2 + 1);
1527 if (b >= mp->m_sb.sb_rextents)
1528 div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
1529 if (b + len > mp->m_sb.sb_rextents)
1530 b = mp->m_sb.sb_rextents - len;
1531 }
1532 *seqp = seq + 1;
1533 xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1534 *pick = b;
1535 return 0;
1536 }
1537