Commit 9c457aea authored by Dave Chinner's avatar Dave Chinner Committed by Ben Hutchings

xfs: fix swapext ilock deadlock

commit 81217683 upstream.

xfs_swap_extents() holds the ilock over a call to
filemap_write_and_wait(), which can then try to write data and take
the ilock. That causes a self-deadlock.

Fix the deadlock and clean up the code by separating the locking
appropriately. Add a lockflags variable to track what locks we are
holding as we gain and drop them and cleanup the error handling to
always use "out_unlock" with the lockflags variable.
Signed-off-by: default avatarDave Chinner <dchinner@redhat.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarDave Chinner <david@fromorbit.com>
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent 11e2f8dc
...@@ -1633,6 +1633,7 @@ xfs_swap_extents( ...@@ -1633,6 +1633,7 @@ xfs_swap_extents(
int aforkblks = 0; int aforkblks = 0;
int taforkblks = 0; int taforkblks = 0;
__uint64_t tmp; __uint64_t tmp;
int lock_flags;
tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL); tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
if (!tempifp) { if (!tempifp) {
...@@ -1641,13 +1642,13 @@ xfs_swap_extents( ...@@ -1641,13 +1642,13 @@ xfs_swap_extents(
} }
/* /*
* we have to do two separate lock calls here to keep lockdep * Lock up the inodes against other IO and truncate to begin with.
* happy. If we try to get all the locks in one call, lock will * Then we can ensure the inodes are flushed and have no page cache
* report false positives when we drop the ILOCK and regain them * safely. Once we have done this we can take the ilocks and do the rest
* below. * of the checks.
*/ */
lock_flags = XFS_IOLOCK_EXCL;
xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL); xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
/* Verify that both files have the same format */ /* Verify that both files have the same format */
if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) { if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
...@@ -1666,6 +1667,9 @@ xfs_swap_extents( ...@@ -1666,6 +1667,9 @@ xfs_swap_extents(
goto out_unlock; goto out_unlock;
truncate_pagecache_range(VFS_I(tip), 0, -1); truncate_pagecache_range(VFS_I(tip), 0, -1);
xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
lock_flags |= XFS_ILOCK_EXCL;
/* Verify O_DIRECT for ftmp */ /* Verify O_DIRECT for ftmp */
if (VN_CACHED(VFS_I(tip)) != 0) { if (VN_CACHED(VFS_I(tip)) != 0) {
error = XFS_ERROR(EINVAL); error = XFS_ERROR(EINVAL);
...@@ -1720,6 +1724,7 @@ xfs_swap_extents( ...@@ -1720,6 +1724,7 @@ xfs_swap_extents(
xfs_iunlock(ip, XFS_ILOCK_EXCL); xfs_iunlock(ip, XFS_ILOCK_EXCL);
xfs_iunlock(tip, XFS_ILOCK_EXCL); xfs_iunlock(tip, XFS_ILOCK_EXCL);
lock_flags &= ~XFS_ILOCK_EXCL;
/* /*
* There is a race condition here since we gave up the * There is a race condition here since we gave up the
...@@ -1732,13 +1737,11 @@ xfs_swap_extents( ...@@ -1732,13 +1737,11 @@ xfs_swap_extents(
tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT); tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0); error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
if (error) { if (error)
xfs_iunlock(ip, XFS_IOLOCK_EXCL); goto out_trans_cancel;
xfs_iunlock(tip, XFS_IOLOCK_EXCL);
xfs_trans_cancel(tp, 0);
goto out;
}
xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL); xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
lock_flags |= XFS_ILOCK_EXCL;
/* /*
* Count the number of extended attribute blocks * Count the number of extended attribute blocks
...@@ -1757,8 +1760,8 @@ xfs_swap_extents( ...@@ -1757,8 +1760,8 @@ xfs_swap_extents(
goto out_trans_cancel; goto out_trans_cancel;
} }
xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); xfs_trans_ijoin(tp, ip, lock_flags);
xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); xfs_trans_ijoin(tp, tip, lock_flags);
/* /*
* Before we've swapped the forks, lets set the owners of the forks * Before we've swapped the forks, lets set the owners of the forks
...@@ -1887,8 +1890,8 @@ xfs_swap_extents( ...@@ -1887,8 +1890,8 @@ xfs_swap_extents(
return error; return error;
out_unlock: out_unlock:
xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); xfs_iunlock(ip, lock_flags);
xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); xfs_iunlock(tip, lock_flags);
goto out; goto out;
out_trans_cancel: out_trans_cancel:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment