Linux Audio

Check our new training course

Loading...
v3.5.6
   1/*
   2 * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
   3 * Written by Takashi Sato <t-sato@yk.jp.nec.com>
   4 *            Akira Fujita <a-fujita@rs.jp.nec.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of version 2.1 of the GNU Lesser General Public License
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/fs.h>
  17#include <linux/quotaops.h>
  18#include <linux/slab.h>
  19#include "ext4_jbd2.h"
  20#include "ext4.h"
 
  21
  22/**
  23 * get_ext_path - Find an extent path for designated logical block number.
  24 *
  25 * @inode:	an inode which is searched
  26 * @lblock:	logical block number to find an extent path
  27 * @path:	pointer to an extent path pointer (for output)
  28 *
  29 * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value
  30 * on failure.
  31 */
  32static inline int
  33get_ext_path(struct inode *inode, ext4_lblk_t lblock,
  34		struct ext4_ext_path **path)
  35{
  36	int ret = 0;
 
  37
  38	*path = ext4_ext_find_extent(inode, lblock, *path);
  39	if (IS_ERR(*path)) {
  40		ret = PTR_ERR(*path);
  41		*path = NULL;
  42	} else if ((*path)[ext_depth(inode)].p_ext == NULL)
  43		ret = -ENODATA;
 
 
  44
  45	return ret;
  46}
  47
  48/**
  49 * copy_extent_status - Copy the extent's initialization status
  50 *
  51 * @src:	an extent for getting initialize status
  52 * @dest:	an extent to be set the status
  53 */
  54static void
  55copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
  56{
  57	if (ext4_ext_is_uninitialized(src))
  58		ext4_ext_mark_uninitialized(dest);
  59	else
  60		dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
  61}
  62
  63/**
  64 * mext_next_extent - Search for the next extent and set it to "extent"
  65 *
  66 * @inode:	inode which is searched
  67 * @path:	this will obtain data for the next extent
  68 * @extent:	pointer to the next extent we have just gotten
  69 *
  70 * Search the next extent in the array of ext4_ext_path structure (@path)
  71 * and set it to ext4_extent structure (@extent). In addition, the member of
  72 * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
  73 * ext4_ext_path structure refers to the last extent, or a negative error
  74 * value on failure.
  75 */
  76static int
  77mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
  78		      struct ext4_extent **extent)
  79{
  80	struct ext4_extent_header *eh;
  81	int ppos, leaf_ppos = path->p_depth;
  82
  83	ppos = leaf_ppos;
  84	if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
  85		/* leaf block */
  86		*extent = ++path[ppos].p_ext;
  87		path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  88		return 0;
  89	}
  90
  91	while (--ppos >= 0) {
  92		if (EXT_LAST_INDEX(path[ppos].p_hdr) >
  93		    path[ppos].p_idx) {
  94			int cur_ppos = ppos;
  95
  96			/* index block */
  97			path[ppos].p_idx++;
  98			path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
  99			if (path[ppos+1].p_bh)
 100				brelse(path[ppos+1].p_bh);
 101			path[ppos+1].p_bh =
 102				sb_bread(inode->i_sb, path[ppos].p_block);
 103			if (!path[ppos+1].p_bh)
 104				return -EIO;
 105			path[ppos+1].p_hdr =
 106				ext_block_hdr(path[ppos+1].p_bh);
 107
 108			/* Halfway index block */
 109			while (++cur_ppos < leaf_ppos) {
 110				path[cur_ppos].p_idx =
 111					EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
 112				path[cur_ppos].p_block =
 113					ext4_idx_pblock(path[cur_ppos].p_idx);
 114				if (path[cur_ppos+1].p_bh)
 115					brelse(path[cur_ppos+1].p_bh);
 116				path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
 117					path[cur_ppos].p_block);
 118				if (!path[cur_ppos+1].p_bh)
 119					return -EIO;
 120				path[cur_ppos+1].p_hdr =
 121					ext_block_hdr(path[cur_ppos+1].p_bh);
 122			}
 123
 124			path[leaf_ppos].p_ext = *extent = NULL;
 125
 126			eh = path[leaf_ppos].p_hdr;
 127			if (le16_to_cpu(eh->eh_entries) == 0)
 128				/* empty leaf is found */
 129				return -ENODATA;
 130
 131			/* leaf block */
 132			path[leaf_ppos].p_ext = *extent =
 133				EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
 134			path[leaf_ppos].p_block =
 135					ext4_ext_pblock(path[leaf_ppos].p_ext);
 136			return 0;
 137		}
 138	}
 139	/* We found the last extent */
 140	return 1;
 141}
 142
 143/**
 144 * mext_check_null_inode - NULL check for two inodes
 
 145 *
 146 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
 147 */
 148static int
 149mext_check_null_inode(struct inode *inode1, struct inode *inode2,
 150		      const char *function, unsigned int line)
 151{
 152	int ret = 0;
 153
 154	if (inode1 == NULL) {
 155		__ext4_error(inode2->i_sb, function, line,
 156			"Both inodes should not be NULL: "
 157			"inode1 NULL inode2 %lu", inode2->i_ino);
 158		ret = -EIO;
 159	} else if (inode2 == NULL) {
 160		__ext4_error(inode1->i_sb, function, line,
 161			"Both inodes should not be NULL: "
 162			"inode1 %lu inode2 NULL", inode1->i_ino);
 163		ret = -EIO;
 164	}
 165	return ret;
 166}
 167
 168/**
 169 * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem
 170 *
 171 * @orig_inode:		original inode structure
 172 * @donor_inode:	donor inode structure
 173 * Acquire write lock of i_data_sem of the two inodes (orig and donor) by
 174 * i_ino order.
 175 */
 176static void
 177double_down_write_data_sem(struct inode *orig_inode, struct inode *donor_inode)
 178{
 179	struct inode *first = orig_inode, *second = donor_inode;
 180
 181	/*
 182	 * Use the inode number to provide the stable locking order instead
 183	 * of its address, because the C language doesn't guarantee you can
 184	 * compare pointers that don't come from the same array.
 185	 */
 186	if (donor_inode->i_ino < orig_inode->i_ino) {
 187		first = donor_inode;
 188		second = orig_inode;
 189	}
 190
 191	down_write(&EXT4_I(first)->i_data_sem);
 192	down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
 193}
 194
 195/**
 196 * double_up_write_data_sem - Release two inodes' write lock of i_data_sem
 197 *
 198 * @orig_inode:		original inode structure to be released its lock first
 199 * @donor_inode:	donor inode structure to be released its lock second
 200 * Release write lock of i_data_sem of two inodes (orig and donor).
 201 */
 202static void
 203double_up_write_data_sem(struct inode *orig_inode, struct inode *donor_inode)
 
 204{
 205	up_write(&EXT4_I(orig_inode)->i_data_sem);
 206	up_write(&EXT4_I(donor_inode)->i_data_sem);
 207}
 208
 209/**
 210 * mext_insert_across_blocks - Insert extents across leaf block
 211 *
 212 * @handle:		journal handle
 213 * @orig_inode:		original inode
 214 * @o_start:		first original extent to be changed
 215 * @o_end:		last original extent to be changed
 216 * @start_ext:		first new extent to be inserted
 217 * @new_ext:		middle of new extent to be inserted
 218 * @end_ext:		last new extent to be inserted
 219 *
 220 * Allocate a new leaf block and insert extents into it. Return 0 on success,
 221 * or a negative error value on failure.
 222 */
 223static int
 224mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
 225		struct ext4_extent *o_start, struct ext4_extent *o_end,
 226		struct ext4_extent *start_ext, struct ext4_extent *new_ext,
 227		struct ext4_extent *end_ext)
 228{
 229	struct ext4_ext_path *orig_path = NULL;
 230	ext4_lblk_t eblock = 0;
 231	int new_flag = 0;
 232	int end_flag = 0;
 233	int err = 0;
 234
 235	if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
 236		if (o_start == o_end) {
 237
 238			/*       start_ext   new_ext    end_ext
 239			 * donor |---------|-----------|--------|
 240			 * orig  |------------------------------|
 241			 */
 242			end_flag = 1;
 243		} else {
 244
 245			/*       start_ext   new_ext   end_ext
 246			 * donor |---------|----------|---------|
 247			 * orig  |---------------|--------------|
 248			 */
 249			o_end->ee_block = end_ext->ee_block;
 250			o_end->ee_len = end_ext->ee_len;
 251			ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
 252		}
 253
 254		o_start->ee_len = start_ext->ee_len;
 255		eblock = le32_to_cpu(start_ext->ee_block);
 256		new_flag = 1;
 257
 258	} else if (start_ext->ee_len && new_ext->ee_len &&
 259		   !end_ext->ee_len && o_start == o_end) {
 260
 261		/*	 start_ext	new_ext
 262		 * donor |--------------|---------------|
 263		 * orig  |------------------------------|
 264		 */
 265		o_start->ee_len = start_ext->ee_len;
 266		eblock = le32_to_cpu(start_ext->ee_block);
 267		new_flag = 1;
 268
 269	} else if (!start_ext->ee_len && new_ext->ee_len &&
 270		   end_ext->ee_len && o_start == o_end) {
 271
 272		/*	  new_ext	end_ext
 273		 * donor |--------------|---------------|
 274		 * orig  |------------------------------|
 275		 */
 276		o_end->ee_block = end_ext->ee_block;
 277		o_end->ee_len = end_ext->ee_len;
 278		ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
 279
 280		/*
 281		 * Set 0 to the extent block if new_ext was
 282		 * the first block.
 283		 */
 284		if (new_ext->ee_block)
 285			eblock = le32_to_cpu(new_ext->ee_block);
 286
 287		new_flag = 1;
 288	} else {
 289		ext4_debug("ext4 move extent: Unexpected insert case\n");
 290		return -EIO;
 291	}
 292
 293	if (new_flag) {
 294		err = get_ext_path(orig_inode, eblock, &orig_path);
 295		if (err)
 296			goto out;
 297
 298		if (ext4_ext_insert_extent(handle, orig_inode,
 299					orig_path, new_ext, 0))
 300			goto out;
 301	}
 302
 303	if (end_flag) {
 304		err = get_ext_path(orig_inode,
 305				le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
 306		if (err)
 307			goto out;
 308
 309		if (ext4_ext_insert_extent(handle, orig_inode,
 310					   orig_path, end_ext, 0))
 311			goto out;
 312	}
 313out:
 314	if (orig_path) {
 315		ext4_ext_drop_refs(orig_path);
 316		kfree(orig_path);
 317	}
 318
 319	return err;
 320
 321}
 322
 323/**
 324 * mext_insert_inside_block - Insert new extent to the extent block
 325 *
 326 * @o_start:		first original extent to be moved
 327 * @o_end:		last original extent to be moved
 328 * @start_ext:		first new extent to be inserted
 329 * @new_ext:		middle of new extent to be inserted
 330 * @end_ext:		last new extent to be inserted
 331 * @eh:			extent header of target leaf block
 332 * @range_to_move:	used to decide how to insert extent
 333 *
 334 * Insert extents into the leaf block. The extent (@o_start) is overwritten
 335 * by inserted extents.
 336 */
 337static void
 338mext_insert_inside_block(struct ext4_extent *o_start,
 339			      struct ext4_extent *o_end,
 340			      struct ext4_extent *start_ext,
 341			      struct ext4_extent *new_ext,
 342			      struct ext4_extent *end_ext,
 343			      struct ext4_extent_header *eh,
 344			      int range_to_move)
 345{
 346	int i = 0;
 347	unsigned long len;
 348
 349	/* Move the existing extents */
 350	if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
 351		len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
 352			(unsigned long)(o_end + 1);
 353		memmove(o_end + 1 + range_to_move, o_end + 1, len);
 354	}
 355
 356	/* Insert start entry */
 357	if (start_ext->ee_len)
 358		o_start[i++].ee_len = start_ext->ee_len;
 359
 360	/* Insert new entry */
 361	if (new_ext->ee_len) {
 362		o_start[i] = *new_ext;
 363		ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
 364	}
 365
 366	/* Insert end entry */
 367	if (end_ext->ee_len)
 368		o_start[i] = *end_ext;
 369
 370	/* Increment the total entries counter on the extent block */
 371	le16_add_cpu(&eh->eh_entries, range_to_move);
 372}
 373
 374/**
 375 * mext_insert_extents - Insert new extent
 376 *
 377 * @handle:	journal handle
 378 * @orig_inode:	original inode
 379 * @orig_path:	path indicates first extent to be changed
 380 * @o_start:	first original extent to be changed
 381 * @o_end:	last original extent to be changed
 382 * @start_ext:	first new extent to be inserted
 383 * @new_ext:	middle of new extent to be inserted
 384 * @end_ext:	last new extent to be inserted
 385 *
 386 * Call the function to insert extents. If we cannot add more extents into
 387 * the leaf block, we call mext_insert_across_blocks() to create a
 388 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
 389 * on success, or a negative error value on failure.
 390 */
 391static int
 392mext_insert_extents(handle_t *handle, struct inode *orig_inode,
 393			 struct ext4_ext_path *orig_path,
 394			 struct ext4_extent *o_start,
 395			 struct ext4_extent *o_end,
 396			 struct ext4_extent *start_ext,
 397			 struct ext4_extent *new_ext,
 398			 struct ext4_extent *end_ext)
 399{
 400	struct  ext4_extent_header *eh;
 401	unsigned long need_slots, slots_range;
 402	int	range_to_move, depth, ret;
 403
 404	/*
 405	 * The extents need to be inserted
 406	 * start_extent + new_extent + end_extent.
 407	 */
 408	need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
 409		(new_ext->ee_len ? 1 : 0);
 410
 411	/* The number of slots between start and end */
 412	slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
 413		/ sizeof(struct ext4_extent);
 414
 415	/* Range to move the end of extent */
 416	range_to_move = need_slots - slots_range;
 417	depth = orig_path->p_depth;
 418	orig_path += depth;
 419	eh = orig_path->p_hdr;
 420
 421	if (depth) {
 422		/* Register to journal */
 423		ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
 424		if (ret)
 425			return ret;
 426	}
 427
 428	/* Expansion */
 429	if (range_to_move > 0 &&
 430		(range_to_move > le16_to_cpu(eh->eh_max)
 431			- le16_to_cpu(eh->eh_entries))) {
 432
 433		ret = mext_insert_across_blocks(handle, orig_inode, o_start,
 434					o_end, start_ext, new_ext, end_ext);
 435		if (ret < 0)
 436			return ret;
 437	} else
 438		mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
 439						end_ext, eh, range_to_move);
 440
 441	if (depth) {
 442		ret = ext4_handle_dirty_metadata(handle, orig_inode,
 443						 orig_path->p_bh);
 444		if (ret)
 445			return ret;
 446	} else {
 447		ret = ext4_mark_inode_dirty(handle, orig_inode);
 448		if (ret < 0)
 449			return ret;
 450	}
 451
 452	return 0;
 453}
 454
 455/**
 456 * mext_leaf_block - Move one leaf extent block into the inode.
 457 *
 458 * @handle:		journal handle
 459 * @orig_inode:		original inode
 460 * @orig_path:		path indicates first extent to be changed
 461 * @dext:		donor extent
 462 * @from:		start offset on the target file
 463 *
 464 * In order to insert extents into the leaf block, we must divide the extent
 465 * in the leaf block into three extents. The one is located to be inserted
 466 * extents, and the others are located around it.
 467 *
 468 * Therefore, this function creates structures to save extents of the leaf
 469 * block, and inserts extents by calling mext_insert_extents() with
 470 * created extents. Return 0 on success, or a negative error value on failure.
 471 */
 472static int
 473mext_leaf_block(handle_t *handle, struct inode *orig_inode,
 474		     struct ext4_ext_path *orig_path, struct ext4_extent *dext,
 475		     ext4_lblk_t *from)
 476{
 477	struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
 478	struct ext4_extent new_ext, start_ext, end_ext;
 479	ext4_lblk_t new_ext_end;
 480	int oext_alen, new_ext_alen, end_ext_alen;
 481	int depth = ext_depth(orig_inode);
 482	int ret;
 483
 484	start_ext.ee_block = end_ext.ee_block = 0;
 485	o_start = o_end = oext = orig_path[depth].p_ext;
 486	oext_alen = ext4_ext_get_actual_len(oext);
 487	start_ext.ee_len = end_ext.ee_len = 0;
 488
 489	new_ext.ee_block = cpu_to_le32(*from);
 490	ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
 491	new_ext.ee_len = dext->ee_len;
 492	new_ext_alen = ext4_ext_get_actual_len(&new_ext);
 493	new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
 494
 495	/*
 496	 * Case: original extent is first
 497	 * oext      |--------|
 498	 * new_ext      |--|
 499	 * start_ext |--|
 500	 */
 501	if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
 502		le32_to_cpu(new_ext.ee_block) <
 503		le32_to_cpu(oext->ee_block) + oext_alen) {
 504		start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
 505					       le32_to_cpu(oext->ee_block));
 506		start_ext.ee_block = oext->ee_block;
 507		copy_extent_status(oext, &start_ext);
 508	} else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
 509		prev_ext = oext - 1;
 510		/*
 511		 * We can merge new_ext into previous extent,
 512		 * if these are contiguous and same extent type.
 513		 */
 514		if (ext4_can_extents_be_merged(orig_inode, prev_ext,
 515					       &new_ext)) {
 516			o_start = prev_ext;
 517			start_ext.ee_len = cpu_to_le16(
 518				ext4_ext_get_actual_len(prev_ext) +
 519				new_ext_alen);
 520			start_ext.ee_block = oext->ee_block;
 521			copy_extent_status(prev_ext, &start_ext);
 522			new_ext.ee_len = 0;
 523		}
 524	}
 525
 526	/*
 527	 * Case: new_ext_end must be less than oext
 528	 * oext      |-----------|
 529	 * new_ext       |-------|
 530	 */
 531	if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
 532		EXT4_ERROR_INODE(orig_inode,
 533			"new_ext_end(%u) should be less than or equal to "
 534			"oext->ee_block(%u) + oext_alen(%d) - 1",
 535			new_ext_end, le32_to_cpu(oext->ee_block),
 536			oext_alen);
 537		ret = -EIO;
 538		goto out;
 539	}
 540
 541	/*
 542	 * Case: new_ext is smaller than original extent
 543	 * oext    |---------------|
 544	 * new_ext |-----------|
 545	 * end_ext             |---|
 546	 */
 547	if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
 548		new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
 549		end_ext.ee_len =
 550			cpu_to_le16(le32_to_cpu(oext->ee_block) +
 551			oext_alen - 1 - new_ext_end);
 552		copy_extent_status(oext, &end_ext);
 553		end_ext_alen = ext4_ext_get_actual_len(&end_ext);
 554		ext4_ext_store_pblock(&end_ext,
 555			(ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
 556		end_ext.ee_block =
 557			cpu_to_le32(le32_to_cpu(o_end->ee_block) +
 558			oext_alen - end_ext_alen);
 559	}
 560
 561	ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
 562				o_end, &start_ext, &new_ext, &end_ext);
 563out:
 564	return ret;
 565}
 566
 567/**
 568 * mext_calc_swap_extents - Calculate extents for extent swapping.
 569 *
 570 * @tmp_dext:		the extent that will belong to the original inode
 571 * @tmp_oext:		the extent that will belong to the donor inode
 572 * @orig_off:		block offset of original inode
 573 * @donor_off:		block offset of donor inode
 574 * @max_count:		the maximum length of extents
 575 *
 576 * Return 0 on success, or a negative error value on failure.
 577 */
 578static int
 579mext_calc_swap_extents(struct ext4_extent *tmp_dext,
 580			      struct ext4_extent *tmp_oext,
 581			      ext4_lblk_t orig_off, ext4_lblk_t donor_off,
 582			      ext4_lblk_t max_count)
 583{
 584	ext4_lblk_t diff, orig_diff;
 585	struct ext4_extent dext_old, oext_old;
 586
 587	BUG_ON(orig_off != donor_off);
 588
 589	/* original and donor extents have to cover the same block offset */
 590	if (orig_off < le32_to_cpu(tmp_oext->ee_block) ||
 591	    le32_to_cpu(tmp_oext->ee_block) +
 592			ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off)
 593		return -ENODATA;
 594
 595	if (orig_off < le32_to_cpu(tmp_dext->ee_block) ||
 596	    le32_to_cpu(tmp_dext->ee_block) +
 597			ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off)
 598		return -ENODATA;
 599
 600	dext_old = *tmp_dext;
 601	oext_old = *tmp_oext;
 602
 603	/* When tmp_dext is too large, pick up the target range. */
 604	diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
 605
 606	ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
 607	tmp_dext->ee_block =
 608			cpu_to_le32(le32_to_cpu(tmp_dext->ee_block) + diff);
 609	tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_dext->ee_len) - diff);
 610
 611	if (max_count < ext4_ext_get_actual_len(tmp_dext))
 612		tmp_dext->ee_len = cpu_to_le16(max_count);
 613
 614	orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
 615	ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
 616
 617	/* Adjust extent length if donor extent is larger than orig */
 618	if (ext4_ext_get_actual_len(tmp_dext) >
 619	    ext4_ext_get_actual_len(tmp_oext) - orig_diff)
 620		tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
 621						orig_diff);
 622
 623	tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
 624
 625	copy_extent_status(&oext_old, tmp_dext);
 626	copy_extent_status(&dext_old, tmp_oext);
 627
 628	return 0;
 629}
 630
 631/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 632 * mext_replace_branches - Replace original extents with new extents
 633 *
 634 * @handle:		journal handle
 635 * @orig_inode:		original inode
 636 * @donor_inode:	donor inode
 637 * @from:		block offset of orig_inode
 638 * @count:		block count to be replaced
 639 * @err:		pointer to save return value
 640 *
 641 * Replace original inode extents and donor inode extents page by page.
 642 * We implement this replacement in the following three steps:
 643 * 1. Save the block information of original and donor inodes into
 644 *    dummy extents.
 645 * 2. Change the block information of original inode to point at the
 646 *    donor inode blocks.
 647 * 3. Change the block information of donor inode to point at the saved
 648 *    original inode blocks in the dummy extents.
 649 *
 650 * Return replaced block count.
 651 */
 652static int
 653mext_replace_branches(handle_t *handle, struct inode *orig_inode,
 654			   struct inode *donor_inode, ext4_lblk_t from,
 655			   ext4_lblk_t count, int *err)
 656{
 657	struct ext4_ext_path *orig_path = NULL;
 658	struct ext4_ext_path *donor_path = NULL;
 659	struct ext4_extent *oext, *dext;
 660	struct ext4_extent tmp_dext, tmp_oext;
 661	ext4_lblk_t orig_off = from, donor_off = from;
 662	int depth;
 663	int replaced_count = 0;
 664	int dext_alen;
 665
 666	/* Protect extent trees against block allocations via delalloc */
 667	double_down_write_data_sem(orig_inode, donor_inode);
 
 
 
 
 
 668
 669	/* Get the original extent for the block "orig_off" */
 670	*err = get_ext_path(orig_inode, orig_off, &orig_path);
 671	if (*err)
 672		goto out;
 673
 674	/* Get the donor extent for the head */
 675	*err = get_ext_path(donor_inode, donor_off, &donor_path);
 676	if (*err)
 677		goto out;
 678	depth = ext_depth(orig_inode);
 679	oext = orig_path[depth].p_ext;
 680	tmp_oext = *oext;
 681
 682	depth = ext_depth(donor_inode);
 683	dext = donor_path[depth].p_ext;
 
 
 684	tmp_dext = *dext;
 685
 686	*err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
 687				      donor_off, count);
 688	if (*err)
 689		goto out;
 690
 691	/* Loop for the donor extents */
 692	while (1) {
 693		/* The extent for donor must be found. */
 694		if (!dext) {
 
 695			EXT4_ERROR_INODE(donor_inode,
 696				   "The extent for donor must be found");
 697			*err = -EIO;
 698			goto out;
 699		} else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
 700			EXT4_ERROR_INODE(donor_inode,
 701				"Donor offset(%u) and the first block of donor "
 702				"extent(%u) should be equal",
 703				donor_off,
 704				le32_to_cpu(tmp_dext.ee_block));
 705			*err = -EIO;
 706			goto out;
 707		}
 708
 709		/* Set donor extent to orig extent */
 710		*err = mext_leaf_block(handle, orig_inode,
 711					   orig_path, &tmp_dext, &orig_off);
 712		if (*err)
 713			goto out;
 714
 715		/* Set orig extent to donor extent */
 716		*err = mext_leaf_block(handle, donor_inode,
 717					   donor_path, &tmp_oext, &donor_off);
 718		if (*err)
 719			goto out;
 720
 721		dext_alen = ext4_ext_get_actual_len(&tmp_dext);
 722		replaced_count += dext_alen;
 723		donor_off += dext_alen;
 724		orig_off += dext_alen;
 725
 
 726		/* Already moved the expected blocks */
 727		if (replaced_count >= count)
 728			break;
 729
 730		if (orig_path)
 731			ext4_ext_drop_refs(orig_path);
 732		*err = get_ext_path(orig_inode, orig_off, &orig_path);
 733		if (*err)
 734			goto out;
 735		depth = ext_depth(orig_inode);
 736		oext = orig_path[depth].p_ext;
 737		tmp_oext = *oext;
 738
 739		if (donor_path)
 740			ext4_ext_drop_refs(donor_path);
 741		*err = get_ext_path(donor_inode, donor_off, &donor_path);
 742		if (*err)
 743			goto out;
 744		depth = ext_depth(donor_inode);
 745		dext = donor_path[depth].p_ext;
 746		tmp_dext = *dext;
 747
 748		*err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
 749					   donor_off, count - replaced_count);
 750		if (*err)
 751			goto out;
 752	}
 753
 754out:
 755	if (orig_path) {
 756		ext4_ext_drop_refs(orig_path);
 757		kfree(orig_path);
 758	}
 759	if (donor_path) {
 760		ext4_ext_drop_refs(donor_path);
 761		kfree(donor_path);
 762	}
 763
 764	ext4_ext_invalidate_cache(orig_inode);
 765	ext4_ext_invalidate_cache(donor_inode);
 766
 767	double_up_write_data_sem(orig_inode, donor_inode);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 768
 769	return replaced_count;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 770}
 771
 772/**
 773 * move_extent_per_page - Move extent data per page
 774 *
 775 * @o_filp:			file structure of original file
 776 * @donor_inode:		donor inode
 777 * @orig_page_offset:		page index on original file
 778 * @data_offset_in_page:	block index where data swapping starts
 779 * @block_len_in_page:		the number of blocks to be swapped
 780 * @uninit:			orig extent is uninitialized or not
 781 * @err:			pointer to save return value
 782 *
 783 * Save the data in original inode blocks and replace original inode extents
 784 * with donor inode extents by calling mext_replace_branches().
 785 * Finally, write out the saved data in new original inode blocks. Return
 786 * replaced block count.
 787 */
 788static int
 789move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
 790		  pgoff_t orig_page_offset, int data_offset_in_page,
 791		  int block_len_in_page, int uninit, int *err)
 792{
 793	struct inode *orig_inode = o_filp->f_dentry->d_inode;
 794	struct address_space *mapping = orig_inode->i_mapping;
 795	struct buffer_head *bh;
 796	struct page *page = NULL;
 797	const struct address_space_operations *a_ops = mapping->a_ops;
 798	handle_t *handle;
 799	ext4_lblk_t orig_blk_offset;
 800	long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
 801	unsigned long blocksize = orig_inode->i_sb->s_blocksize;
 802	unsigned int w_flags = 0;
 803	unsigned int tmp_data_size, data_size, replaced_size;
 804	void *fsdata;
 805	int i, jblocks;
 806	int err2 = 0;
 807	int replaced_count = 0;
 
 808	int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
 809
 810	/*
 811	 * It needs twice the amount of ordinary journal buffers because
 812	 * inode and donor_inode may change each different metadata blocks.
 813	 */
 
 
 814	jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
 815	handle = ext4_journal_start(orig_inode, jblocks);
 816	if (IS_ERR(handle)) {
 817		*err = PTR_ERR(handle);
 818		return 0;
 819	}
 820
 821	if (segment_eq(get_fs(), KERNEL_DS))
 822		w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
 823
 824	orig_blk_offset = orig_page_offset * blocks_per_page +
 825		data_offset_in_page;
 826
 827	/*
 828	 * If orig extent is uninitialized one,
 829	 * it's not necessary force the page into memory
 830	 * and then force it to be written out again.
 831	 * Just swap data blocks between orig and donor.
 832	 */
 833	if (uninit) {
 834		replaced_count = mext_replace_branches(handle, orig_inode,
 835						donor_inode, orig_blk_offset,
 836						block_len_in_page, err);
 837		goto out2;
 838	}
 839
 840	offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
 841
 842	/* Calculate data_size */
 843	if ((orig_blk_offset + block_len_in_page - 1) ==
 844	    ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
 845		/* Replace the last block */
 846		tmp_data_size = orig_inode->i_size & (blocksize - 1);
 847		/*
 848		 * If data_size equal zero, it shows data_size is multiples of
 849		 * blocksize. So we set appropriate value.
 850		 */
 851		if (tmp_data_size == 0)
 852			tmp_data_size = blocksize;
 853
 854		data_size = tmp_data_size +
 855			((block_len_in_page - 1) << orig_inode->i_blkbits);
 856	} else
 857		data_size = block_len_in_page << orig_inode->i_blkbits;
 858
 859	replaced_size = data_size;
 860
 861	*err = a_ops->write_begin(o_filp, mapping, offs, data_size, w_flags,
 862				 &page, &fsdata);
 863	if (unlikely(*err < 0))
 864		goto out;
 865
 866	if (!PageUptodate(page)) {
 867		mapping->a_ops->readpage(o_filp, page);
 868		lock_page(page);
 869	}
 870
 871	/*
 872	 * try_to_release_page() doesn't call releasepage in writeback mode.
 873	 * We should care about the order of writing to the same file
 874	 * by multiple move extent processes.
 875	 * It needs to call wait_on_page_writeback() to wait for the
 876	 * writeback of the page.
 877	 */
 878	wait_on_page_writeback(page);
 
 
 
 
 
 
 
 879
 880	/* Release old bh and drop refs */
 881	try_to_release_page(page, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 882
 883	replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
 884					orig_blk_offset, block_len_in_page,
 885					&err2);
 886	if (err2) {
 887		if (replaced_count) {
 888			block_len_in_page = replaced_count;
 889			replaced_size =
 890				block_len_in_page << orig_inode->i_blkbits;
 891		} else
 892			goto out;
 893	}
 894
 895	if (!page_has_buffers(page))
 896		create_empty_buffers(page, 1 << orig_inode->i_blkbits, 0);
 897
 898	bh = page_buffers(page);
 899	for (i = 0; i < data_offset_in_page; i++)
 900		bh = bh->b_this_page;
 901
 902	for (i = 0; i < block_len_in_page; i++) {
 903		*err = ext4_get_block(orig_inode,
 904				(sector_t)(orig_blk_offset + i), bh, 0);
 905		if (*err < 0)
 906			goto out;
 907
 908		if (bh->b_this_page != NULL)
 909			bh = bh->b_this_page;
 910	}
 
 
 
 
 
 
 911
 912	*err = a_ops->write_end(o_filp, mapping, offs, data_size, replaced_size,
 913			       page, fsdata);
 914	page = NULL;
 915
 916out:
 917	if (unlikely(page)) {
 918		if (PageLocked(page))
 919			unlock_page(page);
 920		page_cache_release(page);
 921		ext4_journal_stop(handle);
 922	}
 923out2:
 
 
 924	ext4_journal_stop(handle);
 925
 926	if (err2)
 927		*err = err2;
 928
 
 929	return replaced_count;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 930}
 931
 932/**
 933 * mext_check_arguments - Check whether move extent can be done
 934 *
 935 * @orig_inode:		original inode
 936 * @donor_inode:	donor inode
 937 * @orig_start:		logical start offset in block for orig
 938 * @donor_start:	logical start offset in block for donor
 939 * @len:		the number of blocks to be moved
 940 *
 941 * Check the arguments of ext4_move_extents() whether the files can be
 942 * exchanged with each other.
 943 * Return 0 on success, or a negative error value on failure.
 944 */
 945static int
 946mext_check_arguments(struct inode *orig_inode,
 947		     struct inode *donor_inode, __u64 orig_start,
 948		     __u64 donor_start, __u64 *len)
 949{
 950	ext4_lblk_t orig_blocks, donor_blocks;
 951	unsigned int blkbits = orig_inode->i_blkbits;
 952	unsigned int blocksize = 1 << blkbits;
 953
 954	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
 955		ext4_debug("ext4 move extent: suid or sgid is set"
 956			   " to donor file [ino:orig %lu, donor %lu]\n",
 957			   orig_inode->i_ino, donor_inode->i_ino);
 958		return -EINVAL;
 959	}
 960
 961	if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
 962		return -EPERM;
 963
 964	/* Ext4 move extent does not support swapfile */
 965	if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
 966		ext4_debug("ext4 move extent: The argument files should "
 967			"not be swapfile [ino:orig %lu, donor %lu]\n",
 968			orig_inode->i_ino, donor_inode->i_ino);
 969		return -EINVAL;
 970	}
 971
 972	/* Files should be in the same ext4 FS */
 973	if (orig_inode->i_sb != donor_inode->i_sb) {
 974		ext4_debug("ext4 move extent: The argument files "
 975			"should be in same FS [ino:orig %lu, donor %lu]\n",
 976			orig_inode->i_ino, donor_inode->i_ino);
 977		return -EINVAL;
 978	}
 979
 980	/* Ext4 move extent supports only extent based file */
 981	if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
 982		ext4_debug("ext4 move extent: orig file is not extents "
 983			"based file [ino:orig %lu]\n", orig_inode->i_ino);
 984		return -EOPNOTSUPP;
 985	} else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
 986		ext4_debug("ext4 move extent: donor file is not extents "
 987			"based file [ino:donor %lu]\n", donor_inode->i_ino);
 988		return -EOPNOTSUPP;
 989	}
 990
 991	if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
 992		ext4_debug("ext4 move extent: File size is 0 byte\n");
 993		return -EINVAL;
 994	}
 995
 996	/* Start offset should be same */
 997	if (orig_start != donor_start) {
 998		ext4_debug("ext4 move extent: orig and donor's start "
 999			"offset are not same [ino:orig %lu, donor %lu]\n",
1000			orig_inode->i_ino, donor_inode->i_ino);
1001		return -EINVAL;
1002	}
1003
1004	if ((orig_start >= EXT_MAX_BLOCKS) ||
1005	    (donor_start >= EXT_MAX_BLOCKS) ||
1006	    (*len > EXT_MAX_BLOCKS) ||
1007	    (orig_start + *len >= EXT_MAX_BLOCKS))  {
1008		ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
1009			"[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
1010			orig_inode->i_ino, donor_inode->i_ino);
1011		return -EINVAL;
1012	}
1013
1014	if (orig_inode->i_size > donor_inode->i_size) {
1015		donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1016		/* TODO: eliminate this artificial restriction */
1017		if (orig_start >= donor_blocks) {
1018			ext4_debug("ext4 move extent: orig start offset "
1019			"[%llu] should be less than donor file blocks "
1020			"[%u] [ino:orig %lu, donor %lu]\n",
1021			orig_start, donor_blocks,
1022			orig_inode->i_ino, donor_inode->i_ino);
1023			return -EINVAL;
1024		}
1025
1026		/* TODO: eliminate this artificial restriction */
1027		if (orig_start + *len > donor_blocks) {
1028			ext4_debug("ext4 move extent: End offset [%llu] should "
1029				"be less than donor file blocks [%u]."
1030				"So adjust length from %llu to %llu "
1031				"[ino:orig %lu, donor %lu]\n",
1032				orig_start + *len, donor_blocks,
1033				*len, donor_blocks - orig_start,
1034				orig_inode->i_ino, donor_inode->i_ino);
1035			*len = donor_blocks - orig_start;
1036		}
1037	} else {
1038		orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1039		if (orig_start >= orig_blocks) {
1040			ext4_debug("ext4 move extent: start offset [%llu] "
1041				"should be less than original file blocks "
1042				"[%u] [ino:orig %lu, donor %lu]\n",
1043				 orig_start, orig_blocks,
1044				orig_inode->i_ino, donor_inode->i_ino);
1045			return -EINVAL;
1046		}
1047
1048		if (orig_start + *len > orig_blocks) {
1049			ext4_debug("ext4 move extent: Adjust length "
1050				"from %llu to %llu. Because it should be "
1051				"less than original file blocks "
1052				"[ino:orig %lu, donor %lu]\n",
1053				*len, orig_blocks - orig_start,
1054				orig_inode->i_ino, donor_inode->i_ino);
1055			*len = orig_blocks - orig_start;
1056		}
1057	}
1058
1059	if (!*len) {
1060		ext4_debug("ext4 move extent: len should not be 0 "
1061			"[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1062			donor_inode->i_ino);
1063		return -EINVAL;
1064	}
1065
1066	return 0;
1067}
1068
1069/**
1070 * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1071 *
1072 * @inode1:	the inode structure
1073 * @inode2:	the inode structure
1074 *
1075 * Lock two inodes' i_mutex by i_ino order.
1076 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
1077 */
1078static int
1079mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
1080{
1081	int ret = 0;
1082
1083	BUG_ON(inode1 == NULL && inode2 == NULL);
1084
1085	ret = mext_check_null_inode(inode1, inode2, __func__, __LINE__);
1086	if (ret < 0)
1087		goto out;
1088
1089	if (inode1 == inode2) {
1090		mutex_lock(&inode1->i_mutex);
1091		goto out;
1092	}
1093
1094	if (inode1->i_ino < inode2->i_ino) {
1095		mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1096		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1097	} else {
1098		mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1099		mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1100	}
1101
1102out:
1103	return ret;
1104}
1105
1106/**
1107 * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1108 *
1109 * @inode1:     the inode that is released first
1110 * @inode2:     the inode that is released second
1111 *
1112 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
1113 */
1114
1115static int
1116mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
1117{
1118	int ret = 0;
1119
1120	BUG_ON(inode1 == NULL && inode2 == NULL);
1121
1122	ret = mext_check_null_inode(inode1, inode2, __func__, __LINE__);
1123	if (ret < 0)
1124		goto out;
1125
1126	if (inode1)
1127		mutex_unlock(&inode1->i_mutex);
1128
1129	if (inode2 && inode2 != inode1)
1130		mutex_unlock(&inode2->i_mutex);
1131
1132out:
1133	return ret;
1134}
1135
1136/**
1137 * ext4_move_extents - Exchange the specified range of a file
1138 *
1139 * @o_filp:		file structure of the original file
1140 * @d_filp:		file structure of the donor file
1141 * @orig_start:		start offset in block for orig
1142 * @donor_start:	start offset in block for donor
1143 * @len:		the number of blocks to be moved
1144 * @moved_len:		moved block length
1145 *
1146 * This function returns 0 and moved block length is set in moved_len
1147 * if succeed, otherwise returns error value.
1148 *
1149 * Note: ext4_move_extents() proceeds the following order.
1150 * 1:ext4_move_extents() calculates the last block number of moving extent
1151 *   function by the start block number (orig_start) and the number of blocks
1152 *   to be moved (len) specified as arguments.
1153 *   If the {orig, donor}_start points a hole, the extent's start offset
1154 *   pointed by ext_cur (current extent), holecheck_path, orig_path are set
1155 *   after hole behind.
1156 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1157 *   or the ext_cur exceeds the block_end which is last logical block number.
1158 * 3:To get the length of continues area, call mext_next_extent()
1159 *   specified with the ext_cur (initial value is holecheck_path) re-cursive,
1160 *   until find un-continuous extent, the start logical block number exceeds
1161 *   the block_end or the extent points to the last extent.
1162 * 4:Exchange the original inode data with donor inode data
1163 *   from orig_page_offset to seq_end_page.
1164 *   The start indexes of data are specified as arguments.
1165 *   That of the original inode is orig_page_offset,
1166 *   and the donor inode is also orig_page_offset
1167 *   (To easily handle blocksize != pagesize case, the offset for the
1168 *   donor inode is block unit).
1169 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1170 *   then returns to step 2.
1171 * 6:Release holecheck_path, orig_path and set the len to moved_len
1172 *   which shows the number of moved blocks.
1173 *   The moved_len is useful for the command to calculate the file offset
1174 *   for starting next move extent ioctl.
1175 * 7:Return 0 on success, or a negative error value on failure.
1176 */
1177int
1178ext4_move_extents(struct file *o_filp, struct file *d_filp,
1179		 __u64 orig_start, __u64 donor_start, __u64 len,
1180		 __u64 *moved_len)
1181{
1182	struct inode *orig_inode = o_filp->f_dentry->d_inode;
1183	struct inode *donor_inode = d_filp->f_dentry->d_inode;
1184	struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1185	struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1186	ext4_lblk_t block_start = orig_start;
1187	ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1188	ext4_lblk_t rest_blocks;
1189	pgoff_t orig_page_offset = 0, seq_end_page;
1190	int ret1, ret2, depth, last_extent = 0;
1191	int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1192	int data_offset_in_page;
1193	int block_len_in_page;
1194	int uninit;
1195
1196	/* orig and donor should be different file */
1197	if (orig_inode->i_ino == donor_inode->i_ino) {
 
 
 
 
 
 
 
1198		ext4_debug("ext4 move extent: The argument files should not "
1199			"be same file [ino:orig %lu, donor %lu]\n",
1200			orig_inode->i_ino, donor_inode->i_ino);
1201		return -EINVAL;
1202	}
1203
1204	/* Regular file check */
1205	if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
1206		ext4_debug("ext4 move extent: The argument files should be "
1207			"regular file [ino:orig %lu, donor %lu]\n",
1208			orig_inode->i_ino, donor_inode->i_ino);
1209		return -EINVAL;
1210	}
1211
 
 
 
 
 
