MIPS: Fix LLVM build issue.
[linux-2.6-block.git] / fs / ext4 / move_extent.c
CommitLineData
748de673
AF
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>
5a0e3ad6 18#include <linux/slab.h>
748de673 19#include "ext4_jbd2.h"
748de673 20#include "ext4.h"
4a092d73 21#include "ext4_extents.h"
748de673 22
e8505970
AF
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 *
ed8a1a76 30 * ext4_find_extent wrapper. Return 0 on success, or a negative error value
e8505970
AF
31 * on failure.
32 */
33static inline int
34get_ext_path(struct inode *inode, ext4_lblk_t lblock,
3bdf14b4 35 struct ext4_ext_path **ppath)
e8505970 36{
0e401101 37 struct ext4_ext_path *path;
e8505970 38
ed8a1a76 39 path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
0e401101 40 if (IS_ERR(path))
3bdf14b4
TT
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;
e8505970 50}
748de673 51
748de673 52/**
393d1d1d
DTB
53 * ext4_double_down_write_data_sem - Acquire two inodes' write lock
54 * of i_data_sem
748de673 55 *
03bd8b9b 56 * Acquire write lock of i_data_sem of the two inodes
748de673 57 */
393d1d1d
DTB
58void
59ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
748de673 60{
03bd8b9b
DM
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);
748de673 67
748de673 68 }
748de673
AF
69}
70
71/**
393d1d1d 72 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
748de673
AF
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
fc04cb49 76 * Release write lock of i_data_sem of two inodes (orig and donor).
748de673 77 */
393d1d1d
DTB
78void
79ext4_double_up_write_data_sem(struct inode *orig_inode,
80 struct inode *donor_inode)
748de673 81{
748de673
AF
82 up_write(&EXT4_I(orig_inode)->i_data_sem);
83 up_write(&EXT4_I(donor_inode)->i_data_sem);
84}
85
8c854473
DM
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
556615dc 92 * @unwritten: extents expected to be unwritten
8c854473
DM
93 * @err: pointer to save error value
94 *
95 * Return 1 if all extents in range has expected type, and zero otherwise.
96 */
97static int
98mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
556615dc 99 int unwritten, int *err)
8c854473
DM
100{
101 struct ext4_ext_path *path = NULL;
102 struct ext4_extent *ext;
0e401101 103 int ret = 0;
8c854473
DM
104 ext4_lblk_t last = from + count;
105 while (from < last) {
106 *err = get_ext_path(inode, from, &path);
107 if (*err)
0e401101 108 goto out;
8c854473 109 ext = path[ext_depth(inode)].p_ext;
556615dc 110 if (unwritten != ext4_ext_is_unwritten(ext))
0e401101 111 goto out;
8c854473
DM
112 from += ext4_ext_get_actual_len(ext);
113 ext4_ext_drop_refs(path);
114 }
0e401101
DM
115 ret = 1;
116out:
b7ea89ad
TT
117 ext4_ext_drop_refs(path);
118 kfree(path);
0e401101 119 return ret;
8c854473
DM
120}
121
bb557488
DM
122/**
123 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
124 *
125 * @inode1: the inode structure
126 * @inode2: the inode structure
65dd8327
XW
127 * @index1: page index
128 * @index2: page index
bb557488
DM
129 * @page: result page vector
130 *
131 * Grab two locked pages for inode's by inode order
132 */
133static int
134mext_page_double_lock(struct inode *inode1, struct inode *inode2,
fcf6b1b7 135 pgoff_t index1, pgoff_t index2, struct page *page[2])
bb557488
DM
136{
137 struct address_space *mapping[2];
138 unsigned fl = AOP_FLAG_NOFS;
139
140 BUG_ON(!inode1 || !inode2);
141 if (inode1 < inode2) {
142 mapping[0] = inode1->i_mapping;
143 mapping[1] = inode2->i_mapping;
144 } else {
fcf6b1b7
DM
145 pgoff_t tmp = index1;
146 index1 = index2;
147 index2 = tmp;
bb557488
DM
148 mapping[0] = inode2->i_mapping;
149 mapping[1] = inode1->i_mapping;
150 }
151
fcf6b1b7 152 page[0] = grab_cache_page_write_begin(mapping[0], index1, fl);
bb557488
DM
153 if (!page[0])
154 return -ENOMEM;
155
fcf6b1b7 156 page[1] = grab_cache_page_write_begin(mapping[1], index2, fl);
bb557488
DM
157 if (!page[1]) {
158 unlock_page(page[0]);
159 page_cache_release(page[0]);
160 return -ENOMEM;
161 }
7e8b12c6
DM
162 /*
163 * grab_cache_page_write_begin() may not wait on page's writeback if
164 * BDI not demand that. But it is reasonable to be very conservative
165 * here and explicitly wait on page's writeback
166 */
167 wait_on_page_writeback(page[0]);
168 wait_on_page_writeback(page[1]);
bf865467
FF
169 if (inode1 > inode2)
170 swap(page[0], page[1]);
171
bb557488
DM
172 return 0;
173}
174
175/* Force page buffers uptodate w/o dropping page's lock */
176static int
177mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
178{
179 struct inode *inode = page->mapping->host;
180 sector_t block;
181 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
182 unsigned int blocksize, block_start, block_end;
183 int i, err, nr = 0, partial = 0;
184 BUG_ON(!PageLocked(page));
185 BUG_ON(PageWriteback(page));
186
187 if (PageUptodate(page))
188 return 0;
189
190 blocksize = 1 << inode->i_blkbits;
191 if (!page_has_buffers(page))
192 create_empty_buffers(page, blocksize, 0);
193
194 head = page_buffers(page);
195 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
196 for (bh = head, block_start = 0; bh != head || !block_start;
197 block++, block_start = block_end, bh = bh->b_this_page) {
198 block_end = block_start + blocksize;
199 if (block_end <= from || block_start >= to) {
200 if (!buffer_uptodate(bh))
201 partial = 1;
202 continue;
203 }
204 if (buffer_uptodate(bh))
205 continue;
206 if (!buffer_mapped(bh)) {
bb557488
DM
207 err = ext4_get_block(inode, block, bh, 0);
208 if (err) {
209 SetPageError(page);
210 return err;
211 }
212 if (!buffer_mapped(bh)) {
213 zero_user(page, block_start, blocksize);
df3a98b0 214 set_buffer_uptodate(bh);
bb557488
DM
215 continue;
216 }
217 }
218 BUG_ON(nr >= MAX_BUF_PER_PAGE);
219 arr[nr++] = bh;
220 }
221 /* No io required */
222 if (!nr)
223 goto out;
224
225 for (i = 0; i < nr; i++) {
226 bh = arr[i];
227 if (!bh_uptodate_or_lock(bh)) {
228 err = bh_submit_read(bh);
229 if (err)
230 return err;
231 }
232 }
233out:
234 if (!partial)
235 SetPageUptodate(page);
236 return 0;
237}
238
748de673
AF
239/**
240 * move_extent_per_page - Move extent data per page
241 *
242 * @o_filp: file structure of original file
243 * @donor_inode: donor inode
244 * @orig_page_offset: page index on original file
65dd8327 245 * @donor_page_offset: page index on donor file
748de673
AF
246 * @data_offset_in_page: block index where data swapping starts
247 * @block_len_in_page: the number of blocks to be swapped
556615dc 248 * @unwritten: orig extent is unwritten or not
f868a48d 249 * @err: pointer to save return value
748de673
AF
250 *
251 * Save the data in original inode blocks and replace original inode extents
65dd8327 252 * with donor inode extents by calling ext4_swap_extents().
f868a48d
AF
253 * Finally, write out the saved data in new original inode blocks. Return
254 * replaced block count.
748de673
AF
255 */
256static int
44fc48f7 257move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
fcf6b1b7
DM
258 pgoff_t orig_page_offset, pgoff_t donor_page_offset,
259 int data_offset_in_page,
260 int block_len_in_page, int unwritten, int *err)
748de673 261{
496ad9aa 262 struct inode *orig_inode = file_inode(o_filp);
bb557488 263 struct page *pagep[2] = {NULL, NULL};
748de673 264 handle_t *handle;
fcf6b1b7 265 ext4_lblk_t orig_blk_offset, donor_blk_offset;
748de673 266 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
f868a48d 267 unsigned int tmp_data_size, data_size, replaced_size;
bb557488 268 int err2, jblocks, retries = 0;
f868a48d 269 int replaced_count = 0;
bb557488 270 int from = data_offset_in_page << orig_inode->i_blkbits;
748de673 271 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
88c6b61f 272 struct super_block *sb = orig_inode->i_sb;
748de673
AF
273
274 /*
275 * It needs twice the amount of ordinary journal buffers because
276 * inode and donor_inode may change each different metadata blocks.
277 */
bb557488
DM
278again:
279 *err = 0;
748de673 280 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
9924a92a 281 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
748de673 282 if (IS_ERR(handle)) {
f868a48d
AF
283 *err = PTR_ERR(handle);
284 return 0;
748de673
AF
285 }
286
748de673
AF
287 orig_blk_offset = orig_page_offset * blocks_per_page +
288 data_offset_in_page;
289
fcf6b1b7
DM
290 donor_blk_offset = donor_page_offset * blocks_per_page +
291 data_offset_in_page;
292
f868a48d 293 /* Calculate data_size */
748de673
AF
294 if ((orig_blk_offset + block_len_in_page - 1) ==
295 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
296 /* Replace the last block */
f868a48d 297 tmp_data_size = orig_inode->i_size & (blocksize - 1);
748de673 298 /*
f868a48d 299 * If data_size equal zero, it shows data_size is multiples of
748de673
AF
300 * blocksize. So we set appropriate value.
301 */
f868a48d
AF
302 if (tmp_data_size == 0)
303 tmp_data_size = blocksize;
748de673 304
f868a48d 305 data_size = tmp_data_size +
748de673 306 ((block_len_in_page - 1) << orig_inode->i_blkbits);
f868a48d
AF
307 } else
308 data_size = block_len_in_page << orig_inode->i_blkbits;
309
310 replaced_size = data_size;
748de673 311
bb557488 312 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
fcf6b1b7 313 donor_page_offset, pagep);
f868a48d 314 if (unlikely(*err < 0))
bb557488 315 goto stop_journal;
8c854473 316 /*
556615dc 317 * If orig extent was unwritten it can become initialized
8c854473
DM
318 * at any time after i_data_sem was dropped, in order to
319 * serialize with delalloc we have recheck extent while we
320 * hold page's lock, if it is still the case data copy is not
321 * necessary, just swap data blocks between orig and donor.
322 */
556615dc 323 if (unwritten) {
393d1d1d 324 ext4_double_down_write_data_sem(orig_inode, donor_inode);
8c854473
DM
325 /* If any of extents in range became initialized we have to
326 * fallback to data copying */
556615dc
LC
327 unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
328 block_len_in_page, 1, err);
8c854473
DM
329 if (*err)
330 goto drop_data_sem;
748de673 331
fcf6b1b7 332 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
556615dc 333 block_len_in_page, 1, err);
8c854473
DM
334 if (*err)
335 goto drop_data_sem;
336
556615dc 337 if (!unwritten) {
393d1d1d 338 ext4_double_up_write_data_sem(orig_inode, donor_inode);
8c854473
DM
339 goto data_copy;
340 }
341 if ((page_has_private(pagep[0]) &&
342 !try_to_release_page(pagep[0], 0)) ||
343 (page_has_private(pagep[1]) &&
344 !try_to_release_page(pagep[1], 0))) {
345 *err = -EBUSY;
346 goto drop_data_sem;
347 }
fcf6b1b7
DM
348 replaced_count = ext4_swap_extents(handle, orig_inode,
349 donor_inode, orig_blk_offset,
350 donor_blk_offset,
351 block_len_in_page, 1, err);
8c854473 352 drop_data_sem:
393d1d1d 353 ext4_double_up_write_data_sem(orig_inode, donor_inode);
8c854473
DM
354 goto unlock_pages;
355 }
356data_copy:
bb557488
DM
357 *err = mext_page_mkuptodate(pagep[0], from, from + replaced_size);
358 if (*err)
359 goto unlock_pages;
360
361 /* At this point all buffers in range are uptodate, old mapping layout
362 * is no longer required, try to drop it now. */
363 if ((page_has_private(pagep[0]) && !try_to_release_page(pagep[0], 0)) ||
364 (page_has_private(pagep[1]) && !try_to_release_page(pagep[1], 0))) {
365 *err = -EBUSY;
366 goto unlock_pages;
748de673 367 }
6e263146 368 ext4_double_down_write_data_sem(orig_inode, donor_inode);
fcf6b1b7
DM
369 replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
370 orig_blk_offset, donor_blk_offset,
371 block_len_in_page, 1, err);
6e263146 372 ext4_double_up_write_data_sem(orig_inode, donor_inode);
bb557488 373 if (*err) {
f868a48d
AF
374 if (replaced_count) {
375 block_len_in_page = replaced_count;
376 replaced_size =
377 block_len_in_page << orig_inode->i_blkbits;
ac48b0a1 378 } else
bb557488 379 goto unlock_pages;
748de673 380 }
bb557488
DM
381 /* Perform all necessary steps similar write_begin()/write_end()
382 * but keeping in mind that i_size will not change */
7e8b12c6 383 *err = __block_write_begin(pagep[0], from, replaced_size,
bb557488
DM
384 ext4_get_block);
385 if (!*err)
386 *err = block_commit_write(pagep[0], from, from + replaced_size);
748de673 387
bb557488
DM
388 if (unlikely(*err < 0))
389 goto repair_branches;
390
391 /* Even in case of data=writeback it is reasonable to pin
392 * inode to transaction, to prevent unexpected data loss */
393 *err = ext4_jbd2_file_inode(handle, orig_inode);
394
395unlock_pages:
396 unlock_page(pagep[0]);
397 page_cache_release(pagep[0]);
398 unlock_page(pagep[1]);
399 page_cache_release(pagep[1]);
400stop_journal:
748de673 401 ext4_journal_stop(handle);
88c6b61f
DM
402 if (*err == -ENOSPC &&
403 ext4_should_retry_alloc(sb, &retries))
404 goto again;
bb557488
DM
405 /* Buffer was busy because probably is pinned to journal transaction,
406 * force transaction commit may help to free it. */
88c6b61f
DM
407 if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
408 jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
bb557488 409 goto again;
f868a48d 410 return replaced_count;
bb557488
DM
411
412repair_branches:
413 /*
414 * This should never ever happen!
415 * Extents are swapped already, but we are not able to copy data.
416 * Try to swap extents to it's original places
417 */
393d1d1d 418 ext4_double_down_write_data_sem(orig_inode, donor_inode);
fcf6b1b7
DM
419 replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
420 orig_blk_offset, donor_blk_offset,
421 block_len_in_page, 0, &err2);
393d1d1d 422 ext4_double_up_write_data_sem(orig_inode, donor_inode);
bb557488
DM
423 if (replaced_count != block_len_in_page) {
424 EXT4_ERROR_INODE_BLOCK(orig_inode, (sector_t)(orig_blk_offset),
425 "Unable to copy data block,"
426 " data will be lost.");
427 *err = -EIO;
428 }
429 replaced_count = 0;
430 goto unlock_pages;
748de673
AF
431}
432
433/**
c437b273 434 * mext_check_arguments - Check whether move extent can be done
748de673
AF
435 *
436 * @orig_inode: original inode
437 * @donor_inode: donor inode
438 * @orig_start: logical start offset in block for orig
439 * @donor_start: logical start offset in block for donor
440 * @len: the number of blocks to be moved
748de673
AF
441 *
442 * Check the arguments of ext4_move_extents() whether the files can be
443 * exchanged with each other.
444 * Return 0 on success, or a negative error value on failure.
445 */
446static int
447mext_check_arguments(struct inode *orig_inode,
446aaa6e
KM
448 struct inode *donor_inode, __u64 orig_start,
449 __u64 donor_start, __u64 *len)
748de673 450{
fcf6b1b7 451 __u64 orig_eof, donor_eof;
70d5d3dc
AF
452 unsigned int blkbits = orig_inode->i_blkbits;
453 unsigned int blocksize = 1 << blkbits;
454
fcf6b1b7
DM
455 orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
456 donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
457
458
4a58579b
AF
459 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
460 ext4_debug("ext4 move extent: suid or sgid is set"
461 " to donor file [ino:orig %lu, donor %lu]\n",
462 orig_inode->i_ino, donor_inode->i_ino);
463 return -EINVAL;
464 }
465
1f5a81e4
TT
466 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
467 return -EPERM;
468
748de673
AF
469 /* Ext4 move extent does not support swapfile */
470 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
471 ext4_debug("ext4 move extent: The argument files should "
472 "not be swapfile [ino:orig %lu, donor %lu]\n",
473 orig_inode->i_ino, donor_inode->i_ino);
fcf6b1b7 474 return -EBUSY;
748de673
AF
475 }
476
748de673 477 /* Ext4 move extent supports only extent based file */
12e9b892 478 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
748de673
AF
479 ext4_debug("ext4 move extent: orig file is not extents "
480 "based file [ino:orig %lu]\n", orig_inode->i_ino);
481 return -EOPNOTSUPP;
12e9b892 482 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
748de673
AF
483 ext4_debug("ext4 move extent: donor file is not extents "
484 "based file [ino:donor %lu]\n", donor_inode->i_ino);
485 return -EOPNOTSUPP;
486 }
487
488 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
489 ext4_debug("ext4 move extent: File size is 0 byte\n");
490 return -EINVAL;
491 }
492
493 /* Start offset should be same */
fcf6b1b7
DM
494 if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
495 (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
748de673 496 ext4_debug("ext4 move extent: orig and donor's start "
fcf6b1b7 497 "offset are not alligned [ino:orig %lu, donor %lu]\n",
748de673
AF
498 orig_inode->i_ino, donor_inode->i_ino);
499 return -EINVAL;
500 }
501
f17722f9 502 if ((orig_start >= EXT_MAX_BLOCKS) ||
fcf6b1b7 503 (donor_start >= EXT_MAX_BLOCKS) ||
f17722f9 504 (*len > EXT_MAX_BLOCKS) ||
fcf6b1b7 505 (donor_start + *len >= EXT_MAX_BLOCKS) ||
f17722f9 506 (orig_start + *len >= EXT_MAX_BLOCKS)) {
0a80e986 507 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
f17722f9 508 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
748de673
AF
509 orig_inode->i_ino, donor_inode->i_ino);
510 return -EINVAL;
511 }
fcf6b1b7
DM
512 if (orig_eof < orig_start + *len - 1)
513 *len = orig_eof - orig_start;
514 if (donor_eof < donor_start + *len - 1)
515 *len = donor_eof - donor_start;
748de673 516 if (!*len) {
92c28159 517 ext4_debug("ext4 move extent: len should not be 0 "
748de673
AF
518 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
519 donor_inode->i_ino);
520 return -EINVAL;
521 }
522
523 return 0;
524}
525
748de673
AF
526/**
527 * ext4_move_extents - Exchange the specified range of a file
528 *
529 * @o_filp: file structure of the original file
530 * @d_filp: file structure of the donor file
65dd8327
XW
531 * @orig_blk: start offset in block for orig
532 * @donor_blk: start offset in block for donor
748de673
AF
533 * @len: the number of blocks to be moved
534 * @moved_len: moved block length
535 *
536 * This function returns 0 and moved block length is set in moved_len
537 * if succeed, otherwise returns error value.
538 *
748de673
AF
539 */
540int
fcf6b1b7
DM
541ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
542 __u64 donor_blk, __u64 len, __u64 *moved_len)
748de673 543{
496ad9aa
AV
544 struct inode *orig_inode = file_inode(o_filp);
545 struct inode *donor_inode = file_inode(d_filp);
fcf6b1b7 546 struct ext4_ext_path *path = NULL;
748de673 547 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
fcf6b1b7
DM
548 ext4_lblk_t o_end, o_start = orig_blk;
549 ext4_lblk_t d_start = donor_blk;
550 int ret;
748de673 551
03bd8b9b
DM
552 if (orig_inode->i_sb != donor_inode->i_sb) {
553 ext4_debug("ext4 move extent: The argument files "
554 "should be in same FS [ino:orig %lu, donor %lu]\n",
555 orig_inode->i_ino, donor_inode->i_ino);
556 return -EINVAL;
557 }
558
559 /* orig and donor should be different inodes */
560 if (orig_inode == donor_inode) {
f3ce8064 561 ext4_debug("ext4 move extent: The argument files should not "
03bd8b9b 562 "be same inode [ino:orig %lu, donor %lu]\n",
f3ce8064
TT
563 orig_inode->i_ino, donor_inode->i_ino);
564 return -EINVAL;
565 }
566
7247c0ca
AF
567 /* Regular file check */
568 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
569 ext4_debug("ext4 move extent: The argument files should be "
570 "regular file [ino:orig %lu, donor %lu]\n",
571 orig_inode->i_ino, donor_inode->i_ino);
572 return -EINVAL;
573 }
04e22412
EW
574
575 /* TODO: it's not obvious how to swap blocks for inodes with full
576 journaling enabled */
f066055a
DM
577 if (ext4_should_journal_data(orig_inode) ||
578 ext4_should_journal_data(donor_inode)) {
04e22412
EW
579 ext4_msg(orig_inode->i_sb, KERN_ERR,
580 "Online defrag not supported with data journaling");
581 return -EOPNOTSUPP;
f066055a 582 }
04e22412 583
fc04cb49 584 /* Protect orig and donor inodes against a truncate */
375e289e 585 lock_two_nondirectories(orig_inode, donor_inode);
748de673 586
17335dcc
DM
587 /* Wait for all existing dio workers */
588 ext4_inode_block_unlocked_dio(orig_inode);
589 ext4_inode_block_unlocked_dio(donor_inode);
590 inode_dio_wait(orig_inode);
591 inode_dio_wait(donor_inode);
592
fc04cb49 593 /* Protect extent tree against block allocations via delalloc */
393d1d1d 594 ext4_double_down_write_data_sem(orig_inode, donor_inode);
748de673 595 /* Check the filesystem environment whether move_extent can be done */
fcf6b1b7
DM
596 ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
597 donor_blk, &len);
03bd8b9b 598 if (ret)
347fa6f1 599 goto out;
fcf6b1b7 600 o_end = o_start + len;
748de673 601
fcf6b1b7
DM
602 while (o_start < o_end) {
603 struct ext4_extent *ex;
604 ext4_lblk_t cur_blk, next_blk;
605 pgoff_t orig_page_index, donor_page_index;
606 int offset_in_page;
607 int unwritten, cur_len;
748de673 608
fcf6b1b7
DM
609 ret = get_ext_path(orig_inode, o_start, &path);
610 if (ret)
748de673 611 goto out;
fcf6b1b7
DM
612 ex = path[path->p_depth].p_ext;
613 next_blk = ext4_ext_next_allocated_block(path);
614 cur_blk = le32_to_cpu(ex->ee_block);
615 cur_len = ext4_ext_get_actual_len(ex);
616 /* Check hole before the start pos */
617 if (cur_blk + cur_len - 1 < o_start) {
618 if (next_blk == EXT_MAX_BLOCKS) {
619 o_start = o_end;
620 ret = -ENODATA;
621 goto out;
622 }
623 d_start += next_blk - o_start;
624 o_start = next_blk;
3bdf14b4 625 continue;
fcf6b1b7
DM
626 /* Check hole after the start pos */
627 } else if (cur_blk > o_start) {
628 /* Skip hole */
629 d_start += cur_blk - o_start;
630 o_start = cur_blk;
631 /* Extent inside requested range ?*/
632 if (cur_blk >= o_end)
633 goto out;
634 } else { /* in_range(o_start, o_blk, o_len) */
635 cur_len += cur_blk - o_start;
748de673 636 }
fcf6b1b7
DM
637 unwritten = ext4_ext_is_unwritten(ex);
638 if (o_end - o_start < cur_len)
639 cur_len = o_end - o_start;
640
641 orig_page_index = o_start >> (PAGE_CACHE_SHIFT -
642 orig_inode->i_blkbits);
643 donor_page_index = d_start >> (PAGE_CACHE_SHIFT -
644 donor_inode->i_blkbits);
645 offset_in_page = o_start % blocks_per_page;
646 if (cur_len > blocks_per_page- offset_in_page)
647 cur_len = blocks_per_page - offset_in_page;
fc04cb49
AF
648 /*
649 * Up semaphore to avoid following problems:
650 * a. transaction deadlock among ext4_journal_start,
651 * ->write_begin via pagefault, and jbd2_journal_commit
652 * b. racing with ->readpage, ->write_begin, and ext4_get_block
653 * in move_extent_per_page
654 */
393d1d1d 655 ext4_double_up_write_data_sem(orig_inode, donor_inode);
fcf6b1b7
DM
656 /* Swap original branches with new branches */
657 move_extent_per_page(o_filp, donor_inode,
658 orig_page_index, donor_page_index,
659 offset_in_page, cur_len,
660 unwritten, &ret);
393d1d1d 661 ext4_double_down_write_data_sem(orig_inode, donor_inode);
03bd8b9b 662 if (ret < 0)
fc04cb49 663 break;
fcf6b1b7
DM
664 o_start += cur_len;
665 d_start += cur_len;
748de673 666 }
fcf6b1b7
DM
667 *moved_len = o_start - orig_blk;
668 if (*moved_len > len)
669 *moved_len = len;
670
748de673 671out:
94d7c16c
AF
672 if (*moved_len) {
673 ext4_discard_preallocations(orig_inode);
674 ext4_discard_preallocations(donor_inode);
675 }
676
b7ea89ad
TT
677 ext4_ext_drop_refs(path);
678 kfree(path);
393d1d1d 679 ext4_double_up_write_data_sem(orig_inode, donor_inode);
17335dcc
DM
680 ext4_inode_resume_unlocked_dio(orig_inode);
681 ext4_inode_resume_unlocked_dio(donor_inode);
375e289e 682 unlock_two_nondirectories(orig_inode, donor_inode);
748de673 683
03bd8b9b 684 return ret;
748de673 685}