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