move_extent.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
 * Written by Takashi Sato <t-sato@yk.jp.nec.com>
 *            Akira Fujita <a-fujita@rs.jp.nec.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will 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.
 */

#include <linux/fs.h>
#include <linux/quotaops.h>
18
#include <linux/slab.h>
19 20
#include "ext4_jbd2.h"
#include "ext4.h"
21
#include "ext4_extents.h"
22

23 24 25 26 27 28 29 30 31 32 33 34
/**
 * get_ext_path - Find an extent path for designated logical block number.
 *
 * @inode:	an inode which is searched
 * @lblock:	logical block number to find an extent path
 * @path:	pointer to an extent path pointer (for output)
 *
 * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value
 * on failure.
 */
static inline int
get_ext_path(struct inode *inode, ext4_lblk_t lblock,
35
		struct ext4_ext_path **orig_path)
36 37
{
	int ret = 0;
38
	struct ext4_ext_path *path;
39

40
	path = ext4_ext_find_extent(inode, lblock, orig_path, EXT4_EX_NOCACHE);
41 42 43
	if (IS_ERR(path))
		ret = PTR_ERR(path);
	else if (path[ext_depth(inode)].p_ext == NULL)
44
		ret = -ENODATA;
45 46
	else
		*orig_path = path;
47

48 49
	return ret;
}
50 51

/**
52 53
 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
 *                                   of i_data_sem
54
 *
D
Dmitry Monakhov 已提交
55
 * Acquire write lock of i_data_sem of the two inodes
56
 */
57 58
void
ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
59
{
D
Dmitry Monakhov 已提交
60 61 62 63 64 65
	if (first < second) {
		down_write(&EXT4_I(first)->i_data_sem);
		down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
	} else {
		down_write(&EXT4_I(second)->i_data_sem);
		down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
66 67 68 69 70

	}
}

/**
71
 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
72 73 74
 *
 * @orig_inode:		original inode structure to be released its lock first
 * @donor_inode:	donor inode structure to be released its lock second
75
 * Release write lock of i_data_sem of two inodes (orig and donor).
76
 */
77 78 79
void
ext4_double_up_write_data_sem(struct inode *orig_inode,
			      struct inode *donor_inode)
80 81 82 83 84
{
	up_write(&EXT4_I(orig_inode)->i_data_sem);
	up_write(&EXT4_I(donor_inode)->i_data_sem);
}

85 86 87 88 89 90
/**
 * mext_check_coverage - Check that all extents in range has the same type
 *
 * @inode:		inode in question
 * @from:		block offset of inode
 * @count:		block count to be checked
91
 * @unwritten:		extents expected to be unwritten
92 93 94 95 96 97
 * @err:		pointer to save error value
 *
 * Return 1 if all extents in range has expected type, and zero otherwise.
 */
static int
mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
98
		    int unwritten, int *err)
99 100 101
{
	struct ext4_ext_path *path = NULL;
	struct ext4_extent *ext;
102
	int ret = 0;
103 104 105 106
	ext4_lblk_t last = from + count;
	while (from < last) {
		*err = get_ext_path(inode, from, &path);
		if (*err)
107
			goto out;
108
		ext = path[ext_depth(inode)].p_ext;
109
		if (unwritten != ext4_ext_is_unwritten(ext))
110
			goto out;
111 112 113
		from += ext4_ext_get_actual_len(ext);
		ext4_ext_drop_refs(path);
	}
114 115
	ret = 1;
out:
116 117
	ext4_ext_drop_refs(path);
	kfree(path);
118
	return ret;
119 120
}

121 122 123 124 125 126 127 128
/**
 * mext_replace_branches - Replace original extents with new extents
 *
 * @handle:		journal handle
 * @orig_inode:		original inode
 * @donor_inode:	donor inode
 * @from:		block offset of orig_inode
 * @count:		block count to be replaced
129
 * @err:		pointer to save return value
130 131 132 133 134 135 136 137 138 139
 *
 * Replace original inode extents and donor inode extents page by page.
 * We implement this replacement in the following three steps:
 * 1. Save the block information of original and donor inodes into
 *    dummy extents.
 * 2. Change the block information of original inode to point at the
 *    donor inode blocks.
 * 3. Change the block information of donor inode to point at the saved
 *    original inode blocks in the dummy extents.
 *
140
 * Return replaced block count.
141 142
 */

