blob: e36445ceaf80c82b46c1ef634a3495c43c845685 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_bmap_btree.h"
Eric Sandeen427d9fe2012-03-27 15:40:26 -050027#include "xfs_alloc_btree.h"
28#include "xfs_ialloc_btree.h"
29#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_itable.h"
35#include "xfs_dfrag.h"
36#include "xfs_error.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100037#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000038#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Dave Chinner6bded0f2010-01-14 01:33:56 +000040
41static int xfs_swap_extents(
42 xfs_inode_t *ip, /* target inode */
43 xfs_inode_t *tip, /* tmp inode */
44 xfs_swapext_t *sxp);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Dave Chinner6bded0f2010-01-14 01:33:56 +000047 * ioctl interface for swapext
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49int
50xfs_swapext(
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -060051 xfs_swapext_t *sxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Hellwig35fec8d2008-02-05 12:13:07 +110053 xfs_inode_t *ip, *tip;
Al Viro2903ff02012-08-28 12:52:22 -040054 struct fd f, tmp;
55 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 /* Pull information for the target fd */
Al Viro2903ff02012-08-28 12:52:22 -040058 f = fdget((int)sxp->sx_fdtarget);
59 if (!f.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 error = XFS_ERROR(EINVAL);
Eric Sandeenac12b4e2009-01-25 20:53:00 -060061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
Al Viro2903ff02012-08-28 12:52:22 -040064 if (!(f.file->f_mode & FMODE_WRITE) ||
65 !(f.file->f_mode & FMODE_READ) ||
66 (f.file->f_flags & O_APPEND)) {
Christoph Hellwigf6aa7f22008-02-05 12:13:15 +110067 error = XFS_ERROR(EBADF);
68 goto out_put_file;
69 }
70
Al Viro2903ff02012-08-28 12:52:22 -040071 tmp = fdget((int)sxp->sx_fdtmp);
72 if (!tmp.file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 error = XFS_ERROR(EINVAL);
Christoph Hellwig35fec8d2008-02-05 12:13:07 +110074 goto out_put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
Al Viro2903ff02012-08-28 12:52:22 -040077 if (!(tmp.file->f_mode & FMODE_WRITE) ||
78 !(tmp.file->f_mode & FMODE_READ) ||
79 (tmp.file->f_flags & O_APPEND)) {
Christoph Hellwigf6aa7f22008-02-05 12:13:15 +110080 error = XFS_ERROR(EBADF);
Dave Chinner6bded0f2010-01-14 01:33:56 +000081 goto out_put_tmp_file;
Christoph Hellwigf6aa7f22008-02-05 12:13:15 +110082 }
83
Al Viro496ad9a2013-01-23 17:07:38 -050084 if (IS_SWAPFILE(file_inode(f.file)) ||
85 IS_SWAPFILE(file_inode(tmp.file))) {
Christoph Hellwig7c8f7af2009-02-12 19:56:00 +010086 error = XFS_ERROR(EINVAL);
Dave Chinner6bded0f2010-01-14 01:33:56 +000087 goto out_put_tmp_file;
Christoph Hellwig7c8f7af2009-02-12 19:56:00 +010088 }
89
Al Viro496ad9a2013-01-23 17:07:38 -050090 ip = XFS_I(file_inode(f.file));
91 tip = XFS_I(file_inode(tmp.file));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (ip->i_mount != tip->i_mount) {
Christoph Hellwig35fec8d2008-02-05 12:13:07 +110094 error = XFS_ERROR(EINVAL);
Dave Chinner6bded0f2010-01-14 01:33:56 +000095 goto out_put_tmp_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97
98 if (ip->i_ino == tip->i_ino) {
Christoph Hellwig35fec8d2008-02-05 12:13:07 +110099 error = XFS_ERROR(EINVAL);
Dave Chinner6bded0f2010-01-14 01:33:56 +0000100 goto out_put_tmp_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
Christoph Hellwig35fec8d2008-02-05 12:13:07 +1100103 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
104 error = XFS_ERROR(EIO);
Dave Chinner6bded0f2010-01-14 01:33:56 +0000105 goto out_put_tmp_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000108 error = xfs_swap_extents(ip, tip, sxp);
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000109
Dave Chinner6bded0f2010-01-14 01:33:56 +0000110 out_put_tmp_file:
Al Viro2903ff02012-08-28 12:52:22 -0400111 fdput(tmp);
Christoph Hellwig35fec8d2008-02-05 12:13:07 +1100112 out_put_file:
Al Viro2903ff02012-08-28 12:52:22 -0400113 fdput(f);
Christoph Hellwig35fec8d2008-02-05 12:13:07 +1100114 out:
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000115 return error;
116}
117
Dave Chinnere09f9862010-01-14 01:33:54 +0000118/*
119 * We need to check that the format of the data fork in the temporary inode is
120 * valid for the target inode before doing the swap. This is not a problem with
121 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
122 * data fork depending on the space the attribute fork is taking so we can get
123 * invalid formats on the target inode.
124 *
125 * E.g. target has space for 7 extents in extent format, temp inode only has
126 * space for 6. If we defragment down to 7 extents, then the tmp format is a
127 * btree, but when swapped it needs to be in extent format. Hence we can't just
128 * blindly swap data forks on attr2 filesystems.
129 *
130 * Note that we check the swap in both directions so that we don't end up with
131 * a corrupt temporary inode, either.
132 *
133 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
134 * inode will prevent this situation from occurring, so all we do here is
135 * reject and log the attempt. basically we are putting the responsibility on
136 * userspace to get this right.
137 */
138static int
139xfs_swap_extents_check_format(
140 xfs_inode_t *ip, /* target inode */
141 xfs_inode_t *tip) /* tmp inode */
142{
143
144 /* Should never get a local format */
145 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
146 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
147 return EINVAL;
148
149 /*
150 * if the target inode has less extents that then temporary inode then
151 * why did userspace call us?
152 */
153 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
154 return EINVAL;
155
156 /*
157 * if the target inode is in extent form and the temp inode is in btree
158 * form then we will end up with the target inode in the wrong format
159 * as we already know there are less extents in the temp inode.
160 */
161 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
162 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
163 return EINVAL;
164
165 /* Check temp in extent form to max in target */
166 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000167 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
168 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinnere09f9862010-01-14 01:33:54 +0000169 return EINVAL;
170
171 /* Check target in extent form to max in temp */
172 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000173 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
174 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinnere09f9862010-01-14 01:33:54 +0000175 return EINVAL;
176
Dave Chinnerdd77ef92010-04-20 17:00:37 +1000177 /*
178 * If we are in a btree format, check that the temp root block will fit
179 * in the target and that it has enough extents to be in btree format
180 * in the target.
181 *
182 * Note that we have to be careful to allow btree->extent conversions
183 * (a common defrag case) which will occur when the temp inode is in
184 * extent format...
185 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000186 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
187 if (XFS_IFORK_BOFF(ip) &&
Eric Sandeen427d9fe2012-03-27 15:40:26 -0500188 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000189 return EINVAL;
190 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
191 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
192 return EINVAL;
193 }
Dave Chinnere09f9862010-01-14 01:33:54 +0000194
Dave Chinnerdd77ef92010-04-20 17:00:37 +1000195 /* Reciprocal target->temp btree format checks */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000196 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
197 if (XFS_IFORK_BOFF(tip) &&
Eric Sandeen427d9fe2012-03-27 15:40:26 -0500198 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000199 return EINVAL;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000200 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
201 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
202 return EINVAL;
203 }
Dave Chinnere09f9862010-01-14 01:33:54 +0000204
205 return 0;
206}
207
Dave Chinner6bded0f2010-01-14 01:33:56 +0000208static int
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000209xfs_swap_extents(
Dave Chinnere09f9862010-01-14 01:33:54 +0000210 xfs_inode_t *ip, /* target inode */
211 xfs_inode_t *tip, /* tmp inode */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000212 xfs_swapext_t *sxp)
213{
David Sterba45c51b92011-04-13 22:03:28 +0000214 xfs_mount_t *mp = ip->i_mount;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000215 xfs_trans_t *tp;
216 xfs_bstat_t *sbp = &sxp->sx_stat;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000217 xfs_ifork_t *tempifp, *ifp, *tifp;
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000218 int src_log_flags, target_log_flags;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000219 int error = 0;
220 int aforkblks = 0;
221 int taforkblks = 0;
222 __uint64_t tmp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000223
Dave Chinner02f75402013-05-27 16:38:24 +1000224 /*
225 * We have no way of updating owner information in the BMBT blocks for
226 * each inode on CRC enabled filesystems, so to avoid corrupting the
227 * this metadata we simply don't allow extent swaps to occur.
228 */
229 if (xfs_sb_version_hascrc(&mp->m_sb))
230 return XFS_ERROR(EINVAL);
231
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000232 tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
233 if (!tempifp) {
234 error = XFS_ERROR(ENOMEM);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100235 goto out;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000236 }
237
David Chinnerf9114eb2008-09-17 16:51:21 +1000238 /*
239 * we have to do two separate lock calls here to keep lockdep
240 * happy. If we try to get all the locks in one call, lock will
241 * report false positives when we drop the ILOCK and regain them
242 * below.
243 */
244 xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
245 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Verify that both files have the same format */
248 if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
249 error = XFS_ERROR(EINVAL);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100250 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 /* Verify both files are either real-time or non-realtime */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100254 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 error = XFS_ERROR(EINVAL);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100256 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Torsten Kaiser65e3aa72013-01-20 10:24:49 +0100259 error = -filemap_write_and_wait(VFS_I(tip)->i_mapping);
Dave Chinnerfb595812012-11-12 22:53:57 +1100260 if (error)
261 goto out_unlock;
Torsten Kaiser65e3aa72013-01-20 10:24:49 +0100262 truncate_pagecache_range(VFS_I(tip), 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Verify O_DIRECT for ftmp */
Christoph Hellwigdf80c932008-08-13 16:22:09 +1000265 if (VN_CACHED(VFS_I(tip)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 error = XFS_ERROR(EINVAL);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100267 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 /* Verify all data are being swapped */
Eric Sandeend0cfb372005-11-02 10:29:04 +1100271 if (sxp->sx_offset != 0 ||
272 sxp->sx_length != ip->i_d.di_size ||
273 sxp->sx_length != tip->i_d.di_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 error = XFS_ERROR(EFAULT);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100275 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
Dave Chinner3a85cd92010-01-14 01:33:55 +0000278 trace_xfs_swap_extent_before(ip, 0);
279 trace_xfs_swap_extent_before(tip, 1);
280
Dave Chinnere09f9862010-01-14 01:33:54 +0000281 /* check inode formats now that data is flushed */
282 error = xfs_swap_extents_check_format(ip, tip);
283 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +1100284 xfs_notice(mp,
Dave Chinnere09f9862010-01-14 01:33:54 +0000285 "%s: inode 0x%llx format is incompatible for exchanging.",
Dave Chinner53487782011-03-07 10:05:35 +1100286 __func__, ip->i_ino);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100287 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 /*
291 * Compare the current change & modify times with that
292 * passed in. If they differ, we abort this swap.
293 * This is the mechanism used to ensure the calling
294 * process that the file was not changed out from
295 * under it.
296 */
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000297 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
298 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
299 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
300 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 error = XFS_ERROR(EBUSY);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100302 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 /* We need to fail if the file is memory mapped. Once we have tossed
306 * all existing pages, the page fault will have no option
307 * but to go to the filesystem for pages. By making the page fault call
Nathan Scott67fcaa72006-06-09 17:00:52 +1000308 * vop_read (or write in the case of autogrow) they block on the iolock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * until we have switched the extents.
310 */
Christoph Hellwigdf80c932008-08-13 16:22:09 +1000311 if (VN_MAPPED(VFS_I(ip))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 error = XFS_ERROR(EBUSY);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100313 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 xfs_iunlock(ip, XFS_ILOCK_EXCL);
317 xfs_iunlock(tip, XFS_ILOCK_EXCL);
318
319 /*
320 * There is a race condition here since we gave up the
321 * ilock. However, the data fork will not change since
322 * we have the iolock (locked for truncation too) so we
323 * are safe. We don't really care if non-io related
324 * fields change.
325 */
Dave Chinnerf5b89112012-11-14 17:42:47 +1100326 truncate_pagecache_range(VFS_I(ip), 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
329 if ((error = xfs_trans_reserve(tp, 0,
330 XFS_ICHANGE_LOG_RES(mp), 0,
331 0, 0))) {
332 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
333 xfs_iunlock(tip, XFS_IOLOCK_EXCL);
334 xfs_trans_cancel(tp, 0);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Christoph Hellwige1cccd92008-08-13 16:18:07 +1000337 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
340 * Count the number of extended attribute blocks
341 */
342 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
343 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
344 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100345 if (error)
346 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
349 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
350 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
351 &taforkblks);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100352 if (error)
353 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 /*
357 * Swap the data forks of the inodes
358 */
359 ifp = &ip->i_df;
360 tifp = &tip->i_df;
Eric Sandeend0cfb372005-11-02 10:29:04 +1100361 *tempifp = *ifp; /* struct copy */
362 *ifp = *tifp; /* struct copy */
363 *tifp = *tempifp; /* struct copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /*
366 * Fix the on-disk inode values
367 */
368 tmp = (__uint64_t)ip->i_d.di_nblocks;
369 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
370 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
371
372 tmp = (__uint64_t) ip->i_d.di_nextents;
373 ip->i_d.di_nextents = tip->i_d.di_nextents;
374 tip->i_d.di_nextents = tmp;
375
376 tmp = (__uint64_t) ip->i_d.di_format;
377 ip->i_d.di_format = tip->i_d.di_format;
378 tip->i_d.di_format = tmp;
379
Dave Chinner309c8482010-11-30 15:16:02 +1100380 /*
381 * The extents in the source inode could still contain speculative
382 * preallocation beyond EOF (e.g. the file is open but not modified
383 * while defrag is in progress). In that case, we need to copy over the
384 * number of delalloc blocks the data fork in the source inode is
385 * tracking beyond EOF so that when the fork is truncated away when the
386 * temporary inode is unlinked we don't underrun the i_delayed_blks
387 * counter on that inode.
388 */
389 ASSERT(tip->i_delayed_blks == 0);
390 tip->i_delayed_blks = ip->i_delayed_blks;
391 ip->i_delayed_blks = 0;
392
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000393 src_log_flags = XFS_ILOG_CORE;
394 switch (ip->i_d.di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 case XFS_DINODE_FMT_EXTENTS:
396 /* If the extents fit in the inode, fix the
397 * pointer. Otherwise it's already NULL or
398 * pointing to the extent.
399 */
400 if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
401 ifp->if_u1.if_extents =
402 ifp->if_u2.if_inline_ext;
403 }
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000404 src_log_flags |= XFS_ILOG_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000407 src_log_flags |= XFS_ILOG_DBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409 }
410
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000411 target_log_flags = XFS_ILOG_CORE;
412 switch (tip->i_d.di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 case XFS_DINODE_FMT_EXTENTS:
414 /* If the extents fit in the inode, fix the
415 * pointer. Otherwise it's already NULL or
416 * pointing to the extent.
417 */
418 if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
419 tifp->if_u1.if_extents =
420 tifp->if_u2.if_inline_ext;
421 }
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000422 target_log_flags |= XFS_ILOG_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
424 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000425 target_log_flags |= XFS_ILOG_DBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 }
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Christoph Hellwigddc34152011-09-19 15:00:54 +0000430 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
431 xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000433 xfs_trans_log_inode(tp, ip, src_log_flags);
434 xfs_trans_log_inode(tp, tip, target_log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * If this is a synchronous mount, make sure that the
438 * transaction goes to disk before returning to the user.
439 */
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100440 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 xfs_trans_set_sync(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Christoph Hellwig815cb212011-09-26 09:14:34 +0000443 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Chinner3a85cd92010-01-14 01:33:55 +0000445 trace_xfs_swap_extent_after(ip, 0);
446 trace_xfs_swap_extent_after(tip, 1);
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100447out:
448 kmem_free(tempifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100450
Felix Blyakher1f239202009-05-07 19:49:45 -0500451out_unlock:
452 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
453 xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
454 goto out;
455
Josef 'Jeff' Sipekef8f7fc2009-02-04 09:37:43 +0100456out_trans_cancel:
457 xfs_trans_cancel(tp, 0);
458 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}