Skip to contentSkip to conversation

Zoned device cleanups

PATCH Superseded [email protected] 3 participants 13 messages Mar 10 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover Zoned device cleanups

From: Carlos Maiolino <[email protected]>

These patches initially were part from an attempt to implement atomic writes for zoned devices.

Due to unforeseen circumstances, we can't implement it, at least for now without restructuring a big part of the code.

Christoph though suggested those cleanups were good enough to be sent even without proceeding with the implementation.

Carlos Maiolino (4):
xfs: factor out isize updates from xfs_dio_write_end_io
xfs: move zoned dio ioend to its own function
xfs: factor out xfs_zone_inc_written
xfs: opencode xfs_zone_record_blocks

 fs/xfs/xfs_file.c       | 94 +++++++++++++++++++++++++++++------------
 fs/xfs/xfs_zone_alloc.c | 32 ++++++--------
 2 files changed, 81 insertions(+), 45 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/4 xfs: factor out isize updates from xfs_dio_write_end_io

From: Carlos Maiolino <[email protected]>

This is the only code needed for zoned inodes, so factor it out so we can move zoned inodes ioend to its own callback.

---
 fs/xfs/xfs_file.c | 59 +++++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 23 deletions(-)
fs/xfs/xfs_file.c +36 -23
index 6246f34df9fd..45ecd743fa32 100644
@@ -560,6 +560,41 @@ xfs_zoned_write_space_reserve(
 			flags, ac);
 }
 
+/*
+ * We need to lock the test/set EOF update as we can be racing with
+ * other IO completions here to update the EOF. Failing to serialise
+ * here can result in EOF moving backwards and Bad Things Happen when
+ * that occurs.
+ *
+ * As IO completion only ever extends EOF, we can do an unlocked check
+ * here to avoid taking the spinlock. If we land within the current EOF,
+ * then we do not need to do an extending update at all, and we don't
+ * need to take the lock to check this. If we race with an update moving
+ * EOF, then we'll either still be beyond EOF and need to take the lock,
+ * or we'll be within EOF and we don't need to take it at all.
+ */
+static int
+xfs_dio_endio_set_isize(
+	struct inode		*inode,
+	loff_t			offset,
+	ssize_t			size)
+{
+	struct xfs_inode	*ip = XFS_I(inode);
+
+	if (offset + size <= i_size_read(inode))
+		return 0;
+
+	spin_lock(&ip->i_flags_lock);
+	if (offset + size > i_size_read(inode)) {
+		i_size_write(inode, offset + size);
+		spin_unlock(&ip->i_flags_lock);
+	} else {
+		spin_unlock(&ip->i_flags_lock);
+	}
+
+	return xfs_setfilesize(ip, offset, size);
+}
+
 static int
 xfs_dio_write_end_io(
 	struct kiocb		*iocb,
@@ -623,30 +658,8 @@ xfs_dio_write_end_io(
 	 * with the on-disk inode size being outside the in-core inode size. We
 	 * have no other method of updating EOF for AIO, so always do it here
 	 * if necessary.
-	 *
-	 * We need to lock the test/set EOF update as we can be racing with
-	 * other IO completions here to update the EOF. Failing to serialise
-	 * here can result in EOF moving backwards and Bad Things Happen when
-	 * that occurs.
-	 *
-	 * As IO completion only ever extends EOF, we can do an unlocked check
-	 * here to avoid taking the spinlock. If we land within the current EOF,
-	 * then we do not need to do an extending update at all, and we don't
-	 * need to take the lock to check this. If we race with an update moving
-	 * EOF, then we'll either still be beyond EOF and need to take the lock,
-	 * or we'll be within EOF and we don't need to take it at all.
 	 */
-	if (offset + size <= i_size_read(inode))
-		goto out;
-
-	spin_lock(&ip->i_flags_lock);
-	if (offset + size > i_size_read(inode)) {
-		i_size_write(inode, offset + size);
-		spin_unlock(&ip->i_flags_lock);
-		error = xfs_setfilesize(ip, offset, size);
-	} else {
-		spin_unlock(&ip->i_flags_lock);
-	}
+	error = xfs_dio_endio_set_isize(inode, offset, size);
 
 out:
 	memalloc_nofs_restore(nofs_flag);
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/4] xfs: factor out isize updates from xfs_dio_write_end_io

Quoted 39 lines · On 2026/03/10 12:55, [email protected] wrote:

From: Carlos Maiolino <[email protected]>

This is the only code needed for zoned inodes, so factor it out so we can move zoned inodes ioend to its own callback.

---
 fs/xfs/xfs_file.c | 59 +++++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 23 deletions(-)
fs/xfs/xfs_file.c +21 -0
index 6246f34df9fd..45ecd743fa32 100644
@@ -560,6 +560,41 @@ xfs_zoned_write_space_reserve(
 			flags, ac);
 }
 