143 144 145 146 147 148 149 150 151 152 153 154
/**
 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
 *
 * @inode1:	the inode structure
 * @inode2:	the inode structure
 * @index:	page index
 * @page:	result page vector
 *
 * Grab two locked pages for inode's by inode order
 */
static int
mext_page_double_lock(struct inode *inode1, struct inode *inode2,
155
		      pgoff_t index1, pgoff_t index2, struct page *page[2])
156 157 158 159 160 161 162 163 164
{
	struct address_space *mapping[2];
	unsigned fl = AOP_FLAG_NOFS;

	BUG_ON(!inode1 || !inode2);
	if (inode1 < inode2) {
		mapping[0] = inode1->i_mapping;
		mapping[1] = inode2->i_mapping;
	} else {
165 166 167
		pgoff_t tmp = index1;
		index1 = index2;
		index2 = tmp;
168 169 170 171
		mapping[0] = inode2->i_mapping;
		mapping[1] = inode1->i_mapping;
	}

172
	page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
173 174 175
	if (!page[0])
		return -ENOMEM;

176
	page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
177 178 179 180 181
	if (!page[1]) {
		unlock_page(page[0]);
		page_cache_release(page[0]);
		return -ENOMEM;
	}
182 183 184 185 186 187 188
	/*
	 * grab_cache_page_write_begin() may not wait on page's writeback if
	 * BDI not demand that. But it is reasonable to be very conservative
	 * here and explicitly wait on page's writeback
	 */
	wait_on_page_writeback(page[0]);
	wait_on_page_writeback(page[1]);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	if (inode1 > inode2) {
		struct page *tmp;
		tmp = page[0];
		page[0] = page[1];
		page[1] = tmp;
	}
	return 0;
}

/* Force page buffers uptodate w/o dropping page's lock */
static int
mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
{
	struct inode *inode = page->mapping->host;
	sector_t block;
	struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
	unsigned int blocksize, block_start, block_end;
	int i, err,  nr = 0, partial = 0;
	BUG_ON(!PageLocked(page));
	BUG_ON(PageWriteback(page));

	if (PageUptodate(page))
		return 0;

	blocksize = 1 << inode->i_blkbits;
	if (!page_has_buffers(page))
		create_empty_buffers(page, blocksize, 0);

	head = page_buffers(page);
	block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
	for (bh = head, block_start = 0; bh != head || !block_start;
	     block++, block_start = block_end, bh = bh->b_this_page) {
		block_end = block_start + blocksize;
		if (block_end <= from || block_start >= to) {
			if (!buffer_uptodate(bh))
				partial = 1;
			continue;
		}
		if (buffer_uptodate(bh))
			continue;
		if (!buffer_mapped(bh)) {
			err = ext4_get_block(inode, block, bh, 0);
			if (err) {
				SetPageError(page);
				return err;
			}
			if (!buffer_mapped(bh)) {
				zero_user(page, block_start, blocksize);
237
				set_buffer_uptodate(bh);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
				continue;
			}
		}
		BUG_ON(nr >= MAX_BUF_PER_PAGE);
		arr[nr++] = bh;
	}
	/* No io required */
	if (!nr)
		goto out;

	for (i = 0; i < nr; i++) {
		bh = arr[i];
		if (!bh_uptodate_or_lock(bh)) {
			err = bh_submit_read(bh);
			if (err)
				return err;
		}
	}
out:
	if (!partial)
		SetPageUptodate(page);
	return 0;
}

262 263 264 265 266 267 268 269
/**
 * move_extent_per_page - Move extent data per page
 *
 * @o_filp:			file structure of original file
 * @donor_inode:		donor inode
 * @orig_page_offset:		page index on original file
 * @data_offset_in_page:	block index where data swapping starts
 * @block_len_in_page:		the number of blocks to be swapped
270
 * @unwritten:			orig extent is unwritten or not
271
 * @err:			pointer to save return value
272 273 274
 *
 * Save the data in original inode blocks and replace original inode extents
 * with donor inode extents by calling mext_replace_branches().
275 276
 * Finally, write out the saved data in new original inode blocks. Return
 * replaced block count.
277 278
 */
static int
279
move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
280 281 282
		     pgoff_t orig_page_offset, pgoff_t donor_page_offset,
		     int data_offset_in_page,
		     int block_len_in_page, int unwritten, int *err)
