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