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