blob: e04e5908e29f73723f78584ed1fc4002d0483fa8 [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 Overstreet085d2a32013-11-11 18:20:51 -080017int __bch_keylist_realloc(struct keylist *l, unsigned u64s)
Kent Overstreetcafe5632013-03-23 16:11:31 -070018{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070019 size_t oldsize = bch_keylist_nkeys(l);
Kent Overstreet085d2a32013-11-11 18:20:51 -080020 size_t newsize = oldsize + u64s;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070021 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
Kent Overstreetcafe5632013-03-23 16:11:31 -070024 newsize = roundup_pow_of_two(newsize);
25
26 if (newsize <= KEYLIST_INLINE ||
27 roundup_pow_of_two(oldsize) == newsize)
28 return 0;
29
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070030 new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -070031
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070032 if (!new_keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -070033 return -ENOMEM;
34
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070035 if (!old_keys)
36 memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -070037
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070038 l->keys_p = new_keys;
39 l->top_p = new_keys + oldsize;
Kent Overstreetcafe5632013-03-23 16:11:31 -070040
41 return 0;
42}
43
44struct bkey *bch_keylist_pop(struct keylist *l)
45{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070046 struct bkey *k = l->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -070047
48 if (k == l->top)
49 return NULL;
50
51 while (bkey_next(k) != l->top)
52 k = bkey_next(k);
53
54 return l->top = k;
55}
56
Kent Overstreet26c949f2013-09-10 18:41:15 -070057void bch_keylist_pop_front(struct keylist *l)
58{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070059 l->top_p -= bkey_u64s(l->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -070060
Kent Overstreetc2f95ae2013-07-24 17:24:25 -070061 memmove(l->keys,
62 bkey_next(l->keys),
63 bch_keylist_bytes(l));
Kent Overstreet26c949f2013-09-10 18:41:15 -070064}
65
Kent Overstreetcafe5632013-03-23 16:11:31 -070066/* Pointer validation */
67
Kent Overstreetd5cc66e2013-07-24 23:06:40 -070068static bool __ptr_invalid(struct cache_set *c, const struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -070069{
70 unsigned i;
Kent Overstreetcafe5632013-03-23 16:11:31 -070071
72 for (i = 0; i < KEY_PTRS(k); i++)
73 if (ptr_available(c, k, i)) {
74 struct cache *ca = PTR_CACHE(c, k, i);
75 size_t bucket = PTR_BUCKET_NR(c, k, i);
76 size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
77
78 if (KEY_SIZE(k) + r > c->sb.bucket_size ||
79 bucket < ca->sb.first_bucket ||
80 bucket >= ca->sb.nbuckets)
Kent Overstreetd5cc66e2013-07-24 23:06:40 -070081 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -070082 }
83
84 return false;
Kent Overstreetd5cc66e2013-07-24 23:06:40 -070085}
86
87bool bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
88{
89 char buf[80];
90
91 if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
92 goto bad;
93
94 if (__ptr_invalid(c, k))
95 goto bad;
96
97 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -070098bad:
Kent Overstreet85b14922013-05-14 20:33:16 -070099 bch_bkey_to_text(buf, sizeof(buf), k);
Kent Overstreetd5cc66e2013-07-24 23:06:40 -0700100 cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k));
101 return true;
102}
103
104bool bch_extent_ptr_invalid(struct cache_set *c, const struct bkey *k)
105{
106 char buf[80];
107
108 if (!KEY_SIZE(k))
109 return true;
110
111 if (KEY_SIZE(k) > KEY_OFFSET(k))
112 goto bad;
113
114 if (__ptr_invalid(c, k))
115 goto bad;
116
117 return false;
118bad:
119 bch_bkey_to_text(buf, sizeof(buf), k);
120 cache_bug(c, "spotted extent %s: %s", buf, bch_ptr_status(c, k));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700121 return true;
122}
123
Kent Overstreet280481d2013-10-24 16:36:03 -0700124static bool ptr_bad_expensive_checks(struct btree *b, const struct bkey *k,
125 unsigned ptr)
126{
127 struct bucket *g = PTR_BUCKET(b->c, k, ptr);
128 char buf[80];
129
130 if (mutex_trylock(&b->c->bucket_lock)) {
131 if (b->level) {
132 if (KEY_DIRTY(k) ||
133 g->prio != BTREE_PRIO ||
134 (b->c->gc_mark_valid &&
135 GC_MARK(g) != GC_MARK_METADATA))
136 goto err;
137
138 } else {
139 if (g->prio == BTREE_PRIO)
140 goto err;
141
142 if (KEY_DIRTY(k) &&
143 b->c->gc_mark_valid &&
144 GC_MARK(g) != GC_MARK_DIRTY)
145 goto err;
146 }
147 mutex_unlock(&b->c->bucket_lock);
148 }
149
150 return false;
151err:
152 mutex_unlock(&b->c->bucket_lock);
153 bch_bkey_to_text(buf, sizeof(buf), k);
154 btree_bug(b,
155"inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
156 buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
157 g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
158 return true;
159}
160
Kent Overstreetcafe5632013-03-23 16:11:31 -0700161bool bch_ptr_bad(struct btree *b, const struct bkey *k)
162{
163 struct bucket *g;
164 unsigned i, stale;
165
166 if (!bkey_cmp(k, &ZERO_KEY) ||
167 !KEY_PTRS(k) ||
168 bch_ptr_invalid(b, k))
169 return true;
170
Kent Overstreetd56d0002013-08-09 21:14:13 -0700171 for (i = 0; i < KEY_PTRS(k); i++)
Kent Overstreete58ff152013-07-24 18:14:44 -0700172 if (!ptr_available(b->c, k, i))
173 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174
Kent Overstreetd56d0002013-08-09 21:14:13 -0700175 if (!expensive_debug_checks(b->c) && KEY_DIRTY(k))
176 return false;
177
178 for (i = 0; i < KEY_PTRS(k); i++) {
Kent Overstreete58ff152013-07-24 18:14:44 -0700179 g = PTR_BUCKET(b->c, k, i);
180 stale = ptr_stale(b->c, k, i);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181
Kent Overstreete58ff152013-07-24 18:14:44 -0700182 btree_bug_on(stale > 96, b,
183 "key too stale: %i, need_gc %u",
184 stale, b->c->need_gc);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700185
Kent Overstreete58ff152013-07-24 18:14:44 -0700186 btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
187 b, "stale dirty pointer");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700188
Kent Overstreete58ff152013-07-24 18:14:44 -0700189 if (stale)
190 return true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191
Kent Overstreet280481d2013-10-24 16:36:03 -0700192 if (expensive_debug_checks(b->c) &&
193 ptr_bad_expensive_checks(b, k, i))
194 return true;
Kent Overstreete58ff152013-07-24 18:14:44 -0700195 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196
197 return false;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700198}
199
200/* Key/pointer manipulation */
201
202void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
203 unsigned i)
204{
205 BUG_ON(i > KEY_PTRS(src));
206
207 /* Only copy the header, key, and one pointer. */
208 memcpy(dest, src, 2 * sizeof(uint64_t));
209 dest->ptr[0] = src->ptr[i];
210 SET_KEY_PTRS(dest, 1);
211 /* We didn't copy the checksum so clear that bit. */
212 SET_KEY_CSUM(dest, 0);
213}
214
215bool __bch_cut_front(const struct bkey *where, struct bkey *k)
216{
217 unsigned i, len = 0;
218
219 if (bkey_cmp(where, &START_KEY(k)) <= 0)
220 return false;
221
222 if (bkey_cmp(where, k) < 0)
223 len = KEY_OFFSET(k) - KEY_OFFSET(where);
224 else
225 bkey_copy_key(k, where);
226
227 for (i = 0; i < KEY_PTRS(k); i++)
228 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
229
230 BUG_ON(len > KEY_SIZE(k));
231 SET_KEY_SIZE(k, len);
232 return true;
233}
234
235bool __bch_cut_back(const struct bkey *where, struct bkey *k)
236{
237 unsigned len = 0;
238
239 if (bkey_cmp(where, k) >= 0)
240 return false;
241
242 BUG_ON(KEY_INODE(where) != KEY_INODE(k));
243
244 if (bkey_cmp(where, &START_KEY(k)) > 0)
245 len = KEY_OFFSET(where) - KEY_START(k);
246
247 bkey_copy_key(k, where);
248
249 BUG_ON(len > KEY_SIZE(k));
250 SET_KEY_SIZE(k, len);
251 return true;
252}
253
254static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
255{
256 return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
257 ~((uint64_t)1 << 63);
258}
259
260/* Tries to merge l and r: l should be lower than r
261 * Returns true if we were able to merge. If we did merge, l will be the merged
262 * key, r will be untouched.
263 */
264bool bch_bkey_try_merge(struct btree *b, struct bkey *l, struct bkey *r)
265{
266 unsigned i;
267
268 if (key_merging_disabled(b->c))
269 return false;
270
271 if (KEY_PTRS(l) != KEY_PTRS(r) ||
272 KEY_DIRTY(l) != KEY_DIRTY(r) ||
273 bkey_cmp(l, &START_KEY(r)))
274 return false;
275
276 for (i = 0; i < KEY_PTRS(l); i++)
277 if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
278 PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
279 return false;
280
281 /* Keys with no pointers aren't restricted to one bucket and could
282 * overflow KEY_SIZE
283 */
284 if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
285 SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
286 SET_KEY_SIZE(l, USHRT_MAX);
287
288 bch_cut_front(l, r);
289 return false;
290 }
291
292 if (KEY_CSUM(l)) {
293 if (KEY_CSUM(r))
294 l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
295 else
296 SET_KEY_CSUM(l, 0);
297 }
298
299 SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
300 SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
301
302 return true;
303}
304
Kent Overstreetee811282013-12-17 23:49:49 -0800305/* Auxiliary search trees */
306
307/* 32 bits total: */
308#define BKEY_MID_BITS 3
309#define BKEY_EXPONENT_BITS 7
310#define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
311#define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1)
312
313struct bkey_float {
314 unsigned exponent:BKEY_EXPONENT_BITS;
315 unsigned m:BKEY_MID_BITS;
316 unsigned mantissa:BKEY_MANTISSA_BITS;
317} __packed;
318
319/*
320 * BSET_CACHELINE was originally intended to match the hardware cacheline size -
321 * it used to be 64, but I realized the lookup code would touch slightly less
322 * memory if it was 128.
323 *
324 * It definites the number of bytes (in struct bset) per struct bkey_float in
325 * the auxiliar search tree - when we're done searching the bset_float tree we
326 * have this many bytes left that we do a linear search over.
327 *
328 * Since (after level 5) every level of the bset_tree is on a new cacheline,
329 * we're touching one fewer cacheline in the bset tree in exchange for one more
330 * cacheline in the linear search - but the linear search might stop before it
331 * gets to the second cacheline.
332 */
333
334#define BSET_CACHELINE 128
335
336/* Space required for the btree node keys */
337static inline size_t btree_keys_bytes(struct btree *b)
338{
339 return PAGE_SIZE << b->page_order;
340}
341
342static inline size_t btree_keys_cachelines(struct btree *b)
343{
344 return btree_keys_bytes(b) / BSET_CACHELINE;
345}
346
347/* Space required for the auxiliary search trees */
348static inline size_t bset_tree_bytes(struct btree *b)
349{
350 return btree_keys_cachelines(b) * sizeof(struct bkey_float);
351}
352
353/* Space required for the prev pointers */
354static inline size_t bset_prev_bytes(struct btree *b)
355{
356 return btree_keys_cachelines(b) * sizeof(uint8_t);
357}
358
359/* Memory allocation */
360
361void bch_btree_keys_free(struct btree *b)
362{
363 struct bset_tree *t = b->sets;
364
365 if (bset_prev_bytes(b) < PAGE_SIZE)
366 kfree(t->prev);
367 else
368 free_pages((unsigned long) t->prev,
369 get_order(bset_prev_bytes(b)));
370
371 if (bset_tree_bytes(b) < PAGE_SIZE)
372 kfree(t->tree);
373 else
374 free_pages((unsigned long) t->tree,
375 get_order(bset_tree_bytes(b)));
376
377 free_pages((unsigned long) t->data, b->page_order);
378
379 t->prev = NULL;
380 t->tree = NULL;
381 t->data = NULL;
382}
383
384int bch_btree_keys_alloc(struct btree *b, unsigned page_order, gfp_t gfp)
385{
386 struct bset_tree *t = b->sets;
387
388 BUG_ON(t->data);
389
390 b->page_order = page_order;
391
392 t->data = (void *) __get_free_pages(gfp, b->page_order);
393 if (!t->data)
394 goto err;
395
396 t->tree = bset_tree_bytes(b) < PAGE_SIZE
397 ? kmalloc(bset_tree_bytes(b), gfp)
398 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
399 if (!t->tree)
400 goto err;
401
402 t->prev = bset_prev_bytes(b) < PAGE_SIZE
403 ? kmalloc(bset_prev_bytes(b), gfp)
404 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
405 if (!t->prev)
406 goto err;
407
408 return 0;
409err:
410 bch_btree_keys_free(b);
411 return -ENOMEM;
412}
413
Kent Overstreetcafe5632013-03-23 16:11:31 -0700414/* Binary tree stuff for auxiliary search trees */
415
416static unsigned inorder_next(unsigned j, unsigned size)
417{
418 if (j * 2 + 1 < size) {
419 j = j * 2 + 1;
420
421 while (j * 2 < size)
422 j *= 2;
423 } else
424 j >>= ffz(j) + 1;
425
426 return j;
427}
428
429static unsigned inorder_prev(unsigned j, unsigned size)
430{
431 if (j * 2 < size) {
432 j = j * 2;
433
434 while (j * 2 + 1 < size)
435 j = j * 2 + 1;
436 } else
437 j >>= ffs(j);
438
439 return j;
440}
441
442/* I have no idea why this code works... and I'm the one who wrote it
443 *
444 * However, I do know what it does:
445 * Given a binary tree constructed in an array (i.e. how you normally implement
446 * a heap), it converts a node in the tree - referenced by array index - to the
447 * index it would have if you did an inorder traversal.
448 *
449 * Also tested for every j, size up to size somewhere around 6 million.
450 *
451 * The binary tree starts at array index 1, not 0
452 * extra is a function of size:
453 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
454 */
455static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
456{
457 unsigned b = fls(j);
458 unsigned shift = fls(size - 1) - b;
459
460 j ^= 1U << (b - 1);
461 j <<= 1;
462 j |= 1;
463 j <<= shift;
464
465 if (j > extra)
466 j -= (j - extra) >> 1;
467
468 return j;
469}
470
471static unsigned to_inorder(unsigned j, struct bset_tree *t)
472{
473 return __to_inorder(j, t->size, t->extra);
474}
475
476static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
477{
478 unsigned shift;
479
480 if (j > extra)
481 j += j - extra;
482
483 shift = ffs(j);
484
485 j >>= shift;
486 j |= roundup_pow_of_two(size) >> shift;
487
488 return j;
489}
490
491static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
492{
493 return __inorder_to_tree(j, t->size, t->extra);
494}
495
496#if 0
497void inorder_test(void)
498{
499 unsigned long done = 0;
500 ktime_t start = ktime_get();
501
502 for (unsigned size = 2;
503 size < 65536000;
504 size++) {
505 unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
506 unsigned i = 1, j = rounddown_pow_of_two(size - 1);
507
508 if (!(size % 4096))
509 printk(KERN_NOTICE "loop %u, %llu per us\n", size,
510 done / ktime_us_delta(ktime_get(), start));
511
512 while (1) {
513 if (__inorder_to_tree(i, size, extra) != j)
514 panic("size %10u j %10u i %10u", size, j, i);
515
516 if (__to_inorder(j, size, extra) != i)
517 panic("size %10u j %10u i %10u", size, j, i);
518
519 if (j == rounddown_pow_of_two(size) - 1)
520 break;
521
522 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
523
524 j = inorder_next(j, size);
525 i++;
526 }
527
528 done += size - 1;
529 }
530}
531#endif
532
533/*
Phil Viana48a73022013-06-03 09:51:42 -0300534 * Cacheline/offset <-> bkey pointer arithmetic:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700535 *
536 * t->tree is a binary search tree in an array; each node corresponds to a key
537 * in one cacheline in t->set (BSET_CACHELINE bytes).
538 *
539 * This means we don't have to store the full index of the key that a node in
540 * the binary tree points to; to_inorder() gives us the cacheline, and then
541 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
542 *
Phil Viana48a73022013-06-03 09:51:42 -0300543 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
Kent Overstreetcafe5632013-03-23 16:11:31 -0700544 * make this work.
545 *
546 * To construct the bfloat for an arbitrary key we need to know what the key
547 * immediately preceding it is: we have to check if the two keys differ in the
548 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
549 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
550 */
551
552static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
553 unsigned offset)
554{
555 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
556}
557
558static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
559{
560 return ((void *) k - (void *) t->data) / BSET_CACHELINE;
561}
562
563static unsigned bkey_to_cacheline_offset(struct bkey *k)
564{
565 return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
566}
567
568static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
569{
570 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
571}
572
573static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
574{
575 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
576}
577
578/*
579 * For the write set - the one we're currently inserting keys into - we don't
580 * maintain a full search tree, we just keep a simple lookup table in t->prev.
581 */
582static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
583{
584 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
585}
586
587static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
588{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700589 low >>= shift;
590 low |= (high << 1) << (63U - shift);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700591 return low;
592}
593
594static inline unsigned bfloat_mantissa(const struct bkey *k,
595 struct bkey_float *f)
596{
597 const uint64_t *p = &k->low - (f->exponent >> 6);
598 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
599}
600
601static void make_bfloat(struct bset_tree *t, unsigned j)
602{
603 struct bkey_float *f = &t->tree[j];
604 struct bkey *m = tree_to_bkey(t, j);
605 struct bkey *p = tree_to_prev_bkey(t, j);
606
607 struct bkey *l = is_power_of_2(j)
608 ? t->data->start
609 : tree_to_prev_bkey(t, j >> ffs(j));
610
611 struct bkey *r = is_power_of_2(j + 1)
Kent Overstreetfafff812013-12-17 21:56:21 -0800612 ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700613 : tree_to_bkey(t, j >> (ffz(j) + 1));
614
615 BUG_ON(m < l || m > r);
616 BUG_ON(bkey_next(p) != m);
617
618 if (KEY_INODE(l) != KEY_INODE(r))
619 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
620 else
621 f->exponent = fls64(r->low ^ l->low);
622
623 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
624
625 /*
626 * Setting f->exponent = 127 flags this node as failed, and causes the
627 * lookup code to fall back to comparing against the original key.
628 */
629
630 if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
631 f->mantissa = bfloat_mantissa(m, f) - 1;
632 else
633 f->exponent = 127;
634}
635
636static void bset_alloc_tree(struct btree *b, struct bset_tree *t)
637{
638 if (t != b->sets) {
639 unsigned j = roundup(t[-1].size,
640 64 / sizeof(struct bkey_float));
641
642 t->tree = t[-1].tree + j;
643 t->prev = t[-1].prev + j;
644 }
645
646 while (t < b->sets + MAX_BSETS)
647 t++->size = 0;
648}
649
Kent Overstreetee811282013-12-17 23:49:49 -0800650static void bch_bset_build_unwritten_tree(struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700651{
Kent Overstreetee811282013-12-17 23:49:49 -0800652 struct bset_tree *t = bset_tree_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700653
654 bset_alloc_tree(b, t);
655
Kent Overstreetee811282013-12-17 23:49:49 -0800656 if (t->tree != b->sets->tree + btree_keys_cachelines(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700657 t->prev[0] = bkey_to_cacheline_offset(t->data->start);
658 t->size = 1;
659 }
660}
661
Kent Overstreetee811282013-12-17 23:49:49 -0800662void bch_bset_init_next(struct btree *b, struct bset *i, uint64_t magic)
663{
664 if (i != b->sets->data) {
665 b->sets[++b->nsets].data = i;
666 i->seq = b->sets->data->seq;
667 } else
668 get_random_bytes(&i->seq, sizeof(uint64_t));
669
670 i->magic = magic;
671 i->version = 0;
672 i->keys = 0;
673
674 bch_bset_build_unwritten_tree(b);
675}
676
Kent Overstreetcafe5632013-03-23 16:11:31 -0700677static void bset_build_written_tree(struct btree *b)
678{
Kent Overstreetee811282013-12-17 23:49:49 -0800679 struct bset_tree *t = bset_tree_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700680 struct bkey *k = t->data->start;
681 unsigned j, cacheline = 1;
682
683 bset_alloc_tree(b, t);
684
685 t->size = min_t(unsigned,
Kent Overstreetfafff812013-12-17 21:56:21 -0800686 bkey_to_cacheline(t, bset_bkey_last(t->data)),
Kent Overstreetee811282013-12-17 23:49:49 -0800687 b->sets->tree + btree_keys_cachelines(b) - t->tree);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700688
689 if (t->size < 2) {
690 t->size = 0;
691 return;
692 }
693
694 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
695
696 /* First we figure out where the first key in each cacheline is */
697 for (j = inorder_next(0, t->size);
698 j;
699 j = inorder_next(j, t->size)) {
700 while (bkey_to_cacheline(t, k) != cacheline)
701 k = bkey_next(k);
702
703 t->prev[j] = bkey_u64s(k);
704 k = bkey_next(k);
705 cacheline++;
706 t->tree[j].m = bkey_to_cacheline_offset(k);
707 }
708
Kent Overstreetfafff812013-12-17 21:56:21 -0800709 while (bkey_next(k) != bset_bkey_last(t->data))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700710 k = bkey_next(k);
711
712 t->end = *k;
713
714 /* Then we build the tree */
715 for (j = inorder_next(0, t->size);
716 j;
717 j = inorder_next(j, t->size))
718 make_bfloat(t, j);
719}
720
721void bch_bset_fix_invalidated_key(struct btree *b, struct bkey *k)
722{
723 struct bset_tree *t;
724 unsigned inorder, j = 1;
725
Kent Overstreetee811282013-12-17 23:49:49 -0800726 for (t = b->sets; t <= bset_tree_last(b); t++)
Kent Overstreetfafff812013-12-17 21:56:21 -0800727 if (k < bset_bkey_last(t->data))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728 goto found_set;
729
730 BUG();
731found_set:
732 if (!t->size || !bset_written(b, t))
733 return;
734
735 inorder = bkey_to_cacheline(t, k);
736
737 if (k == t->data->start)
738 goto fix_left;
739
Kent Overstreetfafff812013-12-17 21:56:21 -0800740 if (bkey_next(k) == bset_bkey_last(t->data)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700741 t->end = *k;
742 goto fix_right;
743 }
744
745 j = inorder_to_tree(inorder, t);
746
747 if (j &&
748 j < t->size &&
749 k == tree_to_bkey(t, j))
750fix_left: do {
751 make_bfloat(t, j);
752 j = j * 2;
753 } while (j < t->size);
754
755 j = inorder_to_tree(inorder + 1, t);
756
757 if (j &&
758 j < t->size &&
759 k == tree_to_prev_bkey(t, j))
760fix_right: do {
761 make_bfloat(t, j);
762 j = j * 2 + 1;
763 } while (j < t->size);
764}
765
Kent Overstreetee811282013-12-17 23:49:49 -0800766static void bch_bset_fix_lookup_table(struct btree *b,
767 struct bset_tree *t,
768 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700769{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700770 unsigned shift = bkey_u64s(k);
771 unsigned j = bkey_to_cacheline(t, k);
772
773 /* We're getting called from btree_split() or btree_gc, just bail out */
774 if (!t->size)
775 return;
776
777 /* k is the key we just inserted; we need to find the entry in the
778 * lookup table for the first key that is strictly greater than k:
779 * it's either k's cacheline or the next one
780 */
781 if (j < t->size &&
782 table_to_bkey(t, j) <= k)
783 j++;
784
785 /* Adjust all the lookup table entries, and find a new key for any that
786 * have gotten too big
787 */
788 for (; j < t->size; j++) {
789 t->prev[j] += shift;
790
791 if (t->prev[j] > 7) {
792 k = table_to_bkey(t, j - 1);
793
794 while (k < cacheline_to_bkey(t, j, 0))
795 k = bkey_next(k);
796
797 t->prev[j] = bkey_to_cacheline_offset(k);
798 }
799 }
800
Kent Overstreetee811282013-12-17 23:49:49 -0800801 if (t->size == b->sets->tree + btree_keys_cachelines(b) - t->tree)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700802 return;
803
804 /* Possibly add a new entry to the end of the lookup table */
805
806 for (k = table_to_bkey(t, t->size - 1);
Kent Overstreetfafff812013-12-17 21:56:21 -0800807 k != bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700808 k = bkey_next(k))
809 if (t->size == bkey_to_cacheline(t, k)) {
810 t->prev[t->size] = bkey_to_cacheline_offset(k);
811 t->size++;
812 }
813}
814
Kent Overstreetee811282013-12-17 23:49:49 -0800815void bch_bset_insert(struct btree *b, struct bkey *where,
816 struct bkey *insert)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700817{
Kent Overstreetee811282013-12-17 23:49:49 -0800818 struct bset_tree *t = bset_tree_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700819
Kent Overstreetee811282013-12-17 23:49:49 -0800820 BUG_ON(t->data != write_block(b));
821 BUG_ON(bset_byte_offset(b, t->data) +
822 __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
823 PAGE_SIZE << b->page_order);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700824
Kent Overstreetee811282013-12-17 23:49:49 -0800825 memmove((uint64_t *) where + bkey_u64s(insert),
826 where,
827 (void *) bset_bkey_last(t->data) - (void *) where);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700828
Kent Overstreetee811282013-12-17 23:49:49 -0800829 t->data->keys += bkey_u64s(insert);
830 bkey_copy(where, insert);
831 bch_bset_fix_lookup_table(b, t, where);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700832}
833
834struct bset_search_iter {
835 struct bkey *l, *r;
836};
837
838static struct bset_search_iter bset_search_write_set(struct btree *b,
839 struct bset_tree *t,
840 const struct bkey *search)
841{
842 unsigned li = 0, ri = t->size;
843
844 BUG_ON(!b->nsets &&
Kent Overstreetfafff812013-12-17 21:56:21 -0800845 t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700846
847 while (li + 1 != ri) {
848 unsigned m = (li + ri) >> 1;
849
850 if (bkey_cmp(table_to_bkey(t, m), search) > 0)
851 ri = m;
852 else
853 li = m;
854 }
855
856 return (struct bset_search_iter) {
857 table_to_bkey(t, li),
Kent Overstreetfafff812013-12-17 21:56:21 -0800858 ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700859 };
860}
861
862static struct bset_search_iter bset_search_tree(struct btree *b,
863 struct bset_tree *t,
864 const struct bkey *search)
865{
866 struct bkey *l, *r;
867 struct bkey_float *f;
868 unsigned inorder, j, n = 1;
869
870 do {
871 unsigned p = n << 4;
872 p &= ((int) (p - t->size)) >> 31;
873
874 prefetch(&t->tree[p]);
875
876 j = n;
877 f = &t->tree[j];
878
879 /*
880 * n = (f->mantissa > bfloat_mantissa())
881 * ? j * 2
882 * : j * 2 + 1;
883 *
884 * We need to subtract 1 from f->mantissa for the sign bit trick
885 * to work - that's done in make_bfloat()
886 */
887 if (likely(f->exponent != 127))
888 n = j * 2 + (((unsigned)
889 (f->mantissa -
890 bfloat_mantissa(search, f))) >> 31);
891 else
892 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
893 ? j * 2
894 : j * 2 + 1;
895 } while (n < t->size);
896
897 inorder = to_inorder(j, t);
898
899 /*
900 * n would have been the node we recursed to - the low bit tells us if
901 * we recursed left or recursed right.
902 */
903 if (n & 1) {
904 l = cacheline_to_bkey(t, inorder, f->m);
905
906 if (++inorder != t->size) {
907 f = &t->tree[inorder_next(j, t->size)];
908 r = cacheline_to_bkey(t, inorder, f->m);
909 } else
Kent Overstreetfafff812013-12-17 21:56:21 -0800910 r = bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700911 } else {
912 r = cacheline_to_bkey(t, inorder, f->m);
913
914 if (--inorder) {
915 f = &t->tree[inorder_prev(j, t->size)];
916 l = cacheline_to_bkey(t, inorder, f->m);
917 } else
918 l = t->data->start;
919 }
920
921 return (struct bset_search_iter) {l, r};
922}
923
924struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
925 const struct bkey *search)
926{
927 struct bset_search_iter i;
928
929 /*
930 * First, we search for a cacheline, then lastly we do a linear search
931 * within that cacheline.
932 *
933 * To search for the cacheline, there's three different possibilities:
934 * * The set is too small to have a search tree, so we just do a linear
935 * search over the whole set.
936 * * The set is the one we're currently inserting into; keeping a full
937 * auxiliary search tree up to date would be too expensive, so we
938 * use a much simpler lookup table to do a binary search -
939 * bset_search_write_set().
940 * * Or we use the auxiliary search tree we constructed earlier -
941 * bset_search_tree()
942 */
943
944 if (unlikely(!t->size)) {
945 i.l = t->data->start;
Kent Overstreetfafff812013-12-17 21:56:21 -0800946 i.r = bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700947 } else if (bset_written(b, t)) {
948 /*
949 * Each node in the auxiliary search tree covers a certain range
950 * of bits, and keys above and below the set it covers might
951 * differ outside those bits - so we have to special case the
952 * start and end - handle that here:
953 */
954
955 if (unlikely(bkey_cmp(search, &t->end) >= 0))
Kent Overstreetfafff812013-12-17 21:56:21 -0800956 return bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700957
958 if (unlikely(bkey_cmp(search, t->data->start) < 0))
959 return t->data->start;
960
961 i = bset_search_tree(b, t, search);
962 } else
963 i = bset_search_write_set(b, t, search);
964
Kent Overstreet280481d2013-10-24 16:36:03 -0700965 if (expensive_debug_checks(b->c)) {
966 BUG_ON(bset_written(b, t) &&
967 i.l != t->data->start &&
968 bkey_cmp(tree_to_prev_bkey(t,
969 inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
970 search) > 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700971
Kent Overstreetfafff812013-12-17 21:56:21 -0800972 BUG_ON(i.r != bset_bkey_last(t->data) &&
Kent Overstreet280481d2013-10-24 16:36:03 -0700973 bkey_cmp(i.r, search) <= 0);
974 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700975
976 while (likely(i.l != i.r) &&
977 bkey_cmp(i.l, search) <= 0)
978 i.l = bkey_next(i.l);
979
980 return i.l;
981}
982
983/* Btree iterator */
984
Kent Overstreet911c9612013-07-28 18:35:09 -0700985typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
986 struct btree_iter_set);
987
Kent Overstreetcafe5632013-03-23 16:11:31 -0700988static inline bool btree_iter_cmp(struct btree_iter_set l,
989 struct btree_iter_set r)
990{
Kent Overstreet911c9612013-07-28 18:35:09 -0700991 return bkey_cmp(l.k, r.k) > 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700992}
993
994static inline bool btree_iter_end(struct btree_iter *iter)
995{
996 return !iter->used;
997}
998
999void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
1000 struct bkey *end)
1001{
1002 if (k != end)
1003 BUG_ON(!heap_add(iter,
1004 ((struct btree_iter_set) { k, end }),
1005 btree_iter_cmp));
1006}
1007
Kent Overstreet911c9612013-07-28 18:35:09 -07001008static struct bkey *__bch_btree_iter_init(struct btree *b,
1009 struct btree_iter *iter,
1010 struct bkey *search,
1011 struct bset_tree *start)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001012{
1013 struct bkey *ret = NULL;
1014 iter->size = ARRAY_SIZE(iter->data);
1015 iter->used = 0;
1016
Kent Overstreet280481d2013-10-24 16:36:03 -07001017#ifdef CONFIG_BCACHE_DEBUG
1018 iter->b = b;
1019#endif
1020
Kent Overstreetcafe5632013-03-23 16:11:31 -07001021 for (; start <= &b->sets[b->nsets]; start++) {
1022 ret = bch_bset_search(b, start, search);
Kent Overstreetfafff812013-12-17 21:56:21 -08001023 bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001024 }
1025
1026 return ret;
1027}
1028
Kent Overstreet911c9612013-07-28 18:35:09 -07001029struct bkey *bch_btree_iter_init(struct btree *b,
1030 struct btree_iter *iter,
1031 struct bkey *search)
1032{
1033 return __bch_btree_iter_init(b, iter, search, b->sets);
1034}
1035
1036static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
1037 btree_iter_cmp_fn *cmp)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001038{
1039 struct btree_iter_set unused;
1040 struct bkey *ret = NULL;
1041
1042 if (!btree_iter_end(iter)) {
Kent Overstreet280481d2013-10-24 16:36:03 -07001043 bch_btree_iter_next_check(iter);
1044
Kent Overstreetcafe5632013-03-23 16:11:31 -07001045 ret = iter->data->k;
1046 iter->data->k = bkey_next(iter->data->k);
1047
1048 if (iter->data->k > iter->data->end) {
Kent Overstreetcc0f4ea2013-03-27 12:47:45 -07001049 WARN_ONCE(1, "bset was corrupt!\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001050 iter->data->k = iter->data->end;
1051 }
1052
1053 if (iter->data->k == iter->data->end)
Kent Overstreet911c9612013-07-28 18:35:09 -07001054 heap_pop(iter, unused, cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001055 else
Kent Overstreet911c9612013-07-28 18:35:09 -07001056 heap_sift(iter, 0, cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001057 }
1058
1059 return ret;
1060}
1061
Kent Overstreet911c9612013-07-28 18:35:09 -07001062struct bkey *bch_btree_iter_next(struct btree_iter *iter)
1063{
1064 return __bch_btree_iter_next(iter, btree_iter_cmp);
1065
1066}
1067
Kent Overstreetcafe5632013-03-23 16:11:31 -07001068struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
1069 struct btree *b, ptr_filter_fn fn)
1070{
1071 struct bkey *ret;
1072
1073 do {
1074 ret = bch_btree_iter_next(iter);
1075 } while (ret && fn(b, ret));
1076
1077 return ret;
1078}
1079
Kent Overstreetcafe5632013-03-23 16:11:31 -07001080/* Mergesort */
1081
Kent Overstreet67539e82013-09-10 22:53:34 -07001082void bch_bset_sort_state_free(struct bset_sort_state *state)
1083{
1084 if (state->pool)
1085 mempool_destroy(state->pool);
1086}
1087
1088int bch_bset_sort_state_init(struct bset_sort_state *state, unsigned page_order)
1089{
1090 spin_lock_init(&state->time.lock);
1091
1092 state->page_order = page_order;
1093 state->crit_factor = int_sqrt(1 << page_order);
1094
1095 state->pool = mempool_create_page_pool(1, page_order);
1096 if (!state->pool)
1097 return -ENOMEM;
1098
1099 return 0;
1100}
1101
Kent Overstreet84786432013-09-23 23:17:35 -07001102static void sort_key_next(struct btree_iter *iter,
1103 struct btree_iter_set *i)
1104{
1105 i->k = bkey_next(i->k);
1106
1107 if (i->k == i->end)
1108 *i = iter->data[--iter->used];
1109}
1110
Kent Overstreet911c9612013-07-28 18:35:09 -07001111/*
1112 * Returns true if l > r - unless l == r, in which case returns true if l is
1113 * older than r.
1114 *
1115 * Necessary for btree_sort_fixup() - if there are multiple keys that compare
1116 * equal in different sets, we have to process them newest to oldest.
1117 */
1118static inline bool sort_extent_cmp(struct btree_iter_set l,
1119 struct btree_iter_set r)
1120{
1121 int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
1122
1123 return c ? c > 0 : l.k < r.k;
1124}
1125
1126static inline bool sort_cmp(struct btree_iter_set l,
1127 struct btree_iter_set r)
1128{
1129 int64_t c = bkey_cmp(l.k, r.k);
1130
1131 return c ? c > 0 : l.k < r.k;
1132}
1133
1134static struct bkey *btree_sort_fixup_extents(struct btree_iter *iter,
1135 struct bkey *tmp)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001136{
1137 while (iter->used > 1) {
1138 struct btree_iter_set *top = iter->data, *i = top + 1;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001139
1140 if (iter->used > 2 &&
Kent Overstreet911c9612013-07-28 18:35:09 -07001141 sort_extent_cmp(i[0], i[1]))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001142 i++;
1143
Kent Overstreet84786432013-09-23 23:17:35 -07001144 if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001145 break;
1146
Kent Overstreet84786432013-09-23 23:17:35 -07001147 if (!KEY_SIZE(i->k)) {
1148 sort_key_next(iter, i);
Kent Overstreet911c9612013-07-28 18:35:09 -07001149 heap_sift(iter, i - top, sort_extent_cmp);
Kent Overstreet84786432013-09-23 23:17:35 -07001150 continue;
1151 }
1152
1153 if (top->k > i->k) {
1154 if (bkey_cmp(top->k, i->k) >= 0)
1155 sort_key_next(iter, i);
1156 else
1157 bch_cut_front(top->k, i->k);
1158
Kent Overstreet911c9612013-07-28 18:35:09 -07001159 heap_sift(iter, i - top, sort_extent_cmp);
Kent Overstreet84786432013-09-23 23:17:35 -07001160 } else {
1161 /* can't happen because of comparison func */
1162 BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
Kent Overstreetef71ec02013-12-17 17:51:02 -08001163
1164 if (bkey_cmp(i->k, top->k) < 0) {
1165 bkey_copy(tmp, top->k);
1166
1167 bch_cut_back(&START_KEY(i->k), tmp);
1168 bch_cut_front(i->k, top->k);
1169 heap_sift(iter, 0, btree_iter_cmp);
1170
1171 return tmp;
1172 } else {
1173 bch_cut_back(&START_KEY(i->k), top->k);
1174 }
Kent Overstreet84786432013-09-23 23:17:35 -07001175 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001176 }
Kent Overstreetef71ec02013-12-17 17:51:02 -08001177
1178 return NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001179}
1180
1181static void btree_mergesort(struct btree *b, struct bset *out,
1182 struct btree_iter *iter,
1183 bool fixup, bool remove_stale)
1184{
Kent Overstreet911c9612013-07-28 18:35:09 -07001185 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001186 struct bkey *k, *last = NULL;
Kent Overstreetef71ec02013-12-17 17:51:02 -08001187 BKEY_PADDED(k) tmp;
Kent Overstreet911c9612013-07-28 18:35:09 -07001188 btree_iter_cmp_fn *cmp = b->level
1189 ? sort_cmp
1190 : sort_extent_cmp;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001191 bool (*bad)(struct btree *, const struct bkey *) = remove_stale
1192 ? bch_ptr_bad
1193 : bch_ptr_invalid;
1194
Kent Overstreet911c9612013-07-28 18:35:09 -07001195 /* Heapify the iterator, using our comparison function */
1196 for (i = iter->used / 2 - 1; i >= 0; --i)
1197 heap_sift(iter, i, cmp);
1198
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199 while (!btree_iter_end(iter)) {
1200 if (fixup && !b->level)
Kent Overstreet911c9612013-07-28 18:35:09 -07001201 k = btree_sort_fixup_extents(iter, &tmp.k);
Kent Overstreetef71ec02013-12-17 17:51:02 -08001202 else
1203 k = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001204
Kent Overstreetef71ec02013-12-17 17:51:02 -08001205 if (!k)
Kent Overstreet911c9612013-07-28 18:35:09 -07001206 k = __bch_btree_iter_next(iter, cmp);
Kent Overstreetef71ec02013-12-17 17:51:02 -08001207
Kent Overstreetcafe5632013-03-23 16:11:31 -07001208 if (bad(b, k))
1209 continue;
1210
1211 if (!last) {
1212 last = out->start;
1213 bkey_copy(last, k);
1214 } else if (b->level ||
1215 !bch_bkey_try_merge(b, last, k)) {
1216 last = bkey_next(last);
1217 bkey_copy(last, k);
1218 }
1219 }
1220
1221 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1222
1223 pr_debug("sorted %i keys", out->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001224}
1225
1226static void __btree_sort(struct btree *b, struct btree_iter *iter,
Kent Overstreet67539e82013-09-10 22:53:34 -07001227 unsigned start, unsigned order, bool fixup,
1228 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001229{
1230 uint64_t start_time;
Kent Overstreet0a451142013-12-18 00:01:06 -08001231 bool used_mempool = false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001232 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1233 order);
1234 if (!out) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001235 BUG_ON(order > state->page_order);
1236
1237 out = page_address(mempool_alloc(state->pool, GFP_NOIO));
Kent Overstreet0a451142013-12-18 00:01:06 -08001238 used_mempool = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001239 order = ilog2(bucket_pages(b->c));
1240 }
1241
1242 start_time = local_clock();
1243
Kent Overstreet67539e82013-09-10 22:53:34 -07001244 btree_mergesort(b, out, iter, fixup, false);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001245 b->nsets = start;
1246
Kent Overstreetcafe5632013-03-23 16:11:31 -07001247 if (!start && order == b->page_order) {
1248 /*
1249 * Our temporary buffer is the same size as the btree node's
1250 * buffer, we can just swap buffers instead of doing a big
1251 * memcpy()
1252 */
1253
Kent Overstreet81ab4192013-10-31 15:46:42 -07001254 out->magic = bset_magic(&b->c->sb);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001255 out->seq = b->sets[0].data->seq;
1256 out->version = b->sets[0].data->version;
1257 swap(out, b->sets[0].data);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001258 } else {
1259 b->sets[start].data->keys = out->keys;
1260 memcpy(b->sets[start].data->start, out->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001261 (void *) bset_bkey_last(out) - (void *) out->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001262 }
1263
Kent Overstreet0a451142013-12-18 00:01:06 -08001264 if (used_mempool)
Kent Overstreet67539e82013-09-10 22:53:34 -07001265 mempool_free(virt_to_page(out), state->pool);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001266 else
1267 free_pages((unsigned long) out, order);
1268
Kent Overstreet67539e82013-09-10 22:53:34 -07001269 bset_build_written_tree(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001270
Kent Overstreet65d22e92013-07-31 00:03:54 -07001271 if (!start)
Kent Overstreet67539e82013-09-10 22:53:34 -07001272 bch_time_stats_update(&state->time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001273}
1274
Kent Overstreet67539e82013-09-10 22:53:34 -07001275void bch_btree_sort_partial(struct btree *b, unsigned start,
1276 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001277{
Kent Overstreet280481d2013-10-24 16:36:03 -07001278 size_t order = b->page_order, keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001279 struct btree_iter iter;
Kent Overstreet280481d2013-10-24 16:36:03 -07001280 int oldsize = bch_count_data(b);
1281
Kent Overstreetcafe5632013-03-23 16:11:31 -07001282 __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
1283
Kent Overstreetee811282013-12-17 23:49:49 -08001284 BUG_ON(!bset_written(b, bset_tree_last(b)) &&
1285 (bset_tree_last(b)->size || b->nsets));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001286
1287 if (start) {
1288 unsigned i;
1289
1290 for (i = start; i <= b->nsets; i++)
1291 keys += b->sets[i].data->keys;
1292
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001293 order = roundup_pow_of_two(__set_bytes(b->sets->data,
1294 keys)) / PAGE_SIZE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001295 if (order)
1296 order = ilog2(order);
1297 }
1298
Kent Overstreet67539e82013-09-10 22:53:34 -07001299 __btree_sort(b, &iter, start, order, false, state);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001300
Kent Overstreet280481d2013-10-24 16:36:03 -07001301 EBUG_ON(b->written && oldsize >= 0 && bch_count_data(b) != oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001302}
1303
Kent Overstreet67539e82013-09-10 22:53:34 -07001304void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter,
1305 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001306{
Kent Overstreet67539e82013-09-10 22:53:34 -07001307 __btree_sort(b, iter, 0, b->page_order, true, state);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001308}
1309
Kent Overstreet67539e82013-09-10 22:53:34 -07001310void bch_btree_sort_into(struct btree *b, struct btree *new,
1311 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001312{
1313 uint64_t start_time = local_clock();
1314
1315 struct btree_iter iter;
1316 bch_btree_iter_init(b, &iter, NULL);
1317
1318 btree_mergesort(b, new->sets->data, &iter, false, true);
1319
Kent Overstreet67539e82013-09-10 22:53:34 -07001320 bch_time_stats_update(&state->time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001321
Kent Overstreetcafe5632013-03-23 16:11:31 -07001322 new->sets->size = 0;
1323}
1324
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001325#define SORT_CRIT (4096 / sizeof(uint64_t))
1326
Kent Overstreet67539e82013-09-10 22:53:34 -07001327void bch_btree_sort_lazy(struct btree *b, struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001328{
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001329 unsigned crit = SORT_CRIT;
1330 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001331
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001332 /* Don't sort if nothing to do */
1333 if (!b->nsets)
1334 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001335
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001336 for (i = b->nsets - 1; i >= 0; --i) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001337 crit *= state->crit_factor;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001338
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001339 if (b->sets[i].data->keys < crit) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001340 bch_btree_sort_partial(b, i, state);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001341 return;
1342 }
1343 }
1344
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001345 /* Sort if we'd overflow */
1346 if (b->nsets + 1 == MAX_BSETS) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001347 bch_btree_sort(b, state);
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001348 return;
1349 }
1350
1351out:
Kent Overstreetcafe5632013-03-23 16:11:31 -07001352 bset_build_written_tree(b);
1353}
1354
1355/* Sysfs stuff */
1356
1357struct bset_stats {
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001358 struct btree_op op;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001359 size_t nodes;
1360 size_t sets_written, sets_unwritten;
1361 size_t bytes_written, bytes_unwritten;
1362 size_t floats, failed;
1363};
1364
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001365static int btree_bset_stats(struct btree_op *op, struct btree *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001366{
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001367 struct bset_stats *stats = container_of(op, struct bset_stats, op);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001368 unsigned i;
1369
1370 stats->nodes++;
1371
1372 for (i = 0; i <= b->nsets; i++) {
1373 struct bset_tree *t = &b->sets[i];
1374 size_t bytes = t->data->keys * sizeof(uint64_t);
1375 size_t j;
1376
1377 if (bset_written(b, t)) {
1378 stats->sets_written++;
1379 stats->bytes_written += bytes;
1380
1381 stats->floats += t->size - 1;
1382
1383 for (j = 1; j < t->size; j++)
1384 if (t->tree[j].exponent == 127)
1385 stats->failed++;
1386 } else {
1387 stats->sets_unwritten++;
1388 stats->bytes_unwritten += bytes;
1389 }
1390 }
1391
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001392 return MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001393}
1394
1395int bch_bset_print_stats(struct cache_set *c, char *buf)
1396{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001397 struct bset_stats t;
1398 int ret;
1399
Kent Overstreetcafe5632013-03-23 16:11:31 -07001400 memset(&t, 0, sizeof(struct bset_stats));
Kent Overstreetb54d6932013-07-24 18:04:18 -07001401 bch_btree_op_init(&t.op, -1);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001402
Kent Overstreet48dad8b2013-09-10 18:48:51 -07001403 ret = bch_btree_map_nodes(&t.op, c, &ZERO_KEY, btree_bset_stats);
1404 if (ret < 0)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001405 return ret;
1406
1407 return snprintf(buf, PAGE_SIZE,
1408 "btree nodes: %zu\n"
1409 "written sets: %zu\n"
1410 "unwritten sets: %zu\n"
1411 "written key bytes: %zu\n"
1412 "unwritten key bytes: %zu\n"
1413 "floats: %zu\n"
1414 "failed: %zu\n",
1415 t.nodes,
1416 t.sets_written, t.sets_unwritten,
1417 t.bytes_written, t.bytes_unwritten,
1418 t.floats, t.failed);
1419}