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-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 438 499 87.8 %
Date: 2023-07-31 20:08:07 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 84949933088 : xfs_perag_get(
      43             :         struct xfs_mount        *mp,
      44             :         xfs_agnumber_t          agno)
      45             : {
      46 84949933088 :         struct xfs_perag        *pag;
      47             : 
      48 84949933088 :         rcu_read_lock();
      49 85375333624 :         pag = radix_tree_lookup(&mp->m_perag_tree, agno);
      50 85285810844 :         if (pag) {
      51 85285677456 :                 trace_xfs_perag_get(pag, _RET_IP_);
      52 85404717467 :                 ASSERT(atomic_read(&pag->pag_ref) >= 0);
      53 85404717467 :                 atomic_inc(&pag->pag_ref);
      54             :         }
      55 85473417920 :         rcu_read_unlock();
      56 85480439055 :         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     2707181 : xfs_perag_get_tag(
      64             :         struct xfs_mount        *mp,
      65             :         xfs_agnumber_t          first,
      66             :         unsigned int            tag)
      67             : {
      68     2707181 :         struct xfs_perag        *pag;
      69     2707181 :         int                     found;
      70             : 
      71     2707181 :         rcu_read_lock();
      72     2707181 :         found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
      73             :                                         (void **)&pag, first, 1, tag);
      74     2707186 :         if (found <= 0) {
      75      792834 :                 rcu_read_unlock();
      76      792834 :                 return NULL;
      77             :         }
      78     1914352 :         trace_xfs_perag_get_tag(pag, _RET_IP_);
      79     1914351 :         atomic_inc(&pag->pag_ref);
      80     1914351 :         rcu_read_unlock();
      81     1914355 :         return pag;
      82             : }
      83             : 
      84             : /* Get a passive reference to the given perag. */
      85             : struct xfs_perag *
      86  3898903836 : xfs_perag_hold(
      87             :         struct xfs_perag        *pag)
      88             : {
      89  3898903836 :         ASSERT(atomic_read(&pag->pag_ref) > 0 ||
      90             :                atomic_read(&pag->pag_active_ref) > 0);
      91             : 
      92  3898903836 :         trace_xfs_perag_hold(pag, _RET_IP_);
      93  3899092227 :         atomic_inc(&pag->pag_ref);
      94  3899075586 :         return pag;
      95             : }
      96             : 
      97             : void
      98 88492545180 : xfs_perag_put(
      99             :         struct xfs_perag        *pag)
     100             : {
     101 88492545180 :         trace_xfs_perag_put(pag, _RET_IP_);
     102 89237402567 :         ASSERT(atomic_read(&pag->pag_ref) > 0);
     103 89237402567 :         atomic_dec(&pag->pag_ref);
     104 89254072512 : }
     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   631889962 : xfs_perag_grab(
     114             :         struct xfs_mount        *mp,
     115             :         xfs_agnumber_t          agno)
     116             : {
     117   631889962 :         struct xfs_perag        *pag;
     118             : 
     119   631889962 :         rcu_read_lock();
     120   631916065 :         pag = radix_tree_lookup(&mp->m_perag_tree, agno);
     121   631913303 :         if (pag) {
     122   631913303 :                 trace_xfs_perag_grab(pag, _RET_IP_);
     123  1263850766 :                 if (!atomic_inc_not_zero(&pag->pag_active_ref))
     124           0 :                         pag = NULL;
     125             :         }
     126   631938355 :         rcu_read_unlock();
     127   631954382 :         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    10562573 : xfs_perag_grab_tag(
     135             :         struct xfs_mount        *mp,
     136             :         xfs_agnumber_t          first,
     137             :         int                     tag)
     138             : {
     139    10562573 :         struct xfs_perag        *pag;
     140    10562573 :         int                     found;
     141             : 
     142    10562573 :         rcu_read_lock();
     143    10563090 :         found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
     144             :                                         (void **)&pag, first, 1, tag);
     145    10562744 :         if (found <= 0) {
     146     8049420 :                 rcu_read_unlock();
     147     8049420 :                 return NULL;
     148             :         }
     149     2513324 :         trace_xfs_perag_grab_tag(pag, _RET_IP_);
     150     5026510 :         if (!atomic_inc_not_zero(&pag->pag_active_ref))
     151           0 :                 pag = NULL;
     152     2513233 :         rcu_read_unlock();
     153     2513437 :         return pag;
     154             : }
     155             : 
     156             : void
     157   634583087 : xfs_perag_rele(
     158             :         struct xfs_perag        *pag)
     159             : {
     160   634583087 :         trace_xfs_perag_rele(pag, _RET_IP_);
     161  1269235714 :         if (atomic_dec_and_test(&pag->pag_active_ref))
     162      133416 :                 wake_up(&pag->pag_active_wq);
     163   634625341 : }
     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       11299 : xfs_initialize_perag_data(
     175             :         struct xfs_mount        *mp,
     176             :         xfs_agnumber_t          agcount)
     177             : {
     178       11299 :         xfs_agnumber_t          index;
     179       11299 :         struct xfs_perag        *pag;
     180       11299 :         struct xfs_sb           *sbp = &mp->m_sb;
     181       11299 :         uint64_t                ifree = 0;
     182       11299 :         uint64_t                ialloc = 0;
     183       11299 :         uint64_t                bfree = 0;
     184       11299 :         uint64_t                bfreelst = 0;
     185       11299 :         uint64_t                btree = 0;
     186       11299 :         uint64_t                fdblocks;
     187       11299 :         int                     error = 0;
     188             : 
     189       56477 :         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       45178 :                 pag = xfs_perag_get(mp, index);
     195       45178 :                 error = xfs_alloc_read_agf(pag, NULL, 0, NULL);
     196       45178 :                 if (!error)
     197       45178 :                         error = xfs_ialloc_read_agi(pag, NULL, NULL);
     198       45178 :                 if (error) {
     199           0 :                         xfs_perag_put(pag);
     200           0 :                         return error;
     201             :                 }
     202             : 
     203       45178 :                 ifree += pag->pagi_freecount;
     204       45178 :                 ialloc += pag->pagi_count;
     205       45178 :                 bfree += pag->pagf_freeblks;
     206       45178 :                 bfreelst += pag->pagf_flcount;
     207       45178 :                 btree += pag->pagf_btreeblks;
     208       45178 :                 xfs_perag_put(pag);
     209             :         }
     210       11299 :         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       11299 :         if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
     219           2 :                 xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
     220           2 :                 xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
     221           2 :                 error = -EFSCORRUPTED;
     222           2 :                 goto out;
     223             :         }
     224             : 
     225             :         /* Overwrite incore superblock counters with just-read data */
     226       11297 :         spin_lock(&mp->m_sb_lock);
     227       11297 :         sbp->sb_ifree = ifree;
     228       11297 :         sbp->sb_icount = ialloc;
     229       11297 :         sbp->sb_fdblocks = fdblocks;
     230       11297 :         spin_unlock(&mp->m_sb_lock);
     231             : 
     232       11297 :         xfs_reinit_percpu_counters(mp);
     233       11299 : out:
     234       11299 :         xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
     235       11299 :         return error;
     236             : }
     237             : 
     238             : STATIC void
     239      133416 : __xfs_free_perag(
     240             :         struct rcu_head *head)
     241             : {
     242      133416 :         struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
     243             : 
     244      133416 :         ASSERT(!delayed_work_pending(&pag->pag_blockgc_work));
     245      133416 :         kmem_free(pag);
     246      133416 : }
     247             : 
     248             : /*
     249             :  * Free up the per-ag resources associated with the mount structure.
     250             :  */
     251             : void
     252       24122 : xfs_free_perag(
     253             :         struct xfs_mount        *mp)
     254             : {
     255       24122 :         struct xfs_perag        *pag;
     256       24122 :         xfs_agnumber_t          agno;
     257             : 
     258      157538 :         for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
     259      133416 :                 spin_lock(&mp->m_perag_lock);
     260      133416 :                 pag = radix_tree_delete(&mp->m_perag_tree, agno);
     261      133416 :                 spin_unlock(&mp->m_perag_lock);
     262      133416 :                 ASSERT(pag);
     263      133416 :                 XFS_IS_CORRUPT(pag->pag_mount, atomic_read(&pag->pag_ref) != 0);
     264      133416 :                 xfs_defer_drain_free(&pag->pag_intents_drain);
     265             : 
     266      133416 :                 cancel_delayed_work_sync(&pag->pag_blockgc_work);
     267      133416 :                 xfs_buf_cache_destroy(&pag->pag_bcache);
     268             : 
     269             :                 /* drop the mount's active reference */
     270      133416 :                 xfs_perag_rele(pag);
     271      133416 :                 XFS_IS_CORRUPT(pag->pag_mount,
     272             :                                 atomic_read(&pag->pag_active_ref) != 0);
     273      133416 :                 call_rcu(&pag->rcu_head, __xfs_free_perag);
     274             :         }
     275       24122 : }
     276             : 
     277             : /* Find the size of the AG, in blocks. */
     278             : static xfs_agblock_t
     279 >12660*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 >12660*10^7 :         ASSERT(agno < agcount);
     286             : 
     287 >12660*10^7 :         if (agno < agcount - 1)
     288 98419648011 :                 return mp->m_sb.sb_agblocks;
     289 28190078974 :         return dblocks - (agno * mp->m_sb.sb_agblocks);
     290             : }
     291             : 
     292             : xfs_agblock_t
     293  6781715826 : xfs_ag_block_count(
     294             :         struct xfs_mount        *mp,
     295             :         xfs_agnumber_t          agno)
     296             : {
     297  6781715826 :         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 >11818*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 >11818*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 >11818*10^7 :         bno = round_up(XFS_AGFL_BLOCK(mp) + 1, M_IGEO(mp)->cluster_align);
     316 >11818*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 >11818*10^7 :         bno = round_down(eoag, M_IGEO(mp)->cluster_align);
     323 >11818*10^7 :         *last = XFS_AGB_TO_AGINO(mp, bno) - 1;
     324 >11818*10^7 : }
     325             : 
     326             : void
     327 >11781*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 >11781*10^7 :         return __xfs_agino_range(mp, xfs_ag_block_count(mp, agno), first, last);
     334             : }
     335             : 
     336             : int
     337       35484 : 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       35484 :         struct xfs_perag        *pag;
     344       35484 :         xfs_agnumber_t          index;
     345       35484 :         xfs_agnumber_t          first_initialised = NULLAGNUMBER;
     346       35484 :         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      214580 :         for (index = 0; index < agcount; index++) {
     354      179096 :                 pag = xfs_perag_get(mp, index);
     355      179096 :                 if (pag) {
     356       45708 :                         xfs_perag_put(pag);
     357       45708 :                         continue;
     358             :                 }
     359             : 
     360      133388 :                 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
     361      133388 :                 if (!pag) {
     362           0 :                         error = -ENOMEM;
     363           0 :                         goto out_unwind_new_pags;
     364             :                 }
     365      133388 :                 pag->pag_agno = index;
     366      133388 :                 pag->pag_mount = mp;
     367             : 
     368      133388 :                 error = radix_tree_preload(GFP_NOFS);
     369      133388 :                 if (error)
     370           0 :                         goto out_free_pag;
     371             : 
     372      133388 :                 spin_lock(&mp->m_perag_lock);
     373      133388 :                 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      133388 :                 spin_unlock(&mp->m_perag_lock);
     381      133388 :                 radix_tree_preload_end();
     382             : 
     383             : #ifdef __KERNEL__
     384             :                 /* Place kernel structure only init below this point. */
     385      133388 :                 spin_lock_init(&pag->pag_ici_lock);
     386      133388 :                 spin_lock_init(&pag->pagb_lock);
     387      133388 :                 spin_lock_init(&pag->pag_state_lock);
     388      133388 :                 INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker);
     389      133388 :                 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
     390      133388 :                 xfs_defer_drain_init(&pag->pag_intents_drain);
     391      133388 :                 init_waitqueue_head(&pag->pagb_wait);
     392      133388 :                 init_waitqueue_head(&pag->pag_active_wq);
     393      133388 :                 pag->pagb_count = 0;
     394      133388 :                 pag->pagb_tree = RB_ROOT;
     395      133388 :                 xfs_hooks_init(&pag->pag_rmap_update_hooks);
     396             : #endif /* __KERNEL__ */
     397             : 
     398      133388 :                 error = xfs_buf_cache_init(&pag->pag_bcache);
     399      133388 :                 if (error)
     400           0 :                         goto out_remove_pag;
     401             : 
     402             :                 /* Active ref owned by mount indicates AG is online. */
     403      133388 :                 atomic_set(&pag->pag_active_ref, 1);
     404             : 
     405             :                 /* first new pag is fully initialized */
     406      133388 :                 if (first_initialised == NULLAGNUMBER)
     407       24201 :                         first_initialised = index;
     408             : 
     409             :                 /*
     410             :                  * Pre-calculated geometry
     411             :                  */
     412      133388 :                 pag->block_count = __xfs_ag_block_count(mp, index, agcount,
     413             :                                 dblocks);
     414      133388 :                 pag->min_block = XFS_AGFL_BLOCK(mp);
     415      133388 :                 __xfs_agino_range(mp, pag->block_count, &pag->agino_min,
     416             :                                 &pag->agino_max);
     417             :         }
     418             : 
     419       35484 :         index = xfs_set_inode_alloc(mp, agcount);
     420             : 
     421       35484 :         if (maxagi)
     422       35484 :                 *maxagi = index;
     423             : 
     424       35484 :         mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
     425       35484 :         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       70054 : 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       70054 :         struct xfs_buf          *bp;
     454       70054 :         int                     error;
     455             : 
     456       70054 :         error = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, 0, &bp);
     457       70054 :         if (error)
     458             :                 return error;
     459             : 
     460       70054 :         bp->b_maps[0].bm_bn = blkno;
     461       70054 :         bp->b_ops = ops;
     462             : 
     463       70054 :         *bpp = bp;
     464       70054 :         return 0;
     465             : }
     466             : 
     467             : /*
     468             :  * Generic btree root block init function
     469             :  */
     470             : static void
     471       18506 : xfs_btroot_init(
     472             :         struct xfs_mount        *mp,
     473             :         struct xfs_buf          *bp,
     474             :         struct aghdr_init_data  *id)
     475             : {
     476       18506 :         xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
     477       18506 : }
     478             : 
     479             : /* Finish initializing a free space btree. */
     480             : static void
     481       15380 : xfs_freesp_init_recs(
     482             :         struct xfs_mount        *mp,
     483             :         struct xfs_buf          *bp,
     484             :         struct aghdr_init_data  *id)
     485             : {
     486       15380 :         struct xfs_alloc_rec    *arec;
     487       15380 :         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
     488             : 
     489       15380 :         arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
     490       15380 :         arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
     491             : 
     492       30760 :         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       30760 :         arec->ar_blockcount = cpu_to_be32(id->agsize -
     528             :                                           be32_to_cpu(arec->ar_startblock));
     529       15380 :         if (arec->ar_blockcount)
     530       15380 :                 be16_add_cpu(&block->bb_numrecs, 1);
     531       15380 : }
     532             : 
     533             : /*
     534             :  * bnobt/cntbt btree root block init functions
     535             :  */
     536             : static void
     537       15380 : xfs_bnoroot_init(
     538             :         struct xfs_mount        *mp,
     539             :         struct xfs_buf          *bp,
     540             :         struct aghdr_init_data  *id)
     541             : {
     542       15380 :         xfs_btree_init_buf(mp, bp, id->bc_ops, 0, 0, id->agno);
     543       15380 :         xfs_freesp_init_recs(mp, bp, id);
     544       15380 : }
     545             : 
     546             : /*
     547             :  * Reverse map root block init
     548             :  */
     549             : static void
     550        5408 : xfs_rmaproot_init(
     551             :         struct xfs_mount        *mp,
     552             :         struct xfs_buf          *bp,
     553             :         struct aghdr_init_data  *id)
     554             : {
     555        5408 :         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
     556        5408 :         struct xfs_rmap_rec     *rrec;
     557             : 
     558        5408 :         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        5408 :         rrec = XFS_RMAP_REC_ADDR(block, 1);
     570        5408 :         rrec->rm_startblock = 0;
     571        5408 :         rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
     572        5408 :         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
     573        5408 :         rrec->rm_offset = 0;
     574             : 
     575             :         /* account freespace btree root blocks */
     576        5408 :         rrec = XFS_RMAP_REC_ADDR(block, 2);
     577        5408 :         rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
     578        5408 :         rrec->rm_blockcount = cpu_to_be32(2);
     579        5408 :         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
     580        5408 :         rrec->rm_offset = 0;
     581             : 
     582             :         /* account inode btree root blocks */
     583        5408 :         rrec = XFS_RMAP_REC_ADDR(block, 3);
     584        5408 :         rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
     585        5408 :         rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
     586             :                                           XFS_IBT_BLOCK(mp));
     587        5408 :         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
     588        5408 :         rrec->rm_offset = 0;
     589             : 
     590             :         /* account for rmap btree root */
     591        5408 :         rrec = XFS_RMAP_REC_ADDR(block, 4);
     592        5408 :         rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
     593        5408 :         rrec->rm_blockcount = cpu_to_be32(1);
     594        5408 :         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
     595        5408 :         rrec->rm_offset = 0;
     596             : 
     597             :         /* account for refc btree root */
     598        5408 :         if (xfs_has_reflink(mp)) {
     599        5408 :                 rrec = XFS_RMAP_REC_ADDR(block, 5);
     600        5408 :                 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
     601        5408 :                 rrec->rm_blockcount = cpu_to_be32(1);
     602        5408 :                 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
     603        5408 :                 rrec->rm_offset = 0;
     604        5408 :                 be16_add_cpu(&block->bb_numrecs, 1);
     605             :         }
     606             : 
     607             :         /* account for the log space */
     608       10816 :         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        5408 : }
     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        7690 : xfs_sbblock_init(
     630             :         struct xfs_mount        *mp,
     631             :         struct xfs_buf          *bp,
     632             :         struct aghdr_init_data  *id)
     633             : {
     634        7690 :         struct xfs_dsb          *dsb = bp->b_addr;
     635             : 
     636        7690 :         xfs_sb_to_disk(dsb, &mp->m_sb);
     637        7690 :         dsb->sb_inprogress = 1;
     638        7690 : }
     639             : 
     640             : static void
     641        7690 : xfs_agfblock_init(
     642             :         struct xfs_mount        *mp,
     643             :         struct xfs_buf          *bp,
     644             :         struct aghdr_init_data  *id)
     645             : {
     646        7690 :         struct xfs_agf          *agf = bp->b_addr;
     647        7690 :         xfs_extlen_t            tmpsize;
     648             : 
     649        7690 :         agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
     650        7690 :         agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
     651        7690 :         agf->agf_seqno = cpu_to_be32(id->agno);
     652        7690 :         agf->agf_length = cpu_to_be32(id->agsize);
     653        7690 :         agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
     654        7690 :         agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
     655        7690 :         agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
     656        7690 :         agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
     657        7690 :         if (xfs_has_rmapbt(mp)) {
     658        5408 :                 agf->agf_roots[XFS_BTNUM_RMAPi] =
     659        5408 :                                         cpu_to_be32(XFS_RMAP_BLOCK(mp));
     660        5408 :                 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
     661        5408 :                 agf->agf_rmap_blocks = cpu_to_be32(1);
     662             :         }
     663             : 
     664        7690 :         agf->agf_flfirst = cpu_to_be32(1);
     665        7690 :         agf->agf_fllast = 0;
     666        7690 :         agf->agf_flcount = 0;
     667        7690 :         tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
     668        7690 :         agf->agf_freeblks = cpu_to_be32(tmpsize);
     669        7690 :         agf->agf_longest = cpu_to_be32(tmpsize);
     670        7690 :         if (xfs_has_crc(mp))
     671        5408 :                 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
     672        7690 :         if (xfs_has_reflink(mp)) {
     673        5408 :                 agf->agf_refcount_root = cpu_to_be32(
     674             :                                 xfs_refc_block(mp));
     675        5408 :                 agf->agf_refcount_level = cpu_to_be32(1);
     676        5408 :                 agf->agf_refcount_blocks = cpu_to_be32(1);
     677             :         }
     678             : 
     679       15380 :         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        7690 : }
     687             : 
     688             : static void
     689        7690 : xfs_agflblock_init(
     690             :         struct xfs_mount        *mp,
     691             :         struct xfs_buf          *bp,
     692             :         struct aghdr_init_data  *id)
     693             : {
     694        7690 :         struct xfs_agfl         *agfl = XFS_BUF_TO_AGFL(bp);
     695        7690 :         __be32                  *agfl_bno;
     696        7690 :         int                     bucket;
     697             : 
     698        7690 :         if (xfs_has_crc(mp)) {
     699        5408 :                 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
     700        5408 :                 agfl->agfl_seqno = cpu_to_be32(id->agno);
     701        5408 :                 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
     702             :         }
     703             : 
     704        7690 :         agfl_bno = xfs_buf_to_agfl_bno(bp);
     705     7190250 :         for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
     706     7182560 :                 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
     707        7690 : }
     708             : 
     709             : static void
     710        7690 : xfs_agiblock_init(
     711             :         struct xfs_mount        *mp,
     712             :         struct xfs_buf          *bp,
     713             :         struct aghdr_init_data  *id)
     714             : {
     715        7690 :         struct xfs_agi          *agi = bp->b_addr;
     716        7690 :         int                     bucket;
     717             : 
     718        7690 :         agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
     719        7690 :         agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
     720        7690 :         agi->agi_seqno = cpu_to_be32(id->agno);
     721        7690 :         agi->agi_length = cpu_to_be32(id->agsize);
     722        7690 :         agi->agi_count = 0;
     723        7690 :         agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
     724        7690 :         agi->agi_level = cpu_to_be32(1);
     725        7690 :         agi->agi_freecount = 0;
     726        7690 :         agi->agi_newino = cpu_to_be32(NULLAGINO);
     727        7690 :         agi->agi_dirino = cpu_to_be32(NULLAGINO);
     728        7690 :         if (xfs_has_crc(mp))
     729        5408 :                 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
     730        7690 :         if (xfs_has_finobt(mp)) {
     731        5408 :                 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
     732        5408 :                 agi->agi_free_level = cpu_to_be32(1);
     733             :         }
     734      499850 :         for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
     735      492160 :                 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
     736        7690 :         if (xfs_has_inobtcounts(mp)) {
     737        5408 :                 agi->agi_iblocks = cpu_to_be32(1);
     738        5408 :                 if (xfs_has_finobt(mp))
     739        5408 :                         agi->agi_fblocks = cpu_to_be32(1);
     740             :         }
     741        7690 : }
     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       70054 : 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       70054 :         struct xfs_buf          *bp;
     753       70054 :         int                     error;
     754             : 
     755       70054 :         error = xfs_get_aghdr_buf(mp, id->daddr, id->numblks, &bp, ops);
     756       70054 :         if (error)
     757             :                 return error;
     758             : 
     759       70054 :         (*work)(mp, bp, id);
     760             : 
     761       70054 :         xfs_buf_delwri_queue(bp, &id->buffer_list);
     762       70054 :         xfs_buf_relse(bp);
     763       70054 :         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        7690 : xfs_ag_init_headers(
     787             :         struct xfs_mount        *mp,
     788             :         struct aghdr_init_data  *id)
     789             : 
     790             : {
     791       23070 :         struct xfs_aghdr_grow_data aghdr_data[] = {
     792             :         { /* SB */
     793        7690 :                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
     794        7690 :                 .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        7690 :                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
     801             :                 .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        7690 :                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
     808             :                 .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        7690 :                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
     815             :                 .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        7690 :                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
     822        7690 :                 .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        7690 :                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
     830             :                 .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        7690 :                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
     838             :                 .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        7690 :                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
     846             :                 .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        7690 :                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
     854             :                 .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        7690 :                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
     862        7690 :                 .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        7690 :         struct  xfs_aghdr_grow_data *dp;
     873        7690 :         int                     error = 0;
     874             : 
     875             :         /* Account for AG free space in new AG */
     876        7690 :         id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
     877       84590 :         for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
     878       76900 :                 if (!dp->need_init)
     879        6846 :                         continue;
     880             : 
     881       70054 :                 id->daddr = dp->daddr;
     882       70054 :                 id->numblks = dp->numblks;
     883       70054 :                 id->bc_ops = dp->bc_ops;
     884       70054 :                 error = xfs_ag_init_hdr(mp, id, dp->work, dp->ops);
     885       70054 :                 if (error)
     886             :                         break;
     887             :         }
     888        7690 :         return error;
     889             : }
     890             : 
     891             : int
     892         211 : xfs_ag_shrink_space(
     893             :         struct xfs_perag        *pag,
     894             :         struct xfs_trans        **tpp,
     895             :         xfs_extlen_t            delta)
     896             : {
     897         211 :         struct xfs_mount        *mp = pag->pag_mount;
     898         211 :         struct xfs_alloc_arg    args = {
     899         211 :                 .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         211 :         struct xfs_buf          *agibp, *agfbp;
     909         211 :         struct xfs_agi          *agi;
     910         211 :         struct xfs_agf          *agf;
     911         211 :         xfs_agblock_t           aglen;
     912         211 :         int                     error, err2;
     913             : 
     914         211 :         ASSERT(pag->pag_agno == mp->m_sb.sb_agcount - 1);
     915         211 :         error = xfs_ialloc_read_agi(pag, *tpp, &agibp);
     916         211 :         if (error)
     917             :                 return error;
     918             : 
     919         211 :         agi = agibp->b_addr;
     920             : 
     921         211 :         error = xfs_alloc_read_agf(pag, *tpp, 0, &agfbp);
     922         211 :         if (error)
     923             :                 return error;
     924             : 
     925         211 :         agf = agfbp->b_addr;
     926         211 :         aglen = be32_to_cpu(agi->agi_length);
     927             :         /* some extra paranoid checks before we shrink the ag */
     928         211 :         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         211 :         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         211 :         error = xfs_ialloc_check_shrink(pag, *tpp, agibp, aglen - delta);
     940         211 :         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          85 :         error = xfs_ag_resv_free(pag);
     948          85 :         if (error)
     949             :                 return error;
     950             : 
     951             :         /* internal log shouldn't also show up in the free space btrees */
     952          85 :         error = xfs_alloc_vextent_exact_bno(&args,
     953          85 :                         XFS_AGB_TO_FSB(mp, pag->pag_agno, aglen - delta));
     954          85 :         if (!error && args.agbno == NULLAGBLOCK)
     955             :                 error = -ENOSPC;
     956             : 
     957          36 :         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          49 :                 xfs_trans_bhold(*tpp, agfbp);
     963          49 :                 err2 = xfs_trans_roll(tpp);
     964          49 :                 if (err2)
     965             :                         return err2;
     966          49 :                 xfs_trans_bjoin(*tpp, agfbp);
     967          49 :                 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          36 :         be32_add_cpu(&agi->agi_length, -delta);
     975          36 :         be32_add_cpu(&agf->agf_length, -delta);
     976             : 
     977          36 :         err2 = xfs_ag_resv_init(pag, *tpp);
     978          36 :         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          36 :         xfs_ialloc_log_agi(*tpp, agibp, XFS_AGI_LENGTH);
    1002          36 :         xfs_alloc_log_agf(*tpp, agfbp, XFS_AGF_LENGTH);
    1003          36 :         return 0;
    1004             : 
    1005          49 : resv_init_out:
    1006          49 :         err2 = xfs_ag_resv_init(pag, *tpp);
    1007          49 :         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          28 : xfs_ag_extend_space(
    1020             :         struct xfs_perag        *pag,
    1021             :         struct xfs_trans        *tp,
    1022             :         xfs_extlen_t            len)
    1023             : {
    1024          28 :         struct xfs_buf          *bp;
    1025          28 :         struct xfs_agi          *agi;
    1026          28 :         struct xfs_agf          *agf;
    1027          28 :         int                     error;
    1028             : 
    1029          28 :         ASSERT(pag->pag_agno == pag->pag_mount->m_sb.sb_agcount - 1);
    1030             : 
    1031          28 :         error = xfs_ialloc_read_agi(pag, tp, &bp);
    1032          28 :         if (error)
    1033             :                 return error;
    1034             : 
    1035          28 :         agi = bp->b_addr;
    1036          28 :         be32_add_cpu(&agi->agi_length, len);
    1037          28 :         xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
    1038             : 
    1039             :         /*
    1040             :          * Change agf length.
    1041             :          */
    1042          28 :         error = xfs_alloc_read_agf(pag, tp, 0, &bp);
    1043          28 :         if (error)
    1044             :                 return error;
    1045             : 
    1046          28 :         agf = bp->b_addr;
    1047          28 :         be32_add_cpu(&agf->agf_length, len);
    1048          28 :         ASSERT(agf->agf_length == agi->agi_length);
    1049          28 :         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          56 :         error = xfs_rmap_free(tp, bp, pag, be32_to_cpu(agf->agf_length) - len,
    1058             :                                 len, &XFS_RMAP_OINFO_SKIP_UPDATE);
    1059          28 :         if (error)
    1060             :                 return error;
    1061             : 
    1062          56 :         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          28 :         if (error)
    1065             :                 return error;
    1066             : 
    1067             :         /* Update perag geometry */
    1068          28 :         pag->block_count = be32_to_cpu(agf->agf_length);
    1069          28 :         __xfs_agino_range(pag->pag_mount, pag->block_count, &pag->agino_min,
    1070             :                                 &pag->agino_max);
    1071          28 :         return 0;
    1072             : }
    1073             : 
    1074             : /* Retrieve AG geometry. */
    1075             : int
    1076       15411 : xfs_ag_get_geometry(
    1077             :         struct xfs_perag        *pag,
    1078             :         struct xfs_ag_geometry  *ageo)
    1079             : {
    1080       15411 :         struct xfs_buf          *agi_bp;
    1081       15411 :         struct xfs_buf          *agf_bp;
    1082       15411 :         struct xfs_agi          *agi;
    1083       15411 :         struct xfs_agf          *agf;
    1084       15411 :         unsigned int            freeblks;
    1085       15411 :         int                     error;
    1086             : 
    1087             :         /* Lock the AG headers. */
    1088       15411 :         error = xfs_ialloc_read_agi(pag, NULL, &agi_bp);
    1089       15411 :         if (error)
    1090             :                 return error;
    1091       15411 :         error = xfs_alloc_read_agf(pag, NULL, 0, &agf_bp);
    1092       15411 :         if (error)
    1093           0 :                 goto out_agi;
    1094             : 
    1095             :         /* Fill out form. */
    1096       15411 :         memset(ageo, 0, sizeof(*ageo));
    1097       15411 :         ageo->ag_number = pag->pag_agno;
    1098             : 
    1099       15411 :         agi = agi_bp->b_addr;
    1100       15411 :         ageo->ag_icount = be32_to_cpu(agi->agi_count);
    1101       15411 :         ageo->ag_ifree = be32_to_cpu(agi->agi_freecount);
    1102             : 
    1103       15411 :         agf = agf_bp->b_addr;
    1104       15411 :         ageo->ag_length = be32_to_cpu(agf->agf_length);
    1105       30822 :         freeblks = pag->pagf_freeblks +
    1106       15411 :                    pag->pagf_flcount +
    1107       15411 :                    pag->pagf_btreeblks -
    1108       15411 :                    xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE);
    1109       15411 :         ageo->ag_freeblks = freeblks;
    1110       15411 :         xfs_ag_geom_health(pag, ageo);
    1111             : 
    1112             :         /* Release resources. */
    1113       15411 :         xfs_buf_relse(agf_bp);
    1114       15411 : out_agi:
    1115       15411 :         xfs_buf_relse(agi_bp);
    1116       15411 :         return error;
    1117             : }

Generated by: LCOV version 1.14