283
{
A
Al Viro 已提交
284
	struct inode *orig_inode = file_inode(o_filp);
285
	struct page *pagep[2] = {NULL, NULL};
286
	handle_t *handle;
287
	ext4_lblk_t orig_blk_offset, donor_blk_offset;
288 289
	unsigned long blocksize = orig_inode->i_sb->s_blocksize;
	unsigned int w_flags = 0;
290
	unsigned int tmp_data_size, data_size, replaced_size;
291
	int err2, jblocks, retries = 0;
292
	int replaced_count = 0;
293
	int from = data_offset_in_page << orig_inode->i_blkbits;
294 295 296 297 298 299
	int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;

	/*
	 * It needs twice the amount of ordinary journal buffers because
	 * inode and donor_inode may change each different metadata blocks.
	 */
300 301
again:
	*err = 0;
302
	jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
303
	handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
304
	if (IS_ERR(handle)) {
305 306
		*err = PTR_ERR(handle);
		return 0;
307 308 309 310 311 312 313 314
	}

	if (segment_eq(get_fs(), KERNEL_DS))
		w_flags |= AOP_FLAG_UNINTERRUPTIBLE;

	orig_blk_offset = orig_page_offset * blocks_per_page +
		data_offset_in_page;

315 316 317
	donor_blk_offset = donor_page_offset * blocks_per_page +
		data_offset_in_page;

318
	/* Calculate data_size */
319 320 321
	if ((orig_blk_offset + block_len_in_page - 1) ==
	    ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
		/* Replace the last block */
322
		tmp_data_size = orig_inode->i_size & (blocksize - 1);
323
		/*
324
		 * If data_size equal zero, it shows data_size is multiples of
325 326
		 * blocksize. So we set appropriate value.
		 */
327 328
		if (tmp_data_size == 0)
			tmp_data_size = blocksize;
329

330
		data_size = tmp_data_size +
331
			((block_len_in_page - 1) << orig_inode->i_blkbits);
332 333 334 335
	} else
		data_size = block_len_in_page << orig_inode->i_blkbits;

	replaced_size = data_size;
336

337
	*err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
338
				     donor_page_offset, pagep);
339
	if (unlikely(*err < 0))
340
		goto stop_journal;
341
	/*
342
	 * If orig extent was unwritten it can become initialized
343 344 345 346 347
	 * at any time after i_data_sem was dropped, in order to
	 * serialize with delalloc we have recheck extent while we
	 * hold page's lock, if it is still the case data copy is not
	 * necessary, just swap data blocks between orig and donor.
	 */
348
	if (unwritten) {
349
		ext4_double_down_write_data_sem(orig_inode, donor_inode);
350 351
		/* If any of extents in range became initialized we have to
		 * fallback to data copying */
352 353
		unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
						block_len_in_page, 1, err);
354 355
		if (*err)
			goto drop_data_sem;
356

357
		unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
358
						 block_len_in_page, 1, err);
359 360 361
		if (*err)
			goto drop_data_sem;

362
		if (!unwritten) {
363
			ext4_double_up_write_data_sem(orig_inode, donor_inode);
364 365 366 367 368 369 370 371 372
			goto data_copy;
		}
		if ((page_has_private(pagep[0]) &&
		     !try_to_release_page(pagep[0], 0)) ||
		    (page_has_private(pagep[1]) &&
		     !try_to_release_page(pagep[1], 0))) {
			*err = -EBUSY;
			goto drop_data_sem;
		}
373 374 375 376
		replaced_count = ext4_swap_extents(handle, orig_inode,
						   donor_inode, orig_blk_offset,
						   donor_blk_offset,
						   block_len_in_page, 1, err);
377
	drop_data_sem:
378
		ext4_double_up_write_data_sem(orig_inode, donor_inode);
379 380 381
		goto unlock_pages;
	}
data_copy:
382 383 384 385 386 387 388 389 390 391
	*err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
	if (*err)
		goto unlock_pages;

	/* At this point all buffers in range are uptodate, old mapping layout
	 * is no longer required, try to drop it now. */
	if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
	    (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
		*err = -EBUSY;
		goto unlock_pages;
392
	}
393
	ext4_double_down_write_data_sem(orig_inode, donor_inode);
394 395 396
	replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
					       orig_blk_offset, donor_blk_offset,
					   block_len_in_page, 1, err);
397
	ext4_double_up_write_data_sem(orig_inode, donor_inode);
398
	if (*err) {
399 400 401 402
		if (replaced_count) {
			block_len_in_page = replaced_count;
			replaced_size =
				block_len_in_page << orig_inode->i_blkbits;
403
		} else
404
			goto unlock_pages;
405
	}
