xfs_dfrag.c 12.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * 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.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * 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
L
Linus Torvalds 已提交
17 18
 */
#include "xfs.h"
19
#include "xfs_fs.h"
L
Linus Torvalds 已提交
20 21 22 23
#include "xfs_types.h"
#include "xfs_log.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
24
#include "xfs_ag.h"
L
Linus Torvalds 已提交
25 26
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
27 28 29
#include "xfs_alloc_btree.h"
#include "xfs_ialloc_btree.h"
#include "xfs_btree.h"
L
Linus Torvalds 已提交
30 31
#include "xfs_dinode.h"
#include "xfs_inode.h"
32
#include "xfs_inode_item.h"
L
Linus Torvalds 已提交
33
#include "xfs_bmap.h"
D
Dave Chinner 已提交
34
#include "xfs_bmap_util.h"
L
Linus Torvalds 已提交
35 36 37
#include "xfs_itable.h"
#include "xfs_dfrag.h"
#include "xfs_error.h"
C
Christoph Hellwig 已提交
38
#include "xfs_trace.h"
L
Linus Torvalds 已提交
39

40 41 42 43 44 45

static int xfs_swap_extents(
	xfs_inode_t	*ip,	/* target inode */
	xfs_inode_t	*tip,	/* tmp inode */
	xfs_swapext_t	*sxp);

L
Linus Torvalds 已提交
46
/*
47
 * ioctl interface for swapext
L
Linus Torvalds 已提交
48 49 50
 */
int
xfs_swapext(
51
	xfs_swapext_t	*sxp)
L
Linus Torvalds 已提交
52
{
C
Christoph Hellwig 已提交
53
	xfs_inode_t     *ip, *tip;
54 55
	struct fd	f, tmp;
	int		error = 0;
L
Linus Torvalds 已提交
56 57

	/* Pull information for the target fd */
58 59
	f = fdget((int)sxp->sx_fdtarget);
	if (!f.file) {
L
Linus Torvalds 已提交
60
		error = XFS_ERROR(EINVAL);
61
		goto out;
L
Linus Torvalds 已提交
62 63
	}

64 65 66
	if (!(f.file->f_mode & FMODE_WRITE) ||
	    !(f.file->f_mode & FMODE_READ) ||
	    (f.file->f_flags & O_APPEND)) {
67 68 69 70
		error = XFS_ERROR(EBADF);
		goto out_put_file;
	}

71 72
	tmp = fdget((int)sxp->sx_fdtmp);
	if (!tmp.file) {
L
Linus Torvalds 已提交
73
		error = XFS_ERROR(EINVAL);
C
Christoph Hellwig 已提交
74
		goto out_put_file;
L
Linus Torvalds 已提交
75 76
	}

77 78 79
	if (!(tmp.file->f_mode & FMODE_WRITE) ||
	    !(tmp.file->f_mode & FMODE_READ) ||
	    (tmp.file->f_flags & O_APPEND)) {
80
		error = XFS_ERROR(EBADF);
81
		goto out_put_tmp_file;
82 83
	}

A
Al Viro 已提交
84 85
	if (IS_SWAPFILE(file_inode(f.file)) ||
	    IS_SWAPFILE(file_inode(tmp.file))) {
86
		error = XFS_ERROR(EINVAL);
87
		goto out_put_tmp_file;
88 89
	}

A
Al Viro 已提交
90 91
	ip = XFS_I(file_inode(f.file));
	tip = XFS_I(file_inode(tmp.file));
L
Linus Torvalds 已提交
92 93

	if (ip->i_mount != tip->i_mount) {
C
Christoph Hellwig 已提交
94
		error = XFS_ERROR(EINVAL);
95
		goto out_put_tmp_file;
L
Linus Torvalds 已提交
96 97 98
	}

	if (ip->i_ino == tip->i_ino) {
C
Christoph Hellwig 已提交
99
		error = XFS_ERROR(EINVAL);
100
		goto out_put_tmp_file;
L
Linus Torvalds 已提交
101 102
	}

C
Christoph Hellwig 已提交
103 104
	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
		error = XFS_ERROR(EIO);
105
		goto out_put_tmp_file;
L
Linus Torvalds 已提交
106 107
	}

