1 // SPDX-License-Identifier: LGPL-2.1
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>
9 #include <linux/quotaops.h>
10 #include <linux/slab.h>
11 #include <linux/sched/mm.h>
12 #include "ext4_jbd2.h"
14 #include "ext4_extents.h"
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)
22 * ext4_find_extent wrapper. Return 0 on success, or a negative error value
26 get_ext_path(struct inode *inode, ext4_lblk_t lblock,
27 struct ext4_ext_path **ppath)
29 struct ext4_ext_path *path;
31 path = ext4_find_extent(inode, lblock, ppath, EXT4_EX_NOCACHE);
34 if (path[ext_depth(inode)].p_ext == NULL) {
35 ext4_free_ext_path(path);
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
48 * Acquire write lock of i_data_sem of the two inodes
51 ext4_double_down_write_data_sem(struct inode *first, struct inode *second)
54 down_write(&EXT4_I(first)->i_data_sem);
55 down_write_nested(&EXT4_I(second)->i_data_sem, I_DATA_SEM_OTHER);
57 down_write(&EXT4_I(second)->i_data_sem);
58 down_write_nested(&EXT4_I(first)->i_data_sem, I_DATA_SEM_OTHER);
64 * ext4_double_up_write_data_sem - Release two inodes' write lock of i_data_sem
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).
71 ext4_double_up_write_data_sem(struct inode *orig_inode,
72 struct inode *donor_inode)
74 up_write(&EXT4_I(orig_inode)->i_data_sem);
75 up_write(&EXT4_I(donor_inode)->i_data_sem);
79 * mext_check_coverage - Check that all extents in range has the same type
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
87 * Return 1 if all extents in range has expected type, and zero otherwise.
90 mext_check_coverage(struct inode *inode, ext4_lblk_t from, ext4_lblk_t count,
91 int unwritten, int *err)
93 struct ext4_ext_path *path = NULL;
94 struct ext4_extent *ext;
96 ext4_lblk_t last = from + count;
98 *err = get_ext_path(inode, from, &path);
101 ext = path[ext_depth(inode)].p_ext;
102 if (unwritten != ext4_ext_is_unwritten(ext))
104 from += ext4_ext_get_actual_len(ext);
108 ext4_free_ext_path(path);
113 * mext_page_double_lock - Grab and lock pages on both @inode1 and @inode2
115 * @inode1: the inode structure
116 * @inode2: the inode structure
117 * @index1: page index
118 * @index2: page index
119 * @page: result page vector
121 * Grab two locked pages for inode's by inode order
124 mext_page_double_lock(struct inode *inode1, struct inode *inode2,
125 pgoff_t index1, pgoff_t index2, struct page *page[2])
127 struct address_space *mapping[2];
130 BUG_ON(!inode1 || !inode2);
131 if (inode1 < inode2) {
132 mapping[0] = inode1->i_mapping;
133 mapping[1] = inode2->i_mapping;
135 swap(index1, index2);
136 mapping[0] = inode2->i_mapping;
137 mapping[1] = inode1->i_mapping;
140 flags = memalloc_nofs_save();
141 page[0] = grab_cache_page_write_begin(mapping[0], index1);
143 memalloc_nofs_restore(flags);
147 page[1] = grab_cache_page_write_begin(mapping[1], index2);
148 memalloc_nofs_restore(flags);
150 unlock_page(page[0]);
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
159 wait_on_page_writeback(page[0]);
160 wait_on_page_writeback(page[1]);
162 swap(page[0], page[1]);
167 /* Force page buffers uptodate w/o dropping page's lock */
169 mext_page_mkuptodate(struct page *page, unsigned from, unsigned to)
171 struct inode *inode = page->mapping->host;
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));
179 if (PageUptodate(page))
182 blocksize = i_blocksize(inode);
183 if (!page_has_buffers(page))
184 create_empty_buffers(page, blocksize, 0);
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))
196 if (buffer_uptodate(bh))
198 if (!buffer_mapped(bh)) {
199 err = ext4_get_block(inode, block, bh, 0);
204 if (!buffer_mapped(bh)) {
205 zero_user(page, block_start, blocksize);
206 set_buffer_uptodate(bh);
210 BUG_ON(nr >= MAX_BUF_PER_PAGE);
217 for (i = 0; i < nr; i++) {
219 if (!bh_uptodate_or_lock(bh)) {
220 err = ext4_read_bh(bh, 0, NULL);
227 SetPageUptodate(page);
232 * move_extent_per_page - Move extent data per page
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
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.
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)
254 struct inode *orig_inode = file_inode(o_filp);
255 struct page *pagep[2] = {NULL, NULL};
256 struct folio *folio[2] = {NULL, NULL};
258 ext4_lblk_t orig_blk_offset, donor_blk_offset;
259 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
260 unsigned int tmp_data_size, data_size, replaced_size;
261 int i, err2, jblocks, retries = 0;
262 int replaced_count = 0;
263 int from = data_offset_in_page << orig_inode->i_blkbits;
264 int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
265 struct super_block *sb = orig_inode->i_sb;
266 struct buffer_head *bh = NULL;
269 * It needs twice the amount of ordinary journal buffers because
270 * inode and donor_inode may change each different metadata blocks.
274 jblocks = ext4_writepage_trans_blocks(orig_inode) * 2;
275 handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
276 if (IS_ERR(handle)) {
277 *err = PTR_ERR(handle);
281 orig_blk_offset = orig_page_offset * blocks_per_page +
284 donor_blk_offset = donor_page_offset * blocks_per_page +
287 /* Calculate data_size */
288 if ((orig_blk_offset + block_len_in_page - 1) ==
289 ((orig_inode->i_size - 1) >> orig_inode->i_blkbits)) {
290 /* Replace the last block */
291 tmp_data_size = orig_inode->i_size & (blocksize - 1);
293 * If data_size equal zero, it shows data_size is multiples of
294 * blocksize. So we set appropriate value.
296 if (tmp_data_size == 0)
297 tmp_data_size = blocksize;
299 data_size = tmp_data_size +
300 ((block_len_in_page - 1) << orig_inode->i_blkbits);
302 data_size = block_len_in_page << orig_inode->i_blkbits;
304 replaced_size = data_size;
306 *err = mext_page_double_lock(orig_inode, donor_inode, orig_page_offset,
307 donor_page_offset, pagep);
308 if (unlikely(*err < 0))
311 * If orig extent was unwritten it can become initialized
312 * at any time after i_data_sem was dropped, in order to
313 * serialize with delalloc we have recheck extent while we
314 * hold page's lock, if it is still the case data copy is not
315 * necessary, just swap data blocks between orig and donor.
317 folio[0] = page_folio(pagep[0]);
318 folio[1] = page_folio(pagep[1]);
320 VM_BUG_ON_FOLIO(folio_test_large(folio[0]), folio[0]);
321 VM_BUG_ON_FOLIO(folio_test_large(folio[1]), folio[1]);
322 VM_BUG_ON_FOLIO(folio_nr_pages(folio[0]) != folio_nr_pages(folio[1]), folio[1]);
325 ext4_double_down_write_data_sem(orig_inode, donor_inode);
326 /* If any of extents in range became initialized we have to
327 * fallback to data copying */
328 unwritten = mext_check_coverage(orig_inode, orig_blk_offset,
329 block_len_in_page, 1, err);
333 unwritten &= mext_check_coverage(donor_inode, donor_blk_offset,
334 block_len_in_page, 1, err);
339 ext4_double_up_write_data_sem(orig_inode, donor_inode);
342 if (!filemap_release_folio(folio[0], 0) ||
343 !filemap_release_folio(folio[1], 0)) {
347 replaced_count = ext4_swap_extents(handle, orig_inode,
348 donor_inode, orig_blk_offset,
350 block_len_in_page, 1, err);
352 ext4_double_up_write_data_sem(orig_inode, donor_inode);
356 *err = mext_page_mkuptodate(&folio[0]->page, from, from + replaced_size);
360 /* At this point all buffers in range are uptodate, old mapping layout
361 * is no longer required, try to drop it now. */
362 if (!filemap_release_folio(folio[0], 0) ||
363 !filemap_release_folio(folio[1], 0)) {
367 ext4_double_down_write_data_sem(orig_inode, donor_inode);
368 replaced_count = ext4_swap_extents(handle, orig_inode, donor_inode,
369 orig_blk_offset, donor_blk_offset,
370 block_len_in_page, 1, err);
371 ext4_double_up_write_data_sem(orig_inode, donor_inode);
373 if (replaced_count) {
374 block_len_in_page = replaced_count;
376 block_len_in_page << orig_inode->i_blkbits;
380 /* Perform all necessary steps similar write_begin()/write_end()
381 * but keeping in mind that i_size will not change */
382 if (!folio_buffers(folio[0]))
383 create_empty_buffers(&folio[0]->page, 1 << orig_inode->i_blkbits, 0);
384 bh = folio_buffers(folio[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);
391 bh = bh->b_this_page;
394 *err = block_commit_write(&folio[0]->page, from, from + replaced_size);
396 if (unlikely(*err < 0))
397 goto repair_branches;
399 /* Even in case of data=writeback it is reasonable to pin
400 * inode to transaction, to prevent unexpected data loss */
401 *err = ext4_jbd2_inode_add_write(handle, orig_inode,
402 (loff_t)orig_page_offset << PAGE_SHIFT, replaced_size);
405 folio_unlock(folio[0]);
407 folio_unlock(folio[1]);
410 ext4_journal_stop(handle);
411 if (*err == -ENOSPC &&
412 ext4_should_retry_alloc(sb, &retries))
414 /* Buffer was busy because probably is pinned to journal transaction,
415 * force transaction commit may help to free it. */
416 if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
417 jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
419 return replaced_count;
423 * This should never ever happen!
424 * Extents are swapped already, but we are not able to copy data.
425 * Try to swap extents to it's original places
427 ext4_double_down_write_data_sem(orig_inode, donor_inode);
428 replaced_count = ext4_swap_extents(handle, donor_inode, orig_inode,
429 orig_blk_offset, donor_blk_offset,
430 block_len_in_page, 0, &err2);
431 ext4_double_up_write_data_sem(orig_inode, donor_inode);
432 if (replaced_count != block_len_in_page) {
433 ext4_error_inode_block(orig_inode, (sector_t)(orig_blk_offset),
434 EIO, "Unable to copy data block,"
435 " data will be lost.");
443 * mext_check_arguments - Check whether move extent can be done
445 * @orig_inode: original inode
446 * @donor_inode: donor inode
447 * @orig_start: logical start offset in block for orig
448 * @donor_start: logical start offset in block for donor
449 * @len: the number of blocks to be moved
451 * Check the arguments of ext4_move_extents() whether the files can be
452 * exchanged with each other.
453 * Return 0 on success, or a negative error value on failure.
456 mext_check_arguments(struct inode *orig_inode,
457 struct inode *donor_inode, __u64 orig_start,
458 __u64 donor_start, __u64 *len)
460 __u64 orig_eof, donor_eof;
461 unsigned int blkbits = orig_inode->i_blkbits;
462 unsigned int blocksize = 1 << blkbits;
464 orig_eof = (i_size_read(orig_inode) + blocksize - 1) >> blkbits;
465 donor_eof = (i_size_read(donor_inode) + blocksize - 1) >> blkbits;
468 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
469 ext4_debug("ext4 move extent: suid or sgid is set"
470 " to donor file [ino:orig %lu, donor %lu]\n",
471 orig_inode->i_ino, donor_inode->i_ino);
475 if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode))
478 /* Ext4 move extent does not support swap files */
479 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) {
480 ext4_debug("ext4 move extent: The argument files should not be swap files [ino:orig %lu, donor %lu]\n",
481 orig_inode->i_ino, donor_inode->i_ino);
485 if (ext4_is_quota_file(orig_inode) && ext4_is_quota_file(donor_inode)) {
486 ext4_debug("ext4 move extent: The argument files should not be quota files [ino:orig %lu, donor %lu]\n",
487 orig_inode->i_ino, donor_inode->i_ino);
491 /* Ext4 move extent supports only extent based file */
492 if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) {
493 ext4_debug("ext4 move extent: orig file is not extents "
494 "based file [ino:orig %lu]\n", orig_inode->i_ino);
496 } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) {
497 ext4_debug("ext4 move extent: donor file is not extents "
498 "based file [ino:donor %lu]\n", donor_inode->i_ino);
502 if ((!orig_inode->i_size) || (!donor_inode->i_size)) {
503 ext4_debug("ext4 move extent: File size is 0 byte\n");
507 /* Start offset should be same */
508 if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) !=
509 (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) {
510 ext4_debug("ext4 move extent: orig and donor's start "
511 "offsets are not aligned [ino:orig %lu, donor %lu]\n",
512 orig_inode->i_ino, donor_inode->i_ino);
516 if ((orig_start >= EXT_MAX_BLOCKS) ||
517 (donor_start >= EXT_MAX_BLOCKS) ||
518 (*len > EXT_MAX_BLOCKS) ||
519 (donor_start + *len >= EXT_MAX_BLOCKS) ||
520 (orig_start + *len >= EXT_MAX_BLOCKS)) {
521 ext4_debug("ext4 move extent: Can't handle over [%u] blocks "
522 "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS,
523 orig_inode->i_ino, donor_inode->i_ino);
526 if (orig_eof <= orig_start)
528 else if (orig_eof < orig_start + *len - 1)
529 *len = orig_eof - orig_start;
530 if (donor_eof <= donor_start)
532 else if (donor_eof < donor_start + *len - 1)
533 *len = donor_eof - donor_start;
535 ext4_debug("ext4 move extent: len should not be 0 "
536 "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino,
545 * ext4_move_extents - Exchange the specified range of a file
547 * @o_filp: file structure of the original file
548 * @d_filp: file structure of the donor file
549 * @orig_blk: start offset in block for orig
550 * @donor_blk: start offset in block for donor
551 * @len: the number of blocks to be moved
552 * @moved_len: moved block length
554 * This function returns 0 and moved block length is set in moved_len
555 * if succeed, otherwise returns error value.
559 ext4_move_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
560 __u64 donor_blk, __u64 len, __u64 *moved_len)
562 struct inode *orig_inode = file_inode(o_filp);
563 struct inode *donor_inode = file_inode(d_filp);
564 struct ext4_ext_path *path = NULL;
565 int blocks_per_page = PAGE_SIZE >> orig_inode->i_blkbits;
566 ext4_lblk_t o_end, o_start = orig_blk;
567 ext4_lblk_t d_start = donor_blk;
570 if (orig_inode->i_sb != donor_inode->i_sb) {
571 ext4_debug("ext4 move extent: The argument files "
572 "should be in same FS [ino:orig %lu, donor %lu]\n",
573 orig_inode->i_ino, donor_inode->i_ino);
577 /* orig and donor should be different inodes */
578 if (orig_inode == donor_inode) {
579 ext4_debug("ext4 move extent: The argument files should not "
580 "be same inode [ino:orig %lu, donor %lu]\n",
581 orig_inode->i_ino, donor_inode->i_ino);
585 /* Regular file check */
586 if (!S_ISREG(orig_inode->i_mode) || !S_ISREG(donor_inode->i_mode)) {
587 ext4_debug("ext4 move extent: The argument files should be "
588 "regular file [ino:orig %lu, donor %lu]\n",
589 orig_inode->i_ino, donor_inode->i_ino);
593 /* TODO: it's not obvious how to swap blocks for inodes with full
594 journaling enabled */
595 if (ext4_should_journal_data(orig_inode) ||
596 ext4_should_journal_data(donor_inode)) {
597 ext4_msg(orig_inode->i_sb, KERN_ERR,
598 "Online defrag not supported with data journaling");
602 if (IS_ENCRYPTED(orig_inode) || IS_ENCRYPTED(donor_inode)) {
603 ext4_msg(orig_inode->i_sb, KERN_ERR,
604 "Online defrag not supported for encrypted files");
608 /* Protect orig and donor inodes against a truncate */
609 lock_two_nondirectories(orig_inode, donor_inode);
611 /* Wait for all existing dio workers */
612 inode_dio_wait(orig_inode);
613 inode_dio_wait(donor_inode);
615 /* Protect extent tree against block allocations via delalloc */
616 ext4_double_down_write_data_sem(orig_inode, donor_inode);
617 /* Check the filesystem environment whether move_extent can be done */
618 ret = mext_check_arguments(orig_inode, donor_inode, orig_blk,
622 o_end = o_start + len;
625 while (o_start < o_end) {
626 struct ext4_extent *ex;
627 ext4_lblk_t cur_blk, next_blk;
628 pgoff_t orig_page_index, donor_page_index;
630 int unwritten, cur_len;
632 ret = get_ext_path(orig_inode, o_start, &path);
635 ex = path[path->p_depth].p_ext;
636 cur_blk = le32_to_cpu(ex->ee_block);
637 cur_len = ext4_ext_get_actual_len(ex);
638 /* Check hole before the start pos */
639 if (cur_blk + cur_len - 1 < o_start) {
640 next_blk = ext4_ext_next_allocated_block(path);
641 if (next_blk == EXT_MAX_BLOCKS) {
645 d_start += next_blk - o_start;
648 /* Check hole after the start pos */
649 } else if (cur_blk > o_start) {
651 d_start += cur_blk - o_start;
653 /* Extent inside requested range ?*/
654 if (cur_blk >= o_end)
656 } else { /* in_range(o_start, o_blk, o_len) */
657 cur_len += cur_blk - o_start;
659 unwritten = ext4_ext_is_unwritten(ex);
660 if (o_end - o_start < cur_len)
661 cur_len = o_end - o_start;
663 orig_page_index = o_start >> (PAGE_SHIFT -
664 orig_inode->i_blkbits);
665 donor_page_index = d_start >> (PAGE_SHIFT -
666 donor_inode->i_blkbits);
667 offset_in_page = o_start % blocks_per_page;
668 if (cur_len > blocks_per_page - offset_in_page)
669 cur_len = blocks_per_page - offset_in_page;
671 * Up semaphore to avoid following problems:
672 * a. transaction deadlock among ext4_journal_start,
673 * ->write_begin via pagefault, and jbd2_journal_commit
674 * b. racing with ->read_folio, ->write_begin, and
675 * ext4_get_block in move_extent_per_page
677 ext4_double_up_write_data_sem(orig_inode, donor_inode);
678 /* Swap original branches with new branches */
679 *moved_len += move_extent_per_page(o_filp, donor_inode,
680 orig_page_index, donor_page_index,
681 offset_in_page, cur_len,
683 ext4_double_down_write_data_sem(orig_inode, donor_inode);
692 ext4_discard_preallocations(orig_inode, 0);
693 ext4_discard_preallocations(donor_inode, 0);
696 ext4_free_ext_path(path);
697 ext4_double_up_write_data_sem(orig_inode, donor_inode);
698 unlock_two_nondirectories(orig_inode, donor_inode);