blob: d0512e451dda54d363a982186550da8b13e625fa [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
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#include "bcache.h"
9#include "btree.h"
10#include "debug.h"
11
12#include <linux/random.h>
Geert Uytterhoevencd953ed2013-03-27 18:56:28 +010013#include <linux/prefetch.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
15/* Keylists */
16
Kent Overstreetcafe5632013-03-23 16:11:31 -070017int bch_keylist_realloc(struct keylist *l, int nptrs, struct cache_set *c)
18{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070019 size_t oldsize = bch_keylist_nkeys(l);
20 size_t newsize = oldsize + 2 + nptrs;
21 uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
22 uint64_t *new_keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -070023
24 /* The journalling code doesn't handle the case where the keys to insert
25 * is bigger than an empty write: If we just return -ENOMEM here,
26 * bio_insert() and bio_invalidate() will insert the keys created so far
27 * and finish the rest when the keylist is empty.
28 */
29 if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
30 return -ENOMEM;
31
32 newsize = roundup_pow_of_two(newsize);
33
34 if (newsize <= KEYLIST_INLINE ||
35 roundup_pow_of_two(oldsize) == newsize)
36 return 0;
37
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070038 new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -070039
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070040 if (!new_keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -070041 return -ENOMEM;
42
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070043 if (!old_keys)
44 memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -070045
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070046 l->keys_p = new_keys;
47 l->top_p = new_keys + oldsize;
Kent Overstreetcafe5632013-03-23 16:11:31 -070048
49 return 0;
50}
51
52struct bkey *bch_keylist_pop(struct keylist *l)
53{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070054 struct bkey *k = l->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -070055
56 if (k == l->top)
57 return NULL;
58
59 while (bkey_next(k) != l->top)
60 k = bkey_next(k);
61
62 return l->top = k;
63}
64
Kent Overstreet26c949f2013-09-10 18:41:15 -070065void bch_keylist_pop_front(struct keylist *l)
66{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070067 l->top_p -= bkey_u64s(l->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -070068
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070069 memmove(l->keys,
70 bkey_next(l->keys),
71 bch_keylist_bytes(l));
Kent Overstreet26c949f2013-09-10 18:41:15 -070072}
73
Kent Overstreetcafe5632013-03-23 16:11:31 -070074/* Pointer validation */
75
76bool __bch_ptr_invalid(struct cache_set *c, int level, const struct bkey *k)
77{
78 unsigned i;
Kent Overstreet85b14922013-05-14 20:33:16 -070079 char buf[80];
Kent Overstreetcafe5632013-03-23 16:11:31 -070080
81 if (level && (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k)))
82 goto bad;
83
84 if (!level && KEY_SIZE(k) > KEY_OFFSET(k))
85 goto bad;
86
87 if (!KEY_SIZE(k))
88 return true;
89
90 for (i = 0; i < KEY_PTRS(k); i++)
91 if (ptr_available(c, k, i)) {
92 struct cache *ca = PTR_CACHE(c, k, i);
93 size_t bucket = PTR_BUCKET_NR(c, k, i);
94 size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
95
96 if (KEY_SIZE(k) + r > c->sb.bucket_size ||
97 bucket < ca->sb.first_bucket ||
98 bucket >= ca->sb.nbuckets)
99 goto bad;
100 }
101
102 return false;
103bad:
Kent Overstreet85b14922013-05-14 20:33:16 -0700104 bch_bkey_to_text(buf, sizeof(buf), k);
105 cache_bug(c, "spotted bad key %s: %s", buf, bch_ptr_status(c, k));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700106 return true;
107}
108
109bool bch_ptr_bad(struct btree *b, const struct bkey *k)
110{
111 struct bucket *g;
112 unsigned i, stale;
113
114 if (!bkey_cmp(k, &ZERO_KEY) ||
115 !KEY_PTRS(k) ||
116 bch_ptr_invalid(b, k))
117 return true;
118
119 if (KEY_PTRS(k) && PTR_DEV(k, 0) == PTR_CHECK_DEV)
120 return true;
121
122 for (i = 0; i < KEY_PTRS(k); i++)
123 if (ptr_available(b->c, k, i)) {
124 g = PTR_BUCKET(b->c, k, i);
125 stale = ptr_stale(b->c, k, i);
126
127 btree_bug_on(stale > 96, b,
128 "key too stale: %i, need_gc %u",
129 stale, b->c->need_gc);
130
131 btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
132 b, "stale dirty pointer");
133
134 if (stale)
135 return true;
136
137#ifdef CONFIG_BCACHE_EDEBUG
138 if (!mutex_trylock(&b->c->bucket_lock))
139 continue;
140
141 if (b->level) {
142 if (KEY_DIRTY(k) ||
143 g->prio != BTREE_PRIO ||
144 (b->c->gc_mark_valid &&
145 GC_MARK(g) != GC_MARK_METADATA))
146 goto bug;
147
148 } else {
149 if (g->prio == BTREE_PRIO)
150 goto bug;
151
152 if (KEY_DIRTY(k) &&
153 b->c->gc_mark_valid &&
154 GC_MARK(g) != GC_MARK_DIRTY)
155 goto bug;
156 }
157 mutex_unlock(&b->c->bucket_lock);
158#endif
159 }
160
161 return false;
162#ifdef CONFIG_BCACHE_EDEBUG
163bug:
164 mutex_unlock(&b->c->bucket_lock);
Kent Overstreet85b14922013-05-14 20:33:16 -0700165
166 {
167 char buf[80];
168
169 bch_bkey_to_text(buf, sizeof(buf), k);
170 btree_bug(b,
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700171"inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
Kent Overstreet85b14922013-05-14 20:33:16 -0700172 buf, PTR_BUCKET_NR(b->c, k, i), atomic_read(&g->pin),
173 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
174 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700175 return true;
176#endif
177}
178
179/* Key/pointer manipulation */
180
181void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
182 unsigned i)
183{
184 BUG_ON(i > KEY_PTRS(src));
185
186 /* Only copy the header, key, and one pointer. */
187 memcpy(dest, src, 2 * sizeof(uint64_t));
188 dest->ptr[0] = src->ptr[i];
189 SET_KEY_PTRS(dest, 1);
190 /* We didn't copy the checksum so clear that bit. */
191 SET_KEY_CSUM(dest, 0);
192}
193
194bool __bch_cut_front(const struct bkey *where, struct bkey *k)
195{
196 unsigned i, len = 0;
197
198 if (bkey_cmp(where, &START_KEY(k)) <= 0)
199 return false;
200
201 if (bkey_cmp(where, k) < 0)
202 len = KEY_OFFSET(k) - KEY_OFFSET(where);
203 else
204 bkey_copy_key(k, where);
205
206 for (i = 0; i < KEY_PTRS(k); i++)
207 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
208
209 BUG_ON(len > KEY_SIZE(k));
210 SET_KEY_SIZE(k, len);
211 return true;
212}
213
214bool __bch_cut_back(const struct bkey *where, struct bkey *k)
215{
216 unsigned len = 0;
217
218 if (bkey_cmp(where, k) >= 0)
219 return false;
220
221 BUG_ON(KEY_INODE(where) != KEY_INODE(k));
222
223 if (bkey_cmp(where, &START_KEY(k)) > 0)
224 len = KEY_OFFSET(where) - KEY_START(k);
225
226 bkey_copy_key(k, where);
227
228 BUG_ON(len > KEY_SIZE(k));
229 SET_KEY_SIZE(k, len);
230 return true;
231}
232
233static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
234{
235 return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
236 ~((uint64_t)1 << 63);
237}
238
239/* Tries to merge l and r: l should be lower than r
240 * Returns true if we were able to merge. If we did merge, l will be the merged
241 * key, r will be untouched.
242 */
243bool bch_bkey_try_merge(struct btree *b, struct bkey *l, struct bkey *r)
244{
245 unsigned i;
246
247 if (key_merging_disabled(b->c))
248 return false;
249
250 if (KEY_PTRS(l) != KEY_PTRS(r) ||
251 KEY_DIRTY(l) != KEY_DIRTY(r) ||
252 bkey_cmp(l, &START_KEY(r)))
253 return false;
254
255 for (i = 0; i < KEY_PTRS(l); i++)
256 if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
257 PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
258 return false;
259
260 /* Keys with no pointers aren't restricted to one bucket and could
261 * overflow KEY_SIZE
262 */
263 if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
264 SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
265 SET_KEY_SIZE(l, USHRT_MAX);
266
267 bch_cut_front(l, r);
268 return false;
269 }
270
271 if (KEY_CSUM(l)) {
272 if (KEY_CSUM(r))
273 l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
274 else
275 SET_KEY_CSUM(l, 0);
276 }
277
278 SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
279 SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
280
281 return true;
282}
283
284/* Binary tree stuff for auxiliary search trees */
285
286static unsigned inorder_next(unsigned j, unsigned size)
287{
288 if (j * 2 + 1 < size) {
289 j = j * 2 + 1;
290
291 while (j * 2 < size)
292 j *= 2;
293 } else
294 j >>= ffz(j) + 1;
295
296 return j;
297}
298
299static unsigned inorder_prev(unsigned j, unsigned size)
300{
301 if (j * 2 < size) {
302 j = j * 2;
303
304 while (j * 2 + 1 < size)
305 j = j * 2 + 1;
306 } else
307 j >>= ffs(j);
308
309 return j;
310}
311
312/* I have no idea why this code works... and I'm the one who wrote it
313 *
314 * However, I do know what it does:
315 * Given a binary tree constructed in an array (i.e. how you normally implement
316 * a heap), it converts a node in the tree - referenced by array index - to the
317 * index it would have if you did an inorder traversal.
318 *
319 * Also tested for every j, size up to size somewhere around 6 million.
320 *
321 * The binary tree starts at array index 1, not 0
322 * extra is a function of size:
323 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
324 */
325static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
326{
327 unsigned b = fls(j);
328 unsigned shift = fls(size - 1) - b;
329
330 j ^= 1U << (b - 1);
331 j <<= 1;
332 j |= 1;
333 j <<= shift;
334
335 if (j > extra)
336 j -= (j - extra) >> 1;
337
338 return j;
339}
340
341static unsigned to_inorder(unsigned j, struct bset_tree *t)
342{
343 return __to_inorder(j, t->size, t->extra);
344}
345
346static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
347{
348 unsigned shift;
349
350 if (j > extra)
351 j += j - extra;
352
353 shift = ffs(j);
354
355 j >>= shift;
356 j |= roundup_pow_of_two(size) >> shift;
357
358 return j;
359}
360
361static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
362{
363 return __inorder_to_tree(j, t->size, t->extra);
364}
365
366#if 0
367void inorder_test(void)
368{
369 unsigned long done = 0;
370 ktime_t start = ktime_get();
371
372 for (unsigned size = 2;
373 size < 65536000;
374 size++) {
375 unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
376 unsigned i = 1, j = rounddown_pow_of_two(size - 1);
377
378 if (!(size % 4096))
379 printk(KERN_NOTICE "loop %u, %llu per us\n", size,
380 done / ktime_us_delta(ktime_get(), start));
381
382 while (1) {
383 if (__inorder_to_tree(i, size, extra) != j)
384 panic("size %10u j %10u i %10u", size, j, i);
385
386 if (__to_inorder(j, size, extra) != i)
387 panic("size %10u j %10u i %10u", size, j, i);
388
389 if (j == rounddown_pow_of_two(size) - 1)
390 break;
391
392 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
393
394 j = inorder_next(j, size);
395 i++;
396 }
397
398 done += size - 1;
399 }
400}
401#endif
402
403/*
Phil Viana48a73022013-06-03 09:51:42 -0300404 * Cacheline/offset <-> bkey pointer arithmetic:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700405 *
406 * t->tree is a binary search tree in an array; each node corresponds to a key
407 * in one cacheline in t->set (BSET_CACHELINE bytes).
408 *
409 * This means we don't have to store the full index of the key that a node in
410 * the binary tree points to; to_inorder() gives us the cacheline, and then
411 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
412 *
Phil Viana48a73022013-06-03 09:51:42 -0300413 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
Kent Overstreetcafe5632013-03-23 16:11:31 -0700414 * make this work.
415 *
416 * To construct the bfloat for an arbitrary key we need to know what the key
417 * immediately preceding it is: we have to check if the two keys differ in the
418 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
419 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
420 */
421
422static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
423 unsigned offset)
424{
425 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
426}
427
428static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
429{
430 return ((void *) k - (void *) t->data) / BSET_CACHELINE;
431}
432
433static unsigned bkey_to_cacheline_offset(struct bkey *k)
434{
435 return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
436}
437
438static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
439{
440 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
441}
442
443static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
444{
445 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
446}
447
448/*
449 * For the write set - the one we're currently inserting keys into - we don't
450 * maintain a full search tree, we just keep a simple lookup table in t->prev.
451 */
452static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
453{
454 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
455}
456
457static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
458{
459#ifdef CONFIG_X86_64
460 asm("shrd %[shift],%[high],%[low]"
461 : [low] "+Rm" (low)
462 : [high] "R" (high),
463 [shift] "ci" (shift)
464 : "cc");
465#else
466 low >>= shift;
467 low |= (high << 1) << (63U - shift);
468#endif
469 return low;
470}
471
472static inline unsigned bfloat_mantissa(const struct bkey *k,
473 struct bkey_float *f)
474{
475 const uint64_t *p = &k->low - (f->exponent >> 6);
476 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
477}
478
479static void make_bfloat(struct bset_tree *t, unsigned j)
480{
481 struct bkey_float *f = &t->tree[j];
482 struct bkey *m = tree_to_bkey(t, j);
483 struct bkey *p = tree_to_prev_bkey(t, j);
484
485 struct bkey *l = is_power_of_2(j)
486 ? t->data->start
487 : tree_to_prev_bkey(t, j >> ffs(j));
488
489 struct bkey *r = is_power_of_2(j + 1)
490 ? node(t->data, t->data->keys - bkey_u64s(&t->end))
491 : tree_to_bkey(t, j >> (ffz(j) + 1));
492
493 BUG_ON(m < l || m > r);
494 BUG_ON(bkey_next(p) != m);
495
496 if (KEY_INODE(l) != KEY_INODE(r))
497 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
498 else
499 f->exponent = fls64(r->low ^ l->low);
500
501 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
502
503 /*
504 * Setting f->exponent = 127 flags this node as failed, and causes the
505 * lookup code to fall back to comparing against the original key.
506 */
507
508 if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
509 f->mantissa = bfloat_mantissa(m, f) - 1;
510 else
511 f->exponent = 127;
512}
513
514static void bset_alloc_tree(struct btree *b, struct bset_tree *t)
515{
516 if (t != b->sets) {
517 unsigned j = roundup(t[-1].size,
518 64 / sizeof(struct bkey_float));
519
520 t->tree = t[-1].tree + j;
521 t->prev = t[-1].prev + j;
522 }
523
524 while (t < b->sets + MAX_BSETS)
525 t++->size = 0;
526}
527
528static void bset_build_unwritten_tree(struct btree *b)
529{
530 struct bset_tree *t = b->sets + b->nsets;
531
532 bset_alloc_tree(b, t);
533
534 if (t->tree != b->sets->tree + bset_tree_space(b)) {
535 t->prev[0] = bkey_to_cacheline_offset(t->data->start);
536 t->size = 1;
537 }
538}
539
540static void bset_build_written_tree(struct btree *b)
541{
542 struct bset_tree *t = b->sets + b->nsets;
543 struct bkey *k = t->data->start;
544 unsigned j, cacheline = 1;
545
546 bset_alloc_tree(b, t);
547
548 t->size = min_t(unsigned,
549 bkey_to_cacheline(t, end(t->data)),
550 b->sets->tree + bset_tree_space(b) - t->tree);
551
552 if (t->size < 2) {
553 t->size = 0;
554 return;
555 }
556
557 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
558
559 /* First we figure out where the first key in each cacheline is */
560 for (j = inorder_next(0, t->size);
561 j;
562 j = inorder_next(j, t->size)) {
563 while (bkey_to_cacheline(t, k) != cacheline)
564 k = bkey_next(k);
565
566 t->prev[j] = bkey_u64s(k);
567 k = bkey_next(k);
568 cacheline++;
569 t->tree[j].m = bkey_to_cacheline_offset(k);
570 }
571
572 while (bkey_next(k) != end(t->data))
573 k = bkey_next(k);
574
575 t->end = *k;
576
577 /* Then we build the tree */
578 for (j = inorder_next(0, t->size);
579 j;
580 j = inorder_next(j, t->size))
581 make_bfloat(t, j);
582}
583
584void bch_bset_fix_invalidated_key(struct btree *b, struct bkey *k)
585{
586 struct bset_tree *t;
587 unsigned inorder, j = 1;
588
589 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
590 if (k < end(t->data))
591 goto found_set;
592
593 BUG();
594found_set:
595 if (!t->size || !bset_written(b, t))
596 return;
597
598 inorder = bkey_to_cacheline(t, k);
599
600 if (k == t->data->start)
601 goto fix_left;
602
603 if (bkey_next(k) == end(t->data)) {
604 t->end = *k;
605 goto fix_right;
606 }
607
608 j = inorder_to_tree(inorder, t);
609
610 if (j &&
611 j < t->size &&
612 k == tree_to_bkey(t, j))
613fix_left: do {
614 make_bfloat(t, j);
615 j = j * 2;
616 } while (j < t->size);
617
618 j = inorder_to_tree(inorder + 1, t);
619
620 if (j &&
621 j < t->size &&
622 k == tree_to_prev_bkey(t, j))
623fix_right: do {
624 make_bfloat(t, j);
625 j = j * 2 + 1;
626 } while (j < t->size);
627}
628
629void bch_bset_fix_lookup_table(struct btree *b, struct bkey *k)
630{
631 struct bset_tree *t = &b->sets[b->nsets];
632 unsigned shift = bkey_u64s(k);
633 unsigned j = bkey_to_cacheline(t, k);
634
635 /* We're getting called from btree_split() or btree_gc, just bail out */
636 if (!t->size)
637 return;
638
639 /* k is the key we just inserted; we need to find the entry in the
640 * lookup table for the first key that is strictly greater than k:
641 * it's either k's cacheline or the next one
642 */
643 if (j < t->size &&
644 table_to_bkey(t, j) <= k)
645 j++;
646
647 /* Adjust all the lookup table entries, and find a new key for any that
648 * have gotten too big
649 */
650 for (; j < t->size; j++) {
651 t->prev[j] += shift;
652
653 if (t->prev[j] > 7) {
654 k = table_to_bkey(t, j - 1);
655
656 while (k < cacheline_to_bkey(t, j, 0))
657 k = bkey_next(k);
658
659 t->prev[j] = bkey_to_cacheline_offset(k);
660 }
661 }
662
663 if (t->size == b->sets->tree + bset_tree_space(b) - t->tree)
664 return;
665
666 /* Possibly add a new entry to the end of the lookup table */
667
668 for (k = table_to_bkey(t, t->size - 1);
669 k != end(t->data);
670 k = bkey_next(k))
671 if (t->size == bkey_to_cacheline(t, k)) {
672 t->prev[t->size] = bkey_to_cacheline_offset(k);
673 t->size++;
674 }
675}
676
677void bch_bset_init_next(struct btree *b)
678{
679 struct bset *i = write_block(b);
680
681 if (i != b->sets[0].data) {
682 b->sets[++b->nsets].data = i;
683 i->seq = b->sets[0].data->seq;
684 } else
685 get_random_bytes(&i->seq, sizeof(uint64_t));
686
687 i->magic = bset_magic(b->c);
688 i->version = 0;
689 i->keys = 0;
690
691 bset_build_unwritten_tree(b);
692}
693
694struct bset_search_iter {
695 struct bkey *l, *r;
696};
697
698static struct bset_search_iter bset_search_write_set(struct btree *b,
699 struct bset_tree *t,
700 const struct bkey *search)
701{
702 unsigned li = 0, ri = t->size;
703
704 BUG_ON(!b->nsets &&
705 t->size < bkey_to_cacheline(t, end(t->data)));
706
707 while (li + 1 != ri) {
708 unsigned m = (li + ri) >> 1;
709
710 if (bkey_cmp(table_to_bkey(t, m), search) > 0)
711 ri = m;
712 else
713 li = m;
714 }
715
716 return (struct bset_search_iter) {
717 table_to_bkey(t, li),
718 ri < t->size ? table_to_bkey(t, ri) : end(t->data)
719 };
720}
721
722static struct bset_search_iter bset_search_tree(struct btree *b,
723 struct bset_tree *t,
724 const struct bkey *search)
725{
726 struct bkey *l, *r;
727 struct bkey_float *f;
728 unsigned inorder, j, n = 1;
729
730 do {
731 unsigned p = n << 4;
732 p &= ((int) (p - t->size)) >> 31;
733
734 prefetch(&t->tree[p]);
735
736 j = n;
737 f = &t->tree[j];
738
739 /*
740 * n = (f->mantissa > bfloat_mantissa())
741 * ? j * 2
742 * : j * 2 + 1;
743 *
744 * We need to subtract 1 from f->mantissa for the sign bit trick
745 * to work - that's done in make_bfloat()
746 */
747 if (likely(f->exponent != 127))
748 n = j * 2 + (((unsigned)
749 (f->mantissa -
750 bfloat_mantissa(search, f))) >> 31);
751 else
752 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
753 ? j * 2
754 : j * 2 + 1;
755 } while (n < t->size);
756
757 inorder = to_inorder(j, t);
758
759 /*
760 * n would have been the node we recursed to - the low bit tells us if
761 * we recursed left or recursed right.
762 */
763 if (n & 1) {
764 l = cacheline_to_bkey(t, inorder, f->m);
765
766 if (++inorder != t->size) {
767 f = &t->tree[inorder_next(j, t->size)];
768 r = cacheline_to_bkey(t, inorder, f->m);
769 } else
770 r = end(t->data);
771 } else {
772 r = cacheline_to_bkey(t, inorder, f->m);
773
774 if (--inorder) {
775 f = &t->tree[inorder_prev(j, t->size)];
776 l = cacheline_to_bkey(t, inorder, f->m);
777 } else
778 l = t->data->start;
779 }
780
781 return (struct bset_search_iter) {l, r};
782}
783
784struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
785 const struct bkey *search)
786{
787 struct bset_search_iter i;
788
789 /*
790 * First, we search for a cacheline, then lastly we do a linear search
791 * within that cacheline.
792 *
793 * To search for the cacheline, there's three different possibilities:
794 * * The set is too small to have a search tree, so we just do a linear
795 * search over the whole set.
796 * * The set is the one we're currently inserting into; keeping a full
797 * auxiliary search tree up to date would be too expensive, so we
798 * use a much simpler lookup table to do a binary search -
799 * bset_search_write_set().
800 * * Or we use the auxiliary search tree we constructed earlier -
801 * bset_search_tree()
802 */
803
804 if (unlikely(!t->size)) {
805 i.l = t->data->start;
806 i.r = end(t->data);
807 } else if (bset_written(b, t)) {
808 /*
809 * Each node in the auxiliary search tree covers a certain range
810 * of bits, and keys above and below the set it covers might
811 * differ outside those bits - so we have to special case the
812 * start and end - handle that here:
813 */
814
815 if (unlikely(bkey_cmp(search, &t->end) >= 0))
816 return end(t->data);
817
818 if (unlikely(bkey_cmp(search, t->data->start) < 0))
819 return t->data->start;
820
821 i = bset_search_tree(b, t, search);
822 } else
823 i = bset_search_write_set(b, t, search);
824
825#ifdef CONFIG_BCACHE_EDEBUG
826 BUG_ON(bset_written(b, t) &&
827 i.l != t->data->start &&
828 bkey_cmp(tree_to_prev_bkey(t,
829 inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
830 search) > 0);
831
832 BUG_ON(i.r != end(t->data) &&
833 bkey_cmp(i.r, search) <= 0);
834#endif
835
836 while (likely(i.l != i.r) &&
837 bkey_cmp(i.l, search) <= 0)
838 i.l = bkey_next(i.l);
839
840 return i.l;
841}
842
843/* Btree iterator */
844
845static inline bool btree_iter_cmp(struct btree_iter_set l,
846 struct btree_iter_set r)
847{
848 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
849
850 return c ? c > 0 : l.k < r.k;
851}
852
853static inline bool btree_iter_end(struct btree_iter *iter)
854{
855 return !iter->used;
856}
857
858void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
859 struct bkey *end)
860{
861 if (k != end)
862 BUG_ON(!heap_add(iter,
863 ((struct btree_iter_set) { k, end }),
864 btree_iter_cmp));
865}
866
867struct bkey *__bch_btree_iter_init(struct btree *b, struct btree_iter *iter,
868 struct bkey *search, struct bset_tree *start)
869{
870 struct bkey *ret = NULL;
871 iter->size = ARRAY_SIZE(iter->data);
872 iter->used = 0;
873
874 for (; start <= &b->sets[b->nsets]; start++) {
875 ret = bch_bset_search(b, start, search);
876 bch_btree_iter_push(iter, ret, end(start->data));
877 }
878
879 return ret;
880}
881
882struct bkey *bch_btree_iter_next(struct btree_iter *iter)
883{
884 struct btree_iter_set unused;
885 struct bkey *ret = NULL;
886
887 if (!btree_iter_end(iter)) {
888 ret = iter->data->k;
889 iter->data->k = bkey_next(iter->data->k);
890
891 if (iter->data->k > iter->data->end) {
Kent Overstreetcc0f4ea2013-03-27 12:47:45 -0700892 WARN_ONCE(1, "bset was corrupt!\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700893 iter->data->k = iter->data->end;
894 }
895
896 if (iter->data->k == iter->data->end)
897 heap_pop(iter, unused, btree_iter_cmp);
898 else
899 heap_sift(iter, 0, btree_iter_cmp);
900 }
901
902 return ret;
903}
904
905struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
906 struct btree *b, ptr_filter_fn fn)
907{
908 struct bkey *ret;
909
910 do {
911 ret = bch_btree_iter_next(iter);
912 } while (ret && fn(b, ret));
913
914 return ret;
915}
916
917struct bkey *bch_next_recurse_key(struct btree *b, struct bkey *search)
918{
919 struct btree_iter iter;
920
921 bch_btree_iter_init(b, &iter, search);
922 return bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
923}
924
925/* Mergesort */
926
Kent Overstreet84786432013-09-23 23:17:35 -0700927static void sort_key_next(struct btree_iter *iter,
928 struct btree_iter_set *i)
929{
930 i->k = bkey_next(i->k);
931
932 if (i->k == i->end)
933 *i = iter->data[--iter->used];
934}
935
Kent Overstreetcafe5632013-03-23 16:11:31 -0700936static void btree_sort_fixup(struct btree_iter *iter)
937{
938 while (iter->used > 1) {
939 struct btree_iter_set *top = iter->data, *i = top + 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700940
941 if (iter->used > 2 &&
942 btree_iter_cmp(i[0], i[1]))
943 i++;
944
Kent Overstreet84786432013-09-23 23:17:35 -0700945 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700946 break;
947
Kent Overstreet84786432013-09-23 23:17:35 -0700948 if (!KEY_SIZE(i->k)) {
949 sort_key_next(iter, i);
950 heap_sift(iter, i - top, btree_iter_cmp);
951 continue;
952 }
953
954 if (top->k > i->k) {
955 if (bkey_cmp(top->k, i->k) >= 0)
956 sort_key_next(iter, i);
957 else
958 bch_cut_front(top->k, i->k);
959
960 heap_sift(iter, i - top, btree_iter_cmp);
961 } else {
962 /* can't happen because of comparison func */
963 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
964 bch_cut_back(&START_KEY(i->k), top->k);
965 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700966 }
967}
968
969static void btree_mergesort(struct btree *b, struct bset *out,
970 struct btree_iter *iter,
971 bool fixup, bool remove_stale)
972{
973 struct bkey *k, *last = NULL;
974 bool (*bad)(struct btree *, const struct bkey *) = remove_stale
975 ? bch_ptr_bad
976 : bch_ptr_invalid;
977
978 while (!btree_iter_end(iter)) {
979 if (fixup && !b->level)
980 btree_sort_fixup(iter);
981
982 k = bch_btree_iter_next(iter);
983 if (bad(b, k))
984 continue;
985
986 if (!last) {
987 last = out->start;
988 bkey_copy(last, k);
989 } else if (b->level ||
990 !bch_bkey_try_merge(b, last, k)) {
991 last = bkey_next(last);
992 bkey_copy(last, k);
993 }
994 }
995
996 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
997
998 pr_debug("sorted %i keys", out->keys);
999 bch_check_key_order(b, out);
1000}
1001
1002static void __btree_sort(struct btree *b, struct btree_iter *iter,
1003 unsigned start, unsigned order, bool fixup)
1004{
1005 uint64_t start_time;
1006 bool remove_stale = !b->written;
1007 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1008 order);
1009 if (!out) {
1010 mutex_lock(&b->c->sort_lock);
1011 out = b->c->sort;
1012 order = ilog2(bucket_pages(b->c));
1013 }
1014
1015 start_time = local_clock();
1016
1017 btree_mergesort(b, out, iter, fixup, remove_stale);
1018 b->nsets = start;
1019
1020 if (!fixup && !start && b->written)
1021 bch_btree_verify(b, out);
1022
1023 if (!start && order == b->page_order) {
1024 /*
1025 * Our temporary buffer is the same size as the btree node's
1026 * buffer, we can just swap buffers instead of doing a big
1027 * memcpy()
1028 */
1029
1030 out->magic = bset_magic(b->c);
1031 out->seq = b->sets[0].data->seq;
1032 out->version = b->sets[0].data->version;
1033 swap(out, b->sets[0].data);
1034
1035 if (b->c->sort == b->sets[0].data)
1036 b->c->sort = out;
1037 } else {
1038 b->sets[start].data->keys = out->keys;
1039 memcpy(b->sets[start].data->start, out->start,
1040 (void *) end(out) - (void *) out->start);
1041 }
1042
1043 if (out == b->c->sort)
1044 mutex_unlock(&b->c->sort_lock);
1045 else
1046 free_pages((unsigned long) out, order);
1047
1048 if (b->written)
1049 bset_build_written_tree(b);
1050
1051 if (!start) {
1052 spin_lock(&b->c->sort_time_lock);
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001053 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001054 spin_unlock(&b->c->sort_time_lock);
1055 }
1056}
1057
1058void bch_btree_sort_partial(struct btree *b, unsigned start)
1059{
1060 size_t oldsize = 0, order = b->page_order, keys = 0;
1061 struct btree_iter iter;
1062 __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
1063
1064 BUG_ON(b->sets[b->nsets].data == write_block(b) &&
1065 (b->sets[b->nsets].size || b->nsets));
1066
1067 if (b->written)
1068 oldsize = bch_count_data(b);
1069
1070 if (start) {
1071 unsigned i;
1072
1073 for (i = start; i <= b->nsets; i++)
1074 keys += b->sets[i].data->keys;
1075
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001076 order = roundup_pow_of_two(__set_bytes(b->sets->data,
1077 keys)) / PAGE_SIZE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001078 if (order)
1079 order = ilog2(order);
1080 }
1081
1082 __btree_sort(b, &iter, start, order, false);
1083
1084 EBUG_ON(b->written && bch_count_data(b) != oldsize);
1085}
1086
1087void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)
1088{
1089 BUG_ON(!b->written);
1090 __btree_sort(b, iter, 0, b->page_order, true);
1091}
1092
1093void bch_btree_sort_into(struct btree *b, struct btree *new)
1094{
1095 uint64_t start_time = local_clock();
1096
1097 struct btree_iter iter;
1098 bch_btree_iter_init(b, &iter, NULL);
1099
1100 btree_mergesort(b, new->sets->data, &iter, false, true);
1101
1102 spin_lock(&b->c->sort_time_lock);
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001103 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001104 spin_unlock(&b->c->sort_time_lock);
1105
1106 bkey_copy_key(&new->key, &b->key);
1107 new->sets->size = 0;
1108}
1109
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001110#define SORT_CRIT (4096 / sizeof(uint64_t))
1111
Kent Overstreetcafe5632013-03-23 16:11:31 -07001112void bch_btree_sort_lazy(struct btree *b)
1113{
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001114 unsigned crit = SORT_CRIT;
1115 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001116
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001117 /* Don't sort if nothing to do */
1118 if (!b->nsets)
1119 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001120
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001121 /* If not a leaf node, always sort */
1122 if (b->level) {
1123 bch_btree_sort(b);
1124 return;
1125 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001126
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001127 for (i = b->nsets - 1; i >= 0; --i) {
1128 crit *= b->c->sort_crit_factor;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001129
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001130 if (b->sets[i].data->keys < crit) {
1131 bch_btree_sort_partial(b, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001132 return;
1133 }
1134 }
1135
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001136 /* Sort if we'd overflow */
1137 if (b->nsets + 1 == MAX_BSETS) {
1138 bch_btree_sort(b);
1139 return;
1140 }
1141
1142out:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001143 bset_build_written_tree(b);
1144}
1145
1146/* Sysfs stuff */
1147
1148struct bset_stats {
1149 size_t nodes;
1150 size_t sets_written, sets_unwritten;
1151 size_t bytes_written, bytes_unwritten;
1152 size_t floats, failed;
1153};
1154
1155static int bch_btree_bset_stats(struct btree *b, struct btree_op *op,
1156 struct bset_stats *stats)
1157{
1158 struct bkey *k;
1159 unsigned i;
1160
1161 stats->nodes++;
1162
1163 for (i = 0; i <= b->nsets; i++) {
1164 struct bset_tree *t = &b->sets[i];
1165 size_t bytes = t->data->keys * sizeof(uint64_t);
1166 size_t j;
1167
1168 if (bset_written(b, t)) {
1169 stats->sets_written++;
1170 stats->bytes_written += bytes;
1171
1172 stats->floats += t->size - 1;
1173
1174 for (j = 1; j < t->size; j++)
1175 if (t->tree[j].exponent == 127)
1176 stats->failed++;
1177 } else {
1178 stats->sets_unwritten++;
1179 stats->bytes_unwritten += bytes;
1180 }
1181 }
1182
1183 if (b->level) {
1184 struct btree_iter iter;
1185
1186 for_each_key_filter(b, k, &iter, bch_ptr_bad) {
1187 int ret = btree(bset_stats, k, b, op, stats);
1188 if (ret)
1189 return ret;
1190 }
1191 }
1192
1193 return 0;
1194}
1195
1196int bch_bset_print_stats(struct cache_set *c, char *buf)
1197{
1198 struct btree_op op;
1199 struct bset_stats t;
1200 int ret;
1201
1202 bch_btree_op_init_stack(&op);
1203 memset(&t, 0, sizeof(struct bset_stats));
1204
1205 ret = btree_root(bset_stats, c, &op, &t);
1206 if (ret)
1207 return ret;
1208
1209 return snprintf(buf, PAGE_SIZE,
1210 "btree nodes: %zu\n"
1211 "written sets: %zu\n"
1212 "unwritten sets: %zu\n"
1213 "written key bytes: %zu\n"
1214 "unwritten key bytes: %zu\n"
1215 "floats: %zu\n"
1216 "failed: %zu\n",
1217 t.nodes,
1218 t.sets_written, t.sets_unwritten,
1219 t.bytes_written, t.bytes_unwritten,
1220 t.floats, t.failed);
1221}