Commit 7cafecfa authored by Dave Chinner's avatar Dave Chinner Committed by Ben Hutchings

xfs: lock out page faults from extent swap operations

commit 723cac48 upstream.

Extent swap operations are another extent manipulation operation
that we need to ensure does not race against mmap page faults. The
current code returns if the file is mapped prior to the swap being
done, but it could potentially race against new page faults while
the swap is in progress. Hence we should use the XFS_MMAPLOCK_EXCL
for this operation, too.

While there, fix the error path handling that can result in double
unlocks of the inodes when cancelling the swapext transaction.
Signed-off-by: default avatarDave Chinner <dchinner@redhat.com>
Reviewed-by: default avatarBrian Foster <bfoster@redhat.com>
Signed-off-by: default avatarDave Chinner <david@fromorbit.com>
[bwh: Backported to 3.16:
 - The obsoleted check for mmap'd files was directly in xfs_swap_extents()
   and used VN_MAPPED
 - Adjust context]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent 9949c4ca
...@@ -1642,13 +1642,14 @@ xfs_swap_extents( ...@@ -1642,13 +1642,14 @@ xfs_swap_extents(
} }
/* /*
* Lock up the inodes against other IO and truncate to begin with. * Lock the inodes against other IO, page faults and truncate to
* Then we can ensure the inodes are flushed and have no page cache * begin with. Then we can ensure the inodes are flushed and have no
* safely. Once we have done this we can take the ilocks and do the rest * page cache safely. Once we have done this we can take the ilocks and
* of the checks. * do the rest of the checks.
*/ */
lock_flags = XFS_IOLOCK_EXCL; lock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_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_MMAPLOCK_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)) {
...@@ -1711,17 +1712,6 @@ xfs_swap_extents( ...@@ -1711,17 +1712,6 @@ xfs_swap_extents(
goto out_unlock; goto out_unlock;
} }
/* We need to fail if the file is memory mapped. Once we have tossed
* all existing pages, the page fault will have no option
* but to go to the filesystem for pages. By making the page fault call
* vop_read (or write in the case of autogrow) they block on the iolock
* until we have switched the extents.
*/
if (VN_MAPPED(VFS_I(ip))) {
error = XFS_ERROR(EBUSY);
goto out_unlock;
}
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; lock_flags &= ~XFS_ILOCK_EXCL;
...@@ -1740,8 +1730,15 @@ xfs_swap_extents( ...@@ -1740,8 +1730,15 @@ xfs_swap_extents(
if (error) if (error)
goto out_trans_cancel; goto out_trans_cancel;
/*
* Lock and join the inodes to the tansaction so that transaction commit
* or cancel will unlock the inodes from this point onwards.
*/
xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL); xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
lock_flags |= XFS_ILOCK_EXCL; lock_flags |= XFS_ILOCK_EXCL;
xfs_trans_ijoin(tp, ip, lock_flags);
xfs_trans_ijoin(tp, tip, lock_flags);
/* /*
* Count the number of extended attribute blocks * Count the number of extended attribute blocks
...@@ -1760,9 +1757,6 @@ xfs_swap_extents( ...@@ -1760,9 +1757,6 @@ xfs_swap_extents(
goto out_trans_cancel; goto out_trans_cancel;
} }
xfs_trans_ijoin(tp, ip, lock_flags);
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
* appropriately. We have to do this as we are demand paging the btree * appropriately. We have to do this as we are demand paging the btree
...@@ -1896,5 +1890,5 @@ xfs_swap_extents( ...@@ -1896,5 +1890,5 @@ xfs_swap_extents(
out_trans_cancel: out_trans_cancel:
xfs_trans_cancel(tp, 0); xfs_trans_cancel(tp, 0);
goto out_unlock; goto out;
} }
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