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 26 static int 27 xfs_get_aghdr_buf( 28 struct xfs_mount *mp, 29 xfs_daddr_t blkno, 30 size_t numblks, 31 struct xfs_buf **bpp, 32 const struct xfs_buf_ops *ops) 33 { 34 struct xfs_buf *bp; 35 int error; 36 37 error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp); 38 if (error) 39 return error; 40 41 xfs_buf_zero(bp, 0, BBTOB(bp->b_length)); 42 bp->b_bn = blkno; 43 bp->b_maps[0].bm_bn = blkno; 44 bp->b_ops = ops; 45 46 *bpp = bp; 47 return 0; 48 } 49 50 static inline bool is_log_ag(struct xfs_mount *mp, struct aghdr_init_data *id) 51 { 52 return mp->m_sb.sb_logstart > 0 && 53 id->agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart); 54 } 55 56 /* 57 * Generic btree root block init function 58 */ 59 static void 60 xfs_btroot_init( 61 struct xfs_mount *mp, 62 struct xfs_buf *bp, 63 struct aghdr_init_data *id) 64 { 65 xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno); 66 } 67 68 /* Finish initializing a free space btree. */ 69 static void 70 xfs_freesp_init_recs( 71 struct xfs_mount *mp, 72 struct xfs_buf *bp, 73 struct aghdr_init_data *id) 74 { 75 struct xfs_alloc_rec *arec; 76 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 77 78 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1); 79 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks); 80 81 if (is_log_ag(mp, id)) { 82 struct xfs_alloc_rec *nrec; 83 xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp, 84 mp->m_sb.sb_logstart); 85 86 ASSERT(start >= mp->m_ag_prealloc_blocks); 87 if (start != mp->m_ag_prealloc_blocks) { 88 /* 89 * Modify first record to pad stripe align of log 90 */ 91 arec->ar_blockcount = cpu_to_be32(start - 92 mp->m_ag_prealloc_blocks); 93 nrec = arec + 1; 94 95 /* 96 * Insert second record at start of internal log 97 * which then gets trimmed. 98 */ 99 nrec->ar_startblock = cpu_to_be32( 100 be32_to_cpu(arec->ar_startblock) + 101 be32_to_cpu(arec->ar_blockcount)); 102 arec = nrec; 103 be16_add_cpu(&block->bb_numrecs, 1); 104 } 105 /* 106 * Change record start to after the internal log 107 */ 108 be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks); 109 } 110 111 /* 112 * Calculate the record block count and check for the case where 113 * the log might have consumed all available space in the AG. If 114 * so, reset the record count to 0 to avoid exposure of an invalid 115 * record start block. 116 */ 117 arec->ar_blockcount = cpu_to_be32(id->agsize - 118 be32_to_cpu(arec->ar_startblock)); 119 if (!arec->ar_blockcount) 120 block->bb_numrecs = 0; 121 } 122 123 /* 124 * Alloc btree root block init functions 125 */ 126 static void 127 xfs_bnoroot_init( 128 struct xfs_mount *mp, 129 struct xfs_buf *bp, 130 struct aghdr_init_data *id) 131 { 132 xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno); 133 xfs_freesp_init_recs(mp, bp, id); 134 } 135 136 static void 137 xfs_cntroot_init( 138 struct xfs_mount *mp, 139 struct xfs_buf *bp, 140 struct aghdr_init_data *id) 141 { 142 xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno); 143 xfs_freesp_init_recs(mp, bp, id); 144 } 145 146 /* 147 * Reverse map root block init 148 */ 149 static void 150 xfs_rmaproot_init( 151 struct xfs_mount *mp, 152 struct xfs_buf *bp, 153 struct aghdr_init_data *id) 154 { 155 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 156 struct xfs_rmap_rec *rrec; 157 158 xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno); 159 160 /* 161 * mark the AG header regions as static metadata The BNO 162 * btree block is the first block after the headers, so 163 * it's location defines the size of region the static 164 * metadata consumes. 165 * 166 * Note: unlike mkfs, we never have to account for log 167 * space when growing the data regions 168 */ 169 rrec = XFS_RMAP_REC_ADDR(block, 1); 170 rrec->rm_startblock = 0; 171 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp)); 172 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS); 173 rrec->rm_offset = 0; 174 175 /* account freespace btree root blocks */ 176 rrec = XFS_RMAP_REC_ADDR(block, 2); 177 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp)); 178 rrec->rm_blockcount = cpu_to_be32(2); 179 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 180 rrec->rm_offset = 0; 181 182 /* account inode btree root blocks */ 183 rrec = XFS_RMAP_REC_ADDR(block, 3); 184 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp)); 185 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) - 186 XFS_IBT_BLOCK(mp)); 187 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT); 188 rrec->rm_offset = 0; 189 190 /* account for rmap btree root */ 191 rrec = XFS_RMAP_REC_ADDR(block, 4); 192 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp)); 193 rrec->rm_blockcount = cpu_to_be32(1); 194 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 195 rrec->rm_offset = 0; 196 197 /* account for refc btree root */ 198 if (xfs_sb_version_hasreflink(&mp->m_sb)) { 199 rrec = XFS_RMAP_REC_ADDR(block, 5); 200 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp)); 201 rrec->rm_blockcount = cpu_to_be32(1); 202 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC); 203 rrec->rm_offset = 0; 204 be16_add_cpu(&block->bb_numrecs, 1); 205 } 206 207 /* account for the log space */ 208 if (is_log_ag(mp, id)) { 209 rrec = XFS_RMAP_REC_ADDR(block, 210 be16_to_cpu(block->bb_numrecs) + 1); 211 rrec->rm_startblock = cpu_to_be32( 212 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart)); 213 rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks); 214 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG); 215 rrec->rm_offset = 0; 216 be16_add_cpu(&block->bb_numrecs, 1); 217 } 218 } 219 220 /* 221 * Initialise new secondary superblocks with the pre-grow geometry, but mark 222 * them as "in progress" so we know they haven't yet been activated. This will 223 * get cleared when the update with the new geometry information is done after 224 * changes to the primary are committed. This isn't strictly necessary, but we 225 * get it for free with the delayed buffer write lists and it means we can tell 226 * if a grow operation didn't complete properly after the fact. 227 */ 228 static void 229 xfs_sbblock_init( 230 struct xfs_mount *mp, 231 struct xfs_buf *bp, 232 struct aghdr_init_data *id) 233 { 234 struct xfs_dsb *dsb = bp->b_addr; 235 236 xfs_sb_to_disk(dsb, &mp->m_sb); 237 dsb->sb_inprogress = 1; 238 } 239 240 static void 241 xfs_agfblock_init( 242 struct xfs_mount *mp, 243 struct xfs_buf *bp, 244 struct aghdr_init_data *id) 245 { 246 struct xfs_agf *agf = bp->b_addr; 247 xfs_extlen_t tmpsize; 248 249 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC); 250 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION); 251 agf->agf_seqno = cpu_to_be32(id->agno); 252 agf->agf_length = cpu_to_be32(id->agsize); 253 agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp)); 254 agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp)); 255 agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1); 256 agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1); 257 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) { 258 agf->agf_roots[XFS_BTNUM_RMAPi] = 259 cpu_to_be32(XFS_RMAP_BLOCK(mp)); 260 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1); 261 agf->agf_rmap_blocks = cpu_to_be32(1); 262 } 263 264 agf->agf_flfirst = cpu_to_be32(1); 265 agf->agf_fllast = 0; 266 agf->agf_flcount = 0; 267 tmpsize = id->agsize - mp->m_ag_prealloc_blocks; 268 agf->agf_freeblks = cpu_to_be32(tmpsize); 269 agf->agf_longest = cpu_to_be32(tmpsize); 270 if (xfs_sb_version_hascrc(&mp->m_sb)) 271 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid); 272 if (xfs_sb_version_hasreflink(&mp->m_sb)) { 273 agf->agf_refcount_root = cpu_to_be32( 274 xfs_refc_block(mp)); 275 agf->agf_refcount_level = cpu_to_be32(1); 276 agf->agf_refcount_blocks = cpu_to_be32(1); 277 } 278 279 if (is_log_ag(mp, id)) { 280 int64_t logblocks = mp->m_sb.sb_logblocks; 281 282 be32_add_cpu(&agf->agf_freeblks, -logblocks); 283 agf->agf_longest = cpu_to_be32(id->agsize - 284 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks); 285 } 286 } 287 288 static void 289 xfs_agflblock_init( 290 struct xfs_mount *mp, 291 struct xfs_buf *bp, 292 struct aghdr_init_data *id) 293 { 294 struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp); 295 __be32 *agfl_bno; 296 int bucket; 297 298 if (xfs_sb_version_hascrc(&mp->m_sb)) { 299 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC); 300 agfl->agfl_seqno = cpu_to_be32(id->agno); 301 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid); 302 } 303 304 agfl_bno = xfs_buf_to_agfl_bno(bp); 305 for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++) 306 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK); 307 } 308 309 static void 310 xfs_agiblock_init( 311 struct xfs_mount *mp, 312 struct xfs_buf *bp, 313 struct aghdr_init_data *id) 314 { 315 struct xfs_agi *agi = bp->b_addr; 316 int bucket; 317 318 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC); 319 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION); 320 agi->agi_seqno = cpu_to_be32(id->agno); 321 agi->agi_length = cpu_to_be32(id->agsize); 322 agi->agi_count = 0; 323 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp)); 324 agi->agi_level = cpu_to_be32(1); 325 agi->agi_freecount = 0; 326 agi->agi_newino = cpu_to_be32(NULLAGINO); 327 agi->agi_dirino = cpu_to_be32(NULLAGINO); 328 if (xfs_sb_version_hascrc(&mp->m_sb)) 329 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid); 330 if (xfs_sb_version_hasfinobt(&mp->m_sb)) { 331 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp)); 332 agi->agi_free_level = cpu_to_be32(1); 333 } 334 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) 335 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO); 336 } 337 338 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp, 339 struct aghdr_init_data *id); 340 static int 341 xfs_ag_init_hdr( 342 struct xfs_mount *mp, 343 struct aghdr_init_data *id, 344 aghdr_init_work_f work, 345 const struct xfs_buf_ops *ops) 346 { 347 struct xfs_buf *bp; 348 int error; 349 350 error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops); 351 if (error) 352 return error; 353 354 (*work)(mp, bp, id); 355 356 xfs_buf_delwri_queue(bp, &id->buffer_list); 357 xfs_buf_relse(bp); 358 return 0; 359 } 360 361 struct xfs_aghdr_grow_data { 362 xfs_daddr_t daddr; 363 size_t numblks; 364 const struct xfs_buf_ops *ops; 365 aghdr_init_work_f work; 366 xfs_btnum_t type; 367 bool need_init; 368 }; 369 370 /* 371 * Prepare new AG headers to be written to disk. We use uncached buffers here, 372 * as it is assumed these new AG headers are currently beyond the currently 373 * valid filesystem address space. Using cached buffers would trip over EOFS 374 * corruption detection alogrithms in the buffer cache lookup routines. 375 * 376 * This is a non-transactional function, but the prepared buffers are added to a 377 * delayed write buffer list supplied by the caller so they can submit them to 378 * disk and wait on them as required. 379 */ 380 int 381 xfs_ag_init_headers( 382 struct xfs_mount *mp, 383 struct aghdr_init_data *id) 384 385 { 386 struct xfs_aghdr_grow_data aghdr_data[] = { 387 { /* SB */ 388 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR), 389 .numblks = XFS_FSS_TO_BB(mp, 1), 390 .ops = &xfs_sb_buf_ops, 391 .work = &xfs_sbblock_init, 392 .need_init = true 393 }, 394 { /* AGF */ 395 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)), 396 .numblks = XFS_FSS_TO_BB(mp, 1), 397 .ops = &xfs_agf_buf_ops, 398 .work = &xfs_agfblock_init, 399 .need_init = true 400 }, 401 { /* AGFL */ 402 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)), 403 .numblks = XFS_FSS_TO_BB(mp, 1), 404 .ops = &xfs_agfl_buf_ops, 405 .work = &xfs_agflblock_init, 406 .need_init = true 407 }, 408 { /* AGI */ 409 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)), 410 .numblks = XFS_FSS_TO_BB(mp, 1), 411 .ops = &xfs_agi_buf_ops, 412 .work = &xfs_agiblock_init, 413 .need_init = true 414 }, 415 { /* BNO root block */ 416 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)), 417 .numblks = BTOBB(mp->m_sb.sb_blocksize), 418 .ops = &xfs_bnobt_buf_ops, 419 .work = &xfs_bnoroot_init, 420 .need_init = true 421 }, 422 { /* CNT root block */ 423 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)), 424 .numblks = BTOBB(mp->m_sb.sb_blocksize), 425 .ops = &xfs_cntbt_buf_ops, 426 .work = &xfs_cntroot_init, 427 .need_init = true 428 }, 429 { /* INO root block */ 430 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)), 431 .numblks = BTOBB(mp->m_sb.sb_blocksize), 432 .ops = &xfs_inobt_buf_ops, 433 .work = &xfs_btroot_init, 434 .type = XFS_BTNUM_INO, 435 .need_init = true 436 }, 437 { /* FINO root block */ 438 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)), 439 .numblks = BTOBB(mp->m_sb.sb_blocksize), 440 .ops = &xfs_finobt_buf_ops, 441 .work = &xfs_btroot_init, 442 .type = XFS_BTNUM_FINO, 443 .need_init = xfs_sb_version_hasfinobt(&mp->m_sb) 444 }, 445 { /* RMAP root block */ 446 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)), 447 .numblks = BTOBB(mp->m_sb.sb_blocksize), 448 .ops = &xfs_rmapbt_buf_ops, 449 .work = &xfs_rmaproot_init, 450 .need_init = xfs_sb_version_hasrmapbt(&mp->m_sb) 451 }, 452 { /* REFC root block */ 453 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)), 454 .numblks = BTOBB(mp->m_sb.sb_blocksize), 455 .ops = &xfs_refcountbt_buf_ops, 456 .work = &xfs_btroot_init, 457 .type = XFS_BTNUM_REFC, 458 .need_init = xfs_sb_version_hasreflink(&mp->m_sb) 459 }, 460 { /* NULL terminating block */ 461 .daddr = XFS_BUF_DADDR_NULL, 462 } 463 }; 464 struct xfs_aghdr_grow_data *dp; 465 int error = 0; 466 467 /* Account for AG free space in new AG */ 468 id->nfree += id->agsize - mp->m_ag_prealloc_blocks; 469 for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) { 470 if (!dp->need_init) 471 continue; 472 473 id->daddr = dp->daddr; 474 id->numblks = dp->numblks; 475 id->type = dp->type; 476 error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops); 477 if (error) 478 break; 479 } 480 return error; 481 } 482 483 /* 484 * Extent the AG indicated by the @id by the length passed in 485 */ 486 int 487 xfs_ag_extend_space( 488 struct xfs_mount *mp, 489 struct xfs_trans *tp, 490 struct aghdr_init_data *id, 491 xfs_extlen_t len) 492 { 493 struct xfs_buf *bp; 494 struct xfs_agi *agi; 495 struct xfs_agf *agf; 496 int error; 497 498 /* 499 * Change the agi length. 500 */ 501 error = xfs_ialloc_read_agi(mp, tp, id->agno, &bp); 502 if (error) 503 return error; 504 505 agi = bp->b_addr; 506 be32_add_cpu(&agi->agi_length, len); 507 ASSERT(id->agno == mp->m_sb.sb_agcount - 1 || 508 be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks); 509 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH); 510 511 /* 512 * Change agf length. 513 */ 514 error = xfs_alloc_read_agf(mp, tp, id->agno, 0, &bp); 515 if (error) 516 return error; 517 518 agf = bp->b_addr; 519 be32_add_cpu(&agf->agf_length, len); 520 ASSERT(agf->agf_length == agi->agi_length); 521 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH); 522 523 /* 524 * Free the new space. 525 * 526 * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that 527 * this doesn't actually exist in the rmap btree. 528 */ 529 error = xfs_rmap_free(tp, bp, id->agno, 530 be32_to_cpu(agf->agf_length) - len, 531 len, &XFS_RMAP_OINFO_SKIP_UPDATE); 532 if (error) 533 return error; 534 535 return xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, id->agno, 536 be32_to_cpu(agf->agf_length) - len), 537 len, &XFS_RMAP_OINFO_SKIP_UPDATE, 538 XFS_AG_RESV_NONE); 539 } 540 541 /* Retrieve AG geometry. */ 542 int 543 xfs_ag_get_geometry( 544 struct xfs_mount *mp, 545 xfs_agnumber_t agno, 546 struct xfs_ag_geometry *ageo) 547 { 548 struct xfs_buf *agi_bp; 549 struct xfs_buf *agf_bp; 550 struct xfs_agi *agi; 551 struct xfs_agf *agf; 552 struct xfs_perag *pag; 553 unsigned int freeblks; 554 int error; 555 556 if (agno >= mp->m_sb.sb_agcount) 557 return -EINVAL; 558 559 /* Lock the AG headers. */ 560 error = xfs_ialloc_read_agi(mp, NULL, agno, &agi_bp); 561 if (error) 562 return error; 563 error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agf_bp); 564 if (error) 565 goto out_agi; 566 567 pag = agi_bp->b_pag; 568 569 /* Fill out form. */ 570 memset(ageo, 0, sizeof(*ageo)); 571 ageo->ag_number = agno; 572 573 agi = agi_bp->b_addr; 574 ageo->ag_icount = be32_to_cpu(agi->agi_count); 575 ageo->ag_ifree = be32_to_cpu(agi->agi_freecount); 576 577 agf = agf_bp->b_addr; 578 ageo->ag_length = be32_to_cpu(agf->agf_length); 579 freeblks = pag->pagf_freeblks + 580 pag->pagf_flcount + 581 pag->pagf_btreeblks - 582 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE); 583 ageo->ag_freeblks = freeblks; 584 xfs_ag_geom_health(pag, ageo); 585 586 /* Release resources. */ 587 xfs_buf_relse(agf_bp); 588 out_agi: 589 xfs_buf_relse(agi_bp); 590 return error; 591 } 592