]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/str_hash.h
c6a132b3c5bb2eb24cf112f11fd23d254e01b499
[bcachefs-tools-debian] / libbcachefs / str_hash.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_STR_HASH_H
3 #define _BCACHEFS_STR_HASH_H
4
5 #include "btree_iter.h"
6 #include "btree_update.h"
7 #include "checksum.h"
8 #include "error.h"
9 #include "inode.h"
10 #include "siphash.h"
11 #include "super.h"
12
13 #include <linux/crc32c.h>
14 #include <crypto/hash.h>
15 #include <crypto/sha2.h>
16
17 static inline enum bch_str_hash_type
18 bch2_str_hash_opt_to_type(struct bch_fs *c, enum bch_str_hash_opts opt)
19 {
20         switch (opt) {
21         case BCH_STR_HASH_OPT_crc32c:
22                 return BCH_STR_HASH_CRC32C;
23         case BCH_STR_HASH_OPT_crc64:
24                 return BCH_STR_HASH_CRC64;
25         case BCH_STR_HASH_OPT_siphash:
26                 return c->sb.features & (1ULL << BCH_FEATURE_new_siphash)
27                         ? BCH_STR_HASH_SIPHASH
28                         : BCH_STR_HASH_SIPHASH_OLD;
29         default:
30              BUG();
31         }
32 }
33
34 struct bch_hash_info {
35         u8                      type;
36         /*
37          * For crc32 or crc64 string hashes the first key value of
38          * the siphash_key (k0) is used as the key.
39          */
40         SIPHASH_KEY     siphash_key;
41 };
42
43 static inline struct bch_hash_info
44 bch2_hash_info_init(struct bch_fs *c, const struct bch_inode_unpacked *bi)
45 {
46         /* XXX ick */
47         struct bch_hash_info info = {
48                 .type = (bi->bi_flags >> INODE_STR_HASH_OFFSET) &
49                         ~(~0U << INODE_STR_HASH_BITS),
50                 .siphash_key = { .k0 = bi->bi_hash_seed }
51         };
52
53         if (unlikely(info.type == BCH_STR_HASH_SIPHASH_OLD)) {
54                 SHASH_DESC_ON_STACK(desc, c->sha256);
55                 u8 digest[SHA256_DIGEST_SIZE];
56
57                 desc->tfm = c->sha256;
58
59                 crypto_shash_digest(desc, (void *) &bi->bi_hash_seed,
60                                     sizeof(bi->bi_hash_seed), digest);
61                 memcpy(&info.siphash_key, digest, sizeof(info.siphash_key));
62         }
63
64         return info;
65 }
66
67 struct bch_str_hash_ctx {
68         union {
69                 u32             crc32c;
70                 u64             crc64;
71                 SIPHASH_CTX     siphash;
72         };
73 };
74
75 static inline void bch2_str_hash_init(struct bch_str_hash_ctx *ctx,
76                                      const struct bch_hash_info *info)
77 {
78         switch (info->type) {
79         case BCH_STR_HASH_CRC32C:
80                 ctx->crc32c = crc32c(~0, &info->siphash_key.k0,
81                                      sizeof(info->siphash_key.k0));
82                 break;
83         case BCH_STR_HASH_CRC64:
84                 ctx->crc64 = crc64_be(~0, &info->siphash_key.k0,
85                                       sizeof(info->siphash_key.k0));
86                 break;
87         case BCH_STR_HASH_SIPHASH_OLD:
88         case BCH_STR_HASH_SIPHASH:
89                 SipHash24_Init(&ctx->siphash, &info->siphash_key);
90                 break;
91         default:
92                 BUG();
93         }
94 }
95
96 static inline void bch2_str_hash_update(struct bch_str_hash_ctx *ctx,
97                                        const struct bch_hash_info *info,
98                                        const void *data, size_t len)
99 {
100         switch (info->type) {
101         case BCH_STR_HASH_CRC32C:
102                 ctx->crc32c = crc32c(ctx->crc32c, data, len);
103                 break;
104         case BCH_STR_HASH_CRC64:
105                 ctx->crc64 = crc64_be(ctx->crc64, data, len);
106                 break;
107         case BCH_STR_HASH_SIPHASH_OLD:
108         case BCH_STR_HASH_SIPHASH:
109                 SipHash24_Update(&ctx->siphash, data, len);
110                 break;
111         default:
112                 BUG();
113         }
114 }
115
116 static inline u64 bch2_str_hash_end(struct bch_str_hash_ctx *ctx,
117                                    const struct bch_hash_info *info)
118 {
119         switch (info->type) {
120         case BCH_STR_HASH_CRC32C:
121                 return ctx->crc32c;
122         case BCH_STR_HASH_CRC64:
123                 return ctx->crc64 >> 1;
124         case BCH_STR_HASH_SIPHASH_OLD:
125         case BCH_STR_HASH_SIPHASH:
126                 return SipHash24_End(&ctx->siphash) >> 1;
127         default:
128                 BUG();
129         }
130 }
131
132 struct bch_hash_desc {
133         enum btree_id   btree_id;
134         u8              key_type;
135
136         u64             (*hash_key)(const struct bch_hash_info *, const void *);
137         u64             (*hash_bkey)(const struct bch_hash_info *, struct bkey_s_c);
138         bool            (*cmp_key)(struct bkey_s_c, const void *);
139         bool            (*cmp_bkey)(struct bkey_s_c, struct bkey_s_c);
140 };
141
142 static __always_inline int
143 bch2_hash_lookup(struct btree_trans *trans,
144                  struct btree_iter *iter,
145                  const struct bch_hash_desc desc,
146                  const struct bch_hash_info *info,
147                  u64 inode, const void *key,
148                  unsigned flags)
149 {
150         struct bkey_s_c k;
151         int ret;
152
153         for_each_btree_key(trans, *iter, desc.btree_id,
154                            POS(inode, desc.hash_key(info, key)),
155                            BTREE_ITER_SLOTS|flags, k, ret) {
156                 if (iter->pos.inode != inode)
157                         break;
158
159                 if (k.k->type == desc.key_type) {
160                         if (!desc.cmp_key(k, key))
161                                 return 0;
162                 } else if (k.k->type == KEY_TYPE_hash_whiteout) {
163                         ;
164                 } else {
165                         /* hole, not found */
166                         break;
167                 }
168         }
169         bch2_trans_iter_exit(trans, iter);
170
171         return ret ?: -ENOENT;
172 }
173
174 static __always_inline int
175 bch2_hash_hole(struct btree_trans *trans,
176                struct btree_iter *iter,
177                const struct bch_hash_desc desc,
178                const struct bch_hash_info *info,
179                u64 inode, const void *key)
180 {
181         struct bkey_s_c k;
182         int ret;
183
184         for_each_btree_key(trans, *iter, desc.btree_id,
185                            POS(inode, desc.hash_key(info, key)),
186                            BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
187                 if (iter->pos.inode != inode)
188                         break;
189
190                 if (k.k->type != desc.key_type)
191                         return 0;
192         }
193         bch2_trans_iter_exit(trans, iter);
194
195         return ret ?: -ENOSPC;
196 }
197
198 static __always_inline
199 int bch2_hash_needs_whiteout(struct btree_trans *trans,
200                              const struct bch_hash_desc desc,
201                              const struct bch_hash_info *info,
202                              struct btree_iter *start)
203 {
204         struct btree_iter iter;
205         struct bkey_s_c k;
206         int ret;
207
208         bch2_trans_copy_iter(&iter, start);
209
210         bch2_btree_iter_advance(&iter);
211
212         for_each_btree_key_continue(iter, BTREE_ITER_SLOTS, k, ret) {
213                 if (k.k->type != desc.key_type &&
214                     k.k->type != KEY_TYPE_hash_whiteout)
215                         break;
216
217                 if (k.k->type == desc.key_type &&
218                     desc.hash_bkey(info, k) <= start->pos.offset) {
219                         ret = 1;
220                         break;
221                 }
222         }
223
224         bch2_trans_iter_exit(trans, &iter);
225         return ret;
226 }
227
228 static __always_inline
229 int bch2_hash_set(struct btree_trans *trans,
230                   const struct bch_hash_desc desc,
231                   const struct bch_hash_info *info,
232                   u64 inode, struct bkey_i *insert, int flags)
233 {
234         struct btree_iter iter, slot = { NULL };
235         struct bkey_s_c k;
236         bool found = false;
237         int ret;
238
239         for_each_btree_key(trans, iter, desc.btree_id,
240                            POS(inode, desc.hash_bkey(info, bkey_i_to_s_c(insert))),
241                            BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
242                 if (iter.pos.inode != inode)
243                         break;
244
245                 if (k.k->type == desc.key_type) {
246                         if (!desc.cmp_bkey(k, bkey_i_to_s_c(insert)))
247                                 goto found;
248
249                         /* hash collision: */
250                         continue;
251                 }
252
253                 if (!slot.path &&
254                     !(flags & BCH_HASH_SET_MUST_REPLACE))
255                         bch2_trans_copy_iter(&slot, &iter);
256
257                 if (k.k->type != KEY_TYPE_hash_whiteout)
258                         goto not_found;
259         }
260
261         if (!ret)
262                 ret = -ENOSPC;
263 out:
264         bch2_trans_iter_exit(trans, &slot);
265         bch2_trans_iter_exit(trans, &iter);
266
267         return ret;
268 found:
269         found = true;
270 not_found:
271
272         if (!found && (flags & BCH_HASH_SET_MUST_REPLACE)) {
273                 ret = -ENOENT;
274         } else if (found && (flags & BCH_HASH_SET_MUST_CREATE)) {
275                 ret = -EEXIST;
276         } else {
277                 if (!found && slot.path)
278                         swap(iter, slot);
279
280                 insert->k.p = iter.pos;
281                 ret = bch2_trans_update(trans, &iter, insert, 0);
282         }
283
284         goto out;
285 }
286
287 static __always_inline
288 int bch2_hash_delete_at(struct btree_trans *trans,
289                         const struct bch_hash_desc desc,
290                         const struct bch_hash_info *info,
291                         struct btree_iter *iter)
292 {
293         struct bkey_i *delete;
294         int ret;
295
296         delete = bch2_trans_kmalloc(trans, sizeof(*delete));
297         ret = PTR_ERR_OR_ZERO(delete);
298         if (ret)
299                 return ret;
300
301         ret = bch2_hash_needs_whiteout(trans, desc, info, iter);
302         if (ret < 0)
303                 return ret;
304
305         bkey_init(&delete->k);
306         delete->k.p = iter->pos;
307         delete->k.type = ret ? KEY_TYPE_hash_whiteout : KEY_TYPE_deleted;
308
309         return bch2_trans_update(trans, iter, delete, 0);
310 }
311
312 static __always_inline
313 int bch2_hash_delete(struct btree_trans *trans,
314                      const struct bch_hash_desc desc,
315                      const struct bch_hash_info *info,
316                      u64 inode, const void *key)
317 {
318         struct btree_iter iter;
319         int ret;
320
321         ret = bch2_hash_lookup(trans, &iter, desc, info, inode, key,
322                                 BTREE_ITER_INTENT);
323         if (ret)
324                 return ret;
325
326         ret = bch2_hash_delete_at(trans, desc, info, &iter);
327         bch2_trans_iter_exit(trans, &iter);
328         return ret;
329 }
330
331 #endif /* _BCACHEFS_STR_HASH_H */