bcachefs: add bcachefs xxhash support
authorjpsollie <janpieter.sollie@edpnet.be>
Thu, 17 Jun 2021 11:42:09 +0000 (13:42 +0200)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:07 +0000 (17:09 -0400)
xxhash is a much faster algorithm compared to crc32.
could be used to speed up checksum calculation.
xxhash 64-bit only, as it is much faster on 64-bit CPUs compared to xxh32.

Signed-off-by: jpsollie <janpieter.sollie@edpnet.be>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/Kconfig
fs/bcachefs/bcachefs_format.h
fs/bcachefs/checksum.c
fs/bcachefs/checksum.h

index 151c4b10d54351ee2d0f141f7d88a04efa3b2c43..bfe7e6c9c0649df42d26542cc2d0085eb782a1aa 100644 (file)
@@ -19,6 +19,7 @@ config BCACHEFS_FS
        select KEYS
        select RAID6_PQ
        select XOR_BLOCKS
+       select XXHASH
        select SRCU
        help
        The bcachefs filesystem - a modern, copy on write filesystem, with
index 6cfb8959d57940f72d64f1a6206cbe0ea214b0bf..63f7c7c8f390b5c311933c113073554f237280b8 100644 (file)
@@ -1460,7 +1460,8 @@ enum bch_csum_type {
        BCH_CSUM_CHACHA20_POLY1305_128  = 4,
        BCH_CSUM_CRC32C                 = 5,
        BCH_CSUM_CRC64                  = 6,
-       BCH_CSUM_NR                     = 7,
+       BCH_CSUM_XXHASH                 = 7,
+       BCH_CSUM_NR                     = 8,
 };
 
 static const unsigned bch_crc_bytes[] = {
@@ -1469,6 +1470,7 @@ static const unsigned bch_crc_bytes[] = {
        [BCH_CSUM_CRC32C]                       = 4,
        [BCH_CSUM_CRC64_NONZERO]                = 8,
        [BCH_CSUM_CRC64]                        = 8,
+       [BCH_CSUM_XXHASH]                       = 8,
        [BCH_CSUM_CHACHA20_POLY1305_80]         = 10,
        [BCH_CSUM_CHACHA20_POLY1305_128]        = 16,
 };
@@ -1487,7 +1489,8 @@ static inline _Bool bch2_csum_type_is_encryption(enum bch_csum_type type)
 #define BCH_CSUM_OPTS()                        \
        x(none,                 0)      \
        x(crc32c,               1)      \
-       x(crc64,                2)
+       x(crc64,                2)      \
+       x(xxhash,               3)
 
 enum bch_csum_opts {
 #define x(t, n) BCH_CSUM_OPT_##t = n,
index 6c23a9073dbfa94044f4266120780d3def4800d6..d20924e579bffd9d3b34b1dee7e35857eb22d346 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <linux/crc32c.h>
 #include <linux/crypto.h>
+#include <linux/xxhash.h>
 #include <linux/key.h>
 #include <linux/random.h>
 #include <linux/scatterlist.h>
@@ -26,6 +27,7 @@
 struct bch2_checksum_state {
        union {
                u64 seed;
+               struct xxh64_state h64state;
        };
        unsigned int type;
 };
@@ -44,6 +46,9 @@ static void bch2_checksum_init(struct bch2_checksum_state *state)
        case BCH_CSUM_CRC64_NONZERO:
                state->seed = U64_MAX;
                break;
+       case BCH_CSUM_XXHASH:
+               xxh64_reset(&state->h64state, 0);
+               break;
        default:
                BUG();
        }
@@ -60,6 +65,8 @@ static u64 bch2_checksum_final(const struct bch2_checksum_state *state)
                return state->seed ^ U32_MAX;
        case BCH_CSUM_CRC64_NONZERO:
                return state->seed ^ U64_MAX;
+       case BCH_CSUM_XXHASH:
+               return xxh64_digest(&state->h64state);
        default:
                BUG();
        }
@@ -78,6 +85,9 @@ static void bch2_checksum_update(struct bch2_checksum_state *state, const void *
        case BCH_CSUM_CRC64:
                state->seed = crc64_be(state->seed, data, len);
                break;
+       case BCH_CSUM_XXHASH:
+               xxh64_update(&state->h64state, data, len);
+               break;
        default:
                BUG();
        }
@@ -155,6 +165,7 @@ struct bch_csum bch2_checksum(struct bch_fs *c, unsigned type,
        case BCH_CSUM_CRC32C_NONZERO:
        case BCH_CSUM_CRC64_NONZERO:
        case BCH_CSUM_CRC32C:
+       case BCH_CSUM_XXHASH:
        case BCH_CSUM_CRC64: {
                struct bch2_checksum_state state;
 
@@ -206,6 +217,7 @@ static struct bch_csum __bch2_checksum_bio(struct bch_fs *c, unsigned type,
        case BCH_CSUM_CRC32C_NONZERO:
        case BCH_CSUM_CRC64_NONZERO:
        case BCH_CSUM_CRC32C:
+       case BCH_CSUM_XXHASH:
        case BCH_CSUM_CRC64: {
                struct bch2_checksum_state state;
 
index 728b7ef1a1490f7bd2e38db93df4729d3eec9dd2..6841fb16568a652779be0ed795768b5d5d8c79e9 100644 (file)
@@ -83,6 +83,8 @@ static inline enum bch_csum_type bch2_csum_opt_to_type(enum bch_csum_opts type,
             return data ? BCH_CSUM_CRC32C : BCH_CSUM_CRC32C_NONZERO;
        case BCH_CSUM_OPT_crc64:
             return data ? BCH_CSUM_CRC64 : BCH_CSUM_CRC64_NONZERO;
+       case BCH_CSUM_OPT_xxhash:
+            return BCH_CSUM_XXHASH;
        default:
             BUG();
        }