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