From: <gregkh@linuxfoundation.org>
To: CAEJPjCvT3Uag-pMTYuigEjWZHn1sGMZ0GCjVVCv29tNHK76Cgg@mail.gmail,catherine.hoang@oracle.com,chandan.babu@oracle.com,chandanbabu@kernel.org,djwong@kernel.org,gregkh@linuxfoundation.org,hch@lst.de,leah.rumancik@gmail.com,lyutoon@gmail.com,xfs-stable@lists.linux.dev
Cc: <stable-commits@vger.kernel.org>
Subject: Patch "xfs: fix error returns from xfs_bmapi_write" has been added to the 6.1-stable tree
Date: Mon, 05 May 2025 11:18:24 +0200 [thread overview]
Message-ID: <2025050524-ignore-visitor-e03f@gregkh> (raw)
In-Reply-To: <20250430212704.2905795-2-leah.rumancik@gmail.com>
This is a note to let you know that I've just added the patch titled
xfs: fix error returns from xfs_bmapi_write
to the 6.1-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
xfs-fix-error-returns-from-xfs_bmapi_write.patch
and it can be found in the queue-6.1 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@vger.kernel.org> know about it.
From stable+bounces-139228-greg=kroah.com@vger.kernel.org Wed Apr 30 23:31:33 2025
From: Leah Rumancik <leah.rumancik@gmail.com>
Date: Wed, 30 Apr 2025 14:26:48 -0700
Subject: xfs: fix error returns from xfs_bmapi_write
To: stable@vger.kernel.org
Cc: xfs-stable@lists.linux.dev, chandan.babu@oracle.com, catherine.hoang@oracle.com, djwong@kernel.org, "Christoph Hellwig" <hch@lst.de>, 刘通 <lyutoon@gmail.com>, "Chandan Babu R" <chandanbabu@kernel.org>, "Leah Rumancik" <leah.rumancik@gmail.com>
Message-ID: <20250430212704.2905795-2-leah.rumancik@gmail.com>
From: Christoph Hellwig <hch@lst.de>
[ Upstream commit 6773da870ab89123d1b513da63ed59e32a29cb77 ]
xfs_bmapi_write can return 0 without actually returning a mapping in
mval in two different cases:
1) when there is absolutely no space available to do an allocation
2) when converting delalloc space, and the allocation is so small
that it only covers parts of the delalloc extent before the
range requested by the caller
Callers at best can handle one of these cases, but in many cases can't
cope with either one. Switch xfs_bmapi_write to always return a
mapping or return an error code instead. For case 1) above ENOSPC is
the obvious choice which is very much what the callers expect anyway.
For case 2) there is no really good error code, so pick a funky one
from the SysV streams portfolio.
This fixes the reproducer here:
https://lore.kernel.org/linux-xfs/CAEJPjCvT3Uag-pMTYuigEjWZHn1sGMZ0GCjVVCv29tNHK76Cgg@mail.gmail.com0/
which uses reserved blocks to create file systems that are gravely
out of space and thus cause at least xfs_file_alloc_space to hang
and trigger the lack of ENOSPC handling in xfs_dquot_disk_alloc.
Note that this patch does not actually make any caller but
xfs_alloc_file_space deal intelligently with case 2) above.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reported-by: 刘通 <lyutoon@gmail.com>
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
Signed-off-by: Chandan Babu R <chandanbabu@kernel.org>
Signed-off-by: Leah Rumancik <leah.rumancik@gmail.com>
Acked-by: "Darrick J. Wong" <djwong@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
fs/xfs/libxfs/xfs_attr_remote.c | 1
fs/xfs/libxfs/xfs_bmap.c | 46 ++++++++++++++++++++++++++++++++--------
fs/xfs/libxfs/xfs_da_btree.c | 20 ++++-------------
fs/xfs/xfs_bmap_util.c | 31 +++++++++++++-------------
fs/xfs/xfs_dquot.c | 1
fs/xfs/xfs_iomap.c | 8 ------
fs/xfs/xfs_reflink.c | 14 ------------
fs/xfs/xfs_rtalloc.c | 2 -
8 files changed, 57 insertions(+), 66 deletions(-)
--- a/fs/xfs/libxfs/xfs_attr_remote.c
+++ b/fs/xfs/libxfs/xfs_attr_remote.c
@@ -619,7 +619,6 @@ xfs_attr_rmtval_set_blk(
if (error)
return error;
- ASSERT(nmap == 1);
ASSERT((map->br_startblock != DELAYSTARTBLOCK) &&
(map->br_startblock != HOLESTARTBLOCK));
--- a/fs/xfs/libxfs/xfs_bmap.c
+++ b/fs/xfs/libxfs/xfs_bmap.c
@@ -4113,8 +4113,10 @@ xfs_bmapi_allocate(
} else {
error = xfs_bmap_alloc_userdata(bma);
}
- if (error || bma->blkno == NULLFSBLOCK)
+ if (error)
return error;
+ if (bma->blkno == NULLFSBLOCK)
+ return -ENOSPC;
if (bma->flags & XFS_BMAPI_ZERO) {
error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
@@ -4294,6 +4296,15 @@ xfs_bmapi_finish(
* extent state if necessary. Details behaviour is controlled by the flags
* parameter. Only allocates blocks from a single allocation group, to avoid
* locking problems.
+ *
+ * Returns 0 on success and places the extent mappings in mval. nmaps is used
+ * as an input/output parameter where the caller specifies the maximum number
+ * of mappings that may be returned and xfs_bmapi_write passes back the number
+ * of mappings (including existing mappings) it found.
+ *
+ * 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.
*/
int
xfs_bmapi_write(
@@ -4421,10 +4432,16 @@ xfs_bmapi_write(
ASSERT(len > 0);
ASSERT(bma.length > 0);
error = xfs_bmapi_allocate(&bma);
- if (error)
+ if (error) {
+ /*
+ * If we already allocated space in a previous
+ * iteration return what we go so far when
+ * running out of space.
+ */
+ if (error == -ENOSPC && bma.nallocs)
+ break;
goto error0;
- if (bma.blkno == NULLFSBLOCK)
- break;
+ }
/*
* If this is a CoW allocation, record the data in
@@ -4462,7 +4479,6 @@ xfs_bmapi_write(
if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
eof = true;
}
- *nmap = n;
error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
whichfork);
@@ -4473,7 +4489,22 @@ xfs_bmapi_write(
ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
xfs_bmapi_finish(&bma, whichfork, 0);
xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
- orig_nmap, *nmap);
+ orig_nmap, n);
+
+ /*
+ * When converting delayed allocations, xfs_bmapi_allocate ignores
+ * the passed in bno and always converts from the start of the found
+ * delalloc extent.
+ *
+ * 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.
+ */
+ if (!n) {
+ ASSERT(bma.nallocs >= *nmap);
+ return -ENOSR;
+ }
+ *nmap = n;
return 0;
error0:
xfs_bmapi_finish(&bma, whichfork, error);
@@ -4580,9 +4611,6 @@ xfs_bmapi_convert_delalloc(
if (error)
goto out_finish;
- error = -ENOSPC;
- if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
- goto out_finish;
error = -EFSCORRUPTED;
if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock)))
goto out_finish;
--- a/fs/xfs/libxfs/xfs_da_btree.c
+++ b/fs/xfs/libxfs/xfs_da_btree.c
@@ -2158,8 +2158,8 @@ xfs_da_grow_inode_int(
struct xfs_inode *dp = args->dp;
int w = args->whichfork;
xfs_rfsblock_t nblks = dp->i_nblocks;
- struct xfs_bmbt_irec map, *mapp;
- int nmap, error, got, i, mapi;
+ struct xfs_bmbt_irec map, *mapp = ↦
+ int nmap, error, got, i, mapi = 1;
/*
* Find a spot in the file space to put the new block.
@@ -2175,14 +2175,7 @@ xfs_da_grow_inode_int(
error = xfs_bmapi_write(tp, dp, *bno, count,
xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
args->total, &map, &nmap);
- if (error)
- return error;
-
- ASSERT(nmap <= 1);
- if (nmap == 1) {
- mapp = ↦
- mapi = 1;
- } else if (nmap == 0 && count > 1) {
+ if (error == -ENOSPC && count > 1) {
xfs_fileoff_t b;
int c;
@@ -2199,16 +2192,13 @@ xfs_da_grow_inode_int(
args->total, &mapp[mapi], &nmap);
if (error)
goto out_free_map;
- if (nmap < 1)
- break;
mapi += nmap;
b = mapp[mapi - 1].br_startoff +
mapp[mapi - 1].br_blockcount;
}
- } else {
- mapi = 0;
- mapp = NULL;
}
+ if (error)
+ goto out_free_map;
/*
* Count the blocks we got, make sure it matches the total.
--- a/fs/xfs/xfs_bmap_util.c
+++ b/fs/xfs/xfs_bmap_util.c
@@ -868,33 +868,32 @@ xfs_alloc_file_space(
if (error)
goto error;
- error = xfs_bmapi_write(tp, ip, startoffset_fsb,
- allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
- &nimaps);
- if (error)
- goto error;
-
- ip->i_diflags |= XFS_DIFLAG_PREALLOC;
- xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
-
- error = xfs_trans_commit(tp);
- xfs_iunlock(ip, XFS_ILOCK_EXCL);
- if (error)
- break;
-
/*
* If the allocator cannot find a single free extent large
* enough to cover the start block of the requested range,
- * xfs_bmapi_write will return 0 but leave *nimaps set to 0.
+ * xfs_bmapi_write will return -ENOSR.
*
* In that case we simply need to keep looping with the same
* startoffset_fsb so that one of the following allocations
* will eventually reach the requested range.
*/
- if (nimaps) {
+ error = xfs_bmapi_write(tp, ip, startoffset_fsb,
+ allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
+ &nimaps);
+ if (error) {
+ if (error != -ENOSR)
+ goto error;
+ error = 0;
+ } else {
startoffset_fsb += imapp->br_blockcount;
allocatesize_fsb -= imapp->br_blockcount;
}
+
+ ip->i_diflags |= XFS_DIFLAG_PREALLOC;
+ xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
+
+ error = xfs_trans_commit(tp);
+ xfs_iunlock(ip, XFS_ILOCK_EXCL);
}
return error;
--- a/fs/xfs/xfs_dquot.c
+++ b/fs/xfs/xfs_dquot.c
@@ -333,7 +333,6 @@ xfs_dquot_disk_alloc(
goto err_cancel;
ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
- ASSERT(nmaps == 1);
ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
(map.br_startblock != HOLESTARTBLOCK));
--- a/fs/xfs/xfs_iomap.c
+++ b/fs/xfs/xfs_iomap.c
@@ -309,14 +309,6 @@ xfs_iomap_write_direct(
if (error)
goto out_unlock;
- /*
- * Copy any maps to caller's array and return any error.
- */
- if (nimaps == 0) {
- error = -ENOSPC;
- goto out_unlock;
- }
-
if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
error = xfs_alert_fsblock_zero(ip, imap);
--- a/fs/xfs/xfs_reflink.c
+++ b/fs/xfs/xfs_reflink.c
@@ -431,13 +431,6 @@ xfs_reflink_fill_cow_hole(
if (error)
return error;
- /*
- * Allocation succeeded but the requested range was not even partially
- * satisfied? Bail out!
- */
- if (nimaps == 0)
- return -ENOSPC;
-
convert:
return xfs_reflink_convert_unwritten(ip, imap, cmap, convert_now);
@@ -500,13 +493,6 @@ xfs_reflink_fill_delalloc(
error = xfs_trans_commit(tp);
if (error)
return error;
-
- /*
- * Allocation succeeded but the requested range was not even
- * partially satisfied? Bail out!
- */
- if (nimaps == 0)
- return -ENOSPC;
} while (cmap->br_startoff + cmap->br_blockcount <= imap->br_startoff);
return xfs_reflink_convert_unwritten(ip, imap, cmap, convert_now);
--- a/fs/xfs/xfs_rtalloc.c
+++ b/fs/xfs/xfs_rtalloc.c
@@ -840,8 +840,6 @@ xfs_growfs_rt_alloc(
nmap = 1;
error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
XFS_BMAPI_METADATA, 0, &map, &nmap);
- if (!error && nmap < 1)
- error = -ENOSPC;
if (error)
goto out_trans_cancel;
/*
Patches currently in stable-queue which might be from leah.rumancik@gmail.com are
queue-6.1/xfs-revert-commit-44af6c7e59b12.patch
queue-6.1/xfs-make-the-seq-argument-to-xfs_bmapi_convert_delalloc-optional.patch
queue-6.1/xfs-check-opcode-and-iovec-count-match-in-xlog_recover_attri_commit_pass2.patch
queue-6.1/xfs-allow-symlinks-with-short-remote-targets.patch
queue-6.1/xfs-match-lock-mode-in-xfs_buffered_write_iomap_begin.patch
queue-6.1/xfs-require-xfs_sb_feat_incompat_log_xattrs-for-attr-log-intent-item-recovery.patch
queue-6.1/xfs-allow-unlinked-symlinks-and-dirs-with-zero-size.patch
queue-6.1/xfs-restrict-when-we-try-to-align-cow-fork-delalloc-to-cowextsz-hints.patch
queue-6.1/xfs-fix-xfs_bmap_add_extent_delay_real-for-partial-conversions.patch
queue-6.1/xfs-validate-recovered-name-buffers-when-recovering-xattr-items.patch
queue-6.1/xfs-make-xfs_bmapi_convert_delalloc-to-allocate-the-target-offset.patch
queue-6.1/xfs-convert-delayed-extents-to-unwritten-when-zeroing-post-eof-blocks.patch
queue-6.1/xfs-fix-freeing-speculative-preallocations-for-preallocated-files.patch
queue-6.1/xfs-make-sure-sb_fdblocks-is-non-negative.patch
queue-6.1/xfs-remove-a-racy-if_bytes-check-in-xfs_reflink_end_cow_extent.patch
queue-6.1/xfs-fix-error-returns-from-xfs_bmapi_write.patch
next prev parent reply other threads:[~2025-05-05 9:18 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-30 21:26 [PATCH 6.1 00/16] xfs backports for 6.1.y from 6.10 Leah Rumancik
2025-04-30 21:26 ` [PATCH 6.1 01/16] xfs: fix error returns from xfs_bmapi_write Leah Rumancik
2025-05-05 9:18 ` gregkh [this message]
2025-04-30 21:26 ` [PATCH 6.1 02/16] xfs: fix xfs_bmap_add_extent_delay_real for partial conversions Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: fix xfs_bmap_add_extent_delay_real for partial conversions" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 03/16] xfs: remove a racy if_bytes check in xfs_reflink_end_cow_extent Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: remove a racy if_bytes check in xfs_reflink_end_cow_extent" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 04/16] xfs: require XFS_SB_FEAT_INCOMPAT_LOG_XATTRS for attr log intent item recovery Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: require XFS_SB_FEAT_INCOMPAT_LOG_XATTRS for attr log intent item recovery" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 05/16] xfs: check opcode and iovec count match in xlog_recover_attri_commit_pass2 Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: check opcode and iovec count match in xlog_recover_attri_commit_pass2" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 06/16] xfs: validate recovered name buffers when recovering xattr items Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: validate recovered name buffers when recovering xattr items" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 07/16] xfs: revert commit 44af6c7e59b12 Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: revert commit 44af6c7e59b12" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 08/16] xfs: match lock mode in xfs_buffered_write_iomap_begin() Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: match lock mode in xfs_buffered_write_iomap_begin()" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 09/16] xfs: make the seq argument to xfs_bmapi_convert_delalloc() optional Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: make the seq argument to xfs_bmapi_convert_delalloc() optional" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 10/16] xfs: make xfs_bmapi_convert_delalloc() to allocate the target offset Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: make xfs_bmapi_convert_delalloc() to allocate the target offset" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 11/16] xfs: convert delayed extents to unwritten when zeroing post eof blocks Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: convert delayed extents to unwritten when zeroing post eof blocks" has been added to the 6.1-stable tree gregkh
2025-04-30 21:26 ` [PATCH 6.1 12/16] xfs: allow symlinks with short remote targets Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: allow symlinks with short remote targets" has been added to the 6.1-stable tree gregkh
2025-04-30 21:27 ` [PATCH 6.1 13/16] xfs: make sure sb_fdblocks is non-negative Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: make sure sb_fdblocks is non-negative" has been added to the 6.1-stable tree gregkh
2025-04-30 21:27 ` [PATCH 6.1 14/16] xfs: fix freeing speculative preallocations for preallocated files Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: fix freeing speculative preallocations for preallocated files" has been added to the 6.1-stable tree gregkh
2025-04-30 21:27 ` [PATCH 6.1 15/16] xfs: allow unlinked symlinks and dirs with zero size Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: allow unlinked symlinks and dirs with zero size" has been added to the 6.1-stable tree gregkh
2025-04-30 21:27 ` [PATCH 6.1 16/16] xfs: restrict when we try to align cow fork delalloc to cowextsz hints Leah Rumancik
2025-05-05 9:18 ` Patch "xfs: restrict when we try to align cow fork delalloc to cowextsz hints" has been added to the 6.1-stable tree gregkh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2025050524-ignore-visitor-e03f@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=CAEJPjCvT3Uag-pMTYuigEjWZHn1sGMZ0GCjVVCv29tNHK76Cgg@mail.gmail \
--cc=catherine.hoang@oracle.com \
--cc=chandan.babu@oracle.com \
--cc=chandanbabu@kernel.org \
--cc=djwong@kernel.org \
--cc=hch@lst.de \
--cc=leah.rumancik@gmail.com \
--cc=lyutoon@gmail.com \
--cc=stable-commits@vger.kernel.org \
--cc=xfs-stable@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox