]> git.hungrycats.org Git - linux/commit
btrfs: dont propagate nodatacow mount option on files with inherited compression
authorFilipe Manana <fdmanana@suse.com>
Mon, 2 Nov 2026 19:55:04 +0000 (19:55 +0000)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 17:10:56 +0000 (12:10 -0500)
commitf6f89b848e57b05d5cbda97b4bcd8670519930e0
treed535f9113c718637f951137c01d68f36b4bdcfb6
parent83cd81353093be02aa7e60874c413ec340e55bce
btrfs: dont propagate nodatacow mount option on files with inherited compression

If we set the compression property on a directory and the filesystem was
mounted with the nodatacow option, then when creating a file under that
directory we get both the compression and nodatacow attributes set on the
file. These are incompatible attributes as compression requires COW.

This has a couple unexpected behaviours:

1) If later a user sets another attribute on the file, like immutability
   or append writing mode for example, the chattr command will fail with
   -EINVAL since we in the setattr callback (btrfs_fileattr_set()) we do
   some validation, through a call to check_fsflags(), that rejects having
   both the nodatacow and compression flags set.

   I.e, doing a simple:

     $ chattr +i file
     /usr/bin/chattr: Invalid argument while setting flags on file

2) The file ends up not being compressed, which is unexpected since the
   compression attribute set on a directory/file should have precedence
   over the global nodatacow mount option.

Example reproducer:

   $ cat test.sh
   #!/bin/bash

   DEV=/dev/sdi
   MNT=/mnt/sdi

   mkfs.btrfs -f $DEV
   mount -o nodatacow $DEV $MNT

  # Create a directory with the compression attribute, so that all files
  # created under it inherit the compression attribute.
  mkdir $MNT/dir
  chattr +c $MNT/dir

  # Now create a file with some data inside the directory.
  xfs_io -f -c "pwrite 0 128K" $MNT/dir/foo

  # Now try to make the file immutable, it should work.
  chattr +i $MNT/dir/foo

  umount $MNT

  # Check the file has a compressed extent. It should have since
  # despite the fs being mounted with nodatacow, the directory has
  # the compression flag set and so that should take priority and
  # it's what makes sense.
  btrfs inspect-internal dump-tree -t 5 $DEV | grep -A 5 'EXTENT_DATA 0'

Running it:

  $ ./test.sh
  (...)
  chattr: Invalid argument while setting flags on /mnt/sdi/dir/foo
        item 11 key (258 EXTENT_DATA 0) itemoff 15529 itemsize 53
             generation 9 type 1 (regular)
             extent data disk byte 13631488 nr 131072
             extent data offset 0 nr 131072 ram 131072
             extent compression 0 (none)
             extent encryption 0

Fix this by updating btrfs_create_new_inode() to not apply the mount
options nodatacow and nodatasum on a file that has inherited compression
from its parent directory.

A test case will follow soon for fstests.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Link: https://lore.kernel.org/r/1c729c63075e727042efdeb07df10a70b0ba1a0d.1793647368.git.fdmanana@suse.com
Reviewed-by: Qu Wenruo <wqu@suse.com>
fs/btrfs/inode.c