108
	error = xfs_swap_extents(ip, tip, sxp);
109

110
 out_put_tmp_file:
111
	fdput(tmp);
C
Christoph Hellwig 已提交
112
 out_put_file:
113
	fdput(f);
C
Christoph Hellwig 已提交
114
 out:
115 116 117
	return error;
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
 * 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 &&
167 168
	    XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
			XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
169 170 171 172
		return EINVAL;

	/* Check target in extent form to max in temp */
	if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
173 174
	    XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
			XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
175 176
		return EINVAL;

177 178 179 180 181 182 183 184 185
	/*
	 * 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...
	 */
186 187
	if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
		if (XFS_IFORK_BOFF(ip) &&
188
		    XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
189 190 191 192 193
			return EINVAL;
		if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
		    XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
			return EINVAL;
	}
194

195
	/* Reciprocal target->temp btree format checks */
196 197
	if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
		if (XFS_IFORK_BOFF(tip) &&
198
		    XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
199 200 201 202 203
			return EINVAL;
		if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
		    XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
			return EINVAL;
	}
204 205 206 207

	return 0;
}

208
static int
209
xfs_swap_extents(
210 211
	xfs_inode_t	*ip,	/* target inode */
	xfs_inode_t	*tip,	/* tmp inode */
212 213
	xfs_swapext_t	*sxp)
{
214
	xfs_mount_t	*mp = ip->i_mount;
215 216 217
	xfs_trans_t	*tp;
	xfs_bstat_t	*sbp = &sxp->sx_stat;
	xfs_ifork_t	*tempifp, *ifp, *tifp;
218
	int		src_log_flags, target_log_flags;
219 220 221 222 223
	int		error = 0;
	int		aforkblks = 0;
	int		taforkblks = 0;
	__uint64_t	tmp;

224 225 226 227 228 229 230 231
	/*
	 * We have no way of updating owner information in the BMBT blocks for
	 * each inode on CRC enabled filesystems, so to avoid corrupting the
	 * this metadata we simply don't allow extent swaps to occur.
	 */
	if (xfs_sb_version_hascrc(&mp->m_sb))
		return XFS_ERROR(EINVAL);

232 233 234
	tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
	if (!tempifp) {
		error = XFS_ERROR(ENOMEM);
235
		goto out;
236 237
	}

238 239 240 241 242 243 244 245
	/*
	 * 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);
L
Linus Torvalds 已提交
246 247 248 249

	/* 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);
250
		goto out_unlock;
L
Linus Torvalds 已提交
251 252 253
	}

	/* Verify both files are either real-time or non-realtime */
254
	if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
L
Linus Torvalds 已提交
255
		error = XFS_ERROR(EINVAL);
256
		goto out_unlock;
L
Linus Torvalds 已提交
257 258
	}

259
	error = -filemap_write_and_wait(VFS_I(tip)->i_mapping);
D
Dave Chinner 已提交
260 261
	if (error)
		goto out_unlock;
262
	truncate_pagecache_range(VFS_I(tip), 0, -1);
L
Linus Torvalds 已提交
263 264

	/* Verify O_DIRECT for ftmp */
265
	if (VN_CACHED(VFS_I(tip)) != 0) {
L
Linus Torvalds 已提交
266
		error = XFS_ERROR(EINVAL);
267
		goto out_unlock;
L
Linus Torvalds 已提交
268 269 270
	}

	/* Verify all data are being swapped */
271 272 273
	if (sxp->sx_offset != 0 ||
	    sxp->sx_length != ip->i_d.di_size ||
	    sxp->sx_length != tip->i_d.di_size) {
L
Linus Torvalds 已提交
274
		error = XFS_ERROR(EFAULT);
275
		goto out_unlock;
L
Linus Torvalds 已提交
276 277
	}

278 279 280
	trace_xfs_swap_extent_before(ip, 0);
	trace_xfs_swap_extent_before(tip, 1);

281 282 283
	/* check inode formats now that data is flushed */
	error = xfs_swap_extents_check_format(ip, tip);
	if (error) {
284
		xfs_notice(mp,
285
		    "%s: inode 0x%llx format is incompatible for exchanging.",
286
				__func__, ip->i_ino);
287
		goto out_unlock;
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296
	}

	/*
	 * 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.
	 */
