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