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