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 <linux/slab.h>
19 #include "ext4_jbd2.h"
20 #include "ext4_extents.h"
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
34 get_ext_path(struct inode
*inode
, ext4_lblk_t lblock
,
35 struct ext4_ext_path
**path
)
39 *path
= ext4_ext_find_extent(inode
, lblock
, *path
);
43 } else if ((*path
)[ext_depth(inode
)].p_ext
== NULL
)
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
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
);
61 dest
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(dest
));
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
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
;
85 if (EXT_LAST_EXTENT(path
[ppos
].p_hdr
) > path
[ppos
].p_ext
) {
87 *extent
= ++path
[ppos
].p_ext
;
88 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
93 if (EXT_LAST_INDEX(path
[ppos
].p_hdr
) >
99 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
100 if (path
[ppos
+1].p_bh
)
101 brelse(path
[ppos
+1].p_bh
);
103 sb_bread(inode
->i_sb
, path
[ppos
].p_block
);
104 if (!path
[ppos
+1].p_bh
)
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 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
)
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 */
133 path
[leaf_ppos
].p_ext
= *extent
=
134 EXT_FIRST_EXTENT(path
[leaf_ppos
].p_hdr
);
135 path
[leaf_ppos
].p_block
=
136 ext_pblock(path
[leaf_ppos
].p_ext
);
140 /* We found the last extent */
145 * mext_check_null_inode - NULL check for two inodes
147 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
150 mext_check_null_inode(struct inode
*inode1
, struct inode
*inode2
,
151 const char *function
)
155 if (inode1
== NULL
) {
156 __ext4_error(inode2
->i_sb
, function
,
157 "Both inodes should not be NULL: "
158 "inode1 NULL inode2 %lu", inode2
->i_ino
);
160 } else if (inode2
== NULL
) {
161 __ext4_error(inode1
->i_sb
, function
,
162 "Both inodes should not be NULL: "
163 "inode1 %lu inode2 NULL", inode1
->i_ino
);
170 * double_down_write_data_sem - Acquire two inodes' write lock of i_data_sem
172 * @orig_inode: original inode structure
173 * @donor_inode: donor inode structure
174 * Acquire write lock of i_data_sem of the two inodes (orig and donor) by
178 double_down_write_data_sem(struct inode
*orig_inode
, struct inode
*donor_inode
)
180 struct inode
*first
= orig_inode
, *second
= donor_inode
;
183 * Use the inode number to provide the stable locking order instead
184 * of its address, because the C language doesn't guarantee you can
185 * compare pointers that don't come from the same array.
187 if (donor_inode
->i_ino
< orig_inode
->i_ino
) {
192 down_write(&EXT4_I(first
)->i_data_sem
);
193 down_write_nested(&EXT4_I(second
)->i_data_sem
, SINGLE_DEPTH_NESTING
);
197 * double_up_write_data_sem - Release two inodes' write lock of i_data_sem
199 * @orig_inode: original inode structure to be released its lock first
200 * @donor_inode: donor inode structure to be released its lock second
201 * Release write lock of i_data_sem of two inodes (orig and donor).
204 double_up_write_data_sem(struct inode
*orig_inode
, struct inode
*donor_inode
)
206 up_write(&EXT4_I(orig_inode
)->i_data_sem
);
207 up_write(&EXT4_I(donor_inode
)->i_data_sem
);
211 * mext_insert_across_blocks - Insert extents across leaf block
213 * @handle: journal handle
214 * @orig_inode: original inode
215 * @o_start: first original extent to be changed
216 * @o_end: last original extent to be changed
217 * @start_ext: first new extent to be inserted
218 * @new_ext: middle of new extent to be inserted
219 * @end_ext: last new extent to be inserted
221 * Allocate a new leaf block and insert extents into it. Return 0 on success,
222 * or a negative error value on failure.
225 mext_insert_across_blocks(handle_t
*handle
, struct inode
*orig_inode
,
226 struct ext4_extent
*o_start
, struct ext4_extent
*o_end
,
227 struct ext4_extent
*start_ext
, struct ext4_extent
*new_ext
,
228 struct ext4_extent
*end_ext
)
230 struct ext4_ext_path
*orig_path
= NULL
;
231 ext4_lblk_t eblock
= 0;
236 if (start_ext
->ee_len
&& new_ext
->ee_len
&& end_ext
->ee_len
) {
237 if (o_start
== o_end
) {
239 /* start_ext new_ext end_ext
240 * donor |---------|-----------|--------|
241 * orig |------------------------------|
246 /* start_ext new_ext end_ext
247 * donor |---------|----------|---------|
248 * orig |---------------|--------------|
250 o_end
->ee_block
= end_ext
->ee_block
;
251 o_end
->ee_len
= end_ext
->ee_len
;
252 ext4_ext_store_pblock(o_end
, ext_pblock(end_ext
));
255 o_start
->ee_len
= start_ext
->ee_len
;
256 eblock
= le32_to_cpu(start_ext
->ee_block
);
259 } else if (start_ext
->ee_len
&& new_ext
->ee_len
&&
260 !end_ext
->ee_len
&& o_start
== o_end
) {
263 * donor |--------------|---------------|
264 * orig |------------------------------|
266 o_start
->ee_len
= start_ext
->ee_len
;
267 eblock
= le32_to_cpu(start_ext
->ee_block
);
270 } else if (!start_ext
->ee_len
&& new_ext
->ee_len
&&
271 end_ext
->ee_len
&& o_start
== o_end
) {
274 * donor |--------------|---------------|
275 * orig |------------------------------|
277 o_end
->ee_block
= end_ext
->ee_block
;
278 o_end
->ee_len
= end_ext
->ee_len
;
279 ext4_ext_store_pblock(o_end
, ext_pblock(end_ext
));
282 * Set 0 to the extent block if new_ext was
285 if (new_ext
->ee_block
)
286 eblock
= le32_to_cpu(new_ext
->ee_block
);
290 ext4_debug("ext4 move extent: Unexpected insert case\n");
295 err
= get_ext_path(orig_inode
, eblock
, &orig_path
);
299 if (ext4_ext_insert_extent(handle
, orig_inode
,
300 orig_path
, new_ext
, 0))
305 err
= get_ext_path(orig_inode
,
306 le32_to_cpu(end_ext
->ee_block
) - 1, &orig_path
);
310 if (ext4_ext_insert_extent(handle
, orig_inode
,
311 orig_path
, end_ext
, 0))
316 ext4_ext_drop_refs(orig_path
);
325 * mext_insert_inside_block - Insert new extent to the extent block
327 * @o_start: first original extent to be moved
328 * @o_end: last original extent to be moved
329 * @start_ext: first new extent to be inserted
330 * @new_ext: middle of new extent to be inserted
331 * @end_ext: last new extent to be inserted
332 * @eh: extent header of target leaf block
333 * @range_to_move: used to decide how to insert extent
335 * Insert extents into the leaf block. The extent (@o_start) is overwritten
336 * by inserted extents.
339 mext_insert_inside_block(struct ext4_extent
*o_start
,
340 struct ext4_extent
*o_end
,
341 struct ext4_extent
*start_ext
,
342 struct ext4_extent
*new_ext
,
343 struct ext4_extent
*end_ext
,
344 struct ext4_extent_header
*eh
,
350 /* Move the existing extents */
351 if (range_to_move
&& o_end
< EXT_LAST_EXTENT(eh
)) {
352 len
= (unsigned long)(EXT_LAST_EXTENT(eh
) + 1) -
353 (unsigned long)(o_end
+ 1);
354 memmove(o_end
+ 1 + range_to_move
, o_end
+ 1, len
);
357 /* Insert start entry */
358 if (start_ext
->ee_len
)
359 o_start
[i
++].ee_len
= start_ext
->ee_len
;
361 /* Insert new entry */
362 if (new_ext
->ee_len
) {
363 o_start
[i
] = *new_ext
;
364 ext4_ext_store_pblock(&o_start
[i
++], ext_pblock(new_ext
));
367 /* Insert end entry */
369 o_start
[i
] = *end_ext
;
371 /* Increment the total entries counter on the extent block */
372 le16_add_cpu(&eh
->eh_entries
, range_to_move
);
376 * mext_insert_extents - Insert new extent
378 * @handle: journal handle
379 * @orig_inode: original inode
380 * @orig_path: path indicates first extent to be changed
381 * @o_start: first original extent to be changed
382 * @o_end: last original extent to be changed
383 * @start_ext: first new extent to be inserted
384 * @new_ext: middle of new extent to be inserted
385 * @end_ext: last new extent to be inserted
387 * Call the function to insert extents. If we cannot add more extents into
388 * the leaf block, we call mext_insert_across_blocks() to create a
389 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
390 * on success, or a negative error value on failure.
393 mext_insert_extents(handle_t
*handle
, struct inode
*orig_inode
,
394 struct ext4_ext_path
*orig_path
,
395 struct ext4_extent
*o_start
,
396 struct ext4_extent
*o_end
,
397 struct ext4_extent
*start_ext
,
398 struct ext4_extent
*new_ext
,
399 struct ext4_extent
*end_ext
)
401 struct ext4_extent_header
*eh
;
402 unsigned long need_slots
, slots_range
;
403 int range_to_move
, depth
, ret
;
406 * The extents need to be inserted
407 * start_extent + new_extent + end_extent.
409 need_slots
= (start_ext
->ee_len
? 1 : 0) + (end_ext
->ee_len
? 1 : 0) +
410 (new_ext
->ee_len
? 1 : 0);
412 /* The number of slots between start and end */
413 slots_range
= ((unsigned long)(o_end
+ 1) - (unsigned long)o_start
+ 1)
414 / sizeof(struct ext4_extent
);
416 /* Range to move the end of extent */
417 range_to_move
= need_slots
- slots_range
;
418 depth
= orig_path
->p_depth
;
420 eh
= orig_path
->p_hdr
;
423 /* Register to journal */
424 ret
= ext4_journal_get_write_access(handle
, orig_path
->p_bh
);
430 if (range_to_move
> 0 &&
431 (range_to_move
> le16_to_cpu(eh
->eh_max
)
432 - le16_to_cpu(eh
->eh_entries
))) {
434 ret
= mext_insert_across_blocks(handle
, orig_inode
, o_start
,
435 o_end
, start_ext
, new_ext
, end_ext
);
439 mext_insert_inside_block(o_start
, o_end
, start_ext
, new_ext
,
440 end_ext
, eh
, range_to_move
);
443 ret
= ext4_handle_dirty_metadata(handle
, orig_inode
,
448 ret
= ext4_mark_inode_dirty(handle
, orig_inode
);
457 * mext_leaf_block - Move one leaf extent block into the inode.
459 * @handle: journal handle
460 * @orig_inode: original inode
461 * @orig_path: path indicates first extent to be changed
462 * @dext: donor extent
463 * @from: start offset on the target file
465 * In order to insert extents into the leaf block, we must divide the extent
466 * in the leaf block into three extents. The one is located to be inserted
467 * extents, and the others are located around it.
469 * Therefore, this function creates structures to save extents of the leaf
470 * block, and inserts extents by calling mext_insert_extents() with
471 * created extents. Return 0 on success, or a negative error value on failure.
474 mext_leaf_block(handle_t
*handle
, struct inode
*orig_inode
,
475 struct ext4_ext_path
*orig_path
, struct ext4_extent
*dext
,
478 struct ext4_extent
*oext
, *o_start
, *o_end
, *prev_ext
;
479 struct ext4_extent new_ext
, start_ext
, end_ext
;
480 ext4_lblk_t new_ext_end
;
481 int oext_alen
, new_ext_alen
, end_ext_alen
;
482 int depth
= ext_depth(orig_inode
);
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;
489 new_ext
.ee_block
= cpu_to_le32(*from
);
490 ext4_ext_store_pblock(&new_ext
, 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;
496 * Case: original extent is first
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
)) {
511 * We can merge new_ext into previous extent,
512 * if these are contiguous and same extent type.
514 if (ext4_can_extents_be_merged(orig_inode
, prev_ext
,
517 start_ext
.ee_len
= cpu_to_le16(
518 ext4_ext_get_actual_len(prev_ext
) +
520 start_ext
.ee_block
= oext
->ee_block
;
521 copy_extent_status(prev_ext
, &start_ext
);
527 * Case: new_ext_end must be less than oext
531 if (le32_to_cpu(oext
->ee_block
) + oext_alen
- 1 < new_ext_end
) {
532 ext4_error(orig_inode
->i_sb
,
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
),
542 * Case: new_ext is smaller than original extent
543 * oext |---------------|
544 * new_ext |-----------|
547 if (le32_to_cpu(oext
->ee_block
) <= new_ext_end
&&
548 new_ext_end
< le32_to_cpu(oext
->ee_block
) + oext_alen
- 1) {
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 (ext_pblock(o_end
) + oext_alen
- end_ext_alen
));
557 cpu_to_le32(le32_to_cpu(o_end
->ee_block
) +
558 oext_alen
- end_ext_alen
);
561 ret
= mext_insert_extents(handle
, orig_inode
, orig_path
, o_start
,
562 o_end
, &start_ext
, &new_ext
, &end_ext
);
568 * mext_calc_swap_extents - Calculate extents for extent swapping.
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
576 * Return 0 on success, or a negative error value on failure.
579 mext_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
)
584 ext4_lblk_t diff
, orig_diff
;
585 struct ext4_extent dext_old
, oext_old
;
587 BUG_ON(orig_off
!= donor_off
);
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
)
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
)
600 dext_old
= *tmp_dext
;
601 oext_old
= *tmp_oext
;
603 /* When tmp_dext is too large, pick up the target range. */
604 diff
= donor_off
- le32_to_cpu(tmp_dext
->ee_block
);
606 ext4_ext_store_pblock(tmp_dext
, ext_pblock(tmp_dext
) + diff
);
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
);
611 if (max_count
< ext4_ext_get_actual_len(tmp_dext
))
612 tmp_dext
->ee_len
= cpu_to_le16(max_count
);
614 orig_diff
= orig_off
- le32_to_cpu(tmp_oext
->ee_block
);
615 ext4_ext_store_pblock(tmp_oext
, ext_pblock(tmp_oext
) + orig_diff
);
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
) -
623 tmp_oext
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(tmp_dext
));
625 copy_extent_status(&oext_old
, tmp_dext
);
626 copy_extent_status(&dext_old
, tmp_oext
);
632 * mext_replace_branches - Replace original extents with new extents
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
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
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.
650 * Return replaced block count.
653 mext_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
)
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
;
663 int replaced_count
= 0;
666 /* Protect extent trees against block allocations via delalloc */
667 double_down_write_data_sem(orig_inode
, donor_inode
);
669 /* Get the original extent for the block "orig_off" */
670 *err
= get_ext_path(orig_inode
, orig_off
, &orig_path
);
674 /* Get the donor extent for the head */
675 *err
= get_ext_path(donor_inode
, donor_off
, &donor_path
);
678 depth
= ext_depth(orig_inode
);
679 oext
= orig_path
[depth
].p_ext
;
682 depth
= ext_depth(donor_inode
);
683 dext
= donor_path
[depth
].p_ext
;
686 *err
= mext_calc_swap_extents(&tmp_dext
, &tmp_oext
, orig_off
,
691 /* Loop for the donor extents */
693 /* The extent for donor must be found. */
695 ext4_error(donor_inode
->i_sb
,
696 "The extent for donor must be found");
699 } else if (donor_off
!= le32_to_cpu(tmp_dext
.ee_block
)) {
700 ext4_error(donor_inode
->i_sb
,
701 "Donor offset(%u) and the first block of donor "
702 "extent(%u) should be equal",
704 le32_to_cpu(tmp_dext
.ee_block
));
709 /* Set donor extent to orig extent */
710 *err
= mext_leaf_block(handle
, orig_inode
,
711 orig_path
, &tmp_dext
, &orig_off
);
715 /* Set orig extent to donor extent */
716 *err
= mext_leaf_block(handle
, donor_inode
,
717 donor_path
, &tmp_oext
, &donor_off
);
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
)
731 ext4_ext_drop_refs(orig_path
);
732 *err
= get_ext_path(orig_inode
, orig_off
, &orig_path
);
735 depth
= ext_depth(orig_inode
);
736 oext
= orig_path
[depth
].p_ext
;
740 ext4_ext_drop_refs(donor_path
);
741 *err
= get_ext_path(donor_inode
, donor_off
, &donor_path
);
744 depth
= ext_depth(donor_inode
);
745 dext
= donor_path
[depth
].p_ext
;
748 *err
= mext_calc_swap_extents(&tmp_dext
, &tmp_oext
, orig_off
,
749 donor_off
, count
- replaced_count
);
756 ext4_ext_drop_refs(orig_path
);
760 ext4_ext_drop_refs(donor_path
);
764 ext4_ext_invalidate_cache(orig_inode
);
765 ext4_ext_invalidate_cache(donor_inode
);
767 double_up_write_data_sem(orig_inode
, donor_inode
);
769 return replaced_count
;
773 * move_extent_per_page - Move extent data per page
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
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.
789 move_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
)
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
;
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
;
807 int replaced_count
= 0;
808 int blocks_per_page
= PAGE_CACHE_SIZE
>> orig_inode
->i_blkbits
;
811 * It needs twice the amount of ordinary journal buffers because
812 * inode and donor_inode may change each different metadata blocks.
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
);
821 if (segment_eq(get_fs(), KERNEL_DS
))
822 w_flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
824 orig_blk_offset
= orig_page_offset
* blocks_per_page
+
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.
834 replaced_count
= mext_replace_branches(handle
, orig_inode
,
835 donor_inode
, orig_blk_offset
,
836 block_len_in_page
, err
);
840 offs
= (long long)orig_blk_offset
<< orig_inode
->i_blkbits
;
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);
848 * If data_size equal zero, it shows data_size is multiples of
849 * blocksize. So we set appropriate value.
851 if (tmp_data_size
== 0)
852 tmp_data_size
= blocksize
;
854 data_size
= tmp_data_size
+
855 ((block_len_in_page
- 1) << orig_inode
->i_blkbits
);
857 data_size
= block_len_in_page
<< orig_inode
->i_blkbits
;
859 replaced_size
= data_size
;
861 *err
= a_ops
->write_begin(o_filp
, mapping
, offs
, data_size
, w_flags
,
863 if (unlikely(*err
< 0))
866 if (!PageUptodate(page
)) {
867 mapping
->a_ops
->readpage(o_filp
, page
);
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.
878 if (PageWriteback(page
))
879 wait_on_page_writeback(page
);
881 /* Release old bh and drop refs */
882 try_to_release_page(page
, 0);
884 replaced_count
= mext_replace_branches(handle
, orig_inode
, donor_inode
,
885 orig_blk_offset
, block_len_in_page
,
888 if (replaced_count
) {
889 block_len_in_page
= replaced_count
;
891 block_len_in_page
<< orig_inode
->i_blkbits
;
896 if (!page_has_buffers(page
))
897 create_empty_buffers(page
, 1 << orig_inode
->i_blkbits
, 0);
899 bh
= page_buffers(page
);
900 for (i
= 0; i
< data_offset_in_page
; i
++)
901 bh
= bh
->b_this_page
;
903 for (i
= 0; i
< block_len_in_page
; i
++) {
904 *err
= ext4_get_block(orig_inode
,
905 (sector_t
)(orig_blk_offset
+ i
), bh
, 0);
909 if (bh
->b_this_page
!= NULL
)
910 bh
= bh
->b_this_page
;
913 *err
= a_ops
->write_end(o_filp
, mapping
, offs
, data_size
, replaced_size
,
918 if (unlikely(page
)) {
919 if (PageLocked(page
))
921 page_cache_release(page
);
922 ext4_journal_stop(handle
);
925 ext4_journal_stop(handle
);
930 return replaced_count
;
934 * mext_check_arguments - Check whether move extent can be done
936 * @orig_inode: original inode
937 * @donor_inode: donor inode
938 * @orig_start: logical start offset in block for orig
939 * @donor_start: logical start offset in block for donor
940 * @len: the number of blocks to be moved
942 * Check the arguments of ext4_move_extents() whether the files can be
943 * exchanged with each other.
944 * Return 0 on success, or a negative error value on failure.
947 mext_check_arguments(struct inode
*orig_inode
,
948 struct inode
*donor_inode
, __u64 orig_start
,
949 __u64 donor_start
, __u64
*len
)
951 ext4_lblk_t orig_blocks
, donor_blocks
;
952 unsigned int blkbits
= orig_inode
->i_blkbits
;
953 unsigned int blocksize
= 1 << blkbits
;
955 if (donor_inode
->i_mode
& (S_ISUID
|S_ISGID
)) {
956 ext4_debug("ext4 move extent: suid or sgid is set"
957 " to donor file [ino:orig %lu, donor %lu]\n",
958 orig_inode
->i_ino
, donor_inode
->i_ino
);
962 /* Ext4 move extent does not support swapfile */
963 if (IS_SWAPFILE(orig_inode
) || IS_SWAPFILE(donor_inode
)) {
964 ext4_debug("ext4 move extent: The argument files should "
965 "not be swapfile [ino:orig %lu, donor %lu]\n",
966 orig_inode
->i_ino
, donor_inode
->i_ino
);
970 /* Files should be in the same ext4 FS */
971 if (orig_inode
->i_sb
!= donor_inode
->i_sb
) {
972 ext4_debug("ext4 move extent: The argument files "
973 "should be in same FS [ino:orig %lu, donor %lu]\n",
974 orig_inode
->i_ino
, donor_inode
->i_ino
);
978 /* Ext4 move extent supports only extent based file */
979 if (!(EXT4_I(orig_inode
)->i_flags
& EXT4_EXTENTS_FL
)) {
980 ext4_debug("ext4 move extent: orig file is not extents "
981 "based file [ino:orig %lu]\n", orig_inode
->i_ino
);
983 } else if (!(EXT4_I(donor_inode
)->i_flags
& EXT4_EXTENTS_FL
)) {
984 ext4_debug("ext4 move extent: donor file is not extents "
985 "based file [ino:donor %lu]\n", donor_inode
->i_ino
);
989 if ((!orig_inode
->i_size
) || (!donor_inode
->i_size
)) {
990 ext4_debug("ext4 move extent: File size is 0 byte\n");
994 /* Start offset should be same */
995 if (orig_start
!= donor_start
) {
996 ext4_debug("ext4 move extent: orig and donor's start "
997 "offset are not same [ino:orig %lu, donor %lu]\n",
998 orig_inode
->i_ino
, donor_inode
->i_ino
);
1002 if ((orig_start
> EXT_MAX_BLOCK
) ||
1003 (donor_start
> EXT_MAX_BLOCK
) ||
1004 (*len
> EXT_MAX_BLOCK
) ||
1005 (orig_start
+ *len
> EXT_MAX_BLOCK
)) {
1006 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
1007 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCK
,
1008 orig_inode
->i_ino
, donor_inode
->i_ino
);
1012 if (orig_inode
->i_size
> donor_inode
->i_size
) {
1013 donor_blocks
= (donor_inode
->i_size
+ blocksize
- 1) >> blkbits
;
1014 /* TODO: eliminate this artificial restriction */
1015 if (orig_start
>= donor_blocks
) {
1016 ext4_debug("ext4 move extent: orig start offset "
1017 "[%llu] should be less than donor file blocks "
1018 "[%u] [ino:orig %lu, donor %lu]\n",
1019 orig_start
, donor_blocks
,
1020 orig_inode
->i_ino
, donor_inode
->i_ino
);
1024 /* TODO: eliminate this artificial restriction */
1025 if (orig_start
+ *len
> donor_blocks
) {
1026 ext4_debug("ext4 move extent: End offset [%llu] should "
1027 "be less than donor file blocks [%u]."
1028 "So adjust length from %llu to %llu "
1029 "[ino:orig %lu, donor %lu]\n",
1030 orig_start
+ *len
, donor_blocks
,
1031 *len
, donor_blocks
- orig_start
,
1032 orig_inode
->i_ino
, donor_inode
->i_ino
);
1033 *len
= donor_blocks
- orig_start
;
1036 orig_blocks
= (orig_inode
->i_size
+ blocksize
- 1) >> blkbits
;
1037 if (orig_start
>= orig_blocks
) {
1038 ext4_debug("ext4 move extent: start offset [%llu] "
1039 "should be less than original file blocks "
1040 "[%u] [ino:orig %lu, donor %lu]\n",
1041 orig_start
, orig_blocks
,
1042 orig_inode
->i_ino
, donor_inode
->i_ino
);
1046 if (orig_start
+ *len
> orig_blocks
) {
1047 ext4_debug("ext4 move extent: Adjust length "
1048 "from %llu to %llu. Because it should be "
1049 "less than original file blocks "
1050 "[ino:orig %lu, donor %lu]\n",
1051 *len
, orig_blocks
- orig_start
,
1052 orig_inode
->i_ino
, donor_inode
->i_ino
);
1053 *len
= orig_blocks
- orig_start
;
1058 ext4_debug("ext4 move extent: len should not be 0 "
1059 "[ino:orig %lu, donor %lu]\n", orig_inode
->i_ino
,
1060 donor_inode
->i_ino
);
1068 * mext_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1070 * @inode1: the inode structure
1071 * @inode2: the inode structure
1073 * Lock two inodes' i_mutex by i_ino order.
1074 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
1077 mext_inode_double_lock(struct inode
*inode1
, struct inode
*inode2
)
1081 BUG_ON(inode1
== NULL
&& inode2
== NULL
);
1083 ret
= mext_check_null_inode(inode1
, inode2
, __func__
);
1087 if (inode1
== inode2
) {
1088 mutex_lock(&inode1
->i_mutex
);
1092 if (inode1
->i_ino
< inode2
->i_ino
) {
1093 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
1094 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
1096 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_PARENT
);
1097 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_CHILD
);
1105 * mext_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1107 * @inode1: the inode that is released first
1108 * @inode2: the inode that is released second
1110 * If inode1 or inode2 is NULL, return -EIO. Otherwise, return 0.
1114 mext_inode_double_unlock(struct inode
*inode1
, struct inode
*inode2
)
1118 BUG_ON(inode1
== NULL
&& inode2
== NULL
);
1120 ret
= mext_check_null_inode(inode1
, inode2
, __func__
);
1125 mutex_unlock(&inode1
->i_mutex
);
1127 if (inode2
&& inode2
!= inode1
)
1128 mutex_unlock(&inode2
->i_mutex
);
1135 * ext4_move_extents - Exchange the specified range of a file
1137 * @o_filp: file structure of the original file
1138 * @d_filp: file structure of the donor file
1139 * @orig_start: start offset in block for orig
1140 * @donor_start: start offset in block for donor
1141 * @len: the number of blocks to be moved
1142 * @moved_len: moved block length
1144 * This function returns 0 and moved block length is set in moved_len
1145 * if succeed, otherwise returns error value.
1147 * Note: ext4_move_extents() proceeds the following order.
1148 * 1:ext4_move_extents() calculates the last block number of moving extent
1149 * function by the start block number (orig_start) and the number of blocks
1150 * to be moved (len) specified as arguments.
1151 * If the {orig, donor}_start points a hole, the extent's start offset
1152 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1153 * after hole behind.
1154 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1155 * or the ext_cur exceeds the block_end which is last logical block number.
1156 * 3:To get the length of continues area, call mext_next_extent()
1157 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1158 * until find un-continuous extent, the start logical block number exceeds
1159 * the block_end or the extent points to the last extent.
1160 * 4:Exchange the original inode data with donor inode data
1161 * from orig_page_offset to seq_end_page.
1162 * The start indexes of data are specified as arguments.
1163 * That of the original inode is orig_page_offset,
1164 * and the donor inode is also orig_page_offset
1165 * (To easily handle blocksize != pagesize case, the offset for the
1166 * donor inode is block unit).
1167 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1168 * then returns to step 2.
1169 * 6:Release holecheck_path, orig_path and set the len to moved_len
1170 * which shows the number of moved blocks.
1171 * The moved_len is useful for the command to calculate the file offset
1172 * for starting next move extent ioctl.
1173 * 7:Return 0 on success, or a negative error value on failure.
1176 ext4_move_extents(struct file
*o_filp
, struct file
*d_filp
,
1177 __u64 orig_start
, __u64 donor_start
, __u64 len
,
1180 struct inode
*orig_inode
= o_filp
->f_dentry
->d_inode
;
1181 struct inode
*donor_inode
= d_filp
->f_dentry
->d_inode
;
1182 struct ext4_ext_path
*orig_path
= NULL
, *holecheck_path
= NULL
;
1183 struct ext4_extent
*ext_prev
, *ext_cur
, *ext_dummy
;
1184 ext4_lblk_t block_start
= orig_start
;
1185 ext4_lblk_t block_end
, seq_start
, add_blocks
, file_end
, seq_blocks
= 0;
1186 ext4_lblk_t rest_blocks
;
1187 pgoff_t orig_page_offset
= 0, seq_end_page
;
1188 int ret1
, ret2
, depth
, last_extent
= 0;
1189 int blocks_per_page
= PAGE_CACHE_SIZE
>> orig_inode
->i_blkbits
;
1190 int data_offset_in_page
;
1191 int block_len_in_page
;
1194 /* orig and donor should be different file */
1195 if (orig_inode
->i_ino
== donor_inode
->i_ino
) {
1196 ext4_debug("ext4 move extent: The argument files should not "
1197 "be same file [ino:orig %lu, donor %lu]\n",
1198 orig_inode
->i_ino
, donor_inode
->i_ino
);
1202 /* Regular file check */
1203 if (!S_ISREG(orig_inode
->i_mode
) || !S_ISREG(donor_inode
->i_mode
)) {
1204 ext4_debug("ext4 move extent: The argument files should be "
1205 "regular file [ino:orig %lu, donor %lu]\n",
1206 orig_inode
->i_ino
, donor_inode
->i_ino
);
1210 /* Protect orig and donor inodes against a truncate */
1211 ret1
= mext_inode_double_lock(orig_inode
, donor_inode
);
1215 /* Protect extent tree against block allocations via delalloc */
1216 double_down_write_data_sem(orig_inode
, donor_inode
);
1217 /* Check the filesystem environment whether move_extent can be done */
1218 ret1
= mext_check_arguments(orig_inode
, donor_inode
, orig_start
,
1223 file_end
= (i_size_read(orig_inode
) - 1) >> orig_inode
->i_blkbits
;
1224 block_end
= block_start
+ len
- 1;
1225 if (file_end
< block_end
)
1226 len
-= block_end
- file_end
;
1228 ret1
= get_ext_path(orig_inode
, block_start
, &orig_path
);
1232 /* Get path structure to check the hole */
1233 ret1
= get_ext_path(orig_inode
, block_start
, &holecheck_path
);
1237 depth
= ext_depth(orig_inode
);
1238 ext_cur
= holecheck_path
[depth
].p_ext
;
1241 * Get proper starting location of block replacement if block_start was
1244 if (le32_to_cpu(ext_cur
->ee_block
) +
1245 ext4_ext_get_actual_len(ext_cur
) - 1 < block_start
) {
1247 * The hole exists between extents or the tail of
1250 last_extent
= mext_next_extent(orig_inode
,
1251 holecheck_path
, &ext_cur
);
1252 if (last_extent
< 0) {
1256 last_extent
= mext_next_extent(orig_inode
, orig_path
,
1258 if (last_extent
< 0) {
1262 seq_start
= le32_to_cpu(ext_cur
->ee_block
);
1263 } else if (le32_to_cpu(ext_cur
->ee_block
) > block_start
)
1264 /* The hole exists at the beginning of original file. */
1265 seq_start
= le32_to_cpu(ext_cur
->ee_block
);
1267 seq_start
= block_start
;
1269 /* No blocks within the specified range. */
1270 if (le32_to_cpu(ext_cur
->ee_block
) > block_end
) {
1271 ext4_debug("ext4 move extent: The specified range of file "
1272 "may be the hole\n");
1277 /* Adjust start blocks */
1278 add_blocks
= min(le32_to_cpu(ext_cur
->ee_block
) +
1279 ext4_ext_get_actual_len(ext_cur
), block_end
+ 1) -
1280 max(le32_to_cpu(ext_cur
->ee_block
), block_start
);
1282 while (!last_extent
&& le32_to_cpu(ext_cur
->ee_block
) <= block_end
) {
1283 seq_blocks
+= add_blocks
;
1285 /* Adjust tail blocks */
1286 if (seq_start
+ seq_blocks
- 1 > block_end
)
1287 seq_blocks
= block_end
- seq_start
+ 1;
1290 last_extent
= mext_next_extent(orig_inode
, holecheck_path
,
1292 if (last_extent
< 0) {
1296 add_blocks
= ext4_ext_get_actual_len(ext_cur
);
1299 * Extend the length of contiguous block (seq_blocks)
1300 * if extents are contiguous.
1302 if (ext4_can_extents_be_merged(orig_inode
,
1303 ext_prev
, ext_cur
) &&
1304 block_end
>= le32_to_cpu(ext_cur
->ee_block
) &&
1308 /* Is original extent is uninitialized */
1309 uninit
= ext4_ext_is_uninitialized(ext_prev
);
1311 data_offset_in_page
= seq_start
% blocks_per_page
;
1314 * Calculate data blocks count that should be swapped
1315 * at the first page.
1317 if (data_offset_in_page
+ seq_blocks
> blocks_per_page
) {
1318 /* Swapped blocks are across pages */
1320 blocks_per_page
- data_offset_in_page
;
1322 /* Swapped blocks are in a page */
1323 block_len_in_page
= seq_blocks
;
1326 orig_page_offset
= seq_start
>>
1327 (PAGE_CACHE_SHIFT
- orig_inode
->i_blkbits
);
1328 seq_end_page
= (seq_start
+ seq_blocks
- 1) >>
1329 (PAGE_CACHE_SHIFT
- orig_inode
->i_blkbits
);
1330 seq_start
= le32_to_cpu(ext_cur
->ee_block
);
1331 rest_blocks
= seq_blocks
;
1334 * Up semaphore to avoid following problems:
1335 * a. transaction deadlock among ext4_journal_start,
1336 * ->write_begin via pagefault, and jbd2_journal_commit
1337 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1338 * in move_extent_per_page
1340 double_up_write_data_sem(orig_inode
, donor_inode
);
1342 while (orig_page_offset
<= seq_end_page
) {
1344 /* Swap original branches with new branches */
1345 block_len_in_page
= move_extent_per_page(
1346 o_filp
, donor_inode
,
1348 data_offset_in_page
,
1349 block_len_in_page
, uninit
,
1352 /* Count how many blocks we have exchanged */
1353 *moved_len
+= block_len_in_page
;
1356 if (*moved_len
> len
) {
1357 ext4_error(orig_inode
->i_sb
,
1358 "We replaced blocks too much! "
1359 "sum of replaced: %llu requested: %llu",
1366 data_offset_in_page
= 0;
1367 rest_blocks
-= block_len_in_page
;
1368 if (rest_blocks
> blocks_per_page
)
1369 block_len_in_page
= blocks_per_page
;
1371 block_len_in_page
= rest_blocks
;
1374 double_down_write_data_sem(orig_inode
, donor_inode
);
1378 /* Decrease buffer counter */
1380 ext4_ext_drop_refs(holecheck_path
);
1381 ret1
= get_ext_path(orig_inode
, seq_start
, &holecheck_path
);
1384 depth
= holecheck_path
->p_depth
;
1386 /* Decrease buffer counter */
1388 ext4_ext_drop_refs(orig_path
);
1389 ret1
= get_ext_path(orig_inode
, seq_start
, &orig_path
);
1393 ext_cur
= holecheck_path
[depth
].p_ext
;
1394 add_blocks
= ext4_ext_get_actual_len(ext_cur
);
1400 ext4_discard_preallocations(orig_inode
);
1401 ext4_discard_preallocations(donor_inode
);
1405 ext4_ext_drop_refs(orig_path
);
1408 if (holecheck_path
) {
1409 ext4_ext_drop_refs(holecheck_path
);
1410 kfree(holecheck_path
);
1412 double_up_write_data_sem(orig_inode
, donor_inode
);
1413 ret2
= mext_inode_double_unlock(orig_inode
, donor_inode
);