297 298 299 300
	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)) {
L
Linus Torvalds 已提交
301
		error = XFS_ERROR(EBUSY);
302
		goto out_unlock;
L
Linus Torvalds 已提交
303 304 305 306 307
	}

	/* 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
308
	 * vop_read (or write in the case of autogrow) they block on the iolock
L
Linus Torvalds 已提交
309 310
	 * until we have switched the extents.
	 */
311
	if (VN_MAPPED(VFS_I(ip))) {
L
Linus Torvalds 已提交
312
		error = XFS_ERROR(EBUSY);
313
		goto out_unlock;
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325
	}

	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.
	 */
D
Dave Chinner 已提交
326
	truncate_pagecache_range(VFS_I(ip), 0, -1);
L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334

	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);
335
		goto out;
L
Linus Torvalds 已提交
336
	}
337
	xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
338 339 340 341 342 343 344

	/*
	 * 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);
345 346
		if (error)
			goto out_trans_cancel;
L
Linus Torvalds 已提交
347 348 349 350 351
	}
	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);
352 353
		if (error)
			goto out_trans_cancel;
L
Linus Torvalds 已提交
354 355 356 357 358 359 360
	}

	/*
	 * Swap the data forks of the inodes
	 */
	ifp = &ip->i_df;
	tifp = &tip->i_df;
361 362 363
	*tempifp = *ifp;	/* struct copy */
	*ifp = *tifp;		/* struct copy */
	*tifp = *tempifp;	/* struct copy */
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

	/*
	 * 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;

380 381 382 383 384 385 386 387 388 389 390 391 392
	/*
	 * The extents in the source inode could still contain speculative
	 * preallocation beyond EOF (e.g. the file is open but not modified
	 * while defrag is in progress). In that case, we need to copy over the
	 * number of delalloc blocks the data fork in the source inode is
	 * tracking beyond EOF so that when the fork is truncated away when the
	 * temporary inode is unlinked we don't underrun the i_delayed_blks
	 * counter on that inode.
	 */
	ASSERT(tip->i_delayed_blks == 0);
	tip->i_delayed_blks = ip->i_delayed_blks;
	ip->i_delayed_blks = 0;

393 394
	src_log_flags = XFS_ILOG_CORE;
	switch (ip->i_d.di_format) {
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403
	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;
		}
404
		src_log_flags |= XFS_ILOG_DEXT;
L
Linus Torvalds 已提交
405 406
		break;
	case XFS_DINODE_FMT_BTREE:
407
		src_log_flags |= XFS_ILOG_DBROOT;
L
Linus Torvalds 已提交
408 409 410
		break;
	}

411 412
	target_log_flags = XFS_ILOG_CORE;
	switch (tip->i_d.di_format) {
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421
	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;
		}
422
		target_log_flags |= XFS_ILOG_DEXT;
L
Linus Torvalds 已提交
423 424
		break;
	case XFS_DINODE_FMT_BTREE:
425
		target_log_flags |= XFS_ILOG_DBROOT;
L
Linus Torvalds 已提交
426 427 428 429
		break;
	}


430 431
	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
	xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
L
Linus Torvalds 已提交
432

433 434
	xfs_trans_log_inode(tp, ip,  src_log_flags);
	xfs_trans_log_inode(tp, tip, target_log_flags);
L
Linus Torvalds 已提交
435 436 437 438 439

	/*
	 * If this is a synchronous mount, make sure that the
	 * transaction goes to disk before returning to the user.
	 */
440
	if (mp->m_flags & XFS_MOUNT_WSYNC)
L
Linus Torvalds 已提交
441 442
		xfs_trans_set_sync(tp);

443
	error = xfs_trans_commit(tp, 0);
L
Linus Torvalds 已提交
444

445 446
	trace_xfs_swap_extent_after(ip, 0);
	trace_xfs_swap_extent_after(tip, 1);
447 448
out:
	kmem_free(tempifp);
L
Linus Torvalds 已提交
449
	return error;
450

451 452 453 454 455
out_unlock:
	xfs_iunlock(ip,  XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
	xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
	goto out;

456 457 458
out_trans_cancel:
	xfs_trans_cancel(tp, 0);
	goto out_unlock;
L
Linus Torvalds 已提交
459
}