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