]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: assert we can NOCOW the range in btrfs_truncate_block()
authorFilipe Manana <fdmanana@suse.com>
Wed, 9 Jul 2025 15:26:13 +0000 (16:26 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 21 Jul 2025 22:09:22 +0000 (00:09 +0200)
We call btrfs_check_nocow_lock() to see if we can NOCOW a block sized
range but we don't check later if we can NOCOW the whole range.
It's unexpected to be able to NOCOW a range smaller than blocksize, so
add an assertion to check the NOCOW range matches the blocksize.

Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/inode.c

index d7a2ea7d121f06ecb55133a7e857b6848bdb2e9b..d79a95b3760cf74d4989a29a30286e95b0f097ed 100644 (file)
@@ -4847,7 +4847,6 @@ int btrfs_truncate_block(struct btrfs_inode *inode, u64 offset, u64 start, u64 e
        pgoff_t index = (offset >> PAGE_SHIFT);
        struct folio *folio;
        gfp_t mask = btrfs_alloc_write_mask(mapping);
-       size_t write_bytes = blocksize;
        int ret = 0;
        const bool in_head_block = is_inside_block(offset, round_down(start, blocksize),
                                                   blocksize);
@@ -4899,8 +4898,12 @@ int btrfs_truncate_block(struct btrfs_inode *inode, u64 offset, u64 start, u64 e
        ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
                                          blocksize, false);
        if (ret < 0) {
+               size_t write_bytes = blocksize;
+
                if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) {
-                       /* For nocow case, no need to reserve data space */
+                       /* For nocow case, no need to reserve data space. */
+                       ASSERT(write_bytes == blocksize, "write_bytes=%zu blocksize=%u",
+                              write_bytes, blocksize);
                        only_release_metadata = true;
                } else {
                        goto out;