xfs: support shrinking free space in the last AG
v9 cover xfs: support shrinking free space in the last AG
Hi folks,
v9: https://lore.kernel.org/r/[email protected]
This patchset attempts to support shrinking free space in the last AG. This version mainly addresses previous review of v8. Hope I don't miss previous comments...
changes since v8 (Brian):
- [2/5] rename to `lastag_extended';
- [2/5] use `delta' instead;
- [3/5] refine several comments;
- [3/5] lock agf buffer here to close perag reservation race window;
- [4/5] drop unnecessary `nb == mp->m_sb.sb_dblocks' check.
- [4/5] refine a comment.
Thanks for the time!
Thanks,
Gao Xiang
Gao Xiang (5):
xfs: update lazy sb counters immediately for resizefs
xfs: hoist out xfs_resizefs_init_new_ags()
xfs: introduce xfs_ag_shrink_space()
xfs: support shrinking unused space in the last AG
xfs: add error injection for per-AG resv failure
fs/xfs/libxfs/xfs_ag.c | 115 +++++++++++++++++++++ fs/xfs/libxfs/xfs_ag.h | 2 + fs/xfs/libxfs/xfs_ag_resv.c | 6 +- fs/xfs/libxfs/xfs_errortag.h | 4 +- fs/xfs/xfs_error.c | 3 + fs/xfs/xfs_fsops.c | 194 ++++++++++++++++++++++------------- fs/xfs/xfs_trans.c | 1 - 7 files changed, 248 insertions(+), 77 deletions(-)
Signature · 1 line
2.27.0
PATCH v9 1/5 xfs: update lazy sb counters immediately for resizefs
sb_fdblocks will be updated lazily if lazysbcount is enabled, therefore when shrinking the filesystem sb_fdblocks could be larger than sb_dblocks and xfs_validate_sb_write() would fail.
Even for growfs case, it'd be better to update lazy sb counters immediately to reflect the real sb counters.
--- fs/xfs/xfs_fsops.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/xfs/xfs_fsops.c +9 -0
@@ -128,6 +128,15 @@ xfs_growfs_data_private( nb - mp->m_sb.sb_dblocks);if (id.nfree)xfs_trans_mod_sb (tp,XFS_TRANS_SB_FDBLOCKS , id.nfree); + +/* +* Sync sb counters now to reflect the updated values. This is +* particularly important for shrink because the write verifier +* will fail if sb_fdblocks is ever larger than sb_dblocks. +*/ +if (xfs_sb_version_haslazysbcount (&mp->m_sb)) +xfs_log_sb (tp); +xfs_trans_set_sync (tp); error =xfs_trans_commit (tp);if (error)
Signature · 1 line
2.27.0
PATCH v9 2/5 xfs: hoist out xfs_resizefs_init_new_ags()
In reply to Gao XiangMove out related logic for initializing new added AGs to a new helper in preparation for shrinking. No logic changes.
--- fs/xfs/xfs_fsops.c | 107 +++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 43 deletions(-)
fs/xfs/xfs_fsops.c +64 -43
@@ -20,6 +20,64 @@#include "xfs_ag.h" #include "xfs_ag_resv.h" +/* +* Write new AG headers to disk. Non-transactional, but need to be +* written and completed prior to the growfs transaction being logged. +* To do this, we use a delayed write buffer list and wait for +* submission and IO completion of the list as a whole. This allows the +* IO subsystem to merge all the AG headers in a single AG into a single +* IO and hide most of the latency of the IO from us. +* +* This also means that if we get an error whilst building the buffer +* list to write, we can cancel the entire list without having written +* anything. +*/ +static int +xfs_resizefs_init_new_ags ( +struct xfs_trans *tp, +struct aghdr_init_data *id, +xfs_agnumber_t oagcount, +xfs_agnumber_t nagcount, +xfs_rfsblock_t delta, +bool *lastag_extended) +{ +struct xfs_mount *mp = tp->t_mountp; +xfs_rfsblock_t nb = mp->m_sb.sb_dblocks + delta; +int error; + + *lastag_extended = false; + +INIT_LIST_HEAD (&id->buffer_list); +for (id->agno = nagcount -1 ; + id->agno >= oagcount; + id->agno--, delta -= id->agsize) { + +if (id->agno == nagcount -1 ) + id->agsize = nb - (id->agno * + (xfs_rfsblock_t )mp->m_sb.sb_agblocks); +else + id->agsize = mp->m_sb.sb_agblocks; + + error =xfs_ag_init_headers (mp, id); +if (error) { +xfs_buf_delwri_cancel (&id->buffer_list); +return error; + } + } + + error =xfs_buf_delwri_submit (&id->buffer_list); +if (error) +return error; + +xfs_trans_agblocks_delta (tp, id->nfree); + +if (delta) { + *lastag_extended = true; + error =xfs_ag_extend_space (mp, tp, id, delta); + } +return error; +} +/* * growfs operations */
@@ -34,6 +92,7 @@ xfs_growfs_data_private(xfs_agnumber_t nagimax =0 ;xfs_rfsblock_t nb, nb_div, nb_mod;xfs_rfsblock_t delta; +bool lastag_extended;xfs_agnumber_t oagcount;struct xfs_trans *tp;struct aghdr_init_data id = {};
@@ -74,48 +133,11 @@ xfs_growfs_data_private(if (error)return error; -/* -* Write new AG headers to disk. Non-transactional, but need to be -* written and completed prior to the growfs transaction being logged. -* To do this, we use a delayed write buffer list and wait for -* submission and IO completion of the list as a whole. This allows the -* IO subsystem to merge all the AG headers in a single AG into a single -* IO and hide most of the latency of the IO from us. -* -* This also means that if we get an error whilst building the buffer -* list to write, we can cancel the entire list without having written -* anything. -*/ -INIT_LIST_HEAD (&id.buffer_list); -for (id.agno = nagcount -1 ; - id.agno >= oagcount; - id.agno--, delta -= id.agsize) { - -if (id.agno == nagcount -1 ) - id.agsize = nb - - (id.agno * (xfs_rfsblock_t )mp->m_sb.sb_agblocks); -else - id.agsize = mp->m_sb.sb_agblocks; - - error =xfs_ag_init_headers (mp, &id); -if (error) { -xfs_buf_delwri_cancel (&id.buffer_list); - goto out_trans_cancel; - } - } - error =xfs_buf_delwri_submit (&id.buffer_list); + error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, + delta, &lastag_extended);if (error) goto out_trans_cancel; -xfs_trans_agblocks_delta (tp, id.nfree); - -/* If there are new blocks in the old last AG, extend it. */ -if (delta) { - error =xfs_ag_extend_space (mp, tp, &id, delta); -if (error) - goto out_trans_cancel; - } -/* * Update changed superblock fields transactionally. These are not * seen by the rest of the world until the transaction commit applies
@@ -123,9 +145,8 @@ xfs_growfs_data_private(*/ if (nagcount > oagcount)xfs_trans_mod_sb (tp,XFS_TRANS_SB_AGCOUNT , nagcount - oagcount); -if (nb > mp->m_sb.sb_dblocks) -xfs_trans_mod_sb (tp,XFS_TRANS_SB_DBLOCKS , - nb - mp->m_sb.sb_dblocks); +if (delta) +xfs_trans_mod_sb (tp,XFS_TRANS_SB_DBLOCKS , delta);if (id.nfree)xfs_trans_mod_sb (tp,XFS_TRANS_SB_FDBLOCKS , id.nfree);
@@ -152,7 +173,7 @@ xfs_growfs_data_private(* If we expanded the last AG, free the per-AG reservation * so we can reinitialize it with the new size. */ -if (delta) { +if (lastag_extended) {struct xfs_perag *pag; pag =xfs_perag_get (mp, id.agno);
Signature · 1 line
2.27.0
PATCH v9 3/5 xfs: introduce xfs_ag_shrink_space()
In reply to Gao XiangThis patch introduces a helper to shrink unused space in the last AG by fixing up the freespace btree.
Also make sure that the per-AG reservation works under the new AG size. If such per-AG reservation or extent allocation fails, roll the transaction so the new transaction could cancel without any side effects.
--- fs/xfs/libxfs/xfs_ag.c | 115 +++++++++++++++++++++++++++++++++++++++++ fs/xfs/libxfs/xfs_ag.h | 2 + 2 files changed, 117 insertions(+)
fs/xfs/libxfs/xfs_ag.c +115 -0
@@ -22,6 +22,11 @@#include "xfs_ag.h" #include "xfs_ag_resv.h" #include "xfs_health.h" +#include "xfs_error.h" +#include "xfs_bmap.h" +#include "xfs_defer.h" +#include "xfs_log_format.h" +#include "xfs_trans.h" static int xfs_get_aghdr_buf(
@@ -485,6 +490,116 @@ xfs_ag_init_headers(return error; } +int +xfs_ag_shrink_space ( +struct xfs_mount *mp, +struct xfs_trans **tpp, +xfs_agnumber_t agno, +xfs_extlen_t delta) +{ +struct xfs_alloc_arg args = { + .tp = *tpp, + .mp = mp, + .type =XFS_ALLOCTYPE_THIS_BNO , + .minlen = delta, + .maxlen = delta, + .oinfo =XFS_RMAP_OINFO_SKIP_UPDATE , + .resv =XFS_AG_RESV_NONE , + .prod =1 + }; +struct xfs_buf *agibp, *agfbp; +struct xfs_agi *agi; +struct xfs_agf *agf; +int error, err2; + +ASSERT (agno == mp->m_sb.sb_agcount -1 ); + error =xfs_ialloc_read_agi (mp, *tpp, agno, &agibp); +if (error) +return error; + + agi = agibp->b_addr; + + error =xfs_alloc_read_agf (mp, *tpp, agno,0 , &agfbp); +if (error) +return error; + + agf = agfbp->b_addr; +/* some extra paranoid checks before we shrink the ag */ +if (XFS_IS_CORRUPT (mp, agf->agf_length != agi->agi_length)) +return -EFSCORRUPTED ; +if (delta >= agi->agi_length) +return -EINVAL ; + + args.fsbno =XFS_AGB_TO_FSB (mp, agno, +be32_to_cpu (agi->agi_length) - delta); + +/* +* Disable perag reservations so it doesn't cause the allocation request +* to fail. We'll reestablish reservation before we return. +*/ + error =xfs_ag_resv_free (agibp->b_pag); +if (error) +return error; + +/* internal log shouldn't also show up in the free space btrees */ + error =xfs_alloc_vextent (&args); +if (!error && args.agbno ==NULLAGBLOCK ) + error = -ENOSPC ; + +if (error) { +/* +* if extent allocation fails, need to roll the transaction to +* ensure that the AGFL fixup has been committed anyway. +*/ +xfs_trans_bhold (*tpp, agfbp); + err2 =xfs_trans_roll (tpp); +if (err2) +return err2; +xfs_trans_bjoin (*tpp, agfbp); + goto resv_init_out; + } + +/* +* if successfully deleted from freespace btrees, need to confirm +* per-AG reservation works as expected. +*/ +be32_add_cpu (&agi->agi_length, -delta); +be32_add_cpu (&agf->agf_length, -delta); + + err2 =xfs_ag_resv_init (agibp->b_pag, *tpp); +if (err2) { +be32_add_cpu (&agi->agi_length, delta); +be32_add_cpu (&agf->agf_length, delta); +if (err2 != -ENOSPC ) + goto resv_err; + +__xfs_bmap_add_free (*tpp, args.fsbno, delta,NULL , true); + +/* +* Roll the transaction before trying to re-init the per-ag +* reservation. The new transaction is clean so it will cancel +* without any side effects. +*/ + error =xfs_defer_finish (tpp); +if (error) +return error; + + error = -ENOSPC ; + goto resv_init_out; + } +xfs_ialloc_log_agi (*tpp, agibp,XFS_AGI_LENGTH ); +xfs_alloc_log_agf (*tpp, agfbp,XFS_AGF_LENGTH ); +return 0 ; +resv_init_out: + err2 =xfs_ag_resv_init (agibp->b_pag, *tpp); +if (!err2) +return error; +resv_err: +xfs_warn (mp,"Error %d reserving per-AG metadata reserve pool." , err2); +xfs_force_shutdown (mp,SHUTDOWN_CORRUPT_INCORE ); +return err2; +} +/* * Extent the AG indicated by the @id by the length passed in */
fs/xfs/libxfs/xfs_ag.h +2 -0
@@ -24,6 +24,8 @@ struct aghdr_init_data { };int xfs_ag_init_headers (struct xfs_mount *mp,struct aghdr_init_data *id); +int xfs_ag_shrink_space (struct xfs_mount *mp,struct xfs_trans **tpp, +xfs_agnumber_t agno,xfs_extlen_t delta);int xfs_ag_extend_space (struct xfs_mount *mp,struct xfs_trans *tp,struct aghdr_init_data *id,xfs_extlen_t len); int xfs_ag_get_geometry(struct xfs_mount *mp, xfs_agnumber_t agno,
Signature · 1 line
2.27.0
PATCH v9 4/5 xfs: support shrinking unused space in the last AG
In reply to Gao XiangAs the first step of shrinking, this attempts to enable shrinking unused space in the last allocation group by fixing up freespace btree, agi, agf and adjusting super block and use a helper xfs_ag_shrink_space() to fixup the last AG.
This can be all done in one transaction for now, so I think no additional protection is needed.
--- fs/xfs/xfs_fsops.c | 84 +++++++++++++++++++++++++++------------------- fs/xfs/xfs_trans.c | 1 - 2 files changed, 50 insertions(+), 35 deletions(-)
fs/xfs/xfs_fsops.c +50 -34
@@ -91,23 +91,25 @@ xfs_growfs_data_private(xfs_agnumber_t nagcount;xfs_agnumber_t nagimax =0 ;xfs_rfsblock_t nb, nb_div, nb_mod; -xfs_rfsblock_t delta; +int64_t delta;bool lastag_extended;xfs_agnumber_t oagcount;struct xfs_trans *tp;struct aghdr_init_data id = {}; nb = in->newblocks; -if (nb < mp->m_sb.sb_dblocks) -return -EINVAL ; -if ((error =xfs_sb_validate_fsb_count (&mp->m_sb, nb))) + error =xfs_sb_validate_fsb_count (&mp->m_sb, nb); +if (error)return error; - error =xfs_buf_read_uncached (mp->m_ddev_targp, + +if (nb > mp->m_sb.sb_dblocks) { + error =xfs_buf_read_uncached (mp->m_ddev_targp,XFS_FSB_TO_BB (mp, nb) -XFS_FSS_TO_BB (mp,1 ),XFS_FSS_TO_BB (mp,1 ),0 , &bp,NULL ); -if (error) -return error; -xfs_buf_relse (bp); +if (error) +return error; +xfs_buf_relse (bp); + } nb_div = nb; nb_mod =do_div (nb_div, mp->m_sb.sb_agblocks);
@@ -115,10 +117,16 @@ xfs_growfs_data_private(if (nb_mod && nb_mod <XFS_MIN_AG_BLOCKS ) { nagcount--; nb = (xfs_rfsblock_t )nagcount * mp->m_sb.sb_agblocks; -if (nb < mp->m_sb.sb_dblocks) -return -EINVAL ; } delta = nb - mp->m_sb.sb_dblocks; +/* +* Reject filesystems with a single AG because they are not +* supported, and reject a shrink operation that would cause a +* filesystem to become unsupported. +*/ +if (delta <0 && nagcount <2 ) +return -EINVAL ; + oagcount = mp->m_sb.sb_agcount;/* allocate the new per-ag structures */
@@ -126,15 +134,22 @@ xfs_growfs_data_private( error =xfs_initialize_perag (mp, nagcount, &nagimax);if (error)return error; + }else if (nagcount < oagcount) { +/* TODO: shrinking the entire AGs hasn't yet completed */ +return -EINVAL ; } error =xfs_trans_alloc (mp, &M_RES (mp)->tr_growdata, -XFS_GROWFS_SPACE_RES (mp),0 ,XFS_TRANS_RESERVE , &tp); + (delta >0 ?XFS_GROWFS_SPACE_RES (mp) : -delta),0 , +XFS_TRANS_RESERVE , &tp);if (error)return error; - error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, - delta, &lastag_extended); +if (delta >0 ) + error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, + delta, &lastag_extended); +else + error =xfs_ag_shrink_space (mp, &tp, nagcount -1 , -delta);if (error) goto out_trans_cancel;
@@ -169,28 +184,29 @@ xfs_growfs_data_private(xfs_set_low_space_thresholds (mp); mp->m_alloc_set_aside =xfs_alloc_set_aside (mp); -/* -* If we expanded the last AG, free the per-AG reservation -* so we can reinitialize it with the new size. -*/ -if (lastag_extended) { -struct xfs_perag *pag; - - pag =xfs_perag_get (mp, id.agno); - error =xfs_ag_resv_free (pag); -xfs_perag_put (pag); -if (error) -return error; +if (delta >0 ) { +/* +* If we expanded the last AG, free the per-AG reservation +* so we can reinitialize it with the new size. +*/ +if (lastag_extended) { +struct xfs_perag *pag; + + pag =xfs_perag_get (mp, id.agno); + error =xfs_ag_resv_free (pag); +xfs_perag_put (pag); +if (error) +return error; + } +/* +* Reserve AG metadata blocks. ENOSPC here does not mean there +* was a growfs failure, just that there still isn't space for +* new user data after the grow has been run. +*/ + error =xfs_fs_reserve_ag_blocks (mp); +if (error == -ENOSPC ) + error =0 ; } - -/* -* Reserve AG metadata blocks. ENOSPC here does not mean there was a -* growfs failure, just that there still isn't space for new user data -* after the grow has been run. -*/ - error =xfs_fs_reserve_ag_blocks (mp); -if (error == -ENOSPC ) - error =0 ;return error; out_trans_cancel:
fs/xfs/xfs_trans.c +0 -1
@@ -436,7 +436,6 @@ xfs_trans_mod_sb( tp->t_res_frextents_delta += delta;break ;case XFS_TRANS_SB_DBLOCKS : -ASSERT (delta >0 ); tp->t_dblocks_delta += delta;break ;case XFS_TRANS_SB_AGCOUNT :
Signature · 1 line
2.27.0
Re: [PATCH v9 4/5] xfs: support shrinking unused space in the last AG
Quoted 85 lines · Gao Xiang
As the first step of shrinking, this attempts to enable shrinking unused space in the last allocation group by fixing up freespace btree, agi, agf and adjusting super block and use a helper
xfs_ag_shrink_space()to fixup the last AG.This can be all done in one transaction for now, so I think no additional protection is needed.
Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Gao Xiang <[email protected]>--- fs/xfs/xfs_fsops.c | 84 +++++++++++++++++++++++++++------------------- fs/xfs/xfs_trans.c | 1 - 2 files changed, 50 insertions(+), 35 deletions(-)fs/xfs/xfs_fsops.c +23
-11@@ -91,23 +91,25 @@ xfs_growfs_data_private(xfs_agnumber_t nagcount;xfs_agnumber_t nagimax =0 ;xfs_rfsblock_t nb, nb_div, nb_mod; -xfs_rfsblock_t delta; +int64_t delta;bool lastag_extended;xfs_agnumber_t oagcount;struct xfs_trans *tp;struct aghdr_init_data id = {}; nb = in->newblocks; -if (nb < mp->m_sb.sb_dblocks) -return -EINVAL ; -if ((error =xfs_sb_validate_fsb_count (&mp->m_sb, nb))) + error =xfs_sb_validate_fsb_count (&mp->m_sb, nb); +if (error)return error; - error =xfs_buf_read_uncached (mp->m_ddev_targp, + +if (nb > mp->m_sb.sb_dblocks) { + error =xfs_buf_read_uncached (mp->m_ddev_targp,XFS_FSB_TO_BB (mp, nb) -XFS_FSS_TO_BB (mp,1 ),XFS_FSS_TO_BB (mp,1 ),0 , &bp,NULL ); -if (error) -return error; -xfs_buf_relse (bp); +if (error) +return error; +xfs_buf_relse (bp); + } nb_div = nb; nb_mod =do_div (nb_div, mp->m_sb.sb_agblocks);@@ -115,10 +117,16 @@ xfs_growfs_data_private(if (nb_mod && nb_mod <XFS_MIN_AG_BLOCKS ) { nagcount--; nb = (xfs_rfsblock_t )nagcount * mp->m_sb.sb_agblocks; -if (nb < mp->m_sb.sb_dblocks) -return -EINVAL ; } delta = nb - mp->m_sb.sb_dblocks; +/* +* Reject filesystems with a single AG because they are not +* supported, and reject a shrink operation that would cause a +* filesystem to become unsupported. +*/ +if (delta <0 && nagcount <2 ) +return -EINVAL ; + oagcount = mp->m_sb.sb_agcount;/* allocate the new per-ag structures */ @@ -126,15 +134,22 @@ xfs_growfs_data_private( error =xfs_initialize_perag (mp, nagcount, &nagimax);if (error)return error; + }else if (nagcount < oagcount) { +/* TODO: shrinking the entire AGs hasn't yet completed */ +return -EINVAL ; } error =xfs_trans_alloc (mp, &M_RES (mp)->tr_growdata, -XFS_GROWFS_SPACE_RES (mp),0 ,XFS_TRANS_RESERVE , &tp); + (delta >0 ?XFS_GROWFS_SPACE_RES (mp) : -delta),0 , +XFS_TRANS_RESERVE , &tp);
fs/xfs/xfs_fsops.c xfs_growfs_data_private( · line 152
Quoted 10 lines
fs/xfs/xfs_fsops.c +5
-2if (error)return error; - error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, - delta, &lastag_extended); +if (delta >0 ) + error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, + delta, &lastag_extended); +else + error =xfs_ag_shrink_space (mp, &tp, nagcount -1 , -delta);
Assuming I don't hear anyone yelling NAK in the next day or so, I think I'll stage this for 5.13 with the following change to warn that the shrink feature is still EXPERIMENTAL:
By the way, are you going to send a patch to shrink the realtime device too?
fs/xfs/xfs_fsops.c +11 -2
@@ -145,11 +145,20 @@ xfs_growfs_data_private(if (error)return error; -if (delta >0 ) +if (delta >0 ) { error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, delta, &lastag_extended); -else + }else { +static struct ratelimit_state shrink_warning = \ +RATELIMIT_STATE_INIT ("shrink_warning" ,86400 *HZ ,1 ); +ratelimit_set_flags (&shrink_warning,RATELIMIT_MSG_ON_RELEASE ); + +if (__ratelimit (&shrink_warning)) +xfs_alert (mp, +"EXPERIMENTAL online shrink feature in use. Use at your own risk!" ); + error =xfs_ag_shrink_space (mp, &tp, nagcount -1 , -delta); + }if (error) goto out_trans_cancel;
--D
Quoted 69 lines
fs/xfs/xfs_fsops.c +22
-21if (error) goto out_trans_cancel;@@ -169,28 +184,29 @@ xfs_growfs_data_private(xfs_set_low_space_thresholds (mp); mp->m_alloc_set_aside =xfs_alloc_set_aside (mp); -/* -* If we expanded the last AG, free the per-AG reservation -* so we can reinitialize it with the new size. -*/ -if (lastag_extended) { -struct xfs_perag *pag; - - pag =xfs_perag_get (mp, id.agno); - error =xfs_ag_resv_free (pag); -xfs_perag_put (pag); -if (error) -return error; +if (delta >0 ) { +/* +* If we expanded the last AG, free the per-AG reservation +* so we can reinitialize it with the new size. +*/ +if (lastag_extended) { +struct xfs_perag *pag; + + pag =xfs_perag_get (mp, id.agno); + error =xfs_ag_resv_free (pag); +xfs_perag_put (pag); +if (error) +return error; + } +/* +* Reserve AG metadata blocks. ENOSPC here does not mean there +* was a growfs failure, just that there still isn't space for +* new user data after the grow has been run. +*/ + error =xfs_fs_reserve_ag_blocks (mp); +if (error == -ENOSPC ) + error =0 ; } - -/* -* Reserve AG metadata blocks. ENOSPC here does not mean there was a -* growfs failure, just that there still isn't space for new user data -* after the grow has been run. -*/ - error =xfs_fs_reserve_ag_blocks (mp); -if (error == -ENOSPC ) - error =0 ;return error; out_trans_cancel:fs/xfs/xfs_trans.c +0
-1@@ -436,7 +436,6 @@ xfs_trans_mod_sb( tp->t_res_frextents_delta += delta;break ;case XFS_TRANS_SB_DBLOCKS : -ASSERT (delta >0 ); tp->t_dblocks_delta += delta;break ;case XFS_TRANS_SB_AGCOUNT :Signature · 1 line
2.27.0
Re: [PATCH v9 4/5] xfs: support shrinking unused space in the last AG
Hi Darrick,
Quoted 101 lines · Darrick J. Wong
Quoted 85 lines · Gao Xiang
As the first step of shrinking, this attempts to enable shrinking unused space in the last allocation group by fixing up freespace btree, agi, agf and adjusting super block and use a helper
xfs_ag_shrink_space()to fixup the last AG.This can be all done in one transaction for now, so I think no additional protection is needed.
Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Gao Xiang <[email protected]>--- fs/xfs/xfs_fsops.c | 84 +++++++++++++++++++++++++++------------------- fs/xfs/xfs_trans.c | 1 - 2 files changed, 50 insertions(+), 35 deletions(-)fs/xfs/xfs_fsops.c +23
-11@@ -91,23 +91,25 @@ xfs_growfs_data_private(xfs_agnumber_t nagcount;xfs_agnumber_t nagimax =0 ;xfs_rfsblock_t nb, nb_div, nb_mod; -xfs_rfsblock_t delta; +int64_t delta;bool lastag_extended;xfs_agnumber_t oagcount;struct xfs_trans *tp;struct aghdr_init_data id = {}; nb = in->newblocks; -if (nb < mp->m_sb.sb_dblocks) -return -EINVAL ; -if ((error =xfs_sb_validate_fsb_count (&mp->m_sb, nb))) + error =xfs_sb_validate_fsb_count (&mp->m_sb, nb); +if (error)return error; - error =xfs_buf_read_uncached (mp->m_ddev_targp, + +if (nb > mp->m_sb.sb_dblocks) { + error =xfs_buf_read_uncached (mp->m_ddev_targp,XFS_FSB_TO_BB (mp, nb) -XFS_FSS_TO_BB (mp,1 ),XFS_FSS_TO_BB (mp,1 ),0 , &bp,NULL ); -if (error) -return error; -xfs_buf_relse (bp); +if (error) +return error; +xfs_buf_relse (bp); + } nb_div = nb; nb_mod =do_div (nb_div, mp->m_sb.sb_agblocks);@@ -115,10 +117,16 @@ xfs_growfs_data_private(if (nb_mod && nb_mod <XFS_MIN_AG_BLOCKS ) { nagcount--; nb = (xfs_rfsblock_t )nagcount * mp->m_sb.sb_agblocks; -if (nb < mp->m_sb.sb_dblocks) -return -EINVAL ; } delta = nb - mp->m_sb.sb_dblocks; +/* +* Reject filesystems with a single AG because they are not +* supported, and reject a shrink operation that would cause a +* filesystem to become unsupported. +*/ +if (delta <0 && nagcount <2 ) +return -EINVAL ; + oagcount = mp->m_sb.sb_agcount;/* allocate the new per-ag structures */ @@ -126,15 +134,22 @@ xfs_growfs_data_private( error =xfs_initialize_perag (mp, nagcount, &nagimax);if (error)return error; + }else if (nagcount < oagcount) { +/* TODO: shrinking the entire AGs hasn't yet completed */ +return -EINVAL ; } error =xfs_trans_alloc (mp, &M_RES (mp)->tr_growdata, -XFS_GROWFS_SPACE_RES (mp),0 ,XFS_TRANS_RESERVE , &tp); + (delta >0 ?XFS_GROWFS_SPACE_RES (mp) : -delta),0 , +XFS_TRANS_RESERVE , &tp);fs/xfs/xfs_fsops.c xfs_growfs_data_private( · line 152
Quoted 10 lines
fs/xfs/xfs_fsops.c +5
-2if (error)return error; - error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, - delta, &lastag_extended); +if (delta >0 ) + error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, + delta, &lastag_extended); +else + error =xfs_ag_shrink_space (mp, &tp, nagcount -1 , -delta);Assuming I don't hear anyone yelling NAK in the next day or so, I think I'll stage this for 5.13 with the following change to warn that the shrink feature is still EXPERIMENTAL:
Quoted 2 lines
By the way, are you going to send a patch to shrink the realtime device too?
My first priority will form the formal shrink-whole-AG patchset after this is finalized.
I have little experience about rt device, maybe leave it later.
Quoted 17 lines
fs/xfs/xfs_fsops.c +4
-2@@ -145,11 +145,20 @@ xfs_growfs_data_private(if (error)return error; -if (delta >0 ) +if (delta >0 ) { error =xfs_resizefs_init_new_ags (tp, &id, oagcount, nagcount, delta, &lastag_extended); -else + }else { +static struct ratelimit_state shrink_warning = \ +RATELIMIT_STATE_INIT ("shrink_warning" ,86400 *HZ ,1 );
fs/xfs/xfs_fsops.c xfs_growfs_data_private( · line 163
Quoted 10 lines
fs/xfs/xfs_fsops.c +7
-0+ratelimit_set_flags (&shrink_warning,RATELIMIT_MSG_ON_RELEASE ); + +if (__ratelimit (&shrink_warning)) +xfs_alert (mp, +"EXPERIMENTAL online shrink feature in use. Use at your own risk!" ); + error =xfs_ag_shrink_space (mp, &tp, nagcount -1 , -delta); + }if (error) goto out_trans_cancel;
I'm fine with that, thanks for updating. :)
Thanks,
Gao Xiang
Quoted 73 lines
fs/xfs/xfs_fsops.c +0
-1--D Quoted 70 lines
fs/xfs/xfs_fsops.c +22
-21if (error) goto out_trans_cancel;@@ -169,28 +184,29 @@ xfs_growfs_data_private(xfs_set_low_space_thresholds (mp); mp->m_alloc_set_aside =xfs_alloc_set_aside (mp); -/* -* If we expanded the last AG, free the per-AG reservation -* so we can reinitialize it with the new size. -*/ -if (lastag_extended) { -struct xfs_perag *pag; - - pag =xfs_perag_get (mp, id.agno); - error =xfs_ag_resv_free (pag); -xfs_perag_put (pag); -if (error) -return error; +if (delta >0 ) { +/* +* If we expanded the last AG, free the per-AG reservation +* so we can reinitialize it with the new size. +*/ +if (lastag_extended) { +struct xfs_perag *pag; + + pag =xfs_perag_get (mp, id.agno); + error =xfs_ag_resv_free (pag); +xfs_perag_put (pag); +if (error) +return error; + } +/* +* Reserve AG metadata blocks. ENOSPC here does not mean there +* was a growfs failure, just that there still isn't space for +* new user data after the grow has been run. +*/ + error =xfs_fs_reserve_ag_blocks (mp); +if (error == -ENOSPC ) + error =0 ; } - -/* -* Reserve AG metadata blocks. ENOSPC here does not mean there was a -* growfs failure, just that there still isn't space for new user data -* after the grow has been run. -*/ - error =xfs_fs_reserve_ag_blocks (mp); -if (error == -ENOSPC ) - error =0 ;return error; out_trans_cancel:fs/xfs/xfs_trans.c +0
-1@@ -436,7 +436,6 @@ xfs_trans_mod_sb( tp->t_res_frextents_delta += delta;break ;case XFS_TRANS_SB_DBLOCKS : -ASSERT (delta >0 ); tp->t_dblocks_delta += delta;break ;case XFS_TRANS_SB_AGCOUNT :Signature · 1 line
2.27.0
PATCH v9 5/5 xfs: add error injection for per-AG resv failure
In reply to Gao Xiangper-AG resv failure after fixing up freespace is hard to test in an effective way, so directly add an error injection path to observe such error handling path works as expected.
--- fs/xfs/libxfs/xfs_ag_resv.c | 6 +++++- fs/xfs/libxfs/xfs_errortag.h | 4 +++- fs/xfs/xfs_error.c | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_ag_resv.c +5 -1
@@ -211,7 +211,11 @@ __xfs_ag_resv_init(ASSERT (0 );return -EINVAL ; } - error =xfs_mod_fdblocks (mp, -(int64_t )hidden_space, true); + +if (XFS_TEST_ERROR (false, mp,XFS_ERRTAG_AG_RESV_FAIL )) + error = -ENOSPC ; +else + error =xfs_mod_fdblocks (mp, -(int64_t )hidden_space, true);if (error) {trace_xfs_ag_resv_init_error (pag->pag_mount, pag->pag_agno, error, _RET_IP_);
fs/xfs/libxfs/xfs_errortag.h +3 -1
@@ -58,7 +58,8 @@#define XFS_ERRTAG_BUF_IOERROR 35#define XFS_ERRTAG_REDUCE_MAX_IEXTENTS 36#define XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT 37 -#define XFS_ERRTAG_MAX 38 +#define XFS_ERRTAG_AG_RESV_FAIL 38 +#define XFS_ERRTAG_MAX 39/* * Random factors for above tags, 1 means always, 2 means 1/2 time, etc.
@@ -101,5 +102,6 @@#define XFS_RANDOM_BUF_IOERROR XFS_RANDOM_DEFAULT#define XFS_RANDOM_REDUCE_MAX_IEXTENTS 1#define XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT 1 +#define XFS_RANDOM_AG_RESV_FAIL 1#endif /* __XFS_ERRORTAG_H_ */
fs/xfs/xfs_error.c +3 -0
@@ -56,6 +56,7 @@ static unsigned int xfs_errortag_random_default[] = {XFS_RANDOM_BUF_IOERROR ,XFS_RANDOM_REDUCE_MAX_IEXTENTS ,XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT , + XFS_RANDOM_AG_RESV_FAIL, }; struct xfs_errortag_attr {
@@ -168,6 +169,7 @@ XFS_ERRORTAG_ATTR_RW(iunlink_fallback, XFS_ERRTAG_IUNLINK_FALLBACK);XFS_ERRORTAG_ATTR_RW (buf_ioerror,XFS_ERRTAG_BUF_IOERROR );XFS_ERRORTAG_ATTR_RW (reduce_max_iextents,XFS_ERRTAG_REDUCE_MAX_IEXTENTS );XFS_ERRORTAG_ATTR_RW (bmap_alloc_minlen_extent,XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT ); +XFS_ERRORTAG_ATTR_RW (ag_resv_fail,XFS_ERRTAG_AG_RESV_FAIL );static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (noerror),
@@ -208,6 +210,7 @@ static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (buf_ioerror),XFS_ERRORTAG_ATTR_LIST (reduce_max_iextents),XFS_ERRORTAG_ATTR_LIST (bmap_alloc_minlen_extent), +XFS_ERRORTAG_ATTR_LIST (ag_resv_fail), NULL, };
Signature · 1 line
2.27.0
Re: [PATCH v9 5/5] xfs: add error injection for per-AG resv failure
Quoted 5 lines · Gao Xiang
per-AG resv failure after fixing up freespace is hard to test in an effective way, so directly add an error injection path to observe such error handling path works as expected.
Signed-off-by Gao Xiang <[email protected]>
Looks good to me; can you send the latest version of the xfs_growfs patches to the list to get the review started for 5.13?
--D
Quoted 75 lines
--- fs/xfs/libxfs/xfs_ag_resv.c | 6 +++++- fs/xfs/libxfs/xfs_errortag.h | 4 +++- fs/xfs/xfs_error.c | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-)fs/xfs/libxfs/xfs_ag_resv.c +5
-1@@ -211,7 +211,11 @@ __xfs_ag_resv_init(ASSERT (0 );return -EINVAL ; } - error =xfs_mod_fdblocks (mp, -(int64_t )hidden_space, true); + +if (XFS_TEST_ERROR (false, mp,XFS_ERRTAG_AG_RESV_FAIL )) + error = -ENOSPC ; +else + error =xfs_mod_fdblocks (mp, -(int64_t )hidden_space, true);if (error) {trace_xfs_ag_resv_init_error (pag->pag_mount, pag->pag_agno, error, _RET_IP_);fs/xfs/libxfs/xfs_errortag.h +3
-1@@ -58,7 +58,8 @@#define XFS_ERRTAG_BUF_IOERROR 35#define XFS_ERRTAG_REDUCE_MAX_IEXTENTS 36#define XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT 37 -#define XFS_ERRTAG_MAX 38 +#define XFS_ERRTAG_AG_RESV_FAIL 38 +#define XFS_ERRTAG_MAX 39/* * Random factors for above tags, 1 means always, 2 means 1/2 time, etc. @@ -101,5 +102,6 @@#define XFS_RANDOM_BUF_IOERROR XFS_RANDOM_DEFAULT#define XFS_RANDOM_REDUCE_MAX_IEXTENTS 1#define XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT 1 +#define XFS_RANDOM_AG_RESV_FAIL 1#endif /* __XFS_ERRORTAG_H_ */ fs/xfs/xfs_error.c +3
-0@@ -56,6 +56,7 @@ static unsigned int xfs_errortag_random_default[] = {XFS_RANDOM_BUF_IOERROR ,XFS_RANDOM_REDUCE_MAX_IEXTENTS ,XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT , + XFS_RANDOM_AG_RESV_FAIL, }; struct xfs_errortag_attr {@@ -168,6 +169,7 @@ XFS_ERRORTAG_ATTR_RW(iunlink_fallback, XFS_ERRTAG_IUNLINK_FALLBACK);XFS_ERRORTAG_ATTR_RW (buf_ioerror,XFS_ERRTAG_BUF_IOERROR );XFS_ERRORTAG_ATTR_RW (reduce_max_iextents,XFS_ERRTAG_REDUCE_MAX_IEXTENTS );XFS_ERRORTAG_ATTR_RW (bmap_alloc_minlen_extent,XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT ); +XFS_ERRORTAG_ATTR_RW (ag_resv_fail,XFS_ERRTAG_AG_RESV_FAIL );static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (noerror),@@ -208,6 +210,7 @@ static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (buf_ioerror),XFS_ERRORTAG_ATTR_LIST (reduce_max_iextents),XFS_ERRORTAG_ATTR_LIST (bmap_alloc_minlen_extent), +XFS_ERRORTAG_ATTR_LIST (ag_resv_fail), NULL, };Signature · 1 line
2.27.0
Re: [PATCH v9 5/5] xfs: add error injection for per-AG resv failure
Quoted 9 lines · Darrick J. Wong
Quoted 5 lines · Gao Xiang
per-AG resv failure after fixing up freespace is hard to test in an effective way, so directly add an error injection path to observe such error handling path works as expected.
Signed-off-by Gao Xiang <[email protected]>Looks good to me; can you send the latest version of the
xfs_growfspatches to the list to get the review started for 5.13?
Yeah, I saw your comments on the xfs_growfs. I didn't send out just because it can still apply with no conflict. Will update manpage as well.
Quoted 2 lines
Reviewed-by Darrick J. Wong <[email protected]>
Thanks for the review.
Thanks,
Gao Xiang
Quoted 79 lines
--D
Quoted 75 lines
--- fs/xfs/libxfs/xfs_ag_resv.c | 6 +++++- fs/xfs/libxfs/xfs_errortag.h | 4 +++- fs/xfs/xfs_error.c | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-)fs/xfs/libxfs/xfs_ag_resv.c +5
-1@@ -211,7 +211,11 @@ __xfs_ag_resv_init(ASSERT (0 );return -EINVAL ; } - error =xfs_mod_fdblocks (mp, -(int64_t )hidden_space, true); + +if (XFS_TEST_ERROR (false, mp,XFS_ERRTAG_AG_RESV_FAIL )) + error = -ENOSPC ; +else + error =xfs_mod_fdblocks (mp, -(int64_t )hidden_space, true);if (error) {trace_xfs_ag_resv_init_error (pag->pag_mount, pag->pag_agno, error, _RET_IP_);fs/xfs/libxfs/xfs_errortag.h +3
-1@@ -58,7 +58,8 @@#define XFS_ERRTAG_BUF_IOERROR 35#define XFS_ERRTAG_REDUCE_MAX_IEXTENTS 36#define XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT 37 -#define XFS_ERRTAG_MAX 38 +#define XFS_ERRTAG_AG_RESV_FAIL 38 +#define XFS_ERRTAG_MAX 39/* * Random factors for above tags, 1 means always, 2 means 1/2 time, etc. @@ -101,5 +102,6 @@#define XFS_RANDOM_BUF_IOERROR XFS_RANDOM_DEFAULT#define XFS_RANDOM_REDUCE_MAX_IEXTENTS 1#define XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT 1 +#define XFS_RANDOM_AG_RESV_FAIL 1#endif /* __XFS_ERRORTAG_H_ */ fs/xfs/xfs_error.c +3
-0@@ -56,6 +56,7 @@ static unsigned int xfs_errortag_random_default[] = {XFS_RANDOM_BUF_IOERROR ,XFS_RANDOM_REDUCE_MAX_IEXTENTS ,XFS_RANDOM_BMAP_ALLOC_MINLEN_EXTENT , + XFS_RANDOM_AG_RESV_FAIL, }; struct xfs_errortag_attr {@@ -168,6 +169,7 @@ XFS_ERRORTAG_ATTR_RW(iunlink_fallback, XFS_ERRTAG_IUNLINK_FALLBACK);XFS_ERRORTAG_ATTR_RW (buf_ioerror,XFS_ERRTAG_BUF_IOERROR );XFS_ERRORTAG_ATTR_RW (reduce_max_iextents,XFS_ERRTAG_REDUCE_MAX_IEXTENTS );XFS_ERRORTAG_ATTR_RW (bmap_alloc_minlen_extent,XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT ); +XFS_ERRORTAG_ATTR_RW (ag_resv_fail,XFS_ERRTAG_AG_RESV_FAIL );static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (noerror),@@ -208,6 +210,7 @@ static struct attribute *xfs_errortag_attrs[] = {XFS_ERRORTAG_ATTR_LIST (buf_ioerror),XFS_ERRORTAG_ATTR_LIST (reduce_max_iextents),XFS_ERRORTAG_ATTR_LIST (bmap_alloc_minlen_extent), +XFS_ERRORTAG_ATTR_LIST (ag_resv_fail), NULL, };Signature · 1 line
2.27.0