xref: /linux/fs/bcachefs/checksum.h (revision a3d14d1602ca11429d242d230c31af8f822f614f)
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 
bch2_checksum_mergeable(unsigned type)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 									\
45 	bch2_checksum(_c, _type, _nonce, _start, vstruct_end(_i) - _start);\
46 })
47 
bch2_csum_to_text(struct printbuf * out,enum bch_csum_type type,struct bch_csum csum)48 static inline void bch2_csum_to_text(struct printbuf *out,
49 				     enum bch_csum_type type,
50 				     struct bch_csum csum)
51 {
52 	const u8 *p = (u8 *) &csum;
53 	unsigned bytes = type < BCH_CSUM_NR ? bch_crc_bytes[type] : 16;
54 
55 	for (unsigned i = 0; i < bytes; i++)
56 		prt_hex_byte(out, p[i]);
57 }
58 
bch2_csum_err_msg(struct printbuf * out,enum bch_csum_type type,struct bch_csum expected,struct bch_csum got)59 static inline void bch2_csum_err_msg(struct printbuf *out,
60 				     enum bch_csum_type type,
61 				     struct bch_csum expected,
62 				     struct bch_csum got)
63 {
64 	prt_str(out, "checksum error, type ");
65 	bch2_prt_csum_type(out, type);
66 	prt_str(out, ": got ");
67 	bch2_csum_to_text(out, type, got);
68 	prt_str(out, " should be ");
69 	bch2_csum_to_text(out, type, expected);
70 }
71 
72 void bch2_chacha20(const struct bch_key *, struct nonce, void *, size_t);
73 
74 int bch2_request_key(struct bch_sb *, struct bch_key *);
75 #ifndef __KERNEL__
76 int bch2_revoke_key(struct bch_sb *);
77 #endif
78 
79 int bch2_encrypt(struct bch_fs *, unsigned, struct nonce,
80 		 void *data, size_t);
81 
82 struct bch_csum bch2_checksum_bio(struct bch_fs *, unsigned,
83 				  struct nonce, struct bio *);
84 
85 int bch2_rechecksum_bio(struct bch_fs *, struct bio *, struct bversion,
86 			struct bch_extent_crc_unpacked,
87 			struct bch_extent_crc_unpacked *,
88 			struct bch_extent_crc_unpacked *,
89 			unsigned, unsigned, unsigned);
90 
91 int __bch2_encrypt_bio(struct bch_fs *, unsigned,
92 		       struct nonce, struct bio *);
93 
bch2_encrypt_bio(struct bch_fs * c,unsigned type,struct nonce nonce,struct bio * bio)94 static inline int bch2_encrypt_bio(struct bch_fs *c, unsigned type,
95 				   struct nonce nonce, struct bio *bio)
96 {
97 	return bch2_csum_type_is_encryption(type)
98 		? __bch2_encrypt_bio(c, type, nonce, bio)
99 		: 0;
100 }
101 
102 extern const struct bch_sb_field_ops bch_sb_field_ops_crypt;
103 
104 int bch2_decrypt_sb_key(struct bch_fs *, struct bch_sb_field_crypt *,
105 			struct bch_key *);
106 
107 #if 0
108 int bch2_disable_encryption(struct bch_fs *);
109 int bch2_enable_encryption(struct bch_fs *, bool);
110 #endif
111 
112 void bch2_fs_encryption_exit(struct bch_fs *);
113 int bch2_fs_encryption_init(struct bch_fs *);
114 
bch2_csum_opt_to_type(enum bch_csum_opt type,bool data)115 static inline enum bch_csum_type bch2_csum_opt_to_type(enum bch_csum_opt type,
116 						       bool data)
117 {
118 	switch (type) {
119 	case BCH_CSUM_OPT_none:
120 		return BCH_CSUM_none;
121 	case BCH_CSUM_OPT_crc32c:
122 		return data ? BCH_CSUM_crc32c : BCH_CSUM_crc32c_nonzero;
123 	case BCH_CSUM_OPT_crc64:
124 		return data ? BCH_CSUM_crc64 : BCH_CSUM_crc64_nonzero;
125 	case BCH_CSUM_OPT_xxhash:
126 		return BCH_CSUM_xxhash;
127 	default:
128 		BUG();
129 	}
130 }
131 
bch2_data_checksum_type(struct bch_fs * c,struct bch_io_opts opts)132 static inline enum bch_csum_type bch2_data_checksum_type(struct bch_fs *c,
133 							 struct bch_io_opts opts)
134 {
135 	if (opts.nocow)
136 		return 0;
137 
138 	if (c->sb.encryption_type)
139 		return c->opts.wide_macs
140 			? BCH_CSUM_chacha20_poly1305_128
141 			: BCH_CSUM_chacha20_poly1305_80;
142 
143 	return bch2_csum_opt_to_type(opts.data_checksum, true);
144 }
145 
bch2_meta_checksum_type(struct bch_fs * c)146 static inline enum bch_csum_type bch2_meta_checksum_type(struct bch_fs *c)
147 {
148 	if (c->sb.encryption_type)
149 		return BCH_CSUM_chacha20_poly1305_128;
150 
151 	return bch2_csum_opt_to_type(c->opts.metadata_checksum, false);
152 }
153 
bch2_checksum_type_valid(const struct bch_fs * c,unsigned type)154 static inline bool bch2_checksum_type_valid(const struct bch_fs *c,
155 					   unsigned type)
156 {
157 	if (type >= BCH_CSUM_NR)
158 		return false;
159 
160 	if (bch2_csum_type_is_encryption(type) && !c->chacha20_key_set)
161 		return false;
162 
163 	return true;
164 }
165 
166 /* returns true if not equal */
bch2_crc_cmp(struct bch_csum l,struct bch_csum r)167 static inline bool bch2_crc_cmp(struct bch_csum l, struct bch_csum r)
168 {
169 	/*
170 	 * XXX: need some way of preventing the compiler from optimizing this
171 	 * into a form that isn't constant time..
172 	 */
173 	return ((l.lo ^ r.lo) | (l.hi ^ r.hi)) != 0;
174 }
175 
176 /* for skipping ahead and encrypting/decrypting at an offset: */
nonce_add(struct nonce nonce,unsigned offset)177 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
178 {
179 	EBUG_ON(offset & (CHACHA_BLOCK_SIZE - 1));
180 
181 	le32_add_cpu(&nonce.d[0], offset / CHACHA_BLOCK_SIZE);
182 	return nonce;
183 }
184 
null_nonce(void)185 static inline struct nonce null_nonce(void)
186 {
187 	struct nonce ret;
188 
189 	memset(&ret, 0, sizeof(ret));
190 	return ret;
191 }
192 
extent_nonce(struct bversion version,struct bch_extent_crc_unpacked crc)193 static inline struct nonce extent_nonce(struct bversion version,
194 					struct bch_extent_crc_unpacked crc)
195 {
196 	unsigned compression_type = crc_is_compressed(crc)
197 		? crc.compression_type
198 		: 0;
199 	unsigned size = compression_type ? crc.uncompressed_size : 0;
200 	struct nonce nonce = (struct nonce) {{
201 		[0] = cpu_to_le32(size << 22),
202 		[1] = cpu_to_le32(version.lo),
203 		[2] = cpu_to_le32(version.lo >> 32),
204 		[3] = cpu_to_le32(version.hi|
205 				  (compression_type << 24))^BCH_NONCE_EXTENT,
206 	}};
207 
208 	return nonce_add(nonce, crc.nonce << 9);
209 }
210 
bch2_key_is_encrypted(struct bch_encrypted_key * key)211 static inline bool bch2_key_is_encrypted(struct bch_encrypted_key *key)
212 {
213 	return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
214 }
215 
__bch2_sb_key_nonce(struct bch_sb * sb)216 static inline struct nonce __bch2_sb_key_nonce(struct bch_sb *sb)
217 {
218 	__le64 magic = __bch2_sb_magic(sb);
219 
220 	return (struct nonce) {{
221 		[0] = 0,
222 		[1] = 0,
223 		[2] = ((__le32 *) &magic)[0],
224 		[3] = ((__le32 *) &magic)[1],
225 	}};
226 }
227 
bch2_sb_key_nonce(struct bch_fs * c)228 static inline struct nonce bch2_sb_key_nonce(struct bch_fs *c)
229 {
230 	__le64 magic = bch2_sb_magic(c);
231 
232 	return (struct nonce) {{
233 		[0] = 0,
234 		[1] = 0,
235 		[2] = ((__le32 *) &magic)[0],
236 		[3] = ((__le32 *) &magic)[1],
237 	}};
238 }
239 
240 #endif /* _BCACHEFS_CHECKSUM_H */
241