1b16817b6SDave Chinner /* SPDX-License-Identifier: GPL-2.0 */ 2b16817b6SDave Chinner /* 3b16817b6SDave Chinner * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4b16817b6SDave Chinner * Copyright (c) 2018 Red Hat, Inc. 5b16817b6SDave Chinner * All rights reserved. 6b16817b6SDave Chinner */ 7b16817b6SDave Chinner 8b16817b6SDave Chinner #include "xfs.h" 9b16817b6SDave Chinner #include "xfs_fs.h" 10b16817b6SDave Chinner #include "xfs_shared.h" 11b16817b6SDave Chinner #include "xfs_format.h" 12b16817b6SDave Chinner #include "xfs_trans_resv.h" 13f327a007SDarrick J. Wong #include "xfs_bit.h" 14b16817b6SDave Chinner #include "xfs_sb.h" 15b16817b6SDave Chinner #include "xfs_mount.h" 16b16817b6SDave Chinner #include "xfs_btree.h" 17b16817b6SDave Chinner #include "xfs_alloc_btree.h" 18b16817b6SDave Chinner #include "xfs_rmap_btree.h" 19b16817b6SDave Chinner #include "xfs_alloc.h" 2049dd56f2SDave Chinner #include "xfs_ialloc.h" 21b16817b6SDave Chinner #include "xfs_rmap.h" 22b16817b6SDave Chinner #include "xfs_ag.h" 237cd5006bSDarrick J. Wong #include "xfs_ag_resv.h" 241302c6a2SDarrick J. Wong #include "xfs_health.h" 2546141dc8SGao Xiang #include "xfs_error.h" 2646141dc8SGao Xiang #include "xfs_bmap.h" 2746141dc8SGao Xiang #include "xfs_defer.h" 2846141dc8SGao Xiang #include "xfs_log_format.h" 2946141dc8SGao Xiang #include "xfs_trans.h" 309bbafc71SDave Chinner #include "xfs_trace.h" 3107b6403aSDave Chinner #include "xfs_inode.h" 3207b6403aSDave Chinner #include "xfs_icache.h" 3307b6403aSDave Chinner 349bbafc71SDave Chinner 359bbafc71SDave Chinner /* 369bbafc71SDave Chinner * Passive reference counting access wrappers to the perag structures. If the 379bbafc71SDave Chinner * per-ag structure is to be freed, the freeing code is responsible for cleaning 389bbafc71SDave Chinner * up objects with passive references before freeing the structure. This is 399bbafc71SDave Chinner * things like cached buffers. 409bbafc71SDave Chinner */ 419bbafc71SDave Chinner struct xfs_perag * 429bbafc71SDave Chinner xfs_perag_get( 439bbafc71SDave Chinner struct xfs_mount *mp, 449bbafc71SDave Chinner xfs_agnumber_t agno) 459bbafc71SDave Chinner { 469bbafc71SDave Chinner struct xfs_perag *pag; 479bbafc71SDave Chinner int ref = 0; 489bbafc71SDave Chinner 499bbafc71SDave Chinner rcu_read_lock(); 509bbafc71SDave Chinner pag = radix_tree_lookup(&mp->m_perag_tree, agno); 519bbafc71SDave Chinner if (pag) { 529bbafc71SDave Chinner ASSERT(atomic_read(&pag->pag_ref) >= 0); 539bbafc71SDave Chinner ref = atomic_inc_return(&pag->pag_ref); 549bbafc71SDave Chinner } 559bbafc71SDave Chinner rcu_read_unlock(); 569bbafc71SDave Chinner trace_xfs_perag_get(mp, agno, ref, _RET_IP_); 579bbafc71SDave Chinner return pag; 589bbafc71SDave Chinner } 599bbafc71SDave Chinner 609bbafc71SDave Chinner /* 619bbafc71SDave Chinner * search from @first to find the next perag with the given tag set. 629bbafc71SDave Chinner */ 639bbafc71SDave Chinner struct xfs_perag * 649bbafc71SDave Chinner xfs_perag_get_tag( 659bbafc71SDave Chinner struct xfs_mount *mp, 669bbafc71SDave Chinner xfs_agnumber_t first, 67*ffc18582SDarrick J. Wong unsigned int tag) 689bbafc71SDave Chinner { 699bbafc71SDave Chinner struct xfs_perag *pag; 709bbafc71SDave Chinner int found; 719bbafc71SDave Chinner int ref; 729bbafc71SDave Chinner 739bbafc71SDave Chinner rcu_read_lock(); 749bbafc71SDave Chinner found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, 759bbafc71SDave Chinner (void **)&pag, first, 1, tag); 769bbafc71SDave Chinner if (found <= 0) { 779bbafc71SDave Chinner rcu_read_unlock(); 789bbafc71SDave Chinner return NULL; 799bbafc71SDave Chinner } 809bbafc71SDave Chinner ref = atomic_inc_return(&pag->pag_ref); 819bbafc71SDave Chinner rcu_read_unlock(); 829bbafc71SDave Chinner trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_); 839bbafc71SDave Chinner return pag; 849bbafc71SDave Chinner } 859bbafc71SDave Chinner 869bbafc71SDave Chinner void 879bbafc71SDave Chinner xfs_perag_put( 889bbafc71SDave Chinner struct xfs_perag *pag) 899bbafc71SDave Chinner { 909bbafc71SDave Chinner int ref; 919bbafc71SDave Chinner 929bbafc71SDave Chinner ASSERT(atomic_read(&pag->pag_ref) > 0); 939bbafc71SDave Chinner ref = atomic_dec_return(&pag->pag_ref); 949bbafc71SDave Chinner trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_); 959bbafc71SDave Chinner } 969bbafc71SDave Chinner 979bbafc71SDave Chinner /* 989bbafc71SDave Chinner * xfs_initialize_perag_data 999bbafc71SDave Chinner * 1009bbafc71SDave Chinner * Read in each per-ag structure so we can count up the number of 1019bbafc71SDave Chinner * allocated inodes, free inodes and used filesystem blocks as this 1029bbafc71SDave Chinner * information is no longer persistent in the superblock. Once we have 1039bbafc71SDave Chinner * this information, write it into the in-core superblock structure. 1049bbafc71SDave Chinner */ 1059bbafc71SDave Chinner int 1069bbafc71SDave Chinner xfs_initialize_perag_data( 1079bbafc71SDave Chinner struct xfs_mount *mp, 1089bbafc71SDave Chinner xfs_agnumber_t agcount) 1099bbafc71SDave Chinner { 1109bbafc71SDave Chinner xfs_agnumber_t index; 11150920116SDave Chinner struct xfs_perag *pag; 11250920116SDave Chinner struct xfs_sb *sbp = &mp->m_sb; 1139bbafc71SDave Chinner uint64_t ifree = 0; 1149bbafc71SDave Chinner uint64_t ialloc = 0; 1159bbafc71SDave Chinner uint64_t bfree = 0; 1169bbafc71SDave Chinner uint64_t bfreelst = 0; 1179bbafc71SDave Chinner uint64_t btree = 0; 1189bbafc71SDave Chinner uint64_t fdblocks; 1199bbafc71SDave Chinner int error = 0; 1209bbafc71SDave Chinner 1219bbafc71SDave Chinner for (index = 0; index < agcount; index++) { 1229bbafc71SDave Chinner /* 1239bbafc71SDave Chinner * read the agf, then the agi. This gets us 1249bbafc71SDave Chinner * all the information we need and populates the 1259bbafc71SDave Chinner * per-ag structures for us. 1269bbafc71SDave Chinner */ 1279bbafc71SDave Chinner error = xfs_alloc_pagf_init(mp, NULL, index, 0); 1289bbafc71SDave Chinner if (error) 1299bbafc71SDave Chinner return error; 1309bbafc71SDave Chinner 1319bbafc71SDave Chinner error = xfs_ialloc_pagi_init(mp, NULL, index); 1329bbafc71SDave Chinner if (error) 1339bbafc71SDave Chinner return error; 1349bbafc71SDave Chinner pag = xfs_perag_get(mp, index); 1359bbafc71SDave Chinner ifree += pag->pagi_freecount; 1369bbafc71SDave Chinner ialloc += pag->pagi_count; 1379bbafc71SDave Chinner bfree += pag->pagf_freeblks; 1389bbafc71SDave Chinner bfreelst += pag->pagf_flcount; 1399bbafc71SDave Chinner btree += pag->pagf_btreeblks; 1409bbafc71SDave Chinner xfs_perag_put(pag); 1419bbafc71SDave Chinner } 1429bbafc71SDave Chinner fdblocks = bfree + bfreelst + btree; 1439bbafc71SDave Chinner 1449bbafc71SDave Chinner /* 1459bbafc71SDave Chinner * If the new summary counts are obviously incorrect, fail the 1469bbafc71SDave Chinner * mount operation because that implies the AGFs are also corrupt. 1479bbafc71SDave Chinner * Clear FS_COUNTERS so that we don't unmount with a dirty log, which 1489bbafc71SDave Chinner * will prevent xfs_repair from fixing anything. 1499bbafc71SDave Chinner */ 1509bbafc71SDave Chinner if (fdblocks > sbp->sb_dblocks || ifree > ialloc) { 1519bbafc71SDave Chinner xfs_alert(mp, "AGF corruption. Please run xfs_repair."); 1529bbafc71SDave Chinner error = -EFSCORRUPTED; 1539bbafc71SDave Chinner goto out; 1549bbafc71SDave Chinner } 1559bbafc71SDave Chinner 1569bbafc71SDave Chinner /* Overwrite incore superblock counters with just-read data */ 1579bbafc71SDave Chinner spin_lock(&mp->m_sb_lock); 1589bbafc71SDave Chinner sbp->sb_ifree = ifree; 1599bbafc71SDave Chinner sbp->sb_icount = ialloc; 1609bbafc71SDave Chinner sbp->sb_fdblocks = fdblocks; 1619bbafc71SDave Chinner spin_unlock(&mp->m_sb_lock); 1629bbafc71SDave Chinner 1639bbafc71SDave Chinner xfs_reinit_percpu_counters(mp); 1649bbafc71SDave Chinner out: 1659bbafc71SDave Chinner xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS); 1669bbafc71SDave Chinner return error; 1679bbafc71SDave Chinner } 168b16817b6SDave Chinner 16907b6403aSDave Chinner STATIC void 17007b6403aSDave Chinner __xfs_free_perag( 17107b6403aSDave Chinner struct rcu_head *head) 17207b6403aSDave Chinner { 17307b6403aSDave Chinner struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head); 17407b6403aSDave Chinner 17507b6403aSDave Chinner ASSERT(!delayed_work_pending(&pag->pag_blockgc_work)); 17607b6403aSDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 17707b6403aSDave Chinner kmem_free(pag); 17807b6403aSDave Chinner } 17907b6403aSDave Chinner 18007b6403aSDave Chinner /* 18107b6403aSDave Chinner * Free up the per-ag resources associated with the mount structure. 18207b6403aSDave Chinner */ 18307b6403aSDave Chinner void 18407b6403aSDave Chinner xfs_free_perag( 18507b6403aSDave Chinner struct xfs_mount *mp) 18607b6403aSDave Chinner { 18707b6403aSDave Chinner struct xfs_perag *pag; 18807b6403aSDave Chinner xfs_agnumber_t agno; 18907b6403aSDave Chinner 19007b6403aSDave Chinner for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 19107b6403aSDave Chinner spin_lock(&mp->m_perag_lock); 19207b6403aSDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, agno); 19307b6403aSDave Chinner spin_unlock(&mp->m_perag_lock); 19407b6403aSDave Chinner ASSERT(pag); 19507b6403aSDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 19607b6403aSDave Chinner 19707b6403aSDave Chinner cancel_delayed_work_sync(&pag->pag_blockgc_work); 19807b6403aSDave Chinner xfs_iunlink_destroy(pag); 19907b6403aSDave Chinner xfs_buf_hash_destroy(pag); 20007b6403aSDave Chinner 20107b6403aSDave Chinner call_rcu(&pag->rcu_head, __xfs_free_perag); 20207b6403aSDave Chinner } 20307b6403aSDave Chinner } 20407b6403aSDave Chinner 20507b6403aSDave Chinner int 20607b6403aSDave Chinner xfs_initialize_perag( 20707b6403aSDave Chinner struct xfs_mount *mp, 20807b6403aSDave Chinner xfs_agnumber_t agcount, 20907b6403aSDave Chinner xfs_agnumber_t *maxagi) 21007b6403aSDave Chinner { 21107b6403aSDave Chinner struct xfs_perag *pag; 21207b6403aSDave Chinner xfs_agnumber_t index; 21307b6403aSDave Chinner xfs_agnumber_t first_initialised = NULLAGNUMBER; 21407b6403aSDave Chinner int error; 21507b6403aSDave Chinner 21607b6403aSDave Chinner /* 21707b6403aSDave Chinner * Walk the current per-ag tree so we don't try to initialise AGs 21807b6403aSDave Chinner * that already exist (growfs case). Allocate and insert all the 21907b6403aSDave Chinner * AGs we don't find ready for initialisation. 22007b6403aSDave Chinner */ 22107b6403aSDave Chinner for (index = 0; index < agcount; index++) { 22207b6403aSDave Chinner pag = xfs_perag_get(mp, index); 22307b6403aSDave Chinner if (pag) { 22407b6403aSDave Chinner xfs_perag_put(pag); 22507b6403aSDave Chinner continue; 22607b6403aSDave Chinner } 22707b6403aSDave Chinner 22807b6403aSDave Chinner pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); 22907b6403aSDave Chinner if (!pag) { 23007b6403aSDave Chinner error = -ENOMEM; 23107b6403aSDave Chinner goto out_unwind_new_pags; 23207b6403aSDave Chinner } 23307b6403aSDave Chinner pag->pag_agno = index; 23407b6403aSDave Chinner pag->pag_mount = mp; 23507b6403aSDave Chinner 23607b6403aSDave Chinner error = radix_tree_preload(GFP_NOFS); 23707b6403aSDave Chinner if (error) 23807b6403aSDave Chinner goto out_free_pag; 23907b6403aSDave Chinner 24007b6403aSDave Chinner spin_lock(&mp->m_perag_lock); 24107b6403aSDave Chinner if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { 24207b6403aSDave Chinner WARN_ON_ONCE(1); 24307b6403aSDave Chinner spin_unlock(&mp->m_perag_lock); 24407b6403aSDave Chinner radix_tree_preload_end(); 24507b6403aSDave Chinner error = -EEXIST; 24607b6403aSDave Chinner goto out_free_pag; 24707b6403aSDave Chinner } 24807b6403aSDave Chinner spin_unlock(&mp->m_perag_lock); 24907b6403aSDave Chinner radix_tree_preload_end(); 25007b6403aSDave Chinner 25107b6403aSDave Chinner /* Place kernel structure only init below this point. */ 25207b6403aSDave Chinner spin_lock_init(&pag->pag_ici_lock); 25307b6403aSDave Chinner spin_lock_init(&pag->pagb_lock); 25407b6403aSDave Chinner spin_lock_init(&pag->pag_state_lock); 25507b6403aSDave Chinner INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker); 25607b6403aSDave Chinner INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); 25707b6403aSDave Chinner init_waitqueue_head(&pag->pagb_wait); 25807b6403aSDave Chinner pag->pagb_count = 0; 25907b6403aSDave Chinner pag->pagb_tree = RB_ROOT; 26007b6403aSDave Chinner 26107b6403aSDave Chinner error = xfs_buf_hash_init(pag); 26207b6403aSDave Chinner if (error) 26307b6403aSDave Chinner goto out_remove_pag; 26407b6403aSDave Chinner 26507b6403aSDave Chinner error = xfs_iunlink_init(pag); 26607b6403aSDave Chinner if (error) 26707b6403aSDave Chinner goto out_hash_destroy; 26807b6403aSDave Chinner 26907b6403aSDave Chinner /* first new pag is fully initialized */ 27007b6403aSDave Chinner if (first_initialised == NULLAGNUMBER) 27107b6403aSDave Chinner first_initialised = index; 27207b6403aSDave Chinner } 27307b6403aSDave Chinner 27407b6403aSDave Chinner index = xfs_set_inode_alloc(mp, agcount); 27507b6403aSDave Chinner 27607b6403aSDave Chinner if (maxagi) 27707b6403aSDave Chinner *maxagi = index; 27807b6403aSDave Chinner 27907b6403aSDave Chinner mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); 28007b6403aSDave Chinner return 0; 28107b6403aSDave Chinner 28207b6403aSDave Chinner out_hash_destroy: 28307b6403aSDave Chinner xfs_buf_hash_destroy(pag); 28407b6403aSDave Chinner out_remove_pag: 28507b6403aSDave Chinner radix_tree_delete(&mp->m_perag_tree, index); 28607b6403aSDave Chinner out_free_pag: 28707b6403aSDave Chinner kmem_free(pag); 28807b6403aSDave Chinner out_unwind_new_pags: 28907b6403aSDave Chinner /* unwind any prior newly initialized pags */ 29007b6403aSDave Chinner for (index = first_initialised; index < agcount; index++) { 29107b6403aSDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, index); 29207b6403aSDave Chinner if (!pag) 29307b6403aSDave Chinner break; 29407b6403aSDave Chinner xfs_buf_hash_destroy(pag); 29507b6403aSDave Chinner xfs_iunlink_destroy(pag); 29607b6403aSDave Chinner kmem_free(pag); 29707b6403aSDave Chinner } 29807b6403aSDave Chinner return error; 29907b6403aSDave Chinner } 300b16817b6SDave Chinner 3012842b6dbSDarrick J. Wong static int 302b16817b6SDave Chinner xfs_get_aghdr_buf( 303b16817b6SDave Chinner struct xfs_mount *mp, 304b16817b6SDave Chinner xfs_daddr_t blkno, 305b16817b6SDave Chinner size_t numblks, 3062842b6dbSDarrick J. Wong struct xfs_buf **bpp, 307b16817b6SDave Chinner const struct xfs_buf_ops *ops) 308b16817b6SDave Chinner { 309b16817b6SDave Chinner struct xfs_buf *bp; 3102842b6dbSDarrick J. Wong int error; 311b16817b6SDave Chinner 3122842b6dbSDarrick J. Wong error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp); 3132842b6dbSDarrick J. Wong if (error) 3142842b6dbSDarrick J. Wong return error; 315b16817b6SDave Chinner 316b16817b6SDave Chinner bp->b_bn = blkno; 317b16817b6SDave Chinner bp->b_maps[0].bm_bn = blkno; 318b16817b6SDave Chinner bp->b_ops = ops; 319b16817b6SDave Chinner 3202842b6dbSDarrick J. Wong *bpp = bp; 3212842b6dbSDarrick J. Wong return 0; 322b16817b6SDave Chinner } 323b16817b6SDave Chinner 324f327a007SDarrick J. Wong static inline bool is_log_ag(struct xfs_mount *mp, struct aghdr_init_data *id) 325f327a007SDarrick J. Wong { 326f327a007SDarrick J. Wong return mp->m_sb.sb_logstart > 0 && 327f327a007SDarrick J. Wong id->agno == XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart); 328f327a007SDarrick J. Wong } 329f327a007SDarrick J. Wong 330b16817b6SDave Chinner /* 331b16817b6SDave Chinner * Generic btree root block init function 332b16817b6SDave Chinner */ 333b16817b6SDave Chinner static void 334b16817b6SDave Chinner xfs_btroot_init( 335b16817b6SDave Chinner struct xfs_mount *mp, 336b16817b6SDave Chinner struct xfs_buf *bp, 337b16817b6SDave Chinner struct aghdr_init_data *id) 338b16817b6SDave Chinner { 339f5b999c0SEric Sandeen xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno); 340b16817b6SDave Chinner } 341b16817b6SDave Chinner 3428d90857cSDarrick J. Wong /* Finish initializing a free space btree. */ 3438d90857cSDarrick J. Wong static void 3448d90857cSDarrick J. Wong xfs_freesp_init_recs( 3458d90857cSDarrick J. Wong struct xfs_mount *mp, 3468d90857cSDarrick J. Wong struct xfs_buf *bp, 3478d90857cSDarrick J. Wong struct aghdr_init_data *id) 3488d90857cSDarrick J. Wong { 3498d90857cSDarrick J. Wong struct xfs_alloc_rec *arec; 350f327a007SDarrick J. Wong struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 3518d90857cSDarrick J. Wong 3528d90857cSDarrick J. Wong arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1); 3538d90857cSDarrick J. Wong arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks); 354f327a007SDarrick J. Wong 355f327a007SDarrick J. Wong if (is_log_ag(mp, id)) { 356f327a007SDarrick J. Wong struct xfs_alloc_rec *nrec; 357f327a007SDarrick J. Wong xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp, 358f327a007SDarrick J. Wong mp->m_sb.sb_logstart); 359f327a007SDarrick J. Wong 360f327a007SDarrick J. Wong ASSERT(start >= mp->m_ag_prealloc_blocks); 361f327a007SDarrick J. Wong if (start != mp->m_ag_prealloc_blocks) { 362f327a007SDarrick J. Wong /* 363f327a007SDarrick J. Wong * Modify first record to pad stripe align of log 364f327a007SDarrick J. Wong */ 365f327a007SDarrick J. Wong arec->ar_blockcount = cpu_to_be32(start - 366f327a007SDarrick J. Wong mp->m_ag_prealloc_blocks); 367f327a007SDarrick J. Wong nrec = arec + 1; 368f327a007SDarrick J. Wong 369f327a007SDarrick J. Wong /* 370f327a007SDarrick J. Wong * Insert second record at start of internal log 371f327a007SDarrick J. Wong * which then gets trimmed. 372f327a007SDarrick J. Wong */ 373f327a007SDarrick J. Wong nrec->ar_startblock = cpu_to_be32( 374f327a007SDarrick J. Wong be32_to_cpu(arec->ar_startblock) + 375f327a007SDarrick J. Wong be32_to_cpu(arec->ar_blockcount)); 376f327a007SDarrick J. Wong arec = nrec; 377f327a007SDarrick J. Wong be16_add_cpu(&block->bb_numrecs, 1); 378f327a007SDarrick J. Wong } 379f327a007SDarrick J. Wong /* 380f327a007SDarrick J. Wong * Change record start to after the internal log 381f327a007SDarrick J. Wong */ 382f327a007SDarrick J. Wong be32_add_cpu(&arec->ar_startblock, mp->m_sb.sb_logblocks); 383f327a007SDarrick J. Wong } 384f327a007SDarrick J. Wong 385f327a007SDarrick J. Wong /* 386f327a007SDarrick J. Wong * Calculate the record block count and check for the case where 387f327a007SDarrick J. Wong * the log might have consumed all available space in the AG. If 388f327a007SDarrick J. Wong * so, reset the record count to 0 to avoid exposure of an invalid 389f327a007SDarrick J. Wong * record start block. 390f327a007SDarrick J. Wong */ 3918d90857cSDarrick J. Wong arec->ar_blockcount = cpu_to_be32(id->agsize - 3928d90857cSDarrick J. Wong be32_to_cpu(arec->ar_startblock)); 393f327a007SDarrick J. Wong if (!arec->ar_blockcount) 394f327a007SDarrick J. Wong block->bb_numrecs = 0; 3958d90857cSDarrick J. Wong } 3968d90857cSDarrick J. Wong 397b16817b6SDave Chinner /* 398b16817b6SDave Chinner * Alloc btree root block init functions 399b16817b6SDave Chinner */ 400b16817b6SDave Chinner static void 401b16817b6SDave Chinner xfs_bnoroot_init( 402b16817b6SDave Chinner struct xfs_mount *mp, 403b16817b6SDave Chinner struct xfs_buf *bp, 404b16817b6SDave Chinner struct aghdr_init_data *id) 405b16817b6SDave Chinner { 406f5b999c0SEric Sandeen xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno); 4078d90857cSDarrick J. Wong xfs_freesp_init_recs(mp, bp, id); 408b16817b6SDave Chinner } 409b16817b6SDave Chinner 410b16817b6SDave Chinner static void 411b16817b6SDave Chinner xfs_cntroot_init( 412b16817b6SDave Chinner struct xfs_mount *mp, 413b16817b6SDave Chinner struct xfs_buf *bp, 414b16817b6SDave Chinner struct aghdr_init_data *id) 415b16817b6SDave Chinner { 416f5b999c0SEric Sandeen xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno); 4178d90857cSDarrick J. Wong xfs_freesp_init_recs(mp, bp, id); 418b16817b6SDave Chinner } 419b16817b6SDave Chinner 420b16817b6SDave Chinner /* 421b16817b6SDave Chinner * Reverse map root block init 422b16817b6SDave Chinner */ 423b16817b6SDave Chinner static void 424b16817b6SDave Chinner xfs_rmaproot_init( 425b16817b6SDave Chinner struct xfs_mount *mp, 426b16817b6SDave Chinner struct xfs_buf *bp, 427b16817b6SDave Chinner struct aghdr_init_data *id) 428b16817b6SDave Chinner { 429b16817b6SDave Chinner struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); 430b16817b6SDave Chinner struct xfs_rmap_rec *rrec; 431b16817b6SDave Chinner 432f5b999c0SEric Sandeen xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno); 433b16817b6SDave Chinner 434b16817b6SDave Chinner /* 435b16817b6SDave Chinner * mark the AG header regions as static metadata The BNO 436b16817b6SDave Chinner * btree block is the first block after the headers, so 437b16817b6SDave Chinner * it's location defines the size of region the static 438b16817b6SDave Chinner * metadata consumes. 439b16817b6SDave Chinner * 440b16817b6SDave Chinner * Note: unlike mkfs, we never have to account for log 441b16817b6SDave Chinner * space when growing the data regions 442b16817b6SDave Chinner */ 443b16817b6SDave Chinner rrec = XFS_RMAP_REC_ADDR(block, 1); 444b16817b6SDave Chinner rrec->rm_startblock = 0; 445b16817b6SDave Chinner rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp)); 446b16817b6SDave Chinner rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS); 447b16817b6SDave Chinner rrec->rm_offset = 0; 448b16817b6SDave Chinner 449b16817b6SDave Chinner /* account freespace btree root blocks */ 450b16817b6SDave Chinner rrec = XFS_RMAP_REC_ADDR(block, 2); 451b16817b6SDave Chinner rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp)); 452b16817b6SDave Chinner rrec->rm_blockcount = cpu_to_be32(2); 453b16817b6SDave Chinner rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 454b16817b6SDave Chinner rrec->rm_offset = 0; 455b16817b6SDave Chinner 456b16817b6SDave Chinner /* account inode btree root blocks */ 457b16817b6SDave Chinner rrec = XFS_RMAP_REC_ADDR(block, 3); 458b16817b6SDave Chinner rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp)); 459b16817b6SDave Chinner rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) - 460b16817b6SDave Chinner XFS_IBT_BLOCK(mp)); 461b16817b6SDave Chinner rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT); 462b16817b6SDave Chinner rrec->rm_offset = 0; 463b16817b6SDave Chinner 464b16817b6SDave Chinner /* account for rmap btree root */ 465b16817b6SDave Chinner rrec = XFS_RMAP_REC_ADDR(block, 4); 466b16817b6SDave Chinner rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp)); 467b16817b6SDave Chinner rrec->rm_blockcount = cpu_to_be32(1); 468b16817b6SDave Chinner rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG); 469b16817b6SDave Chinner rrec->rm_offset = 0; 470b16817b6SDave Chinner 471b16817b6SDave Chinner /* account for refc btree root */ 472b16817b6SDave Chinner if (xfs_sb_version_hasreflink(&mp->m_sb)) { 473b16817b6SDave Chinner rrec = XFS_RMAP_REC_ADDR(block, 5); 474b16817b6SDave Chinner rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp)); 475b16817b6SDave Chinner rrec->rm_blockcount = cpu_to_be32(1); 476b16817b6SDave Chinner rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC); 477b16817b6SDave Chinner rrec->rm_offset = 0; 478b16817b6SDave Chinner be16_add_cpu(&block->bb_numrecs, 1); 479b16817b6SDave Chinner } 480f327a007SDarrick J. Wong 481f327a007SDarrick J. Wong /* account for the log space */ 482f327a007SDarrick J. Wong if (is_log_ag(mp, id)) { 483f327a007SDarrick J. Wong rrec = XFS_RMAP_REC_ADDR(block, 484f327a007SDarrick J. Wong be16_to_cpu(block->bb_numrecs) + 1); 485f327a007SDarrick J. Wong rrec->rm_startblock = cpu_to_be32( 486f327a007SDarrick J. Wong XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart)); 487f327a007SDarrick J. Wong rrec->rm_blockcount = cpu_to_be32(mp->m_sb.sb_logblocks); 488f327a007SDarrick J. Wong rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG); 489f327a007SDarrick J. Wong rrec->rm_offset = 0; 490f327a007SDarrick J. Wong be16_add_cpu(&block->bb_numrecs, 1); 491f327a007SDarrick J. Wong } 492b16817b6SDave Chinner } 493b16817b6SDave Chinner 494b16817b6SDave Chinner /* 495b16817b6SDave Chinner * Initialise new secondary superblocks with the pre-grow geometry, but mark 496b16817b6SDave Chinner * them as "in progress" so we know they haven't yet been activated. This will 497b16817b6SDave Chinner * get cleared when the update with the new geometry information is done after 498b16817b6SDave Chinner * changes to the primary are committed. This isn't strictly necessary, but we 499b16817b6SDave Chinner * get it for free with the delayed buffer write lists and it means we can tell 500b16817b6SDave Chinner * if a grow operation didn't complete properly after the fact. 501b16817b6SDave Chinner */ 502b16817b6SDave Chinner static void 503b16817b6SDave Chinner xfs_sbblock_init( 504b16817b6SDave Chinner struct xfs_mount *mp, 505b16817b6SDave Chinner struct xfs_buf *bp, 506b16817b6SDave Chinner struct aghdr_init_data *id) 507b16817b6SDave Chinner { 5083e6e8afdSChristoph Hellwig struct xfs_dsb *dsb = bp->b_addr; 509b16817b6SDave Chinner 510b16817b6SDave Chinner xfs_sb_to_disk(dsb, &mp->m_sb); 511b16817b6SDave Chinner dsb->sb_inprogress = 1; 512b16817b6SDave Chinner } 513b16817b6SDave Chinner 514b16817b6SDave Chinner static void 515b16817b6SDave Chinner xfs_agfblock_init( 516b16817b6SDave Chinner struct xfs_mount *mp, 517b16817b6SDave Chinner struct xfs_buf *bp, 518b16817b6SDave Chinner struct aghdr_init_data *id) 519b16817b6SDave Chinner { 5209798f615SChristoph Hellwig struct xfs_agf *agf = bp->b_addr; 521b16817b6SDave Chinner xfs_extlen_t tmpsize; 522b16817b6SDave Chinner 523b16817b6SDave Chinner agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC); 524b16817b6SDave Chinner agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION); 525b16817b6SDave Chinner agf->agf_seqno = cpu_to_be32(id->agno); 526b16817b6SDave Chinner agf->agf_length = cpu_to_be32(id->agsize); 527b16817b6SDave Chinner agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp)); 528b16817b6SDave Chinner agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp)); 529b16817b6SDave Chinner agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1); 530b16817b6SDave Chinner agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1); 531b16817b6SDave Chinner if (xfs_sb_version_hasrmapbt(&mp->m_sb)) { 532b16817b6SDave Chinner agf->agf_roots[XFS_BTNUM_RMAPi] = 533b16817b6SDave Chinner cpu_to_be32(XFS_RMAP_BLOCK(mp)); 534b16817b6SDave Chinner agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1); 535b16817b6SDave Chinner agf->agf_rmap_blocks = cpu_to_be32(1); 536b16817b6SDave Chinner } 537b16817b6SDave Chinner 538b16817b6SDave Chinner agf->agf_flfirst = cpu_to_be32(1); 539b16817b6SDave Chinner agf->agf_fllast = 0; 540b16817b6SDave Chinner agf->agf_flcount = 0; 541b16817b6SDave Chinner tmpsize = id->agsize - mp->m_ag_prealloc_blocks; 542b16817b6SDave Chinner agf->agf_freeblks = cpu_to_be32(tmpsize); 543b16817b6SDave Chinner agf->agf_longest = cpu_to_be32(tmpsize); 544b16817b6SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) 545b16817b6SDave Chinner uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid); 546b16817b6SDave Chinner if (xfs_sb_version_hasreflink(&mp->m_sb)) { 547b16817b6SDave Chinner agf->agf_refcount_root = cpu_to_be32( 548b16817b6SDave Chinner xfs_refc_block(mp)); 549b16817b6SDave Chinner agf->agf_refcount_level = cpu_to_be32(1); 550b16817b6SDave Chinner agf->agf_refcount_blocks = cpu_to_be32(1); 551b16817b6SDave Chinner } 552f327a007SDarrick J. Wong 553f327a007SDarrick J. Wong if (is_log_ag(mp, id)) { 554f327a007SDarrick J. Wong int64_t logblocks = mp->m_sb.sb_logblocks; 555f327a007SDarrick J. Wong 556f327a007SDarrick J. Wong be32_add_cpu(&agf->agf_freeblks, -logblocks); 557f327a007SDarrick J. Wong agf->agf_longest = cpu_to_be32(id->agsize - 558f327a007SDarrick J. Wong XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart) - logblocks); 559f327a007SDarrick J. Wong } 560b16817b6SDave Chinner } 561b16817b6SDave Chinner 562b16817b6SDave Chinner static void 563b16817b6SDave Chinner xfs_agflblock_init( 564b16817b6SDave Chinner struct xfs_mount *mp, 565b16817b6SDave Chinner struct xfs_buf *bp, 566b16817b6SDave Chinner struct aghdr_init_data *id) 567b16817b6SDave Chinner { 568b16817b6SDave Chinner struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp); 569b16817b6SDave Chinner __be32 *agfl_bno; 570b16817b6SDave Chinner int bucket; 571b16817b6SDave Chinner 572b16817b6SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 573b16817b6SDave Chinner agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC); 574b16817b6SDave Chinner agfl->agfl_seqno = cpu_to_be32(id->agno); 575b16817b6SDave Chinner uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid); 576b16817b6SDave Chinner } 577b16817b6SDave Chinner 578183606d8SChristoph Hellwig agfl_bno = xfs_buf_to_agfl_bno(bp); 579b16817b6SDave Chinner for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++) 580b16817b6SDave Chinner agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK); 581b16817b6SDave Chinner } 582b16817b6SDave Chinner 583b16817b6SDave Chinner static void 584b16817b6SDave Chinner xfs_agiblock_init( 585b16817b6SDave Chinner struct xfs_mount *mp, 586b16817b6SDave Chinner struct xfs_buf *bp, 587b16817b6SDave Chinner struct aghdr_init_data *id) 588b16817b6SDave Chinner { 589370c782bSChristoph Hellwig struct xfs_agi *agi = bp->b_addr; 590b16817b6SDave Chinner int bucket; 591b16817b6SDave Chinner 592b16817b6SDave Chinner agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC); 593b16817b6SDave Chinner agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION); 594b16817b6SDave Chinner agi->agi_seqno = cpu_to_be32(id->agno); 595b16817b6SDave Chinner agi->agi_length = cpu_to_be32(id->agsize); 596b16817b6SDave Chinner agi->agi_count = 0; 597b16817b6SDave Chinner agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp)); 598b16817b6SDave Chinner agi->agi_level = cpu_to_be32(1); 599b16817b6SDave Chinner agi->agi_freecount = 0; 600b16817b6SDave Chinner agi->agi_newino = cpu_to_be32(NULLAGINO); 601b16817b6SDave Chinner agi->agi_dirino = cpu_to_be32(NULLAGINO); 602b16817b6SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) 603b16817b6SDave Chinner uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid); 604b16817b6SDave Chinner if (xfs_sb_version_hasfinobt(&mp->m_sb)) { 605b16817b6SDave Chinner agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp)); 606b16817b6SDave Chinner agi->agi_free_level = cpu_to_be32(1); 607b16817b6SDave Chinner } 608b16817b6SDave Chinner for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) 609b16817b6SDave Chinner agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO); 6102a39946cSDarrick J. Wong if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) { 6112a39946cSDarrick J. Wong agi->agi_iblocks = cpu_to_be32(1); 6122a39946cSDarrick J. Wong if (xfs_sb_version_hasfinobt(&mp->m_sb)) 6132a39946cSDarrick J. Wong agi->agi_fblocks = cpu_to_be32(1); 6142a39946cSDarrick J. Wong } 615b16817b6SDave Chinner } 616b16817b6SDave Chinner 617b16817b6SDave Chinner typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp, 618b16817b6SDave Chinner struct aghdr_init_data *id); 619b16817b6SDave Chinner static int 620b16817b6SDave Chinner xfs_ag_init_hdr( 621b16817b6SDave Chinner struct xfs_mount *mp, 622b16817b6SDave Chinner struct aghdr_init_data *id, 623b16817b6SDave Chinner aghdr_init_work_f work, 624b16817b6SDave Chinner const struct xfs_buf_ops *ops) 625b16817b6SDave Chinner { 626b16817b6SDave Chinner struct xfs_buf *bp; 6272842b6dbSDarrick J. Wong int error; 628b16817b6SDave Chinner 6292842b6dbSDarrick J. Wong error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops); 6302842b6dbSDarrick J. Wong if (error) 6312842b6dbSDarrick J. Wong return error; 632b16817b6SDave Chinner 633b16817b6SDave Chinner (*work)(mp, bp, id); 634b16817b6SDave Chinner 635b16817b6SDave Chinner xfs_buf_delwri_queue(bp, &id->buffer_list); 636b16817b6SDave Chinner xfs_buf_relse(bp); 637b16817b6SDave Chinner return 0; 638b16817b6SDave Chinner } 639b16817b6SDave Chinner 640b16817b6SDave Chinner struct xfs_aghdr_grow_data { 641b16817b6SDave Chinner xfs_daddr_t daddr; 642b16817b6SDave Chinner size_t numblks; 643b16817b6SDave Chinner const struct xfs_buf_ops *ops; 644b16817b6SDave Chinner aghdr_init_work_f work; 645b16817b6SDave Chinner xfs_btnum_t type; 646b16817b6SDave Chinner bool need_init; 647b16817b6SDave Chinner }; 648b16817b6SDave Chinner 649b16817b6SDave Chinner /* 650b16817b6SDave Chinner * Prepare new AG headers to be written to disk. We use uncached buffers here, 651b16817b6SDave Chinner * as it is assumed these new AG headers are currently beyond the currently 652b16817b6SDave Chinner * valid filesystem address space. Using cached buffers would trip over EOFS 653b16817b6SDave Chinner * corruption detection alogrithms in the buffer cache lookup routines. 654b16817b6SDave Chinner * 655b16817b6SDave Chinner * This is a non-transactional function, but the prepared buffers are added to a 656b16817b6SDave Chinner * delayed write buffer list supplied by the caller so they can submit them to 657b16817b6SDave Chinner * disk and wait on them as required. 658b16817b6SDave Chinner */ 659b16817b6SDave Chinner int 660b16817b6SDave Chinner xfs_ag_init_headers( 661b16817b6SDave Chinner struct xfs_mount *mp, 662b16817b6SDave Chinner struct aghdr_init_data *id) 663b16817b6SDave Chinner 664b16817b6SDave Chinner { 665b16817b6SDave Chinner struct xfs_aghdr_grow_data aghdr_data[] = { 666b16817b6SDave Chinner { /* SB */ 667b16817b6SDave Chinner .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR), 668b16817b6SDave Chinner .numblks = XFS_FSS_TO_BB(mp, 1), 669b16817b6SDave Chinner .ops = &xfs_sb_buf_ops, 670b16817b6SDave Chinner .work = &xfs_sbblock_init, 671b16817b6SDave Chinner .need_init = true 672b16817b6SDave Chinner }, 673b16817b6SDave Chinner { /* AGF */ 674b16817b6SDave Chinner .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)), 675b16817b6SDave Chinner .numblks = XFS_FSS_TO_BB(mp, 1), 676b16817b6SDave Chinner .ops = &xfs_agf_buf_ops, 677b16817b6SDave Chinner .work = &xfs_agfblock_init, 678b16817b6SDave Chinner .need_init = true 679b16817b6SDave Chinner }, 680b16817b6SDave Chinner { /* AGFL */ 681b16817b6SDave Chinner .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)), 682b16817b6SDave Chinner .numblks = XFS_FSS_TO_BB(mp, 1), 683b16817b6SDave Chinner .ops = &xfs_agfl_buf_ops, 684b16817b6SDave Chinner .work = &xfs_agflblock_init, 685b16817b6SDave Chinner .need_init = true 686b16817b6SDave Chinner }, 687b16817b6SDave Chinner { /* AGI */ 688b16817b6SDave Chinner .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)), 689b16817b6SDave Chinner .numblks = XFS_FSS_TO_BB(mp, 1), 690b16817b6SDave Chinner .ops = &xfs_agi_buf_ops, 691b16817b6SDave Chinner .work = &xfs_agiblock_init, 692b16817b6SDave Chinner .need_init = true 693b16817b6SDave Chinner }, 694b16817b6SDave Chinner { /* BNO root block */ 695b16817b6SDave Chinner .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)), 696b16817b6SDave Chinner .numblks = BTOBB(mp->m_sb.sb_blocksize), 69727df4f50SBrian Foster .ops = &xfs_bnobt_buf_ops, 698b16817b6SDave Chinner .work = &xfs_bnoroot_init, 699b16817b6SDave Chinner .need_init = true 700b16817b6SDave Chinner }, 701b16817b6SDave Chinner { /* CNT root block */ 702b16817b6SDave Chinner .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)), 703b16817b6SDave Chinner .numblks = BTOBB(mp->m_sb.sb_blocksize), 70427df4f50SBrian Foster .ops = &xfs_cntbt_buf_ops, 705b16817b6SDave Chinner .work = &xfs_cntroot_init, 706b16817b6SDave Chinner .need_init = true 707b16817b6SDave Chinner }, 708b16817b6SDave Chinner { /* INO root block */ 709b16817b6SDave Chinner .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)), 710b16817b6SDave Chinner .numblks = BTOBB(mp->m_sb.sb_blocksize), 711b16817b6SDave Chinner .ops = &xfs_inobt_buf_ops, 712b16817b6SDave Chinner .work = &xfs_btroot_init, 713b16817b6SDave Chinner .type = XFS_BTNUM_INO, 714b16817b6SDave Chinner .need_init = true 715b16817b6SDave Chinner }, 716b16817b6SDave Chinner { /* FINO root block */ 717b16817b6SDave Chinner .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)), 718b16817b6SDave Chinner .numblks = BTOBB(mp->m_sb.sb_blocksize), 71901e68f40SBrian Foster .ops = &xfs_finobt_buf_ops, 720b16817b6SDave Chinner .work = &xfs_btroot_init, 721b16817b6SDave Chinner .type = XFS_BTNUM_FINO, 722b16817b6SDave Chinner .need_init = xfs_sb_version_hasfinobt(&mp->m_sb) 723b16817b6SDave Chinner }, 724b16817b6SDave Chinner { /* RMAP root block */ 725b16817b6SDave Chinner .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)), 726b16817b6SDave Chinner .numblks = BTOBB(mp->m_sb.sb_blocksize), 727b16817b6SDave Chinner .ops = &xfs_rmapbt_buf_ops, 728b16817b6SDave Chinner .work = &xfs_rmaproot_init, 729b16817b6SDave Chinner .need_init = xfs_sb_version_hasrmapbt(&mp->m_sb) 730b16817b6SDave Chinner }, 731b16817b6SDave Chinner { /* REFC root block */ 732b16817b6SDave Chinner .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)), 733b16817b6SDave Chinner .numblks = BTOBB(mp->m_sb.sb_blocksize), 734b16817b6SDave Chinner .ops = &xfs_refcountbt_buf_ops, 735b16817b6SDave Chinner .work = &xfs_btroot_init, 736b16817b6SDave Chinner .type = XFS_BTNUM_REFC, 737b16817b6SDave Chinner .need_init = xfs_sb_version_hasreflink(&mp->m_sb) 738b16817b6SDave Chinner }, 739b16817b6SDave Chinner { /* NULL terminating block */ 740b16817b6SDave Chinner .daddr = XFS_BUF_DADDR_NULL, 741b16817b6SDave Chinner } 742b16817b6SDave Chinner }; 743b16817b6SDave Chinner struct xfs_aghdr_grow_data *dp; 744b16817b6SDave Chinner int error = 0; 745b16817b6SDave Chinner 746b16817b6SDave Chinner /* Account for AG free space in new AG */ 747b16817b6SDave Chinner id->nfree += id->agsize - mp->m_ag_prealloc_blocks; 748b16817b6SDave Chinner for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) { 749b16817b6SDave Chinner if (!dp->need_init) 750b16817b6SDave Chinner continue; 751b16817b6SDave Chinner 752b16817b6SDave Chinner id->daddr = dp->daddr; 753b16817b6SDave Chinner id->numblks = dp->numblks; 754b16817b6SDave Chinner id->type = dp->type; 755b16817b6SDave Chinner error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops); 756b16817b6SDave Chinner if (error) 757b16817b6SDave Chinner break; 758b16817b6SDave Chinner } 759b16817b6SDave Chinner return error; 760b16817b6SDave Chinner } 76149dd56f2SDave Chinner 76246141dc8SGao Xiang int 76346141dc8SGao Xiang xfs_ag_shrink_space( 76446141dc8SGao Xiang struct xfs_mount *mp, 76546141dc8SGao Xiang struct xfs_trans **tpp, 76646141dc8SGao Xiang xfs_agnumber_t agno, 76746141dc8SGao Xiang xfs_extlen_t delta) 76846141dc8SGao Xiang { 76946141dc8SGao Xiang struct xfs_alloc_arg args = { 77046141dc8SGao Xiang .tp = *tpp, 77146141dc8SGao Xiang .mp = mp, 77246141dc8SGao Xiang .type = XFS_ALLOCTYPE_THIS_BNO, 77346141dc8SGao Xiang .minlen = delta, 77446141dc8SGao Xiang .maxlen = delta, 77546141dc8SGao Xiang .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE, 77646141dc8SGao Xiang .resv = XFS_AG_RESV_NONE, 77746141dc8SGao Xiang .prod = 1 77846141dc8SGao Xiang }; 77946141dc8SGao Xiang struct xfs_buf *agibp, *agfbp; 78046141dc8SGao Xiang struct xfs_agi *agi; 78146141dc8SGao Xiang struct xfs_agf *agf; 78246141dc8SGao Xiang int error, err2; 78346141dc8SGao Xiang 78446141dc8SGao Xiang ASSERT(agno == mp->m_sb.sb_agcount - 1); 78546141dc8SGao Xiang error = xfs_ialloc_read_agi(mp, *tpp, agno, &agibp); 78646141dc8SGao Xiang if (error) 78746141dc8SGao Xiang return error; 78846141dc8SGao Xiang 78946141dc8SGao Xiang agi = agibp->b_addr; 79046141dc8SGao Xiang 79146141dc8SGao Xiang error = xfs_alloc_read_agf(mp, *tpp, agno, 0, &agfbp); 79246141dc8SGao Xiang if (error) 79346141dc8SGao Xiang return error; 79446141dc8SGao Xiang 79546141dc8SGao Xiang agf = agfbp->b_addr; 79646141dc8SGao Xiang /* some extra paranoid checks before we shrink the ag */ 79746141dc8SGao Xiang if (XFS_IS_CORRUPT(mp, agf->agf_length != agi->agi_length)) 79846141dc8SGao Xiang return -EFSCORRUPTED; 79946141dc8SGao Xiang if (delta >= agi->agi_length) 80046141dc8SGao Xiang return -EINVAL; 80146141dc8SGao Xiang 80246141dc8SGao Xiang args.fsbno = XFS_AGB_TO_FSB(mp, agno, 80346141dc8SGao Xiang be32_to_cpu(agi->agi_length) - delta); 80446141dc8SGao Xiang 80546141dc8SGao Xiang /* 80646141dc8SGao Xiang * Disable perag reservations so it doesn't cause the allocation request 80746141dc8SGao Xiang * to fail. We'll reestablish reservation before we return. 80846141dc8SGao Xiang */ 80946141dc8SGao Xiang error = xfs_ag_resv_free(agibp->b_pag); 81046141dc8SGao Xiang if (error) 81146141dc8SGao Xiang return error; 81246141dc8SGao Xiang 81346141dc8SGao Xiang /* internal log shouldn't also show up in the free space btrees */ 81446141dc8SGao Xiang error = xfs_alloc_vextent(&args); 81546141dc8SGao Xiang if (!error && args.agbno == NULLAGBLOCK) 81646141dc8SGao Xiang error = -ENOSPC; 81746141dc8SGao Xiang 81846141dc8SGao Xiang if (error) { 81946141dc8SGao Xiang /* 82046141dc8SGao Xiang * if extent allocation fails, need to roll the transaction to 82146141dc8SGao Xiang * ensure that the AGFL fixup has been committed anyway. 82246141dc8SGao Xiang */ 82346141dc8SGao Xiang xfs_trans_bhold(*tpp, agfbp); 82446141dc8SGao Xiang err2 = xfs_trans_roll(tpp); 82546141dc8SGao Xiang if (err2) 82646141dc8SGao Xiang return err2; 82746141dc8SGao Xiang xfs_trans_bjoin(*tpp, agfbp); 82846141dc8SGao Xiang goto resv_init_out; 82946141dc8SGao Xiang } 83046141dc8SGao Xiang 83146141dc8SGao Xiang /* 83246141dc8SGao Xiang * if successfully deleted from freespace btrees, need to confirm 83346141dc8SGao Xiang * per-AG reservation works as expected. 83446141dc8SGao Xiang */ 83546141dc8SGao Xiang be32_add_cpu(&agi->agi_length, -delta); 83646141dc8SGao Xiang be32_add_cpu(&agf->agf_length, -delta); 83746141dc8SGao Xiang 83846141dc8SGao Xiang err2 = xfs_ag_resv_init(agibp->b_pag, *tpp); 83946141dc8SGao Xiang if (err2) { 84046141dc8SGao Xiang be32_add_cpu(&agi->agi_length, delta); 84146141dc8SGao Xiang be32_add_cpu(&agf->agf_length, delta); 84246141dc8SGao Xiang if (err2 != -ENOSPC) 84346141dc8SGao Xiang goto resv_err; 84446141dc8SGao Xiang 84546141dc8SGao Xiang __xfs_bmap_add_free(*tpp, args.fsbno, delta, NULL, true); 84646141dc8SGao Xiang 84746141dc8SGao Xiang /* 84846141dc8SGao Xiang * Roll the transaction before trying to re-init the per-ag 84946141dc8SGao Xiang * reservation. The new transaction is clean so it will cancel 85046141dc8SGao Xiang * without any side effects. 85146141dc8SGao Xiang */ 85246141dc8SGao Xiang error = xfs_defer_finish(tpp); 85346141dc8SGao Xiang if (error) 85446141dc8SGao Xiang return error; 85546141dc8SGao Xiang 85646141dc8SGao Xiang error = -ENOSPC; 85746141dc8SGao Xiang goto resv_init_out; 85846141dc8SGao Xiang } 85946141dc8SGao Xiang xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH); 86046141dc8SGao Xiang xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH); 86146141dc8SGao Xiang return 0; 86246141dc8SGao Xiang resv_init_out: 86346141dc8SGao Xiang err2 = xfs_ag_resv_init(agibp->b_pag, *tpp); 86446141dc8SGao Xiang if (!err2) 86546141dc8SGao Xiang return error; 86646141dc8SGao Xiang resv_err: 86746141dc8SGao Xiang xfs_warn(mp, "Error %d reserving per-AG metadata reserve pool.", err2); 86846141dc8SGao Xiang xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 86946141dc8SGao Xiang return err2; 87046141dc8SGao Xiang } 87146141dc8SGao Xiang 87249dd56f2SDave Chinner /* 87349dd56f2SDave Chinner * Extent the AG indicated by the @id by the length passed in 87449dd56f2SDave Chinner */ 87549dd56f2SDave Chinner int 87649dd56f2SDave Chinner xfs_ag_extend_space( 87749dd56f2SDave Chinner struct xfs_mount *mp, 87849dd56f2SDave Chinner struct xfs_trans *tp, 87949dd56f2SDave Chinner struct aghdr_init_data *id, 88049dd56f2SDave Chinner xfs_extlen_t len) 88149dd56f2SDave Chinner { 88249dd56f2SDave Chinner struct xfs_buf *bp; 88349dd56f2SDave Chinner struct xfs_agi *agi; 88449dd56f2SDave Chinner struct xfs_agf *agf; 88549dd56f2SDave Chinner int error; 88649dd56f2SDave Chinner 88749dd56f2SDave Chinner /* 88849dd56f2SDave Chinner * Change the agi length. 88949dd56f2SDave Chinner */ 89049dd56f2SDave Chinner error = xfs_ialloc_read_agi(mp, tp, id->agno, &bp); 89149dd56f2SDave Chinner if (error) 89249dd56f2SDave Chinner return error; 89349dd56f2SDave Chinner 894370c782bSChristoph Hellwig agi = bp->b_addr; 89549dd56f2SDave Chinner be32_add_cpu(&agi->agi_length, len); 89649dd56f2SDave Chinner ASSERT(id->agno == mp->m_sb.sb_agcount - 1 || 89749dd56f2SDave Chinner be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks); 89849dd56f2SDave Chinner xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH); 89949dd56f2SDave Chinner 90049dd56f2SDave Chinner /* 90149dd56f2SDave Chinner * Change agf length. 90249dd56f2SDave Chinner */ 90349dd56f2SDave Chinner error = xfs_alloc_read_agf(mp, tp, id->agno, 0, &bp); 90449dd56f2SDave Chinner if (error) 90549dd56f2SDave Chinner return error; 90649dd56f2SDave Chinner 9079798f615SChristoph Hellwig agf = bp->b_addr; 90849dd56f2SDave Chinner be32_add_cpu(&agf->agf_length, len); 90949dd56f2SDave Chinner ASSERT(agf->agf_length == agi->agi_length); 91049dd56f2SDave Chinner xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH); 91149dd56f2SDave Chinner 91249dd56f2SDave Chinner /* 91349dd56f2SDave Chinner * Free the new space. 91449dd56f2SDave Chinner * 9157280fedaSDarrick J. Wong * XFS_RMAP_OINFO_SKIP_UPDATE is used here to tell the rmap btree that 91649dd56f2SDave Chinner * this doesn't actually exist in the rmap btree. 91749dd56f2SDave Chinner */ 918fa9c3c19SDave Chinner error = xfs_rmap_free(tp, bp, bp->b_pag, 91949dd56f2SDave Chinner be32_to_cpu(agf->agf_length) - len, 9207280fedaSDarrick J. Wong len, &XFS_RMAP_OINFO_SKIP_UPDATE); 92149dd56f2SDave Chinner if (error) 92249dd56f2SDave Chinner return error; 92349dd56f2SDave Chinner 92449dd56f2SDave Chinner return xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, id->agno, 92549dd56f2SDave Chinner be32_to_cpu(agf->agf_length) - len), 9267280fedaSDarrick J. Wong len, &XFS_RMAP_OINFO_SKIP_UPDATE, 9277280fedaSDarrick J. Wong XFS_AG_RESV_NONE); 92849dd56f2SDave Chinner } 9297cd5006bSDarrick J. Wong 9307cd5006bSDarrick J. Wong /* Retrieve AG geometry. */ 9317cd5006bSDarrick J. Wong int 9327cd5006bSDarrick J. Wong xfs_ag_get_geometry( 9337cd5006bSDarrick J. Wong struct xfs_mount *mp, 9347cd5006bSDarrick J. Wong xfs_agnumber_t agno, 9357cd5006bSDarrick J. Wong struct xfs_ag_geometry *ageo) 9367cd5006bSDarrick J. Wong { 9377cd5006bSDarrick J. Wong struct xfs_buf *agi_bp; 9387cd5006bSDarrick J. Wong struct xfs_buf *agf_bp; 9397cd5006bSDarrick J. Wong struct xfs_agi *agi; 9407cd5006bSDarrick J. Wong struct xfs_agf *agf; 9417cd5006bSDarrick J. Wong struct xfs_perag *pag; 9427cd5006bSDarrick J. Wong unsigned int freeblks; 9437cd5006bSDarrick J. Wong int error; 9447cd5006bSDarrick J. Wong 9457cd5006bSDarrick J. Wong if (agno >= mp->m_sb.sb_agcount) 9467cd5006bSDarrick J. Wong return -EINVAL; 9477cd5006bSDarrick J. Wong 9487cd5006bSDarrick J. Wong /* Lock the AG headers. */ 9497cd5006bSDarrick J. Wong error = xfs_ialloc_read_agi(mp, NULL, agno, &agi_bp); 9507cd5006bSDarrick J. Wong if (error) 9517cd5006bSDarrick J. Wong return error; 9527cd5006bSDarrick J. Wong error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agf_bp); 9537cd5006bSDarrick J. Wong if (error) 9547cd5006bSDarrick J. Wong goto out_agi; 95592a00544SGao Xiang 95692a00544SGao Xiang pag = agi_bp->b_pag; 9577cd5006bSDarrick J. Wong 9587cd5006bSDarrick J. Wong /* Fill out form. */ 9597cd5006bSDarrick J. Wong memset(ageo, 0, sizeof(*ageo)); 9607cd5006bSDarrick J. Wong ageo->ag_number = agno; 9617cd5006bSDarrick J. Wong 962370c782bSChristoph Hellwig agi = agi_bp->b_addr; 9637cd5006bSDarrick J. Wong ageo->ag_icount = be32_to_cpu(agi->agi_count); 9647cd5006bSDarrick J. Wong ageo->ag_ifree = be32_to_cpu(agi->agi_freecount); 9657cd5006bSDarrick J. Wong 9669798f615SChristoph Hellwig agf = agf_bp->b_addr; 9677cd5006bSDarrick J. Wong ageo->ag_length = be32_to_cpu(agf->agf_length); 9687cd5006bSDarrick J. Wong freeblks = pag->pagf_freeblks + 9697cd5006bSDarrick J. Wong pag->pagf_flcount + 9707cd5006bSDarrick J. Wong pag->pagf_btreeblks - 9717cd5006bSDarrick J. Wong xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE); 9727cd5006bSDarrick J. Wong ageo->ag_freeblks = freeblks; 9731302c6a2SDarrick J. Wong xfs_ag_geom_health(pag, ageo); 9747cd5006bSDarrick J. Wong 9757cd5006bSDarrick J. Wong /* Release resources. */ 9767cd5006bSDarrick J. Wong xfs_buf_relse(agf_bp); 9777cd5006bSDarrick J. Wong out_agi: 9787cd5006bSDarrick J. Wong xfs_buf_relse(agi_bp); 9797cd5006bSDarrick J. Wong return error; 9807cd5006bSDarrick J. Wong } 981