xref: /linux/fs/ext4/move_extent.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
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 				folio_set_error(folio);
204 				return err;
205 			}
206 			if (!buffer_mapped(bh)) {
207 				folio_zero_range(folio, block_start, blocksize);
208 				set_buffer_uptodate(bh);
209 				continue;
210 			}
211 		}
212 		BUG_ON(nr >= MAX_BUF_PER_PAGE);
213 		arr[nr++] = bh;
214 	}
215 	/* No io required */
216 	if (!nr)
217 		goto out;
218 
219 	for (i = 0; i < nr; i++) {
220 		bh = arr[i];
221 		if (!bh_uptodate_or_lock(bh)) {
222 			err = ext4_read_bh(bh, 0, NULL);
223 			if (err)
224 				return err;
225 		}
226 	}
227 out:
228 	if (!partial)
229 		folio_mark_uptodate(folio);
230 	return 0;
231 }
232 
233 /**
234  * move_extent_per_page - Move extent data per page
235  *
236  * @o_filp:			file structure of original file
237  * @donor_inode:		donor inode
238  * @orig_page_offset:		page index on original file
239  * @donor_page_offset:		page index on donor file
240  * @data_offset_in_page:	block index where data swapping starts
241  * @block_len_in_page:		the number of blocks to be swapped
242  * @unwritten:			orig extent is unwritten or not
243  * @err:			pointer to save return value
244  *
245  * Save the data in original inode blocks and replace original inode extents
246  * with donor inode extents by calling ext4_swap_extents().
247  * Finally, write out the saved data in new original inode blocks. Return
248  * replaced block count.
249  */
250 static int
251 move_extent_per_page(struct file *o_filp, struct inode *donor_inode,
252 		     pgoff_t orig_page_offset, pgoff_t donor_page_offset,
253 		     int data_offset_in_page,
254 		     int block_len_in_page, int unwritten, int *err)
255 {
256 	struct inode *orig_inode = file_inode(o_filp);
257 	struct folio *folio[2] = {NULL, NULL};
258 	handle_t *handle;
259 	ext4_lblk_t orig_blk_offset, donor_blk_offset;
260 	unsigned long blocksize = orig_inode->i_sb->s_blocksize;
261 	unsigned int tmp_data_size, data_size, replaced_size;
262 	int i, err2, jblocks, retries = 0;
263 	int replaced_count = 0;
264 	int from = data_offset_in_page << orig_inode->i_blkbits;
265 	int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
266 	struct super_block *sb = orig_inode->i_sb;
267 	struct buffer_head *bh = NULL;
268 
269 	/*
270 	 * It needs twice the amount of ordinary journal buffers because
271 	 * inode and donor_inode may change each different metadata blocks.
272 	 */
273 again:
274 	*err = 0;
275 	jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
276 	handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
277 	if (IS_ERR(handle)) {
278 		*err = PTR_ERR(handle);
279 		return 0;
280 	}
281 
282 	orig_blk_offset = orig_page_offset * blocks_per_page +
283 		data_offset_in_page;
284 
285 	donor_blk_offset = donor_page_offset * blocks_per_page +
286 		data_offset_in_page;
287 
288 	/* Calculate data_size */
289 	if ((orig_blk_offset + block_len_in_page - 1) ==
290 	    ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
291 		/* Replace the last block */
292 		tmp_data_size = orig_inode->i_size & (blocksize - 1);
293 		/*
294 		 * If data_size equal zero, it shows data_size is multiples of
295 		 * blocksize. So we set appropriate value.
296 		 */
297 		if (tmp_data_size == 0)
298 			tmp_data_size = blocksize;
299 
300 		data_size = tmp_data_size +
301 			((block_len_in_page - 1) << orig_inode->i_blkbits);
302 	} else
303 		data_size = block_len_in_page << orig_inode->i_blkbits;
304 
305 	replaced_size = data_size;
306 
307 	*err = mext_folio_double_lock(orig_inode, donor_inode, orig_page_offset,
308 				     donor_page_offset, folio);
309 	if (unlikely(*err < 0))
310 		goto stop_journal;
311 	/*
312 	 * If orig extent was unwritten it can become initialized
313 	 * at any time after i_data_sem was dropped, in order to
314 	 * serialize with delalloc we have recheck extent while we
315 	 * hold page's lock, if it is still the case data copy is not
316 	 * necessary, just swap data blocks between orig and donor.
317 	 */
318 
319 	VM_BUG_ON_FOLIO(folio_test_large(folio[0]), folio[0]);
320 	VM_BUG_ON_FOLIO(folio_test_large(folio[1]), folio[1]);
321 	VM_BUG_ON_FOLIO(folio_nr_pages(folio[0]) != folio_nr_pages(folio[1]), folio[1]);
322 
323 	if (unwritten) {
324 		ext4_double_down_write_data_sem(orig_inode, donor_inode);
325 		/* If any of extents in range became initialized we have to
326 		 * fallback to data copying */
327 		unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
328 						block_len_in_page, 1, err);
329 		if (*err)
330 			goto drop_data_sem;
331 
332 		unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
333 						 block_len_in_page, 1, err);
334 		if (*err)
335 			goto drop_data_sem;
336 
337 		if (!unwritten) {
338 			ext4_double_up_write_data_sem(orig_inode, donor_inode);
339 			goto data_copy;
340 		}
341 		if (!filemap_release_folio(folio[0], 0) ||
342 		    !filemap_release_folio(folio[1], 0)) {
343 			*err = -EBUSY;
344 			goto drop_data_sem;
345 		}
346 		replaced_count = ext4_swap_extents(handle, orig_inode,
347 						   donor_inode, orig_blk_offset,
348 						   donor_blk_offset,
349 						   block_len_in_page, 1, err);
350 	drop_data_sem:
351 		ext4_double_up_write_data_sem(orig_inode, donor_inode);
352 		goto unlock_folios;
353 	}
354 data_copy:
355 	*err = mext_page_mkuptodate(folio[0], from, from + replaced_size);
356 	if (*err)
357 		goto unlock_folios;
358 
359 	/* At this point all buffers in range are uptodate, old mapping layout
360 	 * is no longer required, try to drop it now. */
361 	if (!filemap_release_folio(folio[0], 0) ||
362 	    !filemap_release_folio(folio[1], 0)) {
363 		*err = -EBUSY;
364 		goto unlock_folios;
365 	}
366 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
367 	replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
368 					       orig_blk_offset, donor_blk_offset,
369 					   block_len_in_page, 1, err);
370 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
371 	if (*err) {
372 		if (replaced_count) {
373 			block_len_in_page = replaced_count;
374 			replaced_size =
375 				block_len_in_page << orig_inode->i_blkbits;
376 		} else
377 			goto unlock_folios;
378 	}
379 	/* Perform all necessary steps similar write_begin()/write_end()
380 	 * but keeping in mind that i_size will not change */
381 	bh = folio_buffers(folio[0]);
382 	if (!bh)
383 		bh = create_empty_buffers(folio[0],
384 				1 << orig_inode->i_blkbits, 0);
385 	for (i = 0; i < data_offset_in_page; i++)
386 		bh = bh->b_this_page;
387 	for (i = 0; i < block_len_in_page; i++) {
388 		*err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0);
389 		if (*err < 0)
390 			goto repair_branches;
391 		bh = bh->b_this_page;
392 	}
393 
394 	block_commit_write(&folio[0]->page, from, from + replaced_size);
395 
396 	/* Even in case of data=writeback it is reasonable to pin
397 	 * inode to transaction, to prevent unexpected data loss */
398 	*err = ext4_jbd2_inode_add_write(handle, orig_inode,
399 			(loff_t)orig_page_offset << PAGE_SHIFT, replaced_size);
400 
401 unlock_folios:
402 	folio_unlock(folio[0]);
403 	folio_put(folio[0]);
404 	folio_unlock(folio[1]);
405 	folio_put(folio[1]);
406 stop_journal:
407 	ext4_journal_stop(handle);
408 	if (*err == -ENOSPC &&
409 	    ext4_should_retry_alloc(sb, &retries))
410 		goto again;
411 	/* Buffer was busy because probably is pinned to journal transaction,
412 	 * force transaction commit may help to free it. */
413 	if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
414 	    jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
415 		goto again;
416 	return replaced_count;
417 
418 repair_branches:
419 	/*
420 	 * This should never ever happen!
421 	 * Extents are swapped already, but we are not able to copy data.
422 	 * Try to swap extents to it's original places
423 	 */
424 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
425 	replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
426 					       orig_blk_offset, donor_blk_offset,
427 					   block_len_in_page, 0, &err2);
428 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
429 	if (replaced_count != block_len_in_page) {
430 		ext4_error_inode_block(orig_inode, (sector_t)(orig_blk_offset),
431 				       EIO, "Unable to copy data block,"
432 				       " data will be lost.");
433 		*err = -EIO;
434 	}
435 	replaced_count = 0;
436 	goto unlock_folios;
437 }
438 
439 /**
440  * mext_check_arguments - Check whether move extent can be done
441  *
442  * @orig_inode:		original inode
443  * @donor_inode:	donor inode
444  * @orig_start:		logical start offset in block for orig
445  * @donor_start:	logical start offset in block for donor
446  * @len:		the number of blocks to be moved
447  *
448  * Check the arguments of ext4_move_extents() whether the files can be
449  * exchanged with each other.
450  * Return 0 on success, or a negative error value on failure.
451  */
452 static int
453 mext_check_arguments(struct inode *orig_inode,
454 		     struct inode *donor_inode, __u64 orig_start,
455 		     __u64 donor_start, __u64 *len)
456 {
457 	__u64 orig_eof, donor_eof;
458 	unsigned int blkbits = orig_inode->i_blkbits;
459 	unsigned int blocksize = 1 << blkbits;
460 
461 	orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
462 	donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
463 
464 
465 	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
466 		ext4_debug("ext4 move extent: suid or sgid is set"
467 			   " to donor file [ino:orig %lu, donor %lu]\n",
468 			   orig_inode->i_ino, donor_inode->i_ino);
469 		return -EINVAL;
470 	}
471 
472 	if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
473 		return -EPERM;
474 
475 	/* Ext4 move extent does not support swap files */
476 	if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
477 		ext4_debug("ext4 move extent: The argument files should not be swap files [ino:orig %lu, donor %lu]\n",
478 			orig_inode->i_ino, donor_inode->i_ino);
479 		return -ETXTBSY;
480 	}
481 
482 	if (ext4_is_quota_file(orig_inode) && ext4_is_quota_file(donor_inode)) {
483 		ext4_debug("ext4 move extent: The argument files should not be quota files [ino:orig %lu, donor %lu]\n",
484 			orig_inode->i_ino, donor_inode->i_ino);
485 		return -EOPNOTSUPP;
486 	}
487 
488 	/* Ext4 move extent supports only extent based file */
489 	if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
490 		ext4_debug("ext4 move extent: orig file is not extents "
491 			"based file [ino:orig %lu]\n", orig_inode->i_ino);
492 		return -EOPNOTSUPP;
493 	} else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
494 		ext4_debug("ext4 move extent: donor file is not extents "
495 			"based file [ino:donor %lu]\n", donor_inode->i_ino);
496 		return -EOPNOTSUPP;
497 	}
498 
499 	if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
500 		ext4_debug("ext4 move extent: File size is 0 byte\n");
501 		return -EINVAL;
502 	}
503 
504 	/* Start offset should be same */
505 	if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
506 	    (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
507 		ext4_debug("ext4 move extent: orig and donor's start "
508 			"offsets are not aligned [ino:orig %lu, donor %lu]\n",
509 			orig_inode->i_ino, donor_inode->i_ino);
510 		return -EINVAL;
511 	}
512 
513 	if ((orig_start >= EXT_MAX_BLOCKS) ||
514 	    (donor_start >= EXT_MAX_BLOCKS) ||
515 	    (*len > EXT_MAX_BLOCKS) ||
516 	    (donor_start + *len >= EXT_MAX_BLOCKS) ||
517 	    (orig_start + *len >= EXT_MAX_BLOCKS))  {
518 		ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
519 			"[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
520 			orig_inode->i_ino, donor_inode->i_ino);
521 		return -EINVAL;
522 	}
523 	if (orig_eof <= orig_start)
524 		*len = 0;
525 	else if (orig_eof < orig_start + *len - 1)
526 		*len = orig_eof - orig_start;
527 	if (donor_eof <= donor_start)
528 		*len = 0;
529 	else if (donor_eof < donor_start + *len - 1)
530 		*len = donor_eof - donor_start;
531 	if (!*len) {
532 		ext4_debug("ext4 move extent: len should not be 0 "
533 			"[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
534 			donor_inode->i_ino);
535 		return -EINVAL;
536 	}
537 
538 	return 0;
539 }
540 
541 /**
542  * ext4_move_extents - Exchange the specified range of a file
543  *
544  * @o_filp:		file structure of the original file
545  * @d_filp:		file structure of the donor file
546  * @orig_blk:		start offset in block for orig
547  * @donor_blk:		start offset in block for donor
548  * @len:		the number of blocks to be moved
549  * @moved_len:		moved block length
550  *
551  * This function returns 0 and moved block length is set in moved_len
552  * if succeed, otherwise returns error value.
553  *
554  */
555 int
556 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
557 		  __u64 donor_blk, __u64 len, __u64 *moved_len)
558 {
559 	struct inode *orig_inode = file_inode(o_filp);
560 	struct inode *donor_inode = file_inode(d_filp);
561 	struct ext4_ext_path *path = NULL;
562 	int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
563 	ext4_lblk_t o_end, o_start = orig_blk;
564 	ext4_lblk_t d_start = donor_blk;
565 	int ret;
566 
567 	if (orig_inode->i_sb != donor_inode->i_sb) {
568 		ext4_debug("ext4 move extent: The argument files "
569 			"should be in same FS [ino:orig %lu, donor %lu]\n",
570 			orig_inode->i_ino, donor_inode->i_ino);
571 		return -EINVAL;
572 	}
573 
574 	/* orig and donor should be different inodes */
575 	if (orig_inode == donor_inode) {
576 		ext4_debug("ext4 move extent: The argument files should not "
577 			"be same inode [ino:orig %lu, donor %lu]\n",
578 			orig_inode->i_ino, donor_inode->i_ino);
579 		return -EINVAL;
580 	}
581 
582 	/* Regular file check */
583 	if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
584 		ext4_debug("ext4 move extent: The argument files should be "
585 			"regular file [ino:orig %lu, donor %lu]\n",
586 			orig_inode->i_ino, donor_inode->i_ino);
587 		return -EINVAL;
588 	}
589 
590 	/* TODO: it's not obvious how to swap blocks for inodes with full
591 	   journaling enabled */
592 	if (ext4_should_journal_data(orig_inode) ||
593 	    ext4_should_journal_data(donor_inode)) {
594 		ext4_msg(orig_inode->i_sb, KERN_ERR,
595 			 "Online defrag not supported with data journaling");
596 		return -EOPNOTSUPP;
597 	}
598 
599 	if (IS_ENCRYPTED(orig_inode) || IS_ENCRYPTED(donor_inode)) {
600 		ext4_msg(orig_inode->i_sb, KERN_ERR,
601 			 "Online defrag not supported for encrypted files");
602 		return -EOPNOTSUPP;
603 	}
604 
605 	/* Protect orig and donor inodes against a truncate */
606 	lock_two_nondirectories(orig_inode, donor_inode);
607 
608 	/* Wait for all existing dio workers */
609 	inode_dio_wait(orig_inode);
610 	inode_dio_wait(donor_inode);
611 
612 	/* Protect extent tree against block allocations via delalloc */
613 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
614 	/* Check the filesystem environment whether move_extent can be done */
615 	ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
616 				    donor_blk, &len);
617 	if (ret)
618 		goto out;
619 	o_end = o_start + len;
620 
621 	*moved_len = 0;
622 	while (o_start < o_end) {
623 		struct ext4_extent *ex;
624 		ext4_lblk_t cur_blk, next_blk;
625 		pgoff_t orig_page_index, donor_page_index;
626 		int offset_in_page;
627 		int unwritten, cur_len;
628 
629 		ret = get_ext_path(orig_inode, o_start, &path);
630 		if (ret)
631 			goto out;
632 		ex = path[path->p_depth].p_ext;
633 		cur_blk = le32_to_cpu(ex->ee_block);
634 		cur_len = ext4_ext_get_actual_len(ex);
635 		/* Check hole before the start pos */
636 		if (cur_blk + cur_len - 1 < o_start) {
637 			next_blk = ext4_ext_next_allocated_block(path);
638 			if (next_blk == EXT_MAX_BLOCKS) {
639 				ret = -ENODATA;
640 				goto out;
641 			}
642 			d_start += next_blk - o_start;
643 			o_start = next_blk;
644 			continue;
645 		/* Check hole after the start pos */
646 		} else if (cur_blk > o_start) {
647 			/* Skip hole */
648 			d_start += cur_blk - o_start;
649 			o_start = cur_blk;
650 			/* Extent inside requested range ?*/
651 			if (cur_blk >= o_end)
652 				goto out;
653 		} else { /* in_range(o_start, o_blk, o_len) */
654 			cur_len += cur_blk - o_start;
655 		}
656 		unwritten = ext4_ext_is_unwritten(ex);
657 		if (o_end - o_start < cur_len)
658 			cur_len = o_end - o_start;
659 
660 		orig_page_index = o_start >> (PAGE_SHIFT -
661 					       orig_inode->i_blkbits);
662 		donor_page_index = d_start >> (PAGE_SHIFT -
663 					       donor_inode->i_blkbits);
664 		offset_in_page = o_start % blocks_per_page;
665 		if (cur_len > blocks_per_page - offset_in_page)
666 			cur_len = blocks_per_page - offset_in_page;
667 		/*
668 		 * Up semaphore to avoid following problems:
669 		 * a. transaction deadlock among ext4_journal_start,
670 		 *    ->write_begin via pagefault, and jbd2_journal_commit
671 		 * b. racing with ->read_folio, ->write_begin, and
672 		 *    ext4_get_block in move_extent_per_page
673 		 */
674 		ext4_double_up_write_data_sem(orig_inode, donor_inode);
675 		/* Swap original branches with new branches */
676 		*moved_len += move_extent_per_page(o_filp, donor_inode,
677 				     orig_page_index, donor_page_index,
678 				     offset_in_page, cur_len,
679 				     unwritten, &ret);
680 		ext4_double_down_write_data_sem(orig_inode, donor_inode);
681 		if (ret < 0)
682 			break;
683 		o_start += cur_len;
684 		d_start += cur_len;
685 	}
686 
687 out:
688 	if (*moved_len) {
689 		ext4_discard_preallocations(orig_inode);
690 		ext4_discard_preallocations(donor_inode);
691 	}
692 
693 	ext4_free_ext_path(path);
694 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
695 	unlock_two_nondirectories(orig_inode, donor_inode);
696 
697 	return ret;
698 }
699