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"
21 #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 **orig_path)
38 struct ext4_ext_path *path;
40 path = ext4_ext_find_extent(inode, lblock, *orig_path);
43 else if (path[ext_depth(inode)].p_ext == NULL)
52 * copy_extent_status - Copy the extent's initialization status
54 * @src: an extent for getting initialize status
55 * @dest: an extent to be set the status
58 copy_extent_status(struct ext4_extent *src, struct ext4_extent *dest)
60 if (ext4_ext_is_uninitialized(src))
61 ext4_ext_mark_uninitialized(dest);
63 dest->ee_len = cpu_to_le16(ext4_ext_get_actual_len(dest));
67 * mext_next_extent - Search for the next extent and set it to "extent"
69 * @inode: inode which is searched
70 * @path: this will obtain data for the next extent
71 * @extent: pointer to the next extent we have just gotten
73 * Search the next extent in the array of ext4_ext_path structure (@path)
74 * and set it to ext4_extent structure (@extent). In addition, the member of
75 * @path (->p_ext) also points the next extent. Return 0 on success, 1 if
76 * ext4_ext_path structure refers to the last extent, or a negative error
80 mext_next_extent(struct inode *inode, struct ext4_ext_path *path,
81 struct ext4_extent **extent)
83 struct ext4_extent_header *eh;
84 int ppos, leaf_ppos = path->p_depth;
87 if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
89 *extent = ++path[ppos].p_ext;
90 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
95 if (EXT_LAST_INDEX(path[ppos].p_hdr) >
101 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
102 if (path[ppos+1].p_bh)
103 brelse(path[ppos+1].p_bh);
105 sb_bread(inode->i_sb, path[ppos].p_block);
106 if (!path[ppos+1].p_bh)
109 ext_block_hdr(path[ppos+1].p_bh);
111 /* Halfway index block */
112 while (++cur_ppos < leaf_ppos) {
113 path[cur_ppos].p_idx =
114 EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
115 path[cur_ppos].p_block =
116 ext4_idx_pblock(path[cur_ppos].p_idx);
117 if (path[cur_ppos+1].p_bh)
118 brelse(path[cur_ppos+1].p_bh);
119 path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
120 path[cur_ppos].p_block);
121 if (!path[cur_ppos+1].p_bh)
123 path[cur_ppos+1].p_hdr =
124 ext_block_hdr(path[cur_ppos+1].p_bh);
127 path[leaf_ppos].p_ext = *extent = NULL;
129 eh = path[leaf_ppos].p_hdr;
130 if (le16_to_cpu(eh->eh_entries) == 0)
131 /* empty leaf is found */
135 path[leaf_ppos].p_ext = *extent =
136 EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
137 path[leaf_ppos].p_block =
138 ext4_ext_pblock(path[leaf_ppos].p_ext);
142 /* We found the last extent */
147 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
150 * Acquire write lock of i_data_sem of the two inodes
153 ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
155 if (first < second) {
156 down_write(&EXT4_I(first)->i_data_sem);
157 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
159 down_write(&EXT4_I(second)->i_data_sem);
160 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
166 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
168 * @orig_inode: original inode structure to be released its lock first
169 * @donor_inode: donor inode structure to be released its lock second
170 * Release write lock of i_data_sem of two inodes (orig and donor).
173 ext4_double_up_write_data_sem(struct inode *orig_inode,
174 struct inode *donor_inode)
176 up_write(&EXT4_I(orig_inode)->i_data_sem);
177 up_write(&EXT4_I(donor_inode)->i_data_sem);
181 * mext_insert_across_blocks - Insert extents across leaf block
183 * @handle: journal handle
184 * @orig_inode: original inode
185 * @o_start: first original extent to be changed
186 * @o_end: last original extent to be changed
187 * @start_ext: first new extent to be inserted
188 * @new_ext: middle of new extent to be inserted
189 * @end_ext: last new extent to be inserted
191 * Allocate a new leaf block and insert extents into it. Return 0 on success,
192 * or a negative error value on failure.
195 mext_insert_across_blocks(handle_t *handle, struct inode *orig_inode,
196 struct ext4_extent *o_start, struct ext4_extent *o_end,
197 struct ext4_extent *start_ext, struct ext4_extent *new_ext,
198 struct ext4_extent *end_ext)
200 struct ext4_ext_path *orig_path = NULL;
201 ext4_lblk_t eblock = 0;
206 if (start_ext->ee_len && new_ext->ee_len && end_ext->ee_len) {
207 if (o_start == o_end) {
209 /* start_ext new_ext end_ext
210 * donor |---------|-----------|--------|
211 * orig |------------------------------|
216 /* start_ext new_ext end_ext
217 * donor |---------|----------|---------|
218 * orig |---------------|--------------|
220 o_end->ee_block = end_ext->ee_block;
221 o_end->ee_len = end_ext->ee_len;
222 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
225 o_start->ee_len = start_ext->ee_len;
226 eblock = le32_to_cpu(start_ext->ee_block);
229 } else if (start_ext->ee_len && new_ext->ee_len &&
230 !end_ext->ee_len && o_start == o_end) {
233 * donor |--------------|---------------|
234 * orig |------------------------------|
236 o_start->ee_len = start_ext->ee_len;
237 eblock = le32_to_cpu(start_ext->ee_block);
240 } else if (!start_ext->ee_len && new_ext->ee_len &&
241 end_ext->ee_len && o_start == o_end) {
244 * donor |--------------|---------------|
245 * orig |------------------------------|
247 o_end->ee_block = end_ext->ee_block;
248 o_end->ee_len = end_ext->ee_len;
249 ext4_ext_store_pblock(o_end, ext4_ext_pblock(end_ext));
252 * Set 0 to the extent block if new_ext was
255 if (new_ext->ee_block)
256 eblock = le32_to_cpu(new_ext->ee_block);
260 ext4_debug("ext4 move extent: Unexpected insert case\n");
265 err = get_ext_path(orig_inode, eblock, &orig_path);
269 if (ext4_ext_insert_extent(handle, orig_inode,
270 orig_path, new_ext, 0))
275 err = get_ext_path(orig_inode,
276 le32_to_cpu(end_ext->ee_block) - 1, &orig_path);
280 if (ext4_ext_insert_extent(handle, orig_inode,
281 orig_path, end_ext, 0))
286 ext4_ext_drop_refs(orig_path);
295 * mext_insert_inside_block - Insert new extent to the extent block
297 * @o_start: first original extent to be moved
298 * @o_end: last original extent to be moved
299 * @start_ext: first new extent to be inserted
300 * @new_ext: middle of new extent to be inserted
301 * @end_ext: last new extent to be inserted
302 * @eh: extent header of target leaf block
303 * @range_to_move: used to decide how to insert extent
305 * Insert extents into the leaf block. The extent (@o_start) is overwritten
306 * by inserted extents.
309 mext_insert_inside_block(struct ext4_extent *o_start,
310 struct ext4_extent *o_end,
311 struct ext4_extent *start_ext,
312 struct ext4_extent *new_ext,
313 struct ext4_extent *end_ext,
314 struct ext4_extent_header *eh,
320 /* Move the existing extents */
321 if (range_to_move && o_end < EXT_LAST_EXTENT(eh)) {
322 len = (unsigned long)(EXT_LAST_EXTENT(eh) + 1) -
323 (unsigned long)(o_end + 1);
324 memmove(o_end + 1 + range_to_move, o_end + 1, len);
327 /* Insert start entry */
328 if (start_ext->ee_len)
329 o_start[i++].ee_len = start_ext->ee_len;
331 /* Insert new entry */
332 if (new_ext->ee_len) {
333 o_start[i] = *new_ext;
334 ext4_ext_store_pblock(&o_start[i++], ext4_ext_pblock(new_ext));
337 /* Insert end entry */
339 o_start[i] = *end_ext;
341 /* Increment the total entries counter on the extent block */
342 le16_add_cpu(&eh->eh_entries, range_to_move);
346 * mext_insert_extents - Insert new extent
348 * @handle: journal handle
349 * @orig_inode: original inode
350 * @orig_path: path indicates first extent to be changed
351 * @o_start: first original extent to be changed
352 * @o_end: last original extent to be changed
353 * @start_ext: first new extent to be inserted
354 * @new_ext: middle of new extent to be inserted
355 * @end_ext: last new extent to be inserted
357 * Call the function to insert extents. If we cannot add more extents into
358 * the leaf block, we call mext_insert_across_blocks() to create a
359 * new leaf block. Otherwise call mext_insert_inside_block(). Return 0
360 * on success, or a negative error value on failure.
363 mext_insert_extents(handle_t *handle, struct inode *orig_inode,
364 struct ext4_ext_path *orig_path,
365 struct ext4_extent *o_start,
366 struct ext4_extent *o_end,
367 struct ext4_extent *start_ext,
368 struct ext4_extent *new_ext,
369 struct ext4_extent *end_ext)
371 struct ext4_extent_header *eh;
372 unsigned long need_slots, slots_range;
373 int range_to_move, depth, ret;
376 * The extents need to be inserted
377 * start_extent + new_extent + end_extent.
379 need_slots = (start_ext->ee_len ? 1 : 0) + (end_ext->ee_len ? 1 : 0) +
380 (new_ext->ee_len ? 1 : 0);
382 /* The number of slots between start and end */
383 slots_range = ((unsigned long)(o_end + 1) - (unsigned long)o_start + 1)
384 / sizeof(struct ext4_extent);
386 /* Range to move the end of extent */
387 range_to_move = need_slots - slots_range;
388 depth = orig_path->p_depth;
390 eh = orig_path->p_hdr;
393 /* Register to journal */
394 ret = ext4_journal_get_write_access(handle, orig_path->p_bh);
400 if (range_to_move > 0 &&
401 (range_to_move > le16_to_cpu(eh->eh_max)
402 - le16_to_cpu(eh->eh_entries))) {
404 ret = mext_insert_across_blocks(handle, orig_inode, o_start,
405 o_end, start_ext, new_ext, end_ext);
409 mext_insert_inside_block(o_start, o_end, start_ext, new_ext,
410 end_ext, eh, range_to_move);
413 ret = ext4_handle_dirty_metadata(handle, orig_inode,
418 ret = ext4_mark_inode_dirty(handle, orig_inode);
427 * mext_leaf_block - Move one leaf extent block into the inode.
429 * @handle: journal handle
430 * @orig_inode: original inode
431 * @orig_path: path indicates first extent to be changed
432 * @dext: donor extent
433 * @from: start offset on the target file
435 * In order to insert extents into the leaf block, we must divide the extent
436 * in the leaf block into three extents. The one is located to be inserted
437 * extents, and the others are located around it.
439 * Therefore, this function creates structures to save extents of the leaf
440 * block, and inserts extents by calling mext_insert_extents() with
441 * created extents. Return 0 on success, or a negative error value on failure.
444 mext_leaf_block(handle_t *handle, struct inode *orig_inode,
445 struct ext4_ext_path *orig_path, struct ext4_extent *dext,
448 struct ext4_extent *oext, *o_start, *o_end, *prev_ext;
449 struct ext4_extent new_ext, start_ext, end_ext;
450 ext4_lblk_t new_ext_end;
451 int oext_alen, new_ext_alen, end_ext_alen;
452 int depth = ext_depth(orig_inode);
455 start_ext.ee_block = end_ext.ee_block = 0;
456 o_start = o_end = oext = orig_path[depth].p_ext;
457 oext_alen = ext4_ext_get_actual_len(oext);
458 start_ext.ee_len = end_ext.ee_len = 0;
460 new_ext.ee_block = cpu_to_le32(*from);
461 ext4_ext_store_pblock(&new_ext, ext4_ext_pblock(dext));
462 new_ext.ee_len = dext->ee_len;
463 new_ext_alen = ext4_ext_get_actual_len(&new_ext);
464 new_ext_end = le32_to_cpu(new_ext.ee_block) + new_ext_alen - 1;
467 * Case: original extent is first
472 if (le32_to_cpu(oext->ee_block) < le32_to_cpu(new_ext.ee_block) &&
473 le32_to_cpu(new_ext.ee_block) <
474 le32_to_cpu(oext->ee_block) + oext_alen) {
475 start_ext.ee_len = cpu_to_le16(le32_to_cpu(new_ext.ee_block) -
476 le32_to_cpu(oext->ee_block));
477 start_ext.ee_block = oext->ee_block;
478 copy_extent_status(oext, &start_ext);
479 } else if (oext > EXT_FIRST_EXTENT(orig_path[depth].p_hdr)) {
482 * We can merge new_ext into previous extent,
483 * if these are contiguous and same extent type.
485 if (ext4_can_extents_be_merged(orig_inode, prev_ext,
488 start_ext.ee_len = cpu_to_le16(
489 ext4_ext_get_actual_len(prev_ext) +
491 start_ext.ee_block = oext->ee_block;
492 copy_extent_status(prev_ext, &start_ext);
498 * Case: new_ext_end must be less than oext
502 if (le32_to_cpu(oext->ee_block) + oext_alen - 1 < new_ext_end) {
503 EXT4_ERROR_INODE(orig_inode,
504 "new_ext_end(%u) should be less than or equal to "
505 "oext->ee_block(%u) + oext_alen(%d) - 1",
506 new_ext_end, le32_to_cpu(oext->ee_block),
513 * Case: new_ext is smaller than original extent
514 * oext |---------------|
515 * new_ext |-----------|
518 if (le32_to_cpu(oext->ee_block) <= new_ext_end &&
519 new_ext_end < le32_to_cpu(oext->ee_block) + oext_alen - 1) {
521 cpu_to_le16(le32_to_cpu(oext->ee_block) +
522 oext_alen - 1 - new_ext_end);
523 copy_extent_status(oext, &end_ext);
524 end_ext_alen = ext4_ext_get_actual_len(&end_ext);
525 ext4_ext_store_pblock(&end_ext,
526 (ext4_ext_pblock(o_end) + oext_alen - end_ext_alen));
528 cpu_to_le32(le32_to_cpu(o_end->ee_block) +
529 oext_alen - end_ext_alen);
532 ret = mext_insert_extents(handle, orig_inode, orig_path, o_start,
533 o_end, &start_ext, &new_ext, &end_ext);
539 * mext_calc_swap_extents - Calculate extents for extent swapping.
541 * @tmp_dext: the extent that will belong to the original inode
542 * @tmp_oext: the extent that will belong to the donor inode
543 * @orig_off: block offset of original inode
544 * @donor_off: block offset of donor inode
545 * @max_count: the maximum length of extents
547 * Return 0 on success, or a negative error value on failure.
550 mext_calc_swap_extents(struct ext4_extent *tmp_dext,
551 struct ext4_extent *tmp_oext,
552 ext4_lblk_t orig_off, ext4_lblk_t donor_off,
553 ext4_lblk_t max_count)
555 ext4_lblk_t diff, orig_diff;
556 struct ext4_extent dext_old, oext_old;
558 BUG_ON(orig_off != donor_off);
560 /* original and donor extents have to cover the same block offset */
561 if (orig_off < le32_to_cpu(tmp_oext->ee_block) ||
562 le32_to_cpu(tmp_oext->ee_block) +
563 ext4_ext_get_actual_len(tmp_oext) - 1 < orig_off)
566 if (orig_off < le32_to_cpu(tmp_dext->ee_block) ||
567 le32_to_cpu(tmp_dext->ee_block) +
568 ext4_ext_get_actual_len(tmp_dext) - 1 < orig_off)
571 dext_old = *tmp_dext;
572 oext_old = *tmp_oext;
574 /* When tmp_dext is too large, pick up the target range. */
575 diff = donor_off - le32_to_cpu(tmp_dext->ee_block);
577 ext4_ext_store_pblock(tmp_dext, ext4_ext_pblock(tmp_dext) + diff);
578 le32_add_cpu(&tmp_dext->ee_block, diff);
579 le16_add_cpu(&tmp_dext->ee_len, -diff);
581 if (max_count < ext4_ext_get_actual_len(tmp_dext))
582 tmp_dext->ee_len = cpu_to_le16(max_count);
584 orig_diff = orig_off - le32_to_cpu(tmp_oext->ee_block);
585 ext4_ext_store_pblock(tmp_oext, ext4_ext_pblock(tmp_oext) + orig_diff);
587 /* Adjust extent length if donor extent is larger than orig */
588 if (ext4_ext_get_actual_len(tmp_dext) >
589 ext4_ext_get_actual_len(tmp_oext) - orig_diff)
590 tmp_dext->ee_len = cpu_to_le16(le16_to_cpu(tmp_oext->ee_len) -
593 tmp_oext->ee_len = cpu_to_le16(ext4_ext_get_actual_len(tmp_dext));
595 copy_extent_status(&oext_old, tmp_dext);
596 copy_extent_status(&dext_old, tmp_oext);
602 * mext_check_coverage - Check that all extents in range has the same type
604 * @inode: inode in question
605 * @from: block offset of inode
606 * @count: block count to be checked
607 * @uninit: extents expected to be uninitialized
608 * @err: pointer to save error value
610 * Return 1 if all extents in range has expected type, and zero otherwise.
613 mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
614 int uninit, int *err)
616 struct ext4_ext_path *path = NULL;
617 struct ext4_extent *ext;
619 ext4_lblk_t last = from + count;
620 while (from < last) {
621 *err = get_ext_path(inode, from, &path);
624 ext = path[ext_depth(inode)].p_ext;
625 if (uninit != ext4_ext_is_uninitialized(ext))
627 from += ext4_ext_get_actual_len(ext);
628 ext4_ext_drop_refs(path);
633 ext4_ext_drop_refs(path);
640 * mext_replace_branches - Replace original extents with new extents
642 * @handle: journal handle
643 * @orig_inode: original inode
644 * @donor_inode: donor inode
645 * @from: block offset of orig_inode
646 * @count: block count to be replaced
647 * @err: pointer to save return value
649 * Replace original inode extents and donor inode extents page by page.
650 * We implement this replacement in the following three steps:
651 * 1. Save the block information of original and donor inodes into
653 * 2. Change the block information of original inode to point at the
654 * donor inode blocks.
655 * 3. Change the block information of donor inode to point at the saved
656 * original inode blocks in the dummy extents.
658 * Return replaced block count.
661 mext_replace_branches(handle_t *handle, struct inode *orig_inode,
662 struct inode *donor_inode, ext4_lblk_t from,
663 ext4_lblk_t count, int *err)
665 struct ext4_ext_path *orig_path = NULL;
666 struct ext4_ext_path *donor_path = NULL;
667 struct ext4_extent *oext, *dext;
668 struct ext4_extent tmp_dext, tmp_oext;
669 ext4_lblk_t orig_off = from, donor_off = from;
671 int replaced_count = 0;
674 *err = ext4_es_remove_extent(orig_inode, from, count);
678 *err = ext4_es_remove_extent(donor_inode, from, count);
682 /* Get the original extent for the block "orig_off" */
683 *err = get_ext_path(orig_inode, orig_off, &orig_path);
687 /* Get the donor extent for the head */
688 *err = get_ext_path(donor_inode, donor_off, &donor_path);
691 depth = ext_depth(orig_inode);
692 oext = orig_path[depth].p_ext;
695 depth = ext_depth(donor_inode);
696 dext = donor_path[depth].p_ext;
698 goto missing_donor_extent;
701 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
706 /* Loop for the donor extents */
708 /* The extent for donor must be found. */
709 if (unlikely(!dext)) {
710 missing_donor_extent:
711 EXT4_ERROR_INODE(donor_inode,
712 "The extent for donor must be found");
715 } else if (donor_off != le32_to_cpu(tmp_dext.ee_block)) {
716 EXT4_ERROR_INODE(donor_inode,
717 "Donor offset(%u) and the first block of donor "
718 "extent(%u) should be equal",
720 le32_to_cpu(tmp_dext.ee_block));
725 /* Set donor extent to orig extent */
726 *err = mext_leaf_block(handle, orig_inode,
727 orig_path, &tmp_dext, &orig_off);
731 /* Set orig extent to donor extent */
732 *err = mext_leaf_block(handle, donor_inode,
733 donor_path, &tmp_oext, &donor_off);
737 dext_alen = ext4_ext_get_actual_len(&tmp_dext);
738 replaced_count += dext_alen;
739 donor_off += dext_alen;
740 orig_off += dext_alen;
742 /* Already moved the expected blocks */
743 if (replaced_count >= count)
747 ext4_ext_drop_refs(orig_path);
748 *err = get_ext_path(orig_inode, orig_off, &orig_path);
751 depth = ext_depth(orig_inode);
752 oext = orig_path[depth].p_ext;
756 ext4_ext_drop_refs(donor_path);
757 *err = get_ext_path(donor_inode, donor_off, &donor_path);
760 depth = ext_depth(donor_inode);
761 dext = donor_path[depth].p_ext;
764 *err = mext_calc_swap_extents(&tmp_dext, &tmp_oext, orig_off,
765 donor_off, count - replaced_count);
772 ext4_ext_drop_refs(orig_path);
776 ext4_ext_drop_refs(donor_path);
780 return replaced_count;
784 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
786 * @inode1: the inode structure
787 * @inode2: the inode structure
789 * @page: result page vector
791 * Grab two locked pages for inode's by inode order
794 mext_page_double_lock(struct inode *inode1, struct inode *inode2,
795 pgoff_t index, struct page *page[2])
797 struct address_space *mapping[2];
798 unsigned fl = AOP_FLAG_NOFS;
800 BUG_ON(!inode1 || !inode2);
801 if (inode1 < inode2) {
802 mapping[0] = inode1->i_mapping;
803 mapping[1] = inode2->i_mapping;
805 mapping[0] = inode2->i_mapping;
806 mapping[1] = inode1->i_mapping;
809 page[0] = grab_cache_page_write_begin(mapping[0], index, fl);
813 page[1] = grab_cache_page_write_begin(mapping[1], index, fl);
815 unlock_page(page[0]);
816 page_cache_release(page[0]);
820 if (inode1 > inode2) {
829 /* Force page buffers uptodate w/o dropping page's lock */
831 mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
833 struct inode *inode = page->mapping->host;
835 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
836 unsigned int blocksize, block_start, block_end;
837 int i, err, nr = 0, partial = 0;
838 BUG_ON(!PageLocked(page));
839 BUG_ON(PageWriteback(page));
841 if (PageUptodate(page))
844 blocksize = 1 << inode->i_blkbits;
845 if (!page_has_buffers(page))
846 create_empty_buffers(page, blocksize, 0);
848 head = page_buffers(page);
849 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
850 for (bh = head, block_start = 0; bh != head || !block_start;
851 block++, block_start = block_end, bh = bh->b_this_page) {
852 block_end = block_start + blocksize;
853 if (block_end <= from || block_start >= to) {
854 if (!buffer_uptodate(bh))
858 if (buffer_uptodate(bh))
860 if (!buffer_mapped(bh)) {
861 err = ext4_get_block(inode, block, bh, 0);
866 if (!buffer_mapped(bh)) {
867 zero_user(page, block_start, blocksize);
869 set_buffer_uptodate(bh);
873 BUG_ON(nr >= MAX_BUF_PER_PAGE);
880 for (i = 0; i < nr; i++) {
882 if (!bh_uptodate_or_lock(bh)) {
883 err = bh_submit_read(bh);
890 SetPageUptodate(page);
895 * move_extent_per_page - Move extent data per page
897 * @o_filp: file structure of original file
898 * @donor_inode: donor inode
899 * @orig_page_offset: page index on original file
900 * @data_offset_in_page: block index where data swapping starts
901 * @block_len_in_page: the number of blocks to be swapped
902 * @uninit: orig extent is uninitialized or not
903 * @err: pointer to save return value
905 * Save the data in original inode blocks and replace original inode extents
906 * with donor inode extents by calling mext_replace_branches().
907 * Finally, write out the saved data in new original inode blocks. Return
908 * replaced block count.
911 move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
912 pgoff_t orig_page_offset, int data_offset_in_page,
913 int block_len_in_page, int uninit, int *err)
915 struct inode *orig_inode = file_inode(o_filp);
916 struct page *pagep[2] = {NULL, NULL};
918 ext4_lblk_t orig_blk_offset;
919 long long offs = orig_page_offset << PAGE_CACHE_SHIFT;
920 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
921 unsigned int w_flags = 0;
922 unsigned int tmp_data_size, data_size, replaced_size;
923 int err2, jblocks, retries = 0;
924 int replaced_count = 0;
925 int from = data_offset_in_page << orig_inode->i_blkbits;
926 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
929 * It needs twice the amount of ordinary journal buffers because
930 * inode and donor_inode may change each different metadata blocks.
934 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
935 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
936 if (IS_ERR(handle)) {
937 *err = PTR_ERR(handle);
941 if (segment_eq(get_fs(), KERNEL_DS))
942 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
944 orig_blk_offset = orig_page_offset * blocks_per_page +
947 offs = (long long)orig_blk_offset << orig_inode->i_blkbits;
949 /* Calculate data_size */
950 if ((orig_blk_offset + block_len_in_page - 1) ==
951 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
952 /* Replace the last block */
953 tmp_data_size = orig_inode->i_size & (blocksize - 1);
955 * If data_size equal zero, it shows data_size is multiples of
956 * blocksize. So we set appropriate value.
958 if (tmp_data_size == 0)
959 tmp_data_size = blocksize;
961 data_size = tmp_data_size +
962 ((block_len_in_page - 1) << orig_inode->i_blkbits);
964 data_size = block_len_in_page << orig_inode->i_blkbits;
966 replaced_size = data_size;
968 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
970 if (unlikely(*err < 0))
973 * If orig extent was uninitialized it can become initialized
974 * at any time after i_data_sem was dropped, in order to
975 * serialize with delalloc we have recheck extent while we
976 * hold page's lock, if it is still the case data copy is not
977 * necessary, just swap data blocks between orig and donor.
980 ext4_double_down_write_data_sem(orig_inode, donor_inode);
981 /* If any of extents in range became initialized we have to
982 * fallback to data copying */
983 uninit = mext_check_coverage(orig_inode, orig_blk_offset,
984 block_len_in_page, 1, err);
988 uninit &= mext_check_coverage(donor_inode, orig_blk_offset,
989 block_len_in_page, 1, err);
994 ext4_double_up_write_data_sem(orig_inode, donor_inode);
997 if ((page_has_private(pagep[0]) &&
998 !try_to_release_page(pagep[0], 0)) ||
999 (page_has_private(pagep[1]) &&
1000 !try_to_release_page(pagep[1], 0))) {
1004 replaced_count = mext_replace_branches(handle, orig_inode,
1005 donor_inode, orig_blk_offset,
1006 block_len_in_page, err);
1008 ext4_double_up_write_data_sem(orig_inode, donor_inode);
1012 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
1016 /* At this point all buffers in range are uptodate, old mapping layout
1017 * is no longer required, try to drop it now. */
1018 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
1019 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
1024 replaced_count = mext_replace_branches(handle, orig_inode, donor_inode,
1026 block_len_in_page, err);
1028 if (replaced_count) {
1029 block_len_in_page = replaced_count;
1031 block_len_in_page << orig_inode->i_blkbits;
1035 /* Perform all necessary steps similar write_begin()/write_end()
1036 * but keeping in mind that i_size will not change */
1037 *err = __block_write_begin(pagep[0], from, from + replaced_size,
1040 *err = block_commit_write(pagep[0], from, from + replaced_size);
1042 if (unlikely(*err < 0))
1043 goto repair_branches;
1045 /* Even in case of data=writeback it is reasonable to pin
1046 * inode to transaction, to prevent unexpected data loss */
1047 *err = ext4_jbd2_file_inode(handle, orig_inode);
1050 unlock_page(pagep[0]);
1051 page_cache_release(pagep[0]);
1052 unlock_page(pagep[1]);
1053 page_cache_release(pagep[1]);
1055 ext4_journal_stop(handle);
1056 /* Buffer was busy because probably is pinned to journal transaction,
1057 * force transaction commit may help to free it. */
1058 if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
1061 return replaced_count;
1065 * This should never ever happen!
1066 * Extents are swapped already, but we are not able to copy data.
1067 * Try to swap extents to it's original places
1069 ext4_double_down_write_data_sem(orig_inode, donor_inode);
1070 replaced_count = mext_replace_branches(handle, donor_inode, orig_inode,
1072 block_len_in_page, &err2);
1073 ext4_double_up_write_data_sem(orig_inode, donor_inode);
1074 if (replaced_count != block_len_in_page) {
1075 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
1076 "Unable to copy data block,"
1077 " data will be lost.");
1085 * mext_check_arguments - Check whether move extent can be done
1087 * @orig_inode: original inode
1088 * @donor_inode: donor inode
1089 * @orig_start: logical start offset in block for orig
1090 * @donor_start: logical start offset in block for donor
1091 * @len: the number of blocks to be moved
1093 * Check the arguments of ext4_move_extents() whether the files can be
1094 * exchanged with each other.
1095 * Return 0 on success, or a negative error value on failure.
1098 mext_check_arguments(struct inode *orig_inode,
1099 struct inode *donor_inode, __u64 orig_start,
1100 __u64 donor_start, __u64 *len)
1102 ext4_lblk_t orig_blocks, donor_blocks;
1103 unsigned int blkbits = orig_inode->i_blkbits;
1104 unsigned int blocksize = 1 << blkbits;
1106 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
1107 ext4_debug("ext4 move extent: suid or sgid is set"
1108 " to donor file [ino:orig %lu, donor %lu]\n",
1109 orig_inode->i_ino, donor_inode->i_ino);
1113 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
1116 /* Ext4 move extent does not support swapfile */
1117 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
1118 ext4_debug("ext4 move extent: The argument files should "
1119 "not be swapfile [ino:orig %lu, donor %lu]\n",
1120 orig_inode->i_ino, donor_inode->i_ino);
1124 /* Ext4 move extent supports only extent based file */
1125 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
1126 ext4_debug("ext4 move extent: orig file is not extents "
1127 "based file [ino:orig %lu]\n", orig_inode->i_ino);
1129 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
1130 ext4_debug("ext4 move extent: donor file is not extents "
1131 "based file [ino:donor %lu]\n", donor_inode->i_ino);
1135 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
1136 ext4_debug("ext4 move extent: File size is 0 byte\n");
1140 /* Start offset should be same */
1141 if (orig_start != donor_start) {
1142 ext4_debug("ext4 move extent: orig and donor's start "
1143 "offset are not same [ino:orig %lu, donor %lu]\n",
1144 orig_inode->i_ino, donor_inode->i_ino);
1148 if ((orig_start >= EXT_MAX_BLOCKS) ||
1149 (*len > EXT_MAX_BLOCKS) ||
1150 (orig_start + *len >= EXT_MAX_BLOCKS)) {
1151 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
1152 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
1153 orig_inode->i_ino, donor_inode->i_ino);
1157 if (orig_inode->i_size > donor_inode->i_size) {
1158 donor_blocks = (donor_inode->i_size + blocksize - 1) >> blkbits;
1159 /* TODO: eliminate this artificial restriction */
1160 if (orig_start >= donor_blocks) {
1161 ext4_debug("ext4 move extent: orig start offset "
1162 "[%llu] should be less than donor file blocks "
1163 "[%u] [ino:orig %lu, donor %lu]\n",
1164 orig_start, donor_blocks,
1165 orig_inode->i_ino, donor_inode->i_ino);
1169 /* TODO: eliminate this artificial restriction */
1170 if (orig_start + *len > donor_blocks) {
1171 ext4_debug("ext4 move extent: End offset [%llu] should "
1172 "be less than donor file blocks [%u]."
1173 "So adjust length from %llu to %llu "
1174 "[ino:orig %lu, donor %lu]\n",
1175 orig_start + *len, donor_blocks,
1176 *len, donor_blocks - orig_start,
1177 orig_inode->i_ino, donor_inode->i_ino);
1178 *len = donor_blocks - orig_start;
1181 orig_blocks = (orig_inode->i_size + blocksize - 1) >> blkbits;
1182 if (orig_start >= orig_blocks) {
1183 ext4_debug("ext4 move extent: start offset [%llu] "
1184 "should be less than original file blocks "
1185 "[%u] [ino:orig %lu, donor %lu]\n",
1186 orig_start, orig_blocks,
1187 orig_inode->i_ino, donor_inode->i_ino);
1191 if (orig_start + *len > orig_blocks) {
1192 ext4_debug("ext4 move extent: Adjust length "
1193 "from %llu to %llu. Because it should be "
1194 "less than original file blocks "
1195 "[ino:orig %lu, donor %lu]\n",
1196 *len, orig_blocks - orig_start,
1197 orig_inode->i_ino, donor_inode->i_ino);
1198 *len = orig_blocks - orig_start;
1203 ext4_debug("ext4 move extent: len should not be 0 "
1204 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
1205 donor_inode->i_ino);
1213 * ext4_inode_double_lock - Lock i_mutex on both @inode1 and @inode2
1215 * @inode1: the inode structure
1216 * @inode2: the inode structure
1218 * Lock two inodes' i_mutex
1221 ext4_inode_double_lock(struct inode *inode1, struct inode *inode2)
1223 BUG_ON(inode1 == inode2);
1224 if (inode1 < inode2) {
1225 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1226 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1228 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1229 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1234 * ext4_inode_double_unlock - Release i_mutex on both @inode1 and @inode2
1236 * @inode1: the inode that is released first
1237 * @inode2: the inode that is released second
1242 ext4_inode_double_unlock(struct inode *inode1, struct inode *inode2)
1244 mutex_unlock(&inode1->i_mutex);
1245 mutex_unlock(&inode2->i_mutex);
1249 * ext4_move_extents - Exchange the specified range of a file
1251 * @o_filp: file structure of the original file
1252 * @d_filp: file structure of the donor file
1253 * @orig_start: start offset in block for orig
1254 * @donor_start: start offset in block for donor
1255 * @len: the number of blocks to be moved
1256 * @moved_len: moved block length
1258 * This function returns 0 and moved block length is set in moved_len
1259 * if succeed, otherwise returns error value.
1261 * Note: ext4_move_extents() proceeds the following order.
1262 * 1:ext4_move_extents() calculates the last block number of moving extent
1263 * function by the start block number (orig_start) and the number of blocks
1264 * to be moved (len) specified as arguments.
1265 * If the {orig, donor}_start points a hole, the extent's start offset
1266 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
1267 * after hole behind.
1268 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
1269 * or the ext_cur exceeds the block_end which is last logical block number.
1270 * 3:To get the length of continues area, call mext_next_extent()
1271 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
1272 * until find un-continuous extent, the start logical block number exceeds
1273 * the block_end or the extent points to the last extent.
1274 * 4:Exchange the original inode data with donor inode data
1275 * from orig_page_offset to seq_end_page.
1276 * The start indexes of data are specified as arguments.
1277 * That of the original inode is orig_page_offset,
1278 * and the donor inode is also orig_page_offset
1279 * (To easily handle blocksize != pagesize case, the offset for the
1280 * donor inode is block unit).
1281 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
1282 * then returns to step 2.
1283 * 6:Release holecheck_path, orig_path and set the len to moved_len
1284 * which shows the number of moved blocks.
1285 * The moved_len is useful for the command to calculate the file offset
1286 * for starting next move extent ioctl.
1287 * 7:Return 0 on success, or a negative error value on failure.
1290 ext4_move_extents(struct file *o_filp, struct file *d_filp,
1291 __u64 orig_start, __u64 donor_start, __u64 len,
1294 struct inode *orig_inode = file_inode(o_filp);
1295 struct inode *donor_inode = file_inode(d_filp);
1296 struct ext4_ext_path *orig_path = NULL, *holecheck_path = NULL;
1297 struct ext4_extent *ext_prev, *ext_cur, *ext_dummy;
1298 ext4_lblk_t block_start = orig_start;
1299 ext4_lblk_t block_end, seq_start, add_blocks, file_end, seq_blocks = 0;
1300 ext4_lblk_t rest_blocks;
1301 pgoff_t orig_page_offset = 0, seq_end_page;
1302 int ret, depth, last_extent = 0;
1303 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
1304 int data_offset_in_page;
1305 int block_len_in_page;
1308 if (orig_inode->i_sb != donor_inode->i_sb) {
1309 ext4_debug("ext4 move extent: The argument files "
1310 "should be in same FS [ino:orig %lu, donor %lu]\n",
1311 orig_inode->i_ino, donor_inode->i_ino);
1315 /* orig and donor should be different inodes */
1316 if (orig_inode == donor_inode) {
1317 ext4_debug("ext4 move extent: The argument files should not "
1318 "be same inode [ino:orig %lu, donor %lu]\n",
1319 orig_inode->i_ino, donor_inode->i_ino);
1323 /* Regular file check */
1324 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
1325 ext4_debug("ext4 move extent: The argument files should be "
1326 "regular file [ino:orig %lu, donor %lu]\n",
1327 orig_inode->i_ino, donor_inode->i_ino);
1330 /* TODO: This is non obvious task to swap blocks for inodes with full
1331 jornaling enabled */
1332 if (ext4_should_journal_data(orig_inode) ||
1333 ext4_should_journal_data(donor_inode)) {
1336 /* Protect orig and donor inodes against a truncate */
1337 ext4_inode_double_lock(orig_inode, donor_inode);
1339 /* Wait for all existing dio workers */
1340 ext4_inode_block_unlocked_dio(orig_inode);
1341 ext4_inode_block_unlocked_dio(donor_inode);
1342 inode_dio_wait(orig_inode);
1343 inode_dio_wait(donor_inode);
1345 /* Protect extent tree against block allocations via delalloc */
1346 ext4_double_down_write_data_sem(orig_inode, donor_inode);
1347 /* Check the filesystem environment whether move_extent can be done */
1348 ret = mext_check_arguments(orig_inode, donor_inode, orig_start,
1353 file_end = (i_size_read(orig_inode) - 1) >> orig_inode->i_blkbits;
1354 block_end = block_start + len - 1;
1355 if (file_end < block_end)
1356 len -= block_end - file_end;
1358 ret = get_ext_path(orig_inode, block_start, &orig_path);
1362 /* Get path structure to check the hole */
1363 ret = get_ext_path(orig_inode, block_start, &holecheck_path);
1367 depth = ext_depth(orig_inode);
1368 ext_cur = holecheck_path[depth].p_ext;
1371 * Get proper starting location of block replacement if block_start was
1374 if (le32_to_cpu(ext_cur->ee_block) +
1375 ext4_ext_get_actual_len(ext_cur) - 1 < block_start) {
1377 * The hole exists between extents or the tail of
1380 last_extent = mext_next_extent(orig_inode,
1381 holecheck_path, &ext_cur);
1382 if (last_extent < 0) {
1386 last_extent = mext_next_extent(orig_inode, orig_path,
1388 if (last_extent < 0) {
1392 seq_start = le32_to_cpu(ext_cur->ee_block);
1393 } else if (le32_to_cpu(ext_cur->ee_block) > block_start)
1394 /* The hole exists at the beginning of original file. */
1395 seq_start = le32_to_cpu(ext_cur->ee_block);
1397 seq_start = block_start;
1399 /* No blocks within the specified range. */
1400 if (le32_to_cpu(ext_cur->ee_block) > block_end) {
1401 ext4_debug("ext4 move extent: The specified range of file "
1402 "may be the hole\n");
1407 /* Adjust start blocks */
1408 add_blocks = min(le32_to_cpu(ext_cur->ee_block) +
1409 ext4_ext_get_actual_len(ext_cur), block_end + 1) -
1410 max(le32_to_cpu(ext_cur->ee_block), block_start);
1412 while (!last_extent && le32_to_cpu(ext_cur->ee_block) <= block_end) {
1413 seq_blocks += add_blocks;
1415 /* Adjust tail blocks */
1416 if (seq_start + seq_blocks - 1 > block_end)
1417 seq_blocks = block_end - seq_start + 1;
1420 last_extent = mext_next_extent(orig_inode, holecheck_path,
1422 if (last_extent < 0) {
1426 add_blocks = ext4_ext_get_actual_len(ext_cur);
1429 * Extend the length of contiguous block (seq_blocks)
1430 * if extents are contiguous.
1432 if (ext4_can_extents_be_merged(orig_inode,
1433 ext_prev, ext_cur) &&
1434 block_end >= le32_to_cpu(ext_cur->ee_block) &&
1438 /* Is original extent is uninitialized */
1439 uninit = ext4_ext_is_uninitialized(ext_prev);
1441 data_offset_in_page = seq_start % blocks_per_page;
1444 * Calculate data blocks count that should be swapped
1445 * at the first page.
1447 if (data_offset_in_page + seq_blocks > blocks_per_page) {
1448 /* Swapped blocks are across pages */
1450 blocks_per_page - data_offset_in_page;
1452 /* Swapped blocks are in a page */
1453 block_len_in_page = seq_blocks;
1456 orig_page_offset = seq_start >>
1457 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1458 seq_end_page = (seq_start + seq_blocks - 1) >>
1459 (PAGE_CACHE_SHIFT - orig_inode->i_blkbits);
1460 seq_start = le32_to_cpu(ext_cur->ee_block);
1461 rest_blocks = seq_blocks;
1464 * Up semaphore to avoid following problems:
1465 * a. transaction deadlock among ext4_journal_start,
1466 * ->write_begin via pagefault, and jbd2_journal_commit
1467 * b. racing with ->readpage, ->write_begin, and ext4_get_block
1468 * in move_extent_per_page
1470 ext4_double_up_write_data_sem(orig_inode, donor_inode);
1472 while (orig_page_offset <= seq_end_page) {
1474 /* Swap original branches with new branches */
1475 block_len_in_page = move_extent_per_page(
1476 o_filp, donor_inode,
1478 data_offset_in_page,
1479 block_len_in_page, uninit,
1482 /* Count how many blocks we have exchanged */
1483 *moved_len += block_len_in_page;
1486 if (*moved_len > len) {
1487 EXT4_ERROR_INODE(orig_inode,
1488 "We replaced blocks too much! "
1489 "sum of replaced: %llu requested: %llu",
1496 data_offset_in_page = 0;
1497 rest_blocks -= block_len_in_page;
1498 if (rest_blocks > blocks_per_page)
1499 block_len_in_page = blocks_per_page;
1501 block_len_in_page = rest_blocks;
1504 ext4_double_down_write_data_sem(orig_inode, donor_inode);
1508 /* Decrease buffer counter */
1510 ext4_ext_drop_refs(holecheck_path);
1511 ret = get_ext_path(orig_inode, seq_start, &holecheck_path);
1514 depth = holecheck_path->p_depth;
1516 /* Decrease buffer counter */
1518 ext4_ext_drop_refs(orig_path);
1519 ret = get_ext_path(orig_inode, seq_start, &orig_path);
1523 ext_cur = holecheck_path[depth].p_ext;
1524 add_blocks = ext4_ext_get_actual_len(ext_cur);
1530 ext4_discard_preallocations(orig_inode);
1531 ext4_discard_preallocations(donor_inode);
1535 ext4_ext_drop_refs(orig_path);
1538 if (holecheck_path) {
1539 ext4_ext_drop_refs(holecheck_path);
1540 kfree(holecheck_path);
1542 ext4_double_up_write_data_sem(orig_inode, donor_inode);
1543 ext4_inode_resume_unlocked_dio(orig_inode);
1544 ext4_inode_resume_unlocked_dio(donor_inode);
1545 ext4_inode_double_unlock(orig_inode, donor_inode);