ALSA: hda - detect jacks on VT1708 even when no streams are active
[linux/fpc-iii.git] / fs / ext4 / move_extent.c
blobd9cc5ee42f534c9d1dcb87450493d93fbe5d320b
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>
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.
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.
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"
23 /**
24 * get_ext_path - Find an extent path for designated logical block number.
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)
30 * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value
31 * on failure.
33 static inline int
34 get_ext_path(struct inode *inode, ext4_lblk_t lblock,
35 struct ext4_ext_path **path)
37 int ret = 0;
39 *path = ext4_ext_find_extent(inode, lblock, *path);
40 if (IS_ERR(*path)) {
41 ret = PTR_ERR(*path);
42 *path = NULL;
43 } else if ((*path)[ext_depth(inode)].p_ext == NULL)
44 ret = -ENODATA;
46 return ret;
49 /**
50 * copy_extent_status - Copy the extent's initialization status
52 * @src: an extent for getting initialize status
53 * @dest: an extent to be set the status
55 static void
56 copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
58 if (ext4_ext_is_uninitialized(src))
59 ext4_ext_mark_uninitialized(dest);
60 else
61 dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
64 /**
65 * mext_next_extent - Search for the next extent and set it to "extent"
67 * @inode: inode which is searched
68 * @path: this will obtain data for the next extent
69 * @extent: pointer to the next extent we have just gotten
71 * Search the next extent in the array of ext4_ext_path structure (@path)
72 * and set it to ext4_extent structure (@extent). In addition, the member of
73 * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
74 * ext4_ext_path structure refers to the last extent, or a negative error
75 * value on failure.
77 static int
78 mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
79 struct ext4_extent **extent)
81 struct ext4_extent_header *eh;
82 int ppos, leaf_ppos = path->p_depth;
84 ppos = leaf_ppos;
85 if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
86 /* leaf block */
87 *extent = ++path[ppos].p_ext;
88 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
89 return 0;
92 while (--ppos >= 0) {
93 if (EXT_LAST_INDEX(path[ppos].p_hdr) >
94 path[ppos].p_idx) {
95 int cur_ppos = ppos;
97 /* index block */
98 path[ppos].p_idx++;
99 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
100 if (path[ppos+1].p_bh)
101 brelse(path[ppos+1].p_bh);
102 path[ppos+1].p_bh =
103 sb_bread(inode->i_sb, path[ppos].p_block);
104 if (!path[ppos+1].p_bh)
105 return -EIO;
106 path[ppos+1].p_hdr =
107 ext_block_hdr(path[ppos+1].p_bh);
109 /* Halfway index block */
110 while (++cur_ppos < leaf_ppos) {
111 path[cur_ppos].p_idx =
112 EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
113 path[cur_ppos].p_block =
114 ext4_idx_pblock(path[cur_ppos].p_idx);
115 if (path[cur_ppos+1].p_bh)
116 brelse(path[cur_ppos+1].p_bh);
117 path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
118 path[cur_ppos].p_block);
119 if (!path[cur_ppos+1].p_bh)
120 return -EIO;
121 path[cur_ppos+1].p_hdr =
122 ext_block_hdr(path[cur_ppos+1].p_bh);
125 path[leaf_ppos].p_ext = *extent = NULL;
127 eh = path[leaf_ppos].p_hdr;
128 if (le16_to_cpu(eh->eh_entries) == 0)
129 /* empty leaf is found */
130 return -ENODATA;
132 /* leaf block */
133 path[leaf_ppos].p_ext = *extent =
134 EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
135 path[leaf_ppos].p_block =
136 ext4_ext_pblock(path[leaf_ppos].p_ext);
137 return 0;
140 /* We found the last extent */
141 return 1;
145 * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem
147 * Acquire write lock of i_data_sem of the two inodes
149 static void
150 double_down_write_data_sem(struct inode *first, struct inode *second)
152 if (first < second) {
153 down_write(&EXT4_I(first)->i_data_sem);
154 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
155 } else {
156 down_write(&EXT4_I(second)->i_data_sem);
157 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
163 * double_up_write_data_sem - Release two inodes' write lock of i_data_sem
165 * @orig_inode: original inode structure to be released its lock first
166 * @donor_inode: donor inode structure to be released its lock second
167 * Release write lock of i_data_sem of two inodes (orig and donor).
169 static void
170 double_up_write_data_sem(struct inode *orig_inode, struct inode *donor_inode)
172 up_write(&EXT4_I(orig_inode)->i_data_sem);
173 up_write(&EXT4_I(donor_inode)->i_data_sem);
177 * mext_insert_across_blocks - Insert extents across leaf block
179 * @handle: journal handle
180 * @orig_inode: original inode
181 * @o_start: first original extent to be changed
182 * @o_end: last original extent to be changed
183 * @start_ext: first new extent to be inserted
184 * @new_ext: middle of new extent to be inserted
185 * @end_ext: last new extent to be inserted
187 * Allocate a new leaf block and insert extents into it. Return 0 on success,
188 * or a negative error value on failure.
190 static int
191 mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
192 struct ext4_extent *o_start, struct ext4_extent *o_end,
193 struct ext4_extent *start_ext, struct ext4_extent *new_ext,
194 struct ext4_extent *end_ext)
196 struct ext4_ext_path *orig_path = NULL;
197 ext4_lblk_t eblock = 0;
198 int new_flag = 0;
199 int end_flag = 0;
200 int err = 0;
202 if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
203 if (o_start == o_end) {
205 /* start_ext new_ext end_ext
206 * donor |---------|-----------|--------|
207 * orig |------------------------------|
209 end_flag = 1;
210 } else {
212 /* start_ext new_ext end_ext
213 * donor |---------|----------|---------|
214 * orig |---------------|--------------|
216 o_end->ee_block = end_ext->ee_block;
217 o_end->ee_len = end_ext->ee_len;
218 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
221 o_start->ee_len = start_ext->ee_len;
222 eblock = le32_to_cpu(start_ext->ee_block);
223 new_flag = 1;
225 } else if (start_ext->ee_len && new_ext->ee_len &&
226 !end_ext->ee_len && o_start == o_end) {
228 /* start_ext new_ext
229 * donor |--------------|---------------|
230 * orig |------------------------------|
232 o_start->ee_len = start_ext->ee_len;
233 eblock = le32_to_cpu(start_ext->ee_block);
234 new_flag = 1;
236 } else if (!start_ext->ee_len && new_ext->ee_len &&
237 end_ext->ee_len && o_start == o_end) {
239 /* new_ext end_ext
240 * donor |--------------|---------------|
241 * orig |------------------------------|
243 o_end->ee_block = end_ext->ee_block;
244 o_end->ee_len = end_ext->ee_len;
245 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
248 * Set 0 to the extent block if new_ext was
249 * the first block.
251 if (new_ext->ee_block)
252 eblock = le32_to_cpu(new_ext->ee_block);
254 new_flag = 1;
255 } else {
256 ext4_debug("ext4 move extent: Unexpected insert case\n");
257 return -EIO;
260 if (new_flag) {
261 err = get_ext_path(orig_inode, eblock, &orig_path);
262 if (err)
263 goto out;
265 if (ext4_ext_insert_extent(handle, orig_inode,
266 orig_path, new_ext, 0))
267 goto out;
270 if (end_flag) {
271 err = get_ext_path(orig_inode,
272 le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
273 if (err)
274 goto out;
276 if (ext4_ext_insert_extent(handle, orig_inode,
277 orig_path, end_ext, 0))
278 goto out;
280 out:
281 if (orig_path) {
282 ext4_ext_drop_refs(orig_path);
283 kfree(orig_path);
286 return err;
291 * mext_insert_inside_block - Insert new extent to the extent block
293 * @o_start: first original extent to be moved
294 * @o_end: last original extent to be moved
295 * @start_ext: first new extent to be inserted
296 * @new_ext: middle of new extent to be inserted
297 * @end_ext: last new extent to be inserted
298 * @eh: extent header of target leaf block
299 * @range_to_move: used to decide how to insert extent
301 * Insert extents into the leaf block. The extent (@o_start) is overwritten
302 * by inserted extents.
304 static void
305 mext_insert_inside_block(struct ext4_extent *o_start,
306 struct ext4_extent *o_end,
307 struct ext4_extent *start_ext,
308 struct ext4_extent *new_ext,
309 struct ext4_extent *end_ext,
310 struct ext4_extent_header *eh,
311 int range_to_move)
313 int i = 0;
314 unsigned long len;
316 /* Move the existing extents */
317 if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
318 len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
319 (unsigned long)(o_end + 1);
320 memmove(o_end + 1 + range_to_move, o_end + 1, len);
323 /* Insert start entry */
324 if (start_ext->ee_len)
325 o_start[i++].ee_len = start_ext->ee_len;
327 /* Insert new entry */
328 if (new_ext->ee_len) {
329 o_start[i] = *new_ext;
330 ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
333 /* Insert end entry */
334 if (end_ext->ee_len)
335 o_start[i] = *end_ext;
337 /* Increment the total entries counter on the extent block */
338 le16_add_cpu(&eh->eh_entries, range_to_move);
342 * mext_insert_extents - Insert new extent
344 * @handle: journal handle
345 * @orig_inode: original inode
346 * @orig_path: path indicates first extent to be changed
347 * @o_start: first original extent to be changed
348 * @o_end: last original extent to be changed
349 * @start_ext: first new extent to be inserted
350 * @new_ext: middle of new extent to be inserted
351 * @end_ext: last new extent to be inserted
353 * Call the function to insert extents. If we cannot add more extents into
354 * the leaf block, we call mext_insert_across_blocks() to create a
355 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
356 * on success, or a negative error value on failure.
358 static int
359 mext_insert_extents(handle_t *handle, struct inode *orig_inode,
360 struct ext4_ext_path *orig_path,
361 struct ext4_extent *o_start,
362 struct ext4_extent *o_end,
363 struct ext4_extent *start_ext,
364 struct ext4_extent *new_ext,
365 struct ext4_extent *end_ext)
367 struct ext4_extent_header *eh;
368 unsigned long need_slots, slots_range;
369 int range_to_move, depth, ret;
372 * The extents need to be inserted
373 * start_extent + new_extent + end_extent.
375 need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
376 (new_ext->ee_len ? 1 : 0);
378 /* The number of slots between start and end */
379 slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
380 / sizeof(struct ext4_extent);
382 /* Range to move the end of extent */
383 range_to_move = need_slots - slots_range;
384 depth = orig_path->p_depth;
385 orig_path += depth;
386 eh = orig_path->p_hdr;
388 if (depth) {
389 /* Register to journal */
390 ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
391 if (ret)
392 return ret;
395 /* Expansion */
396 if (range_to_move > 0 &&
397 (range_to_move > le16_to_cpu(eh->eh_max)
398 - le16_to_cpu(eh->eh_entries))) {
400 ret = mext_insert_across_blocks(handle, orig_inode, o_start,
401 o_end, start_ext, new_ext, end_ext);
402 if (ret < 0)
403 return ret;
404 } else
405 mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
406 end_ext, eh, range_to_move);
408 if (depth) {
409 ret = ext4_handle_dirty_metadata(handle, orig_inode,
410 orig_path->p_bh);
411 if (ret)
412 return ret;
413 } else {
414 ret = ext4_mark_inode_dirty(handle, orig_inode);
415 if (ret < 0)
416 return ret;
419 return 0;
423 * mext_leaf_block - Move one leaf extent block into the inode.
425 * @handle: journal handle
426 * @orig_inode: original inode
427 * @orig_path: path indicates first extent to be changed
428 * @dext: donor extent
429 * @from: start offset on the target file
431 * In order to insert extents into the leaf block, we must divide the extent
432 * in the leaf block into three extents. The one is located to be inserted
433 * extents, and the others are located around it.
435 * Therefore, this function creates structures to save extents of the leaf
436 * block, and inserts extents by calling mext_insert_extents() with
437 * created extents. Return 0 on success, or a negative error value on failure.
439 static int
440 mext_leaf_block(handle_t *handle, struct inode *orig_inode,
441 struct ext4_ext_path *orig_path, struct ext4_extent *dext,
442 ext4_lblk_t *from)
444 struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
445 struct ext4_extent new_ext, start_ext, end_ext;
446 ext4_lblk_t new_ext_end;
447 int oext_alen, new_ext_alen, end_ext_alen;
448 int depth = ext_depth(orig_inode);
449 int ret;
451 start_ext.ee_block = end_ext.ee_block = 0;
452 o_start = o_end = oext = orig_path[depth].p_ext;
453 oext_alen = ext4_ext_get_actual_len(oext);
454 start_ext.ee_len = end_ext.ee_len = 0;
456 new_ext.ee_block = cpu_to_le32(*from);
457 ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
458 new_ext.ee_len = dext->ee_len;
459 new_ext_alen = ext4_ext_get_actual_len(&new_ext);
460 new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
463 * Case: original extent is first
464 * oext |--------|
465 * new_ext |--|
466 * start_ext |--|
468 if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
469 le32_to_cpu(new_ext.ee_block) <
470 le32_to_cpu(oext->ee_block) + oext_alen) {
471 start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
472 le32_to_cpu(oext->ee_block));
473 start_ext.ee_block = oext->ee_block;
474 copy_extent_status(oext, &start_ext);
475 } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
476 prev_ext = oext - 1;
478 * We can merge new_ext into previous extent,
479 * if these are contiguous and same extent type.
481 if (ext4_can_extents_be_merged(orig_inode, prev_ext,
482 &new_ext)) {
483 o_start = prev_ext;
484 start_ext.ee_len = cpu_to_le16(
485 ext4_ext_get_actual_len(prev_ext) +
486 new_ext_alen);
487 start_ext.ee_block = oext->ee_block;
488 copy_extent_status(prev_ext, &start_ext);
489 new_ext.ee_len = 0;
494 * Case: new_ext_end must be less than oext
495 * oext |-----------|
496 * new_ext |-------|
498 if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
499 EXT4_ERROR_INODE(orig_inode,
500 "new_ext_end(%u) should be less than or equal to "
501 "oext->ee_block(%u) + oext_alen(%d) - 1",
502 new_ext_end, le32_to_cpu(oext->ee_block),
503 oext_alen);
504 ret = -EIO;
505 goto out;
509 * Case: new_ext is smaller than original extent
510 * oext |---------------|
511 * new_ext |-----------|
512 * end_ext |---|
514 if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
515 new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
516 end_ext.ee_len =
517 cpu_to_le16(le32_to_cpu(oext->ee_block) +
518 oext_alen - 1 - new_ext_end);
519 copy_extent_status(oext, &end_ext);
520 end_ext_alen = ext4_ext_get_actual_len(&end_ext);
521 ext4_ext_store_pblock(&end_ext,
522 (ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
523 end_ext.ee_block =
524 cpu_to_le32(le32_to_cpu(o_end->ee_block) +
525 oext_alen - end_ext_alen);
528 ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
529 o_end, &start_ext, &new_ext, &end_ext);
530 out:
531 return ret;
535 * mext_calc_swap_extents - Calculate extents for extent swapping.
537 * @tmp_dext: the extent that will belong to the original inode
538 * @tmp_oext: the extent that will belong to the donor inode
539 * @orig_off: block offset of original inode
540 * @donor_off: block offset of donor inode
541 * @max_count: the maximum length of extents
543 * Return 0 on success, or a negative error value on failure.
545 static int
546 mext_calc_swap_extents(struct ext4_extent *tmp_dext,
547 struct ext4_extent *tmp_oext,
548 ext4_lblk_t orig_off, ext4_lblk_t donor_off,
549 ext4_lblk_t max_count)
551 ext4_lblk_t diff, orig_diff;
552 struct ext4_extent dext_old, oext_old;
554 BUG_ON(orig_off != donor_off);
556 /* original and donor extents have to cover the same block offset */
557 if (orig_off < le32_to_cpu(tmp_oext->ee_block) ||
558 le32_to_cpu(tmp_oext->ee_block) +
559 ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off)
560 return -ENODATA;
562 if (orig_off < le32_to_cpu(tmp_dext->ee_block) ||
563 le32_to_cpu(tmp_dext->ee_block) +
564 ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off)
565 return -ENODATA;
567 dext_old = *tmp_dext;
568 oext_old = *tmp_oext;
570 /* When tmp_dext is too large, pick up the target range. */
571 diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
573 ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
574 le32_add_cpu(&tmp_dext->ee_block, diff);
575 le16_add_cpu(&tmp_dext->ee_len, -diff);
577 if (max_count < ext4_ext_get_actual_len(tmp_dext))
578 tmp_dext->ee_len = cpu_to_le16(max_count);
580 orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
581 ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
583 /* Adjust extent length if donor extent is larger than orig */
584 if (ext4_ext_get_actual_len(tmp_dext) >
585 ext4_ext_get_actual_len(tmp_oext) - orig_diff)
586 tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
587 orig_diff);
589 tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
591 copy_extent_status(&oext_old, tmp_dext);
592 copy_extent_status(&dext_old, tmp_oext);
594 return 0;
598 * mext_check_coverage - Check that all extents in range has the same type
600 * @inode: inode in question
601 * @from: block offset of inode
602 * @count: block count to be checked
603 * @uninit: extents expected to be uninitialized
604 * @err: pointer to save error value
606 * Return 1 if all extents in range has expected type, and zero otherwise.
608 static int
609 mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
610 int uninit, int *err)
612 struct ext4_ext_path *path = NULL;
613 struct ext4_extent *ext;
614 ext4_lblk_t last = from + count;
615 while (from < last) {
616 *err = get_ext_path(inode, from, &path);
617 if (*err)
618 return 0;
619 ext = path[ext_depth(inode)].p_ext;
620 if (!ext) {
621 ext4_ext_drop_refs(path);
622 return 0;
624 if (uninit != ext4_ext_is_uninitialized(ext)) {
625 ext4_ext_drop_refs(path);
626 return 0;
628 from += ext4_ext_get_actual_len(ext);
629 ext4_ext_drop_refs(path);
631 return 1;
635 * mext_replace_branches - Replace original extents with new extents
637 * @handle: journal handle
638 * @orig_inode: original inode
639 * @donor_inode: donor inode
640 * @from: block offset of orig_inode
641 * @count: block count to be replaced
642 * @err: pointer to save return value
644 * Replace original inode extents and donor inode extents page by page.
645 * We implement this replacement in the following three steps:
646 * 1. Save the block information of original and donor inodes into
647 * dummy extents.
648 * 2. Change the block information of original inode to point at the
649 * donor inode blocks.
650 * 3. Change the block information of donor inode to point at the saved
651 * original inode blocks in the dummy extents.
653 * Return replaced block count.
655 static int
656 mext_replace_branches(handle_t *handle, struct inode *orig_inode,
657 struct inode *donor_inode, ext4_lblk_t from,
658 ext4_lblk_t count, int *err)
660 struct ext4_ext_path *orig_path = NULL;
661 struct ext4_ext_path *donor_path = NULL;
662 struct ext4_extent *oext, *dext;
663 struct ext4_extent tmp_dext, tmp_oext;
664 ext4_lblk_t orig_off = from, donor_off = from;
665 int depth;
666 int replaced_count = 0;
667 int dext_alen;
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;
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;
682 depth = ext_depth(donor_inode);
683 dext = donor_path[depth].p_ext;
684 tmp_dext = *dext;
686 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
687 donor_off, count);
688 if (*err)
689 goto out;
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;
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;
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;
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;
726 /* Already moved the expected blocks */
727 if (replaced_count >= count)
728 break;
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;
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;
748 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
749 donor_off, count - replaced_count);
750 if (*err)
751 goto out;
754 out:
755 if (orig_path) {
756 ext4_ext_drop_refs(orig_path);
757 kfree(orig_path);
759 if (donor_path) {
760 ext4_ext_drop_refs(donor_path);
761 kfree(donor_path);
764 ext4_ext_invalidate_cache(orig_inode);
765 ext4_ext_invalidate_cache(donor_inode);
767 return replaced_count;
771 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
773 * @inode1: the inode structure
774 * @inode2: the inode structure
775 * @index: page index
776 * @page: result page vector
778 * Grab two locked pages for inode's by inode order
780 static int
781 mext_page_double_lock(struct inode *inode1, struct inode *inode2,
782 pgoff_t index, struct page *page[2])
784 struct address_space *mapping[2];
785 unsigned fl = AOP_FLAG_NOFS;
787 BUG_ON(!inode1 || !inode2);
788 if (inode1 < inode2) {
789 mapping[0] = inode1->i_mapping;
790 mapping[1] = inode2->i_mapping;
791 } else {
792 mapping[0] = inode2->i_mapping;
793 mapping[1] = inode1->i_mapping;
796 page[0] = grab_cache_page_write_begin(mapping[0], index, fl);
797 if (!page[0])
798 return -ENOMEM;
800 page[1] = grab_cache_page_write_begin(mapping[1], index, fl);
801 if (!page[1]) {
802 unlock_page(page[0]);
803 page_cache_release(page[0]);
804 return -ENOMEM;
807 if (inode1 > inode2) {
808 struct page *tmp;
809 tmp = page[0];
810 page[0] = page[1];
811 page[1] = tmp;
813 return 0;
816 /* Force page buffers uptodate w/o dropping page's lock */
817 static int
818 mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
820 struct inode *inode = page->mapping->host;
821 sector_t block;
822 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
823 unsigned int blocksize, block_start, block_end;
824 int i, err, nr = 0, partial = 0;
825 BUG_ON(!PageLocked(page));
826 BUG_ON(PageWriteback(page));
828 if (PageUptodate(page))
829 return 0;
831 blocksize = 1 << inode->i_blkbits;
832 if (!page_has_buffers(page))
833 create_empty_buffers(page, blocksize, 0);
835 head = page_buffers(page);
836 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
837 for (bh = head, block_start = 0; bh != head || !block_start;
838 block++, block_start = block_end, bh = bh->b_this_page) {
839 block_end = block_start + blocksize;
840 if (block_end <= from || block_start >= to) {
841 if (!buffer_uptodate(bh))
842 partial = 1;
843 continue;
845 if (buffer_uptodate(bh))
846 continue;
847 if (!buffer_mapped(bh)) {
848 int err = 0;
849 err = ext4_get_block(inode, block, bh, 0);
850 if (err) {
851 SetPageError(page);
852 return err;
854 if (!buffer_mapped(bh)) {
855 zero_user(page, block_start, blocksize);
856 if (!err)
857 set_buffer_uptodate(bh);
858 continue;
861 BUG_ON(nr >= MAX_BUF_PER_PAGE);
862 arr[nr++] = bh;
864 /* No io required */
865 if (!nr)
866 goto out;
868 for (i = 0; i < nr; i++) {
869 bh = arr[i];
870 if (!bh_uptodate_or_lock(bh)) {
871 err = bh_submit_read(bh);
872 if (err)
873 return err;
876 out:
877 if (!partial)
878 SetPageUptodate(page);
879 return 0;
883 * move_extent_per_page - Move extent data per page
885 * @o_filp: file structure of original file
886 * @donor_inode: donor inode
887 * @orig_page_offset: page index on original file
888 * @data_offset_in_page: block index where data swapping starts
889 * @block_len_in_page: the number of blocks to be swapped
890 * @uninit: orig extent is uninitialized or not
891 * @err: pointer to save return value
893 * Save the data in original inode blocks and replace original inode extents
894 * with donor inode extents by calling mext_replace_branches().
895 * Finally, write out the saved data in new original inode blocks. Return
896 * replaced block count.
898 static int
899 move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
900 pgoff_t orig_page_offset, int data_offset_in_page,
901 int block_len_in_page, int uninit, int *err)
903 struct inode *orig_inode = o_filp->f_dentry->d_inode;
904 struct page *pagep[2] = {NULL, NULL};
905 handle_t *handle;
906 ext4_lblk_t orig_blk_offset;
907 long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
908 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
909 unsigned int w_flags = 0;
910 unsigned int tmp_data_size, data_size, replaced_size;
911 int err2, jblocks, retries = 0;
912 int replaced_count = 0;
913 int from = data_offset_in_page << orig_inode->i_blkbits;
914 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
917 * It needs twice the amount of ordinary journal buffers because
918 * inode and donor_inode may change each different metadata blocks.
920 again:
921 *err = 0;
922 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
923 handle = ext4_journal_start(orig_inode, jblocks);
924 if (IS_ERR(handle)) {
925 *err = PTR_ERR(handle);
926 return 0;
929 if (segment_eq(get_fs(), KERNEL_DS))
930 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
932 orig_blk_offset = orig_page_offset * blocks_per_page +
933 data_offset_in_page;
935 offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
937 /* Calculate data_size */
938 if ((orig_blk_offset + block_len_in_page - 1) ==
939 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
940 /* Replace the last block */
941 tmp_data_size = orig_inode->i_size & (blocksize - 1);
943 * If data_size equal zero, it shows data_size is multiples of
944 * blocksize. So we set appropriate value.
946 if (tmp_data_size == 0)
947 tmp_data_size = blocksize;
949 data_size = tmp_data_size +
950 ((block_len_in_page - 1) << orig_inode->i_blkbits);
951 } else
952 data_size = block_len_in_page << orig_inode->i_blkbits;
954 replaced_size = data_size;
956 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
957 pagep);
958 if (unlikely(*err < 0))
959 goto stop_journal;
961 * If orig extent was uninitialized it can become initialized
962 * at any time after i_data_sem was dropped, in order to
963 * serialize with delalloc we have recheck extent while we
964 * hold page's lock, if it is still the case data copy is not
965 * necessary, just swap data blocks between orig and donor.
967 if (uninit) {
968 double_down_write_data_sem(orig_inode, donor_inode);
969 /* If any of extents in range became initialized we have to
970 * fallback to data copying */
971 uninit = mext_check_coverage(orig_inode, orig_blk_offset,
972 block_len_in_page, 1, err);
973 if (*err)
974 goto drop_data_sem;
976 uninit &= mext_check_coverage(donor_inode, orig_blk_offset,
977 block_len_in_page, 1, err);
978 if (*err)
979 goto drop_data_sem;
981 if (!uninit) {
982 double_up_write_data_sem(orig_inode, donor_inode);
983 goto data_copy;
985 if ((page_has_private(pagep[0]) &&
986 !try_to_release_page(pagep[0], 0)) ||
987 (page_has_private(pagep[1]) &&
988 !try_to_release_page(pagep[1], 0))) {
989 *err = -EBUSY;
990 goto drop_data_sem;
992 replaced_count = mext_replace_branches(handle, orig_inode,
993 donor_inode, orig_blk_offset,
994 block_len_in_page, err);
995 drop_data_sem:
996 double_up_write_data_sem(orig_inode, donor_inode);
997 goto unlock_pages;
999 data_copy:
1000 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
1001 if (*err)
1002 goto unlock_pages;
1004 /* At this point all buffers in range are uptodate, old mapping layout
1005 * is no longer required, try to drop it now. */
1006 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
1007 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
1008 *err = -EBUSY;
1009 goto unlock_pages;
1012 replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
1013 orig_blk_offset,
1014 block_len_in_page, err);
1015 if (*err) {
1016 if (replaced_count) {
1017 block_len_in_page = replaced_count;
1018 replaced_size =
1019 block_len_in_page << orig_inode->i_blkbits;
1020 } else
1021 goto unlock_pages;
1023 /* Perform all necessary steps similar write_begin()/write_end()
1024 * but keeping in mind that i_size will not change */
1025 *err = __block_write_begin(pagep[0], from, from + replaced_size,
1026 ext4_get_block);
1027 if (!*err)
1028 *err = block_commit_write(pagep[0], from, from + replaced_size);
1030 if (unlikely(*err < 0))
1031 goto repair_branches;
1033 /* Even in case of data=writeback it is reasonable to pin
1034 * inode to transaction, to prevent unexpected data loss */
1035 *err = ext4_jbd2_file_inode(handle, orig_inode);
1037 unlock_pages:
1038 unlock_page(pagep[0]);
1039 page_cache_release(pagep[0]);
1040 unlock_page(pagep[1]);
1041 page_cache_release(pagep[1]);
1042 stop_journal:
1043 ext4_journal_stop(handle);
1044 /* Buffer was busy because probably is pinned to journal transaction,
1045 * force transaction commit may help to free it. */
1046 if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
1047 &retries))
1048 goto again;
1049 return replaced_count;
1051 repair_branches:
1053 * This should never ever happen!
1054 * Extents are swapped already, but we are not able to copy data.
1055 * Try to swap extents to it's original places
1057 double_down_write_data_sem(orig_inode, donor_inode);
1058 replaced_count = mext_replace_branches(handle, donor_inode, orig_inode,
1059 orig_blk_offset,
1060 block_len_in_page, &err2);
1061 double_up_write_data_sem(orig_inode, donor_inode);
1062 if (replaced_count != block_len_in_page) {
1063 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
1064 "Unable to copy data block,"
1065 " data will be lost.");
1066 *err = -EIO;
1068 replaced_count = 0;
1069 goto unlock_pages;
1073 * mext_check_arguments - Check whether move extent can be done
1075 * @orig_inode: original inode
1076 * @donor_inode: donor inode
1077 * @orig_start: logical start offset in block for orig
1078 * @donor_start: logical start offset in block for donor
1079 * @len: the number of blocks to be moved
1081 * Check the arguments of ext4_move_extents() whether the files can be
1082 * exchanged with each other.
1083 * Return 0 on success, or a negative error value on failure.
1085 static int
1086 mext_check_arguments(struct inode *orig_inode,
1087 struct inode *donor_inode, __u64 orig_start,
1088 __u64 donor_start, __u64 *len)
1090 ext4_lblk_t orig_blocks, donor_blocks;
1091 unsigned int blkbits = orig_inode->i_blkbits;
1092 unsigned int blocksize = 1 << blkbits;
1094 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
1095 ext4_debug("ext4 move extent: suid or sgid is set"
1096 " to donor file [ino:orig %lu, donor %lu]\n",
1097 orig_inode->i_ino, donor_inode->i_ino);
1098 return -EINVAL;
1101 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
1102 return -EPERM;
1104 /* Ext4 move extent does not support swapfile */
1105 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
1106 ext4_debug("ext4 move extent: The argument files should "
1107 "not be swapfile [ino:orig %lu, donor %lu]\n",
1108 orig_inode->i_ino, donor_inode->i_ino);
1109 return -EINVAL;
1112 /* Ext4 move extent supports only extent based file */
1113 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
1114 ext4_debug("ext4 move extent: orig file is not extents "
1115 "based file [ino:orig %lu]\n", orig_inode->i_ino);
1116 return -EOPNOTSUPP;
1117 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
1118 ext4_debug("ext4 move extent: donor file is not extents "
1119 "based file [ino:donor %lu]\n", donor_inode->i_ino);
1120 return -EOPNOTSUPP;
1123 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
1124 ext4_debug("ext4 move extent: File size is 0 byte\n");
1125 return -EINVAL;
1128 /* Start offset should be same */
1129 if (orig_start != donor_start) {
1130 ext4_debug("ext4 move extent: orig and donor's start "
1131 "offset are not same [ino:orig %lu, donor %lu]\n",
1132 orig_inode->i_ino, donor_inode->i_ino);
1133 return -EINVAL;
1136 if ((orig_start >= EXT_MAX_BLOCKS) ||
1137 (*len > EXT_MAX_BLOCKS) ||
1138 (orig_start + *len >= EXT_MAX_BLOCKS)) {
1139 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
1140 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
1141 orig_inode->i_ino, donor_inode->i_ino);
1142 return -EINVAL;
1145 if (orig_inode->i_size > donor_inode->i_size) {
1146 donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1147 /* TODO: eliminate this artificial restriction */
1148 if (orig_start >= donor_blocks) {
1149 ext4_debug("ext4 move extent: orig start offset "
1150 "[%llu] should be less than donor file blocks "
1151 "[%u] [ino:orig %lu, donor %lu]\n",
1152 orig_start, donor_blocks,
1153 orig_inode->i_ino, donor_inode->i_ino);
1154 return -EINVAL;
1157 /* TODO: eliminate this artificial restriction */
1158 if (orig_start + *len > donor_blocks) {
1159 ext4_debug("ext4 move extent: End offset [%llu] should "
1160 "be less than donor file blocks [%u]."
1161 "So adjust length from %llu to %llu "
1162 "[ino:orig %lu, donor %lu]\n",
1163 orig_start + *len, donor_blocks,
1164 *len, donor_blocks - orig_start,
1165 orig_inode->i_ino, donor_inode->i_ino);
1166 *len = donor_blocks - orig_start;
1168 } else {
1169 orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1170 if (orig_start >= orig_blocks) {
1171 ext4_debug("ext4 move extent: start offset [%llu] "
1172 "should be less than original file blocks "
1173 "[%u] [ino:orig %lu, donor %lu]\n",
1174 orig_start, orig_blocks,
1175 orig_inode->i_ino, donor_inode->i_ino);
1176 return -EINVAL;
1179 if (orig_start + *len > orig_blocks) {
1180 ext4_debug("ext4 move extent: Adjust length "
1181 "from %llu to %llu. Because it should be "
1182 "less than original file blocks "
1183 "[ino:orig %lu, donor %lu]\n",
1184 *len, orig_blocks - orig_start,
1185 orig_inode->i_ino, donor_inode->i_ino);
1186 *len = orig_blocks - orig_start;
1190 if (!*len) {
1191 ext4_debug("ext4 move extent: len should not be 0 "
1192 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1193 donor_inode->i_ino);
1194 return -EINVAL;
1197 return 0;
1201 * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1203 * @inode1: the inode structure
1204 * @inode2: the inode structure
1206 * Lock two inodes' i_mutex
1208 static void
1209 mext_inode_double_lock(struct inode *inode1, struct inode *inode2)
1211 BUG_ON(inode1 == inode2);
1212 if (inode1 < inode2) {
1213 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1214 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1215 } else {
1216 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1217 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1222 * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1224 * @inode1: the inode that is released first
1225 * @inode2: the inode that is released second
1229 static void
1230 mext_inode_double_unlock(struct inode *inode1, struct inode *inode2)
1232 mutex_unlock(&inode1->i_mutex);
1233 mutex_unlock(&inode2->i_mutex);
1237 * ext4_move_extents - Exchange the specified range of a file
1239 * @o_filp: file structure of the original file
1240 * @d_filp: file structure of the donor file
1241 * @orig_start: start offset in block for orig
1242 * @donor_start: start offset in block for donor
1243 * @len: the number of blocks to be moved
1244 * @moved_len: moved block length
1246 * This function returns 0 and moved block length is set in moved_len
1247 * if succeed, otherwise returns error value.
1249 * Note: ext4_move_extents() proceeds the following order.
1250 * 1:ext4_move_extents() calculates the last block number of moving extent
1251 * function by the start block number (orig_start) and the number of blocks
1252 * to be moved (len) specified as arguments.
1253 * If the {orig, donor}_start points a hole, the extent's start offset
1254 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1255 * after hole behind.
1256 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1257 * or the ext_cur exceeds the block_end which is last logical block number.
1258 * 3:To get the length of continues area, call mext_next_extent()
1259 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1260 * until find un-continuous extent, the start logical block number exceeds
1261 * the block_end or the extent points to the last extent.
1262 * 4:Exchange the original inode data with donor inode data
1263 * from orig_page_offset to seq_end_page.
1264 * The start indexes of data are specified as arguments.
1265 * That of the original inode is orig_page_offset,
1266 * and the donor inode is also orig_page_offset
1267 * (To easily handle blocksize != pagesize case, the offset for the
1268 * donor inode is block unit).
1269 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1270 * then returns to step 2.
1271 * 6:Release holecheck_path, orig_path and set the len to moved_len
1272 * which shows the number of moved blocks.
1273 * The moved_len is useful for the command to calculate the file offset
1274 * for starting next move extent ioctl.
1275 * 7:Return 0 on success, or a negative error value on failure.
1278 ext4_move_extents(struct file *o_filp, struct file *d_filp,
1279 __u64 orig_start, __u64 donor_start, __u64 len,
1280 __u64 *moved_len)
1282 struct inode *orig_inode = o_filp->f_dentry->d_inode;
1283 struct inode *donor_inode = d_filp->f_dentry->d_inode;
1284 struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1285 struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1286 ext4_lblk_t block_start = orig_start;
1287 ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1288 ext4_lblk_t rest_blocks;
1289 pgoff_t orig_page_offset = 0, seq_end_page;
1290 int ret, depth, last_extent = 0;
1291 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1292 int data_offset_in_page;
1293 int block_len_in_page;
1294 int uninit;
1296 if (orig_inode->i_sb != donor_inode->i_sb) {
1297 ext4_debug("ext4 move extent: The argument files "
1298 "should be in same FS [ino:orig %lu, donor %lu]\n",
1299 orig_inode->i_ino, donor_inode->i_ino);
1300 return -EINVAL;
1303 /* orig and donor should be different inodes */
1304 if (orig_inode == donor_inode) {
1305 ext4_debug("ext4 move extent: The argument files should not "
1306 "be same inode [ino:orig %lu, donor %lu]\n",
1307 orig_inode->i_ino, donor_inode->i_ino);
1308 return -EINVAL;
1311 /* Regular file check */
1312 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
1313 ext4_debug("ext4 move extent: The argument files should be "
1314 "regular file [ino:orig %lu, donor %lu]\n",
1315 orig_inode->i_ino, donor_inode->i_ino);
1316 return -EINVAL;
1318 /* TODO: This is non obvious task to swap blocks for inodes with full
1319 jornaling enabled */
1320 if (ext4_should_journal_data(orig_inode) ||
1321 ext4_should_journal_data(donor_inode)) {
1322 return -EINVAL;
1324 /* Protect orig and donor inodes against a truncate */
1325 mext_inode_double_lock(orig_inode, donor_inode);
1327 /* Wait for all existing dio workers */
1328 ext4_inode_block_unlocked_dio(orig_inode);
1329 ext4_inode_block_unlocked_dio(donor_inode);
1330 inode_dio_wait(orig_inode);
1331 inode_dio_wait(donor_inode);
1333 /* Protect extent tree against block allocations via delalloc */
1334 double_down_write_data_sem(orig_inode, donor_inode);
1335 /* Check the filesystem environment whether move_extent can be done */
1336 ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
1337 donor_start, &len);
1338 if (ret)
1339 goto out;
1341 file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1342 block_end = block_start + len - 1;
1343 if (file_end < block_end)
1344 len -= block_end - file_end;
1346 ret = get_ext_path(orig_inode, block_start, &orig_path);
1347 if (ret)
1348 goto out;
1350 /* Get path structure to check the hole */
1351 ret = get_ext_path(orig_inode, block_start, &holecheck_path);
1352 if (ret)
1353 goto out;
1355 depth = ext_depth(orig_inode);
1356 ext_cur = holecheck_path[depth].p_ext;
1359 * Get proper starting location of block replacement if block_start was
1360 * within the hole.
1362 if (le32_to_cpu(ext_cur->ee_block) +
1363 ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
1365 * The hole exists between extents or the tail of
1366 * original file.
1368 last_extent = mext_next_extent(orig_inode,
1369 holecheck_path, &ext_cur);
1370 if (last_extent < 0) {
1371 ret = last_extent;
1372 goto out;
1374 last_extent = mext_next_extent(orig_inode, orig_path,
1375 &ext_dummy);
1376 if (last_extent < 0) {
1377 ret = last_extent;
1378 goto out;
1380 seq_start = le32_to_cpu(ext_cur->ee_block);
1381 } else if (le32_to_cpu(ext_cur->ee_block) > block_start)
1382 /* The hole exists at the beginning of original file. */
1383 seq_start = le32_to_cpu(ext_cur->ee_block);
1384 else
1385 seq_start = block_start;
1387 /* No blocks within the specified range. */
1388 if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1389 ext4_debug("ext4 move extent: The specified range of file "
1390 "may be the hole\n");
1391 ret = -EINVAL;
1392 goto out;
1395 /* Adjust start blocks */
1396 add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1397 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1398 max(le32_to_cpu(ext_cur->ee_block), block_start);
1400 while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1401 seq_blocks += add_blocks;
1403 /* Adjust tail blocks */
1404 if (seq_start + seq_blocks - 1 > block_end)
1405 seq_blocks = block_end - seq_start + 1;
1407 ext_prev = ext_cur;
1408 last_extent = mext_next_extent(orig_inode, holecheck_path,
1409 &ext_cur);
1410 if (last_extent < 0) {
1411 ret = last_extent;
1412 break;
1414 add_blocks = ext4_ext_get_actual_len(ext_cur);
1417 * Extend the length of contiguous block (seq_blocks)
1418 * if extents are contiguous.
1420 if (ext4_can_extents_be_merged(orig_inode,
1421 ext_prev, ext_cur) &&
1422 block_end >= le32_to_cpu(ext_cur->ee_block) &&
1423 !last_extent)
1424 continue;
1426 /* Is original extent is uninitialized */
1427 uninit = ext4_ext_is_uninitialized(ext_prev);
1429 data_offset_in_page = seq_start % blocks_per_page;
1432 * Calculate data blocks count that should be swapped
1433 * at the first page.
1435 if (data_offset_in_page + seq_blocks > blocks_per_page) {
1436 /* Swapped blocks are across pages */
1437 block_len_in_page =
1438 blocks_per_page - data_offset_in_page;
1439 } else {
1440 /* Swapped blocks are in a page */
1441 block_len_in_page = seq_blocks;
1444 orig_page_offset = seq_start >>
1445 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1446 seq_end_page = (seq_start + seq_blocks - 1) >>
1447 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1448 seq_start = le32_to_cpu(ext_cur->ee_block);
1449 rest_blocks = seq_blocks;
1452 * Up semaphore to avoid following problems:
1453 * a. transaction deadlock among ext4_journal_start,
1454 * ->write_begin via pagefault, and jbd2_journal_commit
1455 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1456 * in move_extent_per_page
1458 double_up_write_data_sem(orig_inode, donor_inode);
1460 while (orig_page_offset <= seq_end_page) {
1462 /* Swap original branches with new branches */
1463 block_len_in_page = move_extent_per_page(
1464 o_filp, donor_inode,
1465 orig_page_offset,
1466 data_offset_in_page,
1467 block_len_in_page, uninit,
1468 &ret);
1470 /* Count how many blocks we have exchanged */
1471 *moved_len += block_len_in_page;
1472 if (ret < 0)
1473 break;
1474 if (*moved_len > len) {
1475 EXT4_ERROR_INODE(orig_inode,
1476 "We replaced blocks too much! "
1477 "sum of replaced: %llu requested: %llu",
1478 *moved_len, len);
1479 ret = -EIO;
1480 break;
1483 orig_page_offset++;
1484 data_offset_in_page = 0;
1485 rest_blocks -= block_len_in_page;
1486 if (rest_blocks > blocks_per_page)
1487 block_len_in_page = blocks_per_page;
1488 else
1489 block_len_in_page = rest_blocks;
1492 double_down_write_data_sem(orig_inode, donor_inode);
1493 if (ret < 0)
1494 break;
1496 /* Decrease buffer counter */
1497 if (holecheck_path)
1498 ext4_ext_drop_refs(holecheck_path);
1499 ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
1500 if (ret)
1501 break;
1502 depth = holecheck_path->p_depth;
1504 /* Decrease buffer counter */
1505 if (orig_path)
1506 ext4_ext_drop_refs(orig_path);
1507 ret = get_ext_path(orig_inode, seq_start, &orig_path);
1508 if (ret)
1509 break;
1511 ext_cur = holecheck_path[depth].p_ext;
1512 add_blocks = ext4_ext_get_actual_len(ext_cur);
1513 seq_blocks = 0;
1516 out:
1517 if (*moved_len) {
1518 ext4_discard_preallocations(orig_inode);
1519 ext4_discard_preallocations(donor_inode);
1522 if (orig_path) {
1523 ext4_ext_drop_refs(orig_path);
1524 kfree(orig_path);
1526 if (holecheck_path) {
1527 ext4_ext_drop_refs(holecheck_path);
1528 kfree(holecheck_path);
1530 double_up_write_data_sem(orig_inode, donor_inode);
1531 ext4_inode_resume_unlocked_dio(orig_inode);
1532 ext4_inode_resume_unlocked_dio(donor_inode);
1533 mext_inode_double_unlock(orig_inode, donor_inode);
1535 return ret;