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 **ppath)
37 struct ext4_ext_path *path;
39 path = ext4_ext_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
42 if (path[ext_depth(inode)].p_ext == NULL) {
43 ext4_ext_drop_refs(path);
53 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
56 * Acquire write lock of i_data_sem of the two inodes
59 ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
62 down_write(&EXT4_I(first)->i_data_sem);
63 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
65 down_write(&EXT4_I(second)->i_data_sem);
66 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
72 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
74 * @orig_inode: original inode structure to be released its lock first
75 * @donor_inode: donor inode structure to be released its lock second
76 * Release write lock of i_data_sem of two inodes (orig and donor).
79 ext4_double_up_write_data_sem(struct inode *orig_inode,
80 struct inode *donor_inode)
82 up_write(&EXT4_I(orig_inode)->i_data_sem);
83 up_write(&EXT4_I(donor_inode)->i_data_sem);
87 * mext_check_coverage - Check that all extents in range has the same type
89 * @inode: inode in question
90 * @from: block offset of inode
91 * @count: block count to be checked
92 * @unwritten: extents expected to be unwritten
93 * @err: pointer to save error value
95 * Return 1 if all extents in range has expected type, and zero otherwise.
98 mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
99 int unwritten, int *err)
101 struct ext4_ext_path *path = NULL;
102 struct ext4_extent *ext;
104 ext4_lblk_t last = from + count;
105 while (from < last) {
106 *err = get_ext_path(inode, from, &path);
109 ext = path[ext_depth(inode)].p_ext;
110 if (unwritten != ext4_ext_is_unwritten(ext))
112 from += ext4_ext_get_actual_len(ext);
113 ext4_ext_drop_refs(path);
117 ext4_ext_drop_refs(path);
123 * mext_replace_branches - Replace original extents with new extents
125 * @handle: journal handle
126 * @orig_inode: original inode
127 * @donor_inode: donor inode
128 * @from: block offset of orig_inode
129 * @count: block count to be replaced
130 * @err: pointer to save return value
132 * Replace original inode extents and donor inode extents page by page.
133 * We implement this replacement in the following three steps:
134 * 1. Save the block information of original and donor inodes into
136 * 2. Change the block information of original inode to point at the
137 * donor inode blocks.
138 * 3. Change the block information of donor inode to point at the saved
139 * original inode blocks in the dummy extents.
141 * Return replaced block count.
145 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
147 * @inode1: the inode structure
148 * @inode2: the inode structure
150 * @page: result page vector
152 * Grab two locked pages for inode's by inode order
155 mext_page_double_lock(struct inode *inode1, struct inode *inode2,
156 pgoff_t index1, pgoff_t index2, struct page *page[2])
158 struct address_space *mapping[2];
159 unsigned fl = AOP_FLAG_NOFS;
161 BUG_ON(!inode1 || !inode2);
162 if (inode1 < inode2) {
163 mapping[0] = inode1->i_mapping;
164 mapping[1] = inode2->i_mapping;
166 pgoff_t tmp = index1;
169 mapping[0] = inode2->i_mapping;
170 mapping[1] = inode1->i_mapping;
173 page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
177 page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
179 unlock_page(page[0]);
180 page_cache_release(page[0]);
184 * grab_cache_page_write_begin() may not wait on page's writeback if
185 * BDI not demand that. But it is reasonable to be very conservative
186 * here and explicitly wait on page's writeback
188 wait_on_page_writeback(page[0]);
189 wait_on_page_writeback(page[1]);
190 if (inode1 > inode2) {
199 /* Force page buffers uptodate w/o dropping page's lock */
201 mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
203 struct inode *inode = page->mapping->host;
205 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
206 unsigned int blocksize, block_start, block_end;
207 int i, err, nr = 0, partial = 0;
208 BUG_ON(!PageLocked(page));
209 BUG_ON(PageWriteback(page));
211 if (PageUptodate(page))
214 blocksize = 1 << inode->i_blkbits;
215 if (!page_has_buffers(page))
216 create_empty_buffers(page, blocksize, 0);
218 head = page_buffers(page);
219 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
220 for (bh = head, block_start = 0; bh != head || !block_start;
221 block++, block_start = block_end, bh = bh->b_this_page) {
222 block_end = block_start + blocksize;
223 if (block_end <= from || block_start >= to) {
224 if (!buffer_uptodate(bh))
228 if (buffer_uptodate(bh))
230 if (!buffer_mapped(bh)) {
231 err = ext4_get_block(inode, block, bh, 0);
236 if (!buffer_mapped(bh)) {
237 zero_user(page, block_start, blocksize);
238 set_buffer_uptodate(bh);
242 BUG_ON(nr >= MAX_BUF_PER_PAGE);
249 for (i = 0; i < nr; i++) {
251 if (!bh_uptodate_or_lock(bh)) {
252 err = bh_submit_read(bh);
259 SetPageUptodate(page);
264 * move_extent_per_page - Move extent data per page
266 * @o_filp: file structure of original file
267 * @donor_inode: donor inode
268 * @orig_page_offset: page index on original file
269 * @data_offset_in_page: block index where data swapping starts
270 * @block_len_in_page: the number of blocks to be swapped
271 * @unwritten: orig extent is unwritten or not
272 * @err: pointer to save return value
274 * Save the data in original inode blocks and replace original inode extents
275 * with donor inode extents by calling mext_replace_branches().
276 * Finally, write out the saved data in new original inode blocks. Return
277 * replaced block count.
280 move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
281 pgoff_t orig_page_offset, pgoff_t donor_page_offset,
282 int data_offset_in_page,
283 int block_len_in_page, int unwritten, int *err)
285 struct inode *orig_inode = file_inode(o_filp);
286 struct page *pagep[2] = {NULL, NULL};
288 ext4_lblk_t orig_blk_offset, donor_blk_offset;
289 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
290 unsigned int w_flags = 0;
291 unsigned int tmp_data_size, data_size, replaced_size;
292 int err2, jblocks, retries = 0;
293 int replaced_count = 0;
294 int from = data_offset_in_page << orig_inode->i_blkbits;
295 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
298 * It needs twice the amount of ordinary journal buffers because
299 * inode and donor_inode may change each different metadata blocks.
303 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
304 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
305 if (IS_ERR(handle)) {
306 *err = PTR_ERR(handle);
310 if (segment_eq(get_fs(), KERNEL_DS))
311 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
313 orig_blk_offset = orig_page_offset * blocks_per_page +
316 donor_blk_offset = donor_page_offset * blocks_per_page +
319 /* Calculate data_size */
320 if ((orig_blk_offset + block_len_in_page - 1) ==
321 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
322 /* Replace the last block */
323 tmp_data_size = orig_inode->i_size & (blocksize - 1);
325 * If data_size equal zero, it shows data_size is multiples of
326 * blocksize. So we set appropriate value.
328 if (tmp_data_size == 0)
329 tmp_data_size = blocksize;
331 data_size = tmp_data_size +
332 ((block_len_in_page - 1) << orig_inode->i_blkbits);
334 data_size = block_len_in_page << orig_inode->i_blkbits;
336 replaced_size = data_size;
338 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
339 donor_page_offset, pagep);
340 if (unlikely(*err < 0))
343 * If orig extent was unwritten it can become initialized
344 * at any time after i_data_sem was dropped, in order to
345 * serialize with delalloc we have recheck extent while we
346 * hold page's lock, if it is still the case data copy is not
347 * necessary, just swap data blocks between orig and donor.
350 ext4_double_down_write_data_sem(orig_inode, donor_inode);
351 /* If any of extents in range became initialized we have to
352 * fallback to data copying */
353 unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
354 block_len_in_page, 1, err);
358 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
359 block_len_in_page, 1, err);
364 ext4_double_up_write_data_sem(orig_inode, donor_inode);
367 if ((page_has_private(pagep[0]) &&
368 !try_to_release_page(pagep[0], 0)) ||
369 (page_has_private(pagep[1]) &&
370 !try_to_release_page(pagep[1], 0))) {
374 replaced_count = ext4_swap_extents(handle, orig_inode,
375 donor_inode, orig_blk_offset,
377 block_len_in_page, 1, err);
379 ext4_double_up_write_data_sem(orig_inode, donor_inode);
383 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
387 /* At this point all buffers in range are uptodate, old mapping layout
388 * is no longer required, try to drop it now. */
389 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
390 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
394 ext4_double_down_write_data_sem(orig_inode, donor_inode);
395 replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
396 orig_blk_offset, donor_blk_offset,
397 block_len_in_page, 1, err);
398 ext4_double_up_write_data_sem(orig_inode, donor_inode);
400 if (replaced_count) {
401 block_len_in_page = replaced_count;
403 block_len_in_page << orig_inode->i_blkbits;
407 /* Perform all necessary steps similar write_begin()/write_end()
408 * but keeping in mind that i_size will not change */
409 *err = __block_write_begin(pagep[0], from, replaced_size,
412 *err = block_commit_write(pagep[0], from, from + replaced_size);
414 if (unlikely(*err < 0))
415 goto repair_branches;
417 /* Even in case of data=writeback it is reasonable to pin
418 * inode to transaction, to prevent unexpected data loss */
419 *err = ext4_jbd2_file_inode(handle, orig_inode);
422 unlock_page(pagep[0]);
423 page_cache_release(pagep[0]);
424 unlock_page(pagep[1]);
425 page_cache_release(pagep[1]);
427 ext4_journal_stop(handle);
428 /* Buffer was busy because probably is pinned to journal transaction,
429 * force transaction commit may help to free it. */
430 if (*err == -EBUSY && ext4_should_retry_alloc(orig_inode->i_sb,
433 return replaced_count;
437 * This should never ever happen!
438 * Extents are swapped already, but we are not able to copy data.
439 * Try to swap extents to it's original places
441 ext4_double_down_write_data_sem(orig_inode, donor_inode);
442 replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
443 orig_blk_offset, donor_blk_offset,
444 block_len_in_page, 0, &err2);
445 ext4_double_up_write_data_sem(orig_inode, donor_inode);
446 if (replaced_count != block_len_in_page) {
447 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
448 "Unable to copy data block,"
449 " data will be lost.");
457 * mext_check_arguments - Check whether move extent can be done
459 * @orig_inode: original inode
460 * @donor_inode: donor inode
461 * @orig_start: logical start offset in block for orig
462 * @donor_start: logical start offset in block for donor
463 * @len: the number of blocks to be moved
465 * Check the arguments of ext4_move_extents() whether the files can be
466 * exchanged with each other.
467 * Return 0 on success, or a negative error value on failure.
470 mext_check_arguments(struct inode *orig_inode,
471 struct inode *donor_inode, __u64 orig_start,
472 __u64 donor_start, __u64 *len)
474 __u64 orig_eof, donor_eof;
475 unsigned int blkbits = orig_inode->i_blkbits;
476 unsigned int blocksize = 1 << blkbits;
478 orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
479 donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
482 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
483 ext4_debug("ext4 move extent: suid or sgid is set"
484 " to donor file [ino:orig %lu, donor %lu]\n",
485 orig_inode->i_ino, donor_inode->i_ino);
489 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
492 /* Ext4 move extent does not support swapfile */
493 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
494 ext4_debug("ext4 move extent: The argument files should "
495 "not be swapfile [ino:orig %lu, donor %lu]\n",
496 orig_inode->i_ino, donor_inode->i_ino);
500 /* Ext4 move extent supports only extent based file */
501 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
502 ext4_debug("ext4 move extent: orig file is not extents "
503 "based file [ino:orig %lu]\n", orig_inode->i_ino);
505 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
506 ext4_debug("ext4 move extent: donor file is not extents "
507 "based file [ino:donor %lu]\n", donor_inode->i_ino);
511 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
512 ext4_debug("ext4 move extent: File size is 0 byte\n");
516 /* Start offset should be same */
517 if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
518 (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
519 ext4_debug("ext4 move extent: orig and donor's start "
520 "offset are not alligned [ino:orig %lu, donor %lu]\n",
521 orig_inode->i_ino, donor_inode->i_ino);
525 if ((orig_start >= EXT_MAX_BLOCKS) ||
526 (donor_start >= EXT_MAX_BLOCKS) ||
527 (*len > EXT_MAX_BLOCKS) ||
528 (donor_start + *len >= EXT_MAX_BLOCKS) ||
529 (orig_start + *len >= EXT_MAX_BLOCKS)) {
530 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
531 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
532 orig_inode->i_ino, donor_inode->i_ino);
535 if (orig_eof < orig_start + *len - 1)
536 *len = orig_eof - orig_start;
537 if (donor_eof < donor_start + *len - 1)
538 *len = donor_eof - donor_start;
540 ext4_debug("ext4 move extent: len should not be 0 "
541 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
550 * ext4_move_extents - Exchange the specified range of a file
552 * @o_filp: file structure of the original file
553 * @d_filp: file structure of the donor file
554 * @orig_start: start offset in block for orig
555 * @donor_start: start offset in block for donor
556 * @len: the number of blocks to be moved
557 * @moved_len: moved block length
559 * This function returns 0 and moved block length is set in moved_len
560 * if succeed, otherwise returns error value.
562 * Note: ext4_move_extents() proceeds the following order.
563 * 1:ext4_move_extents() calculates the last block number of moving extent
564 * function by the start block number (orig_start) and the number of blocks
565 * to be moved (len) specified as arguments.
566 * If the {orig, donor}_start points a hole, the extent's start offset
567 * pointed by ext_cur (current extent), holecheck_path, orig_path are set
569 * 2:Continue step 3 to step 5, until the holecheck_path points to last_extent
570 * or the ext_cur exceeds the block_end which is last logical block number.
571 * 3:To get the length of continues area, call mext_next_extent()
572 * specified with the ext_cur (initial value is holecheck_path) re-cursive,
573 * until find un-continuous extent, the start logical block number exceeds
574 * the block_end or the extent points to the last extent.
575 * 4:Exchange the original inode data with donor inode data
576 * from orig_page_offset to seq_end_page.
577 * The start indexes of data are specified as arguments.
578 * That of the original inode is orig_page_offset,
579 * and the donor inode is also orig_page_offset
580 * (To easily handle blocksize != pagesize case, the offset for the
581 * donor inode is block unit).
582 * 5:Update holecheck_path and orig_path to points a next proceeding extent,
583 * then returns to step 2.
584 * 6:Release holecheck_path, orig_path and set the len to moved_len
585 * which shows the number of moved blocks.
586 * The moved_len is useful for the command to calculate the file offset
587 * for starting next move extent ioctl.
588 * 7:Return 0 on success, or a negative error value on failure.
591 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
592 __u64 donor_blk, __u64 len, __u64 *moved_len)
594 struct inode *orig_inode = file_inode(o_filp);
595 struct inode *donor_inode = file_inode(d_filp);
596 struct ext4_ext_path *path = NULL;
597 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
598 ext4_lblk_t o_end, o_start = orig_blk;
599 ext4_lblk_t d_start = donor_blk;
602 if (orig_inode->i_sb != donor_inode->i_sb) {
603 ext4_debug("ext4 move extent: The argument files "
604 "should be in same FS [ino:orig %lu, donor %lu]\n",
605 orig_inode->i_ino, donor_inode->i_ino);
609 /* orig and donor should be different inodes */
610 if (orig_inode == donor_inode) {
611 ext4_debug("ext4 move extent: The argument files should not "
612 "be same inode [ino:orig %lu, donor %lu]\n",
613 orig_inode->i_ino, donor_inode->i_ino);
617 /* Regular file check */
618 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
619 ext4_debug("ext4 move extent: The argument files should be "
620 "regular file [ino:orig %lu, donor %lu]\n",
621 orig_inode->i_ino, donor_inode->i_ino);
624 /* TODO: This is non obvious task to swap blocks for inodes with full
626 if (ext4_should_journal_data(orig_inode) ||
627 ext4_should_journal_data(donor_inode)) {
630 /* Protect orig and donor inodes against a truncate */
631 lock_two_nondirectories(orig_inode, donor_inode);
633 /* Wait for all existing dio workers */
634 ext4_inode_block_unlocked_dio(orig_inode);
635 ext4_inode_block_unlocked_dio(donor_inode);
636 inode_dio_wait(orig_inode);
637 inode_dio_wait(donor_inode);
639 /* Protect extent tree against block allocations via delalloc */
640 ext4_double_down_write_data_sem(orig_inode, donor_inode);
641 /* Check the filesystem environment whether move_extent can be done */
642 ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
646 o_end = o_start + len;
648 while (o_start < o_end) {
649 struct ext4_extent *ex;
650 ext4_lblk_t cur_blk, next_blk;
651 pgoff_t orig_page_index, donor_page_index;
653 int unwritten, cur_len;
655 ret = get_ext_path(orig_inode, o_start, &path);
658 ex = path[path->p_depth].p_ext;
659 next_blk = ext4_ext_next_allocated_block(path);
660 cur_blk = le32_to_cpu(ex->ee_block);
661 cur_len = ext4_ext_get_actual_len(ex);
662 /* Check hole before the start pos */
663 if (cur_blk + cur_len - 1 < o_start) {
664 if (next_blk == EXT_MAX_BLOCKS) {
669 d_start += next_blk - o_start;
672 /* Check hole after the start pos */
673 } else if (cur_blk > o_start) {
675 d_start += cur_blk - o_start;
677 /* Extent inside requested range ?*/
678 if (cur_blk >= o_end)
680 } else { /* in_range(o_start, o_blk, o_len) */
681 cur_len += cur_blk - o_start;
683 unwritten = ext4_ext_is_unwritten(ex);
684 if (o_end - o_start < cur_len)
685 cur_len = o_end - o_start;
687 orig_page_index = o_start >> (PAGE_CACHE_SHIFT -
688 orig_inode->i_blkbits);
689 donor_page_index = d_start >> (PAGE_CACHE_SHIFT -
690 donor_inode->i_blkbits);
691 offset_in_page = o_start % blocks_per_page;
692 if (cur_len > blocks_per_page- offset_in_page)
693 cur_len = blocks_per_page - offset_in_page;
695 * Up semaphore to avoid following problems:
696 * a. transaction deadlock among ext4_journal_start,
697 * ->write_begin via pagefault, and jbd2_journal_commit
698 * b. racing with ->readpage, ->write_begin, and ext4_get_block
699 * in move_extent_per_page
701 ext4_double_up_write_data_sem(orig_inode, donor_inode);
702 /* Swap original branches with new branches */
703 move_extent_per_page(o_filp, donor_inode,
704 orig_page_index, donor_page_index,
705 offset_in_page, cur_len,
707 ext4_double_down_write_data_sem(orig_inode, donor_inode);
713 *moved_len = o_start - orig_blk;
714 if (*moved_len > len)
719 ext4_discard_preallocations(orig_inode);
720 ext4_discard_preallocations(donor_inode);
723 ext4_ext_drop_refs(path);
725 ext4_double_up_write_data_sem(orig_inode, donor_inode);
726 ext4_inode_resume_unlocked_dio(orig_inode);
727 ext4_inode_resume_unlocked_dio(donor_inode);
728 unlock_two_nondirectories(orig_inode, donor_inode);