406 407
	/* Perform all necessary steps similar write_begin()/write_end()
	 * but keeping in mind that i_size will not change */
408
	*err = __block_write_begin(pagep[0], from, replaced_size,
409 410 411
				   ext4_get_block);
	if (!*err)
		*err = block_commit_write(pagep[0], from, from + replaced_size);
412

413 414 415 416 417 418 419 420 421 422 423 424 425
	if (unlikely(*err < 0))
		goto repair_branches;

	/* Even in case of data=writeback it is reasonable to pin
	 * inode to transaction, to prevent unexpected data loss */
	*err = ext4_jbd2_file_inode(handle, orig_inode);

unlock_pages:
	unlock_page(pagep[0]);
	page_cache_release(pagep[0]);
	unlock_page(pagep[1]);
	page_cache_release(pagep[1]);
stop_journal:
426
	ext4_journal_stop(handle);
427 428 429 430 431
	/* Buffer was busy because probably is pinned to journal transaction,
	 * force transaction commit may help to free it. */
	if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
						      &retries))
		goto again;
432
	return replaced_count;
433 434 435 436 437 438 439

repair_branches:
	/*
	 * This should never ever happen!
	 * Extents are swapped already, but we are not able to copy data.
	 * Try to swap extents to it's original places
	 */
440
	ext4_double_down_write_data_sem(orig_inode, donor_inode);
441 442 443
	replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
					       orig_blk_offset, donor_blk_offset,
					   block_len_in_page, 0, &err2);
444
	ext4_double_up_write_data_sem(orig_inode, donor_inode);
445 446 447 448 449 450 451 452
	if (replaced_count != block_len_in_page) {
		EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
				       "Unable to copy data block,"
				       " data will be lost.");
		*err = -EIO;
	}
	replaced_count = 0;
	goto unlock_pages;
453 454 455
}

/**
456
 * mext_check_arguments - Check whether move extent can be done
457 458 459 460 461 462 463 464 465 466 467 468 469
 *
 * @orig_inode:		original inode
 * @donor_inode:	donor inode
 * @orig_start:		logical start offset in block for orig
 * @donor_start:	logical start offset in block for donor
 * @len:		the number of blocks to be moved
 *
 * Check the arguments of ext4_move_extents() whether the files can be
 * exchanged with each other.
 * Return 0 on success, or a negative error value on failure.
 */
static int
mext_check_arguments(struct inode *orig_inode,
470 471
		     struct inode *donor_inode, __u64 orig_start,
		     __u64 donor_start, __u64 *len)
472
{
473
	__u64 orig_eof, donor_eof;
474 475 476
	unsigned int blkbits = orig_inode->i_blkbits;
	unsigned int blocksize = 1 << blkbits;

477 478 479 480
	orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
	donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;


481 482 483 484 485 486 487
	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
		ext4_debug("ext4 move extent: suid or sgid is set"
			   " to donor file [ino:orig %lu, donor %lu]\n",
			   orig_inode->i_ino, donor_inode->i_ino);
		return -EINVAL;
	}

488 489 490
	if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
		return -EPERM;

491 492 493 494 495
	/* Ext4 move extent does not support swapfile */
	if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
		ext4_debug("ext4 move extent: The argument files should "
			"not be swapfile [ino:orig %lu, donor %lu]\n",
			orig_inode->i_ino, donor_inode->i_ino);
496
		return -EBUSY;
497 498 499
	}

	/* Ext4 move extent supports only extent based file */
500
	if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
501 502 503
		ext4_debug("ext4 move extent: orig file is not extents "
			"based file [ino:orig %lu]\n", orig_inode->i_ino);
		return -EOPNOTSUPP;
504
	} else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
505 506 507 508 509 510 511 512 513 514 515
		ext4_debug("ext4 move extent: donor file is not extents "
			"based file [ino:donor %lu]\n", donor_inode->i_ino);
		return -EOPNOTSUPP;
	}

	if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
		ext4_debug("ext4 move extent: File size is 0 byte\n");
		return -EINVAL;
	}

	/* Start offset should be same */
516 517
	if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
	    (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
518
		ext4_debug("ext4 move extent: orig and donor's start "
519
			"offset are not alligned [ino:orig %lu, donor %lu]\n",
520 521 522 523
			orig_inode->i_ino, donor_inode->i_ino);
		return -EINVAL;
	}