1212	/* Protect orig and donor inodes against a truncate */
1213	ret1 = mext_inode_double_lock(orig_inode, donor_inode);
1214	if (ret1 < 0)
1215		return ret1;
 
 
 
 
1216
1217	/* Protect extent tree against block allocations via delalloc */
1218	double_down_write_data_sem(orig_inode, donor_inode);
1219	/* Check the filesystem environment whether move_extent can be done */
1220	ret1 = mext_check_arguments(orig_inode, donor_inode, orig_start,
1221				    donor_start, &len);
1222	if (ret1)
1223		goto out;
1224
1225	file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1226	block_end = block_start + len - 1;
1227	if (file_end < block_end)
1228		len -= block_end - file_end;
1229
1230	ret1 = get_ext_path(orig_inode, block_start, &orig_path);
1231	if (ret1)
1232		goto out;
1233
1234	/* Get path structure to check the hole */
1235	ret1 = get_ext_path(orig_inode, block_start, &holecheck_path);
1236	if (ret1)
1237		goto out;
1238
1239	depth = ext_depth(orig_inode);
1240	ext_cur = holecheck_path[depth].p_ext;
1241
1242	/*
1243	 * Get proper starting location of block replacement if block_start was
1244	 * within the hole.
1245	 */
1246	if (le32_to_cpu(ext_cur->ee_block) +
1247		ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
1248		/*
1249		 * The hole exists between extents or the tail of
1250		 * original file.
1251		 */
1252		last_extent = mext_next_extent(orig_inode,
1253					holecheck_path, &ext_cur);
1254		if (last_extent < 0) {
1255			ret1 = last_extent;
1256			goto out;
1257		}
1258		last_extent = mext_next_extent(orig_inode, orig_path,
1259							&ext_dummy);
1260		if (last_extent < 0) {
1261			ret1 = last_extent;
1262			goto out;
1263		}
1264		seq_start = le32_to_cpu(ext_cur->ee_block);
1265	} else if (le32_to_cpu(ext_cur->ee_block) > block_start)
1266		/* The hole exists at the beginning of original file. */
1267		seq_start = le32_to_cpu(ext_cur->ee_block);
1268	else
1269		seq_start = block_start;
1270
1271	/* No blocks within the specified range. */
1272	if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1273		ext4_debug("ext4 move extent: The specified range of file "
1274							"may be the hole\n");
1275		ret1 = -EINVAL;
1276		goto out;
1277	}
1278
1279	/* Adjust start blocks */
1280	add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1281			 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1282		     max(le32_to_cpu(ext_cur->ee_block), block_start);
1283
1284	while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1285		seq_blocks += add_blocks;
1286
1287		/* Adjust tail blocks */
1288		if (seq_start + seq_blocks - 1 > block_end)
1289			seq_blocks = block_end - seq_start + 1;
1290
1291		ext_prev = ext_cur;
1292		last_extent = mext_next_extent(orig_inode, holecheck_path,
1293						&ext_cur);
1294		if (last_extent < 0) {
1295			ret1 = last_extent;
1296			break;
1297		}
1298		add_blocks = ext4_ext_get_actual_len(ext_cur);
1299
1300		/*
1301		 * Extend the length of contiguous block (seq_blocks)
1302		 * if extents are contiguous.
1303		 */
1304		if (ext4_can_extents_be_merged(orig_inode,
1305					       ext_prev, ext_cur) &&
1306		    block_end >= le32_to_cpu(ext_cur->ee_block) &&
1307		    !last_extent)
1308			continue;
1309
1310		/* Is original extent is uninitialized */
1311		uninit = ext4_ext_is_uninitialized(ext_prev);
1312
1313		data_offset_in_page = seq_start % blocks_per_page;
1314
1315		/*
1316		 * Calculate data blocks count that should be swapped
1317		 * at the first page.
1318		 */
1319		if (data_offset_in_page + seq_blocks > blocks_per_page) {
1320			/* Swapped blocks are across pages */
1321			block_len_in_page =
1322					blocks_per_page - data_offset_in_page;
1323		} else {
1324			/* Swapped blocks are in a page */
1325			block_len_in_page = seq_blocks;
1326		}
1327
1328		orig_page_offset = seq_start >>
1329				(PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1330		seq_end_page = (seq_start + seq_blocks - 1) >>
1331				(PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1332		seq_start = le32_to_cpu(ext_cur->ee_block);
1333		rest_blocks = seq_blocks;
1334
1335		/*
1336		 * Up semaphore to avoid following problems:
1337		 * a. transaction deadlock among ext4_journal_start,
1338		 *    ->write_begin via pagefault, and jbd2_journal_commit
1339		 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1340		 *    in move_extent_per_page
1341		 */
1342		double_up_write_data_sem(orig_inode, donor_inode);
1343
1344		while (orig_page_offset <= seq_end_page) {
1345
1346			/* Swap original branches with new branches */
1347			block_len_in_page = move_extent_per_page(
1348						o_filp, donor_inode,
1349						orig_page_offset,
1350						data_offset_in_page,
1351						block_len_in_page, uninit,
1352						&ret1);
1353
1354			/* Count how many blocks we have exchanged */
1355			*moved_len += block_len_in_page;
1356			if (ret1 < 0)
1357				break;
1358			if (*moved_len > len) {
1359				EXT4_ERROR_INODE(orig_inode,
1360					"We replaced blocks too much! "
1361					"sum of replaced: %llu requested: %llu",
1362					*moved_len, len);
1363				ret1 = -EIO;
1364				break;
1365			}
1366
1367			orig_page_offset++;
1368			data_offset_in_page = 0;
1369			rest_blocks -= block_len_in_page;
1370			if (rest_blocks > blocks_per_page)
1371				block_len_in_page = blocks_per_page;
1372			else
1373				block_len_in_page = rest_blocks;
1374		}
1375
1376		double_down_write_data_sem(orig_inode, donor_inode);
1377		if (ret1 < 0)
1378			break;
1379
1380		/* Decrease buffer counter */
1381		if (holecheck_path)
1382			ext4_ext_drop_refs(holecheck_path);
1383		ret1 = get_ext_path(orig_inode, seq_start, &holecheck_path);
1384		if (ret1)
1385			break;
1386		depth = holecheck_path->p_depth;
1387
1388		/* Decrease buffer counter */
1389		if (orig_path)
1390			ext4_ext_drop_refs(orig_path);
1391		ret1 = get_ext_path(orig_inode, seq_start, &orig_path);
1392		if (ret1)
1393			break;
1394
1395		ext_cur = holecheck_path[depth].p_ext;
1396		add_blocks = ext4_ext_get_actual_len(ext_cur);
1397		seq_blocks = 0;
1398
1399	}
1400out:
1401	if (*moved_len) {
1402		ext4_discard_preallocations(orig_inode);
1403		ext4_discard_preallocations(donor_inode);
1404	}
1405
1406	if (orig_path) {
1407		ext4_ext_drop_refs(orig_path);
1408		kfree(orig_path);
1409	}
1410	if (holecheck_path) {
1411		ext4_ext_drop_refs(holecheck_path);
1412		kfree(holecheck_path);
1413	}
1414	double_up_write_data_sem(orig_inode, donor_inode);
1415	ret2 = mext_inode_double_unlock(orig_inode, donor_inode);
 
 
1416
1417	if (ret1)
1418		return ret1;
1419	else if (ret2)
1420		return ret2;
1421
1422	return 0;
1423}
v3.15
   1/*
   2 * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
   3 * Written by Takashi Sato <t-sato@yk.jp.nec.com>
   4 *            Akira Fujita <a-fujita@rs.jp.nec.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of version 2.1 of the GNU Lesser General Public License
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/fs.h>
  17#include <linux/quotaops.h>
  18#include <linux/slab.h>
  19#include "ext4_jbd2.h"
  20#include "ext4.h"
  21#include "ext4_extents.h"
  22
  23/**
  24 * get_ext_path - Find an extent path for designated logical block number.
  25 *
  26 * @inode:	an inode which is searched
  27 * @lblock:	logical block number to find an extent path
  28 * @path:	pointer to an extent path pointer (for output)
  29 *
  30 * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value
  31 * on failure.
  32 */
  33static inline int
  34get_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	if (IS_ERR(path))
  42		ret = PTR_ERR(path);
  43	else if (path[ext_depth(inode)].p_ext == NULL)
 
  44		ret = -ENODATA;
  45	else
  46		*orig_path = path;
  47
  48	return ret;
  49}
  50
  51/**
  52 * copy_extent_status - Copy the extent's initialization status
  53 *
  54 * @src:	an extent for getting initialize status
  55 * @dest:	an extent to be set the status
  56 */
  57static void
  58copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
  59{
  60	if (ext4_ext_is_uninitialized(src))
  61		ext4_ext_mark_uninitialized(dest);
  62	else
  63		dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
  64}
  65
  66/**
  67 * mext_next_extent - Search for the next extent and set it to "extent"
  68 *
  69 * @inode:	inode which is searched
  70 * @path:	this will obtain data for the next extent
  71 * @extent:	pointer to the next extent we have just gotten
  72 *
  73 * Search the next extent in the array of ext4_ext_path structure (@path)
  74 * and set it to ext4_extent structure (@extent). In addition, the member of
  75 * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
  76 * ext4_ext_path structure refers to the last extent, or a negative error
  77 * value on failure.
  78 */
  79int
  80mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
  81		      struct ext4_extent **extent)
  82{
  83	struct ext4_extent_header *eh;
  84	int ppos, leaf_ppos = path->p_depth;
  85
  86	ppos = leaf_ppos;
  87	if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
  88		/* leaf block */
  89		*extent = ++path[ppos].p_ext;
  90		path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  91		return 0;
  92	}
  93
  94	while (--ppos >= 0) {
  95		if (EXT_LAST_INDEX(path[ppos].p_hdr) >
  96		    path[ppos].p_idx) {
  97			int cur_ppos = ppos;
  98
  99			/* index block */
 100			path[ppos].p_idx++;
 101			path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
 102			if (path[ppos+1].p_bh)
 103				brelse(path[ppos+1].p_bh);
 104			path[ppos+1].p_bh =
 105				sb_bread(inode->i_sb, path[ppos].p_block);
 106			if (!path[ppos+1].p_bh)
 107				return -EIO;
 108			path[ppos+1].p_hdr =
 109				ext_block_hdr(path[ppos+1].p_bh);
 110
 111			/* Halfway index block */
 112			while (++cur_ppos < leaf_ppos) {
 113				path[cur_ppos].p_idx =
 114					EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
 115				path[cur_ppos].p_block =
 116					ext4_idx_pblock(path[cur_ppos].p_idx);
 117				if (path[cur_ppos+1].p_bh)
 118					brelse(path[cur_ppos+1].p_bh);
 119				path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
 120					path[cur_ppos].p_block);
 121				if (!path[cur_ppos+1].p_bh)
 122					return -EIO;
 123				path[cur_ppos+1].p_hdr =
 124					ext_block_hdr(path[cur_ppos+1].p_bh);
 125			}
 126
 127			path[leaf_ppos].p_ext = *extent = NULL;
 128
 129			eh = path[leaf_ppos].p_hdr;
 130			if (le16_to_cpu(eh->eh_entries) == 0)
 131				/* empty leaf is found */
 132				return -ENODATA;
 133
 134			/* leaf block */
 135			path[leaf_ppos].p_ext = *extent =
 136				EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
 137			path[leaf_ppos].p_block =
 138					ext4_ext_pblock(path[leaf_ppos].p_ext);
 139			return 0;
 140		}
 141	}
 142	/* We found the last extent */
 143	return 1;
 144}
 145
 146/**
 147 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
 148 *                                   of i_data_sem
 149 *
 150 * Acquire write lock of i_data_sem of the two inodes
 151 */
 152void
 153ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
 
 154{
 155	if (first < second) {
 156		down_write(&EXT4_I(first)->i_data_sem);
 157		down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
 158	} else {
 159		down_write(&EXT4_I(second)->i_data_sem);
 160		down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 161
 
 
 
 
 
 
 
 
 162	}
 
 
 
 163}
 164
 165/**
 166 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
 167 *
 168 * @orig_inode:		original inode structure to be released its lock first
 169 * @donor_inode:	donor inode structure to be released its lock second
 170 * Release write lock of i_data_sem of two inodes (orig and donor).
 171 */
 172void
 173ext4_double_up_write_data_sem(struct inode *orig_inode,
 174			      struct inode *donor_inode)
 175{
 176	up_write(&EXT4_I(orig_inode)->i_data_sem);
 177	up_write(&EXT4_I(donor_inode)->i_data_sem);
 178}
 179
 180/**
 181 * mext_insert_across_blocks - Insert extents across leaf block
 182 *
 183 * @handle:		journal handle
 184 * @orig_inode:		original inode
 185 * @o_start:		first original extent to be changed
 186 * @o_end:		last original extent to be changed
 187 * @start_ext:		first new extent to be inserted
 188 * @new_ext:		middle of new extent to be inserted
 189 * @end_ext:		last new extent to be inserted
 190 *
 191 * Allocate a new leaf block and insert extents into it. Return 0 on success,
 192 * or a negative error value on failure.
 193 */
 194static int
 195mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
 196		struct ext4_extent *o_start, struct ext4_extent *o_end,
 197		struct ext4_extent *start_ext, struct ext4_extent *new_ext,
 198		struct ext4_extent *end_ext)
 199{
 200	struct ext4_ext_path *orig_path = NULL;
 201	ext4_lblk_t eblock = 0;
 202	int new_flag = 0;
 203	int end_flag = 0;
 204	int err = 0;
 205
 206	if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
 207		if (o_start == o_end) {
 208
 209			/*       start_ext   new_ext    end_ext
 210			 * donor |---------|-----------|--------|
 211			 * orig  |------------------------------|
 212			 */
 213			end_flag = 1;
 214		} else {
 215
 216			/*       start_ext   new_ext   end_ext
 217			 * donor |---------|----------|---------|
 218			 * orig  |---------------|--------------|
 219			 */
 220			o_end->ee_block = end_ext->ee_block;
 221			o_end->ee_len = end_ext->ee_len;
 222			ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
 223		}
 224
 225		o_start->ee_len = start_ext->ee_len;
 226		eblock = le32_to_cpu(start_ext->ee_block);
 227		new_flag = 1;
 228
 229	} else if (start_ext->ee_len && new_ext->ee_len &&
 230		   !end_ext->ee_len && o_start == o_end) {
 231
 232		/*	 start_ext	new_ext
 233		 * donor |--------------|---------------|
 234		 * orig  |------------------------------|
 235		 */
 236		o_start->ee_len = start_ext->ee_len;
 237		eblock = le32_to_cpu(start_ext->ee_block);
 238		new_flag = 1;
 239
 240	} else if (!start_ext->ee_len && new_ext->ee_len &&
 241		   end_ext->ee_len && o_start == o_end) {
 242
 243		/*	  new_ext	end_ext
 244		 * donor |--------------|---------------|
 245		 * orig  |------------------------------|
 246		 */
 247		o_end->ee_block = end_ext->ee_block;
 248		o_end->ee_len = end_ext->ee_len;
 249		ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
 250
 251		/*
 252		 * Set 0 to the extent block if new_ext was
 253		 * the first block.
 254		 */
 255		if (new_ext->ee_block)
 256			eblock = le32_to_cpu(new_ext->ee_block);
 257
 258		new_flag = 1;
 259	} else {
 260		ext4_debug("ext4 move extent: Unexpected insert case\n");
 261		return -EIO;
 262	}
 263
 264	if (new_flag) {
 265		err = get_ext_path(orig_inode, eblock, &orig_path);
 266		if (err)
 267			goto out;
 268
 269		if (ext4_ext_insert_extent(handle, orig_inode,
 270					orig_path, new_ext, 0))
 271			goto out;
 272	}
 273
 274	if (end_flag) {
 275		err = get_ext_path(orig_inode,
 276				le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
 277		if (err)
 278			goto out;
 279
 280		if (ext4_ext_insert_extent(handle, orig_inode,
 281					   orig_path, end_ext, 0))
 282			goto out;
 283	}
 284out:
 285	if (orig_path) {
 286		ext4_ext_drop_refs(orig_path);
 287		kfree(orig_path);
 288	}
 289
 290	return err;
 291
 292}
 293
 294/**
 295 * mext_insert_inside_block - Insert new extent to the extent block
 296 *
 297 * @o_start:		first original extent to be moved
 298 * @o_end:		last original extent to be moved
 299 * @start_ext:		first new extent to be inserted
 300 * @new_ext:		middle of new extent to be inserted
 301 * @end_ext:		last new extent to be inserted
 302 * @eh:			extent header of target leaf block
 303 * @range_to_move:	used to decide how to insert extent
 304 *
 305 * Insert extents into the leaf block. The extent (@o_start) is overwritten
 306 * by inserted extents.
 307 */
 308static void
 309mext_insert_inside_block(struct ext4_extent *o_start,
 310			      struct ext4_extent *o_end,
 311			      struct ext4_extent *start_ext,
 312			      struct ext4_extent *new_ext,
 313			      struct ext4_extent *end_ext,
 314			      struct ext4_extent_header *eh,
 315			      int range_to_move)
 316{
 317	int i = 0;
 318	unsigned long len;
 319
 320	/* Move the existing extents */
 321	if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
 322		len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
 323			(unsigned long)(o_end + 1);
 324		memmove(o_end + 1 + range_to_move, o_end + 1, len);
 325	}
 326
 327	/* Insert start entry */
 328	if (start_ext->ee_len)
 329		o_start[i++].ee_len = start_ext->ee_len;
 330
 331	/* Insert new entry */
 332	if (new_ext->ee_len) {
 333		o_start[i] = *new_ext;
 334		ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
 335	}
 336
 337	/* Insert end entry */
 338	if (end_ext->ee_len)
 339		o_start[i] = *end_ext;
 340
 341	/* Increment the total entries counter on the extent block */
 342	le16_add_cpu(&eh->eh_entries, range_to_move);
 343}
 344
 345/**
 346 * mext_insert_extents - Insert new extent
 347 *
 348 * @handle:	journal handle
 349 * @orig_inode:	original inode
 350 * @orig_path:	path indicates first extent to be changed
 351 * @o_start:	first original extent to be changed
 352 * @o_end:	last original extent to be changed
 353 * @start_ext:	first new extent to be inserted
 354 * @new_ext:	middle of new extent to be inserted
 355 * @end_ext:	last new extent to be inserted
 356 *
 357 * Call the function to insert extents. If we cannot add more extents into
 358 * the leaf block, we call mext_insert_across_blocks() to create a
 359 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
 360 * on success, or a negative error value on failure.
 361 */
 362static int
 363mext_insert_extents(handle_t *handle, struct inode *orig_inode,
 364			 struct ext4_ext_path *orig_path,
 365			 struct ext4_extent *o_start,
 366			 struct ext4_extent *o_end,
 367			 struct ext4_extent *start_ext,
 368			 struct ext4_extent *new_ext,
 369			 struct ext4_extent *end_ext)
 370{
 371	struct  ext4_extent_header *eh;
 372	unsigned long need_slots, slots_range;
 373	int	range_to_move, depth, ret;
 374
 375	/*
 376	 * The extents need to be inserted
 377	 * start_extent + new_extent + end_extent.
 378	 */
 379	need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
 380		(new_ext->ee_len ? 1 : 0);
 381
 382	/* The number of slots between start and end */
 383	slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
 384		/ sizeof(struct ext4_extent);
 385
 386	/* Range to move the end of extent */
 387	range_to_move = need_slots - slots_range;
 388	depth = orig_path->p_depth;
 389	orig_path += depth;
 390	eh = orig_path->p_hdr;
 391
 392	if (depth) {
 393		/* Register to journal */
 394		ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
 395		if (ret)
 396			return ret;
 397	}
 398
 399	/* Expansion */
 400	if (range_to_move > 0 &&
 401		(range_to_move > le16_to_cpu(eh->eh_max)
 402			- le16_to_cpu(eh->eh_entries))) {
 403
 404		ret = mext_insert_across_blocks(handle, orig_inode, o_start,
 405					o_end, start_ext, new_ext, end_ext);
 406		if (ret < 0)
 407			return ret;
 408	} else
 409		mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
 410						end_ext, eh, range_to_move);
 411
 412	return ext4_ext_dirty(handle, orig_inode, orig_path);
 
 
 
 
 
 
 
 
 
 
 
 413}
 414
 415/**
 416 * mext_leaf_block - Move one leaf extent block into the inode.
 417 *
 418 * @handle:		journal handle
 419 * @orig_inode:		original inode
 420 * @orig_path:		path indicates first extent to be changed
 421 * @dext:		donor extent
 422 * @from:		start offset on the target file
 423 *
 424 * In order to insert extents into the leaf block, we must divide the extent
 425 * in the leaf block into three extents. The one is located to be inserted
 426 * extents, and the others are located around it.
 427 *
 428 * Therefore, this function creates structures to save extents of the leaf
 429 * block, and inserts extents by calling mext_insert_extents() with
 430 * created extents. Return 0 on success, or a negative error value on failure.
 431 */
 432static int
 433mext_leaf_block(handle_t *handle, struct inode *orig_inode,
 434		     struct ext4_ext_path *orig_path, struct ext4_extent *dext,
 435		     ext4_lblk_t *from)
 436{
 437	struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
 438	struct ext4_extent new_ext, start_ext, end_ext;
 439	ext4_lblk_t new_ext_end;
 440	int oext_alen, new_ext_alen, end_ext_alen;
 441	int depth = ext_depth(orig_inode);
 442	int ret;
 443
 444	start_ext.ee_block = end_ext.ee_block = 0;
 445	o_start = o_end = oext = orig_path[depth].p_ext;
 446	oext_alen = ext4_ext_get_actual_len(oext);
 447	start_ext.ee_len = end_ext.ee_len = 0;
 448
 449	new_ext.ee_block = cpu_to_le32(*from);
 450	ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
 451	new_ext.ee_len = dext->ee_len;
 452	new_ext_alen = ext4_ext_get_actual_len(&new_ext);
 453	new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
 454
 455	/*
 456	 * Case: original extent is first
 457	 * oext      |--------|
 458	 * new_ext      |--|
 459	 * start_ext |--|
 460	 */
 461	if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
 462		le32_to_cpu(new_ext.ee_block) <
 463		le32_to_cpu(oext->ee_block) + oext_alen) {
 464		start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
 465					       le32_to_cpu(oext->ee_block));
 466		start_ext.ee_block = oext->ee_block;
 467		copy_extent_status(oext, &start_ext);
 468	} else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
 469		prev_ext = oext - 1;
 470		/*
 471		 * We can merge new_ext into previous extent,
 472		 * if these are contiguous and same extent type.
 473		 */
 474		if (ext4_can_extents_be_merged(orig_inode, prev_ext,
 475					       &new_ext)) {
 476			o_start = prev_ext;
 477			start_ext.ee_len = cpu_to_le16(
 478				ext4_ext_get_actual_len(prev_ext) +
 479				new_ext_alen);
 480			start_ext.ee_block = oext->ee_block;
 481			copy_extent_status(prev_ext, &start_ext);
 482			new_ext.ee_len = 0;
 483		}
 484	}
 485
 486	/*
 487	 * Case: new_ext_end must be less than oext
 488	 * oext      |-----------|
 489	 * new_ext       |-------|
 490	 */
 491	if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
 492		EXT4_ERROR_INODE(orig_inode,
 493			"new_ext_end(%u) should be less than or equal to "
 494			"oext->ee_block(%u) + oext_alen(%d) - 1",
 495			new_ext_end, le32_to_cpu(oext->ee_block),
 496			oext_alen);
 497		ret = -EIO;
 498		goto out;
 499	}
 500
 501	/*
 502	 * Case: new_ext is smaller than original extent
 503	 * oext    |---------------|
 504	 * new_ext |-----------|
 505	 * end_ext             |---|
 506	 */
 507	if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
 508		new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
 509		end_ext.ee_len =
 510			cpu_to_le16(le32_to_cpu(oext->ee_block) +
 511			oext_alen - 1 - new_ext_end);
 512		copy_extent_status(oext, &end_ext);
 513		end_ext_alen = ext4_ext_get_actual_len(&end_ext);
 514		ext4_ext_store_pblock(&end_ext,
 515			(ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
 516		end_ext.ee_block =
 517			cpu_to_le32(le32_to_cpu(o_end->ee_block) +
 518			oext_alen - end_ext_alen);
 519	}
 520
 521	ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
 522				o_end, &start_ext, &new_ext, &end_ext);
 523out:
 524	return ret;
 525}
 526
 527/**
 528 * mext_calc_swap_extents - Calculate extents for extent swapping.
 529 *
 530 * @tmp_dext:		the extent that will belong to the original inode
 531 * @tmp_oext:		the extent that will belong to the donor inode
 532 * @orig_off:		block offset of original inode
 533 * @donor_off:		block offset of donor inode
 534 * @max_count:		the maximum length of extents
 535 *
 536 * Return 0 on success, or a negative error value on failure.
 537 */
 538static int
 539mext_calc_swap_extents(struct ext4_extent *tmp_dext,
 540			      struct ext4_extent *tmp_oext,
 541			      ext4_lblk_t orig_off, ext4_lblk_t donor_off,
 542			      ext4_lblk_t max_count)
 543{
 544	ext4_lblk_t diff, orig_diff;
 545	struct ext4_extent dext_old, oext_old;
 546
 547	BUG_ON(orig_off != donor_off);
 548
 549	/* original and donor extents have to cover the same block offset */
 550	if (orig_off < le32_to_cpu(tmp_oext->ee_block) ||
 551	    le32_to_cpu(tmp_oext->ee_block) +
 552			ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off)
 553		return -ENODATA;
 554
 555	if (orig_off < le32_to_cpu(tmp_dext->ee_block) ||
 556	    le32_to_cpu(tmp_dext->ee_block) +
 557			ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off)
 558		return -ENODATA;
 559
 560	dext_old = *tmp_dext;
 561	oext_old = *tmp_oext;
 562
 563	/* When tmp_dext is too large, pick up the target range. */
 564	diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
 565
 566	ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
 567	le32_add_cpu(&tmp_dext->ee_block, diff);
 568	le16_add_cpu(&tmp_dext->ee_len, -diff);
 
 569
 570	if (max_count < ext4_ext_get_actual_len(tmp_dext))
 571		tmp_dext->ee_len = cpu_to_le16(max_count);
 572
 573	orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
 574	ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
 575
 576	/* Adjust extent length if donor extent is larger than orig */
 577	if (ext4_ext_get_actual_len(tmp_dext) >
 578	    ext4_ext_get_actual_len(tmp_oext) - orig_diff)
 579		tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
 580						orig_diff);
 581
 582	tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
 583
 584	copy_extent_status(&oext_old, tmp_dext);
 585	copy_extent_status(&dext_old, tmp_oext);
 586
 587	return 0;
 588}
 589
 590/**
 591 * mext_check_coverage - Check that all extents in range has the same type
 592 *
 593 * @inode:		inode in question
 594 * @from:		block offset of inode
 595 * @count:		block count to be checked
 596 * @uninit:		extents expected to be uninitialized
 597 * @err:		pointer to save error value
 598 *
 599 * Return 1 if all extents in range has expected type, and zero otherwise.
 600 */
 601static int
 602mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
 603			  int uninit, int *err)
 604{
 605	struct ext4_ext_path *path = NULL;
 606	struct ext4_extent *ext;
 607	int ret = 0;
 608	ext4_lblk_t last = from + count;
 609	while (from < last) {
 610		*err = get_ext_path(inode, from, &path);
 611		if (*err)
 612			goto out;
 613		ext = path[ext_depth(inode)].p_ext;
 614		if (uninit != ext4_ext_is_uninitialized(ext))
 615			goto out;
 616		from += ext4_ext_get_actual_len(ext);
 617		ext4_ext_drop_refs(path);
 618	}
 619	ret = 1;
 620out:
 621	if (path) {
 622		ext4_ext_drop_refs(path);
 623		kfree(path);
 624	}
 625	return ret;
 626}
 627
 628/**
 629 * mext_replace_branches - Replace original extents with new extents
 630 *
 631 * @handle:		journal handle
 632 * @orig_inode:		original inode
 633 * @donor_inode:	donor inode
 634 * @from:		block offset of orig_inode
 635 * @count:		block count to be replaced
 636 * @err:		pointer to save return value
 637 *
 638 * Replace original inode extents and donor inode extents page by page.
 639 * We implement this replacement in the following three steps:
 640 * 1. Save the block information of original and donor inodes into
 641 *    dummy extents.
 642 * 2. Change the block information of original inode to point at the
 643 *    donor inode blocks.
 644 * 3. Change the block information of donor inode to point at the saved
 645 *    original inode blocks in the dummy extents.
 646 *
 647 * Return replaced block count.
 648 */
 649static int
 650mext_replace_branches(handle_t *handle, struct inode *orig_inode,
 651			   struct inode *donor_inode, ext4_lblk_t from,
 652			   ext4_lblk_t count, int *err)
 653{
 654	struct ext4_ext_path *orig_path = NULL;
 655	struct ext4_ext_path *donor_path = NULL;
 656	struct ext4_extent *oext, *dext;
 657	struct ext4_extent tmp_dext, tmp_oext;
 658	ext4_lblk_t orig_off = from, donor_off = from;
 659	int depth;
 660	int replaced_count = 0;
 661	int dext_alen;
 662
 663	*err = ext4_es_remove_extent(orig_inode, from, count);
 664	if (*err)
 665		goto out;
 666
 667	*err = ext4_es_remove_extent(donor_inode, from, count);
 668	if (*err)
 669		goto out;
 670
 671	/* Get the original extent for the block "orig_off" */
 672	*err = get_ext_path(orig_inode, orig_off, &orig_path);
 673	if (*err)
 674		goto out;
 675
 676	/* Get the donor extent for the head */
 677	*err = get_ext_path(donor_inode, donor_off, &donor_path);
 678	if (*err)
 679		goto out;
 680	depth = ext_depth(orig_inode);
 681	oext = orig_path[depth].p_ext;
 682	tmp_oext = *oext;
 683
 684	depth = ext_depth(donor_inode);
 685	dext = donor_path[depth].p_ext;
 686	if (unlikely(!dext))
 687		goto missing_donor_extent;
 688	tmp_dext = *dext;
 689
 690	*err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
 691				      donor_off, count);
 692	if (*err)
 693		goto out;
 694
 695	/* Loop for the donor extents */
 696	while (1) {
 697		/* The extent for donor must be found. */
 698		if (unlikely(!dext)) {
 699		missing_donor_extent:
 700			EXT4_ERROR_INODE(donor_inode,
 701				   "The extent for donor must be found");
 702			*err = -EIO;
 703			goto out;
 704		} else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
 705			EXT4_ERROR_INODE(donor_inode,
 706				"Donor offset(%u) and the first block of donor "
 707				"extent(%u) should be equal",
 708				donor_off,
 709				le32_to_cpu(tmp_dext.ee_block));
 710			*err = -EIO;
 711			goto out;
 712		}
 713
 714		/* Set donor extent to orig extent */
 715		*err = mext_leaf_block(handle, orig_inode,
 716					   orig_path, &tmp_dext, &orig_off);
 717		if (*err)
 718			goto out;
 719
 720		/* Set orig extent to donor extent */
 721		*err = mext_leaf_block(handle, donor_inode,
 722					   donor_path, &tmp_oext, &donor_off);
 723		if (*err)
 724			goto out;
 725
 726		dext_alen = ext4_ext_get_actual_len(&tmp_dext);
 727		replaced_count += dext_alen;
 728		donor_off += dext_alen;
 729		orig_off += dext_alen;
 730
 731		BUG_ON(replaced_count > count);
 732		/* Already moved the expected blocks */
 733		if (replaced_count >= count)
 734			break;
 735
 736		if (orig_path)
 737			ext4_ext_drop_refs(orig_path);
 738		*err = get_ext_path(orig_inode, orig_off, &orig_path);
 739		if (*err)
 740			goto out;
 741		depth = ext_depth(orig_inode);
 742		oext = orig_path[depth].p_ext;
 743		tmp_oext = *oext;
 744
 745		if (donor_path)
 746			ext4_ext_drop_refs(donor_path);
 747		*err = get_ext_path(donor_inode, donor_off, &donor_path);
 748		if (*err)
 749			goto out;
 750		depth = ext_depth(donor_inode);
 751		dext = donor_path[depth].p_ext;
 752		tmp_dext = *dext;
 753
 754		*err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
 755					   donor_off, count - replaced_count);
 756		if (*err)
 757			goto out;
 758	}
 759
 760out:
 761	if (orig_path) {
 762		ext4_ext_drop_refs(orig_path);
 763		kfree(orig_path);
 764	}
 765	if (donor_path) {
 766		ext4_ext_drop_refs(donor_path);
 767		kfree(donor_path);
 768	}
 769
 770	return replaced_count;
 771}
 772
 773/**
 774 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
 775 *
 776 * @inode1:	the inode structure
 777 * @inode2:	the inode structure
 778 * @index:	page index
 779 * @page:	result page vector
 780 *
 781 * Grab two locked pages for inode's by inode order
 782 */
 783static int
 784mext_page_double_lock(struct inode *inode1, struct inode *inode2,
 785		      pgoff_t index, struct page *page[2])
 786{
 787	struct address_space *mapping[2];
 788	unsigned fl = AOP_FLAG_NOFS;
 789
 790	BUG_ON(!inode1 || !inode2);
 791	if (inode1 < inode2) {
 792		mapping[0] = inode1->i_mapping;
 793		mapping[1] = inode2->i_mapping;
 794	} else {
 795		mapping[0] = inode2->i_mapping;
 796		mapping[1] = inode1->i_mapping;
 797	}
 798
 799	page[0] = grab_cache_page_write_begin(mapping[0], index, fl);
 800	if (!page[0])
 801		return -ENOMEM;
 802
 803	page[1] = grab_cache_page_write_begin(mapping[1], index, fl);
 804	if (!page[1]) {
 805		unlock_page(page[0]);
 806		page_cache_release(page[0]);
 807		return -ENOMEM;
 808	}
 809	/*
 810	 * grab_cache_page_write_begin() may not wait on page's writeback if
 811	 * BDI not demand that. But it is reasonable to be very conservative
 812	 * here and explicitly wait on page's writeback
 813	 */
 814	wait_on_page_writeback(page[0]);
 815	wait_on_page_writeback(page[1]);
 816	if (inode1 > inode2) {
 817		struct page *tmp;
 818		tmp = page[0];
 819		page[0] = page[1];
 820		page[1] = tmp;
 821	}
 822	return 0;
 823}
 824
 825/* Force page buffers uptodate w/o dropping page's lock */
 826static int
 827mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
 828{
 829	struct inode *inode = page->mapping->host;
 830	sector_t block;
 831	struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
 832	unsigned int blocksize, block_start, block_end;
 833	int i, err,  nr = 0, partial = 0;
 834	BUG_ON(!PageLocked(page));
 835	BUG_ON(PageWriteback(page));
 836
 837	if (PageUptodate(page))
 838		return 0;
 839
 840	blocksize = 1 << inode->i_blkbits;
 841	if (!page_has_buffers(page))
 842		create_empty_buffers(page, blocksize, 0);
 843
 844	head = page_buffers(page);
 845	block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
 846	for (bh = head, block_start = 0; bh != head || !block_start;
 847	     block++, block_start = block_end, bh = bh->b_this_page) {
 848		block_end = block_start + blocksize;
 849		if (block_end <= from || block_start >= to) {
 850			if (!buffer_uptodate(bh))
 851				partial = 1;
 852			continue;
 853		}
 854		if (buffer_uptodate(bh))
 855			continue;
 856		if (!buffer_mapped(bh)) {
 857			err = ext4_get_block(inode, block, bh, 0);
 858			if (err) {
 859				SetPageError(page);
 860				return err;
 861			}
 862			if (!buffer_mapped(bh)) {
 863				zero_user(page, block_start, blocksize);
 864				set_buffer_uptodate(bh);
 865				continue;
 866			}
 867		}
 868		BUG_ON(nr >= MAX_BUF_PER_PAGE);
 869		arr[nr++] = bh;
 870	}
 871	/* No io required */
 872	if (!nr)
 873		goto out;
 874
 875	for (i = 0; i < nr; i++) {
 876		bh = arr[i];
 877		if (!bh_uptodate_or_lock(bh)) {
 878			err = bh_submit_read(bh);
 879			if (err)
 880				return err;
 881		}
 882	}
 883out:
 884	if (!partial)
 885		SetPageUptodate(page);
 886	return 0;
 887}
 888
 889/**
 890 * move_extent_per_page - Move extent data per page
 891 *
 892 * @o_filp:			file structure of original file
 893 * @donor_inode:		donor inode
 894 * @orig_page_offset:		page index on original file
 895 * @data_offset_in_page:	block index where data swapping starts
 896 * @block_len_in_page:		the number of blocks to be swapped
 897 * @uninit:			orig extent is uninitialized or not
 898 * @err:			pointer to save return value
 899 *
 900 * Save the data in original inode blocks and replace original inode extents
 901 * with donor inode extents by calling mext_replace_branches().
 902 * Finally, write out the saved data in new original inode blocks. Return
 903 * replaced block count.
 904 */
 905static int
 906move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
 907		  pgoff_t orig_page_offset, int data_offset_in_page,
 908		  int block_len_in_page, int uninit, int *err)
 909{
 910	struct inode *orig_inode = file_inode(o_filp);
 911	struct page *pagep[2] = {NULL, NULL};
 
 
 
 912	handle_t *handle;
 913	ext4_lblk_t orig_blk_offset;
 
 914	unsigned long blocksize = orig_inode->i_sb->s_blocksize;
 915	unsigned int w_flags = 0;
 916	unsigned int tmp_data_size, data_size, replaced_size;
 917	int err2, jblocks, retries = 0;
 
 
 918	int replaced_count = 0;
 919	int from = data_offset_in_page << orig_inode->i_blkbits;
 920	int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
 921
 922	/*
 923	 * It needs twice the amount of ordinary journal buffers because
 924	 * inode and donor_inode may change each different metadata blocks.
 925	 */
 926again:
 927	*err = 0;
 928	jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
 929	handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
 930	if (IS_ERR(handle)) {
 931		*err = PTR_ERR(handle);
 932		return 0;
 933	}
 934
 935	if (segment_eq(get_fs(), KERNEL_DS))
 936		w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
 937
 938	orig_blk_offset = orig_page_offset * blocks_per_page +
 939		data_offset_in_page;
 940
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 941	/* Calculate data_size */
 942	if ((orig_blk_offset + block_len_in_page - 1) ==
 943	    ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
 944		/* Replace the last block */
 945		tmp_data_size = orig_inode->i_size & (blocksize - 1);
 946		/*
 947		 * If data_size equal zero, it shows data_size is multiples of
 948		 * blocksize. So we set appropriate value.
 949		 */
 950		if (tmp_data_size == 0)
 951			tmp_data_size = blocksize;
 952
 953		data_size = tmp_data_size +
 954			((block_len_in_page - 1) << orig_inode->i_blkbits);
 955	} else
 956		data_size = block_len_in_page << orig_inode->i_blkbits;
 957
 958	replaced_size = data_size;
 959
 960	*err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
 961				     pagep);
 962	if (unlikely(*err < 0))
 963		goto stop_journal;
 
 
 
 
 
 
 964	/*
 965	 * If orig extent was uninitialized it can become initialized
 966	 * at any time after i_data_sem was dropped, in order to
 967	 * serialize with delalloc we have recheck extent while we
 968	 * hold page's lock, if it is still the case data copy is not
 969	 * necessary, just swap data blocks between orig and donor.
 970	 */
 971	if (uninit) {
 972		ext4_double_down_write_data_sem(orig_inode, donor_inode);
 973		/* If any of extents in range became initialized we have to
 974		 * fallback to data copying */
 975		uninit = mext_check_coverage(orig_inode, orig_blk_offset,
 976					     block_len_in_page, 1, err);
 977		if (*err)
 978			goto drop_data_sem;
 979
 980		uninit &= mext_check_coverage(donor_inode, orig_blk_offset,
 981					      block_len_in_page, 1, err);
 982		if (*err)
 983			goto drop_data_sem;
 984
 985		if (!uninit) {
 986			ext4_double_up_write_data_sem(orig_inode, donor_inode);
 987			goto data_copy;
 988		}
 989		if ((page_has_private(pagep[0]) &&
 990		     !try_to_release_page(pagep[0], 0)) ||
 991		    (page_has_private(pagep[1]) &&
 992		     !try_to_release_page(pagep[1], 0))) {
 993			*err = -EBUSY;
 994			goto drop_data_sem;
 995		}
 996		replaced_count = mext_replace_branches(handle, orig_inode,
 997						donor_inode, orig_blk_offset,
 998						block_len_in_page, err);
 999	drop_data_sem:
1000		ext4_double_up_write_data_sem(orig_inode, donor_inode);
1001		goto unlock_pages;
1002	}
1003data_copy:
1004	*err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
1005	if (*err)
1006		goto unlock_pages;
1007
1008	/* At this point all buffers in range are uptodate, old mapping layout
1009	 * is no longer required, try to drop it now. */
1010	if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
1011	    (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
1012		*err = -EBUSY;
1013		goto unlock_pages;
1014	}
1015
1016	replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
1017					       orig_blk_offset,
1018					       block_len_in_page, err);
1019	if (*err) {
1020		if (replaced_count) {
1021			block_len_in_page = replaced_count;
1022			replaced_size =
1023				block_len_in_page << orig_inode->i_blkbits;
1024		} else
1025			goto unlock_pages;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1026	}
1027	/* Perform all necessary steps similar write_begin()/write_end()
1028	 * but keeping in mind that i_size will not change */
1029	*err = __block_write_begin(pagep[0], from, replaced_size,
1030				   ext4_get_block);
1031	if (!*err)
1032		*err = block_commit_write(pagep[0], from, from + replaced_size);
1033
1034	if (unlikely(*err < 0))
1035		goto repair_branches;
 
