For write path set IOMAP_F_FSVERITY flag telling iomap to not update inode size and to not skip folios beyond EOF. For read path let iomap know that we are reading fsverity metadata. So, treat holes in the tree as request to synthesize tree blocks and hole after descriptor as end of the fsverity region. Signed-off-by: Andrey Albershteyn --- fs/xfs/Makefile | 1 + fs/xfs/libxfs/xfs_bmap.c | 4 +-- fs/xfs/libxfs/xfs_verity.c | 20 +++++++++++++ fs/xfs/libxfs/xfs_verity.h | 13 ++++++++ fs/xfs/xfs_aops.c | 6 ++-- fs/xfs/xfs_iomap.c | 61 ++++++++++++++++++++++++++++---------- fs/xfs/xfs_iomap.h | 5 ++-- fs/xfs/xfs_pnfs.c | 2 +- 8 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 fs/xfs/libxfs/xfs_verity.c create mode 100644 fs/xfs/libxfs/xfs_verity.h diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile index 399a207f2d0e..cb7f71457ee9 100644 --- a/fs/xfs/Makefile +++ b/fs/xfs/Makefile @@ -59,6 +59,7 @@ xfs-y += $(addprefix libxfs/, \ xfs_trans_resv.o \ xfs_trans_space.o \ xfs_types.o \ + xfs_verity.o \ ) # xfs_rtbitmap is shared with libxfs xfs-$(CONFIG_XFS_RT) += $(addprefix libxfs/, \ diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c index 050522c58b16..f74411312901 100644 --- a/fs/xfs/libxfs/xfs_bmap.c +++ b/fs/xfs/libxfs/xfs_bmap.c @@ -4414,7 +4414,7 @@ xfs_bmapi_convert_one_delalloc( * the extent. Just return the real extent at this offset. */ if (!isnullstartblock(bma.got.br_startblock)) { - xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags, + xfs_bmbt_to_iomap(ip, iomap, &bma.got, offset, 0, flags, xfs_iomap_inode_sequence(ip, flags)); if (seq) *seq = READ_ONCE(ifp->if_seq); @@ -4462,7 +4462,7 @@ xfs_bmapi_convert_one_delalloc( XFS_STATS_INC(mp, xs_xstrat_quick); ASSERT(!isnullstartblock(bma.got.br_startblock)); - xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags, + xfs_bmbt_to_iomap(ip, iomap, &bma.got, offset, 0, flags, xfs_iomap_inode_sequence(ip, flags)); if (seq) *seq = READ_ONCE(ifp->if_seq); diff --git a/fs/xfs/libxfs/xfs_verity.c b/fs/xfs/libxfs/xfs_verity.c new file mode 100644 index 000000000000..c1731b89a5a1 --- /dev/null +++ b/fs/xfs/libxfs/xfs_verity.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2026 Red Hat, Inc. + */ +#include "xfs_platform.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "xfs_log_format.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_inode.h" +#include "xfs_verity.h" + +loff_t +xfs_fsverity_metadata_offset( + const struct xfs_inode *ip) +{ + return round_up(i_size_read(VFS_IC(ip)), XFS_FSVERITY_START_ALIGN); +} diff --git a/fs/xfs/libxfs/xfs_verity.h b/fs/xfs/libxfs/xfs_verity.h new file mode 100644 index 000000000000..da90f798faa8 --- /dev/null +++ b/fs/xfs/libxfs/xfs_verity.h @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2026 Red Hat, Inc. + */ +#ifndef __XFS_VERITY_H__ +#define __XFS_VERITY_H__ + +#include "xfs_platform.h" +#include + +loff_t xfs_fsverity_metadata_offset(const struct xfs_inode *ip); + +#endif /* __XFS_VERITY_H__ */ diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c index 1db6f3640c53..b9728997f944 100644 --- a/fs/xfs/xfs_aops.c +++ b/fs/xfs/xfs_aops.c @@ -23,6 +23,7 @@ #include "xfs_ioend.h" #include "xfs_zone_alloc.h" #include "xfs_rtgroup.h" +#include "xfs_verity.h" struct xfs_writepage_ctx { struct iomap_writepage_ctx ctx; @@ -265,7 +266,8 @@ xfs_map_blocks( isnullstartblock(imap.br_startblock)) goto allocate_blocks; - xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0, 0, XFS_WPC(wpc)->data_seq); + xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, offset, 0, 0, + XFS_WPC(wpc)->data_seq); trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap); return 0; allocate_blocks: @@ -441,7 +443,7 @@ xfs_zoned_map_blocks( imap.br_startblock = HOLESTARTBLOCK; imap.br_state = XFS_EXT_NORM; xfs_iunlock(ip, XFS_ILOCK_EXCL); - xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0, 0, 0); + xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, offset, 0, 0, 0); return 0; } end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount); diff --git a/fs/xfs/xfs_iomap.c b/fs/xfs/xfs_iomap.c index 50eae7ff6145..5117704530f6 100644 --- a/fs/xfs/xfs_iomap.c +++ b/fs/xfs/xfs_iomap.c @@ -32,6 +32,8 @@ #include "xfs_rtbitmap.h" #include "xfs_icache.h" #include "xfs_zone_alloc.h" +#include "xfs_verity.h" +#include #define XFS_ALLOC_ALIGN(mp, off) \ (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log) @@ -54,6 +56,15 @@ xfs_alert_fsblock_zero( return -EFSCORRUPTED; } +static bool +xfs_iomap_is_fsverity_metadata( + struct xfs_inode *ip, + loff_t offset) +{ + return offset >= xfs_fsverity_metadata_offset(ip) && + (fsverity_is_building(VFS_I(ip)) || fsverity_active(VFS_I(ip))); +} + u64 xfs_iomap_inode_sequence( struct xfs_inode *ip, @@ -101,6 +112,7 @@ xfs_bmbt_to_iomap( struct xfs_inode *ip, struct iomap *iomap, struct xfs_bmbt_irec *imap, + loff_t offset, unsigned int mapping_flags, u16 iomap_flags, u64 sequence_cookie) @@ -152,6 +164,15 @@ xfs_bmbt_to_iomap( iomap->flags |= IOMAP_F_INTEGRITY; } + /* + * Tell iomap when this I/O covers fs-verity metadata so it handles size + * updates and post-EOF folios correctly. The attr fork uses a separate + * offset space, so exclude it. + */ + if (!(iomap_flags & IOMAP_F_XATTR) && + xfs_iomap_is_fsverity_metadata(ip, offset)) + iomap->flags |= IOMAP_F_FSVERITY; + /* * If the inode is dirty for datasync purposes, let iomap know so it * doesn't elide the IO completion journal flushes on O_DSYNC IO. @@ -1006,7 +1027,8 @@ xfs_direct_write_iomap_begin( seq = xfs_iomap_inode_sequence(ip, iomap_flags); xfs_iunlock(ip, lockmode); trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap); - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq); + return xfs_bmbt_to_iomap(ip, iomap, &imap, offset, flags, iomap_flags, + seq); allocate_blocks: error = -EAGAIN; @@ -1037,7 +1059,7 @@ xfs_direct_write_iomap_begin( return error; trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap); - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, + return xfs_bmbt_to_iomap(ip, iomap, &imap, offset, flags, iomap_flags | IOMAP_F_NEW, seq); out_found_cow: @@ -1045,13 +1067,15 @@ xfs_direct_write_iomap_begin( trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap); if (imap.br_startblock != HOLESTARTBLOCK) { seq = xfs_iomap_inode_sequence(ip, 0); - error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, seq); + error = xfs_bmbt_to_iomap(ip, srcmap, &imap, offset, flags, + 0, seq); if (error) goto out_unlock; } seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); xfs_iunlock(ip, lockmode); - return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq); + return xfs_bmbt_to_iomap(ip, iomap, &cmap, offset, flags, + IOMAP_F_SHARED, seq); out_unlock: if (lockmode) @@ -1270,7 +1294,8 @@ xfs_atomic_write_cow_iomap_begin( trace_xfs_iomap_found(ip, offset, length, XFS_COW_FORK, &cmap); seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); xfs_iunlock(ip, XFS_ILOCK_EXCL); - return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED, seq); + return xfs_bmbt_to_iomap(ip, iomap, &cmap, offset, flags, + IOMAP_F_SHARED, seq); convert_delay: xfs_iunlock(ip, XFS_ILOCK_EXCL); @@ -1622,8 +1647,9 @@ xfs_zoned_fill_srcmap( */ xfs_trim_extent(&smap, offset_fsb, *end_fsb - offset_fsb); *end_fsb = min(*end_fsb, smap.br_startoff + smap.br_blockcount); - return xfs_bmbt_to_iomap(ip, srcmap, &smap, flags, 0, - xfs_iomap_inode_sequence(ip, 0)); + return xfs_bmbt_to_iomap(ip, srcmap, &smap, + XFS_FSB_TO_B(ip->i_mount, offset_fsb), flags, + 0, xfs_iomap_inode_sequence(ip, 0)); } static int @@ -1796,7 +1822,7 @@ xfs_zoned_buffered_write_iomap_begin( trace_xfs_iomap_alloc(ip, offset, XFS_FSB_TO_B(mp, count_fsb), XFS_COW_FORK, &got); done: - error = xfs_bmbt_to_iomap(ip, iomap, &got, flags, iomap_flags, + error = xfs_bmbt_to_iomap(ip, iomap, &got, offset, flags, iomap_flags, xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED)); out_unlock: xfs_iunlock(ip, lockmode); @@ -2104,7 +2130,8 @@ xfs_buffered_write_iomap_begin( found_imap: seq = xfs_iomap_inode_sequence(ip, iomap_flags); xfs_iunlock(ip, lockmode); - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags, seq); + return xfs_bmbt_to_iomap(ip, iomap, &imap, offset, flags, iomap_flags, + seq); convert_delay: xfs_iunlock(ip, lockmode); @@ -2119,8 +2146,8 @@ xfs_buffered_write_iomap_begin( found_cow: if (imap.br_startoff <= offset_fsb) { - error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0, - xfs_iomap_inode_sequence(ip, 0)); + error = xfs_bmbt_to_iomap(ip, srcmap, &imap, offset, flags, + 0, xfs_iomap_inode_sequence(ip, 0)); if (error) goto out_unlock; } else { @@ -2131,7 +2158,8 @@ xfs_buffered_write_iomap_begin( iomap_flags |= IOMAP_F_SHARED; seq = xfs_iomap_inode_sequence(ip, iomap_flags); xfs_iunlock(ip, lockmode); - return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, iomap_flags, seq); + return xfs_bmbt_to_iomap(ip, iomap, &cmap, offset, flags, iomap_flags, + seq); out_unlock: xfs_iunlock(ip, lockmode); @@ -2238,7 +2266,7 @@ xfs_read_iomap_begin( if (error) return error; trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap); - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, + return xfs_bmbt_to_iomap(ip, iomap, &imap, offset, flags, shared ? IOMAP_F_SHARED : 0, seq); } @@ -2298,7 +2326,7 @@ xfs_seek_iomap_begin( end_fsb = min(end_fsb, data_fsb); xfs_trim_extent(&cmap, offset_fsb, end_fsb - offset_fsb); seq = xfs_iomap_inode_sequence(ip, IOMAP_F_SHARED); - error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, + error = xfs_bmbt_to_iomap(ip, iomap, &cmap, offset, flags, IOMAP_F_SHARED, seq); /* * This is a COW extent, so we must probe the page cache @@ -2322,7 +2350,7 @@ xfs_seek_iomap_begin( done: seq = xfs_iomap_inode_sequence(ip, 0); xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb); - error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0, seq); + error = xfs_bmbt_to_iomap(ip, iomap, &imap, offset, flags, 0, seq); out_unlock: xfs_iunlock(ip, lockmode); return error; @@ -2370,7 +2398,8 @@ xfs_xattr_iomap_begin( if (error) return error; ASSERT(nimaps); - return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_XATTR, seq); + return xfs_bmbt_to_iomap(ip, iomap, &imap, offset, flags, + IOMAP_F_XATTR, seq); } DEFINE_IOMAP_ITER_NEXT(xfs_xattr_iomap_next, xfs_xattr_iomap_begin); diff --git a/fs/xfs/xfs_iomap.h b/fs/xfs/xfs_iomap.h index 071fff666507..c9bd6de1bfda 100644 --- a/fs/xfs/xfs_iomap.h +++ b/fs/xfs/xfs_iomap.h @@ -22,8 +22,9 @@ xfs_fileoff_t xfs_iomap_eof_align_last_fsb(struct xfs_inode *ip, u64 xfs_iomap_inode_sequence(struct xfs_inode *ip, u16 iomap_flags); int xfs_bmbt_to_iomap(struct xfs_inode *ip, struct iomap *iomap, - struct xfs_bmbt_irec *imap, unsigned int mapping_flags, - u16 iomap_flags, u64 sequence_cookie); + struct xfs_bmbt_irec *imap, loff_t offset, + unsigned int mapping_flags, u16 iomap_flags, + u64 sequence_cookie); int xfs_zero_range(struct xfs_inode *ip, loff_t pos, loff_t len, struct xfs_zone_alloc_ctx *ac, bool *did_zero); diff --git a/fs/xfs/xfs_pnfs.c b/fs/xfs/xfs_pnfs.c index 945f7a3e11ca..f364835e3b58 100644 --- a/fs/xfs/xfs_pnfs.c +++ b/fs/xfs/xfs_pnfs.c @@ -258,7 +258,7 @@ xfs_fs_map_blocks( } xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL); - error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0, 0, seq); + error = xfs_bmbt_to_iomap(ip, iomap, &imap, offset, 0, 0, seq); *device_generation = mp->m_generation; return error; out_unlock: -- 2.54.0