Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
| 22 | #include "xfs_trans.h" |
| 23 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_bmap_btree.h" |
Eric Sandeen | 427d9fe | 2012-03-27 15:40:26 -0500 | [diff] [blame] | 27 | #include "xfs_alloc_btree.h" |
| 28 | #include "xfs_ialloc_btree.h" |
| 29 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame^] | 34 | #include "xfs_bmap_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_itable.h" |
| 36 | #include "xfs_dfrag.h" |
| 37 | #include "xfs_error.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 38 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 39 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 41 | |
| 42 | static int xfs_swap_extents( |
| 43 | xfs_inode_t *ip, /* target inode */ |
| 44 | xfs_inode_t *tip, /* tmp inode */ |
| 45 | xfs_swapext_t *sxp); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 48 | * ioctl interface for swapext |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
| 50 | int |
| 51 | xfs_swapext( |
sandeen@sandeen.net | 743bb465 | 2008-11-25 21:20:06 -0600 | [diff] [blame] | 52 | xfs_swapext_t *sxp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 54 | xfs_inode_t *ip, *tip; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 55 | struct fd f, tmp; |
| 56 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* Pull information for the target fd */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 59 | f = fdget((int)sxp->sx_fdtarget); |
| 60 | if (!f.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | error = XFS_ERROR(EINVAL); |
Eric Sandeen | ac12b4e | 2009-01-25 20:53:00 -0600 | [diff] [blame] | 62 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 65 | if (!(f.file->f_mode & FMODE_WRITE) || |
| 66 | !(f.file->f_mode & FMODE_READ) || |
| 67 | (f.file->f_flags & O_APPEND)) { |
Christoph Hellwig | f6aa7f2 | 2008-02-05 12:13:15 +1100 | [diff] [blame] | 68 | error = XFS_ERROR(EBADF); |
| 69 | goto out_put_file; |
| 70 | } |
| 71 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 72 | tmp = fdget((int)sxp->sx_fdtmp); |
| 73 | if (!tmp.file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | error = XFS_ERROR(EINVAL); |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 75 | goto out_put_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 78 | if (!(tmp.file->f_mode & FMODE_WRITE) || |
| 79 | !(tmp.file->f_mode & FMODE_READ) || |
| 80 | (tmp.file->f_flags & O_APPEND)) { |
Christoph Hellwig | f6aa7f2 | 2008-02-05 12:13:15 +1100 | [diff] [blame] | 81 | error = XFS_ERROR(EBADF); |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 82 | goto out_put_tmp_file; |
Christoph Hellwig | f6aa7f2 | 2008-02-05 12:13:15 +1100 | [diff] [blame] | 83 | } |
| 84 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 85 | if (IS_SWAPFILE(file_inode(f.file)) || |
| 86 | IS_SWAPFILE(file_inode(tmp.file))) { |
Christoph Hellwig | 7c8f7af | 2009-02-12 19:56:00 +0100 | [diff] [blame] | 87 | error = XFS_ERROR(EINVAL); |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 88 | goto out_put_tmp_file; |
Christoph Hellwig | 7c8f7af | 2009-02-12 19:56:00 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 91 | ip = XFS_I(file_inode(f.file)); |
| 92 | tip = XFS_I(file_inode(tmp.file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | if (ip->i_mount != tip->i_mount) { |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 95 | error = XFS_ERROR(EINVAL); |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 96 | goto out_put_tmp_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (ip->i_ino == tip->i_ino) { |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 100 | error = XFS_ERROR(EINVAL); |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 101 | goto out_put_tmp_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 104 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 105 | error = XFS_ERROR(EIO); |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 106 | goto out_put_tmp_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Lachlan McIlroy | 541d7d3 | 2007-10-11 17:34:33 +1000 | [diff] [blame] | 109 | error = xfs_swap_extents(ip, tip, sxp); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 110 | |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 111 | out_put_tmp_file: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 112 | fdput(tmp); |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 113 | out_put_file: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 114 | fdput(f); |
Christoph Hellwig | 35fec8d | 2008-02-05 12:13:07 +1100 | [diff] [blame] | 115 | out: |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 116 | return error; |
| 117 | } |
| 118 | |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 119 | /* |
| 120 | * We need to check that the format of the data fork in the temporary inode is |
| 121 | * valid for the target inode before doing the swap. This is not a problem with |
| 122 | * attr1 because of the fixed fork offset, but attr2 has a dynamically sized |
| 123 | * data fork depending on the space the attribute fork is taking so we can get |
| 124 | * invalid formats on the target inode. |
| 125 | * |
| 126 | * E.g. target has space for 7 extents in extent format, temp inode only has |
| 127 | * space for 6. If we defragment down to 7 extents, then the tmp format is a |
| 128 | * btree, but when swapped it needs to be in extent format. Hence we can't just |
| 129 | * blindly swap data forks on attr2 filesystems. |
| 130 | * |
| 131 | * Note that we check the swap in both directions so that we don't end up with |
| 132 | * a corrupt temporary inode, either. |
| 133 | * |
| 134 | * Note that fixing the way xfs_fsr sets up the attribute fork in the source |
| 135 | * inode will prevent this situation from occurring, so all we do here is |
| 136 | * reject and log the attempt. basically we are putting the responsibility on |
| 137 | * userspace to get this right. |
| 138 | */ |
| 139 | static int |
| 140 | xfs_swap_extents_check_format( |
| 141 | xfs_inode_t *ip, /* target inode */ |
| 142 | xfs_inode_t *tip) /* tmp inode */ |
| 143 | { |
| 144 | |
| 145 | /* Should never get a local format */ |
| 146 | if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL || |
| 147 | tip->i_d.di_format == XFS_DINODE_FMT_LOCAL) |
| 148 | return EINVAL; |
| 149 | |
| 150 | /* |
| 151 | * if the target inode has less extents that then temporary inode then |
| 152 | * why did userspace call us? |
| 153 | */ |
| 154 | if (ip->i_d.di_nextents < tip->i_d.di_nextents) |
| 155 | return EINVAL; |
| 156 | |
| 157 | /* |
| 158 | * if the target inode is in extent form and the temp inode is in btree |
| 159 | * form then we will end up with the target inode in the wrong format |
| 160 | * as we already know there are less extents in the temp inode. |
| 161 | */ |
| 162 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 163 | tip->i_d.di_format == XFS_DINODE_FMT_BTREE) |
| 164 | return EINVAL; |
| 165 | |
| 166 | /* Check temp in extent form to max in target */ |
| 167 | if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 168 | XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) > |
| 169 | XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 170 | return EINVAL; |
| 171 | |
| 172 | /* Check target in extent form to max in temp */ |
| 173 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 174 | XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > |
| 175 | XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 176 | return EINVAL; |
| 177 | |
Dave Chinner | dd77ef9 | 2010-04-20 17:00:37 +1000 | [diff] [blame] | 178 | /* |
| 179 | * If we are in a btree format, check that the temp root block will fit |
| 180 | * in the target and that it has enough extents to be in btree format |
| 181 | * in the target. |
| 182 | * |
| 183 | * Note that we have to be careful to allow btree->extent conversions |
| 184 | * (a common defrag case) which will occur when the temp inode is in |
| 185 | * extent format... |
| 186 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 187 | if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
| 188 | if (XFS_IFORK_BOFF(ip) && |
Eric Sandeen | 427d9fe | 2012-03-27 15:40:26 -0500 | [diff] [blame] | 189 | XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip)) |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 190 | return EINVAL; |
| 191 | if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <= |
| 192 | XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) |
| 193 | return EINVAL; |
| 194 | } |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 195 | |
Dave Chinner | dd77ef9 | 2010-04-20 17:00:37 +1000 | [diff] [blame] | 196 | /* Reciprocal target->temp btree format checks */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 197 | if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { |
| 198 | if (XFS_IFORK_BOFF(tip) && |
Eric Sandeen | 427d9fe | 2012-03-27 15:40:26 -0500 | [diff] [blame] | 199 | XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip)) |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 200 | return EINVAL; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 201 | if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <= |
| 202 | XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) |
| 203 | return EINVAL; |
| 204 | } |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Dave Chinner | 6bded0f | 2010-01-14 01:33:56 +0000 | [diff] [blame] | 209 | static int |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 210 | xfs_swap_extents( |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 211 | xfs_inode_t *ip, /* target inode */ |
| 212 | xfs_inode_t *tip, /* tmp inode */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 213 | xfs_swapext_t *sxp) |
| 214 | { |
David Sterba | 45c51b9 | 2011-04-13 22:03:28 +0000 | [diff] [blame] | 215 | xfs_mount_t *mp = ip->i_mount; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 216 | xfs_trans_t *tp; |
| 217 | xfs_bstat_t *sbp = &sxp->sx_stat; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 218 | xfs_ifork_t *tempifp, *ifp, *tifp; |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 219 | int src_log_flags, target_log_flags; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 220 | int error = 0; |
| 221 | int aforkblks = 0; |
| 222 | int taforkblks = 0; |
| 223 | __uint64_t tmp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 224 | |
Dave Chinner | 02f7540 | 2013-05-27 16:38:24 +1000 | [diff] [blame] | 225 | /* |
| 226 | * We have no way of updating owner information in the BMBT blocks for |
| 227 | * each inode on CRC enabled filesystems, so to avoid corrupting the |
| 228 | * this metadata we simply don't allow extent swaps to occur. |
| 229 | */ |
| 230 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 231 | return XFS_ERROR(EINVAL); |
| 232 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 233 | tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL); |
| 234 | if (!tempifp) { |
| 235 | error = XFS_ERROR(ENOMEM); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 236 | goto out; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 237 | } |
| 238 | |
David Chinner | f9114eb | 2008-09-17 16:51:21 +1000 | [diff] [blame] | 239 | /* |
| 240 | * we have to do two separate lock calls here to keep lockdep |
| 241 | * happy. If we try to get all the locks in one call, lock will |
| 242 | * report false positives when we drop the ILOCK and regain them |
| 243 | * below. |
| 244 | */ |
| 245 | xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL); |
| 246 | xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | /* Verify that both files have the same format */ |
| 249 | if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) { |
| 250 | error = XFS_ERROR(EINVAL); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 251 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* Verify both files are either real-time or non-realtime */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 255 | if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | error = XFS_ERROR(EINVAL); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 257 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Torsten Kaiser | 65e3aa7 | 2013-01-20 10:24:49 +0100 | [diff] [blame] | 260 | error = -filemap_write_and_wait(VFS_I(tip)->i_mapping); |
Dave Chinner | fb59581 | 2012-11-12 22:53:57 +1100 | [diff] [blame] | 261 | if (error) |
| 262 | goto out_unlock; |
Torsten Kaiser | 65e3aa7 | 2013-01-20 10:24:49 +0100 | [diff] [blame] | 263 | truncate_pagecache_range(VFS_I(tip), 0, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | /* Verify O_DIRECT for ftmp */ |
Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 266 | if (VN_CACHED(VFS_I(tip)) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | error = XFS_ERROR(EINVAL); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 268 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Verify all data are being swapped */ |
Eric Sandeen | d0cfb37 | 2005-11-02 10:29:04 +1100 | [diff] [blame] | 272 | if (sxp->sx_offset != 0 || |
| 273 | sxp->sx_length != ip->i_d.di_size || |
| 274 | sxp->sx_length != tip->i_d.di_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | error = XFS_ERROR(EFAULT); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 276 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dave Chinner | 3a85cd9 | 2010-01-14 01:33:55 +0000 | [diff] [blame] | 279 | trace_xfs_swap_extent_before(ip, 0); |
| 280 | trace_xfs_swap_extent_before(tip, 1); |
| 281 | |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 282 | /* check inode formats now that data is flushed */ |
| 283 | error = xfs_swap_extents_check_format(ip, tip); |
| 284 | if (error) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 285 | xfs_notice(mp, |
Dave Chinner | e09f986 | 2010-01-14 01:33:54 +0000 | [diff] [blame] | 286 | "%s: inode 0x%llx format is incompatible for exchanging.", |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 287 | __func__, ip->i_ino); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 288 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Compare the current change & modify times with that |
| 293 | * passed in. If they differ, we abort this swap. |
| 294 | * This is the mechanism used to ensure the calling |
| 295 | * process that the file was not changed out from |
| 296 | * under it. |
| 297 | */ |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 298 | if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) || |
| 299 | (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) || |
| 300 | (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) || |
| 301 | (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | error = XFS_ERROR(EBUSY); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 303 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* We need to fail if the file is memory mapped. Once we have tossed |
| 307 | * all existing pages, the page fault will have no option |
| 308 | * but to go to the filesystem for pages. By making the page fault call |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 309 | * vop_read (or write in the case of autogrow) they block on the iolock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | * until we have switched the extents. |
| 311 | */ |
Christoph Hellwig | df80c93 | 2008-08-13 16:22:09 +1000 | [diff] [blame] | 312 | if (VN_MAPPED(VFS_I(ip))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | error = XFS_ERROR(EBUSY); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 314 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 318 | xfs_iunlock(tip, XFS_ILOCK_EXCL); |
| 319 | |
| 320 | /* |
| 321 | * There is a race condition here since we gave up the |
| 322 | * ilock. However, the data fork will not change since |
| 323 | * we have the iolock (locked for truncation too) so we |
| 324 | * are safe. We don't really care if non-io related |
| 325 | * fields change. |
| 326 | */ |
Dave Chinner | f5b8911 | 2012-11-14 17:42:47 +1100 | [diff] [blame] | 327 | truncate_pagecache_range(VFS_I(ip), 0, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT); |
| 330 | if ((error = xfs_trans_reserve(tp, 0, |
| 331 | XFS_ICHANGE_LOG_RES(mp), 0, |
| 332 | 0, 0))) { |
| 333 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 334 | xfs_iunlock(tip, XFS_IOLOCK_EXCL); |
| 335 | xfs_trans_cancel(tp, 0); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 336 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
Christoph Hellwig | e1cccd9 | 2008-08-13 16:18:07 +1000 | [diff] [blame] | 338 | xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * Count the number of extended attribute blocks |
| 342 | */ |
| 343 | if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) && |
| 344 | (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { |
| 345 | error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 346 | if (error) |
| 347 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) && |
| 350 | (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { |
| 351 | error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, |
| 352 | &taforkblks); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 353 | if (error) |
| 354 | goto out_trans_cancel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Swap the data forks of the inodes |
| 359 | */ |
| 360 | ifp = &ip->i_df; |
| 361 | tifp = &tip->i_df; |
Eric Sandeen | d0cfb37 | 2005-11-02 10:29:04 +1100 | [diff] [blame] | 362 | *tempifp = *ifp; /* struct copy */ |
| 363 | *ifp = *tifp; /* struct copy */ |
| 364 | *tifp = *tempifp; /* struct copy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * Fix the on-disk inode values |
| 368 | */ |
| 369 | tmp = (__uint64_t)ip->i_d.di_nblocks; |
| 370 | ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks; |
| 371 | tip->i_d.di_nblocks = tmp + taforkblks - aforkblks; |
| 372 | |
| 373 | tmp = (__uint64_t) ip->i_d.di_nextents; |
| 374 | ip->i_d.di_nextents = tip->i_d.di_nextents; |
| 375 | tip->i_d.di_nextents = tmp; |
| 376 | |
| 377 | tmp = (__uint64_t) ip->i_d.di_format; |
| 378 | ip->i_d.di_format = tip->i_d.di_format; |
| 379 | tip->i_d.di_format = tmp; |
| 380 | |
Dave Chinner | 309c848 | 2010-11-30 15:16:02 +1100 | [diff] [blame] | 381 | /* |
| 382 | * The extents in the source inode could still contain speculative |
| 383 | * preallocation beyond EOF (e.g. the file is open but not modified |
| 384 | * while defrag is in progress). In that case, we need to copy over the |
| 385 | * number of delalloc blocks the data fork in the source inode is |
| 386 | * tracking beyond EOF so that when the fork is truncated away when the |
| 387 | * temporary inode is unlinked we don't underrun the i_delayed_blks |
| 388 | * counter on that inode. |
| 389 | */ |
| 390 | ASSERT(tip->i_delayed_blks == 0); |
| 391 | tip->i_delayed_blks = ip->i_delayed_blks; |
| 392 | ip->i_delayed_blks = 0; |
| 393 | |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 394 | src_log_flags = XFS_ILOG_CORE; |
| 395 | switch (ip->i_d.di_format) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | case XFS_DINODE_FMT_EXTENTS: |
| 397 | /* If the extents fit in the inode, fix the |
| 398 | * pointer. Otherwise it's already NULL or |
| 399 | * pointing to the extent. |
| 400 | */ |
| 401 | if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) { |
| 402 | ifp->if_u1.if_extents = |
| 403 | ifp->if_u2.if_inline_ext; |
| 404 | } |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 405 | src_log_flags |= XFS_ILOG_DEXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | break; |
| 407 | case XFS_DINODE_FMT_BTREE: |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 408 | src_log_flags |= XFS_ILOG_DBROOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 412 | target_log_flags = XFS_ILOG_CORE; |
| 413 | switch (tip->i_d.di_format) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | case XFS_DINODE_FMT_EXTENTS: |
| 415 | /* If the extents fit in the inode, fix the |
| 416 | * pointer. Otherwise it's already NULL or |
| 417 | * pointing to the extent. |
| 418 | */ |
| 419 | if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) { |
| 420 | tifp->if_u1.if_extents = |
| 421 | tifp->if_u2.if_inline_ext; |
| 422 | } |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 423 | target_log_flags |= XFS_ILOG_DEXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | break; |
| 425 | case XFS_DINODE_FMT_BTREE: |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 426 | target_log_flags |= XFS_ILOG_DBROOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 431 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 432 | xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Christoph Hellwig | f5d8d5c | 2012-02-29 09:53:54 +0000 | [diff] [blame] | 434 | xfs_trans_log_inode(tp, ip, src_log_flags); |
| 435 | xfs_trans_log_inode(tp, tip, target_log_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * If this is a synchronous mount, make sure that the |
| 439 | * transaction goes to disk before returning to the user. |
| 440 | */ |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 441 | if (mp->m_flags & XFS_MOUNT_WSYNC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | xfs_trans_set_sync(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Christoph Hellwig | 815cb21 | 2011-09-26 09:14:34 +0000 | [diff] [blame] | 444 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Dave Chinner | 3a85cd9 | 2010-01-14 01:33:55 +0000 | [diff] [blame] | 446 | trace_xfs_swap_extent_after(ip, 0); |
| 447 | trace_xfs_swap_extent_after(tip, 1); |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 448 | out: |
| 449 | kmem_free(tempifp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | return error; |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 451 | |
Felix Blyakher | 1f23920 | 2009-05-07 19:49:45 -0500 | [diff] [blame] | 452 | out_unlock: |
| 453 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 454 | xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 455 | goto out; |
| 456 | |
Josef 'Jeff' Sipek | ef8f7fc | 2009-02-04 09:37:43 +0100 | [diff] [blame] | 457 | out_trans_cancel: |
| 458 | xfs_trans_cancel(tp, 0); |
| 459 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |