Skip to contentSkip to conversation

bcachefs: split error messages of invalid compression into two lines

PATCH Applied Integral 2 participants 4 messages Apr 6, 2025

lore
Share & tools
lore

Integral

author
#0 lore raw

PATCH 1/2 bcachefs: split error messages of invalid compression into two lines

When an invalid compression type or level is passed as an argument to --compression, two error messages are squashed into one line:

Quoted 1 line

bcachefs format --compression=lzo bcachefs-comp.img

invalid option: invalid compression typecompression: parse error

> bcachefs format --compression=lz4:16 bcachefs-comp.img invalid option: invalid compression levelcompression: parse error

To resolve this issue, add a newline character at the end of the first error message to separate them into two lines.

---
 fs/bcachefs/compress.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/compress.c +2 -2
index 28ed32449913..d68c3c7896a3 100644
@@ -714,7 +714,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 
 	ret = match_string(bch2_compression_opts, -1, type_str);
 	if (ret < 0 && err)
-		prt_str(err, "invalid compression type");
+		prt_str(err, "invalid compression type\n");
 	if (ret < 0)
 		goto err;
 
@@ -729,7 +729,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 		if (!ret && level > 15)
 			ret = -EINVAL;
 		if (ret < 0 && err)
-			prt_str(err, "invalid compression level");
+			prt_str(err, "invalid compression level\n");
 		if (ret < 0)
 			goto err;
 
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH 2/2 bcachefs: refactor if statements in bch2_opt_compression_parse()

Refactor if statements in bch2_opt_compression_parse() to make it simpler & clearer.

---
 fs/bcachefs/compress.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
fs/bcachefs/compress.c +9 -9
index d68c3c7896a3..adf939b47107 100644
@@ -713,10 +713,11 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 	level_str = p;
 
 	ret = match_string(bch2_compression_opts, -1, type_str);
-	if (ret < 0 && err)
-		prt_str(err, "invalid compression type\n");
-	if (ret < 0)
+	if (ret < 0) {
+		if (err)
+			prt_str(err, "invalid compression type\n");
 		goto err;
+	}
 
 	opt.type = ret;
 
@@ -724,14 +725,13 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 		unsigned level;
 
 		ret = kstrtouint(level_str, 10, &level);
-		if (!ret && !opt.type && level)
-			ret = -EINVAL;
-		if (!ret && level > 15)
+		if (!ret && ((!opt.type && level) || level > 15))
 			ret = -EINVAL;
-		if (ret < 0 && err)
-			prt_str(err, "invalid compression level\n");
-		if (ret < 0)
+		if (ret < 0) {
+			if (err)
+				prt_str(err, "invalid compression level\n");
 			goto err;
+		}
 
 		opt.level = level;
 	}
Signature · 1 line
2.49.0
#3 lore raw
In reply to Integral
Quoted 6 lines · Integral

When an invalid compression type or level is passed as an argument to --compression, two error messages are squashed into one line:

Quoted 1 line

bcachefs format --compression=lzo bcachefs-comp.img

invalid option: invalid compression typecompression: parse error

Quoted 7 lines

> bcachefs format --compression=lz4:16 bcachefs-comp.img invalid option: invalid compression levelcompression: parse error

To resolve this issue, add a newline character at the end of the first error message to separate them into two lines.

Applied

I've also been working on consistent indentation for multiline error/log messages, so that we can see grouping better.

If you want to add that, the new helper is

printbuf_indent_add_nextline()

With that we can initialize a printbuffer and at the same time set it so that lines after the first are two space indented.

Quoted 29 lines
---
 fs/bcachefs/compress.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/compress.c +2 -2
index 28ed32449913..d68c3c7896a3 100644
@@ -714,7 +714,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 
 	ret = match_string(bch2_compression_opts, -1, type_str);
 	if (ret < 0 && err)
-		prt_str(err, "invalid compression type");
+		prt_str(err, "invalid compression type\n");
 	if (ret < 0)
 		goto err;
 
@@ -729,7 +729,7 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 		if (!ret && level > 15)
 			ret = -EINVAL;
 		if (ret < 0 && err)
-			prt_str(err, "invalid compression level");
+			prt_str(err, "invalid compression level\n");
 		if (ret < 0)
 			goto err;
 
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH 2/2] bcachefs: refactor if statements in bch2_opt_compression_parse()

In reply to Integral
Quoted 14 lines · Integral

Refactor if statements in bch2_opt_compression_parse() to make it simpler & clearer.

---
 fs/bcachefs/compress.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
fs/bcachefs/compress.c +0 -0
index d68c3c7896a3..adf939b47107 100644
@@ -713,10 +713,11 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 	level_str = p;

fs/bcachefs/compress.c int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res, · line 720

Quoted 10 lines
fs/bcachefs/compress.c +4 -3
 
 	ret = match_string(bch2_compression_opts, -1, type_str);
-	if (ret < 0 && err)
-		prt_str(err, "invalid compression type\n");
-	if (ret < 0)
+	if (ret < 0) {
+		if (err)
+			prt_str(err, "invalid compression type\n");
 		goto err;
+	}

I prefer the old code for this one

fs/bcachefs/compress.c int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res, · line 729

Quoted 12 lines
fs/bcachefs/compress.c +1 -3
 	opt.type = ret;
 
@@ -724,14 +725,13 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
 		unsigned level;
 
 		ret = kstrtouint(level_str, 10, &level);
-		if (!ret && !opt.type && level)
-			ret = -EINVAL;
-		if (!ret && level > 15)
+		if (!ret && ((!opt.type && level) || level > 15))
 			ret = -EINVAL;

This part does look better, though.

Quoted 14 lines
fs/bcachefs/compress.c +4 -3
-		if (ret < 0 && err)
-			prt_str(err, "invalid compression level\n");
-		if (ret < 0)
+		if (ret < 0) {
+			if (err)
+				prt_str(err, "invalid compression level\n");
 			goto err;
+		}
 
 		opt.level = level;
 	}
Signature · 1 line
2.49.0