]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/checksum.h
Faster crc32c
[bcachefs-tools-debian] / libbcachefs / checksum.h
1 #ifndef _BCACHE_CHECKSUM_H
2 #define _BCACHE_CHECKSUM_H
3
4 #include "bcachefs.h"
5 #include "super-io.h"
6
7 #include <crypto/chacha20.h>
8
9 u64 bch2_crc64_update(u64, const void *, size_t);
10
11 #define BCH_NONCE_EXTENT        cpu_to_le32(1 << 28)
12 #define BCH_NONCE_BTREE         cpu_to_le32(2 << 28)
13 #define BCH_NONCE_JOURNAL       cpu_to_le32(3 << 28)
14 #define BCH_NONCE_PRIO          cpu_to_le32(4 << 28)
15 #define BCH_NONCE_POLY          cpu_to_le32(1 << 31)
16
17 struct bch_csum bch2_checksum(struct bch_fs *, unsigned, struct nonce,
18                              const void *, size_t);
19
20 /*
21  * This is used for various on disk data structures - bch_sb, prio_set, bset,
22  * jset: The checksum is _always_ the first field of these structs
23  */
24 #define csum_vstruct(_c, _type, _nonce, _i)                             \
25 ({                                                                      \
26         const void *start = ((const void *) (_i)) + sizeof((_i)->csum); \
27         const void *end = vstruct_end(_i);                              \
28                                                                         \
29         bch2_checksum(_c, _type, _nonce, start, end - start);           \
30 })
31
32 int bch2_chacha_encrypt_key(struct bch_key *, struct nonce, void *, size_t);
33 int bch2_request_key(struct bch_sb *, struct bch_key *);
34
35 void bch2_encrypt(struct bch_fs *, unsigned, struct nonce,
36                  void *data, size_t);
37
38 struct bch_csum bch2_checksum_bio(struct bch_fs *, unsigned,
39                                  struct nonce, struct bio *);
40 void bch2_encrypt_bio(struct bch_fs *, unsigned,
41                     struct nonce, struct bio *);
42
43 int bch2_disable_encryption(struct bch_fs *);
44 int bch2_enable_encryption(struct bch_fs *, bool);
45
46 void bch2_fs_encryption_exit(struct bch_fs *);
47 int bch2_fs_encryption_init(struct bch_fs *);
48
49 static inline unsigned bch2_data_checksum_type(struct bch_fs *c)
50 {
51         if (c->sb.encryption_type)
52                 return c->opts.wide_macs
53                         ? BCH_CSUM_CHACHA20_POLY1305_128
54                         : BCH_CSUM_CHACHA20_POLY1305_80;
55
56         return c->opts.data_checksum;
57 }
58
59 static inline unsigned bch2_meta_checksum_type(struct bch_fs *c)
60 {
61         return c->sb.encryption_type
62                 ? BCH_CSUM_CHACHA20_POLY1305_128
63                 : c->opts.metadata_checksum;
64 }
65
66 static inline bool bch2_checksum_type_valid(const struct bch_fs *c,
67                                            unsigned type)
68 {
69         if (type >= BCH_CSUM_NR)
70                 return false;
71
72         if (bch2_csum_type_is_encryption(type) && !c->chacha20)
73                 return false;
74
75         return true;
76 }
77
78 static const unsigned bch_crc_bytes[] = {
79         [BCH_CSUM_NONE]                         = 0,
80         [BCH_CSUM_CRC32C]                       = 4,
81         [BCH_CSUM_CRC64]                        = 8,
82         [BCH_CSUM_CHACHA20_POLY1305_80]         = 10,
83         [BCH_CSUM_CHACHA20_POLY1305_128]        = 16,
84 };
85
86 static inline bool bch2_crc_cmp(struct bch_csum l, struct bch_csum r)
87 {
88         /*
89          * XXX: need some way of preventing the compiler from optimizing this
90          * into a form that isn't constant time..
91          */
92         return ((l.lo ^ r.lo) | (l.hi ^ r.hi)) != 0;
93 }
94
95 /* for skipping ahead and encrypting/decrypting at an offset: */
96 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
97 {
98         EBUG_ON(offset & (CHACHA20_BLOCK_SIZE - 1));
99
100         le32_add_cpu(&nonce.d[0], offset / CHACHA20_BLOCK_SIZE);
101         return nonce;
102 }
103
104 static inline bool bch2_key_is_encrypted(struct bch_encrypted_key *key)
105 {
106         return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
107 }
108
109 static inline struct nonce __bch2_sb_key_nonce(struct bch_sb *sb)
110 {
111         __le64 magic = __bch2_sb_magic(sb);
112
113         return (struct nonce) {{
114                 [0] = 0,
115                 [1] = 0,
116                 [2] = ((__le32 *) &magic)[0],
117                 [3] = ((__le32 *) &magic)[1],
118         }};
119 }
120
121 static inline struct nonce bch2_sb_key_nonce(struct bch_fs *c)
122 {
123         __le64 magic = bch2_sb_magic(c);
124
125         return (struct nonce) {{
126                 [0] = 0,
127                 [1] = 0,
128                 [2] = ((__le32 *) &magic)[0],
129                 [3] = ((__le32 *) &magic)[1],
130         }};
131 }
132
133 #endif /* _BCACHE_CHECKSUM_H */