Zoned device cleanups
[email protected]
maintainerv2 cover Zoned device cleanups
From: Carlos Maiolino <[email protected]>
This is the V2 of this series, which implement changes suggested by Damien and Christoph on the V1.
Only changes are on patches 1 and 2.
Original cover follows:
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: factor out xfs_dio_write_zoned_end_io
xfs: factor out xfs_zone_inc_written
xfs: opencode xfs_zone_record_blocks
fs/xfs/xfs_file.c | 95 ++++++++++++++++++++++++++++++----------- fs/xfs/xfs_zone_alloc.c | 32 ++++++-------- 2 files changed, 82 insertions(+), 45 deletions(-)
Signature · 1 line
2.53.0
[email protected]
maintainerPATCH v2 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.
---
v2: Don't call xfs_setfilesize() unnecessarily
fs/xfs/xfs_file.c | 60 +++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 23 deletions(-)
fs/xfs/xfs_file.c +37 -23
@@ -560,6 +560,42 @@ 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)) { +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); +} + static int xfs_dio_write_end_io( struct kiocb *iocb,
@@ -623,30 +659,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
[email protected]
maintainerPATCH v2 2/4 xfs: factor out xfs_dio_write_zoned_end_io
In reply to [email protected]From: Carlos Maiolino <[email protected]>
Stop sharing direct IO end_io between regular and zoned devices by factoring out zoned dio end_io to its own function.
---
v2: Rewrite commit description - no code change
fs/xfs/xfs_file.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)
fs/xfs/xfs_file.c +32 -3
@@ -596,6 +596,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,
@@ -608,8 +638,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);
@@ -702,7 +731,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
[email protected]
maintainerPATCH v2 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.
---
v2: add RwB tags, no changes
fs/xfs/xfs_zone_alloc.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
fs/xfs/xfs_zone_alloc.c +14 -6
@@ -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
[email protected]
maintainerPATCH v2 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.
---
v2: add RwB tags, no changes
fs/xfs/xfs_zone_alloc.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-)
fs/xfs/xfs_zone_alloc.c +8 -22
@@ -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
Re: [PATCH v2 1/4] xfs: factor out isize updates from xfs_dio_write_end_io
In reply to [email protected]Quoted 6 lines · On 2026/03/10 18:36, [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.
Signed-off-by Carlos Maiolino <[email protected]>
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v2 2/4] xfs: factor out xfs_dio_write_zoned_end_io
In reply to [email protected]Quoted 7 lines · On 2026/03/10 18:36, [email protected] wrote:
From: Carlos Maiolino <[email protected]>
Stop sharing direct IO
end_iobetween regular and zoned devices by factoring out zoned dioend_ioto its own function.Signed-off-by Carlos Maiolino <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v2 1/4] xfs: factor out isize updates from xfs_dio_write_end_io
In reply to [email protected]Looks good:
Quoted 8 lines · On Tue, 10 Mar 2026 18:36:45 +0100, [email protected] wrote:
From: Carlos Maiolino <[email protected]>
This is the V2 of this series, which implement changes suggested by Damien and Christoph on the V1.
Only changes are on patches 1 and 2.
[...]
Applied to for-next, thanks!
[1/4] xfs: factor out isize updates from xfs_dio_write_end_io
commit: db8367f63b301bbdff6eb00c2e09fad4f2ae75e9
[2/4] xfs: factor out xfs_dio_write_zoned_end_io
commit: 02a5d8993b09fe9a6754e57d0e25399baffe9a06
[3/4] xfs: factor out xfs_zone_inc_written
commit: 3bdc20b005c20ce1bf9b098d1ee2caa1d994141e
[4/4] xfs: opencode xfs_zone_record_blocks
commit: 54514abb9891eb35033608e0fb51809f5a4c07ffBest regards,
Signature · 1 line
Carlos Maiolino <[email protected]>
Re: [PATCH v2 4/4] xfs: opencode xfs_zone_record_blocks
In reply to [email protected]Quoted 3 lines · On Tue, Mar 10, 2026 at 06:36:49PM +0100, [email protected] wrote:
From: Carlos Maiolino <[email protected]>
We only have a single caller, no need to keep it in its own function.
This accidentally dropped the record_blocks trace event. The patch below adds it back. Feel free to fold it in if that works for you.
---
From 6e413887a36cd91af3684d8af357b50538106bea Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Wed, 18 Mar 2026 08:58:38 +0100
Subject: xfs: add back the xfs_zone_record_blocks trace event
Commit 54514abb9891 ("xfs: opencode xfs_zone_record_blocks") accidentally removed it.
--- fs/xfs/xfs_zone_alloc.c | 3 +++ 1 file changed, 3 insertions(+)
fs/xfs/xfs_zone_alloc.c +3 -0
@@ -291,6 +291,9 @@ xfs_zoned_map_extent( } } +trace_xfs_zone_record_blocks (oz, +xfs_rtb_to_rgbno (tp->t_mountp, 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;
Signature · 1 line
2.47.3
Carlos Maiolino
maintainerRe: [PATCH v2 4/4] xfs: opencode xfs_zone_record_blocks
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · On Tue, Mar 10, 2026 at 06:36:49PM +0100, [email protected] wrote:
From: Carlos Maiolino <[email protected]>
We only have a single caller, no need to keep it in its own function.
This accidentally dropped the
record_blockstrace event. The patch below adds it back. Feel free to fold it in if that works for you.
Woops, thanks. I'm folding it into the original patch adding a small note there.
Thanks!
Quoted 33 lines
---
From6e413887a36cd91af3684d8af357b50538106beaMon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Wed, 18 Mar 2026 08:58:38 +0100
Subject: xfs: add back thexfs_zone_record_blockstrace eventCommit
54514abb9891("xfs: opencodexfs_zone_record_blocks") accidentally removed it.Signed-off-by Christoph Hellwig <[email protected]>--- fs/xfs/xfs_zone_alloc.c | 3 +++ 1 file changed, 3 insertions(+)fs/xfs/xfs_zone_alloc.c +3
-0@@ -291,6 +291,9 @@ xfs_zoned_map_extent( } } +trace_xfs_zone_record_blocks (oz, +xfs_rtb_to_rgbno (tp->t_mountp, 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;Signature · 1 line
2.47.3