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