]> git.hungrycats.org Git - linux/commitdiff
btrfs: add per-inode compression levels in xattrs
authorkoraynilay <koray.fra@gmail.com>
Tue, 3 Nov 2026 19:47:31 +0000 (20:47 +0100)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:39:28 +0000 (18:39 -0500)
Add support for specifying algo:level using
btrfs property set /path/to/file compression "algo:level".

Add a signed 8 bit prop_compress_level property to btrfs_inode, which
can support from level -128 to 127, plenty for the currently supported
algo:level(s).

Change prop_compression_apply() to use the already present
btrfs_match_compress_type() and btrfs_compress_str2level() to parse the
xattr, using a stack-based buffer and memcpy() to convert the
user-supplied xattr value to a NUL-terminated string to be used by
btrfs_compress_str2level().

Assisted-by: Gemini:3.1-pro antigravity-cli-1.1.5
Signed-off-by: koraynilay <koray.fra@gmail.com>
Link: https://lore.kernel.org/r/20261103194734.598182-4-koray.fra@gmail.com
fs/btrfs/btrfs_inode.h
fs/btrfs/inode.c
fs/btrfs/props.c

index 0782f917fcccb8b5f5c1263574880ba0cf3aa07f..58ee9bbd7b5bf8a6fb76b0effd64d07da5a94ea0 100644 (file)
@@ -137,6 +137,7 @@ struct btrfs_inode {
 
        /* Cached value of inode property 'compression'. */
        u8 prop_compress;
+       s8 prop_compress_level;
 
        /*
         * Force compression on the file using the defrag ioctl, could be
index 0bc668c9ec883330aa53fa45853185637a5f5745..2078a3b004fe9eeb80170909371051cec20734c4 100644 (file)
@@ -945,6 +945,11 @@ again:
                compress_level = inode->defrag_compress_level;
        } else if (inode->prop_compress) {
                compress_type = inode->prop_compress;
+               if (inode->prop_compress_level)
+                       compress_level = inode->prop_compress_level;
+               else if (compress_type != fs_info->compress_type)
+                       compress_level = 0;
+               /* else use fs_info->compress_level */
        }
 
        /* Compression level is applied here. */
@@ -2330,6 +2335,11 @@ static int run_delalloc_inline(struct btrfs_inode *inode, struct folio *locked_f
                        compress_level = inode->defrag_compress_level;
                } else if (inode->prop_compress) {
                        compress_type = inode->prop_compress;
+                       if (inode->prop_compress_level)
+                               compress_level = inode->prop_compress_level;
+                       else if (compress_type != fs_info->compress_type)
+                               compress_level = 0;
+                       /* else use fs_info->compress_level */
                }
                /*
                 * We need to pass blocksize and not i_size, otherwise we can't
index d196b73f4ffa13d1bda018a43483767e24487daf..effc2daa2e3dfdaa3652845255a01ea44473cfbd 100644 (file)
@@ -332,6 +332,10 @@ static int prop_compression_apply(struct btrfs_inode *inode, const char *value,
 {
        struct btrfs_fs_info *fs_info = inode->root->fs_info;
        int type;
+       int level = 0;
+       int ret = 0;
+       char value_str[BTRFS_COMPRESS_PROP_MAX_LEN];
+       int level_pos = 0;
 
        /* Reset to defaults */
        if (len == 0) {
@@ -351,21 +355,42 @@ static int prop_compression_apply(struct btrfs_inode *inode, const char *value,
                return 0;
        }
 
-       if (!strncmp("lzo", value, 3)) {
+       if (len >= BTRFS_COMPRESS_PROP_MAX_LEN)
+               return -EINVAL;
+
+       memcpy(value_str, value, len);
+       value_str[len] = '\0';
+
+       if (btrfs_match_compress_type(value_str, "lzo", true)) {
                type = BTRFS_COMPRESS_LZO;
                btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
-       } else if (!strncmp("zlib", value, 4)) {
+       } else if (btrfs_match_compress_type(value_str, "zlib", true)) {
                type = BTRFS_COMPRESS_ZLIB;
-       } else if (!strncmp("zstd", value, 4)) {
+               level_pos = 4;
+       } else if (btrfs_match_compress_type(value_str, "zstd", true)) {
                type = BTRFS_COMPRESS_ZSTD;
+               level_pos = 4;
                btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
        } else {
                return -EINVAL;
        }
 
+       /* Parse level if the algorithm supports it */
+       if (level_pos > 0) {
+               const char *level_str = value_str + level_pos;
+
+               /* and if user specified it */
+               if (level_str[0] == ':') {
+                       ret = btrfs_compress_str2level(type, level_str, &level);
+                       if (ret < 0)
+                               return ret;
+               }
+       }
+
        inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
        inode->flags |= BTRFS_INODE_COMPRESS;
        inode->prop_compress = type;
+       inode->prop_compress_level = level;
 
        return 0;
 }