2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2006-06-09 08:48:12 +04:00
|
|
|
* Copyright (c) 2000-2006 Silicon Graphics, Inc.
|
2005-11-02 06:58:39 +03:00
|
|
|
* All Rights Reserved.
|
2005-04-17 02:20:36 +04:00
|
|
|
*
|
2005-11-02 06:58:39 +03:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
2005-04-17 02:20:36 +04:00
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
2005-11-02 06:58:39 +03:00
|
|
|
* This program is distributed in the hope that it would be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2005-04-17 02:20:36 +04:00
|
|
|
*
|
2005-11-02 06:58:39 +03:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_fs.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_types.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_bit.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_log.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_inum.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_trans.h"
|
|
|
|
#include "xfs_sb.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_ag.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_dir2.h"
|
|
|
|
#include "xfs_dmapi.h"
|
|
|
|
#include "xfs_mount.h"
|
|
|
|
#include "xfs_bmap_btree.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_alloc_btree.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_ialloc_btree.h"
|
|
|
|
#include "xfs_dir2_sf.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_attr_sf.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_dinode.h"
|
|
|
|
#include "xfs_inode.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_inode_item.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_bmap.h"
|
2005-11-02 06:38:42 +03:00
|
|
|
#include "xfs_btree.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
#include "xfs_ialloc.h"
|
|
|
|
#include "xfs_itable.h"
|
|
|
|
#include "xfs_dfrag.h"
|
|
|
|
#include "xfs_error.h"
|
|
|
|
#include "xfs_rw.h"
|
2007-08-29 04:58:01 +04:00
|
|
|
#include "xfs_vnodeops.h"
|
2009-12-15 02:14:59 +03:00
|
|
|
#include "xfs_trace.h"
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-01-14 04:33:56 +03:00
|
|
|
|
|
|
|
static int xfs_swap_extents(
|
|
|
|
xfs_inode_t *ip, /* target inode */
|
|
|
|
xfs_inode_t *tip, /* tmp inode */
|
|
|
|
xfs_swapext_t *sxp);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2010-01-14 04:33:56 +03:00
|
|
|
* ioctl interface for swapext
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
xfs_swapext(
|
2008-11-26 06:20:06 +03:00
|
|
|
xfs_swapext_t *sxp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-02-05 04:13:07 +03:00
|
|
|
xfs_inode_t *ip, *tip;
|
2010-01-14 04:33:56 +03:00
|
|
|
struct file *file, *tmp_file;
|
2005-04-17 02:20:36 +04:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* Pull information for the target fd */
|
2008-02-05 04:13:07 +03:00
|
|
|
file = fget((int)sxp->sx_fdtarget);
|
|
|
|
if (!file) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2009-01-26 05:53:00 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-06-24 06:07:47 +04:00
|
|
|
if (!(file->f_mode & FMODE_WRITE) ||
|
|
|
|
!(file->f_mode & FMODE_READ) ||
|
|
|
|
(file->f_flags & O_APPEND)) {
|
2008-02-05 04:13:15 +03:00
|
|
|
error = XFS_ERROR(EBADF);
|
|
|
|
goto out_put_file;
|
|
|
|
}
|
|
|
|
|
2010-01-14 04:33:56 +03:00
|
|
|
tmp_file = fget((int)sxp->sx_fdtmp);
|
|
|
|
if (!tmp_file) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2008-02-05 04:13:07 +03:00
|
|
|
goto out_put_file;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-01-14 04:33:56 +03:00
|
|
|
if (!(tmp_file->f_mode & FMODE_WRITE) ||
|
2010-06-24 06:07:47 +04:00
|
|
|
!(tmp_file->f_mode & FMODE_READ) ||
|
2010-01-14 04:33:56 +03:00
|
|
|
(tmp_file->f_flags & O_APPEND)) {
|
2008-02-05 04:13:15 +03:00
|
|
|
error = XFS_ERROR(EBADF);
|
2010-01-14 04:33:56 +03:00
|
|
|
goto out_put_tmp_file;
|
2008-02-05 04:13:15 +03:00
|
|
|
}
|
|
|
|
|
2009-02-12 21:56:00 +03:00
|
|
|
if (IS_SWAPFILE(file->f_path.dentry->d_inode) ||
|
2010-01-14 04:33:56 +03:00
|
|
|
IS_SWAPFILE(tmp_file->f_path.dentry->d_inode)) {
|
2009-02-12 21:56:00 +03:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2010-01-14 04:33:56 +03:00
|
|
|
goto out_put_tmp_file;
|
2009-02-12 21:56:00 +03:00
|
|
|
}
|
|
|
|
|
2008-02-05 04:13:07 +03:00
|
|
|
ip = XFS_I(file->f_path.dentry->d_inode);
|
2010-01-14 04:33:56 +03:00
|
|
|
tip = XFS_I(tmp_file->f_path.dentry->d_inode);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (ip->i_mount != tip->i_mount) {
|
2008-02-05 04:13:07 +03:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2010-01-14 04:33:56 +03:00
|
|
|
goto out_put_tmp_file;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ip->i_ino == tip->i_ino) {
|
2008-02-05 04:13:07 +03:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2010-01-14 04:33:56 +03:00
|
|
|
goto out_put_tmp_file;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2008-02-05 04:13:07 +03:00
|
|
|
if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
|
|
|
|
error = XFS_ERROR(EIO);
|
2010-01-14 04:33:56 +03:00
|
|
|
goto out_put_tmp_file;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2007-10-11 11:34:33 +04:00
|
|
|
error = xfs_swap_extents(ip, tip, sxp);
|
2006-06-09 08:48:12 +04:00
|
|
|
|
2010-01-14 04:33:56 +03:00
|
|
|
out_put_tmp_file:
|
|
|
|
fput(tmp_file);
|
2008-02-05 04:13:07 +03:00
|
|
|
out_put_file:
|
|
|
|
fput(file);
|
|
|
|
out:
|
2006-06-09 08:48:12 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2010-01-14 04:33:54 +03:00
|
|
|
/*
|
|
|
|
* We need to check that the format of the data fork in the temporary inode is
|
|
|
|
* valid for the target inode before doing the swap. This is not a problem with
|
|
|
|
* attr1 because of the fixed fork offset, but attr2 has a dynamically sized
|
|
|
|
* data fork depending on the space the attribute fork is taking so we can get
|
|
|
|
* invalid formats on the target inode.
|
|
|
|
*
|
|
|
|
* E.g. target has space for 7 extents in extent format, temp inode only has
|
|
|
|
* space for 6. If we defragment down to 7 extents, then the tmp format is a
|
|
|
|
* btree, but when swapped it needs to be in extent format. Hence we can't just
|
|
|
|
* blindly swap data forks on attr2 filesystems.
|
|
|
|
*
|
|
|
|
* Note that we check the swap in both directions so that we don't end up with
|
|
|
|
* a corrupt temporary inode, either.
|
|
|
|
*
|
|
|
|
* Note that fixing the way xfs_fsr sets up the attribute fork in the source
|
|
|
|
* inode will prevent this situation from occurring, so all we do here is
|
|
|
|
* reject and log the attempt. basically we are putting the responsibility on
|
|
|
|
* userspace to get this right.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
xfs_swap_extents_check_format(
|
|
|
|
xfs_inode_t *ip, /* target inode */
|
|
|
|
xfs_inode_t *tip) /* tmp inode */
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Should never get a local format */
|
|
|
|
if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
|
|
|
|
tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the target inode has less extents that then temporary inode then
|
|
|
|
* why did userspace call us?
|
|
|
|
*/
|
|
|
|
if (ip->i_d.di_nextents < tip->i_d.di_nextents)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the target inode is in extent form and the temp inode is in btree
|
|
|
|
* form then we will end up with the target inode in the wrong format
|
|
|
|
* as we already know there are less extents in the temp inode.
|
|
|
|
*/
|
|
|
|
if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
|
|
|
|
tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Check temp in extent form to max in target */
|
|
|
|
if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
|
|
|
|
XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) > ip->i_df.if_ext_max)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Check target in extent form to max in temp */
|
|
|
|
if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
|
|
|
|
XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > tip->i_df.if_ext_max)
|
|
|
|
return EINVAL;
|
|
|
|
|
xfs: more swap extent fixes for dynamic fork offsets
A new xfsqa test (226) with a prototype xfs_fsr change to try to
handle dynamic fork offsets better triggers an assertion failure
where the inode data fork is in btree format, yet there is room in
the inode for it to be in extent format. The two inodes look like:
before: ino 0x101 (target), num_extents 11, Max in-fork extents 6, broot size 40, fork offset 96
before: ino 0x115 (temp), num_extents 5, Max in-fork extents 3, broot size 40, fork offset 56
after: ino 0x101 (target), num_extents 5, Max in-fork extents 6, broot size 40, fork offset 96
after: ino 0x115 (temp), num_extents 11, Max in-fork extents 3, broot size 40, fork offset 56
Basically the target inode ends up with 5 extents in btree format,
but it had space for 6 extents in extent format, so ends up
incorrect. Notably here the broot size is the same, and that is
where the kernel code is going wrong - the btree root will fit, so
it lets the swap go ahead.
The check should not allow the swap to take place if the number of
extents while in btree format is less than the number of extents
that can fit in the inode in extent format. Adding that check will
prevent this swap and corruption from occurring.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
2010-04-20 11:00:37 +04:00
|
|
|
/*
|
|
|
|
* If we are in a btree format, check that the temp root block will fit
|
|
|
|
* in the target and that it has enough extents to be in btree format
|
|
|
|
* in the target.
|
|
|
|
*
|
|
|
|
* Note that we have to be careful to allow btree->extent conversions
|
|
|
|
* (a common defrag case) which will occur when the temp inode is in
|
|
|
|
* extent format...
|
|
|
|
*/
|
2010-01-14 04:33:54 +03:00
|
|
|
if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
|
xfs: more swap extent fixes for dynamic fork offsets
A new xfsqa test (226) with a prototype xfs_fsr change to try to
handle dynamic fork offsets better triggers an assertion failure
where the inode data fork is in btree format, yet there is room in
the inode for it to be in extent format. The two inodes look like:
before: ino 0x101 (target), num_extents 11, Max in-fork extents 6, broot size 40, fork offset 96
before: ino 0x115 (temp), num_extents 5, Max in-fork extents 3, broot size 40, fork offset 56
after: ino 0x101 (target), num_extents 5, Max in-fork extents 6, broot size 40, fork offset 96
after: ino 0x115 (temp), num_extents 11, Max in-fork extents 3, broot size 40, fork offset 56
Basically the target inode ends up with 5 extents in btree format,
but it had space for 6 extents in extent format, so ends up
incorrect. Notably here the broot size is the same, and that is
where the kernel code is going wrong - the btree root will fit, so
it lets the swap go ahead.
The check should not allow the swap to take place if the number of
extents while in btree format is less than the number of extents
that can fit in the inode in extent format. Adding that check will
prevent this swap and corruption from occurring.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
2010-04-20 11:00:37 +04:00
|
|
|
((XFS_IFORK_BOFF(ip) &&
|
|
|
|
tip->i_df.if_broot_bytes > XFS_IFORK_BOFF(ip)) ||
|
|
|
|
XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <= ip->i_df.if_ext_max))
|
2010-01-14 04:33:54 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
xfs: more swap extent fixes for dynamic fork offsets
A new xfsqa test (226) with a prototype xfs_fsr change to try to
handle dynamic fork offsets better triggers an assertion failure
where the inode data fork is in btree format, yet there is room in
the inode for it to be in extent format. The two inodes look like:
before: ino 0x101 (target), num_extents 11, Max in-fork extents 6, broot size 40, fork offset 96
before: ino 0x115 (temp), num_extents 5, Max in-fork extents 3, broot size 40, fork offset 56
after: ino 0x101 (target), num_extents 5, Max in-fork extents 6, broot size 40, fork offset 96
after: ino 0x115 (temp), num_extents 11, Max in-fork extents 3, broot size 40, fork offset 56
Basically the target inode ends up with 5 extents in btree format,
but it had space for 6 extents in extent format, so ends up
incorrect. Notably here the broot size is the same, and that is
where the kernel code is going wrong - the btree root will fit, so
it lets the swap go ahead.
The check should not allow the swap to take place if the number of
extents while in btree format is less than the number of extents
that can fit in the inode in extent format. Adding that check will
prevent this swap and corruption from occurring.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
2010-04-20 11:00:37 +04:00
|
|
|
/* Reciprocal target->temp btree format checks */
|
2010-01-14 04:33:54 +03:00
|
|
|
if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
|
xfs: more swap extent fixes for dynamic fork offsets
A new xfsqa test (226) with a prototype xfs_fsr change to try to
handle dynamic fork offsets better triggers an assertion failure
where the inode data fork is in btree format, yet there is room in
the inode for it to be in extent format. The two inodes look like:
before: ino 0x101 (target), num_extents 11, Max in-fork extents 6, broot size 40, fork offset 96
before: ino 0x115 (temp), num_extents 5, Max in-fork extents 3, broot size 40, fork offset 56
after: ino 0x101 (target), num_extents 5, Max in-fork extents 6, broot size 40, fork offset 96
after: ino 0x115 (temp), num_extents 11, Max in-fork extents 3, broot size 40, fork offset 56
Basically the target inode ends up with 5 extents in btree format,
but it had space for 6 extents in extent format, so ends up
incorrect. Notably here the broot size is the same, and that is
where the kernel code is going wrong - the btree root will fit, so
it lets the swap go ahead.
The check should not allow the swap to take place if the number of
extents while in btree format is less than the number of extents
that can fit in the inode in extent format. Adding that check will
prevent this swap and corruption from occurring.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
2010-04-20 11:00:37 +04:00
|
|
|
((XFS_IFORK_BOFF(tip) &&
|
|
|
|
ip->i_df.if_broot_bytes > XFS_IFORK_BOFF(tip)) ||
|
|
|
|
XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <= tip->i_df.if_ext_max))
|
2010-01-14 04:33:54 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-14 04:33:56 +03:00
|
|
|
static int
|
2006-06-09 08:48:12 +04:00
|
|
|
xfs_swap_extents(
|
2010-01-14 04:33:54 +03:00
|
|
|
xfs_inode_t *ip, /* target inode */
|
|
|
|
xfs_inode_t *tip, /* tmp inode */
|
2006-06-09 08:48:12 +04:00
|
|
|
xfs_swapext_t *sxp)
|
|
|
|
{
|
|
|
|
xfs_mount_t *mp;
|
|
|
|
xfs_trans_t *tp;
|
|
|
|
xfs_bstat_t *sbp = &sxp->sx_stat;
|
|
|
|
xfs_ifork_t *tempifp, *ifp, *tifp;
|
|
|
|
int ilf_fields, tilf_fields;
|
|
|
|
int error = 0;
|
|
|
|
int aforkblks = 0;
|
|
|
|
int taforkblks = 0;
|
|
|
|
__uint64_t tmp;
|
|
|
|
|
|
|
|
mp = ip->i_mount;
|
|
|
|
|
|
|
|
tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
|
|
|
|
if (!tempifp) {
|
|
|
|
error = XFS_ERROR(ENOMEM);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out;
|
2006-06-09 08:48:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sbp = &sxp->sx_stat;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-09-17 10:51:21 +04:00
|
|
|
/*
|
|
|
|
* we have to do two separate lock calls here to keep lockdep
|
|
|
|
* happy. If we try to get all the locks in one call, lock will
|
|
|
|
* report false positives when we drop the ILOCK and regain them
|
|
|
|
* below.
|
|
|
|
*/
|
|
|
|
xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
|
|
|
|
xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* Verify that both files have the same format */
|
|
|
|
if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
|
|
|
|
error = XFS_ERROR(EINVAL);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify both files are either real-time or non-realtime */
|
2007-11-23 08:29:42 +03:00
|
|
|
if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:22:09 +04:00
|
|
|
if (VN_CACHED(VFS_I(tip)) != 0) {
|
2007-08-29 04:58:01 +04:00
|
|
|
error = xfs_flushinval_pages(tip, 0, -1,
|
|
|
|
FI_REMAPF_LOCKED);
|
2007-05-08 07:49:27 +04:00
|
|
|
if (error)
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-06-21 09:47:39 +04:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/* Verify O_DIRECT for ftmp */
|
2008-08-13 10:22:09 +04:00
|
|
|
if (VN_CACHED(VFS_I(tip)) != 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EINVAL);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify all data are being swapped */
|
2005-11-02 02:29:04 +03:00
|
|
|
if (sxp->sx_offset != 0 ||
|
|
|
|
sxp->sx_length != ip->i_d.di_size ||
|
|
|
|
sxp->sx_length != tip->i_d.di_size) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EFAULT);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-01-14 04:33:55 +03:00
|
|
|
trace_xfs_swap_extent_before(ip, 0);
|
|
|
|
trace_xfs_swap_extent_before(tip, 1);
|
|
|
|
|
2010-01-14 04:33:54 +03:00
|
|
|
/* check inode formats now that data is flushed */
|
|
|
|
error = xfs_swap_extents_check_format(ip, tip);
|
|
|
|
if (error) {
|
|
|
|
xfs_fs_cmn_err(CE_NOTE, mp,
|
|
|
|
"%s: inode 0x%llx format is incompatible for exchanging.",
|
|
|
|
__FILE__, ip->i_ino);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare the current change & modify times with that
|
|
|
|
* passed in. If they differ, we abort this swap.
|
|
|
|
* This is the mechanism used to ensure the calling
|
|
|
|
* process that the file was not changed out from
|
|
|
|
* under it.
|
|
|
|
*/
|
2009-10-07 00:29:26 +04:00
|
|
|
if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
|
|
|
|
(sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
|
|
|
|
(sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
|
|
|
|
(sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EBUSY);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2006-06-09 11:00:52 +04:00
|
|
|
* vop_read (or write in the case of autogrow) they block on the iolock
|
2005-04-17 02:20:36 +04:00
|
|
|
* until we have switched the extents.
|
|
|
|
*/
|
2008-08-13 10:22:09 +04:00
|
|
|
if (VN_MAPPED(VFS_I(ip))) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = XFS_ERROR(EBUSY);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
xfs_iunlock(ip, XFS_ILOCK_EXCL);
|
|
|
|
xfs_iunlock(tip, XFS_ILOCK_EXCL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is a race condition here since we gave up the
|
|
|
|
* ilock. However, the data fork will not change since
|
|
|
|
* we have the iolock (locked for truncation too) so we
|
|
|
|
* are safe. We don't really care if non-io related
|
|
|
|
* fields change.
|
|
|
|
*/
|
|
|
|
|
2007-08-29 04:58:01 +04:00
|
|
|
xfs_tosspages(ip, 0, -1, FI_REMAPF);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
|
|
|
|
if ((error = xfs_trans_reserve(tp, 0,
|
|
|
|
XFS_ICHANGE_LOG_RES(mp), 0,
|
|
|
|
0, 0))) {
|
|
|
|
xfs_iunlock(ip, XFS_IOLOCK_EXCL);
|
|
|
|
xfs_iunlock(tip, XFS_IOLOCK_EXCL);
|
|
|
|
xfs_trans_cancel(tp, 0);
|
2009-02-04 11:37:43 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2008-08-13 10:18:07 +04:00
|
|
|
xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the number of extended attribute blocks
|
|
|
|
*/
|
|
|
|
if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
|
|
|
|
(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
|
|
|
|
error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
|
2009-02-04 11:37:43 +03:00
|
|
|
if (error)
|
|
|
|
goto out_trans_cancel;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
|
|
|
|
(tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
|
|
|
|
error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
|
|
|
|
&taforkblks);
|
2009-02-04 11:37:43 +03:00
|
|
|
if (error)
|
|
|
|
goto out_trans_cancel;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swap the data forks of the inodes
|
|
|
|
*/
|
|
|
|
ifp = &ip->i_df;
|
|
|
|
tifp = &tip->i_df;
|
2005-11-02 02:29:04 +03:00
|
|
|
*tempifp = *ifp; /* struct copy */
|
|
|
|
*ifp = *tifp; /* struct copy */
|
|
|
|
*tifp = *tempifp; /* struct copy */
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-01-14 04:33:54 +03:00
|
|
|
/*
|
|
|
|
* Fix the in-memory data fork values that are dependent on the fork
|
|
|
|
* offset in the inode. We can't assume they remain the same as attr2
|
|
|
|
* has dynamic fork offsets.
|
|
|
|
*/
|
|
|
|
ifp->if_ext_max = XFS_IFORK_SIZE(ip, XFS_DATA_FORK) /
|
|
|
|
(uint)sizeof(xfs_bmbt_rec_t);
|
|
|
|
tifp->if_ext_max = XFS_IFORK_SIZE(tip, XFS_DATA_FORK) /
|
|
|
|
(uint)sizeof(xfs_bmbt_rec_t);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Fix the on-disk inode values
|
|
|
|
*/
|
|
|
|
tmp = (__uint64_t)ip->i_d.di_nblocks;
|
|
|
|
ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
|
|
|
|
tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
|
|
|
|
|
|
|
|
tmp = (__uint64_t) ip->i_d.di_nextents;
|
|
|
|
ip->i_d.di_nextents = tip->i_d.di_nextents;
|
|
|
|
tip->i_d.di_nextents = tmp;
|
|
|
|
|
|
|
|
tmp = (__uint64_t) ip->i_d.di_format;
|
|
|
|
ip->i_d.di_format = tip->i_d.di_format;
|
|
|
|
tip->i_d.di_format = tmp;
|
|
|
|
|
|
|
|
ilf_fields = XFS_ILOG_CORE;
|
|
|
|
|
|
|
|
switch(ip->i_d.di_format) {
|
|
|
|
case XFS_DINODE_FMT_EXTENTS:
|
|
|
|
/* If the extents fit in the inode, fix the
|
|
|
|
* pointer. Otherwise it's already NULL or
|
|
|
|
* pointing to the extent.
|
|
|
|
*/
|
|
|
|
if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
|
|
|
|
ifp->if_u1.if_extents =
|
|
|
|
ifp->if_u2.if_inline_ext;
|
|
|
|
}
|
|
|
|
ilf_fields |= XFS_ILOG_DEXT;
|
|
|
|
break;
|
|
|
|
case XFS_DINODE_FMT_BTREE:
|
|
|
|
ilf_fields |= XFS_ILOG_DBROOT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tilf_fields = XFS_ILOG_CORE;
|
|
|
|
|
|
|
|
switch(tip->i_d.di_format) {
|
|
|
|
case XFS_DINODE_FMT_EXTENTS:
|
|
|
|
/* If the extents fit in the inode, fix the
|
|
|
|
* pointer. Otherwise it's already NULL or
|
|
|
|
* pointing to the extent.
|
|
|
|
*/
|
|
|
|
if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
|
|
|
|
tifp->if_u1.if_extents =
|
|
|
|
tifp->if_u2.if_inline_ext;
|
|
|
|
}
|
|
|
|
tilf_fields |= XFS_ILOG_DEXT;
|
|
|
|
break;
|
|
|
|
case XFS_DINODE_FMT_BTREE:
|
|
|
|
tilf_fields |= XFS_ILOG_DBROOT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 10:13:09 +04:00
|
|
|
IHOLD(ip);
|
2009-02-04 11:37:43 +03:00
|
|
|
xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
|
2008-08-13 10:13:09 +04:00
|
|
|
|
|
|
|
IHOLD(tip);
|
2009-02-04 11:37:43 +03:00
|
|
|
xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
xfs_trans_log_inode(tp, ip, ilf_fields);
|
|
|
|
xfs_trans_log_inode(tp, tip, tilf_fields);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a synchronous mount, make sure that the
|
|
|
|
* transaction goes to disk before returning to the user.
|
|
|
|
*/
|
2009-02-04 11:37:43 +03:00
|
|
|
if (mp->m_flags & XFS_MOUNT_WSYNC)
|
2005-04-17 02:20:36 +04:00
|
|
|
xfs_trans_set_sync(tp);
|
|
|
|
|
2007-05-08 07:48:42 +04:00
|
|
|
error = xfs_trans_commit(tp, XFS_TRANS_SWAPEXT);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2010-01-14 04:33:55 +03:00
|
|
|
trace_xfs_swap_extent_after(ip, 0);
|
|
|
|
trace_xfs_swap_extent_after(tip, 1);
|
2009-02-04 11:37:43 +03:00
|
|
|
out:
|
|
|
|
kmem_free(tempifp);
|
2005-04-17 02:20:36 +04:00
|
|
|
return error;
|
2009-02-04 11:37:43 +03:00
|
|
|
|
2009-05-08 04:49:45 +04:00
|
|
|
out_unlock:
|
|
|
|
xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
|
|
|
|
xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
|
|
|
|
goto out;
|
|
|
|
|
2009-02-04 11:37:43 +03:00
|
|
|
out_trans_cancel:
|
|
|
|
xfs_trans_cancel(tp, 0);
|
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|