524
	if ((orig_start >= EXT_MAX_BLOCKS) ||
525
	    (donor_start >= EXT_MAX_BLOCKS) ||
526
	    (*len > EXT_MAX_BLOCKS) ||
527
	    (donor_start + *len >= EXT_MAX_BLOCKS) ||
528
	    (orig_start + *len >= EXT_MAX_BLOCKS))  {
529
		ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
530
			"[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
531 532 533
			orig_inode->i_ino, donor_inode->i_ino);
		return -EINVAL;
	}
534 535 536 537
	if (orig_eof < orig_start + *len - 1)
		*len = orig_eof - orig_start;
	if (donor_eof < donor_start + *len - 1)
		*len = donor_eof - donor_start;
538
	if (!*len) {
539
		ext4_debug("ext4 move extent: len should not be 0 "
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
			"[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
			donor_inode->i_ino);
		return -EINVAL;
	}

	return 0;
}

/**
 * ext4_move_extents - Exchange the specified range of a file
 *
 * @o_filp:		file structure of the original file
 * @d_filp:		file structure of the donor file
 * @orig_start:		start offset in block for orig
 * @donor_start:	start offset in block for donor
 * @len:		the number of blocks to be moved
 * @moved_len:		moved block length
 *
 * This function returns 0 and moved block length is set in moved_len
 * if succeed, otherwise returns error value.
 *
 * Note: ext4_move_extents() proceeds the following order.
 * 1:ext4_move_extents() calculates the last block number of moving extent
 *   function by the start block number (orig_start) and the number of blocks
 *   to be moved (len) specified as arguments.
 *   If the {orig, donor}_start points a hole, the extent's start offset
 *   pointed by ext_cur (current extent), holecheck_path, orig_path are set
 *   after hole behind.
 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
 *   or the ext_cur exceeds the block_end which is last logical block number.
 * 3:To get the length of continues area, call mext_next_extent()
 *   specified with the ext_cur (initial value is holecheck_path) re-cursive,
 *   until find un-continuous extent, the start logical block number exceeds
 *   the block_end or the extent points to the last extent.
 * 4:Exchange the original inode data with donor inode data
 *   from orig_page_offset to seq_end_page.
 *   The start indexes of data are specified as arguments.
 *   That of the original inode is orig_page_offset,
 *   and the donor inode is also orig_page_offset
 *   (To easily handle blocksize != pagesize case, the offset for the
 *   donor inode is block unit).
 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
 *   then returns to step 2.
 * 6:Release holecheck_path, orig_path and set the len to moved_len
 *   which shows the number of moved blocks.
 *   The moved_len is useful for the command to calculate the file offset
 *   for starting next move extent ioctl.
 * 7:Return 0 on success, or a negative error value on failure.
 */
int
590 591
ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
		  __u64 donor_blk, __u64 len, __u64 *moved_len)
592
{
A
Al Viro 已提交
593 594
	struct inode *orig_inode = file_inode(o_filp);
	struct inode *donor_inode = file_inode(d_filp);
595
	struct ext4_ext_path *path = NULL;
596
	int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
597 598 599
	ext4_lblk_t o_end, o_start = orig_blk;
	ext4_lblk_t d_start = donor_blk;
	int ret;
600

D
Dmitry Monakhov 已提交
601 602 603 604 605 606 607 608 609
	if (orig_inode->i_sb != donor_inode->i_sb) {
		ext4_debug("ext4 move extent: The argument files "
			"should be in same FS [ino:orig %lu, donor %lu]\n",
			orig_inode->i_ino, donor_inode->i_ino);
		return -EINVAL;
	}

	/* orig and donor should be different inodes */
	if (orig_inode == donor_inode) {
610
		ext4_debug("ext4 move extent: The argument files should not "
D
Dmitry Monakhov 已提交
611
			"be same inode [ino:orig %lu, donor %lu]\n",
612 613 614 615
			orig_inode->i_ino, donor_inode->i_ino);
		return -EINVAL;
	}

616 617 618 619 620 621 622
	/* Regular file check */
	if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
		ext4_debug("ext4 move extent: The argument files should be "
			"regular file [ino:orig %lu, donor %lu]\n",
			orig_inode->i_ino, donor_inode->i_ino);
		return -EINVAL;
	}
623 624 625 626 627 628
	/* TODO: This is non obvious task to swap blocks for inodes with full
	   jornaling enabled */
	if (ext4_should_journal_data(orig_inode) ||
	    ext4_should_journal_data(donor_inode)) {
		return -EINVAL;
	}
