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