Skip to content

Commit 4ea748e

Browse files
fdmananakdave
authored andcommitted
Btrfs: fix deadlock between clone/dedupe and rename
Reflinking (clone/dedupe) and rename are operations that operate on two inodes and therefore need to lock them in the same order to avoid ABBA deadlocks. It happens that Btrfs' reflink implementation always locked them in a different order from VFS's lock_two_nondirectories() helper, which is used by the rename code in VFS, resulting in ABBA type deadlocks. Btrfs' locking order: static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) { if (inode1 < inode2) swap(inode1, inode2); inode_lock_nested(inode1, I_MUTEX_PARENT); inode_lock_nested(inode2, I_MUTEX_CHILD); } VFS's locking order: void lock_two_nondirectories(struct inode *inode1, struct inode *inode2) { if (inode1 > inode2) swap(inode1, inode2); if (inode1 && !S_ISDIR(inode1->i_mode)) inode_lock(inode1); if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1) inode_lock_nested(inode2, I_MUTEX_NONDIR2); } Fix this by killing the btrfs helper function that does the double inode locking and replace it with VFS's helper lock_two_nondirectories(). Reported-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org> Fixes: 416161d ("btrfs: offline dedupe") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 8e92821 commit 4ea748e

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

fs/btrfs/ioctl.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,21 +3207,6 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
32073207
return ret;
32083208
}
32093209

3210-
static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
3211-
{
3212-
inode_unlock(inode1);
3213-
inode_unlock(inode2);
3214-
}
3215-
3216-
static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
3217-
{
3218-
if (inode1 < inode2)
3219-
swap(inode1, inode2);
3220-
3221-
inode_lock_nested(inode1, I_MUTEX_PARENT);
3222-
inode_lock_nested(inode2, I_MUTEX_CHILD);
3223-
}
3224-
32253210
static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
32263211
struct inode *inode2, u64 loff2, u64 len)
32273212
{
@@ -3956,7 +3941,7 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
39563941
if (same_inode)
39573942
inode_lock(inode_in);
39583943
else
3959-
btrfs_double_inode_lock(inode_in, inode_out);
3944+
lock_two_nondirectories(inode_in, inode_out);
39603945

39613946
/* don't make the dst file partly checksummed */
39623947
if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
@@ -4013,7 +3998,7 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
40133998
if (same_inode)
40143999
inode_unlock(inode_in);
40154000
else
4016-
btrfs_double_inode_unlock(inode_in, inode_out);
4001+
unlock_two_nondirectories(inode_in, inode_out);
40174002

40184003
return ret;
40194004
}
@@ -4043,7 +4028,7 @@ loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
40434028
if (same_inode)
40444029
inode_unlock(src_inode);
40454030
else
4046-
btrfs_double_inode_unlock(src_inode, dst_inode);
4031+
unlock_two_nondirectories(src_inode, dst_inode);
40474032

40484033
return ret < 0 ? ret : len;
40494034
}

0 commit comments

Comments
 (0)