Skip to contentSkip to conversation

xfs: flush delalloc blocks on ENOSPC when creating symlinks

PATCH In review Ravi Singh 3 participants 9 messages Apr 15 – Apr 22

Subsystems (2)
lore
Share & tools
lore

Ravi Singh

author
#0 lore raw

PATCH 1/1 xfs: flush delalloc blocks on ENOSPC when creating symlinks

When creating symlinks on a nearly-full XFS filesystem, xfs_symlink() fails with ENOSPC even though space could be reclaimed from speculative preallocations. xfs_create() already handles this by flushing outstanding delalloc blocks via xfs_flush_inodes() and retrying the transaction allocation, but xfs_symlink() returns the error immediately.

Add the same ENOSPC flush+retry logic to xfs_symlink() so that symlink creation behaves consistently with file creation.

---
 fs/xfs/xfs_symlink.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/xfs/xfs_symlink.c +6 -0
index c4da624fb296..bf3cf23892eb 100644
@@ -148,6 +148,12 @@ xfs_symlink(
 
 	error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink, udqp, gdqp,
 			pdqp, resblks, &tp);
+	if (error == -ENOSPC) {
+		/* flush outstanding delalloc blocks and retry */
+		xfs_flush_inodes(mp);
+		error = xfs_trans_alloc_icreate(mp, &M_RES(mp)->tr_symlink,
+				udqp, gdqp, pdqp, resblks, &tp);
+	}
 	if (error)
 		goto out_parent;
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 9 lines · Ravi Singh

When creating symlinks on a nearly-full XFS filesystem, xfs_symlink() fails with ENOSPC even though space could be reclaimed from speculative preallocations. xfs_create() already handles this by flushing outstanding delalloc blocks via xfs_flush_inodes() and retrying the transaction allocation, but xfs_symlink() returns the error immediately.

Add the same ENOSPC flush+retry logic to xfs_symlink() so that symlink creation behaves consistently with file creation.

While we're at it - do we also want it for tempfiles and scrub? i.e., should we move this logic into xfs_trans_alloc_icreate, similar to the quota logic?

author
#2 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Ravi Singh

When creating symlinks on a nearly-full XFS filesystem, xfs_symlink() fails with ENOSPC even though space could be reclaimed from speculative preallocations. xfs_create() already handles this by flushing outstanding delalloc blocks via xfs_flush_inodes() and retrying the transaction allocation, but xfs_symlink() returns the error immediately.

Quoted 7 lines
Quoted 2 lines

Add the same ENOSPC flush+retry logic to xfs_symlink() so that symlink creation behaves consistently with file creation.

While we're at it - do we also want it for tempfiles and scrub? i.e., should we move this logic into xfs_trans_alloc_icreate, similar to the quota logic?

tmpfiles and scrub tempfiles are created without directory entries, so they don't need a new directory block allocation and don't hit this ENOSPC path.

As for moving the logic into xfs_trans_alloc_icreate(), xfs_flush_inodes() is a heavier operation (sync_inodes_sb). Pushing it down into xfs_trans_alloc_icreate() would make every caller pay for that flush, including paths that don't need it.

I think keeping it in the callers that actually need it (xfs_create and xfs_symlink) is the right scope.

#3 lore raw
Quoted 21 lines · Ravi Singh
Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Ravi Singh

When creating symlinks on a nearly-full XFS filesystem, xfs_symlink() fails with ENOSPC even though space could be reclaimed from speculative preallocations. xfs_create() already handles this by flushing outstanding delalloc blocks via xfs_flush_inodes() and retrying the transaction allocation, but xfs_symlink() returns the error immediately.

Quoted 7 lines
Quoted 2 lines

Add the same ENOSPC flush+retry logic to xfs_symlink() so that symlink creation behaves consistently with file creation.

While we're at it - do we also want it for tempfiles and scrub? i.e., should we move this logic into xfs_trans_alloc_icreate, similar to the quota logic?

tmpfiles and scrub tempfiles are created without directory entries, so they don't need a new directory block allocation and don't hit this ENOSPC path.

Quoted 7 lines

As for moving the logic into xfs_trans_alloc_icreate(), xfs_flush_inodes() is a heavier operation (sync_inodes_sb). Pushing it down into xfs_trans_alloc_icreate() would make every caller pay for that flush, including paths that don't need it.

I think keeping it in the callers that actually need it (xfs_create and xfs_symlink) is the right scope.

I think that's correct. I had looked at this and came to the same conclusions after also thinking i.e. tmpfiles needed the same treatment, couldn't reproduce it, then realized it's because it's directory block allocation that needs the freed up space, something tmpfiles don't require.

Thanks,
-Eric

#4 lore raw
In reply to Ravi Singh
Quoted 7 lines · Ravi Singh
Quoted 4 lines

While we're at it - do we also want it for tempfiles and scrub? i.e., should we move this logic into xfs_trans_alloc_icreate, similar to the quota logic?

tmpfiles and scrub tempfiles are created without directory entries, so they don't need a new directory block allocation and don't hit this ENOSPC path.

But we still ned the ialloc space reservation for all these cases.

Quoted 4 lines

As for moving the logic into xfs_trans_alloc_icreate(), xfs_flush_inodes() is a heavier operation (sync_inodes_sb). Pushing it down into xfs_trans_alloc_icreate() would make every caller pay for that flush, including paths that don't need it.

Only if we git enospc and need to restart the underlying xfs_trans_alloc call, similar to how it handles quotas.

author
#5 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Ravi Singh
Quoted 4 lines

