From: Jianyun Gao xfs_bmapi_remap() was originally a data-fork-only helper, so its error0 cleanup block hardcoded ip->i_df.if_format and the D-series log flags (XFS_ILOG_DEXT / XFS_ILOG_DBROOT). commit 7cf199ba5a70d ("xfs: make xfs_bmapi_remapi work with attribute forks") extended the function to support XFS_BMAPI_ATTRFORK and converted almost the entire body to use whichfork, but the error0 cleanup was left untouched; a later mechanical conversion in commit f7e67b20ecbbc ("xfs: move the fork format fields into struct xfs_ifork") preserved the bug. When called for the attr fork, the cleanup is a complete no-op: it checks the data fork's format and clears D-series flags that were never set, while the A-series flags (XFS_ILOG_AEXT / XFS_ILOG_ABROOT) set by xfs_bmap_add_extent_hole_real() are never stripped. This is reachable on the success path too, since error0 is a fall-through target: if the attr fork starts in BTREE format and a three-way merge drops if_nextents below the threshold, xfs_bmap_btree_to_extents() frees if_broot but a stale XFS_ILOG_ABROOT remains in logflags. In practice this does not crash or corrupt data -- the inode item format and flush helpers dispatch on if_format and clear stale ABROOT before if_broot is dereferenced. But the code is logically inconsistent and relies on downstream defensive checks; fix it now to avoid future trouble if those checks are refactored. Make the cleanup fork-aware, mirroring xfs_bmapi_finish(): use xfs_ilog_fext/fbroot(whichfork) and check ifp->if_format. Fixes: 7cf199ba5a70d ("xfs: make xfs_bmapi_remapi work with attribute forks") Cc: stable@vger.kernel.org Signed-off-by: Jianyun Gao --- fs/xfs/libxfs/xfs_bmap.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c index dcd5faf3428e..9236aa80838e 100644 --- a/fs/xfs/libxfs/xfs_bmap.c +++ b/fs/xfs/libxfs/xfs_bmap.c @@ -4520,10 +4520,12 @@ xfs_bmapi_remap( error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); error0: - if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS) - logflags &= ~XFS_ILOG_DEXT; - else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE) - logflags &= ~XFS_ILOG_DBROOT; + if ((logflags & xfs_ilog_fext(whichfork)) && + ifp->if_format != XFS_DINODE_FMT_EXTENTS) + logflags &= ~xfs_ilog_fext(whichfork); + else if ((logflags & xfs_ilog_fbroot(whichfork)) && + ifp->if_format != XFS_DINODE_FMT_BTREE) + logflags &= ~xfs_ilog_fbroot(whichfork); if (logflags) xfs_trans_log_inode(tp, ip, logflags); -- 2.34.1