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 */
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);
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;