All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 3/5] btrfs: Convert compression description strings into system flags.
@ 2022-01-16  8:50 Li Zhang
  2022-01-19  6:07   ` kernel test robot
  0 siblings, 1 reply; 3+ messages in thread
From: Li Zhang @ 2022-01-16  8:50 UTC (permalink / raw)
  To: linux-btrfs; +Cc: zhanglikernel

1. Calls the function in compress.c to Clean
up the redundant code that parses the compression
description string into system flags.

2. Convert the compressed description string
to a combination of compression type and compression level.

Signed-off-by: Li Zhang <zhanglikernel@gmail.com>
---
 fs/btrfs/props.c | 52 +++++++++++++++++++++++----------
 fs/btrfs/super.c | 89 ++++++++++++++++++++++++--------------------------------
 2 files changed, 74 insertions(+), 67 deletions(-)

diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index 1a6d2d5..f07be37 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -272,13 +272,16 @@ static int prop_compression_apply(struct inode *inode, const char *value,
 {
 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
 	int type;
+    char* clean_value;
+    int ret;
 
 	/* Reset to defaults */
 	if (len == 0) {
 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
 		BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
-		return 0;
+		ret = 0;
+        goto out;
 	}
 
 	/* Set NOCOMPRESS flag */
@@ -287,27 +290,44 @@ static int prop_compression_apply(struct inode *inode, const char *value,
 		BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
 		BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
 		BTRFS_I(inode)->prop_compress = BTRFS_COMPRESS_NONE;
-
-		return 0;
+        ret = 0;
+        goto out;
 	}
 
-	if (!strncmp("lzo", value, 3)) {
-		type = BTRFS_COMPRESS_LZO;
-		btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
-	} else if (!strncmp("zlib", value, 4)) {
-		type = BTRFS_COMPRESS_ZLIB;
-	} else if (!strncmp("zstd", value, 4)) {
-		type = BTRFS_COMPRESS_ZSTD;
-		btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
-	} else {
-		return -EINVAL;
-	}
+    /*
+     * The value contains gibberish at the end, we should clean it up.
+     */
+    clean_value = kzalloc(len + 1, GFP_NOFS);
+    if(!clean_value){
+        ret = -ENOMEM;
+        goto out;
+    }
+    strncpy(clean_value, value, len);
+
+    if(!btrfs_compress_is_valid_type(value, len)){
+        ret = -EINVAL;
+        goto free_clean_value;
+    }
+    type = btrfs_compress_str2type_level(clean_value, NULL, NULL);
+    switch(btrfs_compress_type(type)){
+        case BTRFS_COMPRESS_LZO:
+		    btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
+            break;
+        case BTRFS_COMPRESS_ZSTD:
+		    btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
+            break;
+        default:
+            break;
+    }
 
 	BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
 	BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
 	BTRFS_I(inode)->prop_compress = type;
-
-	return 0;
+    ret = 0;
+free_clean_value:
+    kfree(clean_value);
+out:
+	return ret;
 }
 
 static const char *prop_compression_extract(struct inode *inode)
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 0ec09fe..fea7e2d 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -574,6 +574,7 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
 	int saved_compress_level;
 	bool saved_compress_force;
 	int no_compress = 0;
+    unsigned compress_type_flag, compress_level_flag;
 
 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
@@ -660,62 +661,48 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
 			saved_compress_force =
 				btrfs_test_opt(info, FORCE_COMPRESS);
 			saved_compress_level = info->compress_level;
-			if (token == Opt_compress ||
-			    token == Opt_compress_force ||
-			    strncmp(args[0].from, "zlib", 4) == 0) {
-				compress_type = "zlib";
-
-				info->compress_type = BTRFS_COMPRESS_ZLIB;
-				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
-				/*
-				 * args[0] contains uninitialized data since
-				 * for these tokens we don't expect any
-				 * parameter.
-				 */
-				if (token != Opt_compress &&
-				    token != Opt_compress_force)
-					info->compress_level =
-					  btrfs_compress_str2level(
-							BTRFS_COMPRESS_ZLIB,
-							args[0].from + 4);
-				btrfs_set_opt(info->mount_opt, COMPRESS);
-				btrfs_clear_opt(info->mount_opt, NODATACOW);
-				btrfs_clear_opt(info->mount_opt, NODATASUM);
-				no_compress = 0;
-			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
-				compress_type = "lzo";
-				info->compress_type = BTRFS_COMPRESS_LZO;
+            if( token == Opt_compress || token == Opt_compress_force ){
+                /*
+                 * defaule compression type
+                 */
+                compress_type = "zlib";
+            } else {
+                compress_type = args[0].from;
+            }
+            if(!strncmp(compress_type, "no", 2)){
+                /*
+                 * Handle no compression specially
+                 */
+                btrfs_clear_opt(info->mount_opt, COMPRESS);
+                btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
+                compress_force = false;
+				info->compress_type = 0;
 				info->compress_level = 0;
+                no_compress++;
+            } else {
+                if(!btrfs_compress_is_valid_type(
+                    compress_type, strlen(compress_type))){
+                    ret = -EINVAL;
+                    goto out;
+                }
+                btrfs_compress_str2type_level(compress_type, 
+                    &compress_type_flag, &compress_level_flag);
+                switch(compress_type_flag){
+                    case BTRFS_COMPRESS_ZSTD:
+				        btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
+                        break;
+                    case BTRFS_COMPRESS_LZO:
+				        btrfs_set_fs_incompat(info, COMPRESS_LZO);
+                        break;
+                }
 				btrfs_set_opt(info->mount_opt, COMPRESS);
 				btrfs_clear_opt(info->mount_opt, NODATACOW);
 				btrfs_clear_opt(info->mount_opt, NODATASUM);
-				btrfs_set_fs_incompat(info, COMPRESS_LZO);
-				no_compress = 0;
-			} else if (strncmp(args[0].from, "zstd", 4) == 0) {
-				compress_type = "zstd";
-				info->compress_type = BTRFS_COMPRESS_ZSTD;
-				info->compress_level =
-					btrfs_compress_str2level(
-							 BTRFS_COMPRESS_ZSTD,
-							 args[0].from + 4);
-				btrfs_set_opt(info->mount_opt, COMPRESS);
-				btrfs_clear_opt(info->mount_opt, NODATACOW);
-				btrfs_clear_opt(info->mount_opt, NODATASUM);
-				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
 				no_compress = 0;
-			} else if (strncmp(args[0].from, "no", 2) == 0) {
-				compress_type = "no";
-				info->compress_level = 0;
-				info->compress_type = 0;
-				btrfs_clear_opt(info->mount_opt, COMPRESS);
-				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
-				compress_force = false;
-				no_compress++;
-			} else {
-				ret = -EINVAL;
-				goto out;
-			}
-
+				info->compress_type = compress_type_flag;
+				info->compress_level = compress_level_flag;
+            }
+            
 			if (compress_force) {
 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
 			} else {
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-19  6:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-16  8:50 [PATCH 3/5] btrfs: Convert compression description strings into system flags Li Zhang
2022-01-19  6:07 ` [btrfs] 09d40ccb5b: xfstests.btrfs.048.fail kernel test robot
2022-01-19  6:07   ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.