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