xref: /linux/fs/bcachefs/str_hash.h (revision 0678df8271820bcf8fb4f877129f05d68a237de4)
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 int
153 bch2_hash_lookup(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 		 unsigned flags)
159 {
160 	struct bkey_s_c k;
161 	u32 snapshot;
162 	int ret;
163 
164 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
165 	if (ret)
166 		return ret;
167 
168 	for_each_btree_key_upto_norestart(trans, *iter, desc.btree_id,
169 			   SPOS(inum.inum, desc.hash_key(info, key), snapshot),
170 			   POS(inum.inum, U64_MAX),
171 			   BTREE_ITER_SLOTS|flags, k, ret) {
172 		if (is_visible_key(desc, inum, k)) {
173 			if (!desc.cmp_key(k, key))
174 				return 0;
175 		} else if (k.k->type == KEY_TYPE_hash_whiteout) {
176 			;
177 		} else {
178 			/* hole, not found */
179 			break;
180 		}
181 	}
182 	bch2_trans_iter_exit(trans, iter);
183 
184 	return ret ?: -BCH_ERR_ENOENT_str_hash_lookup;
185 }
186 
187 static __always_inline int
188 bch2_hash_hole(struct btree_trans *trans,
189 	       struct btree_iter *iter,
190 	       const struct bch_hash_desc desc,
191 	       const struct bch_hash_info *info,
192 	       subvol_inum inum, const void *key)
193 {
194 	struct bkey_s_c k;
195 	u32 snapshot;
196 	int ret;
197 
198 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
199 	if (ret)
200 		return ret;
201 
202 	for_each_btree_key_upto_norestart(trans, *iter, desc.btree_id,
203 			   SPOS(inum.inum, desc.hash_key(info, key), snapshot),
204 			   POS(inum.inum, U64_MAX),
205 			   BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret)
206 		if (!is_visible_key(desc, inum, k))
207 			return 0;
208 	bch2_trans_iter_exit(trans, iter);
209 
210 	return ret ?: -BCH_ERR_ENOSPC_str_hash_create;
211 }
212 
213 static __always_inline
214 int bch2_hash_needs_whiteout(struct btree_trans *trans,
215 			     const struct bch_hash_desc desc,
216 			     const struct bch_hash_info *info,
217 			     struct btree_iter *start)
218 {
219 	struct btree_iter iter;
220 	struct bkey_s_c k;
221 	int ret;
222 
223 	bch2_trans_copy_iter(&iter, start);
224 
225 	bch2_btree_iter_advance(&iter);
226 
227 	for_each_btree_key_continue_norestart(iter, BTREE_ITER_SLOTS, k, ret) {
228 		if (k.k->type != desc.key_type &&
229 		    k.k->type != KEY_TYPE_hash_whiteout)
230 			break;
231 
232 		if (k.k->type == desc.key_type &&
233 		    desc.hash_bkey(info, k) <= start->pos.offset) {
234 			ret = 1;
235 			break;
236 		}
237 	}
238 
239 	bch2_trans_iter_exit(trans, &iter);
240 	return ret;
241 }
242 
243 static __always_inline
244 int bch2_hash_set_snapshot(struct btree_trans *trans,
245 			   const struct bch_hash_desc desc,
246 			   const struct bch_hash_info *info,
247 			   subvol_inum inum, u32 snapshot,
248 			   struct bkey_i *insert,
249 			   int flags,
250 			   int update_flags)
251 {
252 	struct btree_iter iter, slot = { NULL };
253 	struct bkey_s_c k;
254 	bool found = false;
255 	int ret;
256 
257 	for_each_btree_key_upto_norestart(trans, iter, desc.btree_id,
258 			   SPOS(insert->k.p.inode,
259 				desc.hash_bkey(info, bkey_i_to_s_c(insert)),
260 				snapshot),
261 			   POS(insert->k.p.inode, U64_MAX),
262 			   BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
263 		if (is_visible_key(desc, inum, k)) {
264 			if (!desc.cmp_bkey(k, bkey_i_to_s_c(insert)))
265 				goto found;
266 
267 			/* hash collision: */
268 			continue;
269 		}
270 
271 		if (!slot.path &&
272 		    !(flags & BCH_HASH_SET_MUST_REPLACE))
273 			bch2_trans_copy_iter(&slot, &iter);
274 
275 		if (k.k->type != KEY_TYPE_hash_whiteout)
276 			goto not_found;
277 	}
278 
279 	if (!ret)
280 		ret = -BCH_ERR_ENOSPC_str_hash_create;
281 out:
282 	bch2_trans_iter_exit(trans, &slot);
283 	bch2_trans_iter_exit(trans, &iter);
284 
285 	return ret;
286 found:
287 	found = true;
288 not_found:
289 
290 	if (!found && (flags & BCH_HASH_SET_MUST_REPLACE)) {
291 		ret = -BCH_ERR_ENOENT_str_hash_set_must_replace;
292 	} else if (found && (flags & BCH_HASH_SET_MUST_CREATE)) {
293 		ret = -EEXIST;
294 	} else {
295 		if (!found && slot.path)
296 			swap(iter, slot);
297 
298 		insert->k.p = iter.pos;
299 		ret = bch2_trans_update(trans, &iter, insert, 0);
300 	}
301 
302 	goto out;
303 }
304 
305 static __always_inline
306 int bch2_hash_set(struct btree_trans *trans,
307 		  const struct bch_hash_desc desc,
308 		  const struct bch_hash_info *info,
309 		  subvol_inum inum,
310 		  struct bkey_i *insert, int flags)
311 {
312 	u32 snapshot;
313 	int ret;
314 
315 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
316 	if (ret)
317 		return ret;
318 
319 	insert->k.p.inode = inum.inum;
320 
321 	return bch2_hash_set_snapshot(trans, desc, info, inum,
322 				      snapshot, insert, flags, 0);
323 }
324 
325 static __always_inline
326 int bch2_hash_delete_at(struct btree_trans *trans,
327 			const struct bch_hash_desc desc,
328 			const struct bch_hash_info *info,
329 			struct btree_iter *iter,
330 			unsigned update_flags)
331 {
332 	struct bkey_i *delete;
333 	int ret;
334 
335 	delete = bch2_trans_kmalloc(trans, sizeof(*delete));
336 	ret = PTR_ERR_OR_ZERO(delete);
337 	if (ret)
338 		return ret;
339 
340 	ret = bch2_hash_needs_whiteout(trans, desc, info, iter);
341 	if (ret < 0)
342 		return ret;
343 
344 	bkey_init(&delete->k);
345 	delete->k.p = iter->pos;
346 	delete->k.type = ret ? KEY_TYPE_hash_whiteout : KEY_TYPE_deleted;
347 
348 	return bch2_trans_update(trans, iter, delete, update_flags);
349 }
350 
351 static __always_inline
352 int bch2_hash_delete(struct btree_trans *trans,
353 		     const struct bch_hash_desc desc,
354 		     const struct bch_hash_info *info,
355 		     subvol_inum inum, const void *key)
356 {
357 	struct btree_iter iter;
358 	int ret;
359 
360 	ret = bch2_hash_lookup(trans, &iter, desc, info, inum, key,
361 				BTREE_ITER_INTENT);
362 	if (ret)
363 		return ret;
364 
365 	ret = bch2_hash_delete_at(trans, desc, info, &iter, 0);
366 	bch2_trans_iter_exit(trans, &iter);
367 	return ret;
368 }
369 
370 #endif /* _BCACHEFS_STR_HASH_H */
371