1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs_platform.h" 7 #include "xfs_fs.h" 8 #include "xfs_format.h" 9 #include "xfs_log_format.h" 10 #include "xfs_shared.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_bit.h" 13 #include "xfs_mount.h" 14 #include "xfs_defer.h" 15 #include "xfs_btree.h" 16 #include "xfs_rmap.h" 17 #include "xfs_alloc_btree.h" 18 #include "xfs_alloc.h" 19 #include "xfs_extent_busy.h" 20 #include "xfs_errortag.h" 21 #include "xfs_error.h" 22 #include "xfs_trace.h" 23 #include "xfs_trans.h" 24 #include "xfs_buf_item.h" 25 #include "xfs_log.h" 26 #include "xfs_ag.h" 27 #include "xfs_ag_resv.h" 28 #include "xfs_bmap.h" 29 #include "xfs_health.h" 30 #include "xfs_extfree_item.h" 31 32 struct kmem_cache *xfs_extfree_item_cache; 33 34 struct workqueue_struct *xfs_alloc_wq; 35 36 #define XFSA_FIXUP_BNO_OK 1 37 #define XFSA_FIXUP_CNT_OK 2 38 39 /* 40 * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in 41 * the beginning of the block for a proper header with the location information 42 * and CRC. 43 */ 44 unsigned int 45 xfs_agfl_size( 46 struct xfs_mount *mp) 47 { 48 unsigned int size = mp->m_sb.sb_sectsize; 49 50 if (xfs_has_crc(mp)) 51 size -= sizeof(struct xfs_agfl); 52 53 return size / sizeof(xfs_agblock_t); 54 } 55 56 unsigned int 57 xfs_refc_block( 58 struct xfs_mount *mp) 59 { 60 if (xfs_has_rmapbt(mp)) 61 return XFS_RMAP_BLOCK(mp) + 1; 62 if (xfs_has_finobt(mp)) 63 return XFS_FIBT_BLOCK(mp) + 1; 64 return XFS_IBT_BLOCK(mp) + 1; 65 } 66 67 xfs_extlen_t 68 xfs_prealloc_blocks( 69 struct xfs_mount *mp) 70 { 71 if (xfs_has_reflink(mp)) 72 return xfs_refc_block(mp) + 1; 73 if (xfs_has_rmapbt(mp)) 74 return XFS_RMAP_BLOCK(mp) + 1; 75 if (xfs_has_finobt(mp)) 76 return XFS_FIBT_BLOCK(mp) + 1; 77 return XFS_IBT_BLOCK(mp) + 1; 78 } 79 80 /* 81 * The number of blocks per AG that we withhold from xfs_dec_fdblocks to 82 * guarantee that we can refill the AGFL prior to allocating space in a nearly 83 * full AG. Although the space described by the free space btrees, the 84 * blocks used by the freesp btrees themselves, and the blocks owned by the 85 * AGFL are counted in the ondisk fdblocks, it's a mistake to let the ondisk 86 * free space in the AG drop so low that the free space btrees cannot refill an 87 * empty AGFL up to the minimum level. Rather than grind through empty AGs 88 * until the fs goes down, we subtract this many AG blocks from the incore 89 * fdblocks to ensure user allocation does not overcommit the space the 90 * filesystem needs for the AGFLs. The rmap btree uses a per-AG reservation to 91 * withhold space from xfs_dec_fdblocks, so we do not account for that here. 92 */ 93 #define XFS_ALLOCBT_AGFL_RESERVE 4 94 95 /* 96 * Compute the number of blocks that we set aside to guarantee the ability to 97 * refill the AGFL and handle a full bmap btree split. 98 * 99 * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of 100 * AGF buffer (PV 947395), we place constraints on the relationship among 101 * actual allocations for data blocks, freelist blocks, and potential file data 102 * bmap btree blocks. However, these restrictions may result in no actual space 103 * allocated for a delayed extent, for example, a data block in a certain AG is 104 * allocated but there is no additional block for the additional bmap btree 105 * block due to a split of the bmap btree of the file. The result of this may 106 * lead to an infinite loop when the file gets flushed to disk and all delayed 107 * extents need to be actually allocated. To get around this, we explicitly set 108 * aside a few blocks which will not be reserved in delayed allocation. 109 * 110 * For each AG, we need to reserve enough blocks to replenish a totally empty 111 * AGFL and 4 more to handle a potential split of the file's bmap btree. 112 */ 113 unsigned int 114 xfs_alloc_set_aside( 115 struct xfs_mount *mp) 116 { 117 return mp->m_sb.sb_agcount * (XFS_ALLOCBT_AGFL_RESERVE + 4); 118 } 119 120 /* 121 * When deciding how much space to allocate out of an AG, we limit the 122 * allocation maximum size to the size the AG. However, we cannot use all the 123 * blocks in the AG - some are permanently used by metadata. These 124 * blocks are generally: 125 * - the AG superblock, AGF, AGI and AGFL 126 * - the AGF (bno and cnt) and AGI btree root blocks, and optionally 127 * the AGI free inode and rmap btree root blocks. 128 * - blocks on the AGFL according to xfs_alloc_set_aside() limits 129 * - the rmapbt root block 130 * 131 * The AG headers are sector sized, so the amount of space they take up is 132 * dependent on filesystem geometry. The others are all single blocks. 133 */ 134 unsigned int 135 xfs_alloc_ag_max_usable( 136 struct xfs_mount *mp) 137 { 138 unsigned int blocks; 139 140 blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */ 141 blocks += XFS_ALLOCBT_AGFL_RESERVE; 142 blocks += 3; /* AGF, AGI btree root blocks */ 143 if (xfs_has_finobt(mp)) 144 blocks++; /* finobt root block */ 145 if (xfs_has_rmapbt(mp)) 146 blocks++; /* rmap root block */ 147 if (xfs_has_reflink(mp)) 148 blocks++; /* refcount root block */ 149 150 return mp->m_sb.sb_agblocks - blocks; 151 } 152 153 154 static int 155 xfs_alloc_lookup( 156 struct xfs_btree_cur *cur, 157 xfs_lookup_t dir, 158 xfs_agblock_t bno, 159 xfs_extlen_t len, 160 int *stat) 161 { 162 int error; 163 164 cur->bc_rec.a.ar_startblock = bno; 165 cur->bc_rec.a.ar_blockcount = len; 166 error = xfs_btree_lookup(cur, dir, stat); 167 if (*stat == 1) 168 cur->bc_flags |= XFS_BTREE_ALLOCBT_ACTIVE; 169 else 170 cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE; 171 return error; 172 } 173 174 /* 175 * Lookup the record equal to [bno, len] in the btree given by cur. 176 */ 177 static inline int /* error */ 178 xfs_alloc_lookup_eq( 179 struct xfs_btree_cur *cur, /* btree cursor */ 180 xfs_agblock_t bno, /* starting block of extent */ 181 xfs_extlen_t len, /* length of extent */ 182 int *stat) /* success/failure */ 183 { 184 return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, bno, len, stat); 185 } 186 187 /* 188 * Lookup the first record greater than or equal to [bno, len] 189 * in the btree given by cur. 190 */ 191 int /* error */ 192 xfs_alloc_lookup_ge( 193 struct xfs_btree_cur *cur, /* btree cursor */ 194 xfs_agblock_t bno, /* starting block of extent */ 195 xfs_extlen_t len, /* length of extent */ 196 int *stat) /* success/failure */ 197 { 198 return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, bno, len, stat); 199 } 200 201 /* 202 * Lookup the first record less than or equal to [bno, len] 203 * in the btree given by cur. 204 */ 205 int /* error */ 206 xfs_alloc_lookup_le( 207 struct xfs_btree_cur *cur, /* btree cursor */ 208 xfs_agblock_t bno, /* starting block of extent */ 209 xfs_extlen_t len, /* length of extent */ 210 int *stat) /* success/failure */ 211 { 212 return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, bno, len, stat); 213 } 214 215 static inline bool 216 xfs_alloc_cur_active( 217 struct xfs_btree_cur *cur) 218 { 219 return cur && (cur->bc_flags & XFS_BTREE_ALLOCBT_ACTIVE); 220 } 221 222 /* 223 * Update the record referred to by cur to the value given 224 * by [bno, len]. 225 * This either works (return 0) or gets an EFSCORRUPTED error. 226 */ 227 STATIC int /* error */ 228 xfs_alloc_update( 229 struct xfs_btree_cur *cur, /* btree cursor */ 230 xfs_agblock_t bno, /* starting block of extent */ 231 xfs_extlen_t len) /* length of extent */ 232 { 233 union xfs_btree_rec rec; 234 235 rec.alloc.ar_startblock = cpu_to_be32(bno); 236 rec.alloc.ar_blockcount = cpu_to_be32(len); 237 return xfs_btree_update(cur, &rec); 238 } 239 240 /* Convert the ondisk btree record to its incore representation. */ 241 void 242 xfs_alloc_btrec_to_irec( 243 const union xfs_btree_rec *rec, 244 struct xfs_alloc_rec_incore *irec) 245 { 246 irec->ar_startblock = be32_to_cpu(rec->alloc.ar_startblock); 247 irec->ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount); 248 } 249 250 /* Simple checks for free space records. */ 251 xfs_failaddr_t 252 xfs_alloc_check_irec( 253 struct xfs_perag *pag, 254 const struct xfs_alloc_rec_incore *irec) 255 { 256 if (irec->ar_blockcount == 0) 257 return __this_address; 258 259 /* check for valid extent range, including overflow */ 260 if (!xfs_verify_agbext(pag, irec->ar_startblock, irec->ar_blockcount)) 261 return __this_address; 262 263 return NULL; 264 } 265 266 static inline int 267 xfs_alloc_complain_bad_rec( 268 struct xfs_btree_cur *cur, 269 xfs_failaddr_t fa, 270 const struct xfs_alloc_rec_incore *irec) 271 { 272 struct xfs_mount *mp = cur->bc_mp; 273 274 xfs_warn(mp, 275 "%sbt record corruption in AG %d detected at %pS!", 276 cur->bc_ops->name, cur->bc_group->xg_gno, fa); 277 xfs_warn(mp, 278 "start block 0x%x block count 0x%x", irec->ar_startblock, 279 irec->ar_blockcount); 280 xfs_btree_mark_sick(cur); 281 return -EFSCORRUPTED; 282 } 283 284 /* 285 * Get the data from the pointed-to record. 286 */ 287 int /* error */ 288 xfs_alloc_get_rec( 289 struct xfs_btree_cur *cur, /* btree cursor */ 290 xfs_agblock_t *bno, /* output: starting block of extent */ 291 xfs_extlen_t *len, /* output: length of extent */ 292 int *stat) /* output: success/failure */ 293 { 294 struct xfs_alloc_rec_incore irec; 295 union xfs_btree_rec *rec; 296 xfs_failaddr_t fa; 297 int error; 298 299 error = xfs_btree_get_rec(cur, &rec, stat); 300 if (error || !(*stat)) 301 return error; 302 303 xfs_alloc_btrec_to_irec(rec, &irec); 304 fa = xfs_alloc_check_irec(to_perag(cur->bc_group), &irec); 305 if (fa) 306 return xfs_alloc_complain_bad_rec(cur, fa, &irec); 307 308 *bno = irec.ar_startblock; 309 *len = irec.ar_blockcount; 310 return 0; 311 } 312 313 /* 314 * Compute aligned version of the found extent. 315 * Takes alignment and min length into account. 316 */ 317 STATIC bool 318 xfs_alloc_compute_aligned( 319 xfs_alloc_arg_t *args, /* allocation argument structure */ 320 xfs_agblock_t foundbno, /* starting block in found extent */ 321 xfs_extlen_t foundlen, /* length in found extent */ 322 xfs_agblock_t *resbno, /* result block number */ 323 xfs_extlen_t *reslen, /* result length */ 324 unsigned *busy_gen) 325 { 326 xfs_agblock_t bno = foundbno; 327 xfs_extlen_t len = foundlen; 328 xfs_extlen_t diff; 329 bool busy; 330 331 /* Trim busy sections out of found extent */ 332 busy = xfs_extent_busy_trim(pag_group(args->pag), args->minlen, 333 args->maxlen, &bno, &len, busy_gen); 334 335 /* 336 * If we have a largish extent that happens to start before min_agbno, 337 * see if we can shift it into range... 338 */ 339 if (bno < args->min_agbno && bno + len > args->min_agbno) { 340 diff = args->min_agbno - bno; 341 if (len > diff) { 342 bno += diff; 343 len -= diff; 344 } 345 } 346 347 if (args->alignment > 1 && len >= args->minlen) { 348 xfs_agblock_t aligned_bno = roundup(bno, args->alignment); 349 350 diff = aligned_bno - bno; 351 352 *resbno = aligned_bno; 353 *reslen = diff >= len ? 0 : len - diff; 354 } else { 355 *resbno = bno; 356 *reslen = len; 357 } 358 359 return busy; 360 } 361 362 /* 363 * Compute best start block and diff for "near" allocations. 364 * freelen >= wantlen already checked by caller. 365 */ 366 STATIC xfs_extlen_t /* difference value (absolute) */ 367 xfs_alloc_compute_diff( 368 xfs_agblock_t wantbno, /* target starting block */ 369 xfs_extlen_t wantlen, /* target length */ 370 xfs_extlen_t alignment, /* target alignment */ 371 int datatype, /* are we allocating data? */ 372 xfs_agblock_t freebno, /* freespace's starting block */ 373 xfs_extlen_t freelen, /* freespace's length */ 374 xfs_agblock_t *newbnop) /* result: best start block from free */ 375 { 376 xfs_agblock_t freeend; /* end of freespace extent */ 377 xfs_agblock_t newbno1; /* return block number */ 378 xfs_agblock_t newbno2; /* other new block number */ 379 xfs_extlen_t newlen1 = 0; /* length with newbno1 */ 380 xfs_extlen_t newlen2 = 0; /* length with newbno2 */ 381 xfs_agblock_t wantend; /* end of target extent */ 382 bool userdata = datatype & XFS_ALLOC_USERDATA; 383 384 ASSERT(freelen >= wantlen); 385 freeend = freebno + freelen; 386 wantend = wantbno + wantlen; 387 /* 388 * We want to allocate from the start of a free extent if it is past 389 * the desired block or if we are allocating user data and the free 390 * extent is before desired block. The second case is there to allow 391 * for contiguous allocation from the remaining free space if the file 392 * grows in the short term. 393 */ 394 if (freebno >= wantbno || (userdata && freeend < wantend)) { 395 if ((newbno1 = roundup(freebno, alignment)) >= freeend) 396 newbno1 = NULLAGBLOCK; 397 } else if (freeend >= wantend && alignment > 1) { 398 newbno1 = roundup(wantbno, alignment); 399 newbno2 = newbno1 - alignment; 400 if (newbno1 >= freeend) 401 newbno1 = NULLAGBLOCK; 402 else 403 newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1); 404 if (newbno2 < freebno) 405 newbno2 = NULLAGBLOCK; 406 else 407 newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2); 408 if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) { 409 if (newlen1 < newlen2 || 410 (newlen1 == newlen2 && 411 abs_diff(newbno1, wantbno) > 412 abs_diff(newbno2, wantbno))) 413 newbno1 = newbno2; 414 } else if (newbno2 != NULLAGBLOCK) 415 newbno1 = newbno2; 416 } else if (freeend >= wantend) { 417 newbno1 = wantbno; 418 } else if (alignment > 1) { 419 newbno1 = roundup(freeend - wantlen, alignment); 420 if (newbno1 > freeend - wantlen && 421 newbno1 - alignment >= freebno) 422 newbno1 -= alignment; 423 else if (newbno1 >= freeend) 424 newbno1 = NULLAGBLOCK; 425 } else 426 newbno1 = freeend - wantlen; 427 *newbnop = newbno1; 428 return newbno1 == NULLAGBLOCK ? 0 : abs_diff(newbno1, wantbno); 429 } 430 431 /* 432 * Fix up the length, based on mod and prod. 433 * len should be k * prod + mod for some k. 434 * If len is too small it is returned unchanged. 435 * If len hits maxlen it is left alone. 436 */ 437 STATIC void 438 xfs_alloc_fix_len( 439 xfs_alloc_arg_t *args) /* allocation argument structure */ 440 { 441 xfs_extlen_t k; 442 xfs_extlen_t rlen; 443 444 ASSERT(args->mod < args->prod); 445 rlen = args->len; 446 ASSERT(rlen >= args->minlen); 447 ASSERT(rlen <= args->maxlen); 448 if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen || 449 (args->mod == 0 && rlen < args->prod)) 450 return; 451 k = rlen % args->prod; 452 if (k == args->mod) 453 return; 454 if (k > args->mod) 455 rlen = rlen - (k - args->mod); 456 else 457 rlen = rlen - args->prod + (args->mod - k); 458 /* casts to (int) catch length underflows */ 459 if ((int)rlen < (int)args->minlen) 460 return; 461 ASSERT(rlen >= args->minlen && rlen <= args->maxlen); 462 ASSERT(rlen % args->prod == args->mod); 463 ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >= 464 rlen + args->minleft); 465 args->len = rlen; 466 } 467 468 /* 469 * Determine if the cursor points to the block that contains the right-most 470 * block of records in the by-count btree. This block contains the largest 471 * contiguous free extent in the AG, so if we modify a record in this block we 472 * need to call xfs_alloc_fixup_longest() once the modifications are done to 473 * ensure the agf->agf_longest field is kept up to date with the longest free 474 * extent tracked by the by-count btree. 475 */ 476 static bool 477 xfs_alloc_cursor_at_lastrec( 478 struct xfs_btree_cur *cnt_cur) 479 { 480 struct xfs_btree_block *block; 481 union xfs_btree_ptr ptr; 482 struct xfs_buf *bp; 483 484 block = xfs_btree_get_block(cnt_cur, 0, &bp); 485 486 xfs_btree_get_sibling(cnt_cur, block, &ptr, XFS_BB_RIGHTSIB); 487 return xfs_btree_ptr_is_null(cnt_cur, &ptr); 488 } 489 490 /* 491 * Find the rightmost record of the cntbt, and return the longest free space 492 * recorded in it. Simply set both the block number and the length to their 493 * maximum values before searching. 494 */ 495 static int 496 xfs_cntbt_longest( 497 struct xfs_btree_cur *cnt_cur, 498 xfs_extlen_t *longest) 499 { 500 struct xfs_alloc_rec_incore irec; 501 union xfs_btree_rec *rec; 502 int stat = 0; 503 int error; 504 505 memset(&cnt_cur->bc_rec, 0xFF, sizeof(cnt_cur->bc_rec)); 506 error = xfs_btree_lookup(cnt_cur, XFS_LOOKUP_LE, &stat); 507 if (error) 508 return error; 509 if (!stat) { 510 /* totally empty tree */ 511 *longest = 0; 512 return 0; 513 } 514 515 error = xfs_btree_get_rec(cnt_cur, &rec, &stat); 516 if (error) 517 return error; 518 if (XFS_IS_CORRUPT(cnt_cur->bc_mp, !stat)) { 519 xfs_btree_mark_sick(cnt_cur); 520 return -EFSCORRUPTED; 521 } 522 523 xfs_alloc_btrec_to_irec(rec, &irec); 524 *longest = irec.ar_blockcount; 525 return 0; 526 } 527 528 /* 529 * Update the longest contiguous free extent in the AG from the by-count cursor 530 * that is passed to us. This should be done at the end of any allocation or 531 * freeing operation that touches the longest extent in the btree. 532 * 533 * Needing to update the longest extent can be determined by calling 534 * xfs_alloc_cursor_at_lastrec() after the cursor is positioned for record 535 * modification but before the modification begins. 536 */ 537 static int 538 xfs_alloc_fixup_longest( 539 struct xfs_btree_cur *cnt_cur) 540 { 541 struct xfs_perag *pag = to_perag(cnt_cur->bc_group); 542 struct xfs_buf *bp = cnt_cur->bc_ag.agbp; 543 struct xfs_agf *agf = bp->b_addr; 544 xfs_extlen_t longest = 0; 545 int error; 546 547 /* Lookup last rec in order to update AGF. */ 548 error = xfs_cntbt_longest(cnt_cur, &longest); 549 if (error) 550 return error; 551 552 pag->pagf_longest = longest; 553 agf->agf_longest = cpu_to_be32(pag->pagf_longest); 554 xfs_alloc_log_agf(cnt_cur->bc_tp, bp, XFS_AGF_LONGEST); 555 556 return 0; 557 } 558 559 /* 560 * Update the two btrees, logically removing from freespace the extent 561 * starting at rbno, rlen blocks. The extent is contained within the 562 * actual (current) free extent fbno for flen blocks. 563 * Flags are passed in indicating whether the cursors are set to the 564 * relevant records. 565 */ 566 STATIC int /* error code */ 567 xfs_alloc_fixup_trees( 568 struct xfs_btree_cur *cnt_cur, /* cursor for by-size btree */ 569 struct xfs_btree_cur *bno_cur, /* cursor for by-block btree */ 570 xfs_agblock_t fbno, /* starting block of free extent */ 571 xfs_extlen_t flen, /* length of free extent */ 572 xfs_agblock_t rbno, /* starting block of returned extent */ 573 xfs_extlen_t rlen, /* length of returned extent */ 574 int flags) /* flags, XFSA_FIXUP_... */ 575 { 576 int error; /* error code */ 577 int i; /* operation results */ 578 xfs_agblock_t nfbno1; /* first new free startblock */ 579 xfs_agblock_t nfbno2; /* second new free startblock */ 580 xfs_extlen_t nflen1 = 0; /* first new free length */ 581 xfs_extlen_t nflen2 = 0; /* second new free length */ 582 struct xfs_mount *mp; 583 bool fixup_longest = false; 584 585 mp = cnt_cur->bc_mp; 586 587 /* 588 * Look up the record in the by-size tree if necessary. 589 */ 590 if (flags & XFSA_FIXUP_CNT_OK) { 591 #ifdef DEBUG 592 if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i))) 593 return error; 594 if (XFS_IS_CORRUPT(mp, 595 i != 1 || 596 nfbno1 != fbno || 597 nflen1 != flen)) { 598 xfs_btree_mark_sick(cnt_cur); 599 return -EFSCORRUPTED; 600 } 601 #endif 602 } else { 603 if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i))) 604 return error; 605 if (XFS_IS_CORRUPT(mp, i != 1)) { 606 xfs_btree_mark_sick(cnt_cur); 607 return -EFSCORRUPTED; 608 } 609 } 610 /* 611 * Look up the record in the by-block tree if necessary. 612 */ 613 if (flags & XFSA_FIXUP_BNO_OK) { 614 #ifdef DEBUG 615 if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i))) 616 return error; 617 if (XFS_IS_CORRUPT(mp, 618 i != 1 || 619 nfbno1 != fbno || 620 nflen1 != flen)) { 621 xfs_btree_mark_sick(bno_cur); 622 return -EFSCORRUPTED; 623 } 624 #endif 625 } else { 626 if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i))) 627 return error; 628 if (XFS_IS_CORRUPT(mp, i != 1)) { 629 xfs_btree_mark_sick(bno_cur); 630 return -EFSCORRUPTED; 631 } 632 } 633 634 #ifdef DEBUG 635 if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) { 636 struct xfs_btree_block *bnoblock; 637 struct xfs_btree_block *cntblock; 638 639 bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_levels[0].bp); 640 cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_levels[0].bp); 641 642 if (XFS_IS_CORRUPT(mp, 643 bnoblock->bb_numrecs != 644 cntblock->bb_numrecs)) { 645 xfs_btree_mark_sick(bno_cur); 646 return -EFSCORRUPTED; 647 } 648 } 649 #endif 650 651 /* 652 * Deal with all four cases: the allocated record is contained 653 * within the freespace record, so we can have new freespace 654 * at either (or both) end, or no freespace remaining. 655 */ 656 if (rbno == fbno && rlen == flen) 657 nfbno1 = nfbno2 = NULLAGBLOCK; 658 else if (rbno == fbno) { 659 nfbno1 = rbno + rlen; 660 nflen1 = flen - rlen; 661 nfbno2 = NULLAGBLOCK; 662 } else if (rbno + rlen == fbno + flen) { 663 nfbno1 = fbno; 664 nflen1 = flen - rlen; 665 nfbno2 = NULLAGBLOCK; 666 } else { 667 nfbno1 = fbno; 668 nflen1 = rbno - fbno; 669 nfbno2 = rbno + rlen; 670 nflen2 = (fbno + flen) - nfbno2; 671 } 672 673 if (xfs_alloc_cursor_at_lastrec(cnt_cur)) 674 fixup_longest = true; 675 676 /* 677 * Delete the entry from the by-size btree. 678 */ 679 if ((error = xfs_btree_delete(cnt_cur, &i))) 680 return error; 681 if (XFS_IS_CORRUPT(mp, i != 1)) { 682 xfs_btree_mark_sick(cnt_cur); 683 return -EFSCORRUPTED; 684 } 685 /* 686 * Add new by-size btree entry(s). 687 */ 688 if (nfbno1 != NULLAGBLOCK) { 689 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i))) 690 return error; 691 if (XFS_IS_CORRUPT(mp, i != 0)) { 692 xfs_btree_mark_sick(cnt_cur); 693 return -EFSCORRUPTED; 694 } 695 if ((error = xfs_btree_insert(cnt_cur, &i))) 696 return error; 697 if (XFS_IS_CORRUPT(mp, i != 1)) { 698 xfs_btree_mark_sick(cnt_cur); 699 return -EFSCORRUPTED; 700 } 701 } 702 if (nfbno2 != NULLAGBLOCK) { 703 if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i))) 704 return error; 705 if (XFS_IS_CORRUPT(mp, i != 0)) { 706 xfs_btree_mark_sick(cnt_cur); 707 return -EFSCORRUPTED; 708 } 709 if ((error = xfs_btree_insert(cnt_cur, &i))) 710 return error; 711 if (XFS_IS_CORRUPT(mp, i != 1)) { 712 xfs_btree_mark_sick(cnt_cur); 713 return -EFSCORRUPTED; 714 } 715 } 716 /* 717 * Fix up the by-block btree entry(s). 718 */ 719 if (nfbno1 == NULLAGBLOCK) { 720 /* 721 * No remaining freespace, just delete the by-block tree entry. 722 */ 723 if ((error = xfs_btree_delete(bno_cur, &i))) 724 return error; 725 if (XFS_IS_CORRUPT(mp, i != 1)) { 726 xfs_btree_mark_sick(bno_cur); 727 return -EFSCORRUPTED; 728 } 729 } else { 730 /* 731 * Update the by-block entry to start later|be shorter. 732 */ 733 if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1))) 734 return error; 735 } 736 if (nfbno2 != NULLAGBLOCK) { 737 /* 738 * 2 resulting free entries, need to add one. 739 */ 740 if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i))) 741 return error; 742 if (XFS_IS_CORRUPT(mp, i != 0)) { 743 xfs_btree_mark_sick(bno_cur); 744 return -EFSCORRUPTED; 745 } 746 if ((error = xfs_btree_insert(bno_cur, &i))) 747 return error; 748 if (XFS_IS_CORRUPT(mp, i != 1)) { 749 xfs_btree_mark_sick(bno_cur); 750 return -EFSCORRUPTED; 751 } 752 } 753 754 if (fixup_longest) 755 return xfs_alloc_fixup_longest(cnt_cur); 756 757 return 0; 758 } 759 760 /* 761 * We do not verify the AGFL contents against AGF-based index counters here, 762 * even though we may have access to the perag that contains shadow copies. We 763 * don't know if the AGF based counters have been checked, and if they have they 764 * still may be inconsistent because they haven't yet been reset on the first 765 * allocation after the AGF has been read in. 766 * 767 * This means we can only check that all agfl entries contain valid or null 768 * values because we can't reliably determine the active range to exclude 769 * NULLAGBNO as a valid value. 770 * 771 * However, we can't even do that for v4 format filesystems because there are 772 * old versions of mkfs out there that does not initialise the AGFL to known, 773 * verifiable values. HEnce we can't tell the difference between a AGFL block 774 * allocated by mkfs and a corrupted AGFL block here on v4 filesystems. 775 * 776 * As a result, we can only fully validate AGFL block numbers when we pull them 777 * from the freelist in xfs_alloc_get_freelist(). 778 */ 779 static xfs_failaddr_t 780 xfs_agfl_verify( 781 struct xfs_buf *bp) 782 { 783 struct xfs_mount *mp = bp->b_mount; 784 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp); 785 __be32 *agfl_bno = xfs_buf_to_agfl_bno(bp); 786 int i; 787 788 if (!xfs_has_crc(mp)) 789 return NULL; 790 791 if (!xfs_verify_magic(bp, agfl->agfl_magicnum)) 792 return __this_address; 793 if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid)) 794 return __this_address; 795 /* 796 * during growfs operations, the perag is not fully initialised, 797 * so we can't use it for any useful checking. growfs ensures we can't 798 * use it by using uncached buffers that don't have the perag attached 799 * so we can detect and avoid this problem. 800 */ 801 if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != pag_agno((bp->b_pag))) 802 return __this_address; 803 804 for (i = 0; i < xfs_agfl_size(mp); i++) { 805 if (be32_to_cpu(agfl_bno[i]) != NULLAGBLOCK && 806 be32_to_cpu(agfl_bno[i]) >= mp->m_sb.sb_agblocks) 807 return __this_address; 808 } 809 810 if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn))) 811 return __this_address; 812 return NULL; 813 } 814 815 static void 816 xfs_agfl_read_verify( 817 struct xfs_buf *bp) 818 { 819 struct xfs_mount *mp = bp->b_mount; 820 xfs_failaddr_t fa; 821 822 /* 823 * There is no verification of non-crc AGFLs because mkfs does not 824 * initialise the AGFL to zero or NULL. Hence the only valid part of the 825 * AGFL is what the AGF says is active. We can't get to the AGF, so we 826 * can't verify just those entries are valid. 827 */ 828 if (!xfs_has_crc(mp)) 829 return; 830 831 if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF)) 832 xfs_verifier_error(bp, -EFSBADCRC, __this_address); 833 else { 834 fa = xfs_agfl_verify(bp); 835 if (fa) 836 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 837 } 838 } 839 840 static void 841 xfs_agfl_write_verify( 842 struct xfs_buf *bp) 843 { 844 struct xfs_mount *mp = bp->b_mount; 845 struct xfs_buf_log_item *bip = bp->b_log_item; 846 xfs_failaddr_t fa; 847 848 /* no verification of non-crc AGFLs */ 849 if (!xfs_has_crc(mp)) 850 return; 851 852 fa = xfs_agfl_verify(bp); 853 if (fa) { 854 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 855 return; 856 } 857 858 if (bip) 859 XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn); 860 861 xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF); 862 } 863 864 const struct xfs_buf_ops xfs_agfl_buf_ops = { 865 .name = "xfs_agfl", 866 .magic = { cpu_to_be32(XFS_AGFL_MAGIC), cpu_to_be32(XFS_AGFL_MAGIC) }, 867 .verify_read = xfs_agfl_read_verify, 868 .verify_write = xfs_agfl_write_verify, 869 .verify_struct = xfs_agfl_verify, 870 }; 871 872 /* 873 * Read in the allocation group free block array. 874 */ 875 int 876 xfs_alloc_read_agfl( 877 struct xfs_perag *pag, 878 struct xfs_trans *tp, 879 struct xfs_buf **bpp) 880 { 881 struct xfs_mount *mp = pag_mount(pag); 882 struct xfs_buf *bp; 883 int error; 884 885 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 886 XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGFL_DADDR(mp)), 887 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops); 888 if (xfs_metadata_is_sick(error)) 889 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGFL); 890 if (error) 891 return error; 892 xfs_buf_set_ref(bp, XFS_AGFL_REF); 893 *bpp = bp; 894 return 0; 895 } 896 897 STATIC int 898 xfs_alloc_update_counters( 899 struct xfs_trans *tp, 900 struct xfs_buf *agbp, 901 long len) 902 { 903 struct xfs_agf *agf = agbp->b_addr; 904 905 agbp->b_pag->pagf_freeblks += len; 906 be32_add_cpu(&agf->agf_freeblks, len); 907 908 if (unlikely(be32_to_cpu(agf->agf_freeblks) > 909 be32_to_cpu(agf->agf_length))) { 910 xfs_buf_mark_corrupt(agbp); 911 xfs_ag_mark_sick(agbp->b_pag, XFS_SICK_AG_AGF); 912 return -EFSCORRUPTED; 913 } 914 915 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS); 916 return 0; 917 } 918 919 /* 920 * Block allocation algorithm and data structures. 921 */ 922 struct xfs_alloc_cur { 923 struct xfs_btree_cur *cnt; /* btree cursors */ 924 struct xfs_btree_cur *bnolt; 925 struct xfs_btree_cur *bnogt; 926 xfs_extlen_t cur_len;/* current search length */ 927 xfs_agblock_t rec_bno;/* extent startblock */ 928 xfs_extlen_t rec_len;/* extent length */ 929 xfs_agblock_t bno; /* alloc bno */ 930 xfs_extlen_t len; /* alloc len */ 931 xfs_extlen_t diff; /* diff from search bno */ 932 unsigned int busy_gen;/* busy state */ 933 bool busy; 934 }; 935 936 /* 937 * Set up cursors, etc. in the extent allocation cursor. This function can be 938 * called multiple times to reset an initialized structure without having to 939 * reallocate cursors. 940 */ 941 static int 942 xfs_alloc_cur_setup( 943 struct xfs_alloc_arg *args, 944 struct xfs_alloc_cur *acur) 945 { 946 int error; 947 int i; 948 949 acur->cur_len = args->maxlen; 950 acur->rec_bno = 0; 951 acur->rec_len = 0; 952 acur->bno = 0; 953 acur->len = 0; 954 acur->diff = -1; 955 acur->busy = false; 956 acur->busy_gen = 0; 957 958 /* 959 * Perform an initial cntbt lookup to check for availability of maxlen 960 * extents. If this fails, we'll return -ENOSPC to signal the caller to 961 * attempt a small allocation. 962 */ 963 if (!acur->cnt) 964 acur->cnt = xfs_cntbt_init_cursor(args->mp, args->tp, 965 args->agbp, args->pag); 966 error = xfs_alloc_lookup_ge(acur->cnt, 0, args->maxlen, &i); 967 if (error) 968 return error; 969 970 /* 971 * Allocate the bnobt left and right search cursors. 972 */ 973 if (!acur->bnolt) 974 acur->bnolt = xfs_bnobt_init_cursor(args->mp, args->tp, 975 args->agbp, args->pag); 976 if (!acur->bnogt) 977 acur->bnogt = xfs_bnobt_init_cursor(args->mp, args->tp, 978 args->agbp, args->pag); 979 return i == 1 ? 0 : -ENOSPC; 980 } 981 982 static void 983 xfs_alloc_cur_close( 984 struct xfs_alloc_cur *acur, 985 bool error) 986 { 987 int cur_error = XFS_BTREE_NOERROR; 988 989 if (error) 990 cur_error = XFS_BTREE_ERROR; 991 992 if (acur->cnt) 993 xfs_btree_del_cursor(acur->cnt, cur_error); 994 if (acur->bnolt) 995 xfs_btree_del_cursor(acur->bnolt, cur_error); 996 if (acur->bnogt) 997 xfs_btree_del_cursor(acur->bnogt, cur_error); 998 acur->cnt = acur->bnolt = acur->bnogt = NULL; 999 } 1000 1001 /* 1002 * Check an extent for allocation and track the best available candidate in the 1003 * allocation structure. The cursor is deactivated if it has entered an out of 1004 * range state based on allocation arguments. Optionally return the extent 1005 * extent geometry and allocation status if requested by the caller. 1006 */ 1007 static int 1008 xfs_alloc_cur_check( 1009 struct xfs_alloc_arg *args, 1010 struct xfs_alloc_cur *acur, 1011 struct xfs_btree_cur *cur, 1012 int *new) 1013 { 1014 int error, i; 1015 xfs_agblock_t bno, bnoa, bnew; 1016 xfs_extlen_t len, lena, diff = -1; 1017 bool busy; 1018 unsigned busy_gen = 0; 1019 bool deactivate = false; 1020 bool isbnobt = xfs_btree_is_bno(cur->bc_ops); 1021 1022 *new = 0; 1023 1024 error = xfs_alloc_get_rec(cur, &bno, &len, &i); 1025 if (error) 1026 return error; 1027 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1028 xfs_btree_mark_sick(cur); 1029 return -EFSCORRUPTED; 1030 } 1031 1032 /* 1033 * Check minlen and deactivate a cntbt cursor if out of acceptable size 1034 * range (i.e., walking backwards looking for a minlen extent). 1035 */ 1036 if (len < args->minlen) { 1037 deactivate = !isbnobt; 1038 goto out; 1039 } 1040 1041 busy = xfs_alloc_compute_aligned(args, bno, len, &bnoa, &lena, 1042 &busy_gen); 1043 acur->busy |= busy; 1044 if (busy) 1045 acur->busy_gen = busy_gen; 1046 /* deactivate a bnobt cursor outside of locality range */ 1047 if (bnoa < args->min_agbno || bnoa > args->max_agbno) { 1048 deactivate = isbnobt; 1049 goto out; 1050 } 1051 if (lena < args->minlen) 1052 goto out; 1053 1054 args->len = XFS_EXTLEN_MIN(lena, args->maxlen); 1055 xfs_alloc_fix_len(args); 1056 ASSERT(args->len >= args->minlen); 1057 if (args->len < acur->len) 1058 goto out; 1059 1060 /* 1061 * We have an aligned record that satisfies minlen and beats or matches 1062 * the candidate extent size. Compare locality for near allocation mode. 1063 */ 1064 diff = xfs_alloc_compute_diff(args->agbno, args->len, 1065 args->alignment, args->datatype, 1066 bnoa, lena, &bnew); 1067 if (bnew == NULLAGBLOCK) 1068 goto out; 1069 1070 /* 1071 * Deactivate a bnobt cursor with worse locality than the current best. 1072 */ 1073 if (diff > acur->diff) { 1074 deactivate = isbnobt; 1075 goto out; 1076 } 1077 1078 ASSERT(args->len > acur->len || 1079 (args->len == acur->len && diff <= acur->diff)); 1080 acur->rec_bno = bno; 1081 acur->rec_len = len; 1082 acur->bno = bnew; 1083 acur->len = args->len; 1084 acur->diff = diff; 1085 *new = 1; 1086 1087 /* 1088 * We're done if we found a perfect allocation. This only deactivates 1089 * the current cursor, but this is just an optimization to terminate a 1090 * cntbt search that otherwise runs to the edge of the tree. 1091 */ 1092 if (acur->diff == 0 && acur->len == args->maxlen) 1093 deactivate = true; 1094 out: 1095 if (deactivate) 1096 cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE; 1097 trace_xfs_alloc_cur_check(cur, bno, len, diff, *new); 1098 return 0; 1099 } 1100 1101 /* 1102 * Complete an allocation of a candidate extent. Remove the extent from both 1103 * trees and update the args structure. 1104 */ 1105 STATIC int 1106 xfs_alloc_cur_finish( 1107 struct xfs_alloc_arg *args, 1108 struct xfs_alloc_cur *acur) 1109 { 1110 int error; 1111 1112 ASSERT(acur->cnt && acur->bnolt); 1113 ASSERT(acur->bno >= acur->rec_bno); 1114 ASSERT(acur->bno + acur->len <= acur->rec_bno + acur->rec_len); 1115 ASSERT(xfs_verify_agbext(args->pag, acur->rec_bno, acur->rec_len)); 1116 1117 error = xfs_alloc_fixup_trees(acur->cnt, acur->bnolt, acur->rec_bno, 1118 acur->rec_len, acur->bno, acur->len, 0); 1119 if (error) 1120 return error; 1121 1122 args->agbno = acur->bno; 1123 args->len = acur->len; 1124 args->wasfromfl = 0; 1125 1126 trace_xfs_alloc_cur(args); 1127 return 0; 1128 } 1129 1130 /* 1131 * Locality allocation lookup algorithm. This expects a cntbt cursor and uses 1132 * bno optimized lookup to search for extents with ideal size and locality. 1133 */ 1134 STATIC int 1135 xfs_alloc_cntbt_iter( 1136 struct xfs_alloc_arg *args, 1137 struct xfs_alloc_cur *acur) 1138 { 1139 struct xfs_btree_cur *cur = acur->cnt; 1140 xfs_agblock_t bno; 1141 xfs_extlen_t len, cur_len; 1142 int error; 1143 int i; 1144 1145 if (!xfs_alloc_cur_active(cur)) 1146 return 0; 1147 1148 /* locality optimized lookup */ 1149 cur_len = acur->cur_len; 1150 error = xfs_alloc_lookup_ge(cur, args->agbno, cur_len, &i); 1151 if (error) 1152 return error; 1153 if (i == 0) 1154 return 0; 1155 error = xfs_alloc_get_rec(cur, &bno, &len, &i); 1156 if (error) 1157 return error; 1158 1159 /* check the current record and update search length from it */ 1160 error = xfs_alloc_cur_check(args, acur, cur, &i); 1161 if (error) 1162 return error; 1163 ASSERT(len >= acur->cur_len); 1164 acur->cur_len = len; 1165 1166 /* 1167 * We looked up the first record >= [agbno, len] above. The agbno is a 1168 * secondary key and so the current record may lie just before or after 1169 * agbno. If it is past agbno, check the previous record too so long as 1170 * the length matches as it may be closer. Don't check a smaller record 1171 * because that could deactivate our cursor. 1172 */ 1173 if (bno > args->agbno) { 1174 error = xfs_btree_decrement(cur, 0, &i); 1175 if (!error && i) { 1176 error = xfs_alloc_get_rec(cur, &bno, &len, &i); 1177 if (!error && i && len == acur->cur_len) 1178 error = xfs_alloc_cur_check(args, acur, cur, 1179 &i); 1180 } 1181 if (error) 1182 return error; 1183 } 1184 1185 /* 1186 * Increment the search key until we find at least one allocation 1187 * candidate or if the extent we found was larger. Otherwise, double the 1188 * search key to optimize the search. Efficiency is more important here 1189 * than absolute best locality. 1190 */ 1191 cur_len <<= 1; 1192 if (!acur->len || acur->cur_len >= cur_len) 1193 acur->cur_len++; 1194 else 1195 acur->cur_len = cur_len; 1196 1197 return error; 1198 } 1199 1200 /* 1201 * Deal with the case where only small freespaces remain. Either return the 1202 * contents of the last freespace record, or allocate space from the freelist if 1203 * there is nothing in the tree. 1204 */ 1205 STATIC int /* error */ 1206 xfs_alloc_ag_vextent_small( 1207 struct xfs_alloc_arg *args, /* allocation argument structure */ 1208 struct xfs_btree_cur *ccur, /* optional by-size cursor */ 1209 xfs_agblock_t *fbnop, /* result block number */ 1210 xfs_extlen_t *flenp, /* result length */ 1211 int *stat) /* status: 0-freelist, 1-normal/none */ 1212 { 1213 struct xfs_agf *agf = args->agbp->b_addr; 1214 int error = 0; 1215 xfs_agblock_t fbno = NULLAGBLOCK; 1216 xfs_extlen_t flen = 0; 1217 int i = 0; 1218 1219 /* 1220 * If a cntbt cursor is provided, try to allocate the largest record in 1221 * the tree. Try the AGFL if the cntbt is empty, otherwise fail the 1222 * allocation. Make sure to respect minleft even when pulling from the 1223 * freelist. 1224 */ 1225 if (ccur) 1226 error = xfs_btree_decrement(ccur, 0, &i); 1227 if (error) 1228 goto error; 1229 if (i) { 1230 error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i); 1231 if (error) 1232 goto error; 1233 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1234 xfs_btree_mark_sick(ccur); 1235 error = -EFSCORRUPTED; 1236 goto error; 1237 } 1238 goto out; 1239 } 1240 1241 if (args->minlen != 1 || args->alignment != 1 || 1242 args->resv == XFS_AG_RESV_AGFL || 1243 be32_to_cpu(agf->agf_flcount) <= args->minleft) 1244 goto out; 1245 1246 error = xfs_alloc_get_freelist(args->pag, args->tp, args->agbp, 1247 &fbno, 0); 1248 if (error) 1249 goto error; 1250 if (fbno == NULLAGBLOCK) 1251 goto out; 1252 1253 xfs_extent_busy_reuse(pag_group(args->pag), fbno, 1, 1254 (args->datatype & XFS_ALLOC_NOBUSY)); 1255 1256 if (args->datatype & XFS_ALLOC_USERDATA) { 1257 struct xfs_buf *bp; 1258 1259 error = xfs_trans_get_buf(args->tp, args->mp->m_ddev_targp, 1260 xfs_agbno_to_daddr(args->pag, fbno), 1261 args->mp->m_bsize, 0, &bp); 1262 if (error) 1263 goto error; 1264 xfs_trans_binval(args->tp, bp); 1265 } 1266 *fbnop = args->agbno = fbno; 1267 *flenp = args->len = 1; 1268 if (XFS_IS_CORRUPT(args->mp, fbno >= be32_to_cpu(agf->agf_length))) { 1269 xfs_btree_mark_sick(ccur); 1270 error = -EFSCORRUPTED; 1271 goto error; 1272 } 1273 args->wasfromfl = 1; 1274 trace_xfs_alloc_small_freelist(args); 1275 1276 /* 1277 * If we're feeding an AGFL block to something that doesn't live in the 1278 * free space, we need to clear out the OWN_AG rmap. 1279 */ 1280 error = xfs_rmap_free(args->tp, args->agbp, args->pag, fbno, 1, 1281 &XFS_RMAP_OINFO_AG); 1282 if (error) 1283 goto error; 1284 1285 *stat = 0; 1286 return 0; 1287 1288 out: 1289 /* 1290 * Can't do the allocation, give up. 1291 */ 1292 if (flen < args->minlen) { 1293 args->agbno = NULLAGBLOCK; 1294 trace_xfs_alloc_small_notenough(args); 1295 flen = 0; 1296 } 1297 *fbnop = fbno; 1298 *flenp = flen; 1299 *stat = 1; 1300 trace_xfs_alloc_small_done(args); 1301 return 0; 1302 1303 error: 1304 trace_xfs_alloc_small_error(args); 1305 return error; 1306 } 1307 1308 /* 1309 * Allocate a variable extent at exactly agno/bno. 1310 * Extent's length (returned in *len) will be between minlen and maxlen, 1311 * and of the form k * prod + mod unless there's nothing that large. 1312 * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it. 1313 */ 1314 STATIC int /* error */ 1315 xfs_alloc_ag_vextent_exact( 1316 xfs_alloc_arg_t *args) /* allocation argument structure */ 1317 { 1318 struct xfs_btree_cur *bno_cur;/* by block-number btree cursor */ 1319 struct xfs_btree_cur *cnt_cur;/* by count btree cursor */ 1320 int error; 1321 xfs_agblock_t fbno; /* start block of found extent */ 1322 xfs_extlen_t flen; /* length of found extent */ 1323 xfs_agblock_t tbno; /* start block of busy extent */ 1324 xfs_extlen_t tlen; /* length of busy extent */ 1325 xfs_agblock_t tend; /* end block of busy extent */ 1326 int i; /* success/failure of operation */ 1327 unsigned busy_gen; 1328 1329 ASSERT(args->alignment == 1); 1330 1331 /* 1332 * Allocate/initialize a cursor for the by-number freespace btree. 1333 */ 1334 bno_cur = xfs_bnobt_init_cursor(args->mp, args->tp, args->agbp, 1335 args->pag); 1336 1337 /* 1338 * Lookup bno and minlen in the btree (minlen is irrelevant, really). 1339 * Look for the closest free block <= bno, it must contain bno 1340 * if any free block does. 1341 */ 1342 error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i); 1343 if (error) 1344 goto error0; 1345 if (!i) 1346 goto not_found; 1347 1348 /* 1349 * Grab the freespace record. 1350 */ 1351 error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i); 1352 if (error) 1353 goto error0; 1354 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1355 xfs_btree_mark_sick(bno_cur); 1356 error = -EFSCORRUPTED; 1357 goto error0; 1358 } 1359 ASSERT(fbno <= args->agbno); 1360 1361 /* 1362 * Check for overlapping busy extents. 1363 */ 1364 tbno = fbno; 1365 tlen = flen; 1366 xfs_extent_busy_trim(pag_group(args->pag), args->minlen, args->maxlen, 1367 &tbno, &tlen, &busy_gen); 1368 1369 /* 1370 * Give up if the start of the extent is busy, or the freespace isn't 1371 * long enough for the minimum request. 1372 */ 1373 if (tbno > args->agbno) 1374 goto not_found; 1375 if (tlen < args->minlen) 1376 goto not_found; 1377 tend = tbno + tlen; 1378 if (tend < args->agbno + args->minlen) 1379 goto not_found; 1380 1381 /* 1382 * End of extent will be smaller of the freespace end and the 1383 * maximal requested end. 1384 * 1385 * Fix the length according to mod and prod if given. 1386 */ 1387 args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen) 1388 - args->agbno; 1389 xfs_alloc_fix_len(args); 1390 ASSERT(args->agbno + args->len <= tend); 1391 1392 /* 1393 * We are allocating agbno for args->len 1394 * Allocate/initialize a cursor for the by-size btree. 1395 */ 1396 cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, args->agbp, 1397 args->pag); 1398 ASSERT(xfs_verify_agbext(args->pag, args->agbno, args->len)); 1399 error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno, 1400 args->len, XFSA_FIXUP_BNO_OK); 1401 if (error) { 1402 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); 1403 goto error0; 1404 } 1405 1406 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); 1407 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 1408 1409 args->wasfromfl = 0; 1410 trace_xfs_alloc_exact_done(args); 1411 return 0; 1412 1413 not_found: 1414 /* Didn't find it, return null. */ 1415 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); 1416 args->agbno = NULLAGBLOCK; 1417 trace_xfs_alloc_exact_notfound(args); 1418 return 0; 1419 1420 error0: 1421 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR); 1422 trace_xfs_alloc_exact_error(args); 1423 return error; 1424 } 1425 1426 /* 1427 * Search a given number of btree records in a given direction. Check each 1428 * record against the good extent we've already found. 1429 */ 1430 STATIC int 1431 xfs_alloc_walk_iter( 1432 struct xfs_alloc_arg *args, 1433 struct xfs_alloc_cur *acur, 1434 struct xfs_btree_cur *cur, 1435 bool increment, 1436 bool find_one, /* quit on first candidate */ 1437 int count, /* rec count (-1 for infinite) */ 1438 int *stat) 1439 { 1440 int error; 1441 int i; 1442 1443 *stat = 0; 1444 1445 /* 1446 * Search so long as the cursor is active or we find a better extent. 1447 * The cursor is deactivated if it extends beyond the range of the 1448 * current allocation candidate. 1449 */ 1450 while (xfs_alloc_cur_active(cur) && count) { 1451 error = xfs_alloc_cur_check(args, acur, cur, &i); 1452 if (error) 1453 return error; 1454 if (i == 1) { 1455 *stat = 1; 1456 if (find_one) 1457 break; 1458 } 1459 if (!xfs_alloc_cur_active(cur)) 1460 break; 1461 1462 if (increment) 1463 error = xfs_btree_increment(cur, 0, &i); 1464 else 1465 error = xfs_btree_decrement(cur, 0, &i); 1466 if (error) 1467 return error; 1468 if (i == 0) 1469 cur->bc_flags &= ~XFS_BTREE_ALLOCBT_ACTIVE; 1470 1471 if (count > 0) 1472 count--; 1473 } 1474 1475 return 0; 1476 } 1477 1478 /* 1479 * Search the by-bno and by-size btrees in parallel in search of an extent with 1480 * ideal locality based on the NEAR mode ->agbno locality hint. 1481 */ 1482 STATIC int 1483 xfs_alloc_ag_vextent_locality( 1484 struct xfs_alloc_arg *args, 1485 struct xfs_alloc_cur *acur, 1486 int *stat) 1487 { 1488 struct xfs_btree_cur *fbcur = NULL; 1489 int error; 1490 int i; 1491 bool fbinc; 1492 1493 ASSERT(acur->len == 0); 1494 1495 *stat = 0; 1496 1497 error = xfs_alloc_lookup_ge(acur->cnt, args->agbno, acur->cur_len, &i); 1498 if (error) 1499 return error; 1500 error = xfs_alloc_lookup_le(acur->bnolt, args->agbno, 0, &i); 1501 if (error) 1502 return error; 1503 error = xfs_alloc_lookup_ge(acur->bnogt, args->agbno, 0, &i); 1504 if (error) 1505 return error; 1506 1507 /* 1508 * Search the bnobt and cntbt in parallel. Search the bnobt left and 1509 * right and lookup the closest extent to the locality hint for each 1510 * extent size key in the cntbt. The entire search terminates 1511 * immediately on a bnobt hit because that means we've found best case 1512 * locality. Otherwise the search continues until the cntbt cursor runs 1513 * off the end of the tree. If no allocation candidate is found at this 1514 * point, give up on locality, walk backwards from the end of the cntbt 1515 * and take the first available extent. 1516 * 1517 * The parallel tree searches balance each other out to provide fairly 1518 * consistent performance for various situations. The bnobt search can 1519 * have pathological behavior in the worst case scenario of larger 1520 * allocation requests and fragmented free space. On the other hand, the 1521 * bnobt is able to satisfy most smaller allocation requests much more 1522 * quickly than the cntbt. The cntbt search can sift through fragmented 1523 * free space and sets of free extents for larger allocation requests 1524 * more quickly than the bnobt. Since the locality hint is just a hint 1525 * and we don't want to scan the entire bnobt for perfect locality, the 1526 * cntbt search essentially bounds the bnobt search such that we can 1527 * find good enough locality at reasonable performance in most cases. 1528 */ 1529 while (xfs_alloc_cur_active(acur->bnolt) || 1530 xfs_alloc_cur_active(acur->bnogt) || 1531 xfs_alloc_cur_active(acur->cnt)) { 1532 1533 trace_xfs_alloc_cur_lookup(args); 1534 1535 /* 1536 * Search the bnobt left and right. In the case of a hit, finish 1537 * the search in the opposite direction and we're done. 1538 */ 1539 error = xfs_alloc_walk_iter(args, acur, acur->bnolt, false, 1540 true, 1, &i); 1541 if (error) 1542 return error; 1543 if (i == 1) { 1544 trace_xfs_alloc_cur_left(args); 1545 fbcur = acur->bnogt; 1546 fbinc = true; 1547 break; 1548 } 1549 error = xfs_alloc_walk_iter(args, acur, acur->bnogt, true, true, 1550 1, &i); 1551 if (error) 1552 return error; 1553 if (i == 1) { 1554 trace_xfs_alloc_cur_right(args); 1555 fbcur = acur->bnolt; 1556 fbinc = false; 1557 break; 1558 } 1559 1560 /* 1561 * Check the extent with best locality based on the current 1562 * extent size search key and keep track of the best candidate. 1563 */ 1564 error = xfs_alloc_cntbt_iter(args, acur); 1565 if (error) 1566 return error; 1567 if (!xfs_alloc_cur_active(acur->cnt)) { 1568 trace_xfs_alloc_cur_lookup_done(args); 1569 break; 1570 } 1571 } 1572 1573 /* 1574 * If we failed to find anything due to busy extents, return empty 1575 * handed so the caller can flush and retry. If no busy extents were 1576 * found, walk backwards from the end of the cntbt as a last resort. 1577 */ 1578 if (!xfs_alloc_cur_active(acur->cnt) && !acur->len && !acur->busy) { 1579 error = xfs_btree_decrement(acur->cnt, 0, &i); 1580 if (error) 1581 return error; 1582 if (i) { 1583 acur->cnt->bc_flags |= XFS_BTREE_ALLOCBT_ACTIVE; 1584 fbcur = acur->cnt; 1585 fbinc = false; 1586 } 1587 } 1588 1589 /* 1590 * Search in the opposite direction for a better entry in the case of 1591 * a bnobt hit or walk backwards from the end of the cntbt. 1592 */ 1593 if (fbcur) { 1594 error = xfs_alloc_walk_iter(args, acur, fbcur, fbinc, true, -1, 1595 &i); 1596 if (error) 1597 return error; 1598 } 1599 1600 if (acur->len) 1601 *stat = 1; 1602 1603 return 0; 1604 } 1605 1606 /* Check the last block of the cnt btree for allocations. */ 1607 static int 1608 xfs_alloc_ag_vextent_lastblock( 1609 struct xfs_alloc_arg *args, 1610 struct xfs_alloc_cur *acur, 1611 xfs_agblock_t *bno, 1612 xfs_extlen_t *len, 1613 bool *allocated) 1614 { 1615 int error; 1616 int i; 1617 1618 #ifdef DEBUG 1619 /* Randomly don't execute the first algorithm. */ 1620 if (get_random_u32_below(2)) 1621 return 0; 1622 #endif 1623 1624 /* 1625 * Start from the entry that lookup found, sequence through all larger 1626 * free blocks. If we're actually pointing at a record smaller than 1627 * maxlen, go to the start of this block, and skip all those smaller 1628 * than minlen. 1629 */ 1630 if (*len || args->alignment > 1) { 1631 acur->cnt->bc_levels[0].ptr = 1; 1632 do { 1633 error = xfs_alloc_get_rec(acur->cnt, bno, len, &i); 1634 if (error) 1635 return error; 1636 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1637 xfs_btree_mark_sick(acur->cnt); 1638 return -EFSCORRUPTED; 1639 } 1640 if (*len >= args->minlen) 1641 break; 1642 error = xfs_btree_increment(acur->cnt, 0, &i); 1643 if (error) 1644 return error; 1645 } while (i); 1646 ASSERT(*len >= args->minlen); 1647 if (!i) 1648 return 0; 1649 } 1650 1651 error = xfs_alloc_walk_iter(args, acur, acur->cnt, true, false, -1, &i); 1652 if (error) 1653 return error; 1654 1655 /* 1656 * It didn't work. We COULD be in a case where there's a good record 1657 * somewhere, so try again. 1658 */ 1659 if (acur->len == 0) 1660 return 0; 1661 1662 trace_xfs_alloc_near_first(args); 1663 *allocated = true; 1664 return 0; 1665 } 1666 1667 /* 1668 * Allocate a variable extent near bno in the allocation group agno. 1669 * Extent's length (returned in len) will be between minlen and maxlen, 1670 * and of the form k * prod + mod unless there's nothing that large. 1671 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it. 1672 */ 1673 STATIC int 1674 xfs_alloc_ag_vextent_near( 1675 struct xfs_alloc_arg *args, 1676 uint32_t alloc_flags) 1677 { 1678 struct xfs_alloc_cur acur = {}; 1679 int error; /* error code */ 1680 int i; /* result code, temporary */ 1681 xfs_agblock_t bno; 1682 xfs_extlen_t len; 1683 1684 /* handle uninitialized agbno range so caller doesn't have to */ 1685 if (!args->min_agbno && !args->max_agbno) 1686 args->max_agbno = args->mp->m_sb.sb_agblocks - 1; 1687 ASSERT(args->min_agbno <= args->max_agbno); 1688 1689 /* clamp agbno to the range if it's outside */ 1690 if (args->agbno < args->min_agbno) 1691 args->agbno = args->min_agbno; 1692 if (args->agbno > args->max_agbno) 1693 args->agbno = args->max_agbno; 1694 1695 /* Retry once quickly if we find busy extents before blocking. */ 1696 alloc_flags |= XFS_ALLOC_FLAG_TRYFLUSH; 1697 restart: 1698 len = 0; 1699 1700 /* 1701 * Set up cursors and see if there are any free extents as big as 1702 * maxlen. If not, pick the last entry in the tree unless the tree is 1703 * empty. 1704 */ 1705 error = xfs_alloc_cur_setup(args, &acur); 1706 if (error == -ENOSPC) { 1707 error = xfs_alloc_ag_vextent_small(args, acur.cnt, &bno, 1708 &len, &i); 1709 if (error) 1710 goto out; 1711 if (i == 0 || len == 0) { 1712 trace_xfs_alloc_near_noentry(args); 1713 goto out; 1714 } 1715 ASSERT(i == 1); 1716 } else if (error) { 1717 goto out; 1718 } 1719 1720 /* 1721 * First algorithm. 1722 * If the requested extent is large wrt the freespaces available 1723 * in this a.g., then the cursor will be pointing to a btree entry 1724 * near the right edge of the tree. If it's in the last btree leaf 1725 * block, then we just examine all the entries in that block 1726 * that are big enough, and pick the best one. 1727 */ 1728 if (xfs_btree_islastblock(acur.cnt, 0)) { 1729 bool allocated = false; 1730 1731 error = xfs_alloc_ag_vextent_lastblock(args, &acur, &bno, &len, 1732 &allocated); 1733 if (error) 1734 goto out; 1735 if (allocated) 1736 goto alloc_finish; 1737 } 1738 1739 /* 1740 * Second algorithm. Combined cntbt and bnobt search to find ideal 1741 * locality. 1742 */ 1743 error = xfs_alloc_ag_vextent_locality(args, &acur, &i); 1744 if (error) 1745 goto out; 1746 1747 /* 1748 * If we couldn't get anything, give up. 1749 */ 1750 if (!acur.len) { 1751 if (acur.busy) { 1752 /* 1753 * Our only valid extents must have been busy. Flush and 1754 * retry the allocation again. If we get an -EAGAIN 1755 * error, we're being told that a deadlock was avoided 1756 * and the current transaction needs committing before 1757 * the allocation can be retried. 1758 */ 1759 trace_xfs_alloc_near_busy(args); 1760 error = xfs_extent_busy_flush(args->tp, 1761 pag_group(args->pag), acur.busy_gen, 1762 alloc_flags); 1763 if (error) 1764 goto out; 1765 1766 alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH; 1767 goto restart; 1768 } 1769 trace_xfs_alloc_size_neither(args); 1770 args->agbno = NULLAGBLOCK; 1771 goto out; 1772 } 1773 1774 alloc_finish: 1775 /* fix up btrees on a successful allocation */ 1776 error = xfs_alloc_cur_finish(args, &acur); 1777 1778 out: 1779 xfs_alloc_cur_close(&acur, error); 1780 return error; 1781 } 1782 1783 /* 1784 * Allocate a variable extent anywhere in the allocation group agno. 1785 * Extent's length (returned in len) will be between minlen and maxlen, 1786 * and of the form k * prod + mod unless there's nothing that large. 1787 * Return the starting a.g. block, or NULLAGBLOCK if we can't do it. 1788 */ 1789 static int 1790 xfs_alloc_ag_vextent_size( 1791 struct xfs_alloc_arg *args, 1792 uint32_t alloc_flags) 1793 { 1794 struct xfs_agf *agf = args->agbp->b_addr; 1795 struct xfs_btree_cur *bno_cur; 1796 struct xfs_btree_cur *cnt_cur; 1797 xfs_agblock_t fbno; /* start of found freespace */ 1798 xfs_extlen_t flen; /* length of found freespace */ 1799 xfs_agblock_t rbno; /* returned block number */ 1800 xfs_extlen_t rlen; /* length of returned extent */ 1801 bool busy; 1802 unsigned busy_gen; 1803 int error; 1804 int i; 1805 1806 /* Retry once quickly if we find busy extents before blocking. */ 1807 alloc_flags |= XFS_ALLOC_FLAG_TRYFLUSH; 1808 restart: 1809 /* 1810 * Allocate and initialize a cursor for the by-size btree. 1811 */ 1812 cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, args->agbp, 1813 args->pag); 1814 bno_cur = NULL; 1815 1816 /* 1817 * Look for an entry >= maxlen+alignment-1 blocks. 1818 */ 1819 if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, 1820 args->maxlen + args->alignment - 1, &i))) 1821 goto error0; 1822 1823 /* 1824 * If none then we have to settle for a smaller extent. In the case that 1825 * there are no large extents, this will return the last entry in the 1826 * tree unless the tree is empty. In the case that there are only busy 1827 * large extents, this will return the largest small extent unless there 1828 * are no smaller extents available. 1829 */ 1830 if (!i) { 1831 error = xfs_alloc_ag_vextent_small(args, cnt_cur, 1832 &fbno, &flen, &i); 1833 if (error) 1834 goto error0; 1835 if (i == 0 || flen == 0) { 1836 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 1837 trace_xfs_alloc_size_noentry(args); 1838 return 0; 1839 } 1840 ASSERT(i == 1); 1841 busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno, 1842 &rlen, &busy_gen); 1843 } else { 1844 /* 1845 * Search for a non-busy extent that is large enough. 1846 */ 1847 for (;;) { 1848 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i); 1849 if (error) 1850 goto error0; 1851 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1852 xfs_btree_mark_sick(cnt_cur); 1853 error = -EFSCORRUPTED; 1854 goto error0; 1855 } 1856 1857 busy = xfs_alloc_compute_aligned(args, fbno, flen, 1858 &rbno, &rlen, &busy_gen); 1859 1860 if (rlen >= args->maxlen) 1861 break; 1862 1863 error = xfs_btree_increment(cnt_cur, 0, &i); 1864 if (error) 1865 goto error0; 1866 if (i) 1867 continue; 1868 1869 /* 1870 * Our only valid extents must have been busy. Flush and 1871 * retry the allocation again. If we get an -EAGAIN 1872 * error, we're being told that a deadlock was avoided 1873 * and the current transaction needs committing before 1874 * the allocation can be retried. 1875 */ 1876 trace_xfs_alloc_size_busy(args); 1877 error = xfs_extent_busy_flush(args->tp, 1878 pag_group(args->pag), busy_gen, 1879 alloc_flags); 1880 if (error) 1881 goto error0; 1882 1883 alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH; 1884 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 1885 goto restart; 1886 } 1887 } 1888 1889 /* 1890 * In the first case above, we got the last entry in the 1891 * by-size btree. Now we check to see if the space hits maxlen 1892 * once aligned; if not, we search left for something better. 1893 * This can't happen in the second case above. 1894 */ 1895 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen); 1896 if (XFS_IS_CORRUPT(args->mp, 1897 rlen != 0 && 1898 (rlen > flen || 1899 rbno + rlen > fbno + flen))) { 1900 xfs_btree_mark_sick(cnt_cur); 1901 error = -EFSCORRUPTED; 1902 goto error0; 1903 } 1904 if (rlen < args->maxlen) { 1905 xfs_agblock_t bestfbno; 1906 xfs_extlen_t bestflen; 1907 xfs_agblock_t bestrbno; 1908 xfs_extlen_t bestrlen; 1909 1910 bestrlen = rlen; 1911 bestrbno = rbno; 1912 bestflen = flen; 1913 bestfbno = fbno; 1914 for (;;) { 1915 if ((error = xfs_btree_decrement(cnt_cur, 0, &i))) 1916 goto error0; 1917 if (i == 0) 1918 break; 1919 if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, 1920 &i))) 1921 goto error0; 1922 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1923 xfs_btree_mark_sick(cnt_cur); 1924 error = -EFSCORRUPTED; 1925 goto error0; 1926 } 1927 if (flen <= bestrlen) 1928 break; 1929 busy = xfs_alloc_compute_aligned(args, fbno, flen, 1930 &rbno, &rlen, &busy_gen); 1931 rlen = XFS_EXTLEN_MIN(args->maxlen, rlen); 1932 if (XFS_IS_CORRUPT(args->mp, 1933 rlen != 0 && 1934 (rlen > flen || 1935 rbno + rlen > fbno + flen))) { 1936 xfs_btree_mark_sick(cnt_cur); 1937 error = -EFSCORRUPTED; 1938 goto error0; 1939 } 1940 if (rlen > bestrlen) { 1941 bestrlen = rlen; 1942 bestrbno = rbno; 1943 bestflen = flen; 1944 bestfbno = fbno; 1945 if (rlen == args->maxlen) 1946 break; 1947 } 1948 } 1949 if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen, 1950 &i))) 1951 goto error0; 1952 if (XFS_IS_CORRUPT(args->mp, i != 1)) { 1953 xfs_btree_mark_sick(cnt_cur); 1954 error = -EFSCORRUPTED; 1955 goto error0; 1956 } 1957 rlen = bestrlen; 1958 rbno = bestrbno; 1959 flen = bestflen; 1960 fbno = bestfbno; 1961 } 1962 args->wasfromfl = 0; 1963 /* 1964 * Fix up the length. 1965 */ 1966 args->len = rlen; 1967 if (rlen < args->minlen) { 1968 if (busy) { 1969 /* 1970 * Our only valid extents must have been busy. Flush and 1971 * retry the allocation again. If we get an -EAGAIN 1972 * error, we're being told that a deadlock was avoided 1973 * and the current transaction needs committing before 1974 * the allocation can be retried. 1975 */ 1976 trace_xfs_alloc_size_busy(args); 1977 error = xfs_extent_busy_flush(args->tp, 1978 pag_group(args->pag), busy_gen, 1979 alloc_flags); 1980 if (error) 1981 goto error0; 1982 1983 alloc_flags &= ~XFS_ALLOC_FLAG_TRYFLUSH; 1984 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 1985 goto restart; 1986 } 1987 goto out_nominleft; 1988 } 1989 xfs_alloc_fix_len(args); 1990 1991 rlen = args->len; 1992 if (XFS_IS_CORRUPT(args->mp, rlen > flen)) { 1993 xfs_btree_mark_sick(cnt_cur); 1994 error = -EFSCORRUPTED; 1995 goto error0; 1996 } 1997 /* 1998 * Allocate and initialize a cursor for the by-block tree. 1999 */ 2000 bno_cur = xfs_bnobt_init_cursor(args->mp, args->tp, args->agbp, 2001 args->pag); 2002 if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, 2003 rbno, rlen, XFSA_FIXUP_CNT_OK))) 2004 goto error0; 2005 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 2006 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); 2007 cnt_cur = bno_cur = NULL; 2008 args->len = rlen; 2009 args->agbno = rbno; 2010 if (XFS_IS_CORRUPT(args->mp, 2011 args->agbno + args->len > 2012 be32_to_cpu(agf->agf_length))) { 2013 xfs_ag_mark_sick(args->pag, XFS_SICK_AG_BNOBT); 2014 error = -EFSCORRUPTED; 2015 goto error0; 2016 } 2017 trace_xfs_alloc_size_done(args); 2018 return 0; 2019 2020 error0: 2021 trace_xfs_alloc_size_error(args); 2022 if (cnt_cur) 2023 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); 2024 if (bno_cur) 2025 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR); 2026 return error; 2027 2028 out_nominleft: 2029 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 2030 trace_xfs_alloc_size_nominleft(args); 2031 args->agbno = NULLAGBLOCK; 2032 return 0; 2033 } 2034 2035 /* 2036 * Free the extent starting at agno/bno for length. 2037 */ 2038 int 2039 xfs_free_ag_extent( 2040 struct xfs_trans *tp, 2041 struct xfs_buf *agbp, 2042 xfs_agblock_t bno, 2043 xfs_extlen_t len, 2044 const struct xfs_owner_info *oinfo, 2045 enum xfs_ag_resv_type type) 2046 { 2047 struct xfs_mount *mp; 2048 struct xfs_btree_cur *bno_cur; 2049 struct xfs_btree_cur *cnt_cur; 2050 xfs_agblock_t gtbno; /* start of right neighbor */ 2051 xfs_extlen_t gtlen; /* length of right neighbor */ 2052 xfs_agblock_t ltbno; /* start of left neighbor */ 2053 xfs_extlen_t ltlen; /* length of left neighbor */ 2054 xfs_agblock_t nbno; /* new starting block of freesp */ 2055 xfs_extlen_t nlen; /* new length of freespace */ 2056 int haveleft; /* have a left neighbor */ 2057 int haveright; /* have a right neighbor */ 2058 int i; 2059 int error; 2060 struct xfs_perag *pag = agbp->b_pag; 2061 bool fixup_longest = false; 2062 2063 bno_cur = cnt_cur = NULL; 2064 mp = tp->t_mountp; 2065 2066 if (!xfs_rmap_should_skip_owner_update(oinfo)) { 2067 error = xfs_rmap_free(tp, agbp, pag, bno, len, oinfo); 2068 if (error) 2069 goto error0; 2070 } 2071 2072 /* 2073 * Allocate and initialize a cursor for the by-block btree. 2074 */ 2075 bno_cur = xfs_bnobt_init_cursor(mp, tp, agbp, pag); 2076 /* 2077 * Look for a neighboring block on the left (lower block numbers) 2078 * that is contiguous with this space. 2079 */ 2080 if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft))) 2081 goto error0; 2082 if (haveleft) { 2083 /* 2084 * There is a block to our left. 2085 */ 2086 if ((error = xfs_alloc_get_rec(bno_cur, <bno, <len, &i))) 2087 goto error0; 2088 if (XFS_IS_CORRUPT(mp, i != 1)) { 2089 xfs_btree_mark_sick(bno_cur); 2090 error = -EFSCORRUPTED; 2091 goto error0; 2092 } 2093 /* 2094 * It's not contiguous, though. 2095 */ 2096 if (ltbno + ltlen < bno) 2097 haveleft = 0; 2098 else { 2099 /* 2100 * If this failure happens the request to free this 2101 * space was invalid, it's (partly) already free. 2102 * Very bad. 2103 */ 2104 if (XFS_IS_CORRUPT(mp, ltbno + ltlen > bno)) { 2105 xfs_btree_mark_sick(bno_cur); 2106 error = -EFSCORRUPTED; 2107 goto error0; 2108 } 2109 } 2110 } 2111 /* 2112 * Look for a neighboring block on the right (higher block numbers) 2113 * that is contiguous with this space. 2114 */ 2115 if ((error = xfs_btree_increment(bno_cur, 0, &haveright))) 2116 goto error0; 2117 if (haveright) { 2118 /* 2119 * There is a block to our right. 2120 */ 2121 if ((error = xfs_alloc_get_rec(bno_cur, >bno, >len, &i))) 2122 goto error0; 2123 if (XFS_IS_CORRUPT(mp, i != 1)) { 2124 xfs_btree_mark_sick(bno_cur); 2125 error = -EFSCORRUPTED; 2126 goto error0; 2127 } 2128 /* 2129 * It's not contiguous, though. 2130 */ 2131 if (bno + len < gtbno) 2132 haveright = 0; 2133 else { 2134 /* 2135 * If this failure happens the request to free this 2136 * space was invalid, it's (partly) already free. 2137 * Very bad. 2138 */ 2139 if (XFS_IS_CORRUPT(mp, bno + len > gtbno)) { 2140 xfs_btree_mark_sick(bno_cur); 2141 error = -EFSCORRUPTED; 2142 goto error0; 2143 } 2144 } 2145 } 2146 /* 2147 * Now allocate and initialize a cursor for the by-size tree. 2148 */ 2149 cnt_cur = xfs_cntbt_init_cursor(mp, tp, agbp, pag); 2150 /* 2151 * Have both left and right contiguous neighbors. 2152 * Merge all three into a single free block. 2153 */ 2154 if (haveleft && haveright) { 2155 /* 2156 * Delete the old by-size entry on the left. 2157 */ 2158 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i))) 2159 goto error0; 2160 if (XFS_IS_CORRUPT(mp, i != 1)) { 2161 xfs_btree_mark_sick(cnt_cur); 2162 error = -EFSCORRUPTED; 2163 goto error0; 2164 } 2165 if ((error = xfs_btree_delete(cnt_cur, &i))) 2166 goto error0; 2167 if (XFS_IS_CORRUPT(mp, i != 1)) { 2168 xfs_btree_mark_sick(cnt_cur); 2169 error = -EFSCORRUPTED; 2170 goto error0; 2171 } 2172 /* 2173 * Delete the old by-size entry on the right. 2174 */ 2175 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i))) 2176 goto error0; 2177 if (XFS_IS_CORRUPT(mp, i != 1)) { 2178 xfs_btree_mark_sick(cnt_cur); 2179 error = -EFSCORRUPTED; 2180 goto error0; 2181 } 2182 if ((error = xfs_btree_delete(cnt_cur, &i))) 2183 goto error0; 2184 if (XFS_IS_CORRUPT(mp, i != 1)) { 2185 xfs_btree_mark_sick(cnt_cur); 2186 error = -EFSCORRUPTED; 2187 goto error0; 2188 } 2189 /* 2190 * Delete the old by-block entry for the right block. 2191 */ 2192 if ((error = xfs_btree_delete(bno_cur, &i))) 2193 goto error0; 2194 if (XFS_IS_CORRUPT(mp, i != 1)) { 2195 xfs_btree_mark_sick(bno_cur); 2196 error = -EFSCORRUPTED; 2197 goto error0; 2198 } 2199 /* 2200 * Move the by-block cursor back to the left neighbor. 2201 */ 2202 if ((error = xfs_btree_decrement(bno_cur, 0, &i))) 2203 goto error0; 2204 if (XFS_IS_CORRUPT(mp, i != 1)) { 2205 xfs_btree_mark_sick(bno_cur); 2206 error = -EFSCORRUPTED; 2207 goto error0; 2208 } 2209 #ifdef DEBUG 2210 /* 2211 * Check that this is the right record: delete didn't 2212 * mangle the cursor. 2213 */ 2214 { 2215 xfs_agblock_t xxbno; 2216 xfs_extlen_t xxlen; 2217 2218 if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen, 2219 &i))) 2220 goto error0; 2221 if (XFS_IS_CORRUPT(mp, 2222 i != 1 || 2223 xxbno != ltbno || 2224 xxlen != ltlen)) { 2225 xfs_btree_mark_sick(bno_cur); 2226 error = -EFSCORRUPTED; 2227 goto error0; 2228 } 2229 } 2230 #endif 2231 /* 2232 * Update remaining by-block entry to the new, joined block. 2233 */ 2234 nbno = ltbno; 2235 nlen = len + ltlen + gtlen; 2236 if ((error = xfs_alloc_update(bno_cur, nbno, nlen))) 2237 goto error0; 2238 } 2239 /* 2240 * Have only a left contiguous neighbor. 2241 * Merge it together with the new freespace. 2242 */ 2243 else if (haveleft) { 2244 /* 2245 * Delete the old by-size entry on the left. 2246 */ 2247 if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i))) 2248 goto error0; 2249 if (XFS_IS_CORRUPT(mp, i != 1)) { 2250 xfs_btree_mark_sick(cnt_cur); 2251 error = -EFSCORRUPTED; 2252 goto error0; 2253 } 2254 if ((error = xfs_btree_delete(cnt_cur, &i))) 2255 goto error0; 2256 if (XFS_IS_CORRUPT(mp, i != 1)) { 2257 xfs_btree_mark_sick(cnt_cur); 2258 error = -EFSCORRUPTED; 2259 goto error0; 2260 } 2261 /* 2262 * Back up the by-block cursor to the left neighbor, and 2263 * update its length. 2264 */ 2265 if ((error = xfs_btree_decrement(bno_cur, 0, &i))) 2266 goto error0; 2267 if (XFS_IS_CORRUPT(mp, i != 1)) { 2268 xfs_btree_mark_sick(bno_cur); 2269 error = -EFSCORRUPTED; 2270 goto error0; 2271 } 2272 nbno = ltbno; 2273 nlen = len + ltlen; 2274 if ((error = xfs_alloc_update(bno_cur, nbno, nlen))) 2275 goto error0; 2276 } 2277 /* 2278 * Have only a right contiguous neighbor. 2279 * Merge it together with the new freespace. 2280 */ 2281 else if (haveright) { 2282 /* 2283 * Delete the old by-size entry on the right. 2284 */ 2285 if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i))) 2286 goto error0; 2287 if (XFS_IS_CORRUPT(mp, i != 1)) { 2288 xfs_btree_mark_sick(cnt_cur); 2289 error = -EFSCORRUPTED; 2290 goto error0; 2291 } 2292 if ((error = xfs_btree_delete(cnt_cur, &i))) 2293 goto error0; 2294 if (XFS_IS_CORRUPT(mp, i != 1)) { 2295 xfs_btree_mark_sick(cnt_cur); 2296 error = -EFSCORRUPTED; 2297 goto error0; 2298 } 2299 /* 2300 * Update the starting block and length of the right 2301 * neighbor in the by-block tree. 2302 */ 2303 nbno = bno; 2304 nlen = len + gtlen; 2305 if ((error = xfs_alloc_update(bno_cur, nbno, nlen))) 2306 goto error0; 2307 } 2308 /* 2309 * No contiguous neighbors. 2310 * Insert the new freespace into the by-block tree. 2311 */ 2312 else { 2313 nbno = bno; 2314 nlen = len; 2315 if ((error = xfs_btree_insert(bno_cur, &i))) 2316 goto error0; 2317 if (XFS_IS_CORRUPT(mp, i != 1)) { 2318 xfs_btree_mark_sick(bno_cur); 2319 error = -EFSCORRUPTED; 2320 goto error0; 2321 } 2322 } 2323 xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR); 2324 bno_cur = NULL; 2325 2326 /* 2327 * In all cases we need to insert the new freespace in the by-size tree. 2328 * 2329 * If this new freespace is being inserted in the block that contains 2330 * the largest free space in the btree, make sure we also fix up the 2331 * agf->agf-longest tracker field. 2332 */ 2333 if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i))) 2334 goto error0; 2335 if (XFS_IS_CORRUPT(mp, i != 0)) { 2336 xfs_btree_mark_sick(cnt_cur); 2337 error = -EFSCORRUPTED; 2338 goto error0; 2339 } 2340 if (xfs_alloc_cursor_at_lastrec(cnt_cur)) 2341 fixup_longest = true; 2342 if ((error = xfs_btree_insert(cnt_cur, &i))) 2343 goto error0; 2344 if (XFS_IS_CORRUPT(mp, i != 1)) { 2345 xfs_btree_mark_sick(cnt_cur); 2346 error = -EFSCORRUPTED; 2347 goto error0; 2348 } 2349 if (fixup_longest) { 2350 error = xfs_alloc_fixup_longest(cnt_cur); 2351 if (error) 2352 goto error0; 2353 } 2354 2355 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR); 2356 cnt_cur = NULL; 2357 2358 /* 2359 * Update the freespace totals in the ag and superblock. 2360 */ 2361 error = xfs_alloc_update_counters(tp, agbp, len); 2362 xfs_ag_resv_free_extent(pag, type, tp, len); 2363 if (error) 2364 goto error0; 2365 2366 XFS_STATS_INC(mp, xs_freex); 2367 XFS_STATS_ADD(mp, xs_freeb, len); 2368 2369 trace_xfs_free_extent(pag, bno, len, type, haveleft, haveright); 2370 2371 return 0; 2372 2373 error0: 2374 trace_xfs_free_extent(pag, bno, len, type, -1, -1); 2375 if (bno_cur) 2376 xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR); 2377 if (cnt_cur) 2378 xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR); 2379 return error; 2380 } 2381 2382 /* 2383 * Visible (exported) allocation/free functions. 2384 * Some of these are used just by xfs_alloc_btree.c and this file. 2385 */ 2386 2387 /* 2388 * Compute and fill in value of m_alloc_maxlevels. 2389 */ 2390 void 2391 xfs_alloc_compute_maxlevels( 2392 xfs_mount_t *mp) /* file system mount structure */ 2393 { 2394 mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr, 2395 (mp->m_sb.sb_agblocks + 1) / 2); 2396 ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk()); 2397 } 2398 2399 /* 2400 * Find the length of the longest extent in an AG. The @min_agfl and @max_agfl 2401 * parameters specify how much space we're going to need for the AGFL and the 2402 * @reserved parameter tells us how many blocks in this AG are reserved for 2403 * other callers. 2404 */ 2405 xfs_extlen_t 2406 xfs_alloc_longest_free_extent( 2407 struct xfs_perag *pag, 2408 xfs_extlen_t min_agfl, 2409 xfs_extlen_t max_agfl, 2410 xfs_extlen_t reserved) 2411 { 2412 xfs_extlen_t delta = 0; 2413 2414 /* 2415 * If the AGFL needs a recharge, subtract that from the longest extent 2416 * because AGFL refill happens before the alloc. 2417 */ 2418 if (min_agfl > pag->pagf_flcount) 2419 delta = min_agfl - pag->pagf_flcount; 2420 2421 /* 2422 * Extra AGFL blocks beyond the min are reserved by ->minleft during 2423 * allocation. Similar to reserved, these blocks are not available to 2424 * this allocation. Check if we can preserve the combined total without 2425 * the longest extent. If not, deduct the necessary blocks from the 2426 * longest extent. 2427 */ 2428 if (max_agfl > min_agfl) 2429 reserved += max_agfl - min_agfl; 2430 if (pag->pagf_freeblks - pag->pagf_longest < reserved) 2431 delta += reserved - (pag->pagf_freeblks - pag->pagf_longest); 2432 2433 /* 2434 * If the longest extent is long enough to satisfy all the 2435 * reservations and AGFL rules in place, we can return this extent. 2436 */ 2437 if (pag->pagf_longest > delta) 2438 return min_t(xfs_extlen_t, pag_mount(pag)->m_ag_max_usable, 2439 pag->pagf_longest - delta); 2440 2441 /* Otherwise, let the caller try for 1 block if there's space. */ 2442 return pag->pagf_flcount > 0 || pag->pagf_longest > 0; 2443 } 2444 2445 /* 2446 * Compute the minimum length of the AGFL in the given AG. If @pag is NULL, 2447 * return the largest possible minimum length. The base calculation accounts 2448 * for a single full split per btree. @extra_levels adds additional split 2449 * levels to compute the prospective AGFL requirement increase for 2450 * multi-allocation transactions. 2451 */ 2452 static unsigned int 2453 __xfs_alloc_freelist( 2454 struct xfs_mount *mp, 2455 struct xfs_perag *pag, 2456 unsigned int extra_levels) 2457 { 2458 /* AG btrees have at least 1 level. */ 2459 const unsigned int bno_level = pag ? pag->pagf_bno_level : 1; 2460 const unsigned int cnt_level = pag ? pag->pagf_cnt_level : 1; 2461 const unsigned int rmap_level = pag ? pag->pagf_rmap_level : 1; 2462 const unsigned int levels = 1 + extra_levels; 2463 unsigned int min_free; 2464 2465 ASSERT(mp->m_alloc_maxlevels > 0); 2466 2467 /* 2468 * For a btree shorter than the maximum height, the worst case is that 2469 * every level gets split and a new level is added, then while inserting 2470 * another entry to refill the AGFL, every level under the old root gets 2471 * split again. This is: 2472 * 2473 * (full height split reservation) + (AGFL refill split height) 2474 * = (current height + 1) + (current height - 1) 2475 * = (new height) + (new height - 2) 2476 * = 2 * new height - 2 2477 * 2478 * For a btree of maximum height, the worst case is that every level 2479 * under the root gets split, then while inserting another entry to 2480 * refill the AGFL, every level under the root gets split again. This is 2481 * also: 2482 * 2483 * 2 * (current height - 1) 2484 * = 2 * (new height - 1) 2485 * = 2 * new height - 2 2486 */ 2487 2488 /* space needed by-bno freespace btree */ 2489 min_free = min(bno_level + levels, mp->m_alloc_maxlevels) * 2 - 2; 2490 /* space needed by-size freespace btree */ 2491 min_free += min(cnt_level + levels, mp->m_alloc_maxlevels) * 2 - 2; 2492 /* space needed reverse mapping used space btree */ 2493 if (xfs_has_rmapbt(mp)) { 2494 min_free += min(rmap_level + levels, 2495 mp->m_rmap_maxlevels) * 2 - 2; 2496 } 2497 return min_free; 2498 } 2499 2500 /* 2501 * Compute the minimum and maximum length of the AGFL in the given AG. The max 2502 * value in this context refers to the max requirement the AG might see in a 2503 * multi-alloc transaction. If @pag is NULL, return the largest possible values. 2504 */ 2505 void 2506 xfs_alloc_freelist( 2507 struct xfs_mount *mp, 2508 struct xfs_perag *pag, 2509 unsigned int *min_free, 2510 unsigned int *max_free) 2511 { 2512 *min_free = __xfs_alloc_freelist(mp, pag, 0); 2513 if (max_free) 2514 *max_free = __xfs_alloc_freelist(mp, pag, 1); 2515 } 2516 2517 /* 2518 * Check if the operation we are fixing up the freelist for should go ahead or 2519 * not. If we are freeing blocks, we always allow it, otherwise the allocation 2520 * is dependent on whether the size and shape of free space available will 2521 * permit the requested allocation to take place. 2522 */ 2523 static bool 2524 xfs_alloc_space_available( 2525 struct xfs_alloc_arg *args, 2526 xfs_extlen_t min_free, 2527 xfs_extlen_t max_free, 2528 int flags) 2529 { 2530 struct xfs_perag *pag = args->pag; 2531 xfs_extlen_t alloc_len, longest; 2532 xfs_extlen_t reservation; /* blocks that are still reserved */ 2533 int available; 2534 xfs_extlen_t agflcount; 2535 xfs_extlen_t minleft; 2536 2537 if (flags & XFS_ALLOC_FLAG_FREEING) 2538 return true; 2539 2540 reservation = xfs_ag_resv_needed(pag, args->resv); 2541 2542 /* 2543 * minleft implies a multi-alloc transaction. If set, the first alloc 2544 * might cause btree splits that increase the AGFL requirement for the 2545 * next. This worst case requirement is calculated in max_free. 2546 * 2547 * We don't prepopulate the AGFL because we don't know in advance if 2548 * splits will occur. Instead, add the delta to minleft so it is 2549 * accounted for in AG selection. This ensures the AG has enough space 2550 * for the caller's minleft plus that needed to repopulate the AGFL on 2551 * the next alloc if splits do occur. 2552 */ 2553 minleft = args->minleft; 2554 if (minleft) 2555 minleft += max_free - min_free; 2556 2557 /* do we have enough contiguous free space for the allocation? */ 2558 alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop; 2559 longest = xfs_alloc_longest_free_extent(pag, min_free, 2560 minleft ? max_free : min_free, reservation); 2561 if (longest < alloc_len) 2562 return false; 2563 2564 /* 2565 * Do we have enough free space remaining for the allocation? Don't 2566 * account extra agfl blocks because we are about to defer free them, 2567 * making them unavailable until the current transaction commits. 2568 */ 2569 agflcount = min_t(xfs_extlen_t, pag->pagf_flcount, min_free); 2570 available = (int)(pag->pagf_freeblks + agflcount - 2571 reservation - min_free - minleft); 2572 if (available < (int)max(args->total, alloc_len)) 2573 return false; 2574 2575 /* 2576 * Clamp maxlen to the amount of free space available for the actual 2577 * extent allocation. 2578 */ 2579 if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) { 2580 args->maxlen = available; 2581 ASSERT(args->maxlen > 0); 2582 ASSERT(args->maxlen >= args->minlen); 2583 } 2584 2585 return true; 2586 } 2587 2588 /* 2589 * Check the agfl fields of the agf for inconsistency or corruption. 2590 * 2591 * The original purpose was to detect an agfl header padding mismatch between 2592 * current and early v5 kernels. This problem manifests as a 1-slot size 2593 * difference between the on-disk flcount and the active [first, last] range of 2594 * a wrapped agfl. 2595 * 2596 * However, we need to use these same checks to catch agfl count corruptions 2597 * unrelated to padding. This could occur on any v4 or v5 filesystem, so either 2598 * way, we need to reset the agfl and warn the user. 2599 * 2600 * Return true if a reset is required before the agfl can be used, false 2601 * otherwise. 2602 */ 2603 static bool 2604 xfs_agfl_needs_reset( 2605 struct xfs_mount *mp, 2606 struct xfs_agf *agf) 2607 { 2608 uint32_t f = be32_to_cpu(agf->agf_flfirst); 2609 uint32_t l = be32_to_cpu(agf->agf_fllast); 2610 uint32_t c = be32_to_cpu(agf->agf_flcount); 2611 int agfl_size = xfs_agfl_size(mp); 2612 int active; 2613 2614 /* 2615 * The agf read verifier catches severe corruption of these fields. 2616 * Repeat some sanity checks to cover a packed -> unpacked mismatch if 2617 * the verifier allows it. 2618 */ 2619 if (f >= agfl_size || l >= agfl_size) 2620 return true; 2621 if (c > agfl_size) 2622 return true; 2623 2624 /* 2625 * Check consistency between the on-disk count and the active range. An 2626 * agfl padding mismatch manifests as an inconsistent flcount. 2627 */ 2628 if (c && l >= f) 2629 active = l - f + 1; 2630 else if (c) 2631 active = agfl_size - f + l + 1; 2632 else 2633 active = 0; 2634 2635 return active != c; 2636 } 2637 2638 /* 2639 * Reset the agfl to an empty state. Ignore/drop any existing blocks since the 2640 * agfl content cannot be trusted. Warn the user that a repair is required to 2641 * recover leaked blocks. 2642 * 2643 * The purpose of this mechanism is to handle filesystems affected by the agfl 2644 * header padding mismatch problem. A reset keeps the filesystem online with a 2645 * relatively minor free space accounting inconsistency rather than suffer the 2646 * inevitable crash from use of an invalid agfl block. 2647 */ 2648 static void 2649 xfs_agfl_reset( 2650 struct xfs_trans *tp, 2651 struct xfs_buf *agbp, 2652 struct xfs_perag *pag) 2653 { 2654 struct xfs_mount *mp = tp->t_mountp; 2655 struct xfs_agf *agf = agbp->b_addr; 2656 2657 ASSERT(xfs_perag_agfl_needs_reset(pag)); 2658 trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_); 2659 2660 xfs_warn(mp, 2661 "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. " 2662 "Please unmount and run xfs_repair.", 2663 pag_agno(pag), pag->pagf_flcount); 2664 2665 agf->agf_flfirst = 0; 2666 agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1); 2667 agf->agf_flcount = 0; 2668 xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST | 2669 XFS_AGF_FLCOUNT); 2670 2671 pag->pagf_flcount = 0; 2672 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate); 2673 } 2674 2675 /* 2676 * Add the extent to the list of extents to be free at transaction end. 2677 * The list is maintained sorted (by block number). 2678 */ 2679 static int 2680 xfs_defer_extent_free( 2681 struct xfs_trans *tp, 2682 xfs_fsblock_t bno, 2683 xfs_filblks_t len, 2684 const struct xfs_owner_info *oinfo, 2685 enum xfs_ag_resv_type type, 2686 unsigned int free_flags, 2687 struct xfs_defer_pending **dfpp) 2688 { 2689 struct xfs_extent_free_item *xefi; 2690 struct xfs_mount *mp = tp->t_mountp; 2691 2692 ASSERT(len <= XFS_MAX_BMBT_EXTLEN); 2693 ASSERT(!isnullstartblock(bno)); 2694 ASSERT(!(free_flags & ~XFS_FREE_EXTENT_ALL_FLAGS)); 2695 2696 if (free_flags & XFS_FREE_EXTENT_REALTIME) { 2697 if (type != XFS_AG_RESV_NONE) { 2698 ASSERT(type == XFS_AG_RESV_NONE); 2699 return -EFSCORRUPTED; 2700 } 2701 if (XFS_IS_CORRUPT(mp, !xfs_verify_rtbext(mp, bno, len))) 2702 return -EFSCORRUPTED; 2703 } else { 2704 if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbext(mp, bno, len))) 2705 return -EFSCORRUPTED; 2706 } 2707 2708 xefi = kmem_cache_zalloc(xfs_extfree_item_cache, 2709 GFP_KERNEL | __GFP_NOFAIL); 2710 xefi->xefi_startblock = bno; 2711 xefi->xefi_blockcount = (xfs_extlen_t)len; 2712 xefi->xefi_agresv = type; 2713 if (free_flags & XFS_FREE_EXTENT_SKIP_DISCARD) 2714 xefi->xefi_flags |= XFS_EFI_SKIP_DISCARD; 2715 if (free_flags & XFS_FREE_EXTENT_REALTIME) 2716 xefi->xefi_flags |= XFS_EFI_REALTIME; 2717 if (oinfo) { 2718 ASSERT(oinfo->oi_offset == 0); 2719 2720 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK) 2721 xefi->xefi_flags |= XFS_EFI_ATTR_FORK; 2722 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK) 2723 xefi->xefi_flags |= XFS_EFI_BMBT_BLOCK; 2724 xefi->xefi_owner = oinfo->oi_owner; 2725 } else { 2726 xefi->xefi_owner = XFS_RMAP_OWN_NULL; 2727 } 2728 2729 xfs_extent_free_defer_add(tp, xefi, dfpp); 2730 return 0; 2731 } 2732 2733 int 2734 xfs_free_extent_later( 2735 struct xfs_trans *tp, 2736 xfs_fsblock_t bno, 2737 xfs_filblks_t len, 2738 const struct xfs_owner_info *oinfo, 2739 enum xfs_ag_resv_type type, 2740 unsigned int free_flags) 2741 { 2742 struct xfs_defer_pending *dontcare = NULL; 2743 2744 return xfs_defer_extent_free(tp, bno, len, oinfo, type, free_flags, 2745 &dontcare); 2746 } 2747 2748 /* 2749 * Set up automatic freeing of unwritten space in the filesystem. 2750 * 2751 * This function attached a paused deferred extent free item to the 2752 * transaction. Pausing means that the EFI will be logged in the next 2753 * transaction commit, but the pending EFI will not be finished until the 2754 * pending item is unpaused. 2755 * 2756 * If the system goes down after the EFI has been persisted to the log but 2757 * before the pending item is unpaused, log recovery will find the EFI, fail to 2758 * find the EFD, and free the space. 2759 * 2760 * If the pending item is unpaused, the next transaction commit will log an EFD 2761 * without freeing the space. 2762 * 2763 * Caller must ensure that the tp, fsbno, len, oinfo, and resv flags of the 2764 * @args structure are set to the relevant values. 2765 */ 2766 int 2767 xfs_alloc_schedule_autoreap( 2768 const struct xfs_alloc_arg *args, 2769 unsigned int free_flags, 2770 struct xfs_alloc_autoreap *aarp) 2771 { 2772 int error; 2773 2774 error = xfs_defer_extent_free(args->tp, args->fsbno, args->len, 2775 &args->oinfo, args->resv, free_flags, &aarp->dfp); 2776 if (error) 2777 return error; 2778 2779 xfs_defer_item_pause(args->tp, aarp->dfp); 2780 return 0; 2781 } 2782 2783 /* 2784 * Cancel automatic freeing of unwritten space in the filesystem. 2785 * 2786 * Earlier, we created a paused deferred extent free item and attached it to 2787 * this transaction so that we could automatically roll back a new space 2788 * allocation if the system went down. Now we want to cancel the paused work 2789 * item by marking the EFI stale so we don't actually free the space, unpausing 2790 * the pending item and logging an EFD. 2791 * 2792 * The caller generally should have already mapped the space into the ondisk 2793 * filesystem. If the reserved space was partially used, the caller must call 2794 * xfs_free_extent_later to create a new EFI to free the unused space. 2795 */ 2796 void 2797 xfs_alloc_cancel_autoreap( 2798 struct xfs_trans *tp, 2799 struct xfs_alloc_autoreap *aarp) 2800 { 2801 struct xfs_defer_pending *dfp = aarp->dfp; 2802 struct xfs_extent_free_item *xefi; 2803 2804 if (!dfp) 2805 return; 2806 2807 list_for_each_entry(xefi, &dfp->dfp_work, xefi_list) 2808 xefi->xefi_flags |= XFS_EFI_CANCELLED; 2809 2810 xfs_defer_item_unpause(tp, dfp); 2811 } 2812 2813 /* 2814 * Commit automatic freeing of unwritten space in the filesystem. 2815 * 2816 * This unpauses an earlier _schedule_autoreap and commits to freeing the 2817 * allocated space. Call this if none of the reserved space was used. 2818 */ 2819 void 2820 xfs_alloc_commit_autoreap( 2821 struct xfs_trans *tp, 2822 struct xfs_alloc_autoreap *aarp) 2823 { 2824 if (aarp->dfp) 2825 xfs_defer_item_unpause(tp, aarp->dfp); 2826 } 2827 2828 /* 2829 * Check if an AGF has a free extent record whose length is equal to 2830 * args->minlen. 2831 */ 2832 STATIC int 2833 xfs_exact_minlen_extent_available( 2834 struct xfs_alloc_arg *args, 2835 struct xfs_buf *agbp, 2836 int *stat) 2837 { 2838 struct xfs_btree_cur *cnt_cur; 2839 xfs_agblock_t fbno; 2840 xfs_extlen_t flen; 2841 int error = 0; 2842 2843 cnt_cur = xfs_cntbt_init_cursor(args->mp, args->tp, agbp, 2844 args->pag); 2845 error = xfs_alloc_lookup_ge(cnt_cur, 0, args->minlen, stat); 2846 if (error) 2847 goto out; 2848 2849 if (*stat == 0) { 2850 xfs_btree_mark_sick(cnt_cur); 2851 error = -EFSCORRUPTED; 2852 goto out; 2853 } 2854 2855 error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, stat); 2856 if (error) 2857 goto out; 2858 2859 if (*stat == 1 && flen != args->minlen) 2860 *stat = 0; 2861 2862 out: 2863 xfs_btree_del_cursor(cnt_cur, error); 2864 2865 return error; 2866 } 2867 2868 /* 2869 * Decide whether to use this allocation group for this allocation. 2870 * If so, fix up the btree freelist's size. 2871 */ 2872 int /* error */ 2873 xfs_alloc_fix_freelist( 2874 struct xfs_alloc_arg *args, /* allocation argument structure */ 2875 uint32_t alloc_flags) 2876 { 2877 struct xfs_mount *mp = args->mp; 2878 struct xfs_perag *pag = args->pag; 2879 struct xfs_trans *tp = args->tp; 2880 struct xfs_buf *agbp = NULL; 2881 struct xfs_buf *agflbp = NULL; 2882 struct xfs_alloc_arg targs; /* local allocation arguments */ 2883 xfs_agblock_t bno; /* freelist block */ 2884 xfs_extlen_t min_free;/* total blocks needed in freelist */ 2885 xfs_extlen_t max_free; /* max freelist requirement */ 2886 int error = 0; 2887 2888 /* deferred ops (AGFL block frees) require permanent transactions */ 2889 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 2890 2891 if (!xfs_perag_initialised_agf(pag)) { 2892 error = xfs_alloc_read_agf(pag, tp, alloc_flags, &agbp); 2893 if (error) { 2894 /* Couldn't lock the AGF so skip this AG. */ 2895 if (error == -EAGAIN) 2896 error = 0; 2897 goto out_no_agbp; 2898 } 2899 } 2900 2901 /* 2902 * If this is a metadata preferred pag and we are user data then try 2903 * somewhere else if we are not being asked to try harder at this 2904 * point 2905 */ 2906 if (xfs_perag_prefers_metadata(pag) && 2907 (args->datatype & XFS_ALLOC_USERDATA) && 2908 (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK)) { 2909 ASSERT(!(alloc_flags & XFS_ALLOC_FLAG_FREEING)); 2910 goto out_agbp_relse; 2911 } 2912 2913 xfs_alloc_freelist(mp, pag, &min_free, &max_free); 2914 if (!xfs_alloc_space_available(args, min_free, max_free, alloc_flags | 2915 XFS_ALLOC_FLAG_CHECK)) 2916 goto out_agbp_relse; 2917 2918 /* 2919 * Get the a.g. freespace buffer. 2920 * Can fail if we're not blocking on locks, and it's held. 2921 */ 2922 if (!agbp) { 2923 error = xfs_alloc_read_agf(pag, tp, alloc_flags, &agbp); 2924 if (error) { 2925 /* Couldn't lock the AGF so skip this AG. */ 2926 if (error == -EAGAIN) 2927 error = 0; 2928 goto out_no_agbp; 2929 } 2930 } 2931 2932 /* reset a padding mismatched agfl before final free space check */ 2933 if (xfs_perag_agfl_needs_reset(pag)) 2934 xfs_agfl_reset(tp, agbp, pag); 2935 2936 /* If there isn't enough total space or single-extent, reject it. */ 2937 xfs_alloc_freelist(mp, pag, &min_free, &max_free); 2938 if (!xfs_alloc_space_available(args, min_free, max_free, alloc_flags)) 2939 goto out_agbp_relse; 2940 2941 if (IS_ENABLED(CONFIG_XFS_DEBUG) && args->alloc_minlen_only) { 2942 int stat; 2943 2944 error = xfs_exact_minlen_extent_available(args, agbp, &stat); 2945 if (error || !stat) 2946 goto out_agbp_relse; 2947 } 2948 2949 /* 2950 * Make the freelist shorter if it's too long. 2951 * 2952 * Note that from this point onwards, we will always release the agf and 2953 * agfl buffers on error. This handles the case where we error out and 2954 * the buffers are clean or may not have been joined to the transaction 2955 * and hence need to be released manually. If they have been joined to 2956 * the transaction, then xfs_trans_brelse() will handle them 2957 * appropriately based on the recursion count and dirty state of the 2958 * buffer. 2959 * 2960 * XXX (dgc): When we have lots of free space, does this buy us 2961 * anything other than extra overhead when we need to put more blocks 2962 * back on the free list? Maybe we should only do this when space is 2963 * getting low or the AGFL is more than half full? 2964 * 2965 * The NOSHRINK flag prevents the AGFL from being shrunk if it's too 2966 * big; the NORMAP flag prevents AGFL expand/shrink operations from 2967 * updating the rmapbt. Both flags are used in xfs_repair while we're 2968 * rebuilding the rmapbt, and neither are used by the kernel. They're 2969 * both required to ensure that rmaps are correctly recorded for the 2970 * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and 2971 * repair/rmap.c in xfsprogs for details. 2972 */ 2973 memset(&targs, 0, sizeof(targs)); 2974 /* struct copy below */ 2975 if (alloc_flags & XFS_ALLOC_FLAG_NORMAP) 2976 targs.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; 2977 else 2978 targs.oinfo = XFS_RMAP_OINFO_AG; 2979 while (!(alloc_flags & XFS_ALLOC_FLAG_NOSHRINK) && 2980 pag->pagf_flcount > min_free) { 2981 error = xfs_alloc_get_freelist(pag, tp, agbp, &bno, 0); 2982 if (error) 2983 goto out_agbp_relse; 2984 2985 /* 2986 * Defer the AGFL block free. 2987 * 2988 * This helps to prevent log reservation overruns due to too 2989 * many allocation operations in a transaction. AGFL frees are 2990 * prone to this problem because for one they are always freed 2991 * one at a time. Further, an immediate AGFL block free can 2992 * cause a btree join and require another block free before the 2993 * real allocation can proceed. 2994 * Deferring the free disconnects freeing up the AGFL slot from 2995 * freeing the block. 2996 */ 2997 error = xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, bno), 2998 1, &targs.oinfo, XFS_AG_RESV_AGFL, 0); 2999 if (error) 3000 goto out_agbp_relse; 3001 } 3002 3003 targs.tp = tp; 3004 targs.mp = mp; 3005 targs.agbp = agbp; 3006 targs.agno = args->agno; 3007 targs.alignment = targs.minlen = targs.prod = 1; 3008 targs.pag = pag; 3009 error = xfs_alloc_read_agfl(pag, tp, &agflbp); 3010 if (error) 3011 goto out_agbp_relse; 3012 3013 /* Make the freelist longer if it's too short. */ 3014 while (pag->pagf_flcount < min_free) { 3015 targs.agbno = 0; 3016 targs.maxlen = min_free - pag->pagf_flcount; 3017 targs.resv = XFS_AG_RESV_AGFL; 3018 3019 /* Allocate as many blocks as possible at once. */ 3020 error = xfs_alloc_ag_vextent_size(&targs, alloc_flags); 3021 if (error) 3022 goto out_agflbp_relse; 3023 3024 /* 3025 * Stop if we run out. Won't happen if callers are obeying 3026 * the restrictions correctly. Can happen for free calls 3027 * on a completely full ag. 3028 */ 3029 if (targs.agbno == NULLAGBLOCK) { 3030 if (alloc_flags & XFS_ALLOC_FLAG_FREEING) 3031 break; 3032 goto out_agflbp_relse; 3033 } 3034 3035 if (!xfs_rmap_should_skip_owner_update(&targs.oinfo)) { 3036 error = xfs_rmap_alloc(tp, agbp, pag, 3037 targs.agbno, targs.len, &targs.oinfo); 3038 if (error) 3039 goto out_agflbp_relse; 3040 } 3041 error = xfs_alloc_update_counters(tp, agbp, 3042 -((long)(targs.len))); 3043 if (error) 3044 goto out_agflbp_relse; 3045 3046 /* 3047 * Put each allocated block on the list. 3048 */ 3049 for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) { 3050 error = xfs_alloc_put_freelist(pag, tp, agbp, 3051 agflbp, bno, 0); 3052 if (error) 3053 goto out_agflbp_relse; 3054 } 3055 } 3056 xfs_trans_brelse(tp, agflbp); 3057 args->agbp = agbp; 3058 return 0; 3059 3060 out_agflbp_relse: 3061 xfs_trans_brelse(tp, agflbp); 3062 out_agbp_relse: 3063 if (agbp) 3064 xfs_trans_brelse(tp, agbp); 3065 out_no_agbp: 3066 args->agbp = NULL; 3067 return error; 3068 } 3069 3070 /* 3071 * Get a block from the freelist. 3072 * Returns with the buffer for the block gotten. 3073 */ 3074 int 3075 xfs_alloc_get_freelist( 3076 struct xfs_perag *pag, 3077 struct xfs_trans *tp, 3078 struct xfs_buf *agbp, 3079 xfs_agblock_t *bnop, 3080 int btreeblk) 3081 { 3082 struct xfs_agf *agf = agbp->b_addr; 3083 struct xfs_buf *agflbp; 3084 xfs_agblock_t bno; 3085 __be32 *agfl_bno; 3086 int error; 3087 uint32_t logflags; 3088 struct xfs_mount *mp = tp->t_mountp; 3089 3090 /* 3091 * Freelist is empty, give up. 3092 */ 3093 if (!agf->agf_flcount) { 3094 *bnop = NULLAGBLOCK; 3095 return 0; 3096 } 3097 /* 3098 * Read the array of free blocks. 3099 */ 3100 error = xfs_alloc_read_agfl(pag, tp, &agflbp); 3101 if (error) 3102 return error; 3103 3104 3105 /* 3106 * Get the block number and update the data structures. 3107 */ 3108 agfl_bno = xfs_buf_to_agfl_bno(agflbp); 3109 bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]); 3110 if (XFS_IS_CORRUPT(tp->t_mountp, !xfs_verify_agbno(pag, bno))) 3111 return -EFSCORRUPTED; 3112 3113 be32_add_cpu(&agf->agf_flfirst, 1); 3114 xfs_trans_brelse(tp, agflbp); 3115 if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp)) 3116 agf->agf_flfirst = 0; 3117 3118 ASSERT(!xfs_perag_agfl_needs_reset(pag)); 3119 be32_add_cpu(&agf->agf_flcount, -1); 3120 pag->pagf_flcount--; 3121 3122 logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT; 3123 if (btreeblk) { 3124 be32_add_cpu(&agf->agf_btreeblks, 1); 3125 pag->pagf_btreeblks++; 3126 logflags |= XFS_AGF_BTREEBLKS; 3127 } 3128 3129 xfs_alloc_log_agf(tp, agbp, logflags); 3130 *bnop = bno; 3131 3132 return 0; 3133 } 3134 3135 /* 3136 * Log the given fields from the agf structure. 3137 */ 3138 void 3139 xfs_alloc_log_agf( 3140 struct xfs_trans *tp, 3141 struct xfs_buf *bp, 3142 uint32_t fields) 3143 { 3144 int first; /* first byte offset */ 3145 int last; /* last byte offset */ 3146 static const short offsets[] = { 3147 offsetof(xfs_agf_t, agf_magicnum), 3148 offsetof(xfs_agf_t, agf_versionnum), 3149 offsetof(xfs_agf_t, agf_seqno), 3150 offsetof(xfs_agf_t, agf_length), 3151 offsetof(xfs_agf_t, agf_bno_root), /* also cnt/rmap root */ 3152 offsetof(xfs_agf_t, agf_bno_level), /* also cnt/rmap levels */ 3153 offsetof(xfs_agf_t, agf_flfirst), 3154 offsetof(xfs_agf_t, agf_fllast), 3155 offsetof(xfs_agf_t, agf_flcount), 3156 offsetof(xfs_agf_t, agf_freeblks), 3157 offsetof(xfs_agf_t, agf_longest), 3158 offsetof(xfs_agf_t, agf_btreeblks), 3159 offsetof(xfs_agf_t, agf_uuid), 3160 offsetof(xfs_agf_t, agf_rmap_blocks), 3161 offsetof(xfs_agf_t, agf_refcount_blocks), 3162 offsetof(xfs_agf_t, agf_refcount_root), 3163 offsetof(xfs_agf_t, agf_refcount_level), 3164 /* needed so that we don't log the whole rest of the structure: */ 3165 offsetof(xfs_agf_t, agf_spare64), 3166 sizeof(xfs_agf_t) 3167 }; 3168 3169 trace_xfs_agf(tp->t_mountp, bp->b_addr, fields, _RET_IP_); 3170 3171 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF); 3172 3173 xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last); 3174 xfs_trans_log_buf(tp, bp, (uint)first, (uint)last); 3175 } 3176 3177 /* 3178 * Put the block on the freelist for the allocation group. 3179 */ 3180 int 3181 xfs_alloc_put_freelist( 3182 struct xfs_perag *pag, 3183 struct xfs_trans *tp, 3184 struct xfs_buf *agbp, 3185 struct xfs_buf *agflbp, 3186 xfs_agblock_t bno, 3187 int btreeblk) 3188 { 3189 struct xfs_mount *mp = tp->t_mountp; 3190 struct xfs_agf *agf = agbp->b_addr; 3191 __be32 *blockp; 3192 int error; 3193 uint32_t logflags; 3194 __be32 *agfl_bno; 3195 int startoff; 3196 3197 if (!agflbp) { 3198 error = xfs_alloc_read_agfl(pag, tp, &agflbp); 3199 if (error) 3200 return error; 3201 } 3202 3203 be32_add_cpu(&agf->agf_fllast, 1); 3204 if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp)) 3205 agf->agf_fllast = 0; 3206 3207 ASSERT(!xfs_perag_agfl_needs_reset(pag)); 3208 be32_add_cpu(&agf->agf_flcount, 1); 3209 pag->pagf_flcount++; 3210 3211 logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT; 3212 if (btreeblk) { 3213 be32_add_cpu(&agf->agf_btreeblks, -1); 3214 pag->pagf_btreeblks--; 3215 logflags |= XFS_AGF_BTREEBLKS; 3216 } 3217 3218 ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)); 3219 3220 agfl_bno = xfs_buf_to_agfl_bno(agflbp); 3221 blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)]; 3222 *blockp = cpu_to_be32(bno); 3223 startoff = (char *)blockp - (char *)agflbp->b_addr; 3224 3225 xfs_alloc_log_agf(tp, agbp, logflags); 3226 3227 xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF); 3228 xfs_trans_log_buf(tp, agflbp, startoff, 3229 startoff + sizeof(xfs_agblock_t) - 1); 3230 return 0; 3231 } 3232 3233 /* 3234 * Check that this AGF/AGI header's sequence number and length matches the AG 3235 * number and size in fsblocks. 3236 */ 3237 xfs_failaddr_t 3238 xfs_validate_ag_length( 3239 struct xfs_buf *bp, 3240 uint32_t seqno, 3241 uint32_t length) 3242 { 3243 struct xfs_mount *mp = bp->b_mount; 3244 /* 3245 * During growfs operations, the perag is not fully initialised, 3246 * so we can't use it for any useful checking. growfs ensures we can't 3247 * use it by using uncached buffers that don't have the perag attached 3248 * so we can detect and avoid this problem. 3249 */ 3250 if (bp->b_pag && seqno != pag_agno(bp->b_pag)) 3251 return __this_address; 3252 3253 /* 3254 * Only the last AG in the filesystem is allowed to be shorter 3255 * than the AG size recorded in the superblock. 3256 */ 3257 if (length != mp->m_sb.sb_agblocks) { 3258 /* 3259 * During growfs, the new last AG can get here before we 3260 * have updated the superblock. Give it a pass on the seqno 3261 * check. 3262 */ 3263 if (bp->b_pag && seqno != mp->m_sb.sb_agcount - 1) 3264 return __this_address; 3265 if (length < XFS_MIN_AG_BLOCKS) 3266 return __this_address; 3267 if (length > mp->m_sb.sb_agblocks) 3268 return __this_address; 3269 } 3270 3271 return NULL; 3272 } 3273 3274 /* 3275 * Verify the AGF is consistent. 3276 * 3277 * We do not verify the AGFL indexes in the AGF are fully consistent here 3278 * because of issues with variable on-disk structure sizes. Instead, we check 3279 * the agfl indexes for consistency when we initialise the perag from the AGF 3280 * information after a read completes. 3281 * 3282 * If the index is inconsistent, then we mark the perag as needing an AGFL 3283 * reset. The first AGFL update performed then resets the AGFL indexes and 3284 * refills the AGFL with known good free blocks, allowing the filesystem to 3285 * continue operating normally at the cost of a few leaked free space blocks. 3286 */ 3287 static xfs_failaddr_t 3288 xfs_agf_verify( 3289 struct xfs_buf *bp) 3290 { 3291 struct xfs_mount *mp = bp->b_mount; 3292 struct xfs_agf *agf = bp->b_addr; 3293 xfs_failaddr_t fa; 3294 uint32_t agf_seqno = be32_to_cpu(agf->agf_seqno); 3295 uint32_t agf_length = be32_to_cpu(agf->agf_length); 3296 3297 if (xfs_has_crc(mp)) { 3298 if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid)) 3299 return __this_address; 3300 if (!xfs_log_check_lsn(mp, be64_to_cpu(agf->agf_lsn))) 3301 return __this_address; 3302 } 3303 3304 if (!xfs_verify_magic(bp, agf->agf_magicnum)) 3305 return __this_address; 3306 3307 if (!XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum))) 3308 return __this_address; 3309 3310 /* 3311 * Both agf_seqno and agf_length need to validated before anything else 3312 * block number related in the AGF or AGFL can be checked. 3313 */ 3314 fa = xfs_validate_ag_length(bp, agf_seqno, agf_length); 3315 if (fa) 3316 return fa; 3317 3318 if (be32_to_cpu(agf->agf_flfirst) >= xfs_agfl_size(mp)) 3319 return __this_address; 3320 if (be32_to_cpu(agf->agf_fllast) >= xfs_agfl_size(mp)) 3321 return __this_address; 3322 if (be32_to_cpu(agf->agf_flcount) > xfs_agfl_size(mp)) 3323 return __this_address; 3324 3325 if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) || 3326 be32_to_cpu(agf->agf_freeblks) > agf_length) 3327 return __this_address; 3328 3329 if (be32_to_cpu(agf->agf_bno_level) < 1 || 3330 be32_to_cpu(agf->agf_cnt_level) < 1 || 3331 be32_to_cpu(agf->agf_bno_level) > mp->m_alloc_maxlevels || 3332 be32_to_cpu(agf->agf_cnt_level) > mp->m_alloc_maxlevels) 3333 return __this_address; 3334 3335 if (xfs_has_lazysbcount(mp) && 3336 be32_to_cpu(agf->agf_btreeblks) > agf_length) 3337 return __this_address; 3338 3339 if (xfs_has_rmapbt(mp)) { 3340 if (be32_to_cpu(agf->agf_rmap_blocks) > agf_length) 3341 return __this_address; 3342 3343 if (be32_to_cpu(agf->agf_rmap_level) < 1 || 3344 be32_to_cpu(agf->agf_rmap_level) > mp->m_rmap_maxlevels) 3345 return __this_address; 3346 } 3347 3348 if (xfs_has_reflink(mp)) { 3349 if (be32_to_cpu(agf->agf_refcount_blocks) > agf_length) 3350 return __this_address; 3351 3352 if (be32_to_cpu(agf->agf_refcount_level) < 1 || 3353 be32_to_cpu(agf->agf_refcount_level) > mp->m_refc_maxlevels) 3354 return __this_address; 3355 } 3356 3357 return NULL; 3358 } 3359 3360 static void 3361 xfs_agf_read_verify( 3362 struct xfs_buf *bp) 3363 { 3364 struct xfs_mount *mp = bp->b_mount; 3365 xfs_failaddr_t fa; 3366 3367 if (xfs_has_crc(mp) && 3368 !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF)) 3369 xfs_verifier_error(bp, -EFSBADCRC, __this_address); 3370 else { 3371 fa = xfs_agf_verify(bp); 3372 if (fa || XFS_TEST_ERROR(mp, XFS_ERRTAG_ALLOC_READ_AGF)) 3373 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 3374 } 3375 } 3376 3377 static void 3378 xfs_agf_write_verify( 3379 struct xfs_buf *bp) 3380 { 3381 struct xfs_mount *mp = bp->b_mount; 3382 struct xfs_buf_log_item *bip = bp->b_log_item; 3383 struct xfs_agf *agf = bp->b_addr; 3384 xfs_failaddr_t fa; 3385 3386 fa = xfs_agf_verify(bp); 3387 if (fa) { 3388 xfs_verifier_error(bp, -EFSCORRUPTED, fa); 3389 return; 3390 } 3391 3392 if (!xfs_has_crc(mp)) 3393 return; 3394 3395 if (bip) 3396 agf->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn); 3397 3398 xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF); 3399 } 3400 3401 const struct xfs_buf_ops xfs_agf_buf_ops = { 3402 .name = "xfs_agf", 3403 .magic = { cpu_to_be32(XFS_AGF_MAGIC), cpu_to_be32(XFS_AGF_MAGIC) }, 3404 .verify_read = xfs_agf_read_verify, 3405 .verify_write = xfs_agf_write_verify, 3406 .verify_struct = xfs_agf_verify, 3407 }; 3408 3409 /* 3410 * Read in the allocation group header (free/alloc section). 3411 */ 3412 int 3413 xfs_read_agf( 3414 struct xfs_perag *pag, 3415 struct xfs_trans *tp, 3416 int flags, 3417 struct xfs_buf **agfbpp) 3418 { 3419 struct xfs_mount *mp = pag_mount(pag); 3420 int error; 3421 3422 trace_xfs_read_agf(pag); 3423 3424 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 3425 XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGF_DADDR(mp)), 3426 XFS_FSS_TO_BB(mp, 1), flags, agfbpp, &xfs_agf_buf_ops); 3427 if (xfs_metadata_is_sick(error)) 3428 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF); 3429 if (error) 3430 return error; 3431 3432 xfs_buf_set_ref(*agfbpp, XFS_AGF_REF); 3433 return 0; 3434 } 3435 3436 /* 3437 * Read in the allocation group header (free/alloc section) and initialise the 3438 * perag structure if necessary. If the caller provides @agfbpp, then return the 3439 * locked buffer to the caller, otherwise free it. 3440 */ 3441 int 3442 xfs_alloc_read_agf( 3443 struct xfs_perag *pag, 3444 struct xfs_trans *tp, 3445 int flags, 3446 struct xfs_buf **agfbpp) 3447 { 3448 struct xfs_mount *mp = pag_mount(pag); 3449 struct xfs_buf *agfbp; 3450 struct xfs_agf *agf; 3451 int error; 3452 int allocbt_blks; 3453 3454 trace_xfs_alloc_read_agf(pag); 3455 3456 /* We don't support trylock when freeing. */ 3457 ASSERT((flags & (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)) != 3458 (XFS_ALLOC_FLAG_FREEING | XFS_ALLOC_FLAG_TRYLOCK)); 3459 error = xfs_read_agf(pag, tp, 3460 (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0, 3461 &agfbp); 3462 if (error) 3463 return error; 3464 3465 agf = agfbp->b_addr; 3466 if (!xfs_perag_initialised_agf(pag)) { 3467 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks); 3468 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks); 3469 pag->pagf_flcount = be32_to_cpu(agf->agf_flcount); 3470 pag->pagf_longest = be32_to_cpu(agf->agf_longest); 3471 pag->pagf_bno_level = be32_to_cpu(agf->agf_bno_level); 3472 pag->pagf_cnt_level = be32_to_cpu(agf->agf_cnt_level); 3473 pag->pagf_rmap_level = be32_to_cpu(agf->agf_rmap_level); 3474 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level); 3475 if (xfs_agfl_needs_reset(mp, agf)) 3476 set_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate); 3477 else 3478 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET, &pag->pag_opstate); 3479 3480 /* 3481 * Update the in-core allocbt counter. Filter out the rmapbt 3482 * subset of the btreeblks counter because the rmapbt is managed 3483 * by perag reservation. Subtract one for the rmapbt root block 3484 * because the rmap counter includes it while the btreeblks 3485 * counter only tracks non-root blocks. 3486 */ 3487 allocbt_blks = pag->pagf_btreeblks; 3488 if (xfs_has_rmapbt(mp)) 3489 allocbt_blks -= be32_to_cpu(agf->agf_rmap_blocks) - 1; 3490 if (allocbt_blks > 0) 3491 atomic64_add(allocbt_blks, &mp->m_allocbt_blks); 3492 3493 set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate); 3494 } 3495 3496 #ifdef DEBUG 3497 /* 3498 * It's possible for the AGF to be out of sync if the block device is 3499 * silently dropping writes. This can happen in fstests with dmflakey 3500 * enabled, which allows the buffer to be cleaned and reclaimed by 3501 * memory pressure and then re-read from disk here. We will get a 3502 * stale version of the AGF from disk, and nothing good can happen from 3503 * here. Hence if we detect this situation, immediately shut down the 3504 * filesystem. 3505 * 3506 * This can also happen if we are already in the middle of a forced 3507 * shutdown, so don't bother checking if we are already shut down. 3508 */ 3509 if (!xfs_is_shutdown(pag_mount(pag))) { 3510 bool ok = true; 3511 3512 ok &= pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks); 3513 ok &= pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks); 3514 ok &= pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks); 3515 ok &= pag->pagf_flcount == be32_to_cpu(agf->agf_flcount); 3516 ok &= pag->pagf_longest == be32_to_cpu(agf->agf_longest); 3517 ok &= pag->pagf_bno_level == be32_to_cpu(agf->agf_bno_level); 3518 ok &= pag->pagf_cnt_level == be32_to_cpu(agf->agf_cnt_level); 3519 3520 if (XFS_IS_CORRUPT(pag_mount(pag), !ok)) { 3521 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF); 3522 xfs_trans_brelse(tp, agfbp); 3523 xfs_force_shutdown(pag_mount(pag), 3524 SHUTDOWN_CORRUPT_ONDISK); 3525 return -EFSCORRUPTED; 3526 } 3527 } 3528 #endif /* DEBUG */ 3529 3530 if (agfbpp) 3531 *agfbpp = agfbp; 3532 else 3533 xfs_trans_brelse(tp, agfbp); 3534 return 0; 3535 } 3536 3537 /* 3538 * Pre-process allocation arguments to set initial state that we don't require 3539 * callers to set up correctly, as well as bounds check the allocation args 3540 * that are set up. 3541 */ 3542 static int 3543 xfs_alloc_vextent_check_args( 3544 struct xfs_alloc_arg *args, 3545 xfs_fsblock_t target, 3546 xfs_agnumber_t *minimum_agno) 3547 { 3548 struct xfs_mount *mp = args->mp; 3549 xfs_agblock_t agsize; 3550 3551 args->fsbno = NULLFSBLOCK; 3552 3553 *minimum_agno = 0; 3554 if (args->tp->t_highest_agno != NULLAGNUMBER) 3555 *minimum_agno = args->tp->t_highest_agno; 3556 3557 /* 3558 * Just fix this up, for the case where the last a.g. is shorter 3559 * (or there's only one a.g.) and the caller couldn't easily figure 3560 * that out (xfs_bmap_alloc). 3561 */ 3562 agsize = mp->m_sb.sb_agblocks; 3563 if (args->maxlen > agsize) 3564 args->maxlen = agsize; 3565 if (args->alignment == 0) 3566 args->alignment = 1; 3567 3568 ASSERT(args->minlen > 0); 3569 ASSERT(args->maxlen > 0); 3570 ASSERT(args->alignment > 0); 3571 ASSERT(args->resv != XFS_AG_RESV_AGFL); 3572 3573 ASSERT(XFS_FSB_TO_AGNO(mp, target) < mp->m_sb.sb_agcount); 3574 ASSERT(XFS_FSB_TO_AGBNO(mp, target) < agsize); 3575 ASSERT(args->minlen <= args->maxlen); 3576 ASSERT(args->minlen <= agsize); 3577 ASSERT(args->mod < args->prod); 3578 3579 if (XFS_FSB_TO_AGNO(mp, target) >= mp->m_sb.sb_agcount || 3580 XFS_FSB_TO_AGBNO(mp, target) >= agsize || 3581 args->minlen > args->maxlen || args->minlen > agsize || 3582 args->mod >= args->prod) { 3583 trace_xfs_alloc_vextent_badargs(args); 3584 return -ENOSPC; 3585 } 3586 3587 if (args->agno != NULLAGNUMBER && *minimum_agno > args->agno) { 3588 trace_xfs_alloc_vextent_skip_deadlock(args); 3589 return -ENOSPC; 3590 } 3591 return 0; 3592 3593 } 3594 3595 /* 3596 * Prepare an AG for allocation. If the AG is not prepared to accept the 3597 * allocation, return failure. 3598 * 3599 * XXX(dgc): The complexity of "need_pag" will go away as all caller paths are 3600 * modified to hold their own perag references. 3601 */ 3602 static int 3603 xfs_alloc_vextent_prepare_ag( 3604 struct xfs_alloc_arg *args, 3605 uint32_t alloc_flags) 3606 { 3607 bool need_pag = !args->pag; 3608 int error; 3609 3610 if (need_pag) 3611 args->pag = xfs_perag_get(args->mp, args->agno); 3612 3613 args->agbp = NULL; 3614 error = xfs_alloc_fix_freelist(args, alloc_flags); 3615 if (error) { 3616 trace_xfs_alloc_vextent_nofix(args); 3617 if (need_pag) 3618 xfs_perag_put(args->pag); 3619 args->agbno = NULLAGBLOCK; 3620 return error; 3621 } 3622 if (!args->agbp) { 3623 /* cannot allocate in this AG at all */ 3624 trace_xfs_alloc_vextent_noagbp(args); 3625 args->agbno = NULLAGBLOCK; 3626 return 0; 3627 } 3628 args->wasfromfl = 0; 3629 return 0; 3630 } 3631 3632 /* 3633 * Post-process allocation results to account for the allocation if it succeed 3634 * and set the allocated block number correctly for the caller. 3635 * 3636 * XXX: we should really be returning ENOSPC for ENOSPC, not 3637 * hiding it behind a "successful" NULLFSBLOCK allocation. 3638 */ 3639 static int 3640 xfs_alloc_vextent_finish( 3641 struct xfs_alloc_arg *args, 3642 xfs_agnumber_t minimum_agno, 3643 int alloc_error, 3644 bool drop_perag) 3645 { 3646 struct xfs_mount *mp = args->mp; 3647 int error = 0; 3648 3649 /* 3650 * We can end up here with a locked AGF. If we failed, the caller is 3651 * likely going to try to allocate again with different parameters, and 3652 * that can widen the AGs that are searched for free space. If we have 3653 * to do BMBT block allocation, we have to do a new allocation. 3654 * 3655 * Hence leaving this function with the AGF locked opens up potential 3656 * ABBA AGF deadlocks because a future allocation attempt in this 3657 * transaction may attempt to lock a lower number AGF. 3658 * 3659 * We can't release the AGF until the transaction is committed, so at 3660 * this point we must update the "first allocation" tracker to point at 3661 * this AG if the tracker is empty or points to a lower AG. This allows 3662 * the next allocation attempt to be modified appropriately to avoid 3663 * deadlocks. 3664 */ 3665 if (args->agbp && 3666 (args->tp->t_highest_agno == NULLAGNUMBER || 3667 args->agno > minimum_agno)) 3668 args->tp->t_highest_agno = args->agno; 3669 3670 /* 3671 * If the allocation failed with an error or we had an ENOSPC result, 3672 * preserve the returned error whilst also marking the allocation result 3673 * as "no extent allocated". This ensures that callers that fail to 3674 * capture the error will still treat it as a failed allocation. 3675 */ 3676 if (alloc_error || args->agbno == NULLAGBLOCK) { 3677 args->fsbno = NULLFSBLOCK; 3678 error = alloc_error; 3679 goto out_drop_perag; 3680 } 3681 3682 args->fsbno = xfs_agbno_to_fsb(args->pag, args->agbno); 3683 3684 ASSERT(args->len >= args->minlen); 3685 ASSERT(args->len <= args->maxlen); 3686 ASSERT(args->agbno % args->alignment == 0); 3687 XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno), args->len); 3688 3689 /* if not file data, insert new block into the reverse map btree */ 3690 if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) { 3691 error = xfs_rmap_alloc(args->tp, args->agbp, args->pag, 3692 args->agbno, args->len, &args->oinfo); 3693 if (error) 3694 goto out_drop_perag; 3695 } 3696 3697 if (!args->wasfromfl) { 3698 error = xfs_alloc_update_counters(args->tp, args->agbp, 3699 -((long)(args->len))); 3700 if (error) 3701 goto out_drop_perag; 3702 3703 ASSERT(!xfs_extent_busy_search(pag_group(args->pag), 3704 args->agbno, args->len)); 3705 } 3706 3707 xfs_ag_resv_alloc_extent(args->pag, args->resv, args); 3708 3709 XFS_STATS_INC(mp, xs_allocx); 3710 XFS_STATS_ADD(mp, xs_allocb, args->len); 3711 3712 trace_xfs_alloc_vextent_finish(args); 3713 3714 out_drop_perag: 3715 if (drop_perag && args->pag) { 3716 xfs_perag_rele(args->pag); 3717 args->pag = NULL; 3718 } 3719 return error; 3720 } 3721 3722 /* 3723 * Allocate within a single AG only. This uses a best-fit length algorithm so if 3724 * you need an exact sized allocation without locality constraints, this is the 3725 * fastest way to do it. 3726 * 3727 * Caller is expected to hold a perag reference in args->pag. 3728 */ 3729 int 3730 xfs_alloc_vextent_this_ag( 3731 struct xfs_alloc_arg *args, 3732 xfs_agnumber_t agno) 3733 { 3734 xfs_agnumber_t minimum_agno; 3735 uint32_t alloc_flags = 0; 3736 int error; 3737 3738 ASSERT(args->pag != NULL); 3739 ASSERT(pag_agno(args->pag) == agno); 3740 3741 args->agno = agno; 3742 args->agbno = 0; 3743 3744 trace_xfs_alloc_vextent_this_ag(args); 3745 3746 error = xfs_alloc_vextent_check_args(args, 3747 xfs_agbno_to_fsb(args->pag, 0), &minimum_agno); 3748 if (error) { 3749 if (error == -ENOSPC) 3750 return 0; 3751 return error; 3752 } 3753 3754 error = xfs_alloc_vextent_prepare_ag(args, alloc_flags); 3755 if (!error && args->agbp) 3756 error = xfs_alloc_ag_vextent_size(args, alloc_flags); 3757 3758 return xfs_alloc_vextent_finish(args, minimum_agno, error, false); 3759 } 3760 3761 /* 3762 * Iterate all AGs trying to allocate an extent starting from @start_ag. 3763 * 3764 * If the incoming allocation type is XFS_ALLOCTYPE_NEAR_BNO, it means the 3765 * allocation attempts in @start_agno have locality information. If we fail to 3766 * allocate in that AG, then we revert to anywhere-in-AG for all the other AGs 3767 * we attempt to allocation in as there is no locality optimisation possible for 3768 * those allocations. 3769 * 3770 * On return, args->pag may be left referenced if we finish before the "all 3771 * failed" return point. The allocation finish still needs the perag, and 3772 * so the caller will release it once they've finished the allocation. 3773 * 3774 * When we wrap the AG iteration at the end of the filesystem, we have to be 3775 * careful not to wrap into AGs below ones we already have locked in the 3776 * transaction if we are doing a blocking iteration. This will result in an 3777 * out-of-order locking of AGFs and hence can cause deadlocks. 3778 */ 3779 static int 3780 xfs_alloc_vextent_iterate_ags( 3781 struct xfs_alloc_arg *args, 3782 xfs_agnumber_t minimum_agno, 3783 xfs_agnumber_t start_agno, 3784 xfs_agblock_t target_agbno, 3785 uint32_t alloc_flags) 3786 { 3787 struct xfs_mount *mp = args->mp; 3788 xfs_agnumber_t restart_agno = minimum_agno; 3789 xfs_agnumber_t agno; 3790 int error = 0; 3791 3792 if (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK) 3793 restart_agno = 0; 3794 restart: 3795 for_each_perag_wrap_range(mp, start_agno, restart_agno, 3796 mp->m_sb.sb_agcount, agno, args->pag) { 3797 args->agno = agno; 3798 error = xfs_alloc_vextent_prepare_ag(args, alloc_flags); 3799 if (error) 3800 break; 3801 if (!args->agbp) { 3802 trace_xfs_alloc_vextent_loopfailed(args); 3803 continue; 3804 } 3805 3806 /* 3807 * Allocation is supposed to succeed now, so break out of the 3808 * loop regardless of whether we succeed or not. 3809 */ 3810 if (args->agno == start_agno && target_agbno) { 3811 args->agbno = target_agbno; 3812 error = xfs_alloc_ag_vextent_near(args, alloc_flags); 3813 } else { 3814 args->agbno = 0; 3815 error = xfs_alloc_ag_vextent_size(args, alloc_flags); 3816 } 3817 break; 3818 } 3819 if (error) { 3820 xfs_perag_rele(args->pag); 3821 args->pag = NULL; 3822 return error; 3823 } 3824 if (args->agbp) 3825 return 0; 3826 3827 /* 3828 * We didn't find an AG we can alloation from. If we were given 3829 * constraining flags by the caller, drop them and retry the allocation 3830 * without any constraints being set. 3831 */ 3832 if (alloc_flags & XFS_ALLOC_FLAG_TRYLOCK) { 3833 alloc_flags &= ~XFS_ALLOC_FLAG_TRYLOCK; 3834 restart_agno = minimum_agno; 3835 goto restart; 3836 } 3837 3838 ASSERT(args->pag == NULL); 3839 trace_xfs_alloc_vextent_allfailed(args); 3840 return 0; 3841 } 3842 3843 /* 3844 * Iterate from the AGs from the start AG to the end of the filesystem, trying 3845 * to allocate blocks. It starts with a near allocation attempt in the initial 3846 * AG, then falls back to anywhere-in-ag after the first AG fails. It will wrap 3847 * back to zero if allowed by previous allocations in this transaction, 3848 * otherwise will wrap back to the start AG and run a second blocking pass to 3849 * the end of the filesystem. 3850 */ 3851 int 3852 xfs_alloc_vextent_start_ag( 3853 struct xfs_alloc_arg *args, 3854 xfs_fsblock_t target) 3855 { 3856 struct xfs_mount *mp = args->mp; 3857 xfs_agnumber_t minimum_agno; 3858 xfs_agnumber_t start_agno; 3859 xfs_agnumber_t rotorstep = xfs_rotorstep; 3860 bool bump_rotor = false; 3861 uint32_t alloc_flags = XFS_ALLOC_FLAG_TRYLOCK; 3862 int error; 3863 3864 ASSERT(args->pag == NULL); 3865 3866 args->agno = NULLAGNUMBER; 3867 args->agbno = NULLAGBLOCK; 3868 3869 trace_xfs_alloc_vextent_start_ag(args); 3870 3871 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno); 3872 if (error) { 3873 if (error == -ENOSPC) 3874 return 0; 3875 return error; 3876 } 3877 3878 if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) && 3879 xfs_is_inode32(mp)) { 3880 target = XFS_AGB_TO_FSB(mp, 3881 ((mp->m_agfrotor / rotorstep) % 3882 mp->m_sb.sb_agcount), 0); 3883 bump_rotor = 1; 3884 } 3885 3886 start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target)); 3887 error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno, 3888 XFS_FSB_TO_AGBNO(mp, target), alloc_flags); 3889 3890 if (bump_rotor) { 3891 if (args->agno == start_agno) 3892 mp->m_agfrotor = (mp->m_agfrotor + 1) % 3893 (mp->m_sb.sb_agcount * rotorstep); 3894 else 3895 mp->m_agfrotor = (args->agno * rotorstep + 1) % 3896 (mp->m_sb.sb_agcount * rotorstep); 3897 } 3898 3899 return xfs_alloc_vextent_finish(args, minimum_agno, error, true); 3900 } 3901 3902 /* 3903 * Iterate from the agno indicated via @target through to the end of the 3904 * filesystem attempting blocking allocation. This does not wrap or try a second 3905 * pass, so will not recurse into AGs lower than indicated by the target. 3906 */ 3907 int 3908 xfs_alloc_vextent_first_ag( 3909 struct xfs_alloc_arg *args, 3910 xfs_fsblock_t target) 3911 { 3912 struct xfs_mount *mp = args->mp; 3913 xfs_agnumber_t minimum_agno; 3914 xfs_agnumber_t start_agno; 3915 uint32_t alloc_flags = XFS_ALLOC_FLAG_TRYLOCK; 3916 int error; 3917 3918 ASSERT(args->pag == NULL); 3919 3920 args->agno = NULLAGNUMBER; 3921 args->agbno = NULLAGBLOCK; 3922 3923 trace_xfs_alloc_vextent_first_ag(args); 3924 3925 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno); 3926 if (error) { 3927 if (error == -ENOSPC) 3928 return 0; 3929 return error; 3930 } 3931 3932 start_agno = max(minimum_agno, XFS_FSB_TO_AGNO(mp, target)); 3933 error = xfs_alloc_vextent_iterate_ags(args, minimum_agno, start_agno, 3934 XFS_FSB_TO_AGBNO(mp, target), alloc_flags); 3935 return xfs_alloc_vextent_finish(args, minimum_agno, error, true); 3936 } 3937 3938 /* 3939 * Allocate at the exact block target or fail. Caller is expected to hold a 3940 * perag reference in args->pag. 3941 */ 3942 int 3943 xfs_alloc_vextent_exact_bno( 3944 struct xfs_alloc_arg *args, 3945 xfs_fsblock_t target) 3946 { 3947 struct xfs_mount *mp = args->mp; 3948 xfs_agnumber_t minimum_agno; 3949 int error; 3950 3951 ASSERT(args->pag != NULL); 3952 ASSERT(pag_agno(args->pag) == XFS_FSB_TO_AGNO(mp, target)); 3953 3954 args->agno = XFS_FSB_TO_AGNO(mp, target); 3955 args->agbno = XFS_FSB_TO_AGBNO(mp, target); 3956 3957 trace_xfs_alloc_vextent_exact_bno(args); 3958 3959 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno); 3960 if (error) { 3961 if (error == -ENOSPC) 3962 return 0; 3963 return error; 3964 } 3965 3966 error = xfs_alloc_vextent_prepare_ag(args, 0); 3967 if (!error && args->agbp) 3968 error = xfs_alloc_ag_vextent_exact(args); 3969 3970 return xfs_alloc_vextent_finish(args, minimum_agno, error, false); 3971 } 3972 3973 /* 3974 * Allocate an extent as close to the target as possible. If there are not 3975 * viable candidates in the AG, then fail the allocation. 3976 * 3977 * Caller may or may not have a per-ag reference in args->pag. 3978 */ 3979 int 3980 xfs_alloc_vextent_near_bno( 3981 struct xfs_alloc_arg *args, 3982 xfs_fsblock_t target) 3983 { 3984 struct xfs_mount *mp = args->mp; 3985 xfs_agnumber_t minimum_agno; 3986 bool needs_perag = args->pag == NULL; 3987 uint32_t alloc_flags = 0; 3988 int error; 3989 3990 if (!needs_perag) 3991 ASSERT(pag_agno(args->pag) == XFS_FSB_TO_AGNO(mp, target)); 3992 3993 args->agno = XFS_FSB_TO_AGNO(mp, target); 3994 args->agbno = XFS_FSB_TO_AGBNO(mp, target); 3995 3996 trace_xfs_alloc_vextent_near_bno(args); 3997 3998 error = xfs_alloc_vextent_check_args(args, target, &minimum_agno); 3999 if (error) { 4000 if (error == -ENOSPC) 4001 return 0; 4002 return error; 4003 } 4004 4005 if (needs_perag) 4006 args->pag = xfs_perag_grab(mp, args->agno); 4007 4008 error = xfs_alloc_vextent_prepare_ag(args, alloc_flags); 4009 if (!error && args->agbp) 4010 error = xfs_alloc_ag_vextent_near(args, alloc_flags); 4011 4012 return xfs_alloc_vextent_finish(args, minimum_agno, error, needs_perag); 4013 } 4014 4015 /* Ensure that the freelist is at full capacity. */ 4016 int 4017 xfs_free_extent_fix_freelist( 4018 struct xfs_trans *tp, 4019 struct xfs_perag *pag, 4020 struct xfs_buf **agbp) 4021 { 4022 struct xfs_alloc_arg args; 4023 int error; 4024 4025 memset(&args, 0, sizeof(struct xfs_alloc_arg)); 4026 args.tp = tp; 4027 args.mp = tp->t_mountp; 4028 args.agno = pag_agno(pag); 4029 args.pag = pag; 4030 4031 /* 4032 * validate that the block number is legal - the enables us to detect 4033 * and handle a silent filesystem corruption rather than crashing. 4034 */ 4035 if (args.agno >= args.mp->m_sb.sb_agcount) 4036 return -EFSCORRUPTED; 4037 4038 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING); 4039 if (error) 4040 return error; 4041 4042 *agbp = args.agbp; 4043 return 0; 4044 } 4045 4046 /* 4047 * Free an extent. 4048 * Just break up the extent address and hand off to xfs_free_ag_extent 4049 * after fixing up the freelist. 4050 */ 4051 int 4052 __xfs_free_extent( 4053 struct xfs_trans *tp, 4054 struct xfs_perag *pag, 4055 xfs_agblock_t agbno, 4056 xfs_extlen_t len, 4057 const struct xfs_owner_info *oinfo, 4058 enum xfs_ag_resv_type type, 4059 bool skip_discard) 4060 { 4061 struct xfs_mount *mp = tp->t_mountp; 4062 struct xfs_buf *agbp; 4063 struct xfs_agf *agf; 4064 int error; 4065 unsigned int busy_flags = 0; 4066 4067 ASSERT(len != 0); 4068 ASSERT(type != XFS_AG_RESV_AGFL); 4069 4070 if (XFS_TEST_ERROR(mp, XFS_ERRTAG_FREE_EXTENT)) 4071 return -EIO; 4072 4073 error = xfs_free_extent_fix_freelist(tp, pag, &agbp); 4074 if (error) { 4075 if (xfs_metadata_is_sick(error)) 4076 xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT); 4077 return error; 4078 } 4079 4080 agf = agbp->b_addr; 4081 4082 if (XFS_IS_CORRUPT(mp, agbno >= mp->m_sb.sb_agblocks)) { 4083 xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT); 4084 error = -EFSCORRUPTED; 4085 goto err_release; 4086 } 4087 4088 /* validate the extent size is legal now we have the agf locked */ 4089 if (XFS_IS_CORRUPT(mp, agbno + len > be32_to_cpu(agf->agf_length))) { 4090 xfs_ag_mark_sick(pag, XFS_SICK_AG_BNOBT); 4091 error = -EFSCORRUPTED; 4092 goto err_release; 4093 } 4094 4095 error = xfs_free_ag_extent(tp, agbp, agbno, len, oinfo, type); 4096 if (error) 4097 goto err_release; 4098 4099 if (skip_discard) 4100 busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD; 4101 xfs_extent_busy_insert(tp, pag_group(pag), agbno, len, busy_flags); 4102 return 0; 4103 4104 err_release: 4105 xfs_trans_brelse(tp, agbp); 4106 return error; 4107 } 4108 4109 struct xfs_alloc_query_range_info { 4110 xfs_alloc_query_range_fn fn; 4111 void *priv; 4112 }; 4113 4114 /* Format btree record and pass to our callback. */ 4115 STATIC int 4116 xfs_alloc_query_range_helper( 4117 struct xfs_btree_cur *cur, 4118 const union xfs_btree_rec *rec, 4119 void *priv) 4120 { 4121 struct xfs_alloc_query_range_info *query = priv; 4122 struct xfs_alloc_rec_incore irec; 4123 xfs_failaddr_t fa; 4124 4125 xfs_alloc_btrec_to_irec(rec, &irec); 4126 fa = xfs_alloc_check_irec(to_perag(cur->bc_group), &irec); 4127 if (fa) 4128 return xfs_alloc_complain_bad_rec(cur, fa, &irec); 4129 4130 return query->fn(cur, &irec, query->priv); 4131 } 4132 4133 /* Find all free space within a given range of blocks. */ 4134 int 4135 xfs_alloc_query_range( 4136 struct xfs_btree_cur *cur, 4137 const struct xfs_alloc_rec_incore *low_rec, 4138 const struct xfs_alloc_rec_incore *high_rec, 4139 xfs_alloc_query_range_fn fn, 4140 void *priv) 4141 { 4142 union xfs_btree_irec low_brec = { .a = *low_rec }; 4143 union xfs_btree_irec high_brec = { .a = *high_rec }; 4144 struct xfs_alloc_query_range_info query = { .priv = priv, .fn = fn }; 4145 4146 ASSERT(xfs_btree_is_bno(cur->bc_ops)); 4147 return xfs_btree_query_range(cur, &low_brec, &high_brec, 4148 xfs_alloc_query_range_helper, &query); 4149 } 4150 4151 /* Find all free space records. */ 4152 int 4153 xfs_alloc_query_all( 4154 struct xfs_btree_cur *cur, 4155 xfs_alloc_query_range_fn fn, 4156 void *priv) 4157 { 4158 struct xfs_alloc_query_range_info query; 4159 4160 ASSERT(xfs_btree_is_bno(cur->bc_ops)); 4161 query.priv = priv; 4162 query.fn = fn; 4163 return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query); 4164 } 4165 4166 /* 4167 * Scan part of the keyspace of the free space and tell us if the area has no 4168 * records, is fully mapped by records, or is partially filled. 4169 */ 4170 int 4171 xfs_alloc_has_records( 4172 struct xfs_btree_cur *cur, 4173 xfs_agblock_t bno, 4174 xfs_extlen_t len, 4175 enum xbtree_recpacking *outcome) 4176 { 4177 union xfs_btree_irec low; 4178 union xfs_btree_irec high; 4179 4180 memset(&low, 0, sizeof(low)); 4181 low.a.ar_startblock = bno; 4182 memset(&high, 0xFF, sizeof(high)); 4183 high.a.ar_startblock = bno + len - 1; 4184 4185 return xfs_btree_has_records(cur, &low, &high, NULL, outcome); 4186 } 4187 4188 /* 4189 * Walk all the blocks in the AGFL. The @walk_fn can return any negative 4190 * error code or XFS_ITER_*. 4191 */ 4192 int 4193 xfs_agfl_walk( 4194 struct xfs_mount *mp, 4195 struct xfs_agf *agf, 4196 struct xfs_buf *agflbp, 4197 xfs_agfl_walk_fn walk_fn, 4198 void *priv) 4199 { 4200 __be32 *agfl_bno; 4201 unsigned int i; 4202 int error; 4203 4204 agfl_bno = xfs_buf_to_agfl_bno(agflbp); 4205 i = be32_to_cpu(agf->agf_flfirst); 4206 4207 /* Nothing to walk in an empty AGFL. */ 4208 if (agf->agf_flcount == cpu_to_be32(0)) 4209 return 0; 4210 4211 /* Otherwise, walk from first to last, wrapping as needed. */ 4212 for (;;) { 4213 error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv); 4214 if (error) 4215 return error; 4216 if (i == be32_to_cpu(agf->agf_fllast)) 4217 break; 4218 if (++i == xfs_agfl_size(mp)) 4219 i = 0; 4220 } 4221 4222 return 0; 4223 } 4224 4225 int __init 4226 xfs_extfree_intent_init_cache(void) 4227 { 4228 xfs_extfree_item_cache = kmem_cache_create("xfs_extfree_intent", 4229 sizeof(struct xfs_extent_free_item), 4230 0, 0, NULL); 4231 4232 return xfs_extfree_item_cache != NULL ? 0 : -ENOMEM; 4233 } 4234 4235 void 4236 xfs_extfree_intent_destroy_cache(void) 4237 { 4238 kmem_cache_destroy(xfs_extfree_item_cache); 4239 xfs_extfree_item_cache = NULL; 4240 } 4241