]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/checksum.h
Update bcachefs sources to 70fa0c1ff4 fixup! bcachefs: Btree key cache improvements
[bcachefs-tools-debian] / libbcachefs / checksum.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_CHECKSUM_H
3 #define _BCACHEFS_CHECKSUM_H
4
5 #include "bcachefs.h"
6 #include "extents_types.h"
7 #include "super-io.h"
8
9 #include <linux/crc64.h>
10 #include <crypto/chacha.h>
11
12 static inline bool bch2_checksum_mergeable(unsigned type)
13 {
14
15         switch (type) {
16         case BCH_CSUM_none:
17         case BCH_CSUM_crc32c:
18         case BCH_CSUM_crc64:
19                 return true;
20         default:
21                 return false;
22         }
23 }
24
25 struct bch_csum bch2_checksum_merge(unsigned, struct bch_csum,
26                                     struct bch_csum, size_t);
27
28 #define BCH_NONCE_EXTENT        cpu_to_le32(1 << 28)
29 #define BCH_NONCE_BTREE         cpu_to_le32(2 << 28)
30 #define BCH_NONCE_JOURNAL       cpu_to_le32(3 << 28)
31 #define BCH_NONCE_PRIO          cpu_to_le32(4 << 28)
32 #define BCH_NONCE_POLY          cpu_to_le32(1 << 31)
33
34 struct bch_csum bch2_checksum(struct bch_fs *, unsigned, struct nonce,
35                              const void *, size_t);
36
37 /*
38  * This is used for various on disk data structures - bch_sb, prio_set, bset,
39  * jset: The checksum is _always_ the first field of these structs
40  */
41 #define csum_vstruct(_c, _type, _nonce, _i)                             \
42 ({                                                                      \
43         const void *start = ((const void *) (_i)) + sizeof((_i)->csum); \
44         const void *end = vstruct_end(_i);                              \
45                                                                         \
46         bch2_checksum(_c, _type, _nonce, start, end - start);           \
47 })
48
49 int bch2_chacha_encrypt_key(struct bch_key *, struct nonce, void *, size_t);
50 int bch2_request_key(struct bch_sb *, struct bch_key *);
51
52 int bch2_encrypt(struct bch_fs *, unsigned, struct nonce,
53                  void *data, size_t);
54
55 struct bch_csum bch2_checksum_bio(struct bch_fs *, unsigned,
56                                   struct nonce, struct bio *);
57
58 int bch2_rechecksum_bio(struct bch_fs *, struct bio *, struct bversion,
59                         struct bch_extent_crc_unpacked,
60                         struct bch_extent_crc_unpacked *,
61                         struct bch_extent_crc_unpacked *,
62                         unsigned, unsigned, unsigned);
63
64 int bch2_encrypt_bio(struct bch_fs *, unsigned,
65                      struct nonce, struct bio *);
66
67 int bch2_decrypt_sb_key(struct bch_fs *, struct bch_sb_field_crypt *,
68                         struct bch_key *);
69
70 int bch2_disable_encryption(struct bch_fs *);
71 int bch2_enable_encryption(struct bch_fs *, bool);
72
73 void bch2_fs_encryption_exit(struct bch_fs *);
74 int bch2_fs_encryption_init(struct bch_fs *);
75
76 static inline enum bch_csum_type bch2_csum_opt_to_type(enum bch_csum_opts type,
77                                                        bool data)
78 {
79         switch (type) {
80         case BCH_CSUM_OPT_none:
81                 return BCH_CSUM_none;
82         case BCH_CSUM_OPT_crc32c:
83                 return data ? BCH_CSUM_crc32c : BCH_CSUM_crc32c_nonzero;
84         case BCH_CSUM_OPT_crc64:
85                 return data ? BCH_CSUM_crc64 : BCH_CSUM_crc64_nonzero;
86         case BCH_CSUM_OPT_xxhash:
87                 return BCH_CSUM_xxhash;
88         default:
89                 BUG();
90         }
91 }
92
93 static inline enum bch_csum_type bch2_data_checksum_type(struct bch_fs *c,
94                                                          unsigned opt)
95 {
96         if (c->sb.encryption_type)
97                 return c->opts.wide_macs
98                         ? BCH_CSUM_chacha20_poly1305_128
99                         : BCH_CSUM_chacha20_poly1305_80;
100
101         return bch2_csum_opt_to_type(opt, true);
102 }
103
104 static inline enum bch_csum_type bch2_meta_checksum_type(struct bch_fs *c)
105 {
106         if (c->sb.encryption_type)
107                 return BCH_CSUM_chacha20_poly1305_128;
108
109         return bch2_csum_opt_to_type(c->opts.metadata_checksum, false);
110 }
111
112 static const unsigned bch2_compression_opt_to_type[] = {
113 #define x(t, n) [BCH_COMPRESSION_OPT_##t] = BCH_COMPRESSION_TYPE_##t,
114         BCH_COMPRESSION_OPTS()
115 #undef x
116 };
117
118 static inline bool bch2_checksum_type_valid(const struct bch_fs *c,
119                                            unsigned type)
120 {
121         if (type >= BCH_CSUM_NR)
122                 return false;
123
124         if (bch2_csum_type_is_encryption(type) && !c->chacha20)
125                 return false;
126
127         return true;
128 }
129
130 /* returns true if not equal */
131 static inline bool bch2_crc_cmp(struct bch_csum l, struct bch_csum r)
132 {
133         /*
134          * XXX: need some way of preventing the compiler from optimizing this
135          * into a form that isn't constant time..
136          */
137         return ((l.lo ^ r.lo) | (l.hi ^ r.hi)) != 0;
138 }
139
140 /* for skipping ahead and encrypting/decrypting at an offset: */
141 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
142 {
143         EBUG_ON(offset & (CHACHA_BLOCK_SIZE - 1));
144
145         le32_add_cpu(&nonce.d[0], offset / CHACHA_BLOCK_SIZE);
146         return nonce;
147 }
148
149 static inline struct nonce null_nonce(void)
150 {
151         struct nonce ret;
152
153         memset(&ret, 0, sizeof(ret));
154         return ret;
155 }
156
157 static inline struct nonce extent_nonce(struct bversion version,
158                                         struct bch_extent_crc_unpacked crc)
159 {
160         unsigned compression_type = crc_is_compressed(crc)
161                 ? crc.compression_type
162                 : 0;
163         unsigned size = compression_type ? crc.uncompressed_size : 0;
164         struct nonce nonce = (struct nonce) {{
165                 [0] = cpu_to_le32(size << 22),
166                 [1] = cpu_to_le32(version.lo),
167                 [2] = cpu_to_le32(version.lo >> 32),
168                 [3] = cpu_to_le32(version.hi|
169                                   (compression_type << 24))^BCH_NONCE_EXTENT,
170         }};
171
172         return nonce_add(nonce, crc.nonce << 9);
173 }
174
175 static inline bool bch2_key_is_encrypted(struct bch_encrypted_key *key)
176 {
177         return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
178 }
179
180 static inline struct nonce __bch2_sb_key_nonce(struct bch_sb *sb)
181 {
182         __le64 magic = __bch2_sb_magic(sb);
183
184         return (struct nonce) {{
185                 [0] = 0,
186                 [1] = 0,
187                 [2] = ((__le32 *) &magic)[0],
188                 [3] = ((__le32 *) &magic)[1],
189         }};
190 }
191
192 static inline struct nonce bch2_sb_key_nonce(struct bch_fs *c)
193 {
194         __le64 magic = bch2_sb_magic(c);
195
196         return (struct nonce) {{
197                 [0] = 0,
198                 [1] = 0,
199                 [2] = ((__le32 *) &magic)[0],
200                 [3] = ((__le32 *) &magic)[1],
201         }};
202 }
203
204 #endif /* _BCACHEFS_CHECKSUM_H */