Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ext4: refactor mext_check_arguments()

When moving extents, mext_check_validity() performs some basic file
system and file checks. However, some essential checks need to be
performed after acquiring the i_rwsem are still scattered in
mext_check_arguments(). Move those checks into mext_check_validity() and
make it executes entirely under the i_rwsem to make the checks clearer.
Furthermore, rename mext_check_arguments() to mext_check_adjust_range(),
as it only performs checks and length adjustments on the move extent
range. Finally, also change the print message for the non-existent file
check to be consistent with other unsupported checks.

Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Message-ID: <20251013015128.499308-8-yi.zhang@huaweicloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>

authored by

Zhang Yi and committed by
Theodore Ts'o
57c1df07 22218516

+43 -54
+43 -54
fs/ext4/move_extent.c
··· 501 501 return -EOPNOTSUPP; 502 502 } 503 503 504 - return 0; 505 - } 506 - 507 - /** 508 - * mext_check_arguments - Check whether move extent can be done 509 - * 510 - * @orig_inode: original inode 511 - * @donor_inode: donor inode 512 - * @orig_start: logical start offset in block for orig 513 - * @donor_start: logical start offset in block for donor 514 - * @len: the number of blocks to be moved 515 - * 516 - * Check the arguments of ext4_move_extents() whether the files can be 517 - * exchanged with each other. 518 - * Return 0 on success, or a negative error value on failure. 519 - */ 520 - static int 521 - mext_check_arguments(struct inode *orig_inode, 522 - struct inode *donor_inode, __u64 orig_start, 523 - __u64 donor_start, __u64 *len) 524 - { 525 - __u64 orig_eof, donor_eof; 504 + /* Ext4 move extent supports only extent based file */ 505 + if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS)) || 506 + !(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) { 507 + ext4_msg(sb, KERN_ERR, 508 + "Online defrag not supported for non-extent files"); 509 + return -EOPNOTSUPP; 510 + } 526 511 527 512 if (donor_inode->i_mode & (S_ISUID|S_ISGID)) { 528 - ext4_debug("ext4 move extent: suid or sgid is set" 529 - " to donor file [ino:orig %lu, donor %lu]\n", 513 + ext4_debug("ext4 move extent: suid or sgid is set to donor file [ino:orig %lu, donor %lu]\n", 530 514 orig_inode->i_ino, donor_inode->i_ino); 531 515 return -EINVAL; 532 516 } 533 517 534 - if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode)) 518 + if (IS_IMMUTABLE(donor_inode) || IS_APPEND(donor_inode)) { 519 + ext4_debug("ext4 move extent: donor should not be immutable or append file [ino:orig %lu, donor %lu]\n", 520 + orig_inode->i_ino, donor_inode->i_ino); 535 521 return -EPERM; 522 + } 536 523 537 524 /* Ext4 move extent does not support swap files */ 538 525 if (IS_SWAPFILE(orig_inode) || IS_SWAPFILE(donor_inode)) { 539 526 ext4_debug("ext4 move extent: The argument files should not be swap files [ino:orig %lu, donor %lu]\n", 540 - orig_inode->i_ino, donor_inode->i_ino); 527 + orig_inode->i_ino, donor_inode->i_ino); 541 528 return -ETXTBSY; 542 529 } 543 530 544 531 if (ext4_is_quota_file(orig_inode) || ext4_is_quota_file(donor_inode)) { 545 532 ext4_debug("ext4 move extent: The argument files should not be quota files [ino:orig %lu, donor %lu]\n", 546 - orig_inode->i_ino, donor_inode->i_ino); 547 - return -EOPNOTSUPP; 548 - } 549 - 550 - /* Ext4 move extent supports only extent based file */ 551 - if (!(ext4_test_inode_flag(orig_inode, EXT4_INODE_EXTENTS))) { 552 - ext4_debug("ext4 move extent: orig file is not extents " 553 - "based file [ino:orig %lu]\n", orig_inode->i_ino); 554 - return -EOPNOTSUPP; 555 - } else if (!(ext4_test_inode_flag(donor_inode, EXT4_INODE_EXTENTS))) { 556 - ext4_debug("ext4 move extent: donor file is not extents " 557 - "based file [ino:donor %lu]\n", donor_inode->i_ino); 533 + orig_inode->i_ino, donor_inode->i_ino); 558 534 return -EOPNOTSUPP; 559 535 } 560 536 ··· 539 563 return -EINVAL; 540 564 } 541 565 566 + return 0; 567 + } 568 + 569 + /* 570 + * Check the moving range of ext4_move_extents() whether the files can be 571 + * exchanged with each other, and adjust the length to fit within the file 572 + * size. Return 0 on success, or a negative error value on failure. 573 + */ 574 + static int mext_check_adjust_range(struct inode *orig_inode, 575 + struct inode *donor_inode, __u64 orig_start, 576 + __u64 donor_start, __u64 *len) 577 + { 578 + __u64 orig_eof, donor_eof; 579 + 542 580 /* Start offset should be same */ 543 581 if ((orig_start & ~(PAGE_MASK >> orig_inode->i_blkbits)) != 544 582 (donor_start & ~(PAGE_MASK >> orig_inode->i_blkbits))) { 545 - ext4_debug("ext4 move extent: orig and donor's start " 546 - "offsets are not aligned [ino:orig %lu, donor %lu]\n", 547 - orig_inode->i_ino, donor_inode->i_ino); 583 + ext4_debug("ext4 move extent: orig and donor's start offsets are not aligned [ino:orig %lu, donor %lu]\n", 584 + orig_inode->i_ino, donor_inode->i_ino); 548 585 return -EINVAL; 549 586 } 550 587 ··· 566 577 (*len > EXT_MAX_BLOCKS) || 567 578 (donor_start + *len >= EXT_MAX_BLOCKS) || 568 579 (orig_start + *len >= EXT_MAX_BLOCKS)) { 569 - ext4_debug("ext4 move extent: Can't handle over [%u] blocks " 570 - "[ino:orig %lu, donor %lu]\n", EXT_MAX_BLOCKS, 571 - orig_inode->i_ino, donor_inode->i_ino); 580 + ext4_debug("ext4 move extent: Can't handle over [%u] blocks [ino:orig %lu, donor %lu]\n", 581 + EXT_MAX_BLOCKS, 582 + orig_inode->i_ino, donor_inode->i_ino); 572 583 return -EINVAL; 573 584 } 574 585 ··· 583 594 else if (donor_eof < donor_start + *len - 1) 584 595 *len = donor_eof - donor_start; 585 596 if (!*len) { 586 - ext4_debug("ext4 move extent: len should not be 0 " 587 - "[ino:orig %lu, donor %lu]\n", orig_inode->i_ino, 588 - donor_inode->i_ino); 597 + ext4_debug("ext4 move extent: len should not be 0 [ino:orig %lu, donor %lu]\n", 598 + orig_inode->i_ino, donor_inode->i_ino); 589 599 return -EINVAL; 590 600 } 591 601 ··· 617 629 ext4_lblk_t d_start = donor_blk; 618 630 int ret; 619 631 620 - ret = mext_check_validity(orig_inode, donor_inode); 621 - if (ret) 622 - return ret; 623 - 624 632 /* Protect orig and donor inodes against a truncate */ 625 633 lock_two_nondirectories(orig_inode, donor_inode); 634 + 635 + ret = mext_check_validity(orig_inode, donor_inode); 636 + if (ret) 637 + goto unlock; 626 638 627 639 /* Wait for all existing dio workers */ 628 640 inode_dio_wait(orig_inode); ··· 630 642 631 643 /* Protect extent tree against block allocations via delalloc */ 632 644 ext4_double_down_write_data_sem(orig_inode, donor_inode); 633 - /* Check the filesystem environment whether move_extent can be done */ 634 - ret = mext_check_arguments(orig_inode, donor_inode, orig_blk, 635 - donor_blk, &len); 645 + /* Check and adjust the specified move_extent range. */ 646 + ret = mext_check_adjust_range(orig_inode, donor_inode, orig_blk, 647 + donor_blk, &len); 636 648 if (ret) 637 649 goto out; 638 650 o_end = o_start + len; ··· 713 725 714 726 ext4_free_ext_path(path); 715 727 ext4_double_up_write_data_sem(orig_inode, donor_inode); 728 + unlock: 716 729 unlock_two_nondirectories(orig_inode, donor_inode); 717 730 718 731 return ret;