]> git.hungrycats.org Git - linux/commitdiff
btrfs: fix chattr failing on nodatasum files that have the compress flag
authorHengyu Liang <hengyul@cs.unc.edu>
Sun, 11 Oct 2026 15:58:15 +0000 (11:58 -0400)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:15 +0000 (21:36 +0100)
Commit cf1afec09e9f ("btrfs: validate properties before setting them")
made btrfs_set_prop() validate a property before setting it. For the
compression property, the validation fails with -EINVAL when the inode
has NODATASUM.

However, chattr writes back all the flags it read from the file, and
btrfs_fileattr_set() sets the compression property whenever the new
flags include FS_COMPR_FL, even when the compression flags do not
change. A file created on a filesystem mounted with nodatasum has
NODATASUM and still inherits the compress flag from its directory. As
of now, chattr fails on such a file when it changes any other flag.

The issue can be reproduced with:

    mkfs.btrfs -f /dev/sdb
    mount -o nodatasum /dev/sdb /mnt
    mkdir /mnt/dir
    chattr +c /mnt/dir
    touch /mnt/dir/file
    chattr +A /mnt/dir/file

Before commit cf1afec09e9f ("btrfs: validate properties before setting
them"), the last command succeeds.

After that commit, it fails with EINVAL.

This patch will make btrfs_fileattr_set() leave the compression flags
and the compression property alone when the call changes neither
FS_COMPR_FL nor FS_NOCOMP_FL.

Fixes: cf1afec09e9f ("btrfs: validate properties before setting them")
CC: stable@vger.kernel.org # 7.2+
Suggested-by: Qu Wenruo <wqu@suse.com>
Assisted-by: LLM
Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ioctl.c

index f3e2afe221be35feff84bd79ef3003995923df74..bfa6f08e6dd5b09e5393b8cfa3b8c4cc27ab7ada 100644 (file)
@@ -375,6 +375,19 @@ int btrfs_fileattr_set(const struct mnt_idmap *idmap,
                }
        }
 
+       /*
+        * Callers write back the whole flag set, so only touch the compression
+        * flags and the compression property if FS_COMPR_FL or FS_NOCOMP_FL
+        * changes.
+        */
+       if (!((fsflags ^ old_fsflags) & (FS_COMPR_FL | FS_NOCOMP_FL))) {
+               /* 1 item for the inode */
+               trans = btrfs_start_transaction(root, 1);
+               if (IS_ERR(trans))
+                       return PTR_ERR(trans);
+               goto update_flags;
+       }
+
        /*
         * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
         * flag may be changed automatically if compression code won't make