Re: [PATCH v4 2/9] xfs: add a flag to convert unwritten extents without allocating blocks
From: Pankaj Raghav (Samsung)
Date: Thu Oct 22 2026 - 08:47:08 EST
On Tue, Oct 20, 2026 at 12:08:45PM +0900, Daejun Park wrote:
> xfs_bmapi_write() with XFS_BMAPI_CONVERT converts unwritten extents to
> written ones, and allocates blocks for any hole or delalloc extent it
> finds in the range. A caller that may only convert blocks that are
> already allocated has to look at the mapping first, and the mapping can
> change once the ILOCK is dropped.
>
> Add XFS_BMAPI_CONVERT_ONLY. With it, xfs_bmapi_write() stops at a hole
> or a delalloc extent instead of allocating blocks and returns the
> mappings that it converted before it. If the range starts with a hole or
> a delalloc extent, it returns -ENOSR, as it does when a delalloc
> conversion maps nothing in the range. Nothing has been changed then, and
> the caller decides what to do with its transaction.
>
> A flag of this name was removed by commit 26b91c728b2d ("xfs: make COW
> fork unwritten extent conversions more robust"). That one skipped the
> allocation and still mapped the hole; this one stops before it.
>
> "xfs: refuse a pNFS LAYOUTCOMMIT of a range without blocks" uses it,
> which is why this is marked for stable.
>
> Suggested-by: Christoph Hellwig <hch@xxxxxx>
> Link: https://lore.kernel.org/r/20261013120359.GB2944@xxxxxx
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Daejun Park <daejun7.park@xxxxxxxxxxx>
> Reviewed-by: Christoph Hellwig <hch@xxxxxx>
> ---
> fs/xfs/libxfs/xfs_bmap.c | 14 ++++++++++++--
> fs/xfs/libxfs/xfs_bmap.h | 10 +++++++++-
> 2 files changed, 21 insertions(+), 3 deletions(-)
>
> diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c
> index ca49bf4a50..95c0178b44 100644
> --- a/fs/xfs/libxfs/xfs_bmap.c
> +++ b/fs/xfs/libxfs/xfs_bmap.c
> @@ -4130,6 +4130,8 @@ xfs_bmapi_finish(
> * Returns a negative error code on failure, including -ENOSPC when it could not
> * allocate any blocks and -ENOSR when it did allocate blocks to convert a
> * delalloc range, but those blocks were before the passed in range.
> + * With XFS_BMAPI_CONVERT_ONLY, it stops at a hole or a delalloc extent instead
> + * of allocating blocks, and returns -ENOSR if it stops before mapping anything.
> */
> int
> xfs_bmapi_write(
> @@ -4189,6 +4191,8 @@ xfs_bmapi_write(
> */
> ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
> (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
> + ASSERT(!(flags & XFS_BMAPI_CONVERT_ONLY) ||
> + (flags & XFS_BMAPI_CONVERT));
>
> if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
> XFS_TEST_ERROR(mp, XFS_ERRTAG_BMAPIFORMAT)) {
> @@ -4237,6 +4241,9 @@ xfs_bmapi_write(
> * that we found, if any.
> */
> if (need_alloc || wasdelay) {
> + if (flags & XFS_BMAPI_CONVERT_ONLY)
> + break;
> +
> bma.eof = eof;
> bma.conv = !!(flags & XFS_BMAPI_CONVERT);
> bma.wasdel = wasdelay;
> @@ -4332,10 +4339,13 @@ xfs_bmapi_write(
> *
> * To avoid a successful return with *nmap set to 0, return the magic
> * -ENOSR error code for this particular case so that the caller can
> - * handle it.
> + * handle it. The same goes for XFS_BMAPI_CONVERT_ONLY stopping at a
> + * hole or a delalloc extent at the start of the range, in which case
> + * nothing has been changed.
> */
> if (!n) {
> - ASSERT(bma.nallocs >= *nmap);
> + ASSERT(bma.nallocs >= *nmap ||
> + (flags & XFS_BMAPI_CONVERT_ONLY));
Minor nit:
The following might be more explicit here:
ASSERT((flags & XFS_BMAPI_CONVERT_ONLY) ? bma.nallocs == 0 :
bma.nallocs >= *nmap);
Otherwise, the patch looks good.
Reviewed-by: Pankaj Raghav <p.raghav@xxxxxxxxxxx>
--
Pankaj