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