629
	/* Protect orig and donor inodes against a truncate */
630
	lock_two_nondirectories(orig_inode, donor_inode);
631

632 633 634 635 636 637
	/* Wait for all existing dio workers */
	ext4_inode_block_unlocked_dio(orig_inode);
	ext4_inode_block_unlocked_dio(donor_inode);
	inode_dio_wait(orig_inode);
	inode_dio_wait(donor_inode);

638
	/* Protect extent tree against block allocations via delalloc */
639
	ext4_double_down_write_data_sem(orig_inode, donor_inode);
640
	/* Check the filesystem environment whether move_extent can be done */
641 642
	ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
				    donor_blk, &len);
D
Dmitry Monakhov 已提交
643
	if (ret)
644
		goto out;
645
	o_end = o_start + len;
646

647 648 649 650 651 652
	while (o_start < o_end) {
		struct ext4_extent *ex;
		ext4_lblk_t cur_blk, next_blk;
		pgoff_t orig_page_index, donor_page_index;
		int offset_in_page;
		int unwritten, cur_len;
653

654 655
		ret = get_ext_path(orig_inode, o_start, &path);
		if (ret)
656
			goto out;
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
		ex = path[path->p_depth].p_ext;
		next_blk = ext4_ext_next_allocated_block(path);
		cur_blk = le32_to_cpu(ex->ee_block);
		cur_len = ext4_ext_get_actual_len(ex);
		/* Check hole before the start pos */
		if (cur_blk + cur_len - 1 < o_start) {
			if (next_blk == EXT_MAX_BLOCKS) {
				o_start = o_end;
				ret = -ENODATA;
				goto out;
			}
			d_start += next_blk - o_start;
			o_start = next_blk;
			goto repeat;
		/* Check hole after the start pos */
		} else if (cur_blk > o_start) {
			/* Skip hole */
			d_start += cur_blk - o_start;
			o_start = cur_blk;
			/* Extent inside requested range ?*/
			if (cur_blk >= o_end)
				goto out;
		} else { /* in_range(o_start, o_blk, o_len) */
			cur_len += cur_blk - o_start;
681
		}
682 683 684 685 686 687 688 689 690 691 692
		unwritten = ext4_ext_is_unwritten(ex);
		if (o_end - o_start < cur_len)
			cur_len = o_end - o_start;

		orig_page_index = o_start >> (PAGE_CACHE_SHIFT -
					       orig_inode->i_blkbits);
		donor_page_index = d_start >> (PAGE_CACHE_SHIFT -
					       donor_inode->i_blkbits);
		offset_in_page = o_start % blocks_per_page;
		if (cur_len > blocks_per_page- offset_in_page)
			cur_len = blocks_per_page - offset_in_page;
693 694 695 696 697 698 699
		/*
		 * Up semaphore to avoid following problems:
		 * a. transaction deadlock among ext4_journal_start,
		 *    ->write_begin via pagefault, and jbd2_journal_commit
		 * b. racing with ->readpage, ->write_begin, and ext4_get_block
		 *    in move_extent_per_page
		 */
700
		ext4_double_up_write_data_sem(orig_inode, donor_inode);
701 702 703 704 705
		/* Swap original branches with new branches */
		move_extent_per_page(o_filp, donor_inode,
				     orig_page_index, donor_page_index,
				     offset_in_page, cur_len,
				     unwritten, &ret);
706
		ext4_double_down_write_data_sem(orig_inode, donor_inode);
D
Dmitry Monakhov 已提交
707
		if (ret < 0)
708
			break;
709 710 711
		o_start += cur_len;
		d_start += cur_len;
	repeat:
712 713 714
		ext4_ext_drop_refs(path);
		kfree(path);
		path = NULL;
715
	}
716 717 718 719
	*moved_len = o_start - orig_blk;
	if (*moved_len > len)
		*moved_len = len;

720
out:
721 722 723 724 725
	if (*moved_len) {
		ext4_discard_preallocations(orig_inode);
		ext4_discard_preallocations(donor_inode);
	}

726 727
	ext4_ext_drop_refs(path);
	kfree(path);
728
	ext4_double_up_write_data_sem(orig_inode, donor_inode);
729 730
	ext4_inode_resume_unlocked_dio(orig_inode);
	ext4_inode_resume_unlocked_dio(donor_inode);
731
	unlock_two_nondirectories(orig_inode, donor_inode);
732

D
Dmitry Monakhov 已提交
733
	return ret;
734
}