blob: f32216c759480a670e675dcf877d27627f8d39c4 [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
Kent Overstreete58ff152013-07-24 18:14:44 -0700119 for (i = 0; i < KEY_PTRS(k); i++) {
120 if (!ptr_available(b->c, k, i))
121 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700122
Kent Overstreete58ff152013-07-24 18:14:44 -0700123 g = PTR_BUCKET(b->c, k, i);
124 stale = ptr_stale(b->c, k, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125
Kent Overstreete58ff152013-07-24 18:14:44 -0700126 btree_bug_on(stale > 96, b,
127 "key too stale: %i, need_gc %u",
128 stale, b->c->need_gc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700129
Kent Overstreete58ff152013-07-24 18:14:44 -0700130 btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
131 b, "stale dirty pointer");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700132
Kent Overstreete58ff152013-07-24 18:14:44 -0700133 if (stale)
134 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135
136#ifdef CONFIG_BCACHE_EDEBUG
Kent Overstreete58ff152013-07-24 18:14:44 -0700137 if (!mutex_trylock(&b->c->bucket_lock))
138 continue;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700139
Kent Overstreete58ff152013-07-24 18:14:44 -0700140 if (b->level) {
141 if (KEY_DIRTY(k) ||
142 g->prio != BTREE_PRIO ||
143 (b->c->gc_mark_valid &&
144 GC_MARK(g) != GC_MARK_METADATA))
145 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700146
Kent Overstreete58ff152013-07-24 18:14:44 -0700147 } else {
148 if (g->prio == BTREE_PRIO)
149 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700150
Kent Overstreete58ff152013-07-24 18:14:44 -0700151 if (KEY_DIRTY(k) &&
152 b->c->gc_mark_valid &&
153 GC_MARK(g) != GC_MARK_DIRTY)
154 goto bug;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700155 }
Kent Overstreete58ff152013-07-24 18:14:44 -0700156 mutex_unlock(&b->c->bucket_lock);
157#endif
158 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700159
160 return false;
161#ifdef CONFIG_BCACHE_EDEBUG
162bug:
163 mutex_unlock(&b->c->bucket_lock);
Kent Overstreet85b14922013-05-14 20:33:16 -0700164
165 {
166 char buf[80];
167
168 bch_bkey_to_text(buf, sizeof(buf), k);
169 btree_bug(b,
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700170"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 -0700171 buf, PTR_BUCKET_NR(b->c, k, i), atomic_read(&g->pin),
172 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
173 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174 return true;
175#endif
176}
177
178/* Key/pointer manipulation */
179
180void 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
193bool __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
213bool __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
232static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
233{
234 return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
235 ~((uint64_t)1 << 63);
236}
237
238/* Tries to merge l and r: l should be lower than r
239 * Returns true if we were able to merge. If we did merge, l will be the merged
240 * key, r will be untouched.
241 */
242bool bch_bkey_try_merge(struct btree *b, struct bkey *l, struct bkey *r)
243{
244 unsigned i;
245
246 if (key_merging_disabled(b->c))
247 return false;
248
249 if (KEY_PTRS(l) != KEY_PTRS(r) ||
250 KEY_DIRTY(l) != KEY_DIRTY(r) ||
251 bkey_cmp(l, &START_KEY(r)))
252 return false;
253
254 for (i = 0; i < KEY_PTRS(l); i++)
255 if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
256 PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
257 return false;
258
259 /* Keys with no pointers aren't restricted to one bucket and could
260 * overflow KEY_SIZE
261 */
262 if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
263 SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
264 SET_KEY_SIZE(l, USHRT_MAX);
265
266 bch_cut_front(l, r);
267 return false;
268 }
269
270 if (KEY_CSUM(l)) {
271 if (KEY_CSUM(r))
272 l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
273 else
274 SET_KEY_CSUM(l, 0);
275 }
276
277 SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
278 SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
279
280 return true;
281}
282
283/* Binary tree stuff for auxiliary search trees */
284
285static unsigned inorder_next(unsigned j, unsigned size)
286{
287 if (j * 2 + 1 < size) {
288 j = j * 2 + 1;
289
290 while (j * 2 < size)
291 j *= 2;
292 } else
293 j >>= ffz(j) + 1;
294
295 return j;
296}
297
298static unsigned inorder_prev(unsigned j, unsigned size)
299{
300 if (j * 2 < size) {
301 j = j * 2;
302
303 while (j * 2 + 1 < size)
304 j = j * 2 + 1;
305 } else
306 j >>= ffs(j);
307
308 return j;
309}
310
311/* I have no idea why this code works... and I'm the one who wrote it
312 *
313 * However, I do know what it does:
314 * Given a binary tree constructed in an array (i.e. how you normally implement
315 * a heap), it converts a node in the tree - referenced by array index - to the
316 * index it would have if you did an inorder traversal.
317 *
318 * Also tested for every j, size up to size somewhere around 6 million.
319 *
320 * The binary tree starts at array index 1, not 0
321 * extra is a function of size:
322 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
323 */
324static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
325{
326 unsigned b = fls(j);
327 unsigned shift = fls(size - 1) - b;
328
329 j ^= 1U << (b - 1);
330 j <<= 1;
331 j |= 1;
332 j <<= shift;
333
334 if (j > extra)
335 j -= (j - extra) >> 1;
336
337 return j;
338}
339
340static unsigned to_inorder(unsigned j, struct bset_tree *t)
341{
342 return __to_inorder(j, t->size, t->extra);
343}
344
345static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
346{
347 unsigned shift;
348
349 if (j > extra)
350 j += j - extra;
351
352 shift = ffs(j);
353
354 j >>= shift;
355 j |= roundup_pow_of_two(size) >> shift;
356
357 return j;
358}
359
360static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
361{
362 return __inorder_to_tree(j, t->size, t->extra);
363}
364
365#if 0
366void inorder_test(void)
367{
368 unsigned long done = 0;
369 ktime_t start = ktime_get();
370
371 for (unsigned size = 2;
372 size < 65536000;
373 size++) {
374 unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
375 unsigned i = 1, j = rounddown_pow_of_two(size - 1);
376
377 if (!(size % 4096))
378 printk(KERN_NOTICE "loop %u, %llu per us\n", size,
379 done / ktime_us_delta(ktime_get(), start));
380
381 while (1) {
382 if (__inorder_to_tree(i, size, extra) != j)
383 panic("size %10u j %10u i %10u", size, j, i);
384
385 if (__to_inorder(j, size, extra) != i)
386 panic("size %10u j %10u i %10u", size, j, i);
387
388 if (j == rounddown_pow_of_two(size) - 1)
389 break;
390
391 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
392
393 j = inorder_next(j, size);
394 i++;
395 }
396
397 done += size - 1;
398 }
399}
400#endif
401
402/*
Phil Viana48a73022013-06-03 09:51:42 -0300403 * Cacheline/offset <-> bkey pointer arithmetic:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700404 *
405 * t->tree is a binary search tree in an array; each node corresponds to a key
406 * in one cacheline in t->set (BSET_CACHELINE bytes).
407 *
408 * This means we don't have to store the full index of the key that a node in
409 * the binary tree points to; to_inorder() gives us the cacheline, and then
410 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
411 *
Phil Viana48a73022013-06-03 09:51:42 -0300412 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
Kent Overstreetcafe5632013-03-23 16:11:31 -0700413 * make this work.
414 *
415 * To construct the bfloat for an arbitrary key we need to know what the key
416 * immediately preceding it is: we have to check if the two keys differ in the
417 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
418 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
419 */
420
421static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
422 unsigned offset)
423{
424 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
425}
426
427static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
428{
429 return ((void *) k - (void *) t->data) / BSET_CACHELINE;
430}
431
432static unsigned bkey_to_cacheline_offset(struct bkey *k)
433{
434 return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
435}
436
437static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
438{
439 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
440}
441
442static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
443{
444 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
445}
446
447/*
448 * For the write set - the one we're currently inserting keys into - we don't
449 * maintain a full search tree, we just keep a simple lookup table in t->prev.
450 */
451static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
452{
453 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
454}
455
456static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
457{
458#ifdef CONFIG_X86_64
459 asm("shrd %[shift],%[high],%[low]"
460 : [low] "+Rm" (low)
461 : [high] "R" (high),
462 [shift] "ci" (shift)
463 : "cc");
464#else
465 low >>= shift;
466 low |= (high << 1) << (63U - shift);
467#endif
468 return low;
469}
470
471static inline unsigned bfloat_mantissa(const struct bkey *k,
472 struct bkey_float *f)
473{
474 const uint64_t *p = &k->low - (f->exponent >> 6);
475 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
476}
477
478static void make_bfloat(struct bset_tree *t, unsigned j)
479{
480 struct bkey_float *f = &t->tree[j];
481 struct bkey *m = tree_to_bkey(t, j);
482 struct bkey *p = tree_to_prev_bkey(t, j);
483
484 struct bkey *l = is_power_of_2(j)
485 ? t->data->start
486 : tree_to_prev_bkey(t, j >> ffs(j));
487
488 struct bkey *r = is_power_of_2(j + 1)
489 ? node(t->data, t->data->keys - bkey_u64s(&t->end))
490 : tree_to_bkey(t, j >> (ffz(j) + 1));
491
492 BUG_ON(m < l || m > r);
493 BUG_ON(bkey_next(p) != m);
494
495 if (KEY_INODE(l) != KEY_INODE(r))
496 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
497 else
498 f->exponent = fls64(r->low ^ l->low);
499
500 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
501
502 /*
503 * Setting f->exponent = 127 flags this node as failed, and causes the
504 * lookup code to fall back to comparing against the original key.
505 */
506
507 if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
508 f->mantissa = bfloat_mantissa(m, f) - 1;
509 else
510 f->exponent = 127;
511}
512
513static void bset_alloc_tree(struct btree *b, struct bset_tree *t)
514{
515 if (t != b->sets) {
516 unsigned j = roundup(t[-1].size,
517 64 / sizeof(struct bkey_float));
518
519 t->tree = t[-1].tree + j;
520 t->prev = t[-1].prev + j;
521 }
522
523 while (t < b->sets + MAX_BSETS)
524 t++->size = 0;
525}
526
527static void bset_build_unwritten_tree(struct btree *b)
528{
529 struct bset_tree *t = b->sets + b->nsets;
530
531 bset_alloc_tree(b, t);
532
533 if (t->tree != b->sets->tree + bset_tree_space(b)) {
534 t->prev[0] = bkey_to_cacheline_offset(t->data->start);
535 t->size = 1;
536 }
537}
538
539static void bset_build_written_tree(struct btree *b)
540{
541 struct bset_tree *t = b->sets + b->nsets;
542 struct bkey *k = t->data->start;
543 unsigned j, cacheline = 1;
544
545 bset_alloc_tree(b, t);
546
547 t->size = min_t(unsigned,
548 bkey_to_cacheline(t, end(t->data)),
549 b->sets->tree + bset_tree_space(b) - t->tree);
550
551 if (t->size < 2) {
552 t->size = 0;
553 return;
554 }
555
556 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
557
558 /* First we figure out where the first key in each cacheline is */
559 for (j = inorder_next(0, t->size);
560 j;
561 j = inorder_next(j, t->size)) {
562 while (bkey_to_cacheline(t, k) != cacheline)
563 k = bkey_next(k);
564
565 t->prev[j] = bkey_u64s(k);
566 k = bkey_next(k);
567 cacheline++;
568 t->tree[j].m = bkey_to_cacheline_offset(k);
569 }
570
571 while (bkey_next(k) != end(t->data))
572 k = bkey_next(k);
573
574 t->end = *k;
575
576 /* Then we build the tree */
577 for (j = inorder_next(0, t->size);
578 j;
579 j = inorder_next(j, t->size))
580 make_bfloat(t, j);
581}
582
583void bch_bset_fix_invalidated_key(struct btree *b, struct bkey *k)
584{
585 struct bset_tree *t;
586 unsigned inorder, j = 1;
587
588 for (t = b->sets; t <= &b->sets[b->nsets]; t++)
589 if (k < end(t->data))
590 goto found_set;
591
592 BUG();
593found_set:
594 if (!t->size || !bset_written(b, t))
595 return;
596
597 inorder = bkey_to_cacheline(t, k);
598
599 if (k == t->data->start)
600 goto fix_left;
601
602 if (bkey_next(k) == end(t->data)) {
603 t->end = *k;
604 goto fix_right;
605 }
606
607 j = inorder_to_tree(inorder, t);
608
609 if (j &&
610 j < t->size &&
611 k == tree_to_bkey(t, j))
612fix_left: do {
613 make_bfloat(t, j);
614 j = j * 2;
615 } while (j < t->size);
616
617 j = inorder_to_tree(inorder + 1, t);
618
619 if (j &&
620 j < t->size &&
621 k == tree_to_prev_bkey(t, j))
622fix_right: do {
623 make_bfloat(t, j);
624 j = j * 2 + 1;
625 } while (j < t->size);
626}
627
628void bch_bset_fix_lookup_table(struct btree *b, struct bkey *k)
629{
630 struct bset_tree *t = &b->sets[b->nsets];
631 unsigned shift = bkey_u64s(k);
632 unsigned j = bkey_to_cacheline(t, k);
633
634 /* We're getting called from btree_split() or btree_gc, just bail out */
635 if (!t->size)
636 return;
637
638 /* k is the key we just inserted; we need to find the entry in the
639 * lookup table for the first key that is strictly greater than k:
640 * it's either k's cacheline or the next one
641 */
642 if (j < t->size &&
643 table_to_bkey(t, j) <= k)
644 j++;
645
646 /* Adjust all the lookup table entries, and find a new key for any that
647 * have gotten too big
648 */
649 for (; j < t->size; j++) {
650 t->prev[j] += shift;
651
652 if (t->prev[j] > 7) {
653 k = table_to_bkey(t, j - 1);
654
655 while (k < cacheline_to_bkey(t, j, 0))
656 k = bkey_next(k);
657
658 t->prev[j] = bkey_to_cacheline_offset(k);
659 }
660 }
661
662 if (t->size == b->sets->tree + bset_tree_space(b) - t->tree)
663 return;
664
665 /* Possibly add a new entry to the end of the lookup table */
666
667 for (k = table_to_bkey(t, t->size - 1);
668 k != end(t->data);
669 k = bkey_next(k))
670 if (t->size == bkey_to_cacheline(t, k)) {
671 t->prev[t->size] = bkey_to_cacheline_offset(k);
672 t->size++;
673 }
674}
675
676void bch_bset_init_next(struct btree *b)
677{
678 struct bset *i = write_block(b);
679
680 if (i != b->sets[0].data) {
681 b->sets[++b->nsets].data = i;
682 i->seq = b->sets[0].data->seq;
683 } else
684 get_random_bytes(&i->seq, sizeof(uint64_t));
685
Kent Overstreet81ab4192013-10-31 15:46:42 -0700686 i->magic = bset_magic(&b->c->sb);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700687 i->version = 0;
688 i->keys = 0;
689
690 bset_build_unwritten_tree(b);
691}
692
693struct bset_search_iter {
694 struct bkey *l, *r;
695};
696
697static struct bset_search_iter bset_search_write_set(struct btree *b,
698 struct bset_tree *t,
699 const struct bkey *search)
700{
701 unsigned li = 0, ri = t->size;
702
703 BUG_ON(!b->nsets &&
704 t->size < bkey_to_cacheline(t, end(t->data)));
705
706 while (li + 1 != ri) {
707 unsigned m = (li + ri) >> 1;
708
709 if (bkey_cmp(table_to_bkey(t, m), search) > 0)
710 ri = m;
711 else
712 li = m;
713 }
714
715 return (struct bset_search_iter) {
716 table_to_bkey(t, li),
717 ri < t->size ? table_to_bkey(t, ri) : end(t->data)
718 };
719}
720
721static struct bset_search_iter bset_search_tree(struct btree *b,
722 struct bset_tree *t,
723 const struct bkey *search)
724{
725 struct bkey *l, *r;
726 struct bkey_float *f;
727 unsigned inorder, j, n = 1;
728
729 do {
730 unsigned p = n << 4;
731 p &= ((int) (p - t->size)) >> 31;
732
733 prefetch(&t->tree[p]);
734
735 j = n;
736 f = &t->tree[j];
737
738 /*
739 * n = (f->mantissa > bfloat_mantissa())
740 * ? j * 2
741 * : j * 2 + 1;
742 *
743 * We need to subtract 1 from f->mantissa for the sign bit trick
744 * to work - that's done in make_bfloat()
745 */
746 if (likely(f->exponent != 127))
747 n = j * 2 + (((unsigned)
748 (f->mantissa -
749 bfloat_mantissa(search, f))) >> 31);
750 else
751 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
752 ? j * 2
753 : j * 2 + 1;
754 } while (n < t->size);
755
756 inorder = to_inorder(j, t);
757
758 /*
759 * n would have been the node we recursed to - the low bit tells us if
760 * we recursed left or recursed right.
761 */
762 if (n & 1) {
763 l = cacheline_to_bkey(t, inorder, f->m);
764
765 if (++inorder != t->size) {
766 f = &t->tree[inorder_next(j, t->size)];
767 r = cacheline_to_bkey(t, inorder, f->m);
768 } else
769 r = end(t->data);
770 } else {
771 r = cacheline_to_bkey(t, inorder, f->m);
772
773 if (--inorder) {
774 f = &t->tree[inorder_prev(j, t->size)];
775 l = cacheline_to_bkey(t, inorder, f->m);
776 } else
777 l = t->data->start;
778 }
779
780 return (struct bset_search_iter) {l, r};
781}
782
783struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
784 const struct bkey *search)
785{
786 struct bset_search_iter i;
787
788 /*
789 * First, we search for a cacheline, then lastly we do a linear search
790 * within that cacheline.
791 *
792 * To search for the cacheline, there's three different possibilities:
793 * * The set is too small to have a search tree, so we just do a linear
794 * search over the whole set.
795 * * The set is the one we're currently inserting into; keeping a full
796 * auxiliary search tree up to date would be too expensive, so we
797 * use a much simpler lookup table to do a binary search -
798 * bset_search_write_set().
799 * * Or we use the auxiliary search tree we constructed earlier -
800 * bset_search_tree()
801 */
802
803 if (unlikely(!t->size)) {
804 i.l = t->data->start;
805 i.r = end(t->data);
806 } else if (bset_written(b, t)) {
807 /*
808 * Each node in the auxiliary search tree covers a certain range
809 * of bits, and keys above and below the set it covers might
810 * differ outside those bits - so we have to special case the
811 * start and end - handle that here:
812 */
813
814 if (unlikely(bkey_cmp(search, &t->end) >= 0))
815 return end(t->data);
816
817 if (unlikely(bkey_cmp(search, t->data->start) < 0))
818 return t->data->start;
819
820 i = bset_search_tree(b, t, search);
821 } else
822 i = bset_search_write_set(b, t, search);
823
824#ifdef CONFIG_BCACHE_EDEBUG
825 BUG_ON(bset_written(b, t) &&
826 i.l != t->data->start &&
827 bkey_cmp(tree_to_prev_bkey(t,
828 inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
829 search) > 0);
830
831 BUG_ON(i.r != end(t->data) &&
832 bkey_cmp(i.r, search) <= 0);
833#endif
834
835 while (likely(i.l != i.r) &&
836 bkey_cmp(i.l, search) <= 0)
837 i.l = bkey_next(i.l);
838
839 return i.l;
840}
841
842/* Btree iterator */
843
Kent Overstreet48dad8b2013-09-10 18:48:51 -0700844/*
845 * Returns true if l > r - unless l == r, in which case returns true if l is
846 * older than r.
847 *
848 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
849 * equal in different sets, we have to process them newest to oldest.
850 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700851static inline bool btree_iter_cmp(struct btree_iter_set l,
852 struct btree_iter_set r)
853{
854 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
855
856 return c ? c > 0 : l.k < r.k;
857}
858
859static inline bool btree_iter_end(struct btree_iter *iter)
860{
861 return !iter->used;
862}
863
864void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
865 struct bkey *end)
866{
867 if (k != end)
868 BUG_ON(!heap_add(iter,
869 ((struct btree_iter_set) { k, end }),
870 btree_iter_cmp));
871}
872
873struct bkey *__bch_btree_iter_init(struct btree *b, struct btree_iter *iter,
874 struct bkey *search, struct bset_tree *start)
875{
876 struct bkey *ret = NULL;
877 iter->size = ARRAY_SIZE(iter->data);
878 iter->used = 0;
879
880 for (; start <= &b->sets[b->nsets]; start++) {
881 ret = bch_bset_search(b, start, search);
882 bch_btree_iter_push(iter, ret, end(start->data));
883 }
884
885 return ret;
886}
887
888struct bkey *bch_btree_iter_next(struct btree_iter *iter)
889{
890 struct btree_iter_set unused;
891 struct bkey *ret = NULL;
892
893 if (!btree_iter_end(iter)) {
894 ret = iter->data->k;
895 iter->data->k = bkey_next(iter->data->k);
896
897 if (iter->data->k > iter->data->end) {
Kent Overstreetcc0f4ea2013-03-27 12:47:45 -0700898 WARN_ONCE(1, "bset was corrupt!\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700899 iter->data->k = iter->data->end;
900 }
901
902 if (iter->data->k == iter->data->end)
903 heap_pop(iter, unused, btree_iter_cmp);
904 else
905 heap_sift(iter, 0, btree_iter_cmp);
906 }
907
908 return ret;
909}
910
911struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
912 struct btree *b, ptr_filter_fn fn)
913{
914 struct bkey *ret;
915
916 do {
917 ret = bch_btree_iter_next(iter);
918 } while (ret && fn(b, ret));
919
920 return ret;
921}
922
923struct bkey *bch_next_recurse_key(struct btree *b, struct bkey *search)
924{
925 struct btree_iter iter;
926
927 bch_btree_iter_init(b, &iter, search);
928 return bch_btree_iter_next_filter(&iter, b, bch_ptr_bad);
929}
930
931/* Mergesort */
932
Kent Overstreet84786432013-09-23 23:17:35 -0700933static void sort_key_next(struct btree_iter *iter,
934 struct btree_iter_set *i)
935{
936 i->k = bkey_next(i->k);
937
938 if (i->k == i->end)
939 *i = iter->data[--iter->used];
940}
941
Kent Overstreetcafe5632013-03-23 16:11:31 -0700942static void btree_sort_fixup(struct btree_iter *iter)
943{
944 while (iter->used > 1) {
945 struct btree_iter_set *top = iter->data, *i = top + 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700946
947 if (iter->used > 2 &&
948 btree_iter_cmp(i[0], i[1]))
949 i++;
950
Kent Overstreet84786432013-09-23 23:17:35 -0700951 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700952 break;
953
Kent Overstreet84786432013-09-23 23:17:35 -0700954 if (!KEY_SIZE(i->k)) {
955 sort_key_next(iter, i);
956 heap_sift(iter, i - top, btree_iter_cmp);
957 continue;
958 }
959
960 if (top->k > i->k) {
961 if (bkey_cmp(top->k, i->k) >= 0)
962 sort_key_next(iter, i);
963 else
964 bch_cut_front(top->k, i->k);
965
966 heap_sift(iter, i - top, btree_iter_cmp);
967 } else {
968 /* can't happen because of comparison func */
969 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
970 bch_cut_back(&START_KEY(i->k), top->k);
971 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700972 }
973}
974
975static void btree_mergesort(struct btree *b, struct bset *out,
976 struct btree_iter *iter,
977 bool fixup, bool remove_stale)
978{
979 struct bkey *k, *last = NULL;
980 bool (*bad)(struct btree *, const struct bkey *) = remove_stale
981 ? bch_ptr_bad
982 : bch_ptr_invalid;
983
984 while (!btree_iter_end(iter)) {
985 if (fixup && !b->level)
986 btree_sort_fixup(iter);
987
988 k = bch_btree_iter_next(iter);
989 if (bad(b, k))
990 continue;
991
992 if (!last) {
993 last = out->start;
994 bkey_copy(last, k);
995 } else if (b->level ||
996 !bch_bkey_try_merge(b, last, k)) {
997 last = bkey_next(last);
998 bkey_copy(last, k);
999 }
1000 }
1001
1002 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1003
1004 pr_debug("sorted %i keys", out->keys);
1005 bch_check_key_order(b, out);
1006}
1007
1008static void __btree_sort(struct btree *b, struct btree_iter *iter,
1009 unsigned start, unsigned order, bool fixup)
1010{
1011 uint64_t start_time;
1012 bool remove_stale = !b->written;
1013 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1014 order);
1015 if (!out) {
1016 mutex_lock(&b->c->sort_lock);
1017 out = b->c->sort;
1018 order = ilog2(bucket_pages(b->c));
1019 }
1020
1021 start_time = local_clock();
1022
1023 btree_mergesort(b, out, iter, fixup, remove_stale);
1024 b->nsets = start;
1025
1026 if (!fixup && !start && b->written)
1027 bch_btree_verify(b, out);
1028
1029 if (!start && order == b->page_order) {
1030 /*
1031 * Our temporary buffer is the same size as the btree node's
1032 * buffer, we can just swap buffers instead of doing a big
1033 * memcpy()
1034 */
1035
Kent Overstreet81ab4192013-10-31 15:46:42 -07001036 out->magic = bset_magic(&b->c->sb);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001037 out->seq = b->sets[0].data->seq;
1038 out->version = b->sets[0].data->version;
1039 swap(out, b->sets[0].data);
1040
1041 if (b->c->sort == b->sets[0].data)
1042 b->c->sort = out;
1043 } else {
1044 b->sets[start].data->keys = out->keys;
1045 memcpy(b->sets[start].data->start, out->start,
1046 (void *) end(out) - (void *) out->start);
1047 }
1048
1049 if (out == b->c->sort)
1050 mutex_unlock(&b->c->sort_lock);
1051 else
1052 free_pages((unsigned long) out, order);
1053
1054 if (b->written)
1055 bset_build_written_tree(b);
1056
1057 if (!start) {
1058 spin_lock(&b->c->sort_time_lock);
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001059 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001060 spin_unlock(&b->c->sort_time_lock);
1061 }
1062}
1063
1064void bch_btree_sort_partial(struct btree *b, unsigned start)
1065{
1066 size_t oldsize = 0, order = b->page_order, keys = 0;
1067 struct btree_iter iter;
1068 __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
1069
1070 BUG_ON(b->sets[b->nsets].data == write_block(b) &&
1071 (b->sets[b->nsets].size || b->nsets));
1072
1073 if (b->written)
1074 oldsize = bch_count_data(b);
1075
1076 if (start) {
1077 unsigned i;
1078
1079 for (i = start; i <= b->nsets; i++)
1080 keys += b->sets[i].data->keys;
1081
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001082 order = roundup_pow_of_two(__set_bytes(b->sets->data,
1083 keys)) / PAGE_SIZE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001084 if (order)
1085 order = ilog2(order);
1086 }
1087
1088 __btree_sort(b, &iter, start, order, false);
1089
1090 EBUG_ON(b->written && bch_count_data(b) != oldsize);
1091}
1092
1093void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)
1094{
1095 BUG_ON(!b->written);
1096 __btree_sort(b, iter, 0, b->page_order, true);
1097}
1098
1099void bch_btree_sort_into(struct btree *b, struct btree *new)
1100{
1101 uint64_t start_time = local_clock();
1102
1103 struct btree_iter iter;
1104 bch_btree_iter_init(b, &iter, NULL);
1105
1106 btree_mergesort(b, new->sets->data, &iter, false, true);
1107
1108 spin_lock(&b->c->sort_time_lock);
Kent Overstreet169ef1c2013-03-28 12:50:55 -06001109 bch_time_stats_update(&b->c->sort_time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001110 spin_unlock(&b->c->sort_time_lock);
1111
1112 bkey_copy_key(&new->key, &b->key);
1113 new->sets->size = 0;
1114}
1115
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001116#define SORT_CRIT (4096 / sizeof(uint64_t))
1117
Kent Overstreetcafe5632013-03-23 16:11:31 -07001118void bch_btree_sort_lazy(struct btree *b)
1119{
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001120 unsigned crit = SORT_CRIT;
1121 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001122
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001123 /* Don't sort if nothing to do */
1124 if (!b->nsets)
1125 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001126
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001127 /* If not a leaf node, always sort */
1128 if (b->level) {
1129 bch_btree_sort(b);
1130 return;
1131 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001132
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001133 for (i = b->nsets - 1; i >= 0; --i) {
1134 crit *= b->c->sort_crit_factor;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001135
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001136 if (b->sets[i].data->keys < crit) {
1137 bch_btree_sort_partial(b, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001138 return;
1139 }
1140 }
1141
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001142 /* Sort if we'd overflow */
1143 if (b->nsets + 1 == MAX_BSETS) {
1144 bch_btree_sort(b);
1145 return;
1146 }
1147
1148out:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001149 bset_build_written_tree(b);
1150}
1151
1152/* Sysfs stuff */
1153
1154struct bset_stats {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001155 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001156 size_t nodes;
1157 size_t sets_written, sets_unwritten;
1158 size_t bytes_written, bytes_unwritten;
1159 size_t floats, failed;
1160};
1161
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001162static int btree_bset_stats(struct btree_op *op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001163{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001164 struct bset_stats *stats = container_of(op, struct bset_stats, op);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001165 unsigned i;
1166
1167 stats->nodes++;
1168
1169 for (i = 0; i <= b->nsets; i++) {
1170 struct bset_tree *t = &b->sets[i];
1171 size_t bytes = t->data->keys * sizeof(uint64_t);
1172 size_t j;
1173
1174 if (bset_written(b, t)) {
1175 stats->sets_written++;
1176 stats->bytes_written += bytes;
1177
1178 stats->floats += t->size - 1;
1179
1180 for (j = 1; j < t->size; j++)
1181 if (t->tree[j].exponent == 127)
1182 stats->failed++;
1183 } else {
1184 stats->sets_unwritten++;
1185 stats->bytes_unwritten += bytes;
1186 }
1187 }
1188
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001189 return MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001190}
1191
1192int bch_bset_print_stats(struct cache_set *c, char *buf)
1193{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001194 struct bset_stats t;
1195 int ret;
1196
Kent Overstreetcafe5632013-03-23 16:11:31 -07001197 memset(&t, 0, sizeof(struct bset_stats));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001198 bch_btree_op_init(&t.op, -1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001200 ret = bch_btree_map_nodes(&t.op, c, &ZERO_KEY, btree_bset_stats);
1201 if (ret < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001202 return ret;
1203
1204 return snprintf(buf, PAGE_SIZE,
1205 "btree nodes: %zu\n"
1206 "written sets: %zu\n"
1207 "unwritten sets: %zu\n"
1208 "written key bytes: %zu\n"
1209 "unwritten key bytes: %zu\n"
1210 "floats: %zu\n"
1211 "failed: %zu\n",
1212 t.nodes,
1213 t.sets_written, t.sets_unwritten,
1214 t.bytes_written, t.bytes_unwritten,
1215 t.floats, t.failed);
1216}