ext4: enable block_validity by default
[firefly-linux-kernel-4.4.55.git] / fs / ext4 / move_extent.c
1 /*
2  * Copyright (c) 2008,2009 NEC Software Tohoku, Ltd.
3  * Written by Takashi Sato <t-sato@yk.jp.nec.com>
4  *            Akira Fujita <a-fujita@rs.jp.nec.com>
5  *
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.
9  *
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.
14  */
15
16 #include <linux/fs.h>
17 #include <linux/quotaops.h>
18 #include <linux/slab.h>
19 #include "ext4_jbd2.h"
20 #include "ext4.h"
21 #include "ext4_extents.h"
22
23 /**
24  * get_ext_path - Find an extent path for designated logical block number.
25  *
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)
29  *
30  * ext4_find_extent wrapper. Return 0 on success, or a negative error value
31  * on failure.
32  */
33 static inline int
34 get_ext_path(struct inode *inode, ext4_lblk_t lblock,
35                 struct ext4_ext_path **ppath)
36 {
37         struct ext4_ext_path *path;
38
39         path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
40         if (IS_ERR(path))
41                 return PTR_ERR(path);
42         if (path[ext_depth(inode)].p_ext == NULL) {
43                 ext4_ext_drop_refs(path);
44                 kfree(path);
45                 *ppath = NULL;
46                 return -ENODATA;
47         }
48         *ppath = path;
49         return 0;
50 }
51
52 /**
53  * ext4_double_down_write_data_sem - Acquire two inodes' write lock
54  *                                   of i_data_sem
55  *
56  * Acquire write lock of i_data_sem of the two inodes
57  */
58 void
59 ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
60 {
61         if (first < second) {
62                 down_write(&EXT4_I(first)->i_data_sem);
63                 down_write_nested(&EXT4_I(second)->i_data_sem, SINGLE_DEPTH_NESTING);
64         } else {
65                 down_write(&EXT4_I(second)->i_data_sem);
66                 down_write_nested(&EXT4_I(first)->i_data_sem, SINGLE_DEPTH_NESTING);
67
68         }
69 }
70
71 /**
72  * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
73  *
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).
77  */
78 void
79 ext4_double_up_write_data_sem(struct inode *orig_inode,
80                               struct inode *donor_inode)
81 {
82         up_write(&EXT4_I(orig_inode)->i_data_sem);
83         up_write(&EXT4_I(donor_inode)->i_data_sem);
84 }
85
86 /**
87  * mext_check_coverage - Check that all extents in range has the same type
88  *
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
94  *
95  * Return 1 if all extents in range has expected type, and zero otherwise.
96  */
97 static int
98 mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
99                     int unwritten, int *err)
100 {
101         struct ext4_ext_path *path = NULL;
102         struct ext4_extent *ext;
103         int ret = 0;
104         ext4_lblk_t last = from + count;
105         while (from < last) {
106                 *err = get_ext_path(inode, from, &path);
107                 if (*err)
108                         goto out;
109                 ext = path[ext_depth(inode)].p_ext;
110                 if (unwritten != ext4_ext_is_unwritten(ext))
111                         goto out;
112                 from += ext4_ext_get_actual_len(ext);
113                 ext4_ext_drop_refs(path);
114         }
115         ret = 1;
116 out:
117         ext4_ext_drop_refs(path);
118         kfree(path);
119         return ret;
120 }
121
122 /**
123  * mext_replace_branches - Replace original extents with new extents
124  *
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
131  *
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
135  *    dummy extents.
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.
140  *
141  * Return replaced block count.
142  */
143
144 /**
145  * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
146  *
147  * @inode1:     the inode structure
148  * @inode2:     the inode structure
149  * @index:      page index
150  * @page:       result page vector
151  *
152  * Grab two locked pages for inode's by inode order
153  */
154 static int
155 mext_page_double_lock(struct inode *inode1, struct inode *inode2,
156                       pgoff_t index1, pgoff_t index2, struct page *page[2])
157 {
158         struct address_space *mapping[2];
159         unsigned fl = AOP_FLAG_NOFS;
160
161         BUG_ON(!inode1 || !inode2);
162         if (inode1 < inode2) {
163                 mapping[0] = inode1->i_mapping;
164                 mapping[1] = inode2->i_mapping;
165         } else {
166                 pgoff_t tmp = index1;
167                 index1 = index2;
168                 index2 = tmp;
169                 mapping[0] = inode2->i_mapping;
170                 mapping[1] = inode1->i_mapping;
171         }
172
173         page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
174         if (!page[0])
175                 return -ENOMEM;
176
177         page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
178         if (!page[1]) {
179                 unlock_page(page[0]);
180                 page_cache_release(page[0]);
181                 return -ENOMEM;
182         }
183         /*
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
187          */
188         wait_on_page_writeback(page[0]);
189         wait_on_page_writeback(page[1]);
190         if (inode1 > inode2) {
191                 struct page *tmp;
192                 tmp = page[0];
193                 page[0] = page[1];
194                 page[1] = tmp;
195         }
196         return 0;
197 }
198
199 /* Force page buffers uptodate w/o dropping page's lock */
200 static int
201 mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
202 {
203         struct inode *inode = page->mapping->host;
204         sector_t block;
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));
210
211         if (PageUptodate(page))
212                 return 0;
213
214         blocksize = 1 << inode->i_blkbits;
215         if (!page_has_buffers(page))
216                 create_empty_buffers(page, blocksize, 0);
217
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))
225                                 partial = 1;
226                         continue;
227                 }
228                 if (buffer_uptodate(bh))
229                         continue;
230                 if (!buffer_mapped(bh)) {
231                         err = ext4_get_block(inode, block, bh, 0);
232                         if (err) {
233                                 SetPageError(page);
234                                 return err;
235                         }
236                         if (!buffer_mapped(bh)) {
237                                 zero_user(page, block_start, blocksize);
238                                 set_buffer_uptodate(bh);
239                                 continue;
240                         }
241                 }
242                 BUG_ON(nr >= MAX_BUF_PER_PAGE);
243                 arr[nr++] = bh;
244         }
245         /* No io required */
246         if (!nr)
247                 goto out;
248
249         for (i = 0; i < nr; i++) {
250                 bh = arr[i];
251                 if (!bh_uptodate_or_lock(bh)) {
252                         err = bh_submit_read(bh);
253                         if (err)
254                                 return err;
255                 }
256         }
257 out:
258         if (!partial)
259                 SetPageUptodate(page);
260         return 0;
261 }
262
263 /**
264  * move_extent_per_page - Move extent data per page
265  *
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
273  *
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.
278  */
279 static int
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)
284 {
285         struct inode *orig_inode = file_inode(o_filp);
286         struct page *pagep[2] = {NULL, NULL};
287         handle_t *handle;
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;
296
297         /*
298          * It needs twice the amount of ordinary journal buffers because
299          * inode and donor_inode may change each different metadata blocks.
300          */
301 again:
302         *err = 0;
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);
307                 return 0;
308         }
309
310         if (segment_eq(get_fs(), KERNEL_DS))
311                 w_flags |= AOP_FLAG_UNINTERRUPTIBLE;
312
313         orig_blk_offset = orig_page_offset * blocks_per_page +
314                 data_offset_in_page;
315
316         donor_blk_offset = donor_page_offset * blocks_per_page +
317                 data_offset_in_page;
318
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);
324                 /*
325                  * If data_size equal zero, it shows data_size is multiples of
326                  * blocksize. So we set appropriate value.
327                  */
328                 if (tmp_data_size == 0)
329                         tmp_data_size = blocksize;
330
331                 data_size = tmp_data_size +
332                         ((block_len_in_page - 1) << orig_inode->i_blkbits);
333         } else
334                 data_size = block_len_in_page << orig_inode->i_blkbits;
335
336         replaced_size = data_size;
337
338         *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
339                                      donor_page_offset, pagep);
340         if (unlikely(*err < 0))
341                 goto stop_journal;
342         /*
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.
348          */
349         if (unwritten) {
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);
355                 if (*err)
356                         goto drop_data_sem;
357
358                 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
359                                                  block_len_in_page, 1, err);
360                 if (*err)
361                         goto drop_data_sem;
362
363                 if (!unwritten) {
364                         ext4_double_up_write_data_sem(orig_inode, donor_inode);
365                         goto data_copy;
366                 }
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))) {
371                         *err = -EBUSY;
372                         goto drop_data_sem;
373                 }
374                 replaced_count = ext4_swap_extents(handle, orig_inode,
375                                                    donor_inode, orig_blk_offset,
376                                                    donor_blk_offset,
377                                                    block_len_in_page, 1, err);
378         drop_data_sem:
379                 ext4_double_up_write_data_sem(orig_inode, donor_inode);
380                 goto unlock_pages;
381         }
382 data_copy:
383         *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
384         if (*err)
385                 goto unlock_pages;
386
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))) {
391                 *err = -EBUSY;
392                 goto unlock_pages;
393         }
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);
399         if (*err) {
400                 if (replaced_count) {
401                         block_len_in_page = replaced_count;
402                         replaced_size =
403                                 block_len_in_page << orig_inode->i_blkbits;
404                 } else
405                         goto unlock_pages;
406         }
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,
410                                    ext4_get_block);
411         if (!*err)
412                 *err = block_commit_write(pagep[0], from, from + replaced_size);
413
414         if (unlikely(*err < 0))
415                 goto repair_branches;
416
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);
420
421 unlock_pages:
422         unlock_page(pagep[0]);
423         page_cache_release(pagep[0]);
424         unlock_page(pagep[1]);
425         page_cache_release(pagep[1]);
426 stop_journal:
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,
431                                                       &retries))
432                 goto again;
433         return replaced_count;
434
435 repair_branches:
436         /*
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
440          */
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.");
450                 *err = -EIO;
451         }
452         replaced_count = 0;
453         goto unlock_pages;
454 }
455
456 /**
457  * mext_check_arguments - Check whether move extent can be done
458  *
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
464  *
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.
468  */
469 static int
470 mext_check_arguments(struct inode *orig_inode,
471                      struct inode *donor_inode, __u64 orig_start,
472                      __u64 donor_start, __u64 *len)
473 {
474         __u64 orig_eof, donor_eof;
475         unsigned int blkbits = orig_inode->i_blkbits;
476         unsigned int blocksize = 1 << blkbits;
477
478         orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
479         donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
480
481
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);
486                 return -EINVAL;
487         }
488
489         if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
490                 return -EPERM;
491
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);
497                 return -EBUSY;
498         }
499
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);
504                 return -EOPNOTSUPP;
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);
508                 return -EOPNOTSUPP;
509         }
510
511         if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
512                 ext4_debug("ext4 move extent: File size is 0 byte\n");
513                 return -EINVAL;
514         }
515
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);
522                 return -EINVAL;
523         }
524
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);
533                 return -EINVAL;
534         }
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;
539         if (!*len) {
540                 ext4_debug("ext4 move extent: len should not be 0 "
541                         "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
542                         donor_inode->i_ino);
543                 return -EINVAL;
544         }
545
546         return 0;
547 }
548
549 /**
550  * ext4_move_extents - Exchange the specified range of a file
551  *
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
558  *
559  * This function returns 0 and moved block length is set in moved_len
560  * if succeed, otherwise returns error value.
561  *
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
568  *   after hole behind.
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.
589  */
590 int
591 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
592                   __u64 donor_blk, __u64 len, __u64 *moved_len)
593 {
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;
600         int ret;
601
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);
606                 return -EINVAL;
607         }
608
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);
614                 return -EINVAL;
615         }
616
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);
622                 return -EINVAL;
623         }
624         /* TODO: This is non obvious task to swap blocks for inodes with full
625            jornaling enabled */
626         if (ext4_should_journal_data(orig_inode) ||
627             ext4_should_journal_data(donor_inode)) {
628                 return -EINVAL;
629         }
630         /* Protect orig and donor inodes against a truncate */
631         lock_two_nondirectories(orig_inode, donor_inode);
632
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);
638
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,
643                                     donor_blk, &len);
644         if (ret)
645                 goto out;
646         o_end = o_start + len;
647
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;
652                 int offset_in_page;
653                 int unwritten, cur_len;
654
655                 ret = get_ext_path(orig_inode, o_start, &path);
656                 if (ret)
657                         goto out;
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) {
665                                 o_start = o_end;
666                                 ret = -ENODATA;
667                                 goto out;
668                         }
669                         d_start += next_blk - o_start;
670                         o_start = next_blk;
671                         continue;
672                 /* Check hole after the start pos */
673                 } else if (cur_blk > o_start) {
674                         /* Skip hole */
675                         d_start += cur_blk - o_start;
676                         o_start = cur_blk;
677                         /* Extent inside requested range ?*/
678                         if (cur_blk >= o_end)
679                                 goto out;
680                 } else { /* in_range(o_start, o_blk, o_len) */
681                         cur_len += cur_blk - o_start;
682                 }
683                 unwritten = ext4_ext_is_unwritten(ex);
684                 if (o_end - o_start < cur_len)
685                         cur_len = o_end - o_start;
686
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;
694                 /*
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
700                  */
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,
706                                      unwritten, &ret);
707                 ext4_double_down_write_data_sem(orig_inode, donor_inode);
708                 if (ret < 0)
709                         break;
710                 o_start += cur_len;
711                 d_start += cur_len;
712         }
713         *moved_len = o_start - orig_blk;
714         if (*moved_len > len)
715                 *moved_len = len;
716
717 out:
718         if (*moved_len) {
719                 ext4_discard_preallocations(orig_inode);
720                 ext4_discard_preallocations(donor_inode);
721         }
722
723         ext4_ext_drop_refs(path);
724         kfree(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);
729
730         return ret;
731 }