xfs_dfrag.c 8.9 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
#include "xfs_types.h"
21
#include "xfs_bit.h"
L
Linus Torvalds 已提交
22
#include "xfs_log.h"
23
#include "xfs_inum.h"
L
Linus Torvalds 已提交
24 25
#include "xfs_trans.h"
#include "xfs_sb.h"
26
#include "xfs_ag.h"
L
Linus Torvalds 已提交
27 28 29 30
#include "xfs_dir2.h"
#include "xfs_dmapi.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
31
#include "xfs_alloc_btree.h"
L
Linus Torvalds 已提交
32 33
#include "xfs_ialloc_btree.h"
#include "xfs_dir2_sf.h"
34
#include "xfs_attr_sf.h"
L
Linus Torvalds 已提交
35 36
#include "xfs_dinode.h"
#include "xfs_inode.h"
37
#include "xfs_inode_item.h"
L
Linus Torvalds 已提交
38
#include "xfs_bmap.h"
39
#include "xfs_btree.h"
L
Linus Torvalds 已提交
40 41 42 43 44
#include "xfs_ialloc.h"
#include "xfs_itable.h"
#include "xfs_dfrag.h"
#include "xfs_error.h"
#include "xfs_rw.h"
45
#include "xfs_vnodeops.h"
L
Linus Torvalds 已提交
46 47 48 49 50 51

/*
 * Syssgi interface for swapext
 */
int
xfs_swapext(
52
	xfs_swapext_t	*sxp)
L
Linus Torvalds 已提交
53
{
C
Christoph Hellwig 已提交
54 55
	xfs_inode_t     *ip, *tip;
	struct file	*file, *target_file;
L
Linus Torvalds 已提交
56 57 58
	int		error = 0;

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

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

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

76 77 78 79 80 81
	if (!(target_file->f_mode & FMODE_WRITE) ||
	    (target_file->f_flags & O_APPEND)) {
		error = XFS_ERROR(EBADF);
		goto out_put_target_file;
	}

82 83 84 85 86 87
	if (IS_SWAPFILE(file->f_path.dentry->d_inode) ||
	    IS_SWAPFILE(target_file->f_path.dentry->d_inode)) {
		error = XFS_ERROR(EINVAL);
		goto out_put_target_file;
	}

C
Christoph Hellwig 已提交
88 89
	ip = XFS_I(file->f_path.dentry->d_inode);
	tip = XFS_I(target_file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
90 91

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

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

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

106
	error = xfs_swap_extents(ip, tip, sxp);
107

C
Christoph Hellwig 已提交
108 109 110 111 112
 out_put_target_file:
	fput(target_file);
 out_put_file:
	fput(file);
 out:
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	return error;
}

int
xfs_swap_extents(
	xfs_inode_t	*ip,
	xfs_inode_t	*tip,
	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);
137
		goto out;
138 139 140
	}

	sbp = &sxp->sx_stat;
L
Linus Torvalds 已提交
141

142 143 144 145 146 147 148 149
	/*
	 * 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 已提交
150 151 152 153

	/* 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);
154
		goto out_unlock;
L
Linus Torvalds 已提交
155 156 157
	}

	/* Verify both files are either real-time or non-realtime */
158
	if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
L
Linus Torvalds 已提交
159
		error = XFS_ERROR(EINVAL);
160
		goto out_unlock;
L
Linus Torvalds 已提交
161 162 163 164 165 166
	}

	/* 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) {
		error = XFS_ERROR(EINVAL);
167
		goto out_unlock;
L
Linus Torvalds 已提交
168 169
	}

170
	if (VN_CACHED(VFS_I(tip)) != 0) {
C
Christoph Hellwig 已提交
171
		xfs_inval_cached_trace(tip, 0, -1, 0, -1);
172 173
		error = xfs_flushinval_pages(tip, 0, -1,
				FI_REMAPF_LOCKED);
174
		if (error)
175
			goto out_unlock;
176
	}
L
Linus Torvalds 已提交
177 178

	/* Verify O_DIRECT for ftmp */
179
	if (VN_CACHED(VFS_I(tip)) != 0) {
L
Linus Torvalds 已提交
180
		error = XFS_ERROR(EINVAL);
181
		goto out_unlock;
L
Linus Torvalds 已提交
182 183 184
	}

	/* Verify all data are being swapped */
185 186 187
	if (sxp->sx_offset != 0 ||
	    sxp->sx_length != ip->i_d.di_size ||
	    sxp->sx_length != tip->i_d.di_size) {
L
Linus Torvalds 已提交
188
		error = XFS_ERROR(EFAULT);
189
		goto out_unlock;
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198
	}

	/*
	 * If the target has extended attributes, the tmp file
	 * must also in order to ensure the correct data fork
	 * format.
	 */
	if ( XFS_IFORK_Q(ip) != XFS_IFORK_Q(tip) ) {
		error = XFS_ERROR(EINVAL);
199
		goto out_unlock;
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208
	}

	/*
	 * 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.
	 */
209 210 211 212
	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 已提交
213
		error = XFS_ERROR(EBUSY);
214
		goto out_unlock;
L
Linus Torvalds 已提交
215 216 217 218 219
	}

	/* 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
220
	 * vop_read (or write in the case of autogrow) they block on the iolock
L
Linus Torvalds 已提交
221 222
	 * until we have switched the extents.
	 */
223
	if (VN_MAPPED(VFS_I(ip))) {
L
Linus Torvalds 已提交
224
		error = XFS_ERROR(EBUSY);
225
		goto out_unlock;
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
	}

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

239
	xfs_tosspages(ip, 0, -1, FI_REMAPF);
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247

	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);
248
		goto out;
L
Linus Torvalds 已提交
249
	}
250
	xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
251 252 253 254 255 256 257

	/*
	 * 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);
258 259
		if (error)
			goto out_trans_cancel;
L
Linus Torvalds 已提交
260 261 262 263 264
	}
	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);
265 266
		if (error)
			goto out_trans_cancel;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273
	}

	/*
	 * Swap the data forks of the inodes
	 */
	ifp = &ip->i_df;
	tifp = &tip->i_df;
274 275 276
	*tempifp = *ifp;	/* struct copy */
	*ifp = *tifp;		/* struct copy */
	*tifp = *tempifp;	/* struct copy */
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

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


332
	IHOLD(ip);
333
	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
334 335

	IHOLD(tip);
336
	xfs_trans_ijoin(tp, tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344

	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.
	 */
345
	if (mp->m_flags & XFS_MOUNT_WSYNC)
L
Linus Torvalds 已提交
346 347
		xfs_trans_set_sync(tp);

348
	error = xfs_trans_commit(tp, XFS_TRANS_SWAPEXT);
L
Linus Torvalds 已提交
349

350 351
out:
	kmem_free(tempifp);
L
Linus Torvalds 已提交
352
	return error;
353

354 355 356 357 358
out_unlock:
	xfs_iunlock(ip,  XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
	xfs_iunlock(tip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
	goto out;

359 360 361
out_trans_cancel:
	xfs_trans_cancel(tp, 0);
	goto out_unlock;
L
Linus Torvalds 已提交
362
}