Re: [PATCH v2 1/2] xfs: add a XFS_TRANS_WRITECOUNT_TRYLOCK flag

From: Aditya Prakash Srivastava

Date: Fri Jun 12 2026 - 08:53:50 EST


Apologies for the mailing list noise in v2; a local stale patch file
collision caused the wrong files and version headers to be sent.
Please ignore this version v2 completely and take a look at the
clean v3 series.

Thanks
Aditya Prakash Srivastava

On Fri, Jun 12, 2026 at 5:20 PM Aditya Srivastava
<aditya.ansh182@xxxxxxxxx> wrote:
>
> From: Aditya Prakash Srivastava <aditya.ansh182@xxxxxxxxx>
>
> Introduce a new transaction allocation flag, XFS_TRANS_WRITECOUNT_TRYLOCK.
> When this flag is specified, __xfs_trans_alloc() attempts to obtain
> freeze protection using sb_start_intwrite_trylock() instead of blocking
> indefinitely on sb_start_intwrite().
>
> If the trylock fails, the allocation is aborted gracefully: the freshly
> allocated transaction handle is freed, and the function returns the
> appropriate error pointer ERR_PTR(-EAGAIN), which is then propagated
> to the caller by xfs_trans_alloc().
>
> Also add an assertion in __xfs_trans_alloc() to ensure that both
> XFS_TRANS_NO_WRITECOUNT and XFS_TRANS_WRITECOUNT_TRYLOCK are never
> specified at the same time, as they are mutually exclusive.
>
> Suggested-by: Christoph Hellwig <hch@xxxxxxxxxxxxx>
> Signed-off-by: Aditya Prakash Srivastava <aditya.ansh182@xxxxxxxxx>
> ---
> fs/xfs/libxfs/xfs_shared.h | 3 +++
> fs/xfs/xfs_trans.c | 12 +++++++++++-
> 2 files changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/fs/xfs/libxfs/xfs_shared.h b/fs/xfs/libxfs/xfs_shared.h
> index b1e0d9bc1f7d..68d22b6cddd3 100644
> --- a/fs/xfs/libxfs/xfs_shared.h
> +++ b/fs/xfs/libxfs/xfs_shared.h
> @@ -164,6 +164,9 @@ void xfs_log_get_max_trans_res(struct xfs_mount *mp,
> /* Transaction has locked the rtbitmap and rtsum inodes */
> #define XFS_TRANS_RTBITMAP_LOCKED (1u << 9)
>
> +/* Try lock filesystem superblock for freeze protection */
> +#define XFS_TRANS_WRITECOUNT_TRYLOCK (1u << 10)
> +
> /*
> * Field values for xfs_trans_mod_sb.
> */
> diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> index 148cc32449c1..3860e44d6439 100644
> --- a/fs/xfs/xfs_trans.c
> +++ b/fs/xfs/xfs_trans.c
> @@ -216,10 +216,18 @@ __xfs_trans_alloc(
> struct xfs_trans *tp;
>
> ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) || xfs_has_lazysbcount(mp));
> + ASSERT(!((flags & XFS_TRANS_NO_WRITECOUNT) &&
> + (flags & XFS_TRANS_WRITECOUNT_TRYLOCK)));
>
> tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
> - if (!(flags & XFS_TRANS_NO_WRITECOUNT))
> + if (flags & XFS_TRANS_WRITECOUNT_TRYLOCK) {
> + if (!sb_start_intwrite_trylock(mp->m_super)) {
> + kmem_cache_free(xfs_trans_cache, tp);
> + return ERR_PTR(-EAGAIN);
> + }
> + } else if (!(flags & XFS_TRANS_NO_WRITECOUNT)) {
> sb_start_intwrite(mp->m_super);
> + }
> xfs_trans_set_context(tp);
> tp->t_flags = flags;
> tp->t_mountp = mp;
> @@ -252,6 +260,8 @@ xfs_trans_alloc(
> */
> retry:
> tp = __xfs_trans_alloc(mp, flags);
> + if (IS_ERR(tp))
> + return PTR_ERR(tp);
> WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
> error = xfs_trans_reserve(tp, resp, blocks, rtextents);
> if (error == -ENOSPC && want_retry) {
> --
> 2.47.3
>