bcachefs: bch2_prt_compression_type()
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 7 Jan 2024 02:01:47 +0000 (21:01 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 21 Jan 2024 11:01:45 +0000 (06:01 -0500)
bounds checking helper, since compression types are extensible

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/compress.h
fs/bcachefs/extents.c
fs/bcachefs/opts.c
fs/bcachefs/opts.h
fs/bcachefs/sysfs.c

index 607fd5e..58c2eb4 100644 (file)
@@ -47,6 +47,14 @@ static inline enum bch_compression_type bch2_compression_opt_to_type(unsigned v)
        return __bch2_compression_opt_to_type[bch2_compression_decode(v).type];
 }
 
+static inline void bch2_prt_compression_type(struct printbuf *out, enum bch_compression_type type)
+{
+       if (type < BCH_COMPRESSION_TYPE_NR)
+               prt_str(out, __bch2_compression_types[type]);
+       else
+               prt_printf(out, "(invalid compression type %u)", type);
+}
+
 int bch2_bio_uncompress_inplace(struct bch_fs *, struct bio *,
                                struct bch_extent_crc_unpacked *);
 int bch2_bio_uncompress(struct bch_fs *, struct bio *, struct bio *,
index 82ec056..edb1e32 100644 (file)
@@ -1018,12 +1018,12 @@ void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
                        struct bch_extent_crc_unpacked crc =
                                bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
 
-                       prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress %s",
+                       prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress ",
                               crc.compressed_size,
                               crc.uncompressed_size,
                               crc.offset, crc.nonce,
-                              bch2_csum_types[crc.csum_type],
-                              bch2_compression_types[crc.compression_type]);
+                              bch2_csum_types[crc.csum_type]);
+                       bch2_prt_compression_type(out, crc.compression_type);
                        break;
                }
                case BCH_EXTENT_ENTRY_stripe_ptr: {
index 6aaf78d..b1ed0b9 100644 (file)
@@ -52,7 +52,7 @@ const char * const bch2_csum_opts[] = {
        NULL
 };
 
-const char * const bch2_compression_types[] = {
+const char * const __bch2_compression_types[] = {
        BCH_COMPRESSION_TYPES()
        NULL
 };
index 67e98e0..7414c56 100644 (file)
@@ -18,7 +18,7 @@ extern const char * const bch2_sb_compat[];
 extern const char * const __bch2_btree_ids[];
 extern const char * const bch2_csum_types[];
 extern const char * const bch2_csum_opts[];
-extern const char * const bch2_compression_types[];
+extern const char * const __bch2_compression_types[];
 extern const char * const bch2_compression_opts[];
 extern const char * const bch2_str_hash_types[];
 extern const char * const bch2_str_hash_opts[];
index 434961e..553190d 100644 (file)
@@ -21,6 +21,7 @@
 #include "btree_gc.h"
 #include "buckets.h"
 #include "clock.h"
+#include "compress.h"
 #include "disk_groups.h"
 #include "ec.h"
 #include "inode.h"
@@ -330,7 +331,7 @@ static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c
        prt_newline(out);
 
        for (unsigned i = 0; i < ARRAY_SIZE(s); i++) {
-               prt_str(out, bch2_compression_types[i]);
+               bch2_prt_compression_type(out, i);
                prt_tab(out);
 
                prt_human_readable_u64(out, s[i].sectors_compressed << 9);