xref: /linux/fs/ext4/move_extent.c (revision da5b2ad1c2f18834cb1ce429e2e5a5cf5cbdf21b)
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_folio_double_lock - Grab and lock folio on both @inode1 and @inode2
114  *
115  * @inode1:	the inode structure
116  * @inode2:	the inode structure
117  * @index1:	folio index
118  * @index2:	folio index
119  * @folio:	result folio vector
120  *
121  * Grab two locked folio for inode's by inode order
122  */
123 static int
124 mext_folio_double_lock(struct inode *inode1, struct inode *inode2,
125 		      pgoff_t index1, pgoff_t index2, struct folio *folio[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 	folio[0] = __filemap_get_folio(mapping[0], index1, FGP_WRITEBEGIN,
142 			mapping_gfp_mask(mapping[0]));
143 	if (IS_ERR(folio[0])) {
144 		memalloc_nofs_restore(flags);
145 		return PTR_ERR(folio[0]);
146 	}
147 
148 	folio[1] = __filemap_get_folio(mapping[1], index2, FGP_WRITEBEGIN,
149 			mapping_gfp_mask(mapping[1]));
150 	memalloc_nofs_restore(flags);
151 	if (IS_ERR(folio[1])) {
152 		folio_unlock(folio[0]);
153 		folio_put(folio[0]);
154 		return PTR_ERR(folio[1]);
155 	}
156 	/*
157 	 * __filemap_get_folio() may not wait on folio's writeback if
158 	 * BDI not demand that. But it is reasonable to be very conservative
159 	 * here and explicitly wait on folio's writeback
160 	 */
161 	folio_wait_writeback(folio[0]);
162 	folio_wait_writeback(folio[1]);
163 	if (inode1 > inode2)
164 		swap(folio[0], folio[1]);
165 
166 	return 0;
167 }
168 
169 /* Force page buffers uptodate w/o dropping page's lock */
170 static int
171 mext_page_mkuptodate(struct folio *folio, unsigned from, unsigned to)
172 {
173 	struct inode *inode = folio->mapping->host;
174 	sector_t block;
175 	struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
176 	unsigned int blocksize, block_start, block_end;
177 	int i, err,  nr = 0, partial = 0;
178 	BUG_ON(!folio_test_locked(folio));
179 	BUG_ON(folio_test_writeback(folio));
180 
181 	if (folio_test_uptodate(folio))
182 		return 0;
183 
184 	blocksize = i_blocksize(inode);
185 	head = folio_buffers(folio);
186 	if (!head)
187 		head = create_empty_buffers(folio, blocksize, 0);
188 
189 	block = (sector_t)folio->index << (PAGE_SHIFT - inode->i_blkbits);
190 	for (bh = head, block_start = 0; bh != head || !block_start;
191 	     block++, block_start = block_end, bh = bh->b_this_page) {
192 		block_end = block_start + blocksize;
193 		if (block_end <= from || block_start >= to) {
194 			if (!buffer_uptodate(bh))
195 				partial = 1;
196 			continue;
197 		}
198 		if (buffer_uptodate(bh))
199 			continue;
200 		if (!buffer_mapped(bh)) {
201 			err = ext4_get_block(inode, block, bh, 0);
202 			if (err)
203 				return err;
204 			if (!buffer_mapped(bh)) {
205 				folio_zero_range(folio, 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 		folio_mark_uptodate(folio);
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 folio *folio[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_folio_double_lock(orig_inode, donor_inode, orig_page_offset,
306 				     donor_page_offset, folio);
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 
317 	VM_BUG_ON_FOLIO(folio_test_large(folio[0]), folio[0]);
318 	VM_BUG_ON_FOLIO(folio_test_large(folio[1]), folio[1]);
319 	VM_BUG_ON_FOLIO(folio_nr_pages(folio[0]) != folio_nr_pages(folio[1]), folio[1]);
320 
321 	if (unwritten) {
322 		ext4_double_down_write_data_sem(orig_inode, donor_inode);
323 		/* If any of extents in range became initialized we have to
324 		 * fallback to data copying */
325 		unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
326 						block_len_in_page, 1, err);
327 		if (*err)
328 			goto drop_data_sem;
329 
330 		unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
331 						 block_len_in_page, 1, err);
332 		if (*err)
333 			goto drop_data_sem;
334 
335 		if (!unwritten) {
336 			ext4_double_up_write_data_sem(orig_inode, donor_inode);
337 			goto data_copy;
338 		}
339 		if (!filemap_release_folio(folio[0], 0) ||
340 		    !filemap_release_folio(folio[1], 0)) {
341 			*err = -EBUSY;
342 			goto drop_data_sem;
343 		}
344 		replaced_count = ext4_swap_extents(handle, orig_inode,
345 						   donor_inode, orig_blk_offset,
346 						   donor_blk_offset,
347 						   block_len_in_page, 1, err);
348 	drop_data_sem:
349 		ext4_double_up_write_data_sem(orig_inode, donor_inode);
350 		goto unlock_folios;
351 	}
352 data_copy:
353 	*err = mext_page_mkuptodate(folio[0], from, from + replaced_size);
354 	if (*err)
355 		goto unlock_folios;
356 
357 	/* At this point all buffers in range are uptodate, old mapping layout
358 	 * is no longer required, try to drop it now. */
359 	if (!filemap_release_folio(folio[0], 0) ||
360 	    !filemap_release_folio(folio[1], 0)) {
361 		*err = -EBUSY;
362 		goto unlock_folios;
363 	}
364 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
365 	replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
366 					       orig_blk_offset, donor_blk_offset,
367 					   block_len_in_page, 1, err);
368 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
369 	if (*err) {
370 		if (replaced_count) {
371 			block_len_in_page = replaced_count;
372 			replaced_size =
373 				block_len_in_page << orig_inode->i_blkbits;
374 		} else
375 			goto unlock_folios;
376 	}
377 	/* Perform all necessary steps similar write_begin()/write_end()
378 	 * but keeping in mind that i_size will not change */
379 	bh = folio_buffers(folio[0]);
380 	if (!bh)
381 		bh = create_empty_buffers(folio[0],
382 				1 << orig_inode->i_blkbits, 0);
383 	for (i = 0; i < data_offset_in_page; i++)
384 		bh = bh->b_this_page;
385 	for (i = 0; i < block_len_in_page; i++) {
386 		*err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0);
387 		if (*err < 0)
388 			goto repair_branches;
389 		bh = bh->b_this_page;
390 	}
391 
392 	block_commit_write(&folio[0]->page, from, from + replaced_size);
393 
394 	/* Even in case of data=writeback it is reasonable to pin
395 	 * inode to transaction, to prevent unexpected data loss */
396 	*err = ext4_jbd2_inode_add_write(handle, orig_inode,
397 			(loff_t)orig_page_offset << PAGE_SHIFT, replaced_size);
398 
399 unlock_folios:
400 	folio_unlock(folio[0]);
401 	folio_put(folio[0]);
402 	folio_unlock(folio[1]);
403 	folio_put(folio[1]);
404 stop_journal:
405 	ext4_journal_stop(handle);
406 	if (*err == -ENOSPC &&
407 	    ext4_should_retry_alloc(sb, &retries))
408 		goto again;
409 	/* Buffer was busy because probably is pinned to journal transaction,
410 	 * force transaction commit may help to free it. */
411 	if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
412 	    jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
413 		goto again;
414 	return replaced_count;
415 
416 repair_branches:
417 	/*
418 	 * This should never ever happen!
419 	 * Extents are swapped already, but we are not able to copy data.
420 	 * Try to swap extents to it's original places
421 	 */
422 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
423 	replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
424 					       orig_blk_offset, donor_blk_offset,
425 					   block_len_in_page, 0, &err2);
426 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
427 	if (replaced_count != block_len_in_page) {
428 		ext4_error_inode_block(orig_inode, (sector_t)(orig_blk_offset),
429 				       EIO, "Unable to copy data block,"
430 				       " data will be lost.");
431 		*err = -EIO;
432 	}
433 	replaced_count = 0;
434 	goto unlock_folios;
435 }
436 
437 /**
438  * mext_check_arguments - Check whether move extent can be done
439  *
440  * @orig_inode:		original inode
441  * @donor_inode:	donor inode
442  * @orig_start:		logical start offset in block for orig
443  * @donor_start:	logical start offset in block for donor
444  * @len:		the number of blocks to be moved
445  *
446  * Check the arguments of ext4_move_extents() whether the files can be
447  * exchanged with each other.
448  * Return 0 on success, or a negative error value on failure.
449  */
450 static int
451 mext_check_arguments(struct inode *orig_inode,
452 		     struct inode *donor_inode, __u64 orig_start,
453 		     __u64 donor_start, __u64 *len)
454 {
455 	__u64 orig_eof, donor_eof;
456 	unsigned int blkbits = orig_inode->i_blkbits;
457 	unsigned int blocksize = 1 << blkbits;
458 
459 	orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
460 	donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
461 
462 
463 	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
464 		ext4_debug("ext4 move extent: suid or sgid is set"
465 			   " to donor file [ino:orig %lu, donor %lu]\n",
466 			   orig_inode->i_ino, donor_inode->i_ino);
467 		return -EINVAL;
468 	}
469 
470 	if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
471 		return -EPERM;
472 
473 	/* Ext4 move extent does not support swap files */
474 	if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
475 		ext4_debug("ext4 move extent: The argument files should not be swap files [ino:orig %lu, donor %lu]\n",
476 			orig_inode->i_ino, donor_inode->i_ino);
477 		return -ETXTBSY;
478 	}
479 
480 	if (ext4_is_quota_file(orig_inode) && ext4_is_quota_file(donor_inode)) {
481 		ext4_debug("ext4 move extent: The argument files should not be quota files [ino:orig %lu, donor %lu]\n",
482 			orig_inode->i_ino, donor_inode->i_ino);
483 		return -EOPNOTSUPP;
484 	}
485 
486 	/* Ext4 move extent supports only extent based file */
487 	if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
488 		ext4_debug("ext4 move extent: orig file is not extents "
489 			"based file [ino:orig %lu]\n", orig_inode->i_ino);
490 		return -EOPNOTSUPP;
491 	} else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
492 		ext4_debug("ext4 move extent: donor file is not extents "
493 			"based file [ino:donor %lu]\n", donor_inode->i_ino);
494 		return -EOPNOTSUPP;
495 	}
496 
497 	if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
498 		ext4_debug("ext4 move extent: File size is 0 byte\n");
499 		return -EINVAL;
500 	}
501 
502 	/* Start offset should be same */
503 	if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
504 	    (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
505 		ext4_debug("ext4 move extent: orig and donor's start "
506 			"offsets are not aligned [ino:orig %lu, donor %lu]\n",
507 			orig_inode->i_ino, donor_inode->i_ino);
508 		return -EINVAL;
509 	}
510 
511 	if ((orig_start >= EXT_MAX_BLOCKS) ||
512 	    (donor_start >= EXT_MAX_BLOCKS) ||
513 	    (*len > EXT_MAX_BLOCKS) ||
514 	    (donor_start + *len >= EXT_MAX_BLOCKS) ||
515 	    (orig_start + *len >= EXT_MAX_BLOCKS))  {
516 		ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
517 			"[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
518 			orig_inode->i_ino, donor_inode->i_ino);
519 		return -EINVAL;
520 	}
521 	if (orig_eof <= orig_start)
522 		*len = 0;
523 	else if (orig_eof < orig_start + *len - 1)
524 		*len = orig_eof - orig_start;
525 	if (donor_eof <= donor_start)
526 		*len = 0;
527 	else if (donor_eof < donor_start + *len - 1)
528 		*len = donor_eof - donor_start;
529 	if (!*len) {
530 		ext4_debug("ext4 move extent: len should not be 0 "
531 			"[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
532 			donor_inode->i_ino);
533 		return -EINVAL;
534 	}
535 
536 	return 0;
537 }
538 
539 /**
540  * ext4_move_extents - Exchange the specified range of a file
541  *
542  * @o_filp:		file structure of the original file
543  * @d_filp:		file structure of the donor file
544  * @orig_blk:		start offset in block for orig
545  * @donor_blk:		start offset in block for donor
546  * @len:		the number of blocks to be moved
547  * @moved_len:		moved block length
548  *
549  * This function returns 0 and moved block length is set in moved_len
550  * if succeed, otherwise returns error value.
551  *
552  */
553 int
554 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
555 		  __u64 donor_blk, __u64 len, __u64 *moved_len)
556 {
557 	struct inode *orig_inode = file_inode(o_filp);
558 	struct inode *donor_inode = file_inode(d_filp);
559 	struct ext4_ext_path *path = NULL;
560 	int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
561 	ext4_lblk_t o_end, o_start = orig_blk;
562 	ext4_lblk_t d_start = donor_blk;
563 	int ret;
564 
565 	if (orig_inode->i_sb != donor_inode->i_sb) {
566 		ext4_debug("ext4 move extent: The argument files "
567 			"should be in same FS [ino:orig %lu, donor %lu]\n",
568 			orig_inode->i_ino, donor_inode->i_ino);
569 		return -EINVAL;
570 	}
571 
572 	/* orig and donor should be different inodes */
573 	if (orig_inode == donor_inode) {
574 		ext4_debug("ext4 move extent: The argument files should not "
575 			"be same inode [ino:orig %lu, donor %lu]\n",
576 			orig_inode->i_ino, donor_inode->i_ino);
577 		return -EINVAL;
578 	}
579 
580 	/* Regular file check */
581 	if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
582 		ext4_debug("ext4 move extent: The argument files should be "
583 			"regular file [ino:orig %lu, donor %lu]\n",
584 			orig_inode->i_ino, donor_inode->i_ino);
585 		return -EINVAL;
586 	}
587 
588 	/* TODO: it's not obvious how to swap blocks for inodes with full
589 	   journaling enabled */
590 	if (ext4_should_journal_data(orig_inode) ||
591 	    ext4_should_journal_data(donor_inode)) {
592 		ext4_msg(orig_inode->i_sb, KERN_ERR,
593 			 "Online defrag not supported with data journaling");
594 		return -EOPNOTSUPP;
595 	}
596 
597 	if (IS_ENCRYPTED(orig_inode) || IS_ENCRYPTED(donor_inode)) {
598 		ext4_msg(orig_inode->i_sb, KERN_ERR,
599 			 "Online defrag not supported for encrypted files");
600 		return -EOPNOTSUPP;
601 	}
602 
603 	/* Protect orig and donor inodes against a truncate */
604 	lock_two_nondirectories(orig_inode, donor_inode);
605 
606 	/* Wait for all existing dio workers */
607 	inode_dio_wait(orig_inode);
608 	inode_dio_wait(donor_inode);
609 
610 	/* Protect extent tree against block allocations via delalloc */
611 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
612 	/* Check the filesystem environment whether move_extent can be done */
613 	ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
614 				    donor_blk, &len);
615 	if (ret)
616 		goto out;
617 	o_end = o_start + len;
618 
619 	*moved_len = 0;
620 	while (o_start < o_end) {
621 		struct ext4_extent *ex;
622 		ext4_lblk_t cur_blk, next_blk;
623 		pgoff_t orig_page_index, donor_page_index;
624 		int offset_in_page;
625 		int unwritten, cur_len;
626 
627 		ret = get_ext_path(orig_inode, o_start, &path);
628 		if (ret)
629 			goto out;
630 		ex = path[path->p_depth].p_ext;
631 		cur_blk = le32_to_cpu(ex->ee_block);
632 		cur_len = ext4_ext_get_actual_len(ex);
633 		/* Check hole before the start pos */
634 		if (cur_blk + cur_len - 1 < o_start) {
635 			next_blk = ext4_ext_next_allocated_block(path);
636 			if (next_blk == EXT_MAX_BLOCKS) {
637 				ret = -ENODATA;
638 				goto out;
639 			}
640 			d_start += next_blk - o_start;
641 			o_start = next_blk;
642 			continue;
643 		/* Check hole after the start pos */
644 		} else if (cur_blk > o_start) {
645 			/* Skip hole */
646 			d_start += cur_blk - o_start;
647 			o_start = cur_blk;
648 			/* Extent inside requested range ?*/
649 			if (cur_blk >= o_end)
650 				goto out;
651 		} else { /* in_range(o_start, o_blk, o_len) */
652 			cur_len += cur_blk - o_start;
653 		}
654 		unwritten = ext4_ext_is_unwritten(ex);
655 		if (o_end - o_start < cur_len)
656 			cur_len = o_end - o_start;
657 
658 		orig_page_index = o_start >> (PAGE_SHIFT -
659 					       orig_inode->i_blkbits);
660 		donor_page_index = d_start >> (PAGE_SHIFT -
661 					       donor_inode->i_blkbits);
662 		offset_in_page = o_start % blocks_per_page;
663 		if (cur_len > blocks_per_page - offset_in_page)
664 			cur_len = blocks_per_page - offset_in_page;
665 		/*
666 		 * Up semaphore to avoid following problems:
667 		 * a. transaction deadlock among ext4_journal_start,
668 		 *    ->write_begin via pagefault, and jbd2_journal_commit
669 		 * b. racing with ->read_folio, ->write_begin, and
670 		 *    ext4_get_block in move_extent_per_page
671 		 */
672 		ext4_double_up_write_data_sem(orig_inode, donor_inode);
673 		/* Swap original branches with new branches */
674 		*moved_len += move_extent_per_page(o_filp, donor_inode,
675 				     orig_page_index, donor_page_index,
676 				     offset_in_page, cur_len,
677 				     unwritten, &ret);
678 		ext4_double_down_write_data_sem(orig_inode, donor_inode);
679 		if (ret < 0)
680 			break;
681 		o_start += cur_len;
682 		d_start += cur_len;
683 	}
684 
685 out:
686 	if (*moved_len) {
687 		ext4_discard_preallocations(orig_inode);
688 		ext4_discard_preallocations(donor_inode);
689 	}
690 
691 	ext4_free_ext_path(path);
692 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
693 	unlock_two_nondirectories(orig_inode, donor_inode);
694 
695 	return ret;
696 }
697