blob: f7b5525ddafa08bd42e4906f7a91877bd4e8cae1 [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
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700845/*
846 * Returns true if l > r - unless l == r, in which case returns true if l is
847 * older than r.
848 *
849 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
850 * equal in different sets, we have to process them newest to oldest.
851 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700852static inline bool btree_iter_cmp(struct btree_iter_set l,
853 struct btree_iter_set r)
854{
855 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
856
857 return c ? c > 0 : l.k < r.k;
858}
859
860static inline bool btree_iter_end(struct btree_iter *iter)
861{
862 return !iter->used;
863}
864
865void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
866 struct bkey *end)
867{
868 if (k != end)
869 BUG_ON(!heap_add(iter,
870 ((struct btree_iter_set) { k, end }),
871 btree_iter_cmp));
872}
873
874struct bkey *__bch_btree_iter_init(struct btree *b, struct btree_iter *iter,
875 struct bkey *search, struct bset_tree *start)
876{
877 struct bkey *ret = NULL;
878 iter->size = ARRAY_SIZE(iter->data);
879 iter->used = 0;
880
881 for (; start <= &b->sets[b->nsets]; start++) {
882 ret = bch_bset_search(b, start, search);
883 bch_btree_iter_push(iter, ret, end(start->data));
884 }
885
886 return ret;
887}
888
889struct bkey *bch_btree_iter_next(struct btree_iter *iter)
890{
891 struct btree_iter_set unused;
892 struct bkey *ret = NULL;
893
894 if (!btree_iter_end(iter)) {
895 ret = iter->data->k;
896 iter->data->k = bkey_next(iter->data->k);
897
898 if (iter->data->k > iter->data->end) {
Kent Overstreetcc0f4ea2013-03-27 12:47:45 -0700899 WARN_ONCE(1, "bset was corrupt!\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700900 iter->data->k = iter->data->end;
901 }
902
903 if (iter->data->k == iter->data->end)
904 heap_pop(iter, unused, btree_iter_cmp);
905 else
906 heap_sift(iter, 0, btree_iter_cmp);
907 }
908
909 return ret;
910}
911
912struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
913 struct btree *b, ptr_filter_fn fn)
914{
915 struct bkey *ret;
916
917 do {
918 ret = bch_btree_iter_next(iter);
919 } while (ret && fn(b, ret));
920
921 return ret;
922}
923
924struct bkey *bch_next_recurse_key(struct btree *b, struct bkey *search)
925{
926 struct btree_iter iter;
927
928 bch_btree_iter_init(b, &iter, search);
929 return bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
930}
931
932/* Mergesort */
933
Kent Overstreet84786432013-09-23 23:17:35 -0700934static void sort_key_next(struct btree_iter *iter,
935 struct btree_iter_set *i)
936{
937 i->k = bkey_next(i->k);
938
939 if (i->k == i->end)
940 *i = iter->data[--iter->used];
941}
942
Kent Overstreetcafe5632013-03-23 16:11:31 -0700943static void btree_sort_fixup(struct btree_iter *iter)
944{
945 while (iter->used > 1) {
946 struct btree_iter_set *top = iter->data, *i = top + 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700947
948 if (iter->used > 2 &&
949 btree_iter_cmp(i[0], i[1]))
950 i++;
951
Kent Overstreet84786432013-09-23 23:17:35 -0700952 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700953 break;
954
Kent Overstreet84786432013-09-23 23:17:35 -0700955 if (!KEY_SIZE(i->k)) {
956 sort_key_next(iter, i);
957 heap_sift(iter, i - top, btree_iter_cmp);
958 continue;
959 }
960
961 if (top->k > i->k) {
962 if (bkey_cmp(top->k, i->k) >= 0)
963 sort_key_next(iter, i);
964 else
965 bch_cut_front(top->k, i->k);
966
967 heap_sift(iter, i - top, btree_iter_cmp);
968 } else {
969 /* can't happen because of comparison func */
970 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
971 bch_cut_back(&START_KEY(i->k), top->k);
972 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700973 }
974}
975
976static void btree_mergesort(struct btree *b, struct bset *out,
977 struct btree_iter *iter,
978 bool fixup, bool remove_stale)
979{
980 struct bkey *k, *last = NULL;
981 bool (*bad)(struct btree *, const struct bkey *) = remove_stale
982 ? bch_ptr_bad
983 : bch_ptr_invalid;
984
985 while (!btree_iter_end(iter)) {
986 if (fixup && !b->level)
987 btree_sort_fixup(iter);
988
989 k = bch_btree_iter_next(iter);
990 if (bad(b, k))
991 continue;
992
993 if (!last) {
994 last = out->start;
995 bkey_copy(last, k);
996 } else if (b->level ||
997 !bch_bkey_try_merge(b, last, k)) {
998 last = bkey_next(last);
999 bkey_copy(last, k);
1000 }
1001 }
1002
1003 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1004
1005 pr_debug("sorted %i keys", out->keys);
1006 bch_check_key_order(b, out);
1007}
1008
1009static void __btree_sort(struct btree *b, struct btree_iter *iter,
1010 unsigned start, unsigned order, bool fixup)
1011{
1012 uint64_t start_time;
1013 bool remove_stale = !b->written;
1014 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1015 order);
1016 if (!out) {
1017 mutex_lock(&b->c->sort_lock);
1018 out = b->c->sort;
1019 order = ilog2(bucket_pages(b->c));
1020 }
1021
1022 start_time = local_clock();
1023
1024 btree_mergesort(b, out, iter, fixup, remove_stale);
1025 b->nsets = start;
1026
1027 if (!fixup && !start && b->written)
1028 bch_btree_verify(b, out);
1029
1030 if (!start && order == b->page_order) {
1031 /*
1032 * Our temporary buffer is the same size as the btree node's
1033 * buffer, we can just swap buffers instead of doing a big
1034 * memcpy()
1035 */
1036
1037 out->magic = bset_magic(b->c);
1038 out->seq = b->sets[0].data->seq;
1039 out->version = b->sets[0].data->version;
1040 swap(out, b->sets[0].data);
1041
1042 if (b->c->sort == b->sets[0].data)
1043 b->c->sort = out;
1044 } else {
1045 b->sets[start].data->keys = out->keys;
1046 memcpy(b->sets[start].data->start, out->start,
1047 (void *) end(out) - (void *) out->start);
1048 }
1049
1050 if (out == b->c->sort)
1051 mutex_unlock(&b->c->sort_lock);
1052 else
1053 free_pages((unsigned long) out, order);
1054
1055 if (b->written)
1056 bset_build_written_tree(b);
1057
1058 if (!start) {
1059 spin_lock(&b->c->sort_time_lock);
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001060 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001061 spin_unlock(&b->c->sort_time_lock);
1062 }
1063}
1064
1065void bch_btree_sort_partial(struct btree *b, unsigned start)
1066{
1067 size_t oldsize = 0, order = b->page_order, keys = 0;
1068 struct btree_iter iter;
1069 __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
1070
1071 BUG_ON(b->sets[b->nsets].data == write_block(b) &&
1072 (b->sets[b->nsets].size || b->nsets));
1073
1074 if (b->written)
1075 oldsize = bch_count_data(b);
1076
1077 if (start) {
1078 unsigned i;
1079
1080 for (i = start; i <= b->nsets; i++)
1081 keys += b->sets[i].data->keys;
1082
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001083 order = roundup_pow_of_two(__set_bytes(b->sets->data,
1084 keys)) / PAGE_SIZE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001085 if (order)
1086 order = ilog2(order);
1087 }
1088
1089 __btree_sort(b, &iter, start, order, false);
1090
1091 EBUG_ON(b->written && bch_count_data(b) != oldsize);
1092}
1093
1094void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)
1095{
1096 BUG_ON(!b->written);
1097 __btree_sort(b, iter, 0, b->page_order, true);
1098}
1099
1100void bch_btree_sort_into(struct btree *b, struct btree *new)
1101{
1102 uint64_t start_time = local_clock();
1103
1104 struct btree_iter iter;
1105 bch_btree_iter_init(b, &iter, NULL);
1106
1107 btree_mergesort(b, new->sets->data, &iter, false, true);
1108
1109 spin_lock(&b->c->sort_time_lock);
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001110 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001111 spin_unlock(&b->c->sort_time_lock);
1112
1113 bkey_copy_key(&new->key, &b->key);
1114 new->sets->size = 0;
1115}
1116
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001117#define SORT_CRIT (4096 / sizeof(uint64_t))
1118
Kent Overstreetcafe5632013-03-23 16:11:31 -07001119void bch_btree_sort_lazy(struct btree *b)
1120{
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001121 unsigned crit = SORT_CRIT;
1122 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001123
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001124 /* Don't sort if nothing to do */
1125 if (!b->nsets)
1126 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001127
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001128 /* If not a leaf node, always sort */
1129 if (b->level) {
1130 bch_btree_sort(b);
1131 return;
1132 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001133
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001134 for (i = b->nsets - 1; i >= 0; --i) {
1135 crit *= b->c->sort_crit_factor;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001136
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001137 if (b->sets[i].data->keys < crit) {
1138 bch_btree_sort_partial(b, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001139 return;
1140 }
1141 }
1142
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001143 /* Sort if we'd overflow */
1144 if (b->nsets + 1 == MAX_BSETS) {
1145 bch_btree_sort(b);
1146 return;
1147 }
1148
1149out:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001150 bset_build_written_tree(b);
1151}
1152
1153/* Sysfs stuff */
1154
1155struct bset_stats {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001156 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001157 size_t nodes;
1158 size_t sets_written, sets_unwritten;
1159 size_t bytes_written, bytes_unwritten;
1160 size_t floats, failed;
1161};
1162
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001163static int btree_bset_stats(struct btree_op *op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001164{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001165 struct bset_stats *stats = container_of(op, struct bset_stats, op);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001166 unsigned i;
1167
1168 stats->nodes++;
1169
1170 for (i = 0; i <= b->nsets; i++) {
1171 struct bset_tree *t = &b->sets[i];
1172 size_t bytes = t->data->keys * sizeof(uint64_t);
1173 size_t j;
1174
1175 if (bset_written(b, t)) {
1176 stats->sets_written++;
1177 stats->bytes_written += bytes;
1178
1179 stats->floats += t->size - 1;
1180
1181 for (j = 1; j < t->size; j++)
1182 if (t->tree[j].exponent == 127)
1183 stats->failed++;
1184 } else {
1185 stats->sets_unwritten++;
1186 stats->bytes_unwritten += bytes;
1187 }
1188 }
1189
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001190 return MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001191}
1192
1193int bch_bset_print_stats(struct cache_set *c, char *buf)
1194{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001195 struct bset_stats t;
1196 int ret;
1197
Kent Overstreetcafe5632013-03-23 16:11:31 -07001198 memset(&t, 0, sizeof(struct bset_stats));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001199 bch_btree_op_init(&t.op, -1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001200
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001201 ret = bch_btree_map_nodes(&t.op, c, &ZERO_KEY, btree_bset_stats);
1202 if (ret < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001203 return ret;
1204
1205 return snprintf(buf, PAGE_SIZE,
1206 "btree nodes: %zu\n"
1207 "written sets: %zu\n"
1208 "unwritten sets: %zu\n"
1209 "written key bytes: %zu\n"
1210 "unwritten key bytes: %zu\n"
1211 "floats: %zu\n"
1212 "failed: %zu\n",
1213 t.nodes,
1214 t.sets_written, t.sets_unwritten,
1215 t.bytes_written, t.bytes_unwritten,
1216 t.floats, t.failed);
1217}