]> git.hungrycats.org Git - linux/commitdiff
btrfs: props: keep compressing inodes with legacy compression property values misc-next/topics/compress-fixes
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:41:31 +0000 (18:41 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 3 Nov 2026 23:41:31 +0000 (18:41 -0500)
Older kernels matched btrfs.compression values by algorithm name prefix
only, both in the property validator and in prop_compression_apply(),
so values such as "zstd:banana", "zstd:", "zstd:3x", "zstdgarbage", or
anything longer than BTRFS_COMPRESS_PROP_MAX_LEN were accepted, stored
verbatim, and compressed with the named algorithm.  Such values are
still found on existing filesystems.

Since per-inode compression levels were added, prop_compression_apply()
parses the value strictly and rejects these with -EINVAL.  The property is
loaded with the inode, and inode_prop_iterator() only warns about a
failed apply, so these inodes silently stop compressing, with a warning
every time the inode is loaded.

Keep compressing them as older kernels did:  in prop_compression_apply(),
match the algorithm by name prefix again, and treat a level suffix that
does not parse, or a value too long to hold a valid level, as an
unspecified level (prop_compress_level = 0).  The compression paths then
pick the level as for a value with no level at all:  the mount option's
level if the mount option uses the same algorithm, otherwise the
algorithm's default level.

setxattr still goes through prop_compression_validate(), which keeps
rejecting these values, so only values that are already stored take this
path.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/props.c

index 0fabd64cffd2be57c4e59ae6cd1794f6c9c8e5dd..4d58c14ff9427030a6afec71b55d56f76fd837b6 100644 (file)
@@ -332,8 +332,8 @@ 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];
+       size_t copy_len = min(len, sizeof(value_str) - 1);
        int level_pos = 0;
 
        /* Reset to defaults */
@@ -354,19 +354,23 @@ static int prop_compression_apply(struct btrfs_inode *inode, const char *value,
                return 0;
        }
 
-       if (len >= BTRFS_COMPRESS_PROP_MAX_LEN)
-               return -EINVAL;
-
-       memcpy(value_str, value, len);
-       value_str[len] = '\0';
+       /*
+        * Older kernels matched only the algorithm name prefix and stored the
+        * rest of the value without validation, and inodes with such values
+        * are still out there.  Keep compressing them with the named
+        * algorithm, as those kernels did:  match the prefix here, and leave
+        * the strict checks to prop_compression_validate().
+        */
+       memcpy(value_str, value, copy_len);
+       value_str[copy_len] = '\0';
 
-       if (btrfs_match_compress_type(value_str, "lzo", true)) {
+       if (strncmp(value_str, "lzo", 3) == 0) {
                type = BTRFS_COMPRESS_LZO;
                btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
-       } else if (btrfs_match_compress_type(value_str, "zlib", true)) {
+       } else if (strncmp(value_str, "zlib", 4) == 0) {
                type = BTRFS_COMPRESS_ZLIB;
                level_pos = 4;
-       } else if (btrfs_match_compress_type(value_str, "zstd", true)) {
+       } else if (strncmp(value_str, "zstd", 4) == 0) {
                type = BTRFS_COMPRESS_ZSTD;
                level_pos = 4;
                btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
@@ -378,12 +382,13 @@ static int prop_compression_apply(struct btrfs_inode *inode, const char *value,
        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;
-               }
+               /*
+                * and if user specified it.  A level that does not parse, or a
+                * value too long to be one, counts as unspecified.
+                */
+               if (len == copy_len && level_str[0] == ':' &&
+                   btrfs_compress_str2level(type, level_str, &level) < 0)
+                       level = 0;
        }
 
        inode->flags &= ~BTRFS_INODE_NOCOMPRESS;