]> git.hungrycats.org Git - linux/commitdiff
btrfs: preserve btrfs.compression when setting inode flags
authorkoraynilay <koray.fra@gmail.com>
Tue, 3 Nov 2026 19:47:34 +0000 (20:47 +0100)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:39:29 +0000 (18:39 -0500)
Commit e8a0095c7df170 ("btrfs: preserve the compression property when
other inode flags change") fixed an issue where any call to
FS_IOC_SETFLAGS (e.g. via chattr) would overwrite the btrfs.compression
property with the mount default compression type, even when no flag bits
change and even for flags unrelated to compression.

Update the fixed code to also preserve the newly introduced level
specification by using the new btrfs_compress_typelevel2str() helper.

Also move BTRFS_COMPRESS_PROP_MAX_LEN from props.c to props.h so it can
be used by ioctl.c, since it now requires a temporary buffer to write the
property string to.

[ Commit message and code adapted from Zygo's patch for this same issue ]

Reported-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Suggested-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Assisted-by: Gemini:3.1-pro antigravity-cli-1.1.7
Signed-off-by: koraynilay <koray.fra@gmail.com>
Link: https://lore.kernel.org/r/20261103194734.598182-7-koray.fra@gmail.com
fs/btrfs/ioctl.c
fs/btrfs/props.c
fs/btrfs/props.h

index 215e3220df7faffa2066d6dc48ee37473e6467d2..ecf0f334514db386881a84e7c938fd2f91bdb378 100644 (file)
@@ -288,6 +288,7 @@ int btrfs_fileattr_set(const struct mnt_idmap *idmap,
        unsigned int fsflags, old_fsflags;
        int ret;
        const char *comp = NULL;
+       char comp_buf[BTRFS_COMPRESS_PROP_MAX_LEN];
        u32 inode_flags;
        bool prop_set = false;
 
@@ -397,7 +398,6 @@ int btrfs_fileattr_set(const struct mnt_idmap *idmap,
                inode_flags &= ~BTRFS_INODE_COMPRESS;
                inode_flags |= BTRFS_INODE_NOCOMPRESS;
        } else if (fsflags & FS_COMPR_FL) {
-               enum btrfs_compression_type comp_type;
 
                if (IS_SWAPFILE(&inode->vfs_inode))
                        return -ETXTBSY;
@@ -406,22 +406,23 @@ int btrfs_fileattr_set(const struct mnt_idmap *idmap,
                inode_flags &= ~BTRFS_INODE_NOCOMPRESS;
 
                /*
-                * Keep the algorithm recorded in the compression property,
-                * otherwise changing an unrelated attribute would reset it to
-                * the mount default, since FS_IOC_SETFLAGS callers write back
-                * the whole flag set they got from FS_IOC_GETFLAGS and that
-                * includes FS_COMPR_FL for any inode carrying the property.
+                * If compression is already enabled, we must reconstruct the
+                * full "algo:level" property string and write it back.
+                * Otherwise changing an unrelated attribute would reset it to
+                * the mount default and drop the level, since FS_IOC_SETFLAGS
+                * callers write back the whole flag set they got from
+                * FS_IOC_GETFLAGS, and that includes FS_COMPR_FL.
                 *
                 * Inodes with the compress flag set but no property keep using
                 * the mount default, so they behave as before.
                 */
-               if (inode->prop_compress)
-                       comp_type = inode->prop_compress;
-               else if (fs_info->compress_type)
-                       comp_type = fs_info->compress_type;
-               else
-                       comp_type = BTRFS_COMPRESS_ZLIB;
-               comp = btrfs_compress_type2str(comp_type);
+               comp = btrfs_compress_typelevel2str(inode->prop_compress,
+                                                   inode->prop_compress_level,
+                                                   comp_buf, sizeof(comp_buf));
+               if (!comp || comp[0] == 0)
+                       comp = btrfs_compress_type2str(fs_info->compress_type);
+               if (!comp || comp[0] == 0)
+                       comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
        } else {
                inode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
        }
index 3edda1f11a741a6f2e58773486107662a98f2e76..05e594ce613b0562cbd6df944f45f66446849174 100644 (file)
 #include "super.h"
 #include "dir-item.h"
 
-/*
- * Max length of compression algorithm:level string.
- *
- * For now the longest possible string is "zstd:-15", which is
- * 8 characters + 1 terminating null byte.
- * Rounding it up to the closest power of 2 gives 16.
- */
-#define BTRFS_COMPRESS_PROP_MAX_LEN 16
-
 #define BTRFS_PROP_HANDLERS_HT_BITS 8
 static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
 
index 15d9a025c923db65193d5f6266d205e744adceed..93b55a85a728a9030dd66770a4c811c1086c3206 100644 (file)
@@ -9,6 +9,15 @@
 #include <linux/types.h>
 #include <linux/compiler_types.h>
 
+/*
+ * Max length of compression algorithm:level string.
+ *
+ * For now the longest possible string is "zstd:-15", which is
+ * 8 characters + 1 terminating null byte.
+ * Rounding it up to the closest power of 2 gives 16.
+ */
+#define BTRFS_COMPRESS_PROP_MAX_LEN 16
+
 struct btrfs_inode;
 struct btrfs_path;
 struct btrfs_trans_handle;