1036
1037	/* Even in case of data=writeback it is reasonable to pin
1038	 * inode to transaction, to prevent unexpected data loss */
1039	*err = ext4_jbd2_file_inode(handle, orig_inode);
1040
1041unlock_pages:
1042	unlock_page(pagep[0]);
1043	page_cache_release(pagep[0]);
1044	unlock_page(pagep[1]);
1045	page_cache_release(pagep[1]);
1046stop_journal:
1047	ext4_journal_stop(handle);
1048	/* Buffer was busy because probably is pinned to journal transaction,
1049	 * force transaction commit may help to free it. */
1050	if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
1051						      &retries))
1052		goto again;
1053	return replaced_count;
1054
1055repair_branches:
1056	/*
1057	 * This should never ever happen!
1058	 * Extents are swapped already, but we are not able to copy data.
1059	 * Try to swap extents to it's original places
1060	 */
1061	ext4_double_down_write_data_sem(orig_inode, donor_inode);
1062	replaced_count = mext_replace_branches(handle, donor_inode, orig_inode,
1063					       orig_blk_offset,
1064					       block_len_in_page, &err2);
1065	ext4_double_up_write_data_sem(orig_inode, donor_inode);
1066	if (replaced_count != block_len_in_page) {
1067		EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
1068				       "Unable to copy data block,"
1069				       " data will be lost.");
1070		*err = -EIO;
1071	}
1072	replaced_count = 0;
1073	goto unlock_pages;
1074}
1075
1076/**
1077 * mext_check_arguments - Check whether move extent can be done
1078 *
1079 * @orig_inode:		original inode
1080 * @donor_inode:	donor inode
1081 * @orig_start:		logical start offset in block for orig
1082 * @donor_start:	logical start offset in block for donor
1083 * @len:		the number of blocks to be moved
1084 *
1085 * Check the arguments of ext4_move_extents() whether the files can be
1086 * exchanged with each other.
1087 * Return 0 on success, or a negative error value on failure.
1088 */
1089static int
1090mext_check_arguments(struct inode *orig_inode,
1091		     struct inode *donor_inode, __u64 orig_start,
1092		     __u64 donor_start, __u64 *len)
1093{
1094	ext4_lblk_t orig_blocks, donor_blocks;
1095	unsigned int blkbits = orig_inode->i_blkbits;
1096	unsigned int blocksize = 1 << blkbits;
1097
1098	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
1099		ext4_debug("ext4 move extent: suid or sgid is set"
1100			   " to donor file [ino:orig %lu, donor %lu]\n",
1101			   orig_inode->i_ino, donor_inode->i_ino);
1102		return -EINVAL;
1103	}
1104
1105	if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
1106		return -EPERM;
1107
1108	/* Ext4 move extent does not support swapfile */
1109	if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
1110		ext4_debug("ext4 move extent: The argument files should "
1111			"not be swapfile [ino:orig %lu, donor %lu]\n",
1112			orig_inode->i_ino, donor_inode->i_ino);
1113		return -EINVAL;
1114	}
1115
 
 
 
 
 
 
 
 
1116	/* Ext4 move extent supports only extent based file */
1117	if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
1118		ext4_debug("ext4 move extent: orig file is not extents "
1119			"based file [ino:orig %lu]\n", orig_inode->i_ino);
1120		return -EOPNOTSUPP;
1121	} else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
1122		ext4_debug("ext4 move extent: donor file is not extents "
1123			"based file [ino:donor %lu]\n", donor_inode->i_ino);
1124		return -EOPNOTSUPP;
1125	}
1126
1127	if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
1128		ext4_debug("ext4 move extent: File size is 0 byte\n");
1129		return -EINVAL;
1130	}
1131
1132	/* Start offset should be same */
1133	if (orig_start != donor_start) {
1134		ext4_debug("ext4 move extent: orig and donor's start "
1135			"offset are not same [ino:orig %lu, donor %lu]\n",
1136			orig_inode->i_ino, donor_inode->i_ino);
1137		return -EINVAL;
1138	}
1139
1140	if ((orig_start >= EXT_MAX_BLOCKS) ||
 
1141	    (*len > EXT_MAX_BLOCKS) ||
1142	    (orig_start + *len >= EXT_MAX_BLOCKS))  {
1143		ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
1144			"[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
1145			orig_inode->i_ino, donor_inode->i_ino);
1146		return -EINVAL;
1147	}
1148
1149	if (orig_inode->i_size > donor_inode->i_size) {
1150		donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1151		/* TODO: eliminate this artificial restriction */
1152		if (orig_start >= donor_blocks) {
1153			ext4_debug("ext4 move extent: orig start offset "
1154			"[%llu] should be less than donor file blocks "
1155			"[%u] [ino:orig %lu, donor %lu]\n",
1156			orig_start, donor_blocks,
1157			orig_inode->i_ino, donor_inode->i_ino);
1158			return -EINVAL;
1159		}
1160
1161		/* TODO: eliminate this artificial restriction */
1162		if (orig_start + *len > donor_blocks) {
1163			ext4_debug("ext4 move extent: End offset [%llu] should "
1164				"be less than donor file blocks [%u]."
1165				"So adjust length from %llu to %llu "
1166				"[ino:orig %lu, donor %lu]\n",
1167				orig_start + *len, donor_blocks,
1168				*len, donor_blocks - orig_start,
1169				orig_inode->i_ino, donor_inode->i_ino);
1170			*len = donor_blocks - orig_start;
1171		}
1172	} else {
1173		orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1174		if (orig_start >= orig_blocks) {
1175			ext4_debug("ext4 move extent: start offset [%llu] "
1176				"should be less than original file blocks "
1177				"[%u] [ino:orig %lu, donor %lu]\n",
1178				 orig_start, orig_blocks,
1179				orig_inode->i_ino, donor_inode->i_ino);
1180			return -EINVAL;
1181		}
1182
1183		if (orig_start + *len > orig_blocks) {
1184			ext4_debug("ext4 move extent: Adjust length "
1185				"from %llu to %llu. Because it should be "
1186				"less than original file blocks "
1187				"[ino:orig %lu, donor %lu]\n",
1188				*len, orig_blocks - orig_start,
1189				orig_inode->i_ino, donor_inode->i_ino);
1190			*len = orig_blocks - orig_start;
1191		}
1192	}
1193
1194	if (!*len) {
1195		ext4_debug("ext4 move extent: len should not be 0 "
1196			"[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1197			donor_inode->i_ino);
1198		return -EINVAL;
1199	}
1200
1201	return 0;
1202}
1203
1204/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1205 * ext4_move_extents - Exchange the specified range of a file
1206 *
1207 * @o_filp:		file structure of the original file
1208 * @d_filp:		file structure of the donor file
1209 * @orig_start:		start offset in block for orig
1210 * @donor_start:	start offset in block for donor
1211 * @len:		the number of blocks to be moved
1212 * @moved_len:		moved block length
1213 *
1214 * This function returns 0 and moved block length is set in moved_len
1215 * if succeed, otherwise returns error value.
1216 *
1217 * Note: ext4_move_extents() proceeds the following order.
1218 * 1:ext4_move_extents() calculates the last block number of moving extent
1219 *   function by the start block number (orig_start) and the number of blocks
1220 *   to be moved (len) specified as arguments.
1221 *   If the {orig, donor}_start points a hole, the extent's start offset
1222 *   pointed by ext_cur (current extent), holecheck_path, orig_path are set
1223 *   after hole behind.
1224 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1225 *   or the ext_cur exceeds the block_end which is last logical block number.
1226 * 3:To get the length of continues area, call mext_next_extent()
1227 *   specified with the ext_cur (initial value is holecheck_path) re-cursive,
1228 *   until find un-continuous extent, the start logical block number exceeds
1229 *   the block_end or the extent points to the last extent.
1230 * 4:Exchange the original inode data with donor inode data
1231 *   from orig_page_offset to seq_end_page.
1232 *   The start indexes of data are specified as arguments.
1233 *   That of the original inode is orig_page_offset,
1234 *   and the donor inode is also orig_page_offset
1235 *   (To easily handle blocksize != pagesize case, the offset for the
1236 *   donor inode is block unit).
1237 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1238 *   then returns to step 2.
1239 * 6:Release holecheck_path, orig_path and set the len to moved_len
1240 *   which shows the number of moved blocks.
1241 *   The moved_len is useful for the command to calculate the file offset
1242 *   for starting next move extent ioctl.
1243 * 7:Return 0 on success, or a negative error value on failure.
1244 */
1245int
1246ext4_move_extents(struct file *o_filp, struct file *d_filp,
1247		 __u64 orig_start, __u64 donor_start, __u64 len,
1248		 __u64 *moved_len)
1249{
1250	struct inode *orig_inode = file_inode(o_filp);
1251	struct inode *donor_inode = file_inode(d_filp);
1252	struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1253	struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1254	ext4_lblk_t block_start = orig_start;
1255	ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1256	ext4_lblk_t rest_blocks;
1257	pgoff_t orig_page_offset = 0, seq_end_page;
1258	int ret, depth, last_extent = 0;
1259	int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1260	int data_offset_in_page;
1261	int block_len_in_page;
1262	int uninit;
1263
1264	if (orig_inode->i_sb != donor_inode->i_sb) {
1265		ext4_debug("ext4 move extent: The argument files "
1266			"should be in same FS [ino:orig %lu, donor %lu]\n",
1267			orig_inode->i_ino, donor_inode->i_ino);
1268		return -EINVAL;
1269	}
1270
1271	/* orig and donor should be different inodes */
1272	if (orig_inode == donor_inode) {
1273		ext4_debug("ext4 move extent: The argument files should not "
1274			"be same inode [ino:orig %lu, donor %lu]\n",
1275			orig_inode->i_ino, donor_inode->i_ino);
1276		return -EINVAL;
1277	}
1278
1279	/* Regular file check */
1280	if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
1281		ext4_debug("ext4 move extent: The argument files should be "
1282			"regular file [ino:orig %lu, donor %lu]\n",
1283			orig_inode->i_ino, donor_inode->i_ino);
1284		return -EINVAL;
1285	}
1286	/* TODO: This is non obvious task to swap blocks for inodes with full
1287	   jornaling enabled */
1288	if (ext4_should_journal_data(orig_inode) ||
1289	    ext4_should_journal_data(donor_inode)) {
1290		return -EINVAL;
1291	}
1292	/* Protect orig and donor inodes against a truncate */
1293	lock_two_nondirectories(orig_inode, donor_inode);
1294
1295	/* Wait for all existing dio workers */
1296	ext4_inode_block_unlocked_dio(orig_inode);
1297	ext4_inode_block_unlocked_dio(donor_inode);
1298	inode_dio_wait(orig_inode);
1299	inode_dio_wait(donor_inode);
1300
1301	/* Protect extent tree against block allocations via delalloc */
1302	ext4_double_down_write_data_sem(orig_inode, donor_inode);
1303	/* Check the filesystem environment whether move_extent can be done */
1304	ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
1305				    donor_start, &len);
1306	if (ret)
1307		goto out;
1308
1309	file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1310	block_end = block_start + len - 1;
1311	if (file_end < block_end)
1312		len -= block_end - file_end;
1313
1314	ret = get_ext_path(orig_inode, block_start, &orig_path);
1315	if (ret)
1316		goto out;
1317
1318	/* Get path structure to check the hole */
1319	ret = get_ext_path(orig_inode, block_start, &holecheck_path);
1320	if (ret)
1321		goto out;
1322
1323	depth = ext_depth(orig_inode);
1324	ext_cur = holecheck_path[depth].p_ext;
1325
1326	/*
1327	 * Get proper starting location of block replacement if block_start was
1328	 * within the hole.
1329	 */
1330	if (le32_to_cpu(ext_cur->ee_block) +
1331		ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
1332		/*
1333		 * The hole exists between extents or the tail of
1334		 * original file.
1335		 */
1336		last_extent = mext_next_extent(orig_inode,
1337					holecheck_path, &ext_cur);
1338		if (last_extent < 0) {
1339			ret = last_extent;
1340			goto out;
1341		}
1342		last_extent = mext_next_extent(orig_inode, orig_path,
1343							&ext_dummy);
1344		if (last_extent < 0) {
1345			ret = last_extent;
1346			goto out;
1347		}
1348		seq_start = le32_to_cpu(ext_cur->ee_block);
1349	} else if (le32_to_cpu(ext_cur->ee_block) > block_start)
1350		/* The hole exists at the beginning of original file. */
1351		seq_start = le32_to_cpu(ext_cur->ee_block);
1352	else
1353		seq_start = block_start;
1354
1355	/* No blocks within the specified range. */
1356	if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1357		ext4_debug("ext4 move extent: The specified range of file "
1358							"may be the hole\n");
1359		ret = -EINVAL;
1360		goto out;
1361	}
1362
1363	/* Adjust start blocks */
1364	add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1365			 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1366		     max(le32_to_cpu(ext_cur->ee_block), block_start);
1367
1368	while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1369		seq_blocks += add_blocks;
1370
1371		/* Adjust tail blocks */
1372		if (seq_start + seq_blocks - 1 > block_end)
1373			seq_blocks = block_end - seq_start + 1;
1374
1375		ext_prev = ext_cur;
1376		last_extent = mext_next_extent(orig_inode, holecheck_path,
1377						&ext_cur);
1378		if (last_extent < 0) {
1379			ret = last_extent;
1380			break;
1381		}
1382		add_blocks = ext4_ext_get_actual_len(ext_cur);
1383
1384		/*
1385		 * Extend the length of contiguous block (seq_blocks)
1386		 * if extents are contiguous.
1387		 */
1388		if (ext4_can_extents_be_merged(orig_inode,
1389					       ext_prev, ext_cur) &&
1390		    block_end >= le32_to_cpu(ext_cur->ee_block) &&
1391		    !last_extent)
1392			continue;
1393
1394		/* Is original extent is uninitialized */
1395		uninit = ext4_ext_is_uninitialized(ext_prev);
1396
1397		data_offset_in_page = seq_start % blocks_per_page;
1398
1399		/*
1400		 * Calculate data blocks count that should be swapped
1401		 * at the first page.
1402		 */
1403		if (data_offset_in_page + seq_blocks > blocks_per_page) {
1404			/* Swapped blocks are across pages */
1405			block_len_in_page =
1406					blocks_per_page - data_offset_in_page;
1407		} else {
1408			/* Swapped blocks are in a page */
1409			block_len_in_page = seq_blocks;
1410		}
1411
1412		orig_page_offset = seq_start >>
1413				(PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1414		seq_end_page = (seq_start + seq_blocks - 1) >>
1415				(PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1416		seq_start = le32_to_cpu(ext_cur->ee_block);
1417		rest_blocks = seq_blocks;
1418
1419		/*
1420		 * Up semaphore to avoid following problems:
1421		 * a. transaction deadlock among ext4_journal_start,
1422		 *    ->write_begin via pagefault, and jbd2_journal_commit
1423		 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1424		 *    in move_extent_per_page
1425		 */
1426		ext4_double_up_write_data_sem(orig_inode, donor_inode);
1427
1428		while (orig_page_offset <= seq_end_page) {
1429
1430			/* Swap original branches with new branches */
1431			block_len_in_page = move_extent_per_page(
1432						o_filp, donor_inode,
1433						orig_page_offset,
1434						data_offset_in_page,
1435						block_len_in_page, uninit,
1436						&ret);
1437
1438			/* Count how many blocks we have exchanged */
1439			*moved_len += block_len_in_page;
1440			if (ret < 0)
1441				break;
1442			if (*moved_len > len) {
1443				EXT4_ERROR_INODE(orig_inode,
1444					"We replaced blocks too much! "
1445					"sum of replaced: %llu requested: %llu",
1446					*moved_len, len);
1447				ret = -EIO;
1448				break;
1449			}
1450
1451			orig_page_offset++;
1452			data_offset_in_page = 0;
1453			rest_blocks -= block_len_in_page;
1454			if (rest_blocks > blocks_per_page)
1455				block_len_in_page = blocks_per_page;
1456			else
1457				block_len_in_page = rest_blocks;
1458		}
1459
1460		ext4_double_down_write_data_sem(orig_inode, donor_inode);
1461		if (ret < 0)
1462			break;
1463
1464		/* Decrease buffer counter */
1465		if (holecheck_path)
1466			ext4_ext_drop_refs(holecheck_path);
1467		ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
1468		if (ret)
1469			break;
1470		depth = holecheck_path->p_depth;
1471
1472		/* Decrease buffer counter */
1473		if (orig_path)
1474			ext4_ext_drop_refs(orig_path);
1475		ret = get_ext_path(orig_inode, seq_start, &orig_path);
1476		if (ret)
1477			break;
1478
1479		ext_cur = holecheck_path[depth].p_ext;
1480		add_blocks = ext4_ext_get_actual_len(ext_cur);
1481		seq_blocks = 0;
1482
1483	}
1484out:
1485	if (*moved_len) {
1486		ext4_discard_preallocations(orig_inode);
1487		ext4_discard_preallocations(donor_inode);
1488	}
1489
1490	if (orig_path) {
1491		ext4_ext_drop_refs(orig_path);
1492		kfree(orig_path);
1493	}
1494	if (holecheck_path) {
1495		ext4_ext_drop_refs(holecheck_path);
1496		kfree(holecheck_path);
1497	}
1498	ext4_double_up_write_data_sem(orig_inode, donor_inode);
1499	ext4_inode_resume_unlocked_dio(orig_inode);
1500	ext4_inode_resume_unlocked_dio(donor_inode);
1501	unlock_two_nondirectories(orig_inode, donor_inode);
1502
1503	return ret;
 
 
 
 
 
1504}