xfs
[Top] [All Lists]

[PATCH 13/15] xfs: Introduce a helper to truncate an AG

To: xfs@xxxxxxxxxxx
Subject: [PATCH 13/15] xfs: Introduce a helper to truncate an AG
From: Jeff Liu <jeff.liu@xxxxxxxxxx>
Date: Fri, 16 Nov 2012 14:46:50 +0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2
During FS shrinking, if an affected AG is not empty, we need to truncate it 
accordingly.


Signed-off-by: Jie Liu <jeff.liu@xxxxxxxxxx>
---
 fs/xfs/xfs_fsops.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/fs/xfs/xfs_fsops.c b/fs/xfs/xfs_fsops.c
index 24de23f..04a21d1 100644
--- a/fs/xfs/xfs_fsops.c
+++ b/fs/xfs/xfs_fsops.c
@@ -855,6 +855,100 @@ error0:
 }
 
 /*
+ * Truncate the given AG and update the metadatas accordingly.
+ */
+static int
+xfs_truncate_ag(
+       xfs_mount_t             *mp,
+       xfs_agnumber_t          agno,
+       xfs_rfsblock_t          tsize,
+       xfs_extlen_t            *nfree)
+{
+       xfs_agf_t               *agf;
+       xfs_agi_t               *agi;
+       xfs_buf_t               *bp;
+       xfs_perag_t             *pag;
+       xfs_alloc_rec_t         *arec;
+       __uint32_t              bno_btree_blkno;
+       __uint32_t              cnt_btree_blkno;
+       struct xfs_btree_block  *block;
+       int                     error;
+
+       bp = xfs_buf_get(mp->m_ddev_targp,
+                        XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
+                        XFS_FSS_TO_BB(mp, 1), 0);
+       if (!bp) {
+               error = ENOMEM;
+               goto error0;
+       }
+       agf = XFS_BUF_TO_AGF(bp);
+       be32_add_cpu(&agf->agf_length, -tsize);
+       be32_add_cpu(&agf->agf_freeblks, -tsize);
+       agf->agf_longest = agf->agf_freeblks;
+       bno_btree_blkno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNOi]);
+       cnt_btree_blkno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNTi]);
+       error = xfs_bwrite(bp);
+       xfs_buf_relse(bp);
+       if (error)
+               goto error0;
+
+       bp = xfs_buf_get(mp->m_ddev_targp,
+                        XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
+                        XFS_FSS_TO_BB(mp, 1), 0);
+       if (!bp) {
+               error = ENOMEM;
+               goto error0;
+       }
+       agi = XFS_BUF_TO_AGI(bp);
+       be32_add_cpu(&agi->agi_length, -tsize);
+       error = xfs_bwrite(bp);
+       xfs_buf_relse(bp);
+       if (error)
+               goto error0;
+
+       bp = xfs_buf_get(mp->m_ddev_targp,
+                        XFS_AGB_TO_DADDR(mp, agno, bno_btree_blkno),
+                        BTOBB(mp->m_sb.sb_blocksize), 0);
+       if (!bp) {
+               error = ENOMEM;
+               goto error0;
+       }
+       block = XFS_BUF_TO_BLOCK(bp);
+       arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
+       be32_add_cpu(&arec->ar_blockcount, -tsize);
+       error = xfs_bwrite(bp);
+       xfs_buf_relse(bp);
+       if (error)
+               goto error0;
+
+       bp = xfs_buf_get(mp->m_ddev_targp,
+                        XFS_AGB_TO_DADDR(mp, agno, cnt_btree_blkno),
+                        BTOBB(mp->m_sb.sb_blocksize), 0);
+       if (!bp) {
+               error = ENOMEM;
+               goto error0;
+       }
+       block = XFS_BUF_TO_BLOCK(bp);
+       arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
+       be32_add_cpu(&arec->ar_blockcount, -tsize);
+       error = xfs_bwrite(bp);
+       xfs_buf_relse(bp);
+       if (error)
+               goto error0;
+
+       pag = xfs_perag_get(mp, agno);
+       pag->pagf_freeblks -= tsize;
+       pag->pagf_longest = pag->pagf_freeblks;
+       xfs_perag_put(pag);
+
+       if (nfree)
+               *nfree = tsize;
+
+error0:
+       return error;
+}
+
+/*
  * exported through ioctl XFS_IOC_FSCOUNTS
  */
 
-- 
1.7.4.1

<Prev in Thread] Current Thread [Next in Thread>
  • [PATCH 13/15] xfs: Introduce a helper to truncate an AG, Jeff Liu <=