]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: consistently round up or down i_size in btrfs_truncate()
authorFilipe Manana <fdmanana@suse.com>
Sun, 12 Oct 2025 09:26:40 +0000 (10:26 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Nov 2025 20:59:08 +0000 (21:59 +0100)
We're using different ways to round down the i_size by sector size, one
with a bitwise and with a negated mask and another with ALIGN_DOWN(), and
using ALIGN() to round up.

Replace these uses with the round_down() and round_up() macros which have
have names that make it clear the direction of the rounding (unlike the
ALIGN() macro) and getting rid of the bitwise and, negated mask and local
variable for the mask.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Anand Jain <asj@kernel.org>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/inode.c

index 15131873f73da9a65aa89aba32f287f18ca64299..865a288e0e29a414bfbffde232199940d1f0fa8c 100644 (file)
@@ -7662,12 +7662,12 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
        struct btrfs_block_rsv rsv;
        int ret;
        struct btrfs_trans_handle *trans;
-       u64 mask = fs_info->sectorsize - 1;
        const u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
 
        if (!skip_writeback) {
                ret = btrfs_wait_ordered_range(inode,
-                                              inode->vfs_inode.i_size & (~mask),
+                                              round_down(inode->vfs_inode.i_size,
+                                                         fs_info->sectorsize),
                                               (u64)-1);
                if (ret)
                        return ret;
@@ -7733,7 +7733,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
        while (1) {
                struct extent_state *cached_state = NULL;
                const u64 new_size = inode->vfs_inode.i_size;
-               const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
+               const u64 lock_start = round_down(new_size, fs_info->sectorsize);
 
                control.new_size = new_size;
                btrfs_lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
@@ -7743,7 +7743,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
                 * block of the extent just the way it is.
                 */
                btrfs_drop_extent_map_range(inode,
-                                           ALIGN(new_size, fs_info->sectorsize),
+                                           round_up(new_size, fs_info->sectorsize),
                                            (u64)-1, false);
 
                ret = btrfs_truncate_inode_items(trans, root, &control);