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 xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 221 error = -EFSCORRUPTED; 222 goto out; 223 } 224 225 /* Overwrite incore superblock counters with just-read data */ 226 spin_lock(&mp->m_sb_lock); 227 sbp->sb_ifree = ifree; 228 sbp->sb_icount = ialloc; 229 sbp->sb_fdblocks = fdblocks; 230 spin_unlock(&mp->m_sb_lock); 231 232 xfs_reinit_percpu_counters(mp); 233 out: 234 xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS); 235 return error; 236 } 237 238 STATIC void 239 __xfs_free_perag( 240 struct rcu_head *head) 241 { 242 struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head); 243 244 ASSERT(!delayed_work_pending(&pag->pag_blockgc_work)); 245 kfree(pag); 246 } 247 248 /* 249 * Free up the per-ag resources associated with the mount structure. 250 */ 251 void 252 xfs_free_perag( 253 struct xfs_mount *mp) 254 { 255 struct xfs_perag *pag; 256 xfs_agnumber_t agno; 257 258 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 259 spin_lock(&mp->m_perag_lock); 260 pag = radix_tree_delete(&mp->m_perag_tree, agno); 261 spin_unlock(&mp->m_perag_lock); 262 ASSERT(pag); 263 XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0); 264 xfs_defer_drain_free(&pag->pag_intents_drain); 265 266 cancel_delayed_work_sync(&pag->pag_blockgc_work); 267 xfs_buf_cache_destroy(&pag->pag_bcache); 268 269 /* drop the mount's active reference */ 270 xfs_perag_rele(pag); 271 XFS_IS_CORRUPT(pag->pag_mount, 272 atomic_read(&pag->pag_active_ref) != 0); 273 call_rcu(&pag->rcu_head, __xfs_free_perag); 274 } 275 } 276 277 /* Find the size of the AG, in blocks. */ 278 static xfs_agblock_t 279 __xfs_ag_block_count( 280 struct xfs_mount *mp, 281 xfs_agnumber_t agno, 282 xfs_agnumber_t agcount, 283 xfs_rfsblock_t dblocks) 284 { 285 ASSERT(agno < agcount); 286 287 if (agno < agcount - 1) 288 return mp->m_sb.sb_agblocks; 289 return dblocks - (agno * mp->m_sb.sb_agblocks); 290 } 291 292 xfs_agblock_t 293 xfs_ag_block_count( 294 struct xfs_mount *mp, 295 xfs_agnumber_t agno) 296 { 297 return __xfs_ag_block_count(mp, agno, mp->m_sb.sb_agcount, 298 mp->m_sb.sb_dblocks); 299 } 300 301 /* Calculate the first and last possible inode number in an AG. */ 302 static void 303 __xfs_agino_range( 304 struct xfs_mount *mp, 305 xfs_agblock_t eoag, 306 xfs_agino_t *first, 307 xfs_agino_t *last) 308 { 309 xfs_agblock_t bno; 310 311 /* 312 * Calculate the first inode, which will be in the first 313 * cluster-aligned block after the AGFL. 314 */ 315 bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align); 316 *first = XFS_AGB_TO_AGINO(mp, bno); 317 318 /* 319 * Calculate the last inode, which will be at the end of the 320 * last (aligned) cluster that can be allocated in the AG. 321 */ 322 bno = round_down(eoag, M_IGEO(mp)->cluster_align); 323 *last = XFS_AGB_TO_AGINO(mp, bno) - 1; 324 } 325 326 void 327 xfs_agino_range( 328 struct xfs_mount *mp, 329 xfs_agnumber_t agno, 330 xfs_agino_t *first, 331 xfs_agino_t *last) 332 { 333 return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last); 334 } 335 336 /* 337 * Free perag within the specified AG range, it is only used to free unused 338 * perags under the error handling path. 339 */ 340 void 341 xfs_free_unused_perag_range( 342 struct xfs_mount *mp, 343 xfs_agnumber_t agstart, 344 xfs_agnumber_t agend) 345 { 346 struct xfs_perag *pag; 347 xfs_agnumber_t index; 348 349 for (index = agstart; index < agend; index++) { 350 spin_lock(&mp->m_perag_lock); 351 pag = radix_tree_delete(&mp->m_perag_tree, index); 352 spin_unlock(&mp->m_perag_lock); 353 if (!pag) 354 break; 355 xfs_buf_cache_destroy(&pag->pag_bcache); 356 xfs_defer_drain_free(&pag->pag_intents_drain); 357 kfree(pag); 358 } 359 } 360 361 int 362 xfs_initialize_perag( 363 struct xfs_mount *mp, 364 xfs_agnumber_t agcount, 365 xfs_rfsblock_t dblocks, 366 xfs_agnumber_t *maxagi) 367 { 368 struct xfs_perag *pag; 369 xfs_agnumber_t index; 370 xfs_agnumber_t first_initialised = NULLAGNUMBER; 371 int error; 372 373 /* 374 * Walk the current per-ag tree so we don't try to initialise AGs 375 * that already exist (growfs case). Allocate and insert all the 376 * AGs we don't find ready for initialisation. 377 */ 378 for (index = 0; index < agcount; index++) { 379 pag = xfs_perag_get(mp, index); 380 if (pag) { 381 xfs_perag_put(pag); 382 continue; 383 } 384 385 pag = kzalloc(sizeof(*pag), GFP_KERNEL | __GFP_RETRY_MAYFAIL); 386 if (!pag) { 387 error = -ENOMEM; 388 goto out_unwind_new_pags; 389 } 390 pag->pag_agno = index; 391 pag->pag_mount = mp; 392 393 error = radix_tree_preload(GFP_KERNEL | __GFP_RETRY_MAYFAIL); 394 if (error) 395 goto out_free_pag; 396 397 spin_lock(&mp->m_perag_lock); 398 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { 399 WARN_ON_ONCE(1); 400 spin_unlock(&mp->m_perag_lock); 401 radix_tree_preload_end(); 402 error = -EEXIST; 403 goto out_free_pag; 404 } 405 spin_unlock(&mp->m_perag_lock); 406 radix_tree_preload_end(); 407 408 #ifdef __KERNEL__ 409 /* Place kernel structure only init below this point. */ 410 spin_lock_init(&pag->pag_ici_lock); 411 spin_lock_init(&pag->pagb_lock); 412 spin_lock_init(&pag->pag_state_lock); 413 INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker); 414 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); 415 xfs_defer_drain_init(&pag->pag_intents_drain); 416 init_waitqueue_head(&pag->pagb_wait); 417 init_waitqueue_head(&pag->pag_active_wq); 418 pag->pagb_count = 0; 419 pag->pagb_tree = RB_ROOT; 420 #endif /* __KERNEL__ */ 421 422 error = xfs_buf_cache_init(&pag->pag_bcache); 423 if (error) 424 goto out_remove_pag; 425 426 /* Active ref owned by mount indicates AG is online. */ 427 atomic_set(&pag->pag_active_ref, 1); 428 429 /* first new pag is fully initialized */ 430 if (first_initialised == NULLAGNUMBER) 431 first_initialised = index; 432 433 /* 434 * Pre-calculated geometry 435 */ 436 pag->block_count = __xfs_ag_block_count(mp, index, agcount, 437 dblocks); 438 pag->min_block = XFS_AGFL_BLOCK(mp); 439 __xfs_agino_range(mp, pag->block_count, &pag->agino_min, 440 &pag->agino_max); 441 } 442 443 index = xfs_set_inode_alloc(mp, agcount); 444 445 if (maxagi) 446 *maxagi = index; 447 448 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); 449 return 0; 450 451 out_remove_pag: 452 xfs_defer_drain_free(&pag->pag_intents_drain); 453 spin_lock(&mp->m_perag_lock); 454 radix_tree_delete(&mp->m_perag_tree, index); 455 spin_unlock(&mp->m_perag_lock); 456 out_free_pag: 457 kfree(pag); 458 out_unwind_new_pags: 459 /* unwind any prior newly initialized pags */ 460 xfs_free_unused_perag_range(mp, first_initialised, agcount); 461 return error; 462 } 463 464 static int 465 xfs_get_aghdr_buf( 466 struct xfs_mount *mp, 467 xfs_daddr_t blkno, 468 size_t numblks, 469 struct xfs_buf **bpp, 470 const struct xfs_buf_ops *ops) 471 { 472 struct xfs_buf *bp; 473 int error; 474 475 error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp); 476 if (error) 477 return error; 478 479 bp->b_maps[0].bm_bn = blkno; 480 bp->b_ops = ops; 481 482 *bpp = bp; 483 return 0; 484 } 485 486 /* 487 * Generic btree root block init function 488 */ 489 static void 490 xfs_btroot_init( 491 struct xfs_mount *mp, 492 struct xfs_buf *bp, 493 struct aghdr_init_data *id) 494 { 495 xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno); 496 } 497 498 /* Finish initializing a free space btree. */ 499 static void 500 xfs_freesp_init_recs( 501 struct xfs_mount *mp, 502 struct xfs_buf *bp, 503 struct aghdr_init_data *id) 504 { 505 struct xfs_alloc_rec *arec; 506 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 507 508 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1); 509 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks); 510 511 if (xfs_ag_contains_log(mp, id->agno)) { 512 struct xfs_alloc_rec *nrec; 513 xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp, 514 mp->m_sb.sb_logstart); 515 516 ASSERT(start >= mp->m_ag_prealloc_blocks); 517 if (start != mp->m_ag_prealloc_blocks) { 518 /* 519 * Modify first record to pad stripe align of log and 520 * bump the record count. 521 */ 522 arec->ar_blockcount = cpu_to_be32(start - 523 mp->m_ag_prealloc_blocks); 524 be16_add_cpu(&block->bb_numrecs, 1); 525 nrec = arec + 1; 526 527 /* 528 * Insert second record at start of internal log 529 * which then gets trimmed. 530 */ 531 nrec->ar_startblock = cpu_to_be32( 532 be32_to_cpu(arec->ar_startblock) + 533 be32_to_cpu(arec->ar_blockcount)); 534 arec = nrec; 535 } 536 /* 537 * Change record start to after the internal log 538 */ 539 be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks); 540 } 541 542 /* 543 * Calculate the block count of this record; if it is nonzero, 544 * increment the record count. 545 */ 546 arec->ar_blockcount = cpu_to_be32(id->agsize - 547 be32_to_cpu(arec->ar_startblock)); 548 if (arec->ar_blockcount) 549 be16_add_cpu(&block->bb_numrecs, 1); 550 } 551 552 /* 553 * bnobt/cntbt btree root block init functions 554 */ 555 static void 556 xfs_bnoroot_init( 557 struct xfs_mount *mp, 558 struct xfs_buf *bp, 559 struct aghdr_init_data *id) 560 { 561 xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno); 562 xfs_freesp_init_recs(mp, bp, id); 563 } 564 565 /* 566 * Reverse map root block init 567 */ 568 static void 569 xfs_rmaproot_init( 570 struct xfs_mount *mp, 571 struct xfs_buf *bp, 572 struct aghdr_init_data *id) 573 { 574 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 575 struct xfs_rmap_rec *rrec; 576 577 xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 4, id->agno); 578 579 /* 580 * mark the AG header regions as static metadata The BNO 581 * btree block is the first block after the headers, so 582 * it's location defines the size of region the static 583 * metadata consumes. 584 * 585 * Note: unlike mkfs, we never have to account for log 586 * space when growing the data regions 587 */ 588 rrec = XFS_RMAP_REC_ADDR(block, 1); 589 rrec->rm_startblock = 0; 590 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp)); 591 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS); 592 rrec->rm_offset = 0; 593 594 /* account freespace btree root blocks */ 595 rrec = XFS_RMAP_REC_ADDR(block, 2); 596 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp)); 597 rrec->rm_blockcount = cpu_to_be32(2); 598 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 599 rrec->rm_offset = 0; 600 601 /* account inode btree root blocks */ 602 rrec = XFS_RMAP_REC_ADDR(block, 3); 603 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp)); 604 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) - 605 XFS_IBT_BLOCK(mp)); 606 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT); 607 rrec->rm_offset = 0; 608 609 /* account for rmap btree root */ 610 rrec = XFS_RMAP_REC_ADDR(block, 4); 611 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp)); 612 rrec->rm_blockcount = cpu_to_be32(1); 613 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 614 rrec->rm_offset = 0; 615 616 /* account for refc btree root */ 617 if (xfs_has_reflink(mp)) { 618 rrec = XFS_RMAP_REC_ADDR(block, 5); 619 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp)); 620 rrec->rm_blockcount = cpu_to_be32(1); 621 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC); 622 rrec->rm_offset = 0; 623 be16_add_cpu(&block->bb_numrecs, 1); 624 } 625 626 /* account for the log space */ 627 if (xfs_ag_contains_log(mp, id->agno)) { 628 rrec = XFS_RMAP_REC_ADDR(block, 629 be16_to_cpu(block->bb_numrecs) + 1); 630 rrec->rm_startblock = cpu_to_be32( 631 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart)); 632 rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks); 633 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG); 634 rrec->rm_offset = 0; 635 be16_add_cpu(&block->bb_numrecs, 1); 636 } 637 } 638 639 /* 640 * Initialise new secondary superblocks with the pre-grow geometry, but mark 641 * them as "in progress" so we know they haven't yet been activated. This will 642 * get cleared when the update with the new geometry information is done after 643 * changes to the primary are committed. This isn't strictly necessary, but we 644 * get it for free with the delayed buffer write lists and it means we can tell 645 * if a grow operation didn't complete properly after the fact. 646 */ 647 static void 648 xfs_sbblock_init( 649 struct xfs_mount *mp, 650 struct xfs_buf *bp, 651 struct aghdr_init_data *id) 652 { 653 struct xfs_dsb *dsb = bp->b_addr; 654 655 xfs_sb_to_disk(dsb, &mp->m_sb); 656 dsb->sb_inprogress = 1; 657 } 658 659 static void 660 xfs_agfblock_init( 661 struct xfs_mount *mp, 662 struct xfs_buf *bp, 663 struct aghdr_init_data *id) 664 { 665 struct xfs_agf *agf = bp->b_addr; 666 xfs_extlen_t tmpsize; 667 668 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC); 669 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION); 670 agf->agf_seqno = cpu_to_be32(id->agno); 671 agf->agf_length = cpu_to_be32(id->agsize); 672 agf->agf_bno_root = cpu_to_be32(XFS_BNO_BLOCK(mp)); 673 agf->agf_cnt_root = cpu_to_be32(XFS_CNT_BLOCK(mp)); 674 agf->agf_bno_level = cpu_to_be32(1); 675 agf->agf_cnt_level = cpu_to_be32(1); 676 if (xfs_has_rmapbt(mp)) { 677 agf->agf_rmap_root = cpu_to_be32(XFS_RMAP_BLOCK(mp)); 678 agf->agf_rmap_level = cpu_to_be32(1); 679 agf->agf_rmap_blocks = cpu_to_be32(1); 680 } 681 682 agf->agf_flfirst = cpu_to_be32(1); 683 agf->agf_fllast = 0; 684 agf->agf_flcount = 0; 685 tmpsize = id->agsize - mp->m_ag_prealloc_blocks; 686 agf->agf_freeblks = cpu_to_be32(tmpsize); 687 agf->agf_longest = cpu_to_be32(tmpsize); 688 if (xfs_has_crc(mp)) 689 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid); 690 if (xfs_has_reflink(mp)) { 691 agf->agf_refcount_root = cpu_to_be32( 692 xfs_refc_block(mp)); 693 agf->agf_refcount_level = cpu_to_be32(1); 694 agf->agf_refcount_blocks = cpu_to_be32(1); 695 } 696 697 if (xfs_ag_contains_log(mp, id->agno)) { 698 int64_t logblocks = mp->m_sb.sb_logblocks; 699 700 be32_add_cpu(&agf->agf_freeblks, -logblocks); 701 agf->agf_longest = cpu_to_be32(id->agsize - 702 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks); 703 } 704 } 705 706 static void 707 xfs_agflblock_init( 708 struct xfs_mount *mp, 709 struct xfs_buf *bp, 710 struct aghdr_init_data *id) 711 { 712 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp); 713 __be32 *agfl_bno; 714 int bucket; 715 716 if (xfs_has_crc(mp)) { 717 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC); 718 agfl->agfl_seqno = cpu_to_be32(id->agno); 719 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid); 720 } 721 722 agfl_bno = xfs_buf_to_agfl_bno(bp); 723 for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++) 724 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK); 725 } 726 727 static void 728 xfs_agiblock_init( 729 struct xfs_mount *mp, 730 struct xfs_buf *bp, 731 struct aghdr_init_data *id) 732 { 733 struct xfs_agi *agi = bp->b_addr; 734 int bucket; 735 736 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC); 737 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION); 738 agi->agi_seqno = cpu_to_be32(id->agno); 739 agi->agi_length = cpu_to_be32(id->agsize); 740 agi->agi_count = 0; 741 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp)); 742 agi->agi_level = cpu_to_be32(1); 743 agi->agi_freecount = 0; 744 agi->agi_newino = cpu_to_be32(NULLAGINO); 745 agi->agi_dirino = cpu_to_be32(NULLAGINO); 746 if (xfs_has_crc(mp)) 747 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid); 748 if (xfs_has_finobt(mp)) { 749 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp)); 750 agi->agi_free_level = cpu_to_be32(1); 751 } 752 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) 753 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO); 754 if (xfs_has_inobtcounts(mp)) { 755 agi->agi_iblocks = cpu_to_be32(1); 756 if (xfs_has_finobt(mp)) 757 agi->agi_fblocks = cpu_to_be32(1); 758 } 759 } 760 761 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp, 762 struct aghdr_init_data *id); 763 static int 764 xfs_ag_init_hdr( 765 struct xfs_mount *mp, 766 struct aghdr_init_data *id, 767 aghdr_init_work_f work, 768 const struct xfs_buf_ops *ops) 769 { 770 struct xfs_buf *bp; 771 int error; 772 773 error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops); 774 if (error) 775 return error; 776 777 (*work)(mp, bp, id); 778 779 xfs_buf_delwri_queue(bp, &id->buffer_list); 780 xfs_buf_relse(bp); 781 return 0; 782 } 783 784 struct xfs_aghdr_grow_data { 785 xfs_daddr_t daddr; 786 size_t numblks; 787 const struct xfs_buf_ops *ops; 788 aghdr_init_work_f work; 789 const struct xfs_btree_ops *bc_ops; 790 bool need_init; 791 }; 792 793 /* 794 * Prepare new AG headers to be written to disk. We use uncached buffers here, 795 * as it is assumed these new AG headers are currently beyond the currently 796 * valid filesystem address space. Using cached buffers would trip over EOFS 797 * corruption detection alogrithms in the buffer cache lookup routines. 798 * 799 * This is a non-transactional function, but the prepared buffers are added to a 800 * delayed write buffer list supplied by the caller so they can submit them to 801 * disk and wait on them as required. 802 */ 803 int 804 xfs_ag_init_headers( 805 struct xfs_mount *mp, 806 struct aghdr_init_data *id) 807 808 { 809 struct xfs_aghdr_grow_data aghdr_data[] = { 810 { /* SB */ 811 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR), 812 .numblks = XFS_FSS_TO_BB(mp, 1), 813 .ops = &xfs_sb_buf_ops, 814 .work = &xfs_sbblock_init, 815 .need_init = true 816 }, 817 { /* AGF */ 818 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)), 819 .numblks = XFS_FSS_TO_BB(mp, 1), 820 .ops = &xfs_agf_buf_ops, 821 .work = &xfs_agfblock_init, 822 .need_init = true 823 }, 824 { /* AGFL */ 825 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)), 826 .numblks = XFS_FSS_TO_BB(mp, 1), 827 .ops = &xfs_agfl_buf_ops, 828 .work = &xfs_agflblock_init, 829 .need_init = true 830 }, 831 { /* AGI */ 832 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)), 833 .numblks = XFS_FSS_TO_BB(mp, 1), 834 .ops = &xfs_agi_buf_ops, 835 .work = &xfs_agiblock_init, 836 .need_init = true 837 }, 838 { /* BNO root block */ 839 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)), 840 .numblks = BTOBB(mp->m_sb.sb_blocksize), 841 .ops = &xfs_bnobt_buf_ops, 842 .work = &xfs_bnoroot_init, 843 .bc_ops = &xfs_bnobt_ops, 844 .need_init = true 845 }, 846 { /* CNT root block */ 847 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)), 848 .numblks = BTOBB(mp->m_sb.sb_blocksize), 849 .ops = &xfs_cntbt_buf_ops, 850 .work = &xfs_bnoroot_init, 851 .bc_ops = &xfs_cntbt_ops, 852 .need_init = true 853 }, 854 { /* INO root block */ 855 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)), 856 .numblks = BTOBB(mp->m_sb.sb_blocksize), 857 .ops = &xfs_inobt_buf_ops, 858 .work = &xfs_btroot_init, 859 .bc_ops = &xfs_inobt_ops, 860 .need_init = true 861 }, 862 { /* FINO root block */ 863 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)), 864 .numblks = BTOBB(mp->m_sb.sb_blocksize), 865 .ops = &xfs_finobt_buf_ops, 866 .work = &xfs_btroot_init, 867 .bc_ops = &xfs_finobt_ops, 868 .need_init = xfs_has_finobt(mp) 869 }, 870 { /* RMAP root block */ 871 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)), 872 .numblks = BTOBB(mp->m_sb.sb_blocksize), 873 .ops = &xfs_rmapbt_buf_ops, 874 .work = &xfs_rmaproot_init, 875 .bc_ops = &xfs_rmapbt_ops, 876 .need_init = xfs_has_rmapbt(mp) 877 }, 878 { /* REFC root block */ 879 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)), 880 .numblks = BTOBB(mp->m_sb.sb_blocksize), 881 .ops = &xfs_refcountbt_buf_ops, 882 .work = &xfs_btroot_init, 883 .bc_ops = &xfs_refcountbt_ops, 884 .need_init = xfs_has_reflink(mp) 885 }, 886 { /* NULL terminating block */ 887 .daddr = XFS_BUF_DADDR_NULL, 888 } 889 }; 890 struct xfs_aghdr_grow_data *dp; 891 int error = 0; 892 893 /* Account for AG free space in new AG */ 894 id->nfree += id->agsize - mp->m_ag_prealloc_blocks; 895 for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) { 896 if (!dp->need_init) 897 continue; 898 899 id->daddr = dp->daddr; 900 id->numblks = dp->numblks; 901 id->bc_ops = dp->bc_ops; 902 error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops); 903 if (error) 904 break; 905 } 906 return error; 907 } 908 909 int 910 xfs_ag_shrink_space( 911 struct xfs_perag *pag, 912 struct xfs_trans **tpp, 913 xfs_extlen_t delta) 914 { 915 struct xfs_mount *mp = pag->pag_mount; 916 struct xfs_alloc_arg args = { 917 .tp = *tpp, 918 .mp = mp, 919 .pag = pag, 920 .minlen = delta, 921 .maxlen = delta, 922 .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE, 923 .resv = XFS_AG_RESV_NONE, 924 .prod = 1 925 }; 926 struct xfs_buf *agibp, *agfbp; 927 struct xfs_agi *agi; 928 struct xfs_agf *agf; 929 xfs_agblock_t aglen; 930 int error, err2; 931 932 ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1); 933 error = xfs_ialloc_read_agi(pag, *tpp, &agibp); 934 if (error) 935 return error; 936 937 agi = agibp->b_addr; 938 939 error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp); 940 if (error) 941 return error; 942 943 agf = agfbp->b_addr; 944 aglen = be32_to_cpu(agi->agi_length); 945 /* some extra paranoid checks before we shrink the ag */ 946 if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length)) { 947 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGF); 948 return -EFSCORRUPTED; 949 } 950 if (delta >= aglen) 951 return -EINVAL; 952 953 /* 954 * Make sure that the last inode cluster cannot overlap with the new 955 * end of the AG, even if it's sparse. 956 */ 957 error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta); 958 if (error) 959 return error; 960 961 /* 962 * Disable perag reservations so it doesn't cause the allocation request 963 * to fail. We'll reestablish reservation before we return. 964 */ 965 error = xfs_ag_resv_free(pag); 966 if (error) 967 return error; 968 969 /* internal log shouldn't also show up in the free space btrees */ 970 error = xfs_alloc_vextent_exact_bno(&args, 971 XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta)); 972 if (!error && args.agbno == NULLAGBLOCK) 973 error = -ENOSPC; 974 975 if (error) { 976 /* 977 * if extent allocation fails, need to roll the transaction to 978 * ensure that the AGFL fixup has been committed anyway. 979 */ 980 xfs_trans_bhold(*tpp, agfbp); 981 err2 = xfs_trans_roll(tpp); 982 if (err2) 983 return err2; 984 xfs_trans_bjoin(*tpp, agfbp); 985 goto resv_init_out; 986 } 987 988 /* 989 * if successfully deleted from freespace btrees, need to confirm 990 * per-AG reservation works as expected. 991 */ 992 be32_add_cpu(&agi->agi_length, -delta); 993 be32_add_cpu(&agf->agf_length, -delta); 994 995 err2 = xfs_ag_resv_init(pag, *tpp); 996 if (err2) { 997 be32_add_cpu(&agi->agi_length, delta); 998 be32_add_cpu(&agf->agf_length, delta); 999 if (err2 != -ENOSPC) 1000 goto resv_err; 1001 1002 err2 = xfs_free_extent_later(*tpp, args.fsbno, delta, NULL, 1003 XFS_AG_RESV_NONE, true); 1004 if (err2) 1005 goto resv_err; 1006 1007 /* 1008 * Roll the transaction before trying to re-init the per-ag 1009 * reservation. The new transaction is clean so it will cancel 1010 * without any side effects. 1011 */ 1012 error = xfs_defer_finish(tpp); 1013 if (error) 1014 return error; 1015 1016 error = -ENOSPC; 1017 goto resv_init_out; 1018 } 1019 1020 /* Update perag geometry */ 1021 pag->block_count -= delta; 1022 __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min, 1023 &pag->agino_max); 1024 1025 xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH); 1026 xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH); 1027 return 0; 1028 1029 resv_init_out: 1030 err2 = xfs_ag_resv_init(pag, *tpp); 1031 if (!err2) 1032 return error; 1033 resv_err: 1034 xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2); 1035 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 1036 return err2; 1037 } 1038 1039 /* 1040 * Extent the AG indicated by the @id by the length passed in 1041 */ 1042 int 1043 xfs_ag_extend_space( 1044 struct xfs_perag *pag, 1045 struct xfs_trans *tp, 1046 xfs_extlen_t len) 1047 { 1048 struct xfs_buf *bp; 1049 struct xfs_agi *agi; 1050 struct xfs_agf *agf; 1051 int error; 1052 1053 ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1); 1054 1055 error = xfs_ialloc_read_agi(pag, tp, &bp); 1056 if (error) 1057 return error; 1058 1059 agi = bp->b_addr; 1060 be32_add_cpu(&agi->agi_length, len); 1061 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH); 1062 1063 /* 1064 * Change agf length. 1065 */ 1066 error = xfs_alloc_read_agf(pag, tp, 0, &bp); 1067 if (error) 1068 return error; 1069 1070 agf = bp->b_addr; 1071 be32_add_cpu(&agf->agf_length, len); 1072 ASSERT(agf->agf_length == agi->agi_length); 1073 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH); 1074 1075 /* 1076 * Free the new space. 1077 * 1078 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that 1079 * this doesn't actually exist in the rmap btree. 1080 */ 1081 error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len, 1082 len, &XFS_RMAP_OINFO_SKIP_UPDATE); 1083 if (error) 1084 return error; 1085 1086 error = xfs_free_extent(tp, pag, be32_to_cpu(agf->agf_length) - len, 1087 len, &XFS_RMAP_OINFO_SKIP_UPDATE, XFS_AG_RESV_NONE); 1088 if (error) 1089 return error; 1090 1091 /* Update perag geometry */ 1092 pag->block_count = be32_to_cpu(agf->agf_length); 1093 __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min, 1094 &pag->agino_max); 1095 return 0; 1096 } 1097 1098 /* Retrieve AG geometry. */ 1099 int 1100 xfs_ag_get_geometry( 1101 struct xfs_perag *pag, 1102 struct xfs_ag_geometry *ageo) 1103 { 1104 struct xfs_buf *agi_bp; 1105 struct xfs_buf *agf_bp; 1106 struct xfs_agi *agi; 1107 struct xfs_agf *agf; 1108 unsigned int freeblks; 1109 int error; 1110 1111 /* Lock the AG headers. */ 1112 error = xfs_ialloc_read_agi(pag, NULL, &agi_bp); 1113 if (error) 1114 return error; 1115 error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp); 1116 if (error) 1117 goto out_agi; 1118 1119 /* Fill out form. */ 1120 memset(ageo, 0, sizeof(*ageo)); 1121 ageo->ag_number = pag->pag_agno; 1122 1123 agi = agi_bp->b_addr; 1124 ageo->ag_icount = be32_to_cpu(agi->agi_count); 1125 ageo->ag_ifree = be32_to_cpu(agi->agi_freecount); 1126 1127 agf = agf_bp->b_addr; 1128 ageo->ag_length = be32_to_cpu(agf->agf_length); 1129 freeblks = pag->pagf_freeblks + 1130 pag->pagf_flcount + 1131 pag->pagf_btreeblks - 1132 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE); 1133 ageo->ag_freeblks = freeblks; 1134 xfs_ag_geom_health(pag, ageo); 1135 1136 /* Release resources. */ 1137 xfs_buf_relse(agf_bp); 1138 out_agi: 1139 xfs_buf_relse(agi_bp); 1140 return error; 1141 } 1142