+/*
+ * We need to lock the test/set EOF update as we can be racing with
+ * other IO completions here to update the EOF. Failing to serialise
+ * here can result in EOF moving backwards and Bad Things Happen when
+ * that occurs.
+ *
+ * As IO completion only ever extends EOF, we can do an unlocked check
+ * here to avoid taking the spinlock. If we land within the current EOF,
+ * then we do not need to do an extending update at all, and we don't
+ * need to take the lock to check this. If we race with an update moving
+ * EOF, then we'll either still be beyond EOF and need to take the lock,
+ * or we'll be within EOF and we don't need to take it at all.
+ */
+static int
+xfs_dio_endio_set_isize(
+	struct inode		*inode,
+	loff_t			offset,
+	ssize_t			size)
+{
+	struct xfs_inode	*ip = XFS_I(inode);
+

fs/xfs/xfs_file.c xfs_zoned_write_space_reserve( · line 593

Quoted 10 lines
fs/xfs/xfs_file.c +10 -0
+	if (offset + size <= i_size_read(inode))
+		return 0;
+
+	spin_lock(&ip->i_flags_lock);
+	if (offset + size > i_size_read(inode)) {
+		i_size_write(inode, offset + size);
+		spin_unlock(&ip->i_flags_lock);
+	} else {
+		spin_unlock(&ip->i_flags_lock);
+	}

The spinlock unlock does not need to be inside the if and else:

+ spin_lock(&ip->i_flags_lock);
+ if (offset + size > i_size_read(inode))
+ i_size_write(inode, offset + size);
+ spin_unlock(&ip->i_flags_lock);

Other than this, looks OK to me.

Quoted 39 lines
fs/xfs/xfs_file.c +5 -23
+
+	return xfs_setfilesize(ip, offset, size);
+}
+
 static int
 xfs_dio_write_end_io(
 	struct kiocb		*iocb,
@@ -623,30 +658,8 @@ xfs_dio_write_end_io(
 	 * with the on-disk inode size being outside the in-core inode size. We
 	 * have no other method of updating EOF for AIO, so always do it here
 	 * if necessary.
-	 *
-	 * We need to lock the test/set EOF update as we can be racing with
-	 * other IO completions here to update the EOF. Failing to serialise
-	 * here can result in EOF moving backwards and Bad Things Happen when
-	 * that occurs.
-	 *
-	 * As IO completion only ever extends EOF, we can do an unlocked check
-	 * here to avoid taking the spinlock. If we land within the current EOF,
-	 * then we do not need to do an extending update at all, and we don't
-	 * need to take the lock to check this. If we race with an update moving
-	 * EOF, then we'll either still be beyond EOF and need to take the lock,
-	 * or we'll be within EOF and we don't need to take it at all.
 	 */
-	if (offset + size <= i_size_read(inode))
-		goto out;
-
-	spin_lock(&ip->i_flags_lock);
-	if (offset + size > i_size_read(inode)) {
-		i_size_write(inode, offset + size);
-		spin_unlock(&ip->i_flags_lock);
-		error = xfs_setfilesize(ip, offset, size);
-	} else {
-		spin_unlock(&ip->i_flags_lock);
-	}
+	error = xfs_dio_endio_set_isize(inode, offset, size);
 
 out:
 	memalloc_nofs_restore(nofs_flag);
Signature · 2 lines
Damien Le Moal
Western Digital Research
#3 lore raw

Re: [PATCH 1/4] xfs: factor out isize updates from xfs_dio_write_end_io

In reply to [email protected]
Quoted 10 lines · On Tue, Mar 10, 2026 at 12:55:04PM +0100, [email protected] wrote:
fs/xfs/xfs_file.c +10 -0
+static int
+xfs_dio_endio_set_isize(
+	struct inode		*inode,
+	loff_t			offset,
+	ssize_t			size)
+{
+	struct xfs_inode	*ip = XFS_I(inode);
+
+	if (offset + size <= i_size_read(inode))
+		return 0;

fs/xfs/xfs_file.c xfs_zoned_write_space_reserve( · line 595

Quoted 10 lines
fs/xfs/xfs_file.c +10 -0
+
+	spin_lock(&ip->i_flags_lock);
+	if (offset + size > i_size_read(inode)) {
+		i_size_write(inode, offset + size);
+		spin_unlock(&ip->i_flags_lock);
+	} else {
+		spin_unlock(&ip->i_flags_lock);
+	}
+
+	return xfs_setfilesize(ip, offset, size);

This now calls xfs_setfilesize for the case where we don't update the inode size. This looks harmless to be, but isn't needed.

This should become something like:

	if (offset + size <= i_size_read(inode)) {
		spin_unlock(&ip->i_flags_lock);
		return 0;
	}
	i_size_write(inode, offset + size);
	spin_unlock(&ip->i_flags_lock);
	return xfs_setfilesize(ip, offset, size);
author maintainer
#4 lore raw

PATCH 2/4 xfs: move zoned dio ioend to its own function

In reply to [email protected]

From: Carlos Maiolino <[email protected]>

All zoned dio needs during endio is set the file size, so move it out of the generic function.

---
 fs/xfs/xfs_file.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
fs/xfs/xfs_file.c +32 -3
index 45ecd743fa32..29b02673111b 100644
@@ -595,6 +595,36 @@ xfs_dio_endio_set_isize(
 	return xfs_setfilesize(ip, offset, size);
 }
 
+static int
+xfs_zoned_dio_write_end_io(
+	struct kiocb		*iocb,
+	ssize_t			size,
+	int			error,
+	unsigned		flags)
+{
+	struct inode		*inode = file_inode(iocb->ki_filp);
+	struct xfs_inode	*ip = XFS_I(inode);
+	unsigned int		nofs_flag;
+
+	ASSERT(!(flags & (IOMAP_DIO_UNWRITTEN | IOMAP_DIO_COW)));
+
+	trace_xfs_end_io_direct_write(ip, iocb->ki_pos, size);
+
+	if (xfs_is_shutdown(ip->i_mount))
+		return -EIO;
+
+	if (error || !size)
+		return error;
+
+	XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
+
+	nofs_flag = memalloc_nofs_save();
+	error = xfs_dio_endio_set_isize(inode, iocb->ki_pos, size);
+	memalloc_nofs_restore(nofs_flag);
+
+	return error;
+}
+
 static int
 xfs_dio_write_end_io(
 	struct kiocb		*iocb,
@@ -607,8 +637,7 @@ xfs_dio_write_end_io(
 	loff_t			offset = iocb->ki_pos;
 	unsigned int		nofs_flag;
 
-	ASSERT(!xfs_is_zoned_inode(ip) ||
-	       !(flags & (IOMAP_DIO_UNWRITTEN | IOMAP_DIO_COW)));
+	ASSERT(!xfs_is_zoned_inode(ip));
 
 	trace_xfs_end_io_direct_write(ip, offset, size);
 
@@ -701,7 +730,7 @@ xfs_dio_zoned_submit_io(
 static const struct iomap_dio_ops xfs_dio_zoned_write_ops = {
 	.bio_set	= &iomap_ioend_bioset,
 	.submit_io	= xfs_dio_zoned_submit_io,
-	.end_io		= xfs_dio_write_end_io,
+	.end_io		= xfs_zoned_dio_write_end_io,
 };
 
 /*
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 2/4] xfs: move zoned dio ioend to its own function

In reply to [email protected]
Quoted 4 lines · On 2026/03/10 12:55, [email protected] wrote:

From: Carlos Maiolino <[email protected]>

All zoned dio needs during endio is set the file size, so move it out of the generic function.

"move" is unclear here since this patch is adding code but not deleting anything. Maye be rephrase this ?

Quoted 66 lines
---
 fs/xfs/xfs_file.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
fs/xfs/xfs_file.c +32 -3
index 45ecd743fa32..29b02673111b 100644
@@ -595,6 +595,36 @@ xfs_dio_endio_set_isize(
 	return xfs_setfilesize(ip, offset, size);
 }
 
+static int
+xfs_zoned_dio_write_end_io(
+	struct kiocb		*iocb,
+	ssize_t			size,
+	int			error,
+	unsigned		flags)
+{
+	struct inode		*inode = file_inode(iocb->ki_filp);
+	struct xfs_inode	*ip = XFS_I(inode);
+	unsigned int		nofs_flag;
+
+	ASSERT(!(flags & (IOMAP_DIO_UNWRITTEN | IOMAP_DIO_COW)));
+
+	trace_xfs_end_io_direct_write(ip, iocb->ki_pos, size);
+
+	if (xfs_is_shutdown(ip->i_mount))
+		return -EIO;
+
+	if (error || !size)
+		return error;
+
+	XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
+
+	nofs_flag = memalloc_nofs_save();
+	error = xfs_dio_endio_set_isize(inode, iocb->ki_pos, size);
+	memalloc_nofs_restore(nofs_flag);
+
+	return error;
+}
+
 static int
 xfs_dio_write_end_io(
 	struct kiocb		*iocb,
@@ -607,8 +637,7 @@ xfs_dio_write_end_io(
 	loff_t			offset = iocb->ki_pos;
 	unsigned int		nofs_flag;
 
-	ASSERT(!xfs_is_zoned_inode(ip) ||
-	       !(flags & (IOMAP_DIO_UNWRITTEN | IOMAP_DIO_COW)));
+	ASSERT(!xfs_is_zoned_inode(ip));
 
 	trace_xfs_end_io_direct_write(ip, offset, size);
 
@@ -701,7 +730,7 @@ xfs_dio_zoned_submit_io(
 static const struct iomap_dio_ops xfs_dio_zoned_write_ops = {
 	.bio_set	= &iomap_ioend_bioset,
 	.submit_io	= xfs_dio_zoned_submit_io,
-	.end_io		= xfs_dio_write_end_io,
+	.end_io		= xfs_zoned_dio_write_end_io,
 };
 
 /*
Signature · 2 lines
Damien Le Moal
Western Digital Research
author maintainer
#7 lore raw

PATCH 3/4 xfs: factor out xfs_zone_inc_written

In reply to [email protected]

From: Carlos Maiolino <[email protected]>

Move the written blocks increment and full zone check into a new helper. Also add an assert to ensure rmap lock is held here.

---
 fs/xfs/xfs_zone_alloc.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
fs/xfs/xfs_zone_alloc.c +14 -6
index e3d19b6dc64a..97149bfc2512 100644
@@ -189,6 +189,18 @@ xfs_open_zone_mark_full(
 		xfs_zone_account_reclaimable(rtg, rtg_blocks(rtg) - used);
 }
 
+static inline void
+xfs_zone_inc_written(
+	struct xfs_open_zone	*oz,
+	xfs_filblks_t		len)
+{
+	xfs_assert_ilocked(rtg_rmap(oz->oz_rtg), XFS_ILOCK_EXCL);
+
+	oz->oz_written += len;
+	if (oz->oz_written == rtg_blocks(oz->oz_rtg))
+		xfs_open_zone_mark_full(oz);
+}
+
 static void
 xfs_zone_record_blocks(
 	struct xfs_trans	*tp,
@@ -206,9 +218,7 @@ xfs_zone_record_blocks(
 	xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP);
 	rmapip->i_used_blocks += len;
 	ASSERT(rmapip->i_used_blocks <= rtg_blocks(rtg));
-	oz->oz_written += len;
-	if (oz->oz_written == rtg_blocks(rtg))
-		xfs_open_zone_mark_full(oz);
+	xfs_zone_inc_written(oz, len);
 	xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
 }
 
@@ -227,9 +237,7 @@ xfs_zone_skip_blocks(
 	trace_xfs_zone_skip_blocks(oz, 0, len);
 
 	xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
-	oz->oz_written += len;
-	if (oz->oz_written == rtg_blocks(rtg))
-		xfs_open_zone_mark_full(oz);
+	xfs_zone_inc_written(oz, len);
 	xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP);
 
 	xfs_add_frextents(rtg_mount(rtg), len);
Signature · 1 line
2.53.0
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] xfs: factor out xfs_zone_inc_written

In reply to [email protected]
Quoted 6 lines · On 2026/03/10 12:55, [email protected] wrote:

From: Carlos Maiolino <[email protected]>

Move the written blocks increment and full zone check into a new helper. Also add an assert to ensure rmap lock is held here.

Signature · 2 lines
Damien Le Moal
Western Digital Research
author maintainer
#10 lore raw

PATCH 4/4 xfs: opencode xfs_zone_record_blocks

In reply to [email protected]

From: Carlos Maiolino <[email protected]>

We only have a single caller, no need to keep it in its own function.

---
 fs/xfs/xfs_zone_alloc.c | 30 ++++++++----------------------
 1 file changed, 8 insertions(+), 22 deletions(-)
fs/xfs/xfs_zone_alloc.c +8 -22
index 97149bfc2512..7d712d5a5ce0 100644
@@ -201,27 +201,6 @@ xfs_zone_inc_written(
 		xfs_open_zone_mark_full(oz);
 }
 
-static void
-xfs_zone_record_blocks(
-	struct xfs_trans	*tp,
-	struct xfs_open_zone	*oz,
-	xfs_fsblock_t		fsbno,
-	xfs_filblks_t		len)
-{
-	struct xfs_mount	*mp = tp->t_mountp;
-	struct xfs_rtgroup	*rtg = oz->oz_rtg;
-	struct xfs_inode	*rmapip = rtg_rmap(rtg);
-
-	trace_xfs_zone_record_blocks(oz, xfs_rtb_to_rgbno(mp, fsbno), len);
-
-	xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
-	xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP);
-	rmapip->i_used_blocks += len;
-	ASSERT(rmapip->i_used_blocks <= rtg_blocks(rtg));
-	xfs_zone_inc_written(oz, len);
-	xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
-}
-
 /*
  * Called for blocks that have been written to disk, but not actually linked to
  * an inode, which can happen when garbage collection races with user data
@@ -252,6 +231,8 @@ xfs_zoned_map_extent(
 	xfs_fsblock_t		old_startblock)
 {
 	struct xfs_bmbt_irec	data;
+	struct xfs_rtgroup	*rtg = oz->oz_rtg;
+	struct xfs_inode	*rmapip = rtg_rmap(rtg);
 	int			nmaps = 1;
 	int			error;
 
@@ -310,7 +291,12 @@ xfs_zoned_map_extent(
 		}
 	}
 
-	xfs_zone_record_blocks(tp, oz, new->br_startblock, new->br_blockcount);
+	xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
+	xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP);
+	rmapip->i_used_blocks += new->br_blockcount;
+	ASSERT(rmapip->i_used_blocks <= rtg_blocks(rtg));
+	xfs_zone_inc_written(oz, new->br_blockcount);
+	xfs_trans_log_inode(tp, rmapip, XFS_ILOG_CORE);
 
 	/* Map the new blocks into the data fork. */
 	xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, new);
Signature · 1 line
2.53.0