The __xfs_bunmapi() is used to remove all extents past EOF. Add XFS_BMAPI_UNWRITTEN to remove only unwritten extents, while leaving written ones in place. This will be used in following patch to clean up unwritten extents on fsverity inodes. Signed-off-by: Andrey Albershteyn Reviewed-by: "Darrick J. Wong" Reviewed-by: Christoph Hellwig --- fs/xfs/libxfs/xfs_bmap.c | 22 +++++++++++++++++++++- fs/xfs/libxfs/xfs_bmap.h | 6 +++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c index f74411312901..667b693aacb8 100644 --- a/fs/xfs/libxfs/xfs_bmap.c +++ b/fs/xfs/libxfs/xfs_bmap.c @@ -5220,6 +5220,22 @@ __xfs_bunmapi( ASSERT(len > 0); ASSERT(nexts >= 0); + /* + * XFS_BMAPI_UNWRITTEN is only used by fs-verity to drop unwritten + * blocks past EOF while keeping written ones (fsverity metadata). + * + * The XFS_BMAPI_UNWRITTEN could not be used with realtime inodes with + * realtime extent larger than 1 filesystem block. This is because the + * realtime inode with larger extents could have EOF in the middle of + * the extent. Removing these unwritten extent beyond EOF could lead to + * removal of data before EOF. + * + * fs-verity is not allowed to be enabled on realtime files whose + * extent size is larger than 1. + */ + ASSERT(!((flags & XFS_BMAPI_UNWRITTEN) && + xfs_inode_has_bigrtalloc(ip))); + error = xfs_iread_extents(tp, ip, whichfork); if (error) return error; @@ -5281,6 +5297,10 @@ __xfs_bunmapi( if (del.br_startoff + del.br_blockcount > end + 1) del.br_blockcount = end + 1 - del.br_startoff; + if ((flags & XFS_BMAPI_UNWRITTEN) && + del.br_state != XFS_EXT_UNWRITTEN) + goto skip; + if (!isrt || (flags & XFS_BMAPI_REMAP)) goto delete; @@ -5415,7 +5435,7 @@ __xfs_bunmapi( if (error) goto error0; } - +skip: end = del.br_startoff - 1; nodelete: /* diff --git a/fs/xfs/libxfs/xfs_bmap.h b/fs/xfs/libxfs/xfs_bmap.h index fb7481e8190c..f1df6b4b695e 100644 --- a/fs/xfs/libxfs/xfs_bmap.h +++ b/fs/xfs/libxfs/xfs_bmap.h @@ -97,6 +97,9 @@ struct xfs_bmalloca { */ #define XFS_BMAPI_CONVERT_ONLY (1u << 12) +/* Process unwritten extents only. Used for unmapping */ +#define XFS_BMAPI_UNWRITTEN (1u << 13) + #define XFS_BMAPI_FLAGS \ { XFS_BMAPI_ENTIRE, "ENTIRE" }, \ { XFS_BMAPI_METADATA, "METADATA" }, \ @@ -110,7 +113,8 @@ struct xfs_bmalloca { { XFS_BMAPI_NODISCARD, "NODISCARD" }, \ { XFS_BMAPI_NORMAP, "NORMAP" },\ { XFS_BMAPI_EXTSZALIGN, "EXTSZALIGN" }, \ - { XFS_BMAPI_CONVERT_ONLY, "CONVERT_ONLY" } + { XFS_BMAPI_CONVERT_ONLY, "CONVERT_ONLY" }, \ + { XFS_BMAPI_UNWRITTEN, "UNWRITTEN" } static inline int xfs_bmapi_aflag(int w) -- 2.54.0