1 #ifndef _BCACHEFS_CHECKSUM_H
2 #define _BCACHEFS_CHECKSUM_H
5 #include "extents_types.h"
8 #include <linux/crc64.h>
9 #include <crypto/chacha.h>
11 static inline bool bch2_checksum_mergeable(unsigned type)
24 struct bch_csum bch2_checksum_merge(unsigned, struct bch_csum,
25 struct bch_csum, size_t);
27 static inline u64 bch2_crc64_update(u64 crc, const void *p, size_t len)
29 return crc64_be(crc, p, len);
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)
38 struct bch_csum bch2_checksum(struct bch_fs *, unsigned, struct nonce,
39 const void *, size_t);
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
45 #define csum_vstruct(_c, _type, _nonce, _i) \
47 const void *start = ((const void *) (_i)) + sizeof((_i)->csum); \
48 const void *end = vstruct_end(_i); \
50 bch2_checksum(_c, _type, _nonce, start, end - start); \
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 *);
56 void bch2_encrypt(struct bch_fs *, unsigned, struct nonce,
59 struct bch_csum bch2_checksum_bio(struct bch_fs *, unsigned,
60 struct nonce, struct bio *);
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);
68 void bch2_encrypt_bio(struct bch_fs *, unsigned,
69 struct nonce, struct bio *);
71 int bch2_decrypt_sb_key(struct bch_fs *, struct bch_sb_field_crypt *,
74 int bch2_disable_encryption(struct bch_fs *);
75 int bch2_enable_encryption(struct bch_fs *, bool);
77 void bch2_fs_encryption_exit(struct bch_fs *);
78 int bch2_fs_encryption_init(struct bch_fs *);
80 static inline enum bch_csum_type bch2_csum_opt_to_type(enum bch_csum_opts type,
84 case BCH_CSUM_OPT_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;
95 static inline enum bch_csum_type bch2_data_checksum_type(struct bch_fs *c,
98 if (c->sb.encryption_type)
99 return c->opts.wide_macs
100 ? BCH_CSUM_CHACHA20_POLY1305_128
101 : BCH_CSUM_CHACHA20_POLY1305_80;
103 return bch2_csum_opt_to_type(opt, true);
106 static inline enum bch_csum_type bch2_meta_checksum_type(struct bch_fs *c)
108 if (c->sb.encryption_type)
109 return BCH_CSUM_CHACHA20_POLY1305_128;
111 return bch2_csum_opt_to_type(c->opts.metadata_checksum, false);
114 static const unsigned bch2_compression_opt_to_type[] = {
115 #define x(t) [BCH_COMPRESSION_OPT_##t] = BCH_COMPRESSION_##t,
116 BCH_COMPRESSION_TYPES()
120 static inline bool bch2_checksum_type_valid(const struct bch_fs *c,
123 if (type >= BCH_CSUM_NR)
126 if (bch2_csum_type_is_encryption(type) && !c->chacha20)
132 /* returns true if not equal */
133 static inline bool bch2_crc_cmp(struct bch_csum l, struct bch_csum r)
136 * XXX: need some way of preventing the compiler from optimizing this
137 * into a form that isn't constant time..
139 return ((l.lo ^ r.lo) | (l.hi ^ r.hi)) != 0;
142 /* for skipping ahead and encrypting/decrypting at an offset: */
143 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
145 EBUG_ON(offset & (CHACHA_BLOCK_SIZE - 1));
147 le32_add_cpu(&nonce.d[0], offset / CHACHA_BLOCK_SIZE);
151 static inline struct nonce null_nonce(void)
155 memset(&ret, 0, sizeof(ret));
159 static inline struct nonce extent_nonce(struct bversion version,
160 struct bch_extent_crc_unpacked crc)
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,
171 return nonce_add(nonce, crc.nonce << 9);
174 static inline bool bch2_key_is_encrypted(struct bch_encrypted_key *key)
176 return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
179 static inline struct nonce __bch2_sb_key_nonce(struct bch_sb *sb)
181 __le64 magic = __bch2_sb_magic(sb);
183 return (struct nonce) {{
186 [2] = ((__le32 *) &magic)[0],
187 [3] = ((__le32 *) &magic)[1],
191 static inline struct nonce bch2_sb_key_nonce(struct bch_fs *c)
193 __le64 magic = bch2_sb_magic(c);
195 return (struct nonce) {{
198 [2] = ((__le32 *) &magic)[0],
199 [3] = ((__le32 *) &magic)[1],
203 #endif /* _BCACHEFS_CHECKSUM_H */