linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chengguang Xu <cgxu519@mykernel.net>
To: clm@fb.com, josef@toxicpanda.com, dsterba@suse.com
Cc: linux-btrfs@vger.kernel.org, Chengguang Xu <cgxu519@mykernel.net>
Subject: [PATCH 2/3] btrfs: code cleanup for compression type
Date: Sat,  5 Oct 2019 13:17:35 +0800	[thread overview]
Message-ID: <20191005051736.29857-2-cgxu519@mykernel.net> (raw)
In-Reply-To: <20191005051736.29857-1-cgxu519@mykernel.net>

Let BTRFS_COMPRESS_TYPES represents the total number
of cmpressoin types and fix related calling places.
It will be more safe when adding new compression type
in the future.

Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>
---
 fs/btrfs/compression.c  |  2 ++
 fs/btrfs/compression.h  | 12 ++++++------
 fs/btrfs/ioctl.c        |  2 +-
 fs/btrfs/tree-checker.c |  4 ++--
 4 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index d70c46407420..93deaf0cc2b8 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -39,6 +39,8 @@ const char* btrfs_compress_type2str(enum btrfs_compression_type type)
 	case BTRFS_COMPRESS_ZSTD:
 	case BTRFS_COMPRESS_NONE:
 		return btrfs_compress_types[type];
+	default:
+		break;
 	}
 
 	return NULL;
diff --git a/fs/btrfs/compression.h b/fs/btrfs/compression.h
index dd392278ab3f..091ff3f986e5 100644
--- a/fs/btrfs/compression.h
+++ b/fs/btrfs/compression.h
@@ -101,11 +101,11 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 unsigned int btrfs_compress_str2level(unsigned int type, const char *str);
 
 enum btrfs_compression_type {
-	BTRFS_COMPRESS_NONE  = 0,
-	BTRFS_COMPRESS_ZLIB  = 1,
-	BTRFS_COMPRESS_LZO   = 2,
-	BTRFS_COMPRESS_ZSTD  = 3,
-	BTRFS_COMPRESS_TYPES = 3,
+	BTRFS_COMPRESS_NONE,
+	BTRFS_COMPRESS_ZLIB,
+	BTRFS_COMPRESS_LZO,
+	BTRFS_COMPRESS_ZSTD,
+	BTRFS_COMPRESS_TYPES
 };
 
 struct workspace_manager {
@@ -163,7 +163,7 @@ struct btrfs_compress_op {
 };
 
 /* The heuristic workspaces are managed via the 0th workspace manager */
-#define BTRFS_NR_WORKSPACE_MANAGERS	(BTRFS_COMPRESS_TYPES + 1)
+#define BTRFS_NR_WORKSPACE_MANAGERS	BTRFS_COMPRESS_TYPES
 
 extern const struct btrfs_compress_op btrfs_heuristic_compress;
 extern const struct btrfs_compress_op btrfs_zlib_compress;
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index de730e56d3f5..8c7196ed7ae0 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1411,7 +1411,7 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
 		return -EINVAL;
 
 	if (do_compress) {
-		if (range->compress_type > BTRFS_COMPRESS_TYPES)
+		if (range->compress_type >= BTRFS_COMPRESS_TYPES)
 			return -EINVAL;
 		if (range->compress_type)
 			compress_type = range->compress_type;
diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index f28f9725cef1..2d91c34bbf63 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -168,11 +168,11 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 	 * Support for new compression/encryption must introduce incompat flag,
 	 * and must be caught in open_ctree().
 	 */
-	if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
+	if (btrfs_file_extent_compression(leaf, fi) >= BTRFS_COMPRESS_TYPES) {
 		file_extent_err(leaf, slot,
 	"invalid compression for file extent, have %u expect range [0, %u]",
 			btrfs_file_extent_compression(leaf, fi),
-			BTRFS_COMPRESS_TYPES);
+			BTRFS_COMPRESS_TYPES - 1);
 		return -EUCLEAN;
 	}
 	if (btrfs_file_extent_encryption(leaf, fi)) {
-- 
2.21.0




  reply	other threads:[~2019-10-05  5:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-05  5:17 [PATCH 1/3] btrfs: remove unnecessary hash_init() Chengguang Xu
2019-10-05  5:17 ` Chengguang Xu [this message]
2019-10-06 23:28   ` [PATCH 2/3] btrfs: code cleanup for compression type David Sterba
2019-10-07 13:47     ` Chengguang Xu
2019-10-05  5:17 ` [PATCH 3/3] btrfs: using enum to replace macro Chengguang Xu
2019-10-06 23:29   ` David Sterba
2019-10-07 15:44 ` [PATCH 1/3] btrfs: remove unnecessary hash_init() David Sterba
2019-10-08  2:56   ` Chengguang Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191005051736.29857-2-cgxu519@mykernel.net \
    --to=cgxu519@mykernel.net \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).