1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * Copyright (c) 2018 Red Hat, Inc. 5 * All rights reserved. 6 */ 7 8 #include "xfs.h" 9 #include "xfs_fs.h" 10 #include "xfs_shared.h" 11 #include "xfs_format.h" 12 #include "xfs_trans_resv.h" 13 #include "xfs_bit.h" 14 #include "xfs_sb.h" 15 #include "xfs_mount.h" 16 #include "xfs_btree.h" 17 #include "xfs_alloc_btree.h" 18 #include "xfs_rmap_btree.h" 19 #include "xfs_alloc.h" 20 #include "xfs_ialloc.h" 21 #include "xfs_rmap.h" 22 #include "xfs_ag.h" 23 #include "xfs_ag_resv.h" 24 #include "xfs_health.h" 25 #include "xfs_error.h" 26 #include "xfs_bmap.h" 27 #include "xfs_defer.h" 28 #include "xfs_log_format.h" 29 #include "xfs_trans.h" 30 #include "xfs_trace.h" 31 #include "xfs_inode.h" 32 #include "xfs_icache.h" 33 34 35 /* 36 * Passive reference counting access wrappers to the perag structures. If the 37 * per-ag structure is to be freed, the freeing code is responsible for cleaning 38 * up objects with passive references before freeing the structure. This is 39 * things like cached buffers. 40 */ 41 struct xfs_perag * 42 xfs_perag_get( 43 struct xfs_mount *mp, 44 xfs_agnumber_t agno) 45 { 46 struct xfs_perag *pag; 47 48 rcu_read_lock(); 49 pag = radix_tree_lookup(&mp->m_perag_tree, agno); 50 if (pag) { 51 trace_xfs_perag_get(pag, _RET_IP_); 52 ASSERT(atomic_read(&pag->pag_ref) >= 0); 53 atomic_inc(&pag->pag_ref); 54 } 55 rcu_read_unlock(); 56 return pag; 57 } 58 59 /* 60 * search from @first to find the next perag with the given tag set. 61 */ 62 struct xfs_perag * 63 xfs_perag_get_tag( 64 struct xfs_mount *mp, 65 xfs_agnumber_t first, 66 unsigned int tag) 67 { 68 struct xfs_perag *pag; 69 int found; 70 71 rcu_read_lock(); 72 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, 73 (void **)&pag, first, 1, tag); 74 if (found <= 0) { 75 rcu_read_unlock(); 76 return NULL; 77 } 78 trace_xfs_perag_get_tag(pag, _RET_IP_); 79 atomic_inc(&pag->pag_ref); 80 rcu_read_unlock(); 81 return pag; 82 } 83 84 /* Get a passive reference to the given perag. */ 85 struct xfs_perag * 86 xfs_perag_hold( 87 struct xfs_perag *pag) 88 { 89 ASSERT(atomic_read(&pag->pag_ref) > 0 || 90 atomic_read(&pag->pag_active_ref) > 0); 91 92 trace_xfs_perag_hold(pag, _RET_IP_); 93 atomic_inc(&pag->pag_ref); 94 return pag; 95 } 96 97 void 98 xfs_perag_put( 99 struct xfs_perag *pag) 100 { 101 trace_xfs_perag_put(pag, _RET_IP_); 102 ASSERT(atomic_read(&pag->pag_ref) > 0); 103 atomic_dec(&pag->pag_ref); 104 } 105 106 /* 107 * Active references for perag structures. This is for short term access to the 108 * per ag structures for walking trees or accessing state. If an AG is being 109 * shrunk or is offline, then this will fail to find that AG and return NULL 110 * instead. 111 */ 112 struct xfs_perag * 113 xfs_perag_grab( 114 struct xfs_mount *mp, 115 xfs_agnumber_t agno) 116 { 117 struct xfs_perag *pag; 118 119 rcu_read_lock(); 120 pag = radix_tree_lookup(&mp->m_perag_tree, agno); 121 if (pag) { 122 trace_xfs_perag_grab(pag, _RET_IP_); 123 if (!atomic_inc_not_zero(&pag->pag_active_ref)) 124 pag = NULL; 125 } 126 rcu_read_unlock(); 127 return pag; 128 } 129 130 /* 131 * search from @first to find the next perag with the given tag set. 132 */ 133 struct xfs_perag * 134 xfs_perag_grab_tag( 135 struct xfs_mount *mp, 136 xfs_agnumber_t first, 137 int tag) 138 { 139 struct xfs_perag *pag; 140 int found; 141 142 rcu_read_lock(); 143 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, 144 (void **)&pag, first, 1, tag); 145 if (found <= 0) { 146 rcu_read_unlock(); 147 return NULL; 148 } 149 trace_xfs_perag_grab_tag(pag, _RET_IP_); 150 if (!atomic_inc_not_zero(&pag->pag_active_ref)) 151 pag = NULL; 152 rcu_read_unlock(); 153 return pag; 154 } 155 156 void 157 xfs_perag_rele( 158 struct xfs_perag *pag) 159 { 160 trace_xfs_perag_rele(pag, _RET_IP_); 161 if (atomic_dec_and_test(&pag->pag_active_ref)) 162 wake_up(&pag->pag_active_wq); 163 } 164 165 /* 166 * xfs_initialize_perag_data 167 * 168 * Read in each per-ag structure so we can count up the number of 169 * allocated inodes, free inodes and used filesystem blocks as this 170 * information is no longer persistent in the superblock. Once we have 171 * this information, write it into the in-core superblock structure. 172 */ 173 int 174 xfs_initialize_perag_data( 175 struct xfs_mount *mp, 176 xfs_agnumber_t agcount) 177 { 178 xfs_agnumber_t index; 179 struct xfs_perag *pag; 180 struct xfs_sb *sbp = &mp->m_sb; 181 uint64_t ifree = 0; 182 uint64_t ialloc = 0; 183 uint64_t bfree = 0; 184 uint64_t bfreelst = 0; 185 uint64_t btree = 0; 186 uint64_t fdblocks; 187 int error = 0; 188 189 for (index = 0; index < agcount; index++) { 190 /* 191 * Read the AGF and AGI buffers to populate the per-ag 192 * structures for us. 193 */ 194 pag = xfs_perag_get(mp, index); 195 error = xfs_alloc_read_agf(pag, NULL, 0, NULL); 196 if (!error) 197 error = xfs_ialloc_read_agi(pag, NULL, NULL); 198 if (error) { 199 xfs_perag_put(pag); 200 return error; 201 } 202 203 ifree += pag->pagi_freecount; 204 ialloc += pag->pagi_count; 205 bfree += pag->pagf_freeblks; 206 bfreelst += pag->pagf_flcount; 207 btree += pag->pagf_btreeblks; 208 xfs_perag_put(pag); 209 } 210 fdblocks = bfree + bfreelst + btree; 211 212 /* 213 * If the new summary counts are obviously incorrect, fail the 214 * mount operation because that implies the AGFs are also corrupt. 215 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which 216 * will prevent xfs_repair from fixing anything. 217 */ 218 if (fdblocks > sbp->sb_dblocks || ifree > ialloc) { 219 xfs_alert(mp, "AGF corruption. Please run xfs_repair."); 220 error = -EFSCORRUPTED; 221 goto out; 222 } 223 224 /* Overwrite incore superblock counters with just-read data */ 225 spin_lock(&mp->m_sb_lock); 226 sbp->sb_ifree = ifree; 227 sbp->sb_icount = ialloc; 228 sbp->sb_fdblocks = fdblocks; 229 spin_unlock(&mp->m_sb_lock); 230 231 xfs_reinit_percpu_counters(mp); 232 out: 233 xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS); 234 return error; 235 } 236 237 STATIC void 238 __xfs_free_perag( 239 struct rcu_head *head) 240 { 241 struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head); 242 243 ASSERT(!delayed_work_pending(&pag->pag_blockgc_work)); 244 kmem_free(pag); 245 } 246 247 /* 248 * Free up the per-ag resources associated with the mount structure. 249 */ 250 void 251 xfs_free_perag( 252 struct xfs_mount *mp) 253 { 254 struct xfs_perag *pag; 255 xfs_agnumber_t agno; 256 257 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 258 spin_lock(&mp->m_perag_lock); 259 pag = radix_tree_delete(&mp->m_perag_tree, agno); 260 spin_unlock(&mp->m_perag_lock); 261 ASSERT(pag); 262 XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0); 263 264 cancel_delayed_work_sync(&pag->pag_blockgc_work); 265 xfs_buf_hash_destroy(pag); 266 267 /* drop the mount's active reference */ 268 xfs_perag_rele(pag); 269 XFS_IS_CORRUPT(pag->pag_mount, 270 atomic_read(&pag->pag_active_ref) != 0); 271 call_rcu(&pag->rcu_head, __xfs_free_perag); 272 } 273 } 274 275 /* Find the size of the AG, in blocks. */ 276 static xfs_agblock_t 277 __xfs_ag_block_count( 278 struct xfs_mount *mp, 279 xfs_agnumber_t agno, 280 xfs_agnumber_t agcount, 281 xfs_rfsblock_t dblocks) 282 { 283 ASSERT(agno < agcount); 284 285 if (agno < agcount - 1) 286 return mp->m_sb.sb_agblocks; 287 return dblocks - (agno * mp->m_sb.sb_agblocks); 288 } 289 290 xfs_agblock_t 291 xfs_ag_block_count( 292 struct xfs_mount *mp, 293 xfs_agnumber_t agno) 294 { 295 return __xfs_ag_block_count(mp, agno, mp->m_sb.sb_agcount, 296 mp->m_sb.sb_dblocks); 297 } 298 299 /* Calculate the first and last possible inode number in an AG. */ 300 static void 301 __xfs_agino_range( 302 struct xfs_mount *mp, 303 xfs_agblock_t eoag, 304 xfs_agino_t *first, 305 xfs_agino_t *last) 306 { 307 xfs_agblock_t bno; 308 309 /* 310 * Calculate the first inode, which will be in the first 311 * cluster-aligned block after the AGFL. 312 */ 313 bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align); 314 *first = XFS_AGB_TO_AGINO(mp, bno); 315 316 /* 317 * Calculate the last inode, which will be at the end of the 318 * last (aligned) cluster that can be allocated in the AG. 319 */ 320 bno = round_down(eoag, M_IGEO(mp)->cluster_align); 321 *last = XFS_AGB_TO_AGINO(mp, bno) - 1; 322 } 323 324 void 325 xfs_agino_range( 326 struct xfs_mount *mp, 327 xfs_agnumber_t agno, 328 xfs_agino_t *first, 329 xfs_agino_t *last) 330 { 331 return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last); 332 } 333 334 int 335 xfs_initialize_perag( 336 struct xfs_mount *mp, 337 xfs_agnumber_t agcount, 338 xfs_rfsblock_t dblocks, 339 xfs_agnumber_t *maxagi) 340 { 341 struct xfs_perag *pag; 342 xfs_agnumber_t index; 343 xfs_agnumber_t first_initialised = NULLAGNUMBER; 344 int error; 345 346 /* 347 * Walk the current per-ag tree so we don't try to initialise AGs 348 * that already exist (growfs case). Allocate and insert all the 349 * AGs we don't find ready for initialisation. 350 */ 351 for (index = 0; index < agcount; index++) { 352 pag = xfs_perag_get(mp, index); 353 if (pag) { 354 xfs_perag_put(pag); 355 continue; 356 } 357 358 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); 359 if (!pag) { 360 error = -ENOMEM; 361 goto out_unwind_new_pags; 362 } 363 pag->pag_agno = index; 364 pag->pag_mount = mp; 365 366 error = radix_tree_preload(GFP_NOFS); 367 if (error) 368 goto out_free_pag; 369 370 spin_lock(&mp->m_perag_lock); 371 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { 372 WARN_ON_ONCE(1); 373 spin_unlock(&mp->m_perag_lock); 374 radix_tree_preload_end(); 375 error = -EEXIST; 376 goto out_free_pag; 377 } 378 spin_unlock(&mp->m_perag_lock); 379 radix_tree_preload_end(); 380 381 #ifdef __KERNEL__ 382 /* Place kernel structure only init below this point. */ 383 spin_lock_init(&pag->pag_ici_lock); 384 spin_lock_init(&pag->pagb_lock); 385 spin_lock_init(&pag->pag_state_lock); 386 INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker); 387 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); 388 init_waitqueue_head(&pag->pagb_wait); 389 init_waitqueue_head(&pag->pag_active_wq); 390 pag->pagb_count = 0; 391 pag->pagb_tree = RB_ROOT; 392 #endif /* __KERNEL__ */ 393 394 error = xfs_buf_hash_init(pag); 395 if (error) 396 goto out_remove_pag; 397 398 /* Active ref owned by mount indicates AG is online. */ 399 atomic_set(&pag->pag_active_ref, 1); 400 401 /* first new pag is fully initialized */ 402 if (first_initialised == NULLAGNUMBER) 403 first_initialised = index; 404 405 /* 406 * Pre-calculated geometry 407 */ 408 pag->block_count = __xfs_ag_block_count(mp, index, agcount, 409 dblocks); 410 pag->min_block = XFS_AGFL_BLOCK(mp); 411 __xfs_agino_range(mp, pag->block_count, &pag->agino_min, 412 &pag->agino_max); 413 } 414 415 index = xfs_set_inode_alloc(mp, agcount); 416 417 if (maxagi) 418 *maxagi = index; 419 420 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); 421 return 0; 422 423 out_remove_pag: 424 radix_tree_delete(&mp->m_perag_tree, index); 425 out_free_pag: 426 kmem_free(pag); 427 out_unwind_new_pags: 428 /* unwind any prior newly initialized pags */ 429 for (index = first_initialised; index < agcount; index++) { 430 pag = radix_tree_delete(&mp->m_perag_tree, index); 431 if (!pag) 432 break; 433 xfs_buf_hash_destroy(pag); 434 kmem_free(pag); 435 } 436 return error; 437 } 438 439 static int 440 xfs_get_aghdr_buf( 441 struct xfs_mount *mp, 442 xfs_daddr_t blkno, 443 size_t numblks, 444 struct xfs_buf **bpp, 445 const struct xfs_buf_ops *ops) 446 { 447 struct xfs_buf *bp; 448 int error; 449 450 error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp); 451 if (error) 452 return error; 453 454 bp->b_maps[0].bm_bn = blkno; 455 bp->b_ops = ops; 456 457 *bpp = bp; 458 return 0; 459 } 460 461 /* 462 * Generic btree root block init function 463 */ 464 static void 465 xfs_btroot_init( 466 struct xfs_mount *mp, 467 struct xfs_buf *bp, 468 struct aghdr_init_data *id) 469 { 470 xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno); 471 } 472 473 /* Finish initializing a free space btree. */ 474 static void 475 xfs_freesp_init_recs( 476 struct xfs_mount *mp, 477 struct xfs_buf *bp, 478 struct aghdr_init_data *id) 479 { 480 struct xfs_alloc_rec *arec; 481 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 482 483 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1); 484 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks); 485 486 if (xfs_ag_contains_log(mp, id->agno)) { 487 struct xfs_alloc_rec *nrec; 488 xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp, 489 mp->m_sb.sb_logstart); 490 491 ASSERT(start >= mp->m_ag_prealloc_blocks); 492 if (start != mp->m_ag_prealloc_blocks) { 493 /* 494 * Modify first record to pad stripe align of log 495 */ 496 arec->ar_blockcount = cpu_to_be32(start - 497 mp->m_ag_prealloc_blocks); 498 nrec = arec + 1; 499 500 /* 501 * Insert second record at start of internal log 502 * which then gets trimmed. 503 */ 504 nrec->ar_startblock = cpu_to_be32( 505 be32_to_cpu(arec->ar_startblock) + 506 be32_to_cpu(arec->ar_blockcount)); 507 arec = nrec; 508 be16_add_cpu(&block->bb_numrecs, 1); 509 } 510 /* 511 * Change record start to after the internal log 512 */ 513 be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks); 514 } 515 516 /* 517 * Calculate the record block count and check for the case where 518 * the log might have consumed all available space in the AG. If 519 * so, reset the record count to 0 to avoid exposure of an invalid 520 * record start block. 521 */ 522 arec->ar_blockcount = cpu_to_be32(id->agsize - 523 be32_to_cpu(arec->ar_startblock)); 524 if (!arec->ar_blockcount) 525 block->bb_numrecs = 0; 526 } 527 528 /* 529 * Alloc btree root block init functions 530 */ 531 static void 532 xfs_bnoroot_init( 533 struct xfs_mount *mp, 534 struct xfs_buf *bp, 535 struct aghdr_init_data *id) 536 { 537 xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno); 538 xfs_freesp_init_recs(mp, bp, id); 539 } 540 541 static void 542 xfs_cntroot_init( 543 struct xfs_mount *mp, 544 struct xfs_buf *bp, 545 struct aghdr_init_data *id) 546 { 547 xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno); 548 xfs_freesp_init_recs(mp, bp, id); 549 } 550 551 /* 552 * Reverse map root block init 553 */ 554 static void 555 xfs_rmaproot_init( 556 struct xfs_mount *mp, 557 struct xfs_buf *bp, 558 struct aghdr_init_data *id) 559 { 560 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 561 struct xfs_rmap_rec *rrec; 562 563 xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno); 564 565 /* 566 * mark the AG header regions as static metadata The BNO 567 * btree block is the first block after the headers, so 568 * it's location defines the size of region the static 569 * metadata consumes. 570 * 571 * Note: unlike mkfs, we never have to account for log 572 * space when growing the data regions 573 */ 574 rrec = XFS_RMAP_REC_ADDR(block, 1); 575 rrec->rm_startblock = 0; 576 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp)); 577 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS); 578 rrec->rm_offset = 0; 579 580 /* account freespace btree root blocks */ 581 rrec = XFS_RMAP_REC_ADDR(block, 2); 582 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp)); 583 rrec->rm_blockcount = cpu_to_be32(2); 584 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 585 rrec->rm_offset = 0; 586 587 /* account inode btree root blocks */ 588 rrec = XFS_RMAP_REC_ADDR(block, 3); 589 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp)); 590 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) - 591 XFS_IBT_BLOCK(mp)); 592 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT); 593 rrec->rm_offset = 0; 594 595 /* account for rmap btree root */ 596 rrec = XFS_RMAP_REC_ADDR(block, 4); 597 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp)); 598 rrec->rm_blockcount = cpu_to_be32(1); 599 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 600 rrec->rm_offset = 0; 601 602 /* account for refc btree root */ 603 if (xfs_has_reflink(mp)) { 604 rrec = XFS_RMAP_REC_ADDR(block, 5); 605 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp)); 606 rrec->rm_blockcount = cpu_to_be32(1); 607 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC); 608 rrec->rm_offset = 0; 609 be16_add_cpu(&block->bb_numrecs, 1); 610 } 611 612 /* account for the log space */ 613 if (xfs_ag_contains_log(mp, id->agno)) { 614 rrec = XFS_RMAP_REC_ADDR(block, 615 be16_to_cpu(block->bb_numrecs) + 1); 616 rrec->rm_startblock = cpu_to_be32( 617 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart)); 618 rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks); 619 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG); 620 rrec->rm_offset = 0; 621 be16_add_cpu(&block->bb_numrecs, 1); 622 } 623 } 624 625 /* 626 * Initialise new secondary superblocks with the pre-grow geometry, but mark 627 * them as "in progress" so we know they haven't yet been activated. This will 628 * get cleared when the update with the new geometry information is done after 629 * changes to the primary are committed. This isn't strictly necessary, but we 630 * get it for free with the delayed buffer write lists and it means we can tell 631 * if a grow operation didn't complete properly after the fact. 632 */ 633 static void 634 xfs_sbblock_init( 635 struct xfs_mount *mp, 636 struct xfs_buf *bp, 637 struct aghdr_init_data *id) 638 { 639 struct xfs_dsb *dsb = bp->b_addr; 640 641 xfs_sb_to_disk(dsb, &mp->m_sb); 642 dsb->sb_inprogress = 1; 643 } 644 645 static void 646 xfs_agfblock_init( 647 struct xfs_mount *mp, 648 struct xfs_buf *bp, 649 struct aghdr_init_data *id) 650 { 651 struct xfs_agf *agf = bp->b_addr; 652 xfs_extlen_t tmpsize; 653 654 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC); 655 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION); 656 agf->agf_seqno = cpu_to_be32(id->agno); 657 agf->agf_length = cpu_to_be32(id->agsize); 658 agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp)); 659 agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp)); 660 agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1); 661 agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1); 662 if (xfs_has_rmapbt(mp)) { 663 agf->agf_roots[XFS_BTNUM_RMAPi] = 664 cpu_to_be32(XFS_RMAP_BLOCK(mp)); 665 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1); 666 agf->agf_rmap_blocks = cpu_to_be32(1); 667 } 668 669 agf->agf_flfirst = cpu_to_be32(1); 670 agf->agf_fllast = 0; 671 agf->agf_flcount = 0; 672 tmpsize = id->agsize - mp->m_ag_prealloc_blocks; 673 agf->agf_freeblks = cpu_to_be32(tmpsize); 674 agf->agf_longest = cpu_to_be32(tmpsize); 675 if (xfs_has_crc(mp)) 676 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid); 677 if (xfs_has_reflink(mp)) { 678 agf->agf_refcount_root = cpu_to_be32( 679 xfs_refc_block(mp)); 680 agf->agf_refcount_level = cpu_to_be32(1); 681 agf->agf_refcount_blocks = cpu_to_be32(1); 682 } 683 684 if (xfs_ag_contains_log(mp, id->agno)) { 685 int64_t logblocks = mp->m_sb.sb_logblocks; 686 687 be32_add_cpu(&agf->agf_freeblks, -logblocks); 688 agf->agf_longest = cpu_to_be32(id->agsize - 689 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks); 690 } 691 } 692 693 static void 694 xfs_agflblock_init( 695 struct xfs_mount *mp, 696 struct xfs_buf *bp, 697 struct aghdr_init_data *id) 698 { 699 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp); 700 __be32 *agfl_bno; 701 int bucket; 702 703 if (xfs_has_crc(mp)) { 704 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC); 705 agfl->agfl_seqno = cpu_to_be32(id->agno); 706 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid); 707 } 708 709 agfl_bno = xfs_buf_to_agfl_bno(bp); 710 for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++) 711 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK); 712 } 713 714 static void 715 xfs_agiblock_init( 716 struct xfs_mount *mp, 717 struct xfs_buf *bp, 718 struct aghdr_init_data *id) 719 { 720 struct xfs_agi *agi = bp->b_addr; 721 int bucket; 722 723 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC); 724 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION); 725 agi->agi_seqno = cpu_to_be32(id->agno); 726 agi->agi_length = cpu_to_be32(id->agsize); 727 agi->agi_count = 0; 728 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp)); 729 agi->agi_level = cpu_to_be32(1); 730 agi->agi_freecount = 0; 731 agi->agi_newino = cpu_to_be32(NULLAGINO); 732 agi->agi_dirino = cpu_to_be32(NULLAGINO); 733 if (xfs_has_crc(mp)) 734 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid); 735 if (xfs_has_finobt(mp)) { 736 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp)); 737 agi->agi_free_level = cpu_to_be32(1); 738 } 739 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) 740 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO); 741 if (xfs_has_inobtcounts(mp)) { 742 agi->agi_iblocks = cpu_to_be32(1); 743 if (xfs_has_finobt(mp)) 744 agi->agi_fblocks = cpu_to_be32(1); 745 } 746 } 747 748 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp, 749 struct aghdr_init_data *id); 750 static int 751 xfs_ag_init_hdr( 752 struct xfs_mount *mp, 753 struct aghdr_init_data *id, 754 aghdr_init_work_f work, 755 const struct xfs_buf_ops *ops) 756 { 757 struct xfs_buf *bp; 758 int error; 759 760 error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops); 761 if (error) 762 return error; 763 764 (*work)(mp, bp, id); 765 766 xfs_buf_delwri_queue(bp, &id->buffer_list); 767 xfs_buf_relse(bp); 768 return 0; 769 } 770 771 struct xfs_aghdr_grow_data { 772 xfs_daddr_t daddr; 773 size_t numblks; 774 const struct xfs_buf_ops *ops; 775 aghdr_init_work_f work; 776 xfs_btnum_t type; 777 bool need_init; 778 }; 779 780 /* 781 * Prepare new AG headers to be written to disk. We use uncached buffers here, 782 * as it is assumed these new AG headers are currently beyond the currently 783 * valid filesystem address space. Using cached buffers would trip over EOFS 784 * corruption detection alogrithms in the buffer cache lookup routines. 785 * 786 * This is a non-transactional function, but the prepared buffers are added to a 787 * delayed write buffer list supplied by the caller so they can submit them to 788 * disk and wait on them as required. 789 */ 790 int 791 xfs_ag_init_headers( 792 struct xfs_mount *mp, 793 struct aghdr_init_data *id) 794 795 { 796 struct xfs_aghdr_grow_data aghdr_data[] = { 797 { /* SB */ 798 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR), 799 .numblks = XFS_FSS_TO_BB(mp, 1), 800 .ops = &xfs_sb_buf_ops, 801 .work = &xfs_sbblock_init, 802 .need_init = true 803 }, 804 { /* AGF */ 805 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)), 806 .numblks = XFS_FSS_TO_BB(mp, 1), 807 .ops = &xfs_agf_buf_ops, 808 .work = &xfs_agfblock_init, 809 .need_init = true 810 }, 811 { /* AGFL */ 812 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)), 813 .numblks = XFS_FSS_TO_BB(mp, 1), 814 .ops = &xfs_agfl_buf_ops, 815 .work = &xfs_agflblock_init, 816 .need_init = true 817 }, 818 { /* AGI */ 819 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)), 820 .numblks = XFS_FSS_TO_BB(mp, 1), 821 .ops = &xfs_agi_buf_ops, 822 .work = &xfs_agiblock_init, 823 .need_init = true 824 }, 825 { /* BNO root block */ 826 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)), 827 .numblks = BTOBB(mp->m_sb.sb_blocksize), 828 .ops = &xfs_bnobt_buf_ops, 829 .work = &xfs_bnoroot_init, 830 .need_init = true 831 }, 832 { /* CNT root block */ 833 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)), 834 .numblks = BTOBB(mp->m_sb.sb_blocksize), 835 .ops = &xfs_cntbt_buf_ops, 836 .work = &xfs_cntroot_init, 837 .need_init = true 838 }, 839 { /* INO root block */ 840 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)), 841 .numblks = BTOBB(mp->m_sb.sb_blocksize), 842 .ops = &xfs_inobt_buf_ops, 843 .work = &xfs_btroot_init, 844 .type = XFS_BTNUM_INO, 845 .need_init = true 846 }, 847 { /* FINO root block */ 848 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)), 849 .numblks = BTOBB(mp->m_sb.sb_blocksize), 850 .ops = &xfs_finobt_buf_ops, 851 .work = &xfs_btroot_init, 852 .type = XFS_BTNUM_FINO, 853 .need_init = xfs_has_finobt(mp) 854 }, 855 { /* RMAP root block */ 856 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)), 857 .numblks = BTOBB(mp->m_sb.sb_blocksize), 858 .ops = &xfs_rmapbt_buf_ops, 859 .work = &xfs_rmaproot_init, 860 .need_init = xfs_has_rmapbt(mp) 861 }, 862 { /* REFC root block */ 863 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)), 864 .numblks = BTOBB(mp->m_sb.sb_blocksize), 865 .ops = &xfs_refcountbt_buf_ops, 866 .work = &xfs_btroot_init, 867 .type = XFS_BTNUM_REFC, 868 .need_init = xfs_has_reflink(mp) 869 }, 870 { /* NULL terminating block */ 871 .daddr = XFS_BUF_DADDR_NULL, 872 } 873 }; 874 struct xfs_aghdr_grow_data *dp; 875 int error = 0; 876 877 /* Account for AG free space in new AG */ 878 id->nfree += id->agsize - mp->m_ag_prealloc_blocks; 879 for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) { 880 if (!dp->need_init) 881 continue; 882 883 id->daddr = dp->daddr; 884 id->numblks = dp->numblks; 885 id->type = dp->type; 886 error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops); 887 if (error) 888 break; 889 } 890 return error; 891 } 892 893 int 894 xfs_ag_shrink_space( 895 struct xfs_perag *pag, 896 struct xfs_trans **tpp, 897 xfs_extlen_t delta) 898 { 899 struct xfs_mount *mp = pag->pag_mount; 900 struct xfs_alloc_arg args = { 901 .tp = *tpp, 902 .mp = mp, 903 .pag = pag, 904 .minlen = delta, 905 .maxlen = delta, 906 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE, 907 .resv = XFS_AG_RESV_NONE, 908 .prod = 1 909 }; 910 struct xfs_buf *agibp, *agfbp; 911 struct xfs_agi *agi; 912 struct xfs_agf *agf; 913 xfs_agblock_t aglen; 914 int error, err2; 915 916 ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1); 917 error = xfs_ialloc_read_agi(pag, *tpp, &agibp); 918 if (error) 919 return error; 920 921 agi = agibp->b_addr; 922 923 error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp); 924 if (error) 925 return error; 926 927 agf = agfbp->b_addr; 928 aglen = be32_to_cpu(agi->agi_length); 929 /* some extra paranoid checks before we shrink the ag */ 930 if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length)) 931 return -EFSCORRUPTED; 932 if (delta >= aglen) 933 return -EINVAL; 934 935 /* 936 * Make sure that the last inode cluster cannot overlap with the new 937 * end of the AG, even if it's sparse. 938 */ 939 error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta); 940 if (error) 941 return error; 942 943 /* 944 * Disable perag reservations so it doesn't cause the allocation request 945 * to fail. We'll reestablish reservation before we return. 946 */ 947 error = xfs_ag_resv_free(pag); 948 if (error) 949 return error; 950 951 /* internal log shouldn't also show up in the free space btrees */ 952 error = xfs_alloc_vextent_exact_bno(&args, 953 XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta)); 954 if (!error && args.agbno == NULLAGBLOCK) 955 error = -ENOSPC; 956 957 if (error) { 958 /* 959 * if extent allocation fails, need to roll the transaction to 960 * ensure that the AGFL fixup has been committed anyway. 961 */ 962 xfs_trans_bhold(*tpp, agfbp); 963 err2 = xfs_trans_roll(tpp); 964 if (err2) 965 return err2; 966 xfs_trans_bjoin(*tpp, agfbp); 967 goto resv_init_out; 968 } 969 970 /* 971 * if successfully deleted from freespace btrees, need to confirm 972 * per-AG reservation works as expected. 973 */ 974 be32_add_cpu(&agi->agi_length, -delta); 975 be32_add_cpu(&agf->agf_length, -delta); 976 977 err2 = xfs_ag_resv_init(pag, *tpp); 978 if (err2) { 979 be32_add_cpu(&agi->agi_length, delta); 980 be32_add_cpu(&agf->agf_length, delta); 981 if (err2 != -ENOSPC) 982 goto resv_err; 983 984 __xfs_free_extent_later(*tpp, args.fsbno, delta, NULL, true); 985 986 /* 987 * Roll the transaction before trying to re-init the per-ag 988 * reservation. The new transaction is clean so it will cancel 989 * without any side effects. 990 */ 991 error = xfs_defer_finish(tpp); 992 if (error) 993 return error; 994 995 error = -ENOSPC; 996 goto resv_init_out; 997 } 998 xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH); 999 xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH); 1000 return 0; 1001 1002 resv_init_out: 1003 err2 = xfs_ag_resv_init(pag, *tpp); 1004 if (!err2) 1005 return error; 1006 resv_err: 1007 xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2); 1008 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 1009 return err2; 1010 } 1011 1012 /* 1013 * Extent the AG indicated by the @id by the length passed in 1014 */ 1015 int 1016 xfs_ag_extend_space( 1017 struct xfs_perag *pag, 1018 struct xfs_trans *tp, 1019 xfs_extlen_t len) 1020 { 1021 struct xfs_buf *bp; 1022 struct xfs_agi *agi; 1023 struct xfs_agf *agf; 1024 int error; 1025 1026 ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1); 1027 1028 error = xfs_ialloc_read_agi(pag, tp, &bp); 1029 if (error) 1030 return error; 1031 1032 agi = bp->b_addr; 1033 be32_add_cpu(&agi->agi_length, len); 1034 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH); 1035 1036 /* 1037 * Change agf length. 1038 */ 1039 error = xfs_alloc_read_agf(pag, tp, 0, &bp); 1040 if (error) 1041 return error; 1042 1043 agf = bp->b_addr; 1044 be32_add_cpu(&agf->agf_length, len); 1045 ASSERT(agf->agf_length == agi->agi_length); 1046 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH); 1047 1048 /* 1049 * Free the new space. 1050 * 1051 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that 1052 * this doesn't actually exist in the rmap btree. 1053 */ 1054 error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len, 1055 len, &XFS_RMAP_OINFO_SKIP_UPDATE); 1056 if (error) 1057 return error; 1058 1059 error = xfs_free_extent(tp, pag, be32_to_cpu(agf->agf_length) - len, 1060 len, &XFS_RMAP_OINFO_SKIP_UPDATE, XFS_AG_RESV_NONE); 1061 if (error) 1062 return error; 1063 1064 /* Update perag geometry */ 1065 pag->block_count = be32_to_cpu(agf->agf_length); 1066 __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min, 1067 &pag->agino_max); 1068 return 0; 1069 } 1070 1071 /* Retrieve AG geometry. */ 1072 int 1073 xfs_ag_get_geometry( 1074 struct xfs_perag *pag, 1075 struct xfs_ag_geometry *ageo) 1076 { 1077 struct xfs_buf *agi_bp; 1078 struct xfs_buf *agf_bp; 1079 struct xfs_agi *agi; 1080 struct xfs_agf *agf; 1081 unsigned int freeblks; 1082 int error; 1083 1084 /* Lock the AG headers. */ 1085 error = xfs_ialloc_read_agi(pag, NULL, &agi_bp); 1086 if (error) 1087 return error; 1088 error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp); 1089 if (error) 1090 goto out_agi; 1091 1092 /* Fill out form. */ 1093 memset(ageo, 0, sizeof(*ageo)); 1094 ageo->ag_number = pag->pag_agno; 1095 1096 agi = agi_bp->b_addr; 1097 ageo->ag_icount = be32_to_cpu(agi->agi_count); 1098 ageo->ag_ifree = be32_to_cpu(agi->agi_freecount); 1099 1100 agf = agf_bp->b_addr; 1101 ageo->ag_length = be32_to_cpu(agf->agf_length); 1102 freeblks = pag->pagf_freeblks + 1103 pag->pagf_flcount + 1104 pag->pagf_btreeblks - 1105 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE); 1106 ageo->ag_freeblks = freeblks; 1107 xfs_ag_geom_health(pag, ageo); 1108 1109 /* Release resources. */ 1110 xfs_buf_relse(agf_bp); 1111 out_agi: 1112 xfs_buf_relse(agi_bp); 1113 return error; 1114 } 1115