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