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

Generated by: LCOV version 1.14