While we're at it - do we also want it for tempfiles and scrub? i.e., should we move this logic into xfs_trans_alloc_icreate, similar to the quota logic?

Quoted 7 lines
Quoted 3 lines

tmpfiles and scrub tempfiles are created without directory entries, so they don't need a new directory block allocation and don't hit this ENOSPC path.

But we still ned the ialloc space reservation for all these cases.

You're right - the ENOSPC comes from xfs_trans_alloc() failing to reserve blocks via xfs_dec_fdblocks(), which is the same
global counter for all callers. Even the smaller
XFS_IALLOC_SPACE_RES reservation for tmpfiles can fail there when delalloc pressure has consumed the available fdblocks.

I did try several runs to reproduce the ENOSPC for tmpfile creation, but wasn't able to trigger it in practice - the smaller reservation makes it much harder to hit. That said, the code path is the same and it makes sense to handle it centrally rather than leave the gap.

Quoted 9 lines
Quoted 4 lines

As for moving the logic into xfs_trans_alloc_icreate(), xfs_flush_inodes() is a heavier operation (sync_inodes_sb). Pushing it down into xfs_trans_alloc_icreate() would make every caller pay for that flush, including paths that don't need it.

Only if we git enospc and need to restart the underlying xfs_trans_alloc call, similar to how it handles quotas.

v2 moves the xfs_flush_inodes() retry into xfs_trans_alloc_icreate() and removes the existing caller-level retry from xfs_create()

Thanks,
Ravi

author
#6 lore raw

PATCH v2 1/1 xfs: flush delalloc blocks on ENOSPC in xfs_trans_alloc_icreate

In reply to Christoph Hellwig

xfs_trans_alloc_icreate() can fail with ENOSPC when delalloc reservations have consumed most of the available block count (fdblocks). xfs_trans_alloc() already retries internally with xfs_blockgc_flush_all(), but that only trims post-EOF speculative preallocation and may not free enough space for the transaction reservation.

Add a retry with xfs_flush_inodes() when xfs_trans_alloc() returns ENOSPC. This forces writeback of all dirty inodes via sync_inodes_sb(), converting delalloc reservations to real allocations and freeing the over-reserved portion back to fdblocks.

This fixes all callers of xfs_trans_alloc_icreate() and removes the existing caller-level retry from xfs_create(), which is now handled centrally.

---

v2:
- Moved xfs_flush_inodes() retry into xfs_trans_alloc_icreate() instead of xfs_symlink() (Christoph)
- Removed caller-level retry from xfs_create()
- All callers now covered: xfs_create, xfs_symlink, xfs_create_tmpfile, xcrep_tempfile_create

 fs/xfs/xfs_inode.c | 6 ------
 fs/xfs/xfs_trans.c | 9 +++++++++
 2 files changed, 9 insertions(+), 6 deletions(-)
fs/xfs/xfs_inode.c +0 -6
index beaa26ec62da..9978ac1422fc 100644
@@ -699,12 +699,6 @@ xfs_create(
 	 */
 	error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks,
 			&tp);
-	if (error == -ENOSPC) {
-		/* flush outstanding delalloc blocks and retry */
-		xfs_flush_inodes(mp);
-		error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp,
-				resblks, &tp);
-	}
 	if (error)
 		goto out_parent;
 
fs/xfs/xfs_trans.c +9 -0
index bcc470f56e46..2796634b7d2b 100644
@@ -1186,6 +1186,9 @@ xfs_trans_reserve_more_inode(
 /*
  * Allocate an transaction in preparation for inode creation by reserving quota
  * against the given dquots.  Callers are not required to hold any inode locks.
+ * If the block reservation fails with ENOSPC, flush all delalloc blocks to
+ * reclaim space from speculative preallocation and retry.  This is similar to
+ * the quota retry but targets FS-wide ENOSPC.
  */
 int
 xfs_trans_alloc_icreate(
@@ -1199,10 +1202,16 @@ xfs_trans_alloc_icreate(
 {
 	struct xfs_trans	*tp;
 	bool			retried = false;
+	bool			flushed = false;
 	int			error;
 
 retry:
 	error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
+	if (error == -ENOSPC && !flushed) {
+		xfs_flush_inodes(mp);
+		flushed = true;
+		goto retry;
+	}
 	if (error)
 		return error;
 
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH v2] xfs: flush delalloc blocks on ENOSPC in xfs_trans_alloc_icreate

Note: posting the v2 as a reply is a good way to lose track of it, don't do that.

Quoted 3 lines

+ * If the block reservation fails with ENOSPC, flush all delalloc blocks to
+ * reclaim space from speculative preallocation and retry. This is similar to
+ * the quota retry but targets FS-wide ENOSPC.

Can you move this code to sit above the code that implements the logic? It is much more helpful there than at the top of the file.

Otherwise this looks good.

author
#8 lore raw

Re: [PATCH v2] xfs: flush delalloc blocks on ENOSPC in xfs_trans_alloc_icreate

Quoted 4 lines · Christoph Hellwig

Note: posting the v2 as a reply is a good way to lose track of it, don't do that.

Understood, will post v3 as a new thread.

Quoted 10 lines
Quoted 3 lines

+ * If the block reservation fails with ENOSPC, flush all delalloc blocks to
+ * reclaim space from speculative preallocation and retry. This is similar to
+ * the quota retry but targets FS-wide ENOSPC.

Can you move this code to sit above the code that implements the logic? It is much more helpful there than at the top of the file.

Otherwise this looks good.

Thanks. v3 coming shortly with the comment moved.

Thanks,
Ravi