]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: Fix deadlock between AGI and AGF when target_ip exists in xfs_rename()
authorkaixuxia <xiakaixu1987@gmail.com>
Wed, 22 Jan 2020 16:29:44 +0000 (11:29 -0500)
committerEric Sandeen <sandeen@redhat.com>
Wed, 22 Jan 2020 16:29:44 +0000 (11:29 -0500)
Source kernel commit: 93597ae8dac0149b5c00b787cba6bf7ba213e666

When target_ip exists in xfs_rename(), the xfs_dir_replace() call may
need to hold the AGF lock to allocate more blocks, and then invoking
the xfs_droplink() call to hold AGI lock to drop target_ip onto the
unlinked list, so we get the lock order AGF->AGI. This would break the
ordering constraint on AGI and AGF locking - inode allocation locks
the AGI, then can allocate a new extent for new inodes, locking the
AGF after the AGI.

In this patch we check whether the replace operation need more
blocks firstly. If so, acquire the agi lock firstly to preserve
locking order(AGI/AGF). Actually, the locking order problem only
occurs when we are locking the AGI/AGF of the same AG. For multiple
AGs the AGI lock will be released after the transaction committed.

Signed-off-by: kaixuxia <kaixuxia@tencent.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
[darrick: reword the comment]
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
libxfs/xfs_dir2.h
libxfs/xfs_dir2_sf.c

index 34e7a0b64205b4e296d01415374be00fbf3f6438..033777e282f289e529291586172e80183d5ff672 100644 (file)
@@ -47,6 +47,8 @@ extern int xfs_dir_lookup(struct xfs_trans *tp, struct xfs_inode *dp,
 extern int xfs_dir_removename(struct xfs_trans *tp, struct xfs_inode *dp,
                                struct xfs_name *name, xfs_ino_t ino,
                                xfs_extlen_t tot);
+extern bool xfs_dir2_sf_replace_needblock(struct xfs_inode *dp,
+                               xfs_ino_t inum);
 extern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp,
                                struct xfs_name *name, xfs_ino_t inum,
                                xfs_extlen_t tot);
index d38b5d4982f6f34e9427b36b221813ec88b4e917..0053b7c31f7c5bf041bee8260c2222925845b06d 100644 (file)
@@ -1016,6 +1016,27 @@ xfs_dir2_sf_removename(
        return 0;
 }
 
+/*
+ * Check whether the sf dir replace operation need more blocks.
+ */
+bool
+xfs_dir2_sf_replace_needblock(
+       struct xfs_inode        *dp,
+       xfs_ino_t               inum)
+{
+       int                     newsize;
+       struct xfs_dir2_sf_hdr  *sfp;
+
+       if (dp->i_d.di_format != XFS_DINODE_FMT_LOCAL)
+               return false;
+
+       sfp = (struct xfs_dir2_sf_hdr *)dp->i_df.if_u1.if_data;
+       newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
+
+       return inum > XFS_DIR2_MAX_SHORT_INUM &&
+              sfp->i8count == 0 && newsize > XFS_IFORK_DSIZE(dp);
+}
+
 /*
  * Replace the inode number of an entry in a shortform directory.
  */
@@ -1045,17 +1066,14 @@ xfs_dir2_sf_replace(
         */
        if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
                int     error;                  /* error return value */
-               int     newsize;                /* new inode size */
 
-               newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
                /*
                 * Won't fit as shortform, convert to block then do replace.
                 */
-               if (newsize > XFS_IFORK_DSIZE(dp)) {
+               if (xfs_dir2_sf_replace_needblock(dp, args->inumber)) {
                        error = xfs_dir2_sf_to_block(args);
-                       if (error) {
+                       if (error)
                                return error;
-                       }
                        return xfs_dir2_block_replace(args);
                }
                /*