LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_ag.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwa @ Mon Jul 31 20:08:17 PDT 2023 Lines: 456 507 89.9 %
Date: 2023-07-31 20:08:17 Functions: 30 30 100.0 %

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

Generated by: LCOV version 1.14