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.
17 #include <linux/quotaops.h>
18 #include "ext4_jbd2.h"
19 #include "ext4_extents.h"
23 * get_ext_path - Find an extent path for designated logical block number.
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)
29 * ext4_ext_find_extent wrapper. Return 0 on success, or a negative error value
33 get_ext_path(struct inode
*inode
, ext4_lblk_t lblock
,
34 struct ext4_ext_path
**path
)
38 *path
= ext4_ext_find_extent(inode
, lblock
, *path
);
42 } else if ((*path
)[ext_depth(inode
)].p_ext
== NULL
)
49 * copy_extent_status - Copy the extent's initialization status
51 * @src: an extent for getting initialize status
52 * @dest: an extent to be set the status
55 copy_extent_status(struct ext4_extent
*src
, struct ext4_extent
*dest
)
57 if (ext4_ext_is_uninitialized(src
))
58 ext4_ext_mark_uninitialized(dest
);
60 dest
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(dest
));
64 * mext_next_extent - Search for the next extent and set it to "extent"
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
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
77 mext_next_extent(struct inode
*inode
, struct ext4_ext_path
*path
,
78 struct ext4_extent
**extent
)
80 struct ext4_extent_header
*eh
;
81 int ppos
, leaf_ppos
= path
->p_depth
;
84 if (EXT_LAST_EXTENT(path
[ppos
].p_hdr
) > path
[ppos
].p_ext
) {
86 *extent
= ++path
[ppos
].p_ext
;
87 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
92 if (EXT_LAST_INDEX(path
[ppos
].p_hdr
) >
98 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
99 if (path
[ppos
+1].p_bh
)
100 brelse(path
[ppos
+1].p_bh
);
102 sb_bread(inode
->i_sb
, path
[ppos
].p_block
);
103 if (!path
[ppos
+1].p_bh
)
106 ext_block_hdr(path
[ppos
+1].p_bh
);
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 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
)
120 path
[cur_ppos
+1].p_hdr
=
121 ext_block_hdr(path
[cur_ppos
+1].p_bh
);
124 path
[leaf_ppos
].p_ext
= *extent
= NULL
;
126 eh
= path
[leaf_ppos
].p_hdr
;
127 if (le16_to_cpu(eh
->eh_entries
) == 0)
128 /* empty leaf is found */
132 path
[leaf_ppos
].p_ext
= *extent
=
133 EXT_FIRST_EXTENT(path
[leaf_ppos
].p_hdr
);
134 path
[leaf_ppos
].p_block
=
135 ext_pblock(path
[leaf_ppos
].p_ext
);
139 /* We found the last extent */
144 * mext_check_null_inode - NULL check for two inodes
146 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
149 mext_check_null_inode(struct inode
*inode1
, struct inode
*inode2
,
150 const char *function
)
154 if (inode1
== NULL
) {
155 __ext4_error(inode2
->i_sb
, function
,
156 "Both inodes should not be NULL: "
157 "inode1 NULL inode2 %lu", inode2
->i_ino
);
159 } else if (inode2
== NULL
) {
160 __ext4_error(inode1
->i_sb
, function
,
161 "Both inodes should not be NULL: "
162 "inode1 %lu inode2 NULL", inode1
->i_ino
);
169 * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem
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
177 double_down_write_data_sem(struct inode
*orig_inode
, struct inode
*donor_inode
)
179 struct inode
*first
= orig_inode
, *second
= donor_inode
;
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.
186 if (donor_inode
->i_ino
< orig_inode
->i_ino
) {
191 down_write(&EXT4_I(first
)->i_data_sem
);
192 down_write_nested(&EXT4_I(second
)->i_data_sem
, SINGLE_DEPTH_NESTING
);
196 * double_up_write_data_sem - Release two inodes' write lock of i_data_sem
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).
203 double_up_write_data_sem(struct inode
*orig_inode
, struct inode
*donor_inode
)
205 up_write(&EXT4_I(orig_inode
)->i_data_sem
);
206 up_write(&EXT4_I(donor_inode
)->i_data_sem
);
210 * mext_insert_across_blocks - Insert extents across leaf block
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
220 * Allocate a new leaf block and insert extents into it. Return 0 on success,
221 * or a negative error value on failure.
224 mext_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
)
229 struct ext4_ext_path
*orig_path
= NULL
;
230 ext4_lblk_t eblock
= 0;
235 if (start_ext
->ee_len
&& new_ext
->ee_len
&& end_ext
->ee_len
) {
236 if (o_start
== o_end
) {
238 /* start_ext new_ext end_ext
239 * donor |---------|-----------|--------|
240 * orig |------------------------------|
245 /* start_ext new_ext end_ext
246 * donor |---------|----------|---------|
247 * orig |---------------|--------------|
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
, ext_pblock(end_ext
));
254 o_start
->ee_len
= start_ext
->ee_len
;
255 eblock
= le32_to_cpu(start_ext
->ee_block
);
258 } else if (start_ext
->ee_len
&& new_ext
->ee_len
&&
259 !end_ext
->ee_len
&& o_start
== o_end
) {
262 * donor |--------------|---------------|
263 * orig |------------------------------|
265 o_start
->ee_len
= start_ext
->ee_len
;
266 eblock
= le32_to_cpu(start_ext
->ee_block
);
269 } else if (!start_ext
->ee_len
&& new_ext
->ee_len
&&
270 end_ext
->ee_len
&& o_start
== o_end
) {
273 * donor |--------------|---------------|
274 * orig |------------------------------|
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
, ext_pblock(end_ext
));
281 * Set 0 to the extent block if new_ext was
284 if (new_ext
->ee_block
)
285 eblock
= le32_to_cpu(new_ext
->ee_block
);
289 ext4_debug("ext4 move extent: Unexpected insert case\n");
294 err
= get_ext_path(orig_inode
, eblock
, &orig_path
);
298 if (ext4_ext_insert_extent(handle
, orig_inode
,
299 orig_path
, new_ext
, 0))
304 err
= get_ext_path(orig_inode
,
305 le32_to_cpu(end_ext
->ee_block
) - 1, &orig_path
);
309 if (ext4_ext_insert_extent(handle
, orig_inode
,
310 orig_path
, end_ext
, 0))
315 ext4_ext_drop_refs(orig_path
);
324 * mext_insert_inside_block - Insert new extent to the extent block
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
334 * Insert extents into the leaf block. The extent (@o_start) is overwritten
335 * by inserted extents.
338 mext_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
,
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
);
356 /* Insert start entry */
357 if (start_ext
->ee_len
)
358 o_start
[i
++].ee_len
= start_ext
->ee_len
;
360 /* Insert new entry */
361 if (new_ext
->ee_len
) {
362 o_start
[i
] = *new_ext
;
363 ext4_ext_store_pblock(&o_start
[i
++], ext_pblock(new_ext
));
366 /* Insert end entry */
368 o_start
[i
] = *end_ext
;
370 /* Increment the total entries counter on the extent block */
371 le16_add_cpu(&eh
->eh_entries
, range_to_move
);
375 * mext_insert_extents - Insert new extent
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
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.
392 mext_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
)
400 struct ext4_extent_header
*eh
;
401 unsigned long need_slots
, slots_range
;
402 int range_to_move
, depth
, ret
;
405 * The extents need to be inserted
406 * start_extent + new_extent + end_extent.
408 need_slots
= (start_ext
->ee_len
? 1 : 0) + (end_ext
->ee_len
? 1 : 0) +
409 (new_ext
->ee_len
? 1 : 0);
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
);
415 /* Range to move the end of extent */
416 range_to_move
= need_slots
- slots_range
;
417 depth
= orig_path
->p_depth
;
419 eh
= orig_path
->p_hdr
;
422 /* Register to journal */
423 ret
= ext4_journal_get_write_access(handle
, orig_path
->p_bh
);
429 if (range_to_move
> 0 &&
430 (range_to_move
> le16_to_cpu(eh
->eh_max
)
431 - le16_to_cpu(eh
->eh_entries
))) {
433 ret
= mext_insert_across_blocks(handle
, orig_inode
, o_start
,
434 o_end
, start_ext
, new_ext
, end_ext
);
438 mext_insert_inside_block(o_start
, o_end
, start_ext
, new_ext
,
439 end_ext
, eh
, range_to_move
);
442 ret
= ext4_handle_dirty_metadata(handle
, orig_inode
,
447 ret
= ext4_mark_inode_dirty(handle
, orig_inode
);
456 * mext_leaf_block - Move one leaf extent block into the inode.
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
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.
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.
473 mext_leaf_block(handle_t
*handle
, struct inode
*orig_inode
,
474 struct ext4_ext_path
*orig_path
, struct ext4_extent
*dext
,
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
);
484 o_start
= o_end
= oext
= orig_path
[depth
].p_ext
;
485 oext_alen
= ext4_ext_get_actual_len(oext
);
486 start_ext
.ee_len
= end_ext
.ee_len
= 0;
488 new_ext
.ee_block
= cpu_to_le32(*from
);
489 ext4_ext_store_pblock(&new_ext
, ext_pblock(dext
));
490 new_ext
.ee_len
= dext
->ee_len
;
491 new_ext_alen
= ext4_ext_get_actual_len(&new_ext
);
492 new_ext_end
= le32_to_cpu(new_ext
.ee_block
) + new_ext_alen
- 1;
495 * Case: original extent is first
500 if (le32_to_cpu(oext
->ee_block
) < le32_to_cpu(new_ext
.ee_block
) &&
501 le32_to_cpu(new_ext
.ee_block
) <
502 le32_to_cpu(oext
->ee_block
) + oext_alen
) {
503 start_ext
.ee_len
= cpu_to_le16(le32_to_cpu(new_ext
.ee_block
) -
504 le32_to_cpu(oext
->ee_block
));
505 start_ext
.ee_block
= oext
->ee_block
;
506 copy_extent_status(oext
, &start_ext
);
507 } else if (oext
> EXT_FIRST_EXTENT(orig_path
[depth
].p_hdr
)) {
510 * We can merge new_ext into previous extent,
511 * if these are contiguous and same extent type.
513 if (ext4_can_extents_be_merged(orig_inode
, prev_ext
,
516 start_ext
.ee_len
= cpu_to_le16(
517 ext4_ext_get_actual_len(prev_ext
) +
519 start_ext
.ee_block
= oext
->ee_block
;
520 copy_extent_status(prev_ext
, &start_ext
);
526 * Case: new_ext_end must be less than oext
530 if (le32_to_cpu(oext
->ee_block
) + oext_alen
- 1 < new_ext_end
) {
531 ext4_error(orig_inode
->i_sb
,
532 "new_ext_end(%u) should be less than or equal to "
533 "oext->ee_block(%u) + oext_alen(%d) - 1",
534 new_ext_end
, le32_to_cpu(oext
->ee_block
),
541 * Case: new_ext is smaller than original extent
542 * oext |---------------|
543 * new_ext |-----------|
546 if (le32_to_cpu(oext
->ee_block
) <= new_ext_end
&&
547 new_ext_end
< le32_to_cpu(oext
->ee_block
) + oext_alen
- 1) {
549 cpu_to_le16(le32_to_cpu(oext
->ee_block
) +
550 oext_alen
- 1 - new_ext_end
);
551 copy_extent_status(oext
, &end_ext
);
552 end_ext_alen
= ext4_ext_get_actual_len(&end_ext
);
553 ext4_ext_store_pblock(&end_ext
,
554 (ext_pblock(o_end
) + oext_alen
- end_ext_alen
));
556 cpu_to_le32(le32_to_cpu(o_end
->ee_block
) +
557 oext_alen
- end_ext_alen
);
560 ret
= mext_insert_extents(handle
, orig_inode
, orig_path
, o_start
,
561 o_end
, &start_ext
, &new_ext
, &end_ext
);
567 * mext_calc_swap_extents - Calculate extents for extent swapping.
569 * @tmp_dext: the extent that will belong to the original inode
570 * @tmp_oext: the extent that will belong to the donor inode
571 * @orig_off: block offset of original inode
572 * @donor_off: block offset of donor inode
573 * @max_count: the maximum length of extents
575 * Return 0 on success, or a negative error value on failure.
578 mext_calc_swap_extents(struct ext4_extent
*tmp_dext
,
579 struct ext4_extent
*tmp_oext
,
580 ext4_lblk_t orig_off
, ext4_lblk_t donor_off
,
581 ext4_lblk_t max_count
)
583 ext4_lblk_t diff
, orig_diff
;
584 struct ext4_extent dext_old
, oext_old
;
586 BUG_ON(orig_off
!= donor_off
);
588 /* original and donor extents have to cover the same block offset */
589 if (orig_off
< le32_to_cpu(tmp_oext
->ee_block
) ||
590 le32_to_cpu(tmp_oext
->ee_block
) +
591 ext4_ext_get_actual_len(tmp_oext
) - 1 < orig_off
)
594 if (orig_off
< le32_to_cpu(tmp_dext
->ee_block
) ||
595 le32_to_cpu(tmp_dext
->ee_block
) +
596 ext4_ext_get_actual_len(tmp_dext
) - 1 < orig_off
)
599 dext_old
= *tmp_dext
;
600 oext_old
= *tmp_oext
;
602 /* When tmp_dext is too large, pick up the target range. */
603 diff
= donor_off
- le32_to_cpu(tmp_dext
->ee_block
);
605 ext4_ext_store_pblock(tmp_dext
, ext_pblock(tmp_dext
) + diff
);
607 cpu_to_le32(le32_to_cpu(tmp_dext
->ee_block
) + diff
);
608 tmp_dext
->ee_len
= cpu_to_le16(le16_to_cpu(tmp_dext
->ee_len
) - diff
);
610 if (max_count
< ext4_ext_get_actual_len(tmp_dext
))
611 tmp_dext
->ee_len
= cpu_to_le16(max_count
);
613 orig_diff
= orig_off
- le32_to_cpu(tmp_oext
->ee_block
);
614 ext4_ext_store_pblock(tmp_oext
, ext_pblock(tmp_oext
) + orig_diff
);
616 /* Adjust extent length if donor extent is larger than orig */
617 if (ext4_ext_get_actual_len(tmp_dext
) >
618 ext4_ext_get_actual_len(tmp_oext
) - orig_diff
)
619 tmp_dext
->ee_len
= cpu_to_le16(le16_to_cpu(tmp_oext
->ee_len
) -
622 tmp_oext
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(tmp_dext
));
624 copy_extent_status(&oext_old
, tmp_dext
);
625 copy_extent_status(&dext_old
, tmp_oext
);
631 * mext_replace_branches - Replace original extents with new extents
633 * @handle: journal handle
634 * @orig_inode: original inode
635 * @donor_inode: donor inode
636 * @from: block offset of orig_inode
637 * @count: block count to be replaced
638 * @err: pointer to save return value
640 * Replace original inode extents and donor inode extents page by page.
641 * We implement this replacement in the following three steps:
642 * 1. Save the block information of original and donor inodes into
644 * 2. Change the block information of original inode to point at the
645 * donor inode blocks.
646 * 3. Change the block information of donor inode to point at the saved
647 * original inode blocks in the dummy extents.
649 * Return replaced block count.
652 mext_replace_branches(handle_t
*handle
, struct inode
*orig_inode
,
653 struct inode
*donor_inode
, ext4_lblk_t from
,
654 ext4_lblk_t count
, int *err
)
656 struct ext4_ext_path
*orig_path
= NULL
;
657 struct ext4_ext_path
*donor_path
= NULL
;
658 struct ext4_extent
*oext
, *dext
;
659 struct ext4_extent tmp_dext
, tmp_oext
;
660 ext4_lblk_t orig_off
= from
, donor_off
= from
;
662 int replaced_count
= 0;
665 /* Protect extent trees against block allocations via delalloc */
666 double_down_write_data_sem(orig_inode
, donor_inode
);
668 /* Get the original extent for the block "orig_off" */
669 *err
= get_ext_path(orig_inode
, orig_off
, &orig_path
);
673 /* Get the donor extent for the head */
674 *err
= get_ext_path(donor_inode
, donor_off
, &donor_path
);
677 depth
= ext_depth(orig_inode
);
678 oext
= orig_path
[depth
].p_ext
;
681 depth
= ext_depth(donor_inode
);
682 dext
= donor_path
[depth
].p_ext
;
685 *err
= mext_calc_swap_extents(&tmp_dext
, &tmp_oext
, orig_off
,
690 /* Loop for the donor extents */
692 /* The extent for donor must be found. */
694 ext4_error(donor_inode
->i_sb
,
695 "The extent for donor must be found");
698 } else if (donor_off
!= le32_to_cpu(tmp_dext
.ee_block
)) {
699 ext4_error(donor_inode
->i_sb
,
700 "Donor offset(%u) and the first block of donor "
701 "extent(%u) should be equal",
703 le32_to_cpu(tmp_dext
.ee_block
));
708 /* Set donor extent to orig extent */
709 *err
= mext_leaf_block(handle
, orig_inode
,
710 orig_path
, &tmp_dext
, &orig_off
);
714 /* Set orig extent to donor extent */
715 *err
= mext_leaf_block(handle
, donor_inode
,
716 donor_path
, &tmp_oext
, &donor_off
);
720 dext_alen
= ext4_ext_get_actual_len(&tmp_dext
);
721 replaced_count
+= dext_alen
;
722 donor_off
+= dext_alen
;
723 orig_off
+= dext_alen
;
725 /* Already moved the expected blocks */
726 if (replaced_count
>= count
)
730 ext4_ext_drop_refs(orig_path
);
731 *err
= get_ext_path(orig_inode
, orig_off
, &orig_path
);
734 depth
= ext_depth(orig_inode
);
735 oext
= orig_path
[depth
].p_ext
;
739 ext4_ext_drop_refs(donor_path
);
740 *err
= get_ext_path(donor_inode
, donor_off
, &donor_path
);
743 depth
= ext_depth(donor_inode
);
744 dext
= donor_path
[depth
].p_ext
;
747 *err
= mext_calc_swap_extents(&tmp_dext
, &tmp_oext
, orig_off
,
748 donor_off
, count
- replaced_count
);
755 ext4_ext_drop_refs(orig_path
);
759 ext4_ext_drop_refs(donor_path
);
763 ext4_ext_invalidate_cache(orig_inode
);
764 ext4_ext_invalidate_cache(donor_inode
);
766 double_up_write_data_sem(orig_inode
, donor_inode
);
768 return replaced_count
;
772 * move_extent_per_page - Move extent data per page
774 * @o_filp: file structure of original file
775 * @donor_inode: donor inode
776 * @orig_page_offset: page index on original file
777 * @data_offset_in_page: block index where data swapping starts
778 * @block_len_in_page: the number of blocks to be swapped
779 * @uninit: orig extent is uninitialized or not
780 * @err: pointer to save return value
782 * Save the data in original inode blocks and replace original inode extents
783 * with donor inode extents by calling mext_replace_branches().
784 * Finally, write out the saved data in new original inode blocks. Return
785 * replaced block count.
788 move_extent_per_page(struct file
*o_filp
, struct inode
*donor_inode
,
789 pgoff_t orig_page_offset
, int data_offset_in_page
,
790 int block_len_in_page
, int uninit
, int *err
)
792 struct inode
*orig_inode
= o_filp
->f_dentry
->d_inode
;
793 struct address_space
*mapping
= orig_inode
->i_mapping
;
794 struct buffer_head
*bh
;
795 struct page
*page
= NULL
;
796 const struct address_space_operations
*a_ops
= mapping
->a_ops
;
798 ext4_lblk_t orig_blk_offset
;
799 long long offs
= orig_page_offset
<< PAGE_CACHE_SHIFT
;
800 unsigned long blocksize
= orig_inode
->i_sb
->s_blocksize
;
801 unsigned int w_flags
= 0;
802 unsigned int tmp_data_size
, data_size
, replaced_size
;
806 int replaced_count
= 0;
807 int blocks_per_page
= PAGE_CACHE_SIZE
>> orig_inode
->i_blkbits
;
810 * It needs twice the amount of ordinary journal buffers because
811 * inode and donor_inode may change each different metadata blocks.
813 jblocks
= ext4_writepage_trans_blocks(orig_inode
) * 2;
814 handle
= ext4_journal_start(orig_inode
, jblocks
);
815 if (IS_ERR(handle
)) {
816 *err
= PTR_ERR(handle
);
820 if (segment_eq(get_fs(), KERNEL_DS
))
821 w_flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
823 orig_blk_offset
= orig_page_offset
* blocks_per_page
+
827 * If orig extent is uninitialized one,
828 * it's not necessary force the page into memory
829 * and then force it to be written out again.
830 * Just swap data blocks between orig and donor.
833 replaced_count
= mext_replace_branches(handle
, orig_inode
,
834 donor_inode
, orig_blk_offset
,
835 block_len_in_page
, err
);
839 offs
= (long long)orig_blk_offset
<< orig_inode
->i_blkbits
;
841 /* Calculate data_size */
842 if ((orig_blk_offset
+ block_len_in_page
- 1) ==
843 ((orig_inode
->i_size
- 1) >> orig_inode
->i_blkbits
)) {
844 /* Replace the last block */
845 tmp_data_size
= orig_inode
->i_size
& (blocksize
- 1);
847 * If data_size equal zero, it shows data_size is multiples of
848 * blocksize. So we set appropriate value.
850 if (tmp_data_size
== 0)
851 tmp_data_size
= blocksize
;
853 data_size
= tmp_data_size
+
854 ((block_len_in_page
- 1) << orig_inode
->i_blkbits
);
856 data_size
= block_len_in_page
<< orig_inode
->i_blkbits
;
858 replaced_size
= data_size
;
860 *err
= a_ops
->write_begin(o_filp
, mapping
, offs
, data_size
, w_flags
,
862 if (unlikely(*err
< 0))
865 if (!PageUptodate(page
)) {
866 mapping
->a_ops
->readpage(o_filp
, page
);
871 * try_to_release_page() doesn't call releasepage in writeback mode.
872 * We should care about the order of writing to the same file
873 * by multiple move extent processes.
874 * It needs to call wait_on_page_writeback() to wait for the
875 * writeback of the page.
877 if (PageWriteback(page
))
878 wait_on_page_writeback(page
);
880 /* Release old bh and drop refs */
881 try_to_release_page(page
, 0);
883 replaced_count
= mext_replace_branches(handle
, orig_inode
, donor_inode
,
884 orig_blk_offset
, block_len_in_page
,
887 if (replaced_count
) {
888 block_len_in_page
= replaced_count
;
890 block_len_in_page
<< orig_inode
->i_blkbits
;
895 if (!page_has_buffers(page
))
896 create_empty_buffers(page
, 1 << orig_inode
->i_blkbits
, 0);
898 bh
= page_buffers(page
);
899 for (i
= 0; i
< data_offset_in_page
; i
++)
900 bh
= bh
->b_this_page
;
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);
908 if (bh
->b_this_page
!= NULL
)
909 bh
= bh
->b_this_page
;
912 *err
= a_ops
->write_end(o_filp
, mapping
, offs
, data_size
, replaced_size
,
917 if (unlikely(page
)) {
918 if (PageLocked(page
))
920 page_cache_release(page
);
921 ext4_journal_stop(handle
);
924 ext4_journal_stop(handle
);
929 return replaced_count
;
933 * mext_check_arguments - Check whether move extent can be done
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
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.
946 mext_check_arguments(struct inode
*orig_inode
,
947 struct inode
*donor_inode
, __u64 orig_start
,
948 __u64 donor_start
, __u64
*len
)
950 ext4_lblk_t orig_blocks
, donor_blocks
;
951 unsigned int blkbits
= orig_inode
->i_blkbits
;
952 unsigned int blocksize
= 1 << blkbits
;
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
);
961 /* Ext4 move extent does not support swapfile */
962 if (IS_SWAPFILE(orig_inode
) || IS_SWAPFILE(donor_inode
)) {
963 ext4_debug("ext4 move extent: The argument files should "
964 "not be swapfile [ino:orig %lu, donor %lu]\n",
965 orig_inode
->i_ino
, donor_inode
->i_ino
);
969 /* Files should be in the same ext4 FS */
970 if (orig_inode
->i_sb
!= donor_inode
->i_sb
) {
971 ext4_debug("ext4 move extent: The argument files "
972 "should be in same FS [ino:orig %lu, donor %lu]\n",
973 orig_inode
->i_ino
, donor_inode
->i_ino
);
977 /* Ext4 move extent supports only extent based file */
978 if (!(EXT4_I(orig_inode
)->i_flags
& EXT4_EXTENTS_FL
)) {
979 ext4_debug("ext4 move extent: orig file is not extents "
980 "based file [ino:orig %lu]\n", orig_inode
->i_ino
);
982 } else if (!(EXT4_I(donor_inode
)->i_flags
& EXT4_EXTENTS_FL
)) {
983 ext4_debug("ext4 move extent: donor file is not extents "
984 "based file [ino:donor %lu]\n", donor_inode
->i_ino
);
988 if ((!orig_inode
->i_size
) || (!donor_inode
->i_size
)) {
989 ext4_debug("ext4 move extent: File size is 0 byte\n");
993 /* Start offset should be same */
994 if (orig_start
!= donor_start
) {
995 ext4_debug("ext4 move extent: orig and donor's start "
996 "offset are not same [ino:orig %lu, donor %lu]\n",
997 orig_inode
->i_ino
, donor_inode
->i_ino
);
1001 if ((orig_start
> EXT_MAX_BLOCK
) ||
1002 (donor_start
> EXT_MAX_BLOCK
) ||
1003 (*len
> EXT_MAX_BLOCK
) ||
1004 (orig_start
+ *len
> EXT_MAX_BLOCK
)) {
1005 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
1006 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCK
,
1007 orig_inode
->i_ino
, donor_inode
->i_ino
);
1011 if (orig_inode
->i_size
> donor_inode
->i_size
) {
1012 donor_blocks
= (donor_inode
->i_size
+ blocksize
- 1) >> blkbits
;
1013 /* TODO: eliminate this artificial restriction */
1014 if (orig_start
>= donor_blocks
) {
1015 ext4_debug("ext4 move extent: orig start offset "
1016 "[%llu] should be less than donor file blocks "
1017 "[%u] [ino:orig %lu, donor %lu]\n",
1018 orig_start
, donor_blocks
,
1019 orig_inode
->i_ino
, donor_inode
->i_ino
);
1023 /* TODO: eliminate this artificial restriction */
1024 if (orig_start
+ *len
> donor_blocks
) {
1025 ext4_debug("ext4 move extent: End offset [%llu] should "
1026 "be less than donor file blocks [%u]."
1027 "So adjust length from %llu to %llu "
1028 "[ino:orig %lu, donor %lu]\n",
1029 orig_start
+ *len
, donor_blocks
,
1030 *len
, donor_blocks
- orig_start
,
1031 orig_inode
->i_ino
, donor_inode
->i_ino
);
1032 *len
= donor_blocks
- orig_start
;
1035 orig_blocks
= (orig_inode
->i_size
+ blocksize
- 1) >> blkbits
;
1036 if (orig_start
>= orig_blocks
) {
1037 ext4_debug("ext4 move extent: start offset [%llu] "
1038 "should be less than original file blocks "
1039 "[%u] [ino:orig %lu, donor %lu]\n",
1040 orig_start
, orig_blocks
,
1041 orig_inode
->i_ino
, donor_inode
->i_ino
);
1045 if (orig_start
+ *len
> orig_blocks
) {
1046 ext4_debug("ext4 move extent: Adjust length "
1047 "from %llu to %llu. Because it should be "
1048 "less than original file blocks "
1049 "[ino:orig %lu, donor %lu]\n",
1050 *len
, orig_blocks
- orig_start
,
1051 orig_inode
->i_ino
, donor_inode
->i_ino
);
1052 *len
= orig_blocks
- orig_start
;
1057 ext4_debug("ext4 move extent: len should not be 0 "
1058 "[ino:orig %lu, donor %lu]\n", orig_inode
->i_ino
,
1059 donor_inode
->i_ino
);
1067 * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1069 * @inode1: the inode structure
1070 * @inode2: the inode structure
1072 * Lock two inodes' i_mutex by i_ino order.
1073 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
1076 mext_inode_double_lock(struct inode
*inode1
, struct inode
*inode2
)
1080 BUG_ON(inode1
== NULL
&& inode2
== NULL
);
1082 ret
= mext_check_null_inode(inode1
, inode2
, __func__
);
1086 if (inode1
== inode2
) {
1087 mutex_lock(&inode1
->i_mutex
);
1091 if (inode1
->i_ino
< inode2
->i_ino
) {
1092 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
1093 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
1095 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_PARENT
);
1096 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_CHILD
);
1104 * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1106 * @inode1: the inode that is released first
1107 * @inode2: the inode that is released second
1109 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
1113 mext_inode_double_unlock(struct inode
*inode1
, struct inode
*inode2
)
1117 BUG_ON(inode1
== NULL
&& inode2
== NULL
);
1119 ret
= mext_check_null_inode(inode1
, inode2
, __func__
);
1124 mutex_unlock(&inode1
->i_mutex
);
1126 if (inode2
&& inode2
!= inode1
)
1127 mutex_unlock(&inode2
->i_mutex
);
1134 * ext4_move_extents - Exchange the specified range of a file
1136 * @o_filp: file structure of the original file
1137 * @d_filp: file structure of the donor file
1138 * @orig_start: start offset in block for orig
1139 * @donor_start: start offset in block for donor
1140 * @len: the number of blocks to be moved
1141 * @moved_len: moved block length
1143 * This function returns 0 and moved block length is set in moved_len
1144 * if succeed, otherwise returns error value.
1146 * Note: ext4_move_extents() proceeds the following order.
1147 * 1:ext4_move_extents() calculates the last block number of moving extent
1148 * function by the start block number (orig_start) and the number of blocks
1149 * to be moved (len) specified as arguments.
1150 * If the {orig, donor}_start points a hole, the extent's start offset
1151 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1152 * after hole behind.
1153 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1154 * or the ext_cur exceeds the block_end which is last logical block number.
1155 * 3:To get the length of continues area, call mext_next_extent()
1156 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1157 * until find un-continuous extent, the start logical block number exceeds
1158 * the block_end or the extent points to the last extent.
1159 * 4:Exchange the original inode data with donor inode data
1160 * from orig_page_offset to seq_end_page.
1161 * The start indexes of data are specified as arguments.
1162 * That of the original inode is orig_page_offset,
1163 * and the donor inode is also orig_page_offset
1164 * (To easily handle blocksize != pagesize case, the offset for the
1165 * donor inode is block unit).
1166 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1167 * then returns to step 2.
1168 * 6:Release holecheck_path, orig_path and set the len to moved_len
1169 * which shows the number of moved blocks.
1170 * The moved_len is useful for the command to calculate the file offset
1171 * for starting next move extent ioctl.
1172 * 7:Return 0 on success, or a negative error value on failure.
1175 ext4_move_extents(struct file
*o_filp
, struct file
*d_filp
,
1176 __u64 orig_start
, __u64 donor_start
, __u64 len
,
1179 struct inode
*orig_inode
= o_filp
->f_dentry
->d_inode
;
1180 struct inode
*donor_inode
= d_filp
->f_dentry
->d_inode
;
1181 struct ext4_ext_path
*orig_path
= NULL
, *holecheck_path
= NULL
;
1182 struct ext4_extent
*ext_prev
, *ext_cur
, *ext_dummy
;
1183 ext4_lblk_t block_start
= orig_start
;
1184 ext4_lblk_t block_end
, seq_start
, add_blocks
, file_end
, seq_blocks
= 0;
1185 ext4_lblk_t rest_blocks
;
1186 pgoff_t orig_page_offset
= 0, seq_end_page
;
1187 int ret1
, ret2
, depth
, last_extent
= 0;
1188 int blocks_per_page
= PAGE_CACHE_SIZE
>> orig_inode
->i_blkbits
;
1189 int data_offset_in_page
;
1190 int block_len_in_page
;
1193 /* orig and donor should be different file */
1194 if (orig_inode
->i_ino
== donor_inode
->i_ino
) {
1195 ext4_debug("ext4 move extent: The argument files should not "
1196 "be same file [ino:orig %lu, donor %lu]\n",
1197 orig_inode
->i_ino
, donor_inode
->i_ino
);
1201 /* Regular file check */
1202 if (!S_ISREG(orig_inode
->i_mode
) || !S_ISREG(donor_inode
->i_mode
)) {
1203 ext4_debug("ext4 move extent: The argument files should be "
1204 "regular file [ino:orig %lu, donor %lu]\n",
1205 orig_inode
->i_ino
, donor_inode
->i_ino
);
1209 /* Protect orig and donor inodes against a truncate */
1210 ret1
= mext_inode_double_lock(orig_inode
, donor_inode
);
1214 /* Protect extent tree against block allocations via delalloc */
1215 double_down_write_data_sem(orig_inode
, donor_inode
);
1216 /* Check the filesystem environment whether move_extent can be done */
1217 ret1
= mext_check_arguments(orig_inode
, donor_inode
, orig_start
,
1222 file_end
= (i_size_read(orig_inode
) - 1) >> orig_inode
->i_blkbits
;
1223 block_end
= block_start
+ len
- 1;
1224 if (file_end
< block_end
)
1225 len
-= block_end
- file_end
;
1227 ret1
= get_ext_path(orig_inode
, block_start
, &orig_path
);
1231 /* Get path structure to check the hole */
1232 ret1
= get_ext_path(orig_inode
, block_start
, &holecheck_path
);
1236 depth
= ext_depth(orig_inode
);
1237 ext_cur
= holecheck_path
[depth
].p_ext
;
1240 * Get proper starting location of block replacement if block_start was
1243 if (le32_to_cpu(ext_cur
->ee_block
) +
1244 ext4_ext_get_actual_len(ext_cur
) - 1 < block_start
) {
1246 * The hole exists between extents or the tail of
1249 last_extent
= mext_next_extent(orig_inode
,
1250 holecheck_path
, &ext_cur
);
1251 if (last_extent
< 0) {
1255 last_extent
= mext_next_extent(orig_inode
, orig_path
,
1257 if (last_extent
< 0) {
1261 seq_start
= le32_to_cpu(ext_cur
->ee_block
);
1262 } else if (le32_to_cpu(ext_cur
->ee_block
) > block_start
)
1263 /* The hole exists at the beginning of original file. */
1264 seq_start
= le32_to_cpu(ext_cur
->ee_block
);
1266 seq_start
= block_start
;
1268 /* No blocks within the specified range. */
1269 if (le32_to_cpu(ext_cur
->ee_block
) > block_end
) {
1270 ext4_debug("ext4 move extent: The specified range of file "
1271 "may be the hole\n");
1276 /* Adjust start blocks */
1277 add_blocks
= min(le32_to_cpu(ext_cur
->ee_block
) +
1278 ext4_ext_get_actual_len(ext_cur
), block_end
+ 1) -
1279 max(le32_to_cpu(ext_cur
->ee_block
), block_start
);
1281 while (!last_extent
&& le32_to_cpu(ext_cur
->ee_block
) <= block_end
) {
1282 seq_blocks
+= add_blocks
;
1284 /* Adjust tail blocks */
1285 if (seq_start
+ seq_blocks
- 1 > block_end
)
1286 seq_blocks
= block_end
- seq_start
+ 1;
1289 last_extent
= mext_next_extent(orig_inode
, holecheck_path
,
1291 if (last_extent
< 0) {
1295 add_blocks
= ext4_ext_get_actual_len(ext_cur
);
1298 * Extend the length of contiguous block (seq_blocks)
1299 * if extents are contiguous.
1301 if (ext4_can_extents_be_merged(orig_inode
,
1302 ext_prev
, ext_cur
) &&
1303 block_end
>= le32_to_cpu(ext_cur
->ee_block
) &&
1307 /* Is original extent is uninitialized */
1308 uninit
= ext4_ext_is_uninitialized(ext_prev
);
1310 data_offset_in_page
= seq_start
% blocks_per_page
;
1313 * Calculate data blocks count that should be swapped
1314 * at the first page.
1316 if (data_offset_in_page
+ seq_blocks
> blocks_per_page
) {
1317 /* Swapped blocks are across pages */
1319 blocks_per_page
- data_offset_in_page
;
1321 /* Swapped blocks are in a page */
1322 block_len_in_page
= seq_blocks
;
1325 orig_page_offset
= seq_start
>>
1326 (PAGE_CACHE_SHIFT
- orig_inode
->i_blkbits
);
1327 seq_end_page
= (seq_start
+ seq_blocks
- 1) >>
1328 (PAGE_CACHE_SHIFT
- orig_inode
->i_blkbits
);
1329 seq_start
= le32_to_cpu(ext_cur
->ee_block
);
1330 rest_blocks
= seq_blocks
;
1333 * Up semaphore to avoid following problems:
1334 * a. transaction deadlock among ext4_journal_start,
1335 * ->write_begin via pagefault, and jbd2_journal_commit
1336 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1337 * in move_extent_per_page
1339 double_up_write_data_sem(orig_inode
, donor_inode
);
1341 while (orig_page_offset
<= seq_end_page
) {
1343 /* Swap original branches with new branches */
1344 block_len_in_page
= move_extent_per_page(
1345 o_filp
, donor_inode
,
1347 data_offset_in_page
,
1348 block_len_in_page
, uninit
,
1351 /* Count how many blocks we have exchanged */
1352 *moved_len
+= block_len_in_page
;
1355 if (*moved_len
> len
) {
1356 ext4_error(orig_inode
->i_sb
,
1357 "We replaced blocks too much! "
1358 "sum of replaced: %llu requested: %llu",
1365 data_offset_in_page
= 0;
1366 rest_blocks
-= block_len_in_page
;
1367 if (rest_blocks
> blocks_per_page
)
1368 block_len_in_page
= blocks_per_page
;
1370 block_len_in_page
= rest_blocks
;
1373 double_down_write_data_sem(orig_inode
, donor_inode
);
1377 /* Decrease buffer counter */
1379 ext4_ext_drop_refs(holecheck_path
);
1380 ret1
= get_ext_path(orig_inode
, seq_start
, &holecheck_path
);
1383 depth
= holecheck_path
->p_depth
;
1385 /* Decrease buffer counter */
1387 ext4_ext_drop_refs(orig_path
);
1388 ret1
= get_ext_path(orig_inode
, seq_start
, &orig_path
);
1392 ext_cur
= holecheck_path
[depth
].p_ext
;
1393 add_blocks
= ext4_ext_get_actual_len(ext_cur
);
1399 ext4_discard_preallocations(orig_inode
);
1400 ext4_discard_preallocations(donor_inode
);
1404 ext4_ext_drop_refs(orig_path
);
1407 if (holecheck_path
) {
1408 ext4_ext_drop_refs(holecheck_path
);
1409 kfree(holecheck_path
);
1411 double_up_write_data_sem(orig_inode
, donor_inode
);
1412 ret2
= mext_inode_double_unlock(orig_inode
, donor_inode
);