Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
cafe5635 KO |
2 | /* |
3 | * Code for working with individual keys, and sorted sets of keys with in a | |
4 | * btree node | |
5 | * | |
6 | * Copyright 2012 Google, Inc. | |
7 | */ | |
8 | ||
89ebb4a2 KO |
9 | #define pr_fmt(fmt) "bcache: %s() " fmt "\n", __func__ |
10 | ||
11 | #include "util.h" | |
12 | #include "bset.h" | |
cafe5635 | 13 | |
dc9d98d6 | 14 | #include <linux/console.h> |
e6017571 | 15 | #include <linux/sched/clock.h> |
cafe5635 | 16 | #include <linux/random.h> |
cd953ed0 | 17 | #include <linux/prefetch.h> |
cafe5635 | 18 | |
dc9d98d6 KO |
19 | #ifdef CONFIG_BCACHE_DEBUG |
20 | ||
6f10f7d1 | 21 | void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set) |
dc9d98d6 KO |
22 | { |
23 | struct bkey *k, *next; | |
24 | ||
25 | for (k = i->start; k < bset_bkey_last(i); k = next) { | |
26 | next = bkey_next(k); | |
27 | ||
6ae63e35 | 28 | pr_err("block %u key %u/%u: ", set, |
6f10f7d1 | 29 | (unsigned int) ((u64 *) k - i->d), i->keys); |
dc9d98d6 KO |
30 | |
31 | if (b->ops->key_dump) | |
32 | b->ops->key_dump(b, k); | |
33 | else | |
6ae63e35 | 34 | pr_err("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k)); |
dc9d98d6 KO |
35 | |
36 | if (next < bset_bkey_last(i) && | |
37 | bkey_cmp(k, b->ops->is_extents ? | |
38 | &START_KEY(next) : next) > 0) | |
6ae63e35 | 39 | pr_err("Key skipped backwards\n"); |
dc9d98d6 KO |
40 | } |
41 | } | |
42 | ||
43 | void bch_dump_bucket(struct btree_keys *b) | |
44 | { | |
6f10f7d1 | 45 | unsigned int i; |
dc9d98d6 KO |
46 | |
47 | console_lock(); | |
48 | for (i = 0; i <= b->nsets; i++) | |
49 | bch_dump_bset(b, b->set[i].data, | |
50 | bset_sector_offset(b, b->set[i].data)); | |
51 | console_unlock(); | |
52 | } | |
53 | ||
54 | int __bch_count_data(struct btree_keys *b) | |
55 | { | |
6f10f7d1 | 56 | unsigned int ret = 0; |
dc9d98d6 KO |
57 | struct btree_iter iter; |
58 | struct bkey *k; | |
59 | ||
60 | if (b->ops->is_extents) | |
61 | for_each_key(b, k, &iter) | |
62 | ret += KEY_SIZE(k); | |
63 | return ret; | |
64 | } | |
65 | ||
66 | void __bch_check_keys(struct btree_keys *b, const char *fmt, ...) | |
67 | { | |
68 | va_list args; | |
69 | struct bkey *k, *p = NULL; | |
70 | struct btree_iter iter; | |
71 | const char *err; | |
72 | ||
73 | for_each_key(b, k, &iter) { | |
74 | if (b->ops->is_extents) { | |
75 | err = "Keys out of order"; | |
76 | if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0) | |
77 | goto bug; | |
78 | ||
79 | if (bch_ptr_invalid(b, k)) | |
80 | continue; | |
81 | ||
82 | err = "Overlapping keys"; | |
83 | if (p && bkey_cmp(p, &START_KEY(k)) > 0) | |
84 | goto bug; | |
85 | } else { | |
86 | if (bch_ptr_bad(b, k)) | |
87 | continue; | |
88 | ||
89 | err = "Duplicate keys"; | |
90 | if (p && !bkey_cmp(p, k)) | |
91 | goto bug; | |
92 | } | |
93 | p = k; | |
94 | } | |
95 | #if 0 | |
96 | err = "Key larger than btree node key"; | |
97 | if (p && bkey_cmp(p, &b->key) > 0) | |
98 | goto bug; | |
99 | #endif | |
100 | return; | |
101 | bug: | |
102 | bch_dump_bucket(b); | |
103 | ||
104 | va_start(args, fmt); | |
105 | vprintk(fmt, args); | |
106 | va_end(args); | |
107 | ||
108 | panic("bch_check_keys error: %s:\n", err); | |
109 | } | |
110 | ||
111 | static void bch_btree_iter_next_check(struct btree_iter *iter) | |
112 | { | |
113 | struct bkey *k = iter->data->k, *next = bkey_next(k); | |
114 | ||
115 | if (next < iter->data->end && | |
116 | bkey_cmp(k, iter->b->ops->is_extents ? | |
117 | &START_KEY(next) : next) > 0) { | |
118 | bch_dump_bucket(iter->b); | |
119 | panic("Key skipped backwards\n"); | |
120 | } | |
121 | } | |
122 | ||
123 | #else | |
124 | ||
125 | static inline void bch_btree_iter_next_check(struct btree_iter *iter) {} | |
126 | ||
127 | #endif | |
128 | ||
cafe5635 KO |
129 | /* Keylists */ |
130 | ||
6f10f7d1 | 131 | int __bch_keylist_realloc(struct keylist *l, unsigned int u64s) |
cafe5635 | 132 | { |
c2f95ae2 | 133 | size_t oldsize = bch_keylist_nkeys(l); |
085d2a3d | 134 | size_t newsize = oldsize + u64s; |
c2f95ae2 KO |
135 | uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p; |
136 | uint64_t *new_keys; | |
cafe5635 | 137 | |
cafe5635 KO |
138 | newsize = roundup_pow_of_two(newsize); |
139 | ||
140 | if (newsize <= KEYLIST_INLINE || | |
141 | roundup_pow_of_two(oldsize) == newsize) | |
142 | return 0; | |
143 | ||
c2f95ae2 | 144 | new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO); |
cafe5635 | 145 | |
c2f95ae2 | 146 | if (!new_keys) |
cafe5635 KO |
147 | return -ENOMEM; |
148 | ||
c2f95ae2 KO |
149 | if (!old_keys) |
150 | memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize); | |
cafe5635 | 151 | |
c2f95ae2 KO |
152 | l->keys_p = new_keys; |
153 | l->top_p = new_keys + oldsize; | |
cafe5635 KO |
154 | |
155 | return 0; | |
156 | } | |
157 | ||
158 | struct bkey *bch_keylist_pop(struct keylist *l) | |
159 | { | |
c2f95ae2 | 160 | struct bkey *k = l->keys; |
cafe5635 KO |
161 | |
162 | if (k == l->top) | |
163 | return NULL; | |
164 | ||
165 | while (bkey_next(k) != l->top) | |
166 | k = bkey_next(k); | |
167 | ||
168 | return l->top = k; | |
169 | } | |
170 | ||
26c949f8 KO |
171 | void bch_keylist_pop_front(struct keylist *l) |
172 | { | |
c2f95ae2 | 173 | l->top_p -= bkey_u64s(l->keys); |
26c949f8 | 174 | |
c2f95ae2 KO |
175 | memmove(l->keys, |
176 | bkey_next(l->keys), | |
177 | bch_keylist_bytes(l)); | |
26c949f8 KO |
178 | } |
179 | ||
cafe5635 KO |
180 | /* Key/pointer manipulation */ |
181 | ||
182 | void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src, | |
6f10f7d1 | 183 | unsigned int i) |
cafe5635 KO |
184 | { |
185 | BUG_ON(i > KEY_PTRS(src)); | |
186 | ||
187 | /* Only copy the header, key, and one pointer. */ | |
188 | memcpy(dest, src, 2 * sizeof(uint64_t)); | |
189 | dest->ptr[0] = src->ptr[i]; | |
190 | SET_KEY_PTRS(dest, 1); | |
191 | /* We didn't copy the checksum so clear that bit. */ | |
192 | SET_KEY_CSUM(dest, 0); | |
193 | } | |
194 | ||
195 | bool __bch_cut_front(const struct bkey *where, struct bkey *k) | |
196 | { | |
6f10f7d1 | 197 | unsigned int i, len = 0; |
cafe5635 KO |
198 | |
199 | if (bkey_cmp(where, &START_KEY(k)) <= 0) | |
200 | return false; | |
201 | ||
202 | if (bkey_cmp(where, k) < 0) | |
203 | len = KEY_OFFSET(k) - KEY_OFFSET(where); | |
204 | else | |
205 | bkey_copy_key(k, where); | |
206 | ||
207 | for (i = 0; i < KEY_PTRS(k); i++) | |
208 | SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len); | |
209 | ||
210 | BUG_ON(len > KEY_SIZE(k)); | |
211 | SET_KEY_SIZE(k, len); | |
212 | return true; | |
213 | } | |
214 | ||
215 | bool __bch_cut_back(const struct bkey *where, struct bkey *k) | |
216 | { | |
6f10f7d1 | 217 | unsigned int len = 0; |
cafe5635 KO |
218 | |
219 | if (bkey_cmp(where, k) >= 0) | |
220 | return false; | |
221 | ||
222 | BUG_ON(KEY_INODE(where) != KEY_INODE(k)); | |
223 | ||
224 | if (bkey_cmp(where, &START_KEY(k)) > 0) | |
225 | len = KEY_OFFSET(where) - KEY_START(k); | |
226 | ||
227 | bkey_copy_key(k, where); | |
228 | ||
229 | BUG_ON(len > KEY_SIZE(k)); | |
230 | SET_KEY_SIZE(k, len); | |
231 | return true; | |
232 | } | |
233 | ||
ee811287 KO |
234 | /* Auxiliary search trees */ |
235 | ||
236 | /* 32 bits total: */ | |
237 | #define BKEY_MID_BITS 3 | |
238 | #define BKEY_EXPONENT_BITS 7 | |
239 | #define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS) | |
240 | #define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1) | |
241 | ||
242 | struct bkey_float { | |
6f10f7d1 CL |
243 | unsigned int exponent:BKEY_EXPONENT_BITS; |
244 | unsigned int m:BKEY_MID_BITS; | |
245 | unsigned int mantissa:BKEY_MANTISSA_BITS; | |
ee811287 KO |
246 | } __packed; |
247 | ||
248 | /* | |
249 | * BSET_CACHELINE was originally intended to match the hardware cacheline size - | |
250 | * it used to be 64, but I realized the lookup code would touch slightly less | |
251 | * memory if it was 128. | |
252 | * | |
253 | * It definites the number of bytes (in struct bset) per struct bkey_float in | |
254 | * the auxiliar search tree - when we're done searching the bset_float tree we | |
255 | * have this many bytes left that we do a linear search over. | |
256 | * | |
257 | * Since (after level 5) every level of the bset_tree is on a new cacheline, | |
258 | * we're touching one fewer cacheline in the bset tree in exchange for one more | |
259 | * cacheline in the linear search - but the linear search might stop before it | |
260 | * gets to the second cacheline. | |
261 | */ | |
262 | ||
263 | #define BSET_CACHELINE 128 | |
264 | ||
265 | /* Space required for the btree node keys */ | |
a85e968e | 266 | static inline size_t btree_keys_bytes(struct btree_keys *b) |
ee811287 KO |
267 | { |
268 | return PAGE_SIZE << b->page_order; | |
269 | } | |
270 | ||
a85e968e | 271 | static inline size_t btree_keys_cachelines(struct btree_keys *b) |
ee811287 KO |
272 | { |
273 | return btree_keys_bytes(b) / BSET_CACHELINE; | |
274 | } | |
275 | ||
276 | /* Space required for the auxiliary search trees */ | |
a85e968e | 277 | static inline size_t bset_tree_bytes(struct btree_keys *b) |
ee811287 KO |
278 | { |
279 | return btree_keys_cachelines(b) * sizeof(struct bkey_float); | |
280 | } | |
281 | ||
282 | /* Space required for the prev pointers */ | |
a85e968e | 283 | static inline size_t bset_prev_bytes(struct btree_keys *b) |
ee811287 KO |
284 | { |
285 | return btree_keys_cachelines(b) * sizeof(uint8_t); | |
286 | } | |
287 | ||
288 | /* Memory allocation */ | |
289 | ||
a85e968e | 290 | void bch_btree_keys_free(struct btree_keys *b) |
ee811287 | 291 | { |
a85e968e | 292 | struct bset_tree *t = b->set; |
ee811287 KO |
293 | |
294 | if (bset_prev_bytes(b) < PAGE_SIZE) | |
295 | kfree(t->prev); | |
296 | else | |
297 | free_pages((unsigned long) t->prev, | |
298 | get_order(bset_prev_bytes(b))); | |
299 | ||
300 | if (bset_tree_bytes(b) < PAGE_SIZE) | |
301 | kfree(t->tree); | |
302 | else | |
303 | free_pages((unsigned long) t->tree, | |
304 | get_order(bset_tree_bytes(b))); | |
305 | ||
306 | free_pages((unsigned long) t->data, b->page_order); | |
307 | ||
308 | t->prev = NULL; | |
309 | t->tree = NULL; | |
310 | t->data = NULL; | |
311 | } | |
a85e968e | 312 | EXPORT_SYMBOL(bch_btree_keys_free); |
ee811287 | 313 | |
b0d30981 CL |
314 | int bch_btree_keys_alloc(struct btree_keys *b, |
315 | unsigned int page_order, | |
316 | gfp_t gfp) | |
ee811287 | 317 | { |
a85e968e | 318 | struct bset_tree *t = b->set; |
ee811287 KO |
319 | |
320 | BUG_ON(t->data); | |
321 | ||
322 | b->page_order = page_order; | |
323 | ||
324 | t->data = (void *) __get_free_pages(gfp, b->page_order); | |
325 | if (!t->data) | |
326 | goto err; | |
327 | ||
328 | t->tree = bset_tree_bytes(b) < PAGE_SIZE | |
329 | ? kmalloc(bset_tree_bytes(b), gfp) | |
330 | : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b))); | |
331 | if (!t->tree) | |
332 | goto err; | |
333 | ||
334 | t->prev = bset_prev_bytes(b) < PAGE_SIZE | |
335 | ? kmalloc(bset_prev_bytes(b), gfp) | |
336 | : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b))); | |
337 | if (!t->prev) | |
338 | goto err; | |
339 | ||
340 | return 0; | |
341 | err: | |
342 | bch_btree_keys_free(b); | |
343 | return -ENOMEM; | |
344 | } | |
a85e968e KO |
345 | EXPORT_SYMBOL(bch_btree_keys_alloc); |
346 | ||
347 | void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops, | |
348 | bool *expensive_debug_checks) | |
349 | { | |
6f10f7d1 | 350 | unsigned int i; |
a85e968e KO |
351 | |
352 | b->ops = ops; | |
353 | b->expensive_debug_checks = expensive_debug_checks; | |
354 | b->nsets = 0; | |
355 | b->last_set_unwritten = 0; | |
356 | ||
357 | /* XXX: shouldn't be needed */ | |
358 | for (i = 0; i < MAX_BSETS; i++) | |
359 | b->set[i].size = 0; | |
360 | /* | |
361 | * Second loop starts at 1 because b->keys[0]->data is the memory we | |
362 | * allocated | |
363 | */ | |
364 | for (i = 1; i < MAX_BSETS; i++) | |
365 | b->set[i].data = NULL; | |
366 | } | |
367 | EXPORT_SYMBOL(bch_btree_keys_init); | |
ee811287 | 368 | |
cafe5635 KO |
369 | /* Binary tree stuff for auxiliary search trees */ |
370 | ||
b467a6ac CL |
371 | /* |
372 | * return array index next to j when does in-order traverse | |
373 | * of a binary tree which is stored in a linear array | |
374 | */ | |
6f10f7d1 | 375 | static unsigned int inorder_next(unsigned int j, unsigned int size) |
cafe5635 KO |
376 | { |
377 | if (j * 2 + 1 < size) { | |
378 | j = j * 2 + 1; | |
379 | ||
380 | while (j * 2 < size) | |
381 | j *= 2; | |
382 | } else | |
383 | j >>= ffz(j) + 1; | |
384 | ||
385 | return j; | |
386 | } | |
387 | ||
b467a6ac CL |
388 | /* |
389 | * return array index previous to j when does in-order traverse | |
390 | * of a binary tree which is stored in a linear array | |
391 | */ | |
6f10f7d1 | 392 | static unsigned int inorder_prev(unsigned int j, unsigned int size) |
cafe5635 KO |
393 | { |
394 | if (j * 2 < size) { | |
395 | j = j * 2; | |
396 | ||
397 | while (j * 2 + 1 < size) | |
398 | j = j * 2 + 1; | |
399 | } else | |
400 | j >>= ffs(j); | |
401 | ||
402 | return j; | |
403 | } | |
404 | ||
3be11dba CL |
405 | /* |
406 | * I have no idea why this code works... and I'm the one who wrote it | |
cafe5635 KO |
407 | * |
408 | * However, I do know what it does: | |
409 | * Given a binary tree constructed in an array (i.e. how you normally implement | |
410 | * a heap), it converts a node in the tree - referenced by array index - to the | |
411 | * index it would have if you did an inorder traversal. | |
412 | * | |
413 | * Also tested for every j, size up to size somewhere around 6 million. | |
414 | * | |
415 | * The binary tree starts at array index 1, not 0 | |
416 | * extra is a function of size: | |
417 | * extra = (size - rounddown_pow_of_two(size - 1)) << 1; | |
418 | */ | |
6f10f7d1 CL |
419 | static unsigned int __to_inorder(unsigned int j, |
420 | unsigned int size, | |
421 | unsigned int extra) | |
cafe5635 | 422 | { |
6f10f7d1 CL |
423 | unsigned int b = fls(j); |
424 | unsigned int shift = fls(size - 1) - b; | |
cafe5635 KO |
425 | |
426 | j ^= 1U << (b - 1); | |
427 | j <<= 1; | |
428 | j |= 1; | |
429 | j <<= shift; | |
430 | ||
431 | if (j > extra) | |
432 | j -= (j - extra) >> 1; | |
433 | ||
434 | return j; | |
435 | } | |
436 | ||
b467a6ac CL |
437 | /* |
438 | * Return the cacheline index in bset_tree->data, where j is index | |
439 | * from a linear array which stores the auxiliar binary tree | |
440 | */ | |
6f10f7d1 | 441 | static unsigned int to_inorder(unsigned int j, struct bset_tree *t) |
cafe5635 KO |
442 | { |
443 | return __to_inorder(j, t->size, t->extra); | |
444 | } | |
445 | ||
6f10f7d1 CL |
446 | static unsigned int __inorder_to_tree(unsigned int j, |
447 | unsigned int size, | |
448 | unsigned int extra) | |
cafe5635 | 449 | { |
6f10f7d1 | 450 | unsigned int shift; |
cafe5635 KO |
451 | |
452 | if (j > extra) | |
453 | j += j - extra; | |
454 | ||
455 | shift = ffs(j); | |
456 | ||
457 | j >>= shift; | |
458 | j |= roundup_pow_of_two(size) >> shift; | |
459 | ||
460 | return j; | |
461 | } | |
462 | ||
b467a6ac CL |
463 | /* |
464 | * Return an index from a linear array which stores the auxiliar binary | |
465 | * tree, j is the cacheline index of t->data. | |
466 | */ | |
6f10f7d1 | 467 | static unsigned int inorder_to_tree(unsigned int j, struct bset_tree *t) |
cafe5635 KO |
468 | { |
469 | return __inorder_to_tree(j, t->size, t->extra); | |
470 | } | |
471 | ||
472 | #if 0 | |
473 | void inorder_test(void) | |
474 | { | |
475 | unsigned long done = 0; | |
476 | ktime_t start = ktime_get(); | |
477 | ||
6f10f7d1 | 478 | for (unsigned int size = 2; |
cafe5635 KO |
479 | size < 65536000; |
480 | size++) { | |
b0d30981 CL |
481 | unsigned int extra = |
482 | (size - rounddown_pow_of_two(size - 1)) << 1; | |
6f10f7d1 | 483 | unsigned int i = 1, j = rounddown_pow_of_two(size - 1); |
cafe5635 KO |
484 | |
485 | if (!(size % 4096)) | |
6ae63e35 | 486 | pr_notice("loop %u, %llu per us\n", size, |
cafe5635 KO |
487 | done / ktime_us_delta(ktime_get(), start)); |
488 | ||
489 | while (1) { | |
490 | if (__inorder_to_tree(i, size, extra) != j) | |
491 | panic("size %10u j %10u i %10u", size, j, i); | |
492 | ||
493 | if (__to_inorder(j, size, extra) != i) | |
494 | panic("size %10u j %10u i %10u", size, j, i); | |
495 | ||
496 | if (j == rounddown_pow_of_two(size) - 1) | |
497 | break; | |
498 | ||
499 | BUG_ON(inorder_prev(inorder_next(j, size), size) != j); | |
500 | ||
501 | j = inorder_next(j, size); | |
502 | i++; | |
503 | } | |
504 | ||
505 | done += size - 1; | |
506 | } | |
507 | } | |
508 | #endif | |
509 | ||
510 | /* | |
48a73025 | 511 | * Cacheline/offset <-> bkey pointer arithmetic: |
cafe5635 KO |
512 | * |
513 | * t->tree is a binary search tree in an array; each node corresponds to a key | |
514 | * in one cacheline in t->set (BSET_CACHELINE bytes). | |
515 | * | |
516 | * This means we don't have to store the full index of the key that a node in | |
517 | * the binary tree points to; to_inorder() gives us the cacheline, and then | |
518 | * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes. | |
519 | * | |
48a73025 | 520 | * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to |
cafe5635 KO |
521 | * make this work. |
522 | * | |
523 | * To construct the bfloat for an arbitrary key we need to know what the key | |
524 | * immediately preceding it is: we have to check if the two keys differ in the | |
525 | * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size | |
526 | * of the previous key so we can walk backwards to it from t->tree[j]'s key. | |
527 | */ | |
528 | ||
6f10f7d1 CL |
529 | static struct bkey *cacheline_to_bkey(struct bset_tree *t, |
530 | unsigned int cacheline, | |
531 | unsigned int offset) | |
cafe5635 KO |
532 | { |
533 | return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8; | |
534 | } | |
535 | ||
6f10f7d1 | 536 | static unsigned int bkey_to_cacheline(struct bset_tree *t, struct bkey *k) |
cafe5635 KO |
537 | { |
538 | return ((void *) k - (void *) t->data) / BSET_CACHELINE; | |
539 | } | |
540 | ||
6f10f7d1 CL |
541 | static unsigned int bkey_to_cacheline_offset(struct bset_tree *t, |
542 | unsigned int cacheline, | |
9dd6358a | 543 | struct bkey *k) |
cafe5635 | 544 | { |
9dd6358a | 545 | return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0); |
cafe5635 KO |
546 | } |
547 | ||
6f10f7d1 | 548 | static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned int j) |
cafe5635 KO |
549 | { |
550 | return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m); | |
551 | } | |
552 | ||
6f10f7d1 | 553 | static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned int j) |
cafe5635 KO |
554 | { |
555 | return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]); | |
556 | } | |
557 | ||
558 | /* | |
559 | * For the write set - the one we're currently inserting keys into - we don't | |
560 | * maintain a full search tree, we just keep a simple lookup table in t->prev. | |
561 | */ | |
6f10f7d1 | 562 | static struct bkey *table_to_bkey(struct bset_tree *t, unsigned int cacheline) |
cafe5635 KO |
563 | { |
564 | return cacheline_to_bkey(t, cacheline, t->prev[cacheline]); | |
565 | } | |
566 | ||
567 | static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift) | |
568 | { | |
cafe5635 KO |
569 | low >>= shift; |
570 | low |= (high << 1) << (63U - shift); | |
cafe5635 KO |
571 | return low; |
572 | } | |
573 | ||
b467a6ac CL |
574 | /* |
575 | * Calculate mantissa value for struct bkey_float. | |
576 | * If most significant bit of f->exponent is not set, then | |
577 | * - f->exponent >> 6 is 0 | |
578 | * - p[0] points to bkey->low | |
579 | * - p[-1] borrows bits from KEY_INODE() of bkey->high | |
580 | * if most isgnificant bits of f->exponent is set, then | |
581 | * - f->exponent >> 6 is 1 | |
582 | * - p[0] points to bits from KEY_INODE() of bkey->high | |
583 | * - p[-1] points to other bits from KEY_INODE() of | |
584 | * bkey->high too. | |
585 | * See make_bfloat() to check when most significant bit of f->exponent | |
586 | * is set or not. | |
587 | */ | |
6f10f7d1 | 588 | static inline unsigned int bfloat_mantissa(const struct bkey *k, |
cafe5635 KO |
589 | struct bkey_float *f) |
590 | { | |
591 | const uint64_t *p = &k->low - (f->exponent >> 6); | |
1fae7cf0 | 592 | |
cafe5635 KO |
593 | return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK; |
594 | } | |
595 | ||
6f10f7d1 | 596 | static void make_bfloat(struct bset_tree *t, unsigned int j) |
cafe5635 KO |
597 | { |
598 | struct bkey_float *f = &t->tree[j]; | |
599 | struct bkey *m = tree_to_bkey(t, j); | |
600 | struct bkey *p = tree_to_prev_bkey(t, j); | |
601 | ||
602 | struct bkey *l = is_power_of_2(j) | |
603 | ? t->data->start | |
604 | : tree_to_prev_bkey(t, j >> ffs(j)); | |
605 | ||
606 | struct bkey *r = is_power_of_2(j + 1) | |
fafff81c | 607 | ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end)) |
cafe5635 KO |
608 | : tree_to_bkey(t, j >> (ffz(j) + 1)); |
609 | ||
610 | BUG_ON(m < l || m > r); | |
611 | BUG_ON(bkey_next(p) != m); | |
612 | ||
b467a6ac CL |
613 | /* |
614 | * If l and r have different KEY_INODE values (different backing | |
615 | * device), f->exponent records how many least significant bits | |
616 | * are different in KEY_INODE values and sets most significant | |
617 | * bits to 1 (by +64). | |
618 | * If l and r have same KEY_INODE value, f->exponent records | |
619 | * how many different bits in least significant bits of bkey->low. | |
620 | * See bfloat_mantiss() how the most significant bit of | |
621 | * f->exponent is used to calculate bfloat mantissa value. | |
622 | */ | |
cafe5635 KO |
623 | if (KEY_INODE(l) != KEY_INODE(r)) |
624 | f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64; | |
625 | else | |
626 | f->exponent = fls64(r->low ^ l->low); | |
627 | ||
628 | f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0); | |
629 | ||
630 | /* | |
631 | * Setting f->exponent = 127 flags this node as failed, and causes the | |
632 | * lookup code to fall back to comparing against the original key. | |
633 | */ | |
634 | ||
635 | if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f)) | |
636 | f->mantissa = bfloat_mantissa(m, f) - 1; | |
637 | else | |
638 | f->exponent = 127; | |
639 | } | |
640 | ||
a85e968e | 641 | static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t) |
cafe5635 | 642 | { |
a85e968e | 643 | if (t != b->set) { |
6f10f7d1 | 644 | unsigned int j = roundup(t[-1].size, |
cafe5635 KO |
645 | 64 / sizeof(struct bkey_float)); |
646 | ||
647 | t->tree = t[-1].tree + j; | |
648 | t->prev = t[-1].prev + j; | |
649 | } | |
650 | ||
a85e968e | 651 | while (t < b->set + MAX_BSETS) |
cafe5635 KO |
652 | t++->size = 0; |
653 | } | |
654 | ||
a85e968e | 655 | static void bch_bset_build_unwritten_tree(struct btree_keys *b) |
cafe5635 | 656 | { |
ee811287 | 657 | struct bset_tree *t = bset_tree_last(b); |
cafe5635 | 658 | |
a85e968e KO |
659 | BUG_ON(b->last_set_unwritten); |
660 | b->last_set_unwritten = 1; | |
661 | ||
cafe5635 KO |
662 | bset_alloc_tree(b, t); |
663 | ||
a85e968e | 664 | if (t->tree != b->set->tree + btree_keys_cachelines(b)) { |
9dd6358a | 665 | t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start); |
cafe5635 KO |
666 | t->size = 1; |
667 | } | |
668 | } | |
669 | ||
a85e968e | 670 | void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic) |
ee811287 | 671 | { |
a85e968e KO |
672 | if (i != b->set->data) { |
673 | b->set[++b->nsets].data = i; | |
674 | i->seq = b->set->data->seq; | |
ee811287 KO |
675 | } else |
676 | get_random_bytes(&i->seq, sizeof(uint64_t)); | |
677 | ||
678 | i->magic = magic; | |
679 | i->version = 0; | |
680 | i->keys = 0; | |
681 | ||
682 | bch_bset_build_unwritten_tree(b); | |
683 | } | |
a85e968e | 684 | EXPORT_SYMBOL(bch_bset_init_next); |
ee811287 | 685 | |
b467a6ac CL |
686 | /* |
687 | * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to | |
688 | * accelerate bkey search in a btree node (pointed by bset_tree->data in | |
689 | * memory). After search in the auxiliar tree by calling bset_search_tree(), | |
690 | * a struct bset_search_iter is returned which indicates range [l, r] from | |
691 | * bset_tree->data where the searching bkey might be inside. Then a followed | |
692 | * linear comparison does the exact search, see __bch_bset_search() for how | |
693 | * the auxiliary tree is used. | |
694 | */ | |
a85e968e | 695 | void bch_bset_build_written_tree(struct btree_keys *b) |
cafe5635 | 696 | { |
ee811287 | 697 | struct bset_tree *t = bset_tree_last(b); |
9dd6358a | 698 | struct bkey *prev = NULL, *k = t->data->start; |
6f10f7d1 | 699 | unsigned int j, cacheline = 1; |
cafe5635 | 700 | |
a85e968e KO |
701 | b->last_set_unwritten = 0; |
702 | ||
cafe5635 KO |
703 | bset_alloc_tree(b, t); |
704 | ||
6f10f7d1 | 705 | t->size = min_t(unsigned int, |
fafff81c | 706 | bkey_to_cacheline(t, bset_bkey_last(t->data)), |
a85e968e | 707 | b->set->tree + btree_keys_cachelines(b) - t->tree); |
cafe5635 KO |
708 | |
709 | if (t->size < 2) { | |
710 | t->size = 0; | |
711 | return; | |
712 | } | |
713 | ||
714 | t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1; | |
715 | ||
716 | /* First we figure out where the first key in each cacheline is */ | |
717 | for (j = inorder_next(0, t->size); | |
718 | j; | |
719 | j = inorder_next(j, t->size)) { | |
9dd6358a KO |
720 | while (bkey_to_cacheline(t, k) < cacheline) |
721 | prev = k, k = bkey_next(k); | |
cafe5635 | 722 | |
9dd6358a KO |
723 | t->prev[j] = bkey_u64s(prev); |
724 | t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k); | |
cafe5635 KO |
725 | } |
726 | ||
fafff81c | 727 | while (bkey_next(k) != bset_bkey_last(t->data)) |
cafe5635 KO |
728 | k = bkey_next(k); |
729 | ||
730 | t->end = *k; | |
731 | ||
732 | /* Then we build the tree */ | |
733 | for (j = inorder_next(0, t->size); | |
734 | j; | |
735 | j = inorder_next(j, t->size)) | |
736 | make_bfloat(t, j); | |
737 | } | |
a85e968e | 738 | EXPORT_SYMBOL(bch_bset_build_written_tree); |
cafe5635 | 739 | |
829a60b9 KO |
740 | /* Insert */ |
741 | ||
a85e968e | 742 | void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k) |
cafe5635 KO |
743 | { |
744 | struct bset_tree *t; | |
6f10f7d1 | 745 | unsigned int inorder, j = 1; |
cafe5635 | 746 | |
a85e968e | 747 | for (t = b->set; t <= bset_tree_last(b); t++) |
fafff81c | 748 | if (k < bset_bkey_last(t->data)) |
cafe5635 KO |
749 | goto found_set; |
750 | ||
751 | BUG(); | |
752 | found_set: | |
753 | if (!t->size || !bset_written(b, t)) | |
754 | return; | |
755 | ||
756 | inorder = bkey_to_cacheline(t, k); | |
757 | ||
758 | if (k == t->data->start) | |
759 | goto fix_left; | |
760 | ||
fafff81c | 761 | if (bkey_next(k) == bset_bkey_last(t->data)) { |
cafe5635 KO |
762 | t->end = *k; |
763 | goto fix_right; | |
764 | } | |
765 | ||
766 | j = inorder_to_tree(inorder, t); | |
767 | ||
768 | if (j && | |
769 | j < t->size && | |
770 | k == tree_to_bkey(t, j)) | |
771 | fix_left: do { | |
772 | make_bfloat(t, j); | |
773 | j = j * 2; | |
774 | } while (j < t->size); | |
775 | ||
776 | j = inorder_to_tree(inorder + 1, t); | |
777 | ||
778 | if (j && | |
779 | j < t->size && | |
780 | k == tree_to_prev_bkey(t, j)) | |
781 | fix_right: do { | |
782 | make_bfloat(t, j); | |
783 | j = j * 2 + 1; | |
784 | } while (j < t->size); | |
785 | } | |
a85e968e | 786 | EXPORT_SYMBOL(bch_bset_fix_invalidated_key); |
cafe5635 | 787 | |
a85e968e | 788 | static void bch_bset_fix_lookup_table(struct btree_keys *b, |
ee811287 KO |
789 | struct bset_tree *t, |
790 | struct bkey *k) | |
cafe5635 | 791 | { |
6f10f7d1 CL |
792 | unsigned int shift = bkey_u64s(k); |
793 | unsigned int j = bkey_to_cacheline(t, k); | |
cafe5635 KO |
794 | |
795 | /* We're getting called from btree_split() or btree_gc, just bail out */ | |
796 | if (!t->size) | |
797 | return; | |
798 | ||
3be11dba CL |
799 | /* |
800 | * k is the key we just inserted; we need to find the entry in the | |
cafe5635 KO |
801 | * lookup table for the first key that is strictly greater than k: |
802 | * it's either k's cacheline or the next one | |
803 | */ | |
9dd6358a KO |
804 | while (j < t->size && |
805 | table_to_bkey(t, j) <= k) | |
cafe5635 KO |
806 | j++; |
807 | ||
3be11dba CL |
808 | /* |
809 | * Adjust all the lookup table entries, and find a new key for any that | |
cafe5635 KO |
810 | * have gotten too big |
811 | */ | |
812 | for (; j < t->size; j++) { | |
813 | t->prev[j] += shift; | |
814 | ||
815 | if (t->prev[j] > 7) { | |
816 | k = table_to_bkey(t, j - 1); | |
817 | ||
818 | while (k < cacheline_to_bkey(t, j, 0)) | |
819 | k = bkey_next(k); | |
820 | ||
9dd6358a | 821 | t->prev[j] = bkey_to_cacheline_offset(t, j, k); |
cafe5635 KO |
822 | } |
823 | } | |
824 | ||
a85e968e | 825 | if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree) |
cafe5635 KO |
826 | return; |
827 | ||
828 | /* Possibly add a new entry to the end of the lookup table */ | |
829 | ||
830 | for (k = table_to_bkey(t, t->size - 1); | |
fafff81c | 831 | k != bset_bkey_last(t->data); |
cafe5635 KO |
832 | k = bkey_next(k)) |
833 | if (t->size == bkey_to_cacheline(t, k)) { | |
b0d30981 CL |
834 | t->prev[t->size] = |
835 | bkey_to_cacheline_offset(t, t->size, k); | |
cafe5635 KO |
836 | t->size++; |
837 | } | |
838 | } | |
839 | ||
0f49cf3d NS |
840 | /* |
841 | * Tries to merge l and r: l should be lower than r | |
842 | * Returns true if we were able to merge. If we did merge, l will be the merged | |
843 | * key, r will be untouched. | |
844 | */ | |
845 | bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r) | |
846 | { | |
847 | if (!b->ops->key_merge) | |
848 | return false; | |
849 | ||
850 | /* | |
851 | * Generic header checks | |
852 | * Assumes left and right are in order | |
853 | * Left and right must be exactly aligned | |
854 | */ | |
3bdad1e4 NS |
855 | if (!bch_bkey_equal_header(l, r) || |
856 | bkey_cmp(l, &START_KEY(r))) | |
0f49cf3d NS |
857 | return false; |
858 | ||
859 | return b->ops->key_merge(b, l, r); | |
860 | } | |
861 | EXPORT_SYMBOL(bch_bkey_try_merge); | |
862 | ||
a85e968e | 863 | void bch_bset_insert(struct btree_keys *b, struct bkey *where, |
ee811287 | 864 | struct bkey *insert) |
cafe5635 | 865 | { |
ee811287 | 866 | struct bset_tree *t = bset_tree_last(b); |
cafe5635 | 867 | |
a85e968e | 868 | BUG_ON(!b->last_set_unwritten); |
ee811287 KO |
869 | BUG_ON(bset_byte_offset(b, t->data) + |
870 | __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) > | |
871 | PAGE_SIZE << b->page_order); | |
cafe5635 | 872 | |
ee811287 KO |
873 | memmove((uint64_t *) where + bkey_u64s(insert), |
874 | where, | |
875 | (void *) bset_bkey_last(t->data) - (void *) where); | |
cafe5635 | 876 | |
ee811287 KO |
877 | t->data->keys += bkey_u64s(insert); |
878 | bkey_copy(where, insert); | |
879 | bch_bset_fix_lookup_table(b, t, where); | |
cafe5635 | 880 | } |
a85e968e | 881 | EXPORT_SYMBOL(bch_bset_insert); |
cafe5635 | 882 | |
6f10f7d1 | 883 | unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k, |
829a60b9 KO |
884 | struct bkey *replace_key) |
885 | { | |
6f10f7d1 | 886 | unsigned int status = BTREE_INSERT_STATUS_NO_INSERT; |
829a60b9 KO |
887 | struct bset *i = bset_tree_last(b)->data; |
888 | struct bkey *m, *prev = NULL; | |
889 | struct btree_iter iter; | |
31b90956 CL |
890 | struct bkey preceding_key_on_stack = ZERO_KEY; |
891 | struct bkey *preceding_key_p = &preceding_key_on_stack; | |
829a60b9 KO |
892 | |
893 | BUG_ON(b->ops->is_extents && !KEY_SIZE(k)); | |
894 | ||
31b90956 CL |
895 | /* |
896 | * If k has preceding key, preceding_key_p will be set to address | |
897 | * of k's preceding key; otherwise preceding_key_p will be set | |
898 | * to NULL inside preceding_key(). | |
899 | */ | |
900 | if (b->ops->is_extents) | |
901 | preceding_key(&START_KEY(k), &preceding_key_p); | |
902 | else | |
903 | preceding_key(k, &preceding_key_p); | |
904 | ||
905 | m = bch_btree_iter_init(b, &iter, preceding_key_p); | |
829a60b9 KO |
906 | |
907 | if (b->ops->insert_fixup(b, k, &iter, replace_key)) | |
908 | return status; | |
909 | ||
910 | status = BTREE_INSERT_STATUS_INSERT; | |
911 | ||
912 | while (m != bset_bkey_last(i) && | |
913 | bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0) | |
914 | prev = m, m = bkey_next(m); | |
915 | ||
916 | /* prev is in the tree, if we merge we're done */ | |
917 | status = BTREE_INSERT_STATUS_BACK_MERGE; | |
918 | if (prev && | |
919 | bch_bkey_try_merge(b, prev, k)) | |
920 | goto merged; | |
921 | #if 0 | |
922 | status = BTREE_INSERT_STATUS_OVERWROTE; | |
923 | if (m != bset_bkey_last(i) && | |
924 | KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m)) | |
925 | goto copy; | |
926 | #endif | |
927 | status = BTREE_INSERT_STATUS_FRONT_MERGE; | |
928 | if (m != bset_bkey_last(i) && | |
929 | bch_bkey_try_merge(b, k, m)) | |
930 | goto copy; | |
931 | ||
932 | bch_bset_insert(b, m, k); | |
933 | copy: bkey_copy(m, k); | |
934 | merged: | |
935 | return status; | |
936 | } | |
937 | EXPORT_SYMBOL(bch_btree_insert_key); | |
938 | ||
939 | /* Lookup */ | |
940 | ||
cafe5635 KO |
941 | struct bset_search_iter { |
942 | struct bkey *l, *r; | |
943 | }; | |
944 | ||
a85e968e | 945 | static struct bset_search_iter bset_search_write_set(struct bset_tree *t, |
cafe5635 KO |
946 | const struct bkey *search) |
947 | { | |
6f10f7d1 | 948 | unsigned int li = 0, ri = t->size; |
cafe5635 | 949 | |
cafe5635 | 950 | while (li + 1 != ri) { |
6f10f7d1 | 951 | unsigned int m = (li + ri) >> 1; |
cafe5635 KO |
952 | |
953 | if (bkey_cmp(table_to_bkey(t, m), search) > 0) | |
954 | ri = m; | |
955 | else | |
956 | li = m; | |
957 | } | |
958 | ||
959 | return (struct bset_search_iter) { | |
960 | table_to_bkey(t, li), | |
fafff81c | 961 | ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data) |
cafe5635 KO |
962 | }; |
963 | } | |
964 | ||
a85e968e | 965 | static struct bset_search_iter bset_search_tree(struct bset_tree *t, |
cafe5635 KO |
966 | const struct bkey *search) |
967 | { | |
968 | struct bkey *l, *r; | |
969 | struct bkey_float *f; | |
6f10f7d1 | 970 | unsigned int inorder, j, n = 1; |
cafe5635 KO |
971 | |
972 | do { | |
6f10f7d1 | 973 | unsigned int p = n << 4; |
1fae7cf0 | 974 | |
f960facb CL |
975 | if (p < t->size) |
976 | prefetch(&t->tree[p]); | |
cafe5635 KO |
977 | |
978 | j = n; | |
979 | f = &t->tree[j]; | |
980 | ||
981 | /* | |
b467a6ac CL |
982 | * Similar bit trick, use subtract operation to avoid a branch |
983 | * instruction. | |
984 | * | |
cafe5635 KO |
985 | * n = (f->mantissa > bfloat_mantissa()) |
986 | * ? j * 2 | |
987 | * : j * 2 + 1; | |
988 | * | |
989 | * We need to subtract 1 from f->mantissa for the sign bit trick | |
990 | * to work - that's done in make_bfloat() | |
991 | */ | |
992 | if (likely(f->exponent != 127)) | |
6f10f7d1 | 993 | n = j * 2 + (((unsigned int) |
cafe5635 KO |
994 | (f->mantissa - |
995 | bfloat_mantissa(search, f))) >> 31); | |
996 | else | |
997 | n = (bkey_cmp(tree_to_bkey(t, j), search) > 0) | |
998 | ? j * 2 | |
999 | : j * 2 + 1; | |
1000 | } while (n < t->size); | |
1001 | ||
1002 | inorder = to_inorder(j, t); | |
1003 | ||
1004 | /* | |
1005 | * n would have been the node we recursed to - the low bit tells us if | |
1006 | * we recursed left or recursed right. | |
1007 | */ | |
1008 | if (n & 1) { | |
1009 | l = cacheline_to_bkey(t, inorder, f->m); | |
1010 | ||
1011 | if (++inorder != t->size) { | |
1012 | f = &t->tree[inorder_next(j, t->size)]; | |
1013 | r = cacheline_to_bkey(t, inorder, f->m); | |
1014 | } else | |
fafff81c | 1015 | r = bset_bkey_last(t->data); |
cafe5635 KO |
1016 | } else { |
1017 | r = cacheline_to_bkey(t, inorder, f->m); | |
1018 | ||
1019 | if (--inorder) { | |
1020 | f = &t->tree[inorder_prev(j, t->size)]; | |
1021 | l = cacheline_to_bkey(t, inorder, f->m); | |
1022 | } else | |
1023 | l = t->data->start; | |
1024 | } | |
1025 | ||
1026 | return (struct bset_search_iter) {l, r}; | |
1027 | } | |
1028 | ||
c052dd9a | 1029 | struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t, |
cafe5635 KO |
1030 | const struct bkey *search) |
1031 | { | |
1032 | struct bset_search_iter i; | |
1033 | ||
1034 | /* | |
1035 | * First, we search for a cacheline, then lastly we do a linear search | |
1036 | * within that cacheline. | |
1037 | * | |
1038 | * To search for the cacheline, there's three different possibilities: | |
1039 | * * The set is too small to have a search tree, so we just do a linear | |
1040 | * search over the whole set. | |
1041 | * * The set is the one we're currently inserting into; keeping a full | |
1042 | * auxiliary search tree up to date would be too expensive, so we | |
1043 | * use a much simpler lookup table to do a binary search - | |
1044 | * bset_search_write_set(). | |
1045 | * * Or we use the auxiliary search tree we constructed earlier - | |
1046 | * bset_search_tree() | |
1047 | */ | |
1048 | ||
1049 | if (unlikely(!t->size)) { | |
1050 | i.l = t->data->start; | |
fafff81c | 1051 | i.r = bset_bkey_last(t->data); |
c052dd9a | 1052 | } else if (bset_written(b, t)) { |
cafe5635 KO |
1053 | /* |
1054 | * Each node in the auxiliary search tree covers a certain range | |
1055 | * of bits, and keys above and below the set it covers might | |
1056 | * differ outside those bits - so we have to special case the | |
1057 | * start and end - handle that here: | |
1058 | */ | |
1059 | ||
1060 | if (unlikely(bkey_cmp(search, &t->end) >= 0)) | |
fafff81c | 1061 | return bset_bkey_last(t->data); |
cafe5635 KO |
1062 | |
1063 | if (unlikely(bkey_cmp(search, t->data->start) < 0)) | |
1064 | return t->data->start; | |
1065 | ||
a85e968e KO |
1066 | i = bset_search_tree(t, search); |
1067 | } else { | |
c052dd9a | 1068 | BUG_ON(!b->nsets && |
a85e968e KO |
1069 | t->size < bkey_to_cacheline(t, bset_bkey_last(t->data))); |
1070 | ||
1071 | i = bset_search_write_set(t, search); | |
1072 | } | |
cafe5635 | 1073 | |
c052dd9a KO |
1074 | if (btree_keys_expensive_checks(b)) { |
1075 | BUG_ON(bset_written(b, t) && | |
280481d0 KO |
1076 | i.l != t->data->start && |
1077 | bkey_cmp(tree_to_prev_bkey(t, | |
1078 | inorder_to_tree(bkey_to_cacheline(t, i.l), t)), | |
1079 | search) > 0); | |
cafe5635 | 1080 | |
fafff81c | 1081 | BUG_ON(i.r != bset_bkey_last(t->data) && |
280481d0 KO |
1082 | bkey_cmp(i.r, search) <= 0); |
1083 | } | |
cafe5635 KO |
1084 | |
1085 | while (likely(i.l != i.r) && | |
1086 | bkey_cmp(i.l, search) <= 0) | |
1087 | i.l = bkey_next(i.l); | |
1088 | ||
1089 | return i.l; | |
1090 | } | |
a85e968e | 1091 | EXPORT_SYMBOL(__bch_bset_search); |
cafe5635 KO |
1092 | |
1093 | /* Btree iterator */ | |
1094 | ||
911c9610 KO |
1095 | typedef bool (btree_iter_cmp_fn)(struct btree_iter_set, |
1096 | struct btree_iter_set); | |
1097 | ||
cafe5635 KO |
1098 | static inline bool btree_iter_cmp(struct btree_iter_set l, |
1099 | struct btree_iter_set r) | |
1100 | { | |
911c9610 | 1101 | return bkey_cmp(l.k, r.k) > 0; |
cafe5635 KO |
1102 | } |
1103 | ||
1104 | static inline bool btree_iter_end(struct btree_iter *iter) | |
1105 | { | |
1106 | return !iter->used; | |
1107 | } | |
1108 | ||
1109 | void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k, | |
1110 | struct bkey *end) | |
1111 | { | |
1112 | if (k != end) | |
1113 | BUG_ON(!heap_add(iter, | |
1114 | ((struct btree_iter_set) { k, end }), | |
1115 | btree_iter_cmp)); | |
1116 | } | |
1117 | ||
c052dd9a | 1118 | static struct bkey *__bch_btree_iter_init(struct btree_keys *b, |
911c9610 KO |
1119 | struct btree_iter *iter, |
1120 | struct bkey *search, | |
1121 | struct bset_tree *start) | |
cafe5635 KO |
1122 | { |
1123 | struct bkey *ret = NULL; | |
1fae7cf0 | 1124 | |
cafe5635 KO |
1125 | iter->size = ARRAY_SIZE(iter->data); |
1126 | iter->used = 0; | |
1127 | ||
280481d0 KO |
1128 | #ifdef CONFIG_BCACHE_DEBUG |
1129 | iter->b = b; | |
1130 | #endif | |
1131 | ||
c052dd9a | 1132 | for (; start <= bset_tree_last(b); start++) { |
cafe5635 | 1133 | ret = bch_bset_search(b, start, search); |
fafff81c | 1134 | bch_btree_iter_push(iter, ret, bset_bkey_last(start->data)); |
cafe5635 KO |
1135 | } |
1136 | ||
1137 | return ret; | |
1138 | } | |
1139 | ||
c052dd9a | 1140 | struct bkey *bch_btree_iter_init(struct btree_keys *b, |
911c9610 KO |
1141 | struct btree_iter *iter, |
1142 | struct bkey *search) | |
1143 | { | |
c052dd9a | 1144 | return __bch_btree_iter_init(b, iter, search, b->set); |
911c9610 | 1145 | } |
a85e968e | 1146 | EXPORT_SYMBOL(bch_btree_iter_init); |
911c9610 KO |
1147 | |
1148 | static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter, | |
1149 | btree_iter_cmp_fn *cmp) | |
cafe5635 | 1150 | { |
42361469 | 1151 | struct btree_iter_set b __maybe_unused; |
cafe5635 KO |
1152 | struct bkey *ret = NULL; |
1153 | ||
1154 | if (!btree_iter_end(iter)) { | |
280481d0 KO |
1155 | bch_btree_iter_next_check(iter); |
1156 | ||
cafe5635 KO |
1157 | ret = iter->data->k; |
1158 | iter->data->k = bkey_next(iter->data->k); | |
1159 | ||
1160 | if (iter->data->k > iter->data->end) { | |
cc0f4eaa | 1161 | WARN_ONCE(1, "bset was corrupt!\n"); |
cafe5635 KO |
1162 | iter->data->k = iter->data->end; |
1163 | } | |
1164 | ||
1165 | if (iter->data->k == iter->data->end) | |
42361469 | 1166 | heap_pop(iter, b, cmp); |
cafe5635 | 1167 | else |
911c9610 | 1168 | heap_sift(iter, 0, cmp); |
cafe5635 KO |
1169 | } |
1170 | ||
1171 | return ret; | |
1172 | } | |
1173 | ||
911c9610 KO |
1174 | struct bkey *bch_btree_iter_next(struct btree_iter *iter) |
1175 | { | |
1176 | return __bch_btree_iter_next(iter, btree_iter_cmp); | |
1177 | ||
1178 | } | |
a85e968e | 1179 | EXPORT_SYMBOL(bch_btree_iter_next); |
911c9610 | 1180 | |
cafe5635 | 1181 | struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter, |
a85e968e | 1182 | struct btree_keys *b, ptr_filter_fn fn) |
cafe5635 KO |
1183 | { |
1184 | struct bkey *ret; | |
1185 | ||
1186 | do { | |
1187 | ret = bch_btree_iter_next(iter); | |
1188 | } while (ret && fn(b, ret)); | |
1189 | ||
1190 | return ret; | |
1191 | } | |
1192 | ||
cafe5635 KO |
1193 | /* Mergesort */ |
1194 | ||
67539e85 KO |
1195 | void bch_bset_sort_state_free(struct bset_sort_state *state) |
1196 | { | |
d19936a2 | 1197 | mempool_exit(&state->pool); |
67539e85 KO |
1198 | } |
1199 | ||
6f10f7d1 CL |
1200 | int bch_bset_sort_state_init(struct bset_sort_state *state, |
1201 | unsigned int page_order) | |
67539e85 KO |
1202 | { |
1203 | spin_lock_init(&state->time.lock); | |
1204 | ||
1205 | state->page_order = page_order; | |
1206 | state->crit_factor = int_sqrt(1 << page_order); | |
1207 | ||
d19936a2 | 1208 | return mempool_init_page_pool(&state->pool, 1, page_order); |
67539e85 | 1209 | } |
a85e968e | 1210 | EXPORT_SYMBOL(bch_bset_sort_state_init); |
67539e85 | 1211 | |
a85e968e | 1212 | static void btree_mergesort(struct btree_keys *b, struct bset *out, |
cafe5635 KO |
1213 | struct btree_iter *iter, |
1214 | bool fixup, bool remove_stale) | |
1215 | { | |
911c9610 | 1216 | int i; |
cafe5635 | 1217 | struct bkey *k, *last = NULL; |
ef71ec00 | 1218 | BKEY_PADDED(k) tmp; |
a85e968e | 1219 | bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale |
cafe5635 KO |
1220 | ? bch_ptr_bad |
1221 | : bch_ptr_invalid; | |
1222 | ||
911c9610 KO |
1223 | /* Heapify the iterator, using our comparison function */ |
1224 | for (i = iter->used / 2 - 1; i >= 0; --i) | |
65d45231 | 1225 | heap_sift(iter, i, b->ops->sort_cmp); |
911c9610 | 1226 | |
cafe5635 | 1227 | while (!btree_iter_end(iter)) { |
65d45231 KO |
1228 | if (b->ops->sort_fixup && fixup) |
1229 | k = b->ops->sort_fixup(iter, &tmp.k); | |
ef71ec00 KO |
1230 | else |
1231 | k = NULL; | |
1232 | ||
1233 | if (!k) | |
65d45231 | 1234 | k = __bch_btree_iter_next(iter, b->ops->sort_cmp); |
cafe5635 | 1235 | |
cafe5635 KO |
1236 | if (bad(b, k)) |
1237 | continue; | |
1238 | ||
1239 | if (!last) { | |
1240 | last = out->start; | |
1241 | bkey_copy(last, k); | |
65d45231 | 1242 | } else if (!bch_bkey_try_merge(b, last, k)) { |
cafe5635 KO |
1243 | last = bkey_next(last); |
1244 | bkey_copy(last, k); | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0; | |
1249 | ||
1250 | pr_debug("sorted %i keys", out->keys); | |
cafe5635 KO |
1251 | } |
1252 | ||
a85e968e | 1253 | static void __btree_sort(struct btree_keys *b, struct btree_iter *iter, |
6f10f7d1 | 1254 | unsigned int start, unsigned int order, bool fixup, |
67539e85 | 1255 | struct bset_sort_state *state) |
cafe5635 KO |
1256 | { |
1257 | uint64_t start_time; | |
0a451145 | 1258 | bool used_mempool = false; |
501d52a9 | 1259 | struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOWAIT, |
cafe5635 KO |
1260 | order); |
1261 | if (!out) { | |
3572324a KO |
1262 | struct page *outp; |
1263 | ||
67539e85 KO |
1264 | BUG_ON(order > state->page_order); |
1265 | ||
d19936a2 | 1266 | outp = mempool_alloc(&state->pool, GFP_NOIO); |
3572324a | 1267 | out = page_address(outp); |
0a451145 | 1268 | used_mempool = true; |
a85e968e | 1269 | order = state->page_order; |
cafe5635 KO |
1270 | } |
1271 | ||
1272 | start_time = local_clock(); | |
1273 | ||
67539e85 | 1274 | btree_mergesort(b, out, iter, fixup, false); |
cafe5635 KO |
1275 | b->nsets = start; |
1276 | ||
cafe5635 KO |
1277 | if (!start && order == b->page_order) { |
1278 | /* | |
1279 | * Our temporary buffer is the same size as the btree node's | |
1280 | * buffer, we can just swap buffers instead of doing a big | |
1281 | * memcpy() | |
1282 | */ | |
1283 | ||
a85e968e KO |
1284 | out->magic = b->set->data->magic; |
1285 | out->seq = b->set->data->seq; | |
1286 | out->version = b->set->data->version; | |
1287 | swap(out, b->set->data); | |
cafe5635 | 1288 | } else { |
a85e968e KO |
1289 | b->set[start].data->keys = out->keys; |
1290 | memcpy(b->set[start].data->start, out->start, | |
fafff81c | 1291 | (void *) bset_bkey_last(out) - (void *) out->start); |
cafe5635 KO |
1292 | } |
1293 | ||
0a451145 | 1294 | if (used_mempool) |
d19936a2 | 1295 | mempool_free(virt_to_page(out), &state->pool); |
cafe5635 KO |
1296 | else |
1297 | free_pages((unsigned long) out, order); | |
1298 | ||
a85e968e | 1299 | bch_bset_build_written_tree(b); |
cafe5635 | 1300 | |
65d22e91 | 1301 | if (!start) |
67539e85 | 1302 | bch_time_stats_update(&state->time, start_time); |
cafe5635 KO |
1303 | } |
1304 | ||
6f10f7d1 | 1305 | void bch_btree_sort_partial(struct btree_keys *b, unsigned int start, |
67539e85 | 1306 | struct bset_sort_state *state) |
cafe5635 | 1307 | { |
89ebb4a2 | 1308 | size_t order = b->page_order, keys = 0; |
cafe5635 | 1309 | struct btree_iter iter; |
89ebb4a2 | 1310 | int oldsize = bch_count_data(b); |
280481d0 | 1311 | |
89ebb4a2 | 1312 | __bch_btree_iter_init(b, &iter, NULL, &b->set[start]); |
cafe5635 KO |
1313 | |
1314 | if (start) { | |
6f10f7d1 | 1315 | unsigned int i; |
cafe5635 | 1316 | |
89ebb4a2 KO |
1317 | for (i = start; i <= b->nsets; i++) |
1318 | keys += b->set[i].data->keys; | |
cafe5635 | 1319 | |
89ebb4a2 | 1320 | order = get_order(__set_bytes(b->set->data, keys)); |
cafe5635 KO |
1321 | } |
1322 | ||
89ebb4a2 | 1323 | __btree_sort(b, &iter, start, order, false, state); |
cafe5635 | 1324 | |
89ebb4a2 | 1325 | EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize); |
cafe5635 | 1326 | } |
65d45231 | 1327 | EXPORT_SYMBOL(bch_btree_sort_partial); |
cafe5635 | 1328 | |
a85e968e KO |
1329 | void bch_btree_sort_and_fix_extents(struct btree_keys *b, |
1330 | struct btree_iter *iter, | |
67539e85 | 1331 | struct bset_sort_state *state) |
cafe5635 | 1332 | { |
67539e85 | 1333 | __btree_sort(b, iter, 0, b->page_order, true, state); |
cafe5635 KO |
1334 | } |
1335 | ||
89ebb4a2 | 1336 | void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new, |
67539e85 | 1337 | struct bset_sort_state *state) |
cafe5635 KO |
1338 | { |
1339 | uint64_t start_time = local_clock(); | |
cafe5635 | 1340 | struct btree_iter iter; |
1fae7cf0 | 1341 | |
89ebb4a2 | 1342 | bch_btree_iter_init(b, &iter, NULL); |
cafe5635 | 1343 | |
89ebb4a2 | 1344 | btree_mergesort(b, new->set->data, &iter, false, true); |
cafe5635 | 1345 | |
67539e85 | 1346 | bch_time_stats_update(&state->time, start_time); |
cafe5635 | 1347 | |
89ebb4a2 | 1348 | new->set->size = 0; // XXX: why? |
cafe5635 KO |
1349 | } |
1350 | ||
6ded34d1 KO |
1351 | #define SORT_CRIT (4096 / sizeof(uint64_t)) |
1352 | ||
89ebb4a2 | 1353 | void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state) |
cafe5635 | 1354 | { |
6f10f7d1 | 1355 | unsigned int crit = SORT_CRIT; |
6ded34d1 | 1356 | int i; |
cafe5635 | 1357 | |
6ded34d1 | 1358 | /* Don't sort if nothing to do */ |
89ebb4a2 | 1359 | if (!b->nsets) |
6ded34d1 | 1360 | goto out; |
cafe5635 | 1361 | |
89ebb4a2 | 1362 | for (i = b->nsets - 1; i >= 0; --i) { |
67539e85 | 1363 | crit *= state->crit_factor; |
cafe5635 | 1364 | |
89ebb4a2 | 1365 | if (b->set[i].data->keys < crit) { |
67539e85 | 1366 | bch_btree_sort_partial(b, i, state); |
cafe5635 KO |
1367 | return; |
1368 | } | |
1369 | } | |
1370 | ||
6ded34d1 | 1371 | /* Sort if we'd overflow */ |
89ebb4a2 | 1372 | if (b->nsets + 1 == MAX_BSETS) { |
67539e85 | 1373 | bch_btree_sort(b, state); |
6ded34d1 KO |
1374 | return; |
1375 | } | |
1376 | ||
1377 | out: | |
89ebb4a2 | 1378 | bch_bset_build_written_tree(b); |
cafe5635 | 1379 | } |
a85e968e | 1380 | EXPORT_SYMBOL(bch_btree_sort_lazy); |
cafe5635 | 1381 | |
f67342dd | 1382 | void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats) |
cafe5635 | 1383 | { |
6f10f7d1 | 1384 | unsigned int i; |
cafe5635 | 1385 | |
f67342dd KO |
1386 | for (i = 0; i <= b->nsets; i++) { |
1387 | struct bset_tree *t = &b->set[i]; | |
cafe5635 KO |
1388 | size_t bytes = t->data->keys * sizeof(uint64_t); |
1389 | size_t j; | |
1390 | ||
f67342dd | 1391 | if (bset_written(b, t)) { |
cafe5635 KO |
1392 | stats->sets_written++; |
1393 | stats->bytes_written += bytes; | |
1394 | ||
1395 | stats->floats += t->size - 1; | |
1396 | ||
1397 | for (j = 1; j < t->size; j++) | |
1398 | if (t->tree[j].exponent == 127) | |
1399 | stats->failed++; | |
1400 | } else { | |
1401 | stats->sets_unwritten++; | |
1402 | stats->bytes_unwritten += bytes; | |
1403 | } | |
1404 | } | |
cafe5635 | 1405 | } |