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>