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