blob: 448cff8cc052ff98f2767ebc48b72ae19ecbfd37 [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"
Kent Overstreetcafe5632013-03-23 16:11:31 -070010
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080011#include <linux/console.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070012#include <linux/random.h>
Geert Uytterhoevencd953ed2013-03-27 18:56:28 +010013#include <linux/prefetch.h>
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
Kent Overstreetdc9d98d2013-12-17 23:47:33 -080015#ifdef CONFIG_BCACHE_DEBUG
16
17void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned set)
18{
19 struct bkey *k, *next;
20
21 for (k = i->start; k < bset_bkey_last(i); k = next) {
22 next = bkey_next(k);
23
24 printk(KERN_ERR "block %u key %zi/%u: ", set,
25 (uint64_t *) k - i->d, i->keys);
26
27 if (b->ops->key_dump)
28 b->ops->key_dump(b, k);
29 else
30 printk("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
31
32 if (next < bset_bkey_last(i) &&
33 bkey_cmp(k, b->ops->is_extents ?
34 &START_KEY(next) : next) > 0)
35 printk(KERN_ERR "Key skipped backwards\n");
36 }
37}
38
39void bch_dump_bucket(struct btree_keys *b)
40{
41 unsigned i;
42
43 console_lock();
44 for (i = 0; i <= b->nsets; i++)
45 bch_dump_bset(b, b->set[i].data,
46 bset_sector_offset(b, b->set[i].data));
47 console_unlock();
48}
49
50int __bch_count_data(struct btree_keys *b)
51{
52 unsigned ret = 0;
53 struct btree_iter iter;
54 struct bkey *k;
55
56 if (b->ops->is_extents)
57 for_each_key(b, k, &iter)
58 ret += KEY_SIZE(k);
59 return ret;
60}
61
62void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)
63{
64 va_list args;
65 struct bkey *k, *p = NULL;
66 struct btree_iter iter;
67 const char *err;
68
69 for_each_key(b, k, &iter) {
70 if (b->ops->is_extents) {
71 err = "Keys out of order";
72 if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
73 goto bug;
74
75 if (bch_ptr_invalid(b, k))
76 continue;
77
78 err = "Overlapping keys";
79 if (p && bkey_cmp(p, &START_KEY(k)) > 0)
80 goto bug;
81 } else {
82 if (bch_ptr_bad(b, k))
83 continue;
84
85 err = "Duplicate keys";
86 if (p && !bkey_cmp(p, k))
87 goto bug;
88 }
89 p = k;
90 }
91#if 0
92 err = "Key larger than btree node key";
93 if (p && bkey_cmp(p, &b->key) > 0)
94 goto bug;
95#endif
96 return;
97bug:
98 bch_dump_bucket(b);
99
100 va_start(args, fmt);
101 vprintk(fmt, args);
102 va_end(args);
103
104 panic("bch_check_keys error: %s:\n", err);
105}
106
107static void bch_btree_iter_next_check(struct btree_iter *iter)
108{
109 struct bkey *k = iter->data->k, *next = bkey_next(k);
110
111 if (next < iter->data->end &&
112 bkey_cmp(k, iter->b->ops->is_extents ?
113 &START_KEY(next) : next) > 0) {
114 bch_dump_bucket(iter->b);
115 panic("Key skipped backwards\n");
116 }
117}
118
119#else
120
121static inline void bch_btree_iter_next_check(struct btree_iter *iter) {}
122
123#endif
124
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125/* Keylists */
126
Kent Overstreet085d2a32013-11-11 18:20:51 -0800127int __bch_keylist_realloc(struct keylist *l, unsigned u64s)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700128{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700129 size_t oldsize = bch_keylist_nkeys(l);
Kent Overstreet085d2a32013-11-11 18:20:51 -0800130 size_t newsize = oldsize + u64s;
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700131 uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
132 uint64_t *new_keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700133
Kent Overstreetcafe5632013-03-23 16:11:31 -0700134 newsize = roundup_pow_of_two(newsize);
135
136 if (newsize <= KEYLIST_INLINE ||
137 roundup_pow_of_two(oldsize) == newsize)
138 return 0;
139
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700140 new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700141
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700142 if (!new_keys)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700143 return -ENOMEM;
144
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700145 if (!old_keys)
146 memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700147
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700148 l->keys_p = new_keys;
149 l->top_p = new_keys + oldsize;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700150
151 return 0;
152}
153
154struct bkey *bch_keylist_pop(struct keylist *l)
155{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700156 struct bkey *k = l->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700157
158 if (k == l->top)
159 return NULL;
160
161 while (bkey_next(k) != l->top)
162 k = bkey_next(k);
163
164 return l->top = k;
165}
166
Kent Overstreet26c949f2013-09-10 18:41:15 -0700167void bch_keylist_pop_front(struct keylist *l)
168{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700169 l->top_p -= bkey_u64s(l->keys);
Kent Overstreet26c949f2013-09-10 18:41:15 -0700170
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700171 memmove(l->keys,
172 bkey_next(l->keys),
173 bch_keylist_bytes(l));
Kent Overstreet26c949f2013-09-10 18:41:15 -0700174}
175
Kent Overstreetcafe5632013-03-23 16:11:31 -0700176/* Key/pointer manipulation */
177
178void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
179 unsigned i)
180{
181 BUG_ON(i > KEY_PTRS(src));
182
183 /* Only copy the header, key, and one pointer. */
184 memcpy(dest, src, 2 * sizeof(uint64_t));
185 dest->ptr[0] = src->ptr[i];
186 SET_KEY_PTRS(dest, 1);
187 /* We didn't copy the checksum so clear that bit. */
188 SET_KEY_CSUM(dest, 0);
189}
190
191bool __bch_cut_front(const struct bkey *where, struct bkey *k)
192{
193 unsigned i, len = 0;
194
195 if (bkey_cmp(where, &START_KEY(k)) <= 0)
196 return false;
197
198 if (bkey_cmp(where, k) < 0)
199 len = KEY_OFFSET(k) - KEY_OFFSET(where);
200 else
201 bkey_copy_key(k, where);
202
203 for (i = 0; i < KEY_PTRS(k); i++)
204 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
205
206 BUG_ON(len > KEY_SIZE(k));
207 SET_KEY_SIZE(k, len);
208 return true;
209}
210
211bool __bch_cut_back(const struct bkey *where, struct bkey *k)
212{
213 unsigned len = 0;
214
215 if (bkey_cmp(where, k) >= 0)
216 return false;
217
218 BUG_ON(KEY_INODE(where) != KEY_INODE(k));
219
220 if (bkey_cmp(where, &START_KEY(k)) > 0)
221 len = KEY_OFFSET(where) - KEY_START(k);
222
223 bkey_copy_key(k, where);
224
225 BUG_ON(len > KEY_SIZE(k));
226 SET_KEY_SIZE(k, len);
227 return true;
228}
229
Kent Overstreetee811282013-12-17 23:49:49 -0800230/* Auxiliary search trees */
231
232/* 32 bits total: */
233#define BKEY_MID_BITS 3
234#define BKEY_EXPONENT_BITS 7
235#define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
236#define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1)
237
238struct bkey_float {
239 unsigned exponent:BKEY_EXPONENT_BITS;
240 unsigned m:BKEY_MID_BITS;
241 unsigned mantissa:BKEY_MANTISSA_BITS;
242} __packed;
243
244/*
245 * BSET_CACHELINE was originally intended to match the hardware cacheline size -
246 * it used to be 64, but I realized the lookup code would touch slightly less
247 * memory if it was 128.
248 *
249 * It definites the number of bytes (in struct bset) per struct bkey_float in
250 * the auxiliar search tree - when we're done searching the bset_float tree we
251 * have this many bytes left that we do a linear search over.
252 *
253 * Since (after level 5) every level of the bset_tree is on a new cacheline,
254 * we're touching one fewer cacheline in the bset tree in exchange for one more
255 * cacheline in the linear search - but the linear search might stop before it
256 * gets to the second cacheline.
257 */
258
259#define BSET_CACHELINE 128
260
261/* Space required for the btree node keys */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800262static inline size_t btree_keys_bytes(struct btree_keys *b)
Kent Overstreetee811282013-12-17 23:49:49 -0800263{
264 return PAGE_SIZE << b->page_order;
265}
266
Kent Overstreeta85e9682013-12-20 17:28:16 -0800267static inline size_t btree_keys_cachelines(struct btree_keys *b)
Kent Overstreetee811282013-12-17 23:49:49 -0800268{
269 return btree_keys_bytes(b) / BSET_CACHELINE;
270}
271
272/* Space required for the auxiliary search trees */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800273static inline size_t bset_tree_bytes(struct btree_keys *b)
Kent Overstreetee811282013-12-17 23:49:49 -0800274{
275 return btree_keys_cachelines(b) * sizeof(struct bkey_float);
276}
277
278/* Space required for the prev pointers */
Kent Overstreeta85e9682013-12-20 17:28:16 -0800279static inline size_t bset_prev_bytes(struct btree_keys *b)
Kent Overstreetee811282013-12-17 23:49:49 -0800280{
281 return btree_keys_cachelines(b) * sizeof(uint8_t);
282}
283
284/* Memory allocation */
285
Kent Overstreeta85e9682013-12-20 17:28:16 -0800286void bch_btree_keys_free(struct btree_keys *b)
Kent Overstreetee811282013-12-17 23:49:49 -0800287{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800288 struct bset_tree *t = b->set;
Kent Overstreetee811282013-12-17 23:49:49 -0800289
290 if (bset_prev_bytes(b) < PAGE_SIZE)
291 kfree(t->prev);
292 else
293 free_pages((unsigned long) t->prev,
294 get_order(bset_prev_bytes(b)));
295
296 if (bset_tree_bytes(b) < PAGE_SIZE)
297 kfree(t->tree);
298 else
299 free_pages((unsigned long) t->tree,
300 get_order(bset_tree_bytes(b)));
301
302 free_pages((unsigned long) t->data, b->page_order);
303
304 t->prev = NULL;
305 t->tree = NULL;
306 t->data = NULL;
307}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800308EXPORT_SYMBOL(bch_btree_keys_free);
Kent Overstreetee811282013-12-17 23:49:49 -0800309
Kent Overstreeta85e9682013-12-20 17:28:16 -0800310int bch_btree_keys_alloc(struct btree_keys *b, unsigned page_order, gfp_t gfp)
Kent Overstreetee811282013-12-17 23:49:49 -0800311{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800312 struct bset_tree *t = b->set;
Kent Overstreetee811282013-12-17 23:49:49 -0800313
314 BUG_ON(t->data);
315
316 b->page_order = page_order;
317
318 t->data = (void *) __get_free_pages(gfp, b->page_order);
319 if (!t->data)
320 goto err;
321
322 t->tree = bset_tree_bytes(b) < PAGE_SIZE
323 ? kmalloc(bset_tree_bytes(b), gfp)
324 : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
325 if (!t->tree)
326 goto err;
327
328 t->prev = bset_prev_bytes(b) < PAGE_SIZE
329 ? kmalloc(bset_prev_bytes(b), gfp)
330 : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
331 if (!t->prev)
332 goto err;
333
334 return 0;
335err:
336 bch_btree_keys_free(b);
337 return -ENOMEM;
338}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800339EXPORT_SYMBOL(bch_btree_keys_alloc);
340
341void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
342 bool *expensive_debug_checks)
343{
344 unsigned i;
345
346 b->ops = ops;
347 b->expensive_debug_checks = expensive_debug_checks;
348 b->nsets = 0;
349 b->last_set_unwritten = 0;
350
351 /* XXX: shouldn't be needed */
352 for (i = 0; i < MAX_BSETS; i++)
353 b->set[i].size = 0;
354 /*
355 * Second loop starts at 1 because b->keys[0]->data is the memory we
356 * allocated
357 */
358 for (i = 1; i < MAX_BSETS; i++)
359 b->set[i].data = NULL;
360}
361EXPORT_SYMBOL(bch_btree_keys_init);
Kent Overstreetee811282013-12-17 23:49:49 -0800362
Kent Overstreetcafe5632013-03-23 16:11:31 -0700363/* Binary tree stuff for auxiliary search trees */
364
365static unsigned inorder_next(unsigned j, unsigned size)
366{
367 if (j * 2 + 1 < size) {
368 j = j * 2 + 1;
369
370 while (j * 2 < size)
371 j *= 2;
372 } else
373 j >>= ffz(j) + 1;
374
375 return j;
376}
377
378static unsigned inorder_prev(unsigned j, unsigned size)
379{
380 if (j * 2 < size) {
381 j = j * 2;
382
383 while (j * 2 + 1 < size)
384 j = j * 2 + 1;
385 } else
386 j >>= ffs(j);
387
388 return j;
389}
390
391/* I have no idea why this code works... and I'm the one who wrote it
392 *
393 * However, I do know what it does:
394 * Given a binary tree constructed in an array (i.e. how you normally implement
395 * a heap), it converts a node in the tree - referenced by array index - to the
396 * index it would have if you did an inorder traversal.
397 *
398 * Also tested for every j, size up to size somewhere around 6 million.
399 *
400 * The binary tree starts at array index 1, not 0
401 * extra is a function of size:
402 * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
403 */
404static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
405{
406 unsigned b = fls(j);
407 unsigned shift = fls(size - 1) - b;
408
409 j ^= 1U << (b - 1);
410 j <<= 1;
411 j |= 1;
412 j <<= shift;
413
414 if (j > extra)
415 j -= (j - extra) >> 1;
416
417 return j;
418}
419
420static unsigned to_inorder(unsigned j, struct bset_tree *t)
421{
422 return __to_inorder(j, t->size, t->extra);
423}
424
425static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
426{
427 unsigned shift;
428
429 if (j > extra)
430 j += j - extra;
431
432 shift = ffs(j);
433
434 j >>= shift;
435 j |= roundup_pow_of_two(size) >> shift;
436
437 return j;
438}
439
440static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
441{
442 return __inorder_to_tree(j, t->size, t->extra);
443}
444
445#if 0
446void inorder_test(void)
447{
448 unsigned long done = 0;
449 ktime_t start = ktime_get();
450
451 for (unsigned size = 2;
452 size < 65536000;
453 size++) {
454 unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
455 unsigned i = 1, j = rounddown_pow_of_two(size - 1);
456
457 if (!(size % 4096))
458 printk(KERN_NOTICE "loop %u, %llu per us\n", size,
459 done / ktime_us_delta(ktime_get(), start));
460
461 while (1) {
462 if (__inorder_to_tree(i, size, extra) != j)
463 panic("size %10u j %10u i %10u", size, j, i);
464
465 if (__to_inorder(j, size, extra) != i)
466 panic("size %10u j %10u i %10u", size, j, i);
467
468 if (j == rounddown_pow_of_two(size) - 1)
469 break;
470
471 BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
472
473 j = inorder_next(j, size);
474 i++;
475 }
476
477 done += size - 1;
478 }
479}
480#endif
481
482/*
Phil Viana48a73022013-06-03 09:51:42 -0300483 * Cacheline/offset <-> bkey pointer arithmetic:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700484 *
485 * t->tree is a binary search tree in an array; each node corresponds to a key
486 * in one cacheline in t->set (BSET_CACHELINE bytes).
487 *
488 * This means we don't have to store the full index of the key that a node in
489 * the binary tree points to; to_inorder() gives us the cacheline, and then
490 * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
491 *
Phil Viana48a73022013-06-03 09:51:42 -0300492 * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
Kent Overstreetcafe5632013-03-23 16:11:31 -0700493 * make this work.
494 *
495 * To construct the bfloat for an arbitrary key we need to know what the key
496 * immediately preceding it is: we have to check if the two keys differ in the
497 * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
498 * of the previous key so we can walk backwards to it from t->tree[j]'s key.
499 */
500
501static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
502 unsigned offset)
503{
504 return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
505}
506
507static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
508{
509 return ((void *) k - (void *) t->data) / BSET_CACHELINE;
510}
511
512static unsigned bkey_to_cacheline_offset(struct bkey *k)
513{
514 return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
515}
516
517static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
518{
519 return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
520}
521
522static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
523{
524 return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
525}
526
527/*
528 * For the write set - the one we're currently inserting keys into - we don't
529 * maintain a full search tree, we just keep a simple lookup table in t->prev.
530 */
531static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
532{
533 return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
534}
535
536static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
537{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700538 low >>= shift;
539 low |= (high << 1) << (63U - shift);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700540 return low;
541}
542
543static inline unsigned bfloat_mantissa(const struct bkey *k,
544 struct bkey_float *f)
545{
546 const uint64_t *p = &k->low - (f->exponent >> 6);
547 return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
548}
549
550static void make_bfloat(struct bset_tree *t, unsigned j)
551{
552 struct bkey_float *f = &t->tree[j];
553 struct bkey *m = tree_to_bkey(t, j);
554 struct bkey *p = tree_to_prev_bkey(t, j);
555
556 struct bkey *l = is_power_of_2(j)
557 ? t->data->start
558 : tree_to_prev_bkey(t, j >> ffs(j));
559
560 struct bkey *r = is_power_of_2(j + 1)
Kent Overstreetfafff812013-12-17 21:56:21 -0800561 ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700562 : tree_to_bkey(t, j >> (ffz(j) + 1));
563
564 BUG_ON(m < l || m > r);
565 BUG_ON(bkey_next(p) != m);
566
567 if (KEY_INODE(l) != KEY_INODE(r))
568 f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
569 else
570 f->exponent = fls64(r->low ^ l->low);
571
572 f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
573
574 /*
575 * Setting f->exponent = 127 flags this node as failed, and causes the
576 * lookup code to fall back to comparing against the original key.
577 */
578
579 if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
580 f->mantissa = bfloat_mantissa(m, f) - 1;
581 else
582 f->exponent = 127;
583}
584
Kent Overstreeta85e9682013-12-20 17:28:16 -0800585static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700586{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800587 if (t != b->set) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700588 unsigned j = roundup(t[-1].size,
589 64 / sizeof(struct bkey_float));
590
591 t->tree = t[-1].tree + j;
592 t->prev = t[-1].prev + j;
593 }
594
Kent Overstreeta85e9682013-12-20 17:28:16 -0800595 while (t < b->set + MAX_BSETS)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700596 t++->size = 0;
597}
598
Kent Overstreeta85e9682013-12-20 17:28:16 -0800599static void bch_bset_build_unwritten_tree(struct btree_keys *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700600{
Kent Overstreetee811282013-12-17 23:49:49 -0800601 struct bset_tree *t = bset_tree_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700602
Kent Overstreeta85e9682013-12-20 17:28:16 -0800603 BUG_ON(b->last_set_unwritten);
604 b->last_set_unwritten = 1;
605
Kent Overstreetcafe5632013-03-23 16:11:31 -0700606 bset_alloc_tree(b, t);
607
Kent Overstreeta85e9682013-12-20 17:28:16 -0800608 if (t->tree != b->set->tree + btree_keys_cachelines(b)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700609 t->prev[0] = bkey_to_cacheline_offset(t->data->start);
610 t->size = 1;
611 }
612}
613
Kent Overstreeta85e9682013-12-20 17:28:16 -0800614void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic)
Kent Overstreetee811282013-12-17 23:49:49 -0800615{
Kent Overstreeta85e9682013-12-20 17:28:16 -0800616 if (i != b->set->data) {
617 b->set[++b->nsets].data = i;
618 i->seq = b->set->data->seq;
Kent Overstreetee811282013-12-17 23:49:49 -0800619 } else
620 get_random_bytes(&i->seq, sizeof(uint64_t));
621
622 i->magic = magic;
623 i->version = 0;
624 i->keys = 0;
625
626 bch_bset_build_unwritten_tree(b);
627}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800628EXPORT_SYMBOL(bch_bset_init_next);
Kent Overstreetee811282013-12-17 23:49:49 -0800629
Kent Overstreeta85e9682013-12-20 17:28:16 -0800630void bch_bset_build_written_tree(struct btree_keys *b)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700631{
Kent Overstreetee811282013-12-17 23:49:49 -0800632 struct bset_tree *t = bset_tree_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700633 struct bkey *k = t->data->start;
634 unsigned j, cacheline = 1;
635
Kent Overstreeta85e9682013-12-20 17:28:16 -0800636 b->last_set_unwritten = 0;
637
Kent Overstreetcafe5632013-03-23 16:11:31 -0700638 bset_alloc_tree(b, t);
639
640 t->size = min_t(unsigned,
Kent Overstreetfafff812013-12-17 21:56:21 -0800641 bkey_to_cacheline(t, bset_bkey_last(t->data)),
Kent Overstreeta85e9682013-12-20 17:28:16 -0800642 b->set->tree + btree_keys_cachelines(b) - t->tree);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700643
644 if (t->size < 2) {
645 t->size = 0;
646 return;
647 }
648
649 t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
650
651 /* First we figure out where the first key in each cacheline is */
652 for (j = inorder_next(0, t->size);
653 j;
654 j = inorder_next(j, t->size)) {
655 while (bkey_to_cacheline(t, k) != cacheline)
656 k = bkey_next(k);
657
658 t->prev[j] = bkey_u64s(k);
659 k = bkey_next(k);
660 cacheline++;
661 t->tree[j].m = bkey_to_cacheline_offset(k);
662 }
663
Kent Overstreetfafff812013-12-17 21:56:21 -0800664 while (bkey_next(k) != bset_bkey_last(t->data))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700665 k = bkey_next(k);
666
667 t->end = *k;
668
669 /* Then we build the tree */
670 for (j = inorder_next(0, t->size);
671 j;
672 j = inorder_next(j, t->size))
673 make_bfloat(t, j);
674}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800675EXPORT_SYMBOL(bch_bset_build_written_tree);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700676
Kent Overstreeta85e9682013-12-20 17:28:16 -0800677void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700678{
679 struct bset_tree *t;
680 unsigned inorder, j = 1;
681
Kent Overstreeta85e9682013-12-20 17:28:16 -0800682 for (t = b->set; t <= bset_tree_last(b); t++)
Kent Overstreetfafff812013-12-17 21:56:21 -0800683 if (k < bset_bkey_last(t->data))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700684 goto found_set;
685
686 BUG();
687found_set:
688 if (!t->size || !bset_written(b, t))
689 return;
690
691 inorder = bkey_to_cacheline(t, k);
692
693 if (k == t->data->start)
694 goto fix_left;
695
Kent Overstreetfafff812013-12-17 21:56:21 -0800696 if (bkey_next(k) == bset_bkey_last(t->data)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700697 t->end = *k;
698 goto fix_right;
699 }
700
701 j = inorder_to_tree(inorder, t);
702
703 if (j &&
704 j < t->size &&
705 k == tree_to_bkey(t, j))
706fix_left: do {
707 make_bfloat(t, j);
708 j = j * 2;
709 } while (j < t->size);
710
711 j = inorder_to_tree(inorder + 1, t);
712
713 if (j &&
714 j < t->size &&
715 k == tree_to_prev_bkey(t, j))
716fix_right: do {
717 make_bfloat(t, j);
718 j = j * 2 + 1;
719 } while (j < t->size);
720}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800721EXPORT_SYMBOL(bch_bset_fix_invalidated_key);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700722
Kent Overstreeta85e9682013-12-20 17:28:16 -0800723static void bch_bset_fix_lookup_table(struct btree_keys *b,
Kent Overstreetee811282013-12-17 23:49:49 -0800724 struct bset_tree *t,
725 struct bkey *k)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700726{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700727 unsigned shift = bkey_u64s(k);
728 unsigned j = bkey_to_cacheline(t, k);
729
730 /* We're getting called from btree_split() or btree_gc, just bail out */
731 if (!t->size)
732 return;
733
734 /* k is the key we just inserted; we need to find the entry in the
735 * lookup table for the first key that is strictly greater than k:
736 * it's either k's cacheline or the next one
737 */
738 if (j < t->size &&
739 table_to_bkey(t, j) <= k)
740 j++;
741
742 /* Adjust all the lookup table entries, and find a new key for any that
743 * have gotten too big
744 */
745 for (; j < t->size; j++) {
746 t->prev[j] += shift;
747
748 if (t->prev[j] > 7) {
749 k = table_to_bkey(t, j - 1);
750
751 while (k < cacheline_to_bkey(t, j, 0))
752 k = bkey_next(k);
753
754 t->prev[j] = bkey_to_cacheline_offset(k);
755 }
756 }
757
Kent Overstreeta85e9682013-12-20 17:28:16 -0800758 if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700759 return;
760
761 /* Possibly add a new entry to the end of the lookup table */
762
763 for (k = table_to_bkey(t, t->size - 1);
Kent Overstreetfafff812013-12-17 21:56:21 -0800764 k != bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700765 k = bkey_next(k))
766 if (t->size == bkey_to_cacheline(t, k)) {
767 t->prev[t->size] = bkey_to_cacheline_offset(k);
768 t->size++;
769 }
770}
771
Kent Overstreeta85e9682013-12-20 17:28:16 -0800772void bch_bset_insert(struct btree_keys *b, struct bkey *where,
Kent Overstreetee811282013-12-17 23:49:49 -0800773 struct bkey *insert)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700774{
Kent Overstreetee811282013-12-17 23:49:49 -0800775 struct bset_tree *t = bset_tree_last(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700776
Kent Overstreeta85e9682013-12-20 17:28:16 -0800777 BUG_ON(!b->last_set_unwritten);
Kent Overstreetee811282013-12-17 23:49:49 -0800778 BUG_ON(bset_byte_offset(b, t->data) +
779 __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
780 PAGE_SIZE << b->page_order);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700781
Kent Overstreetee811282013-12-17 23:49:49 -0800782 memmove((uint64_t *) where + bkey_u64s(insert),
783 where,
784 (void *) bset_bkey_last(t->data) - (void *) where);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700785
Kent Overstreetee811282013-12-17 23:49:49 -0800786 t->data->keys += bkey_u64s(insert);
787 bkey_copy(where, insert);
788 bch_bset_fix_lookup_table(b, t, where);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700789}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800790EXPORT_SYMBOL(bch_bset_insert);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700791
792struct bset_search_iter {
793 struct bkey *l, *r;
794};
795
Kent Overstreeta85e9682013-12-20 17:28:16 -0800796static struct bset_search_iter bset_search_write_set(struct bset_tree *t,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700797 const struct bkey *search)
798{
799 unsigned li = 0, ri = t->size;
800
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801 while (li + 1 != ri) {
802 unsigned m = (li + ri) >> 1;
803
804 if (bkey_cmp(table_to_bkey(t, m), search) > 0)
805 ri = m;
806 else
807 li = m;
808 }
809
810 return (struct bset_search_iter) {
811 table_to_bkey(t, li),
Kent Overstreetfafff812013-12-17 21:56:21 -0800812 ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700813 };
814}
815
Kent Overstreeta85e9682013-12-20 17:28:16 -0800816static struct bset_search_iter bset_search_tree(struct bset_tree *t,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700817 const struct bkey *search)
818{
819 struct bkey *l, *r;
820 struct bkey_float *f;
821 unsigned inorder, j, n = 1;
822
823 do {
824 unsigned p = n << 4;
825 p &= ((int) (p - t->size)) >> 31;
826
827 prefetch(&t->tree[p]);
828
829 j = n;
830 f = &t->tree[j];
831
832 /*
833 * n = (f->mantissa > bfloat_mantissa())
834 * ? j * 2
835 * : j * 2 + 1;
836 *
837 * We need to subtract 1 from f->mantissa for the sign bit trick
838 * to work - that's done in make_bfloat()
839 */
840 if (likely(f->exponent != 127))
841 n = j * 2 + (((unsigned)
842 (f->mantissa -
843 bfloat_mantissa(search, f))) >> 31);
844 else
845 n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
846 ? j * 2
847 : j * 2 + 1;
848 } while (n < t->size);
849
850 inorder = to_inorder(j, t);
851
852 /*
853 * n would have been the node we recursed to - the low bit tells us if
854 * we recursed left or recursed right.
855 */
856 if (n & 1) {
857 l = cacheline_to_bkey(t, inorder, f->m);
858
859 if (++inorder != t->size) {
860 f = &t->tree[inorder_next(j, t->size)];
861 r = cacheline_to_bkey(t, inorder, f->m);
862 } else
Kent Overstreetfafff812013-12-17 21:56:21 -0800863 r = bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700864 } else {
865 r = cacheline_to_bkey(t, inorder, f->m);
866
867 if (--inorder) {
868 f = &t->tree[inorder_prev(j, t->size)];
869 l = cacheline_to_bkey(t, inorder, f->m);
870 } else
871 l = t->data->start;
872 }
873
874 return (struct bset_search_iter) {l, r};
875}
876
Kent Overstreetc052dd92013-11-11 17:35:24 -0800877struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700878 const struct bkey *search)
879{
880 struct bset_search_iter i;
881
882 /*
883 * First, we search for a cacheline, then lastly we do a linear search
884 * within that cacheline.
885 *
886 * To search for the cacheline, there's three different possibilities:
887 * * The set is too small to have a search tree, so we just do a linear
888 * search over the whole set.
889 * * The set is the one we're currently inserting into; keeping a full
890 * auxiliary search tree up to date would be too expensive, so we
891 * use a much simpler lookup table to do a binary search -
892 * bset_search_write_set().
893 * * Or we use the auxiliary search tree we constructed earlier -
894 * bset_search_tree()
895 */
896
897 if (unlikely(!t->size)) {
898 i.l = t->data->start;
Kent Overstreetfafff812013-12-17 21:56:21 -0800899 i.r = bset_bkey_last(t->data);
Kent Overstreetc052dd92013-11-11 17:35:24 -0800900 } else if (bset_written(b, t)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700901 /*
902 * Each node in the auxiliary search tree covers a certain range
903 * of bits, and keys above and below the set it covers might
904 * differ outside those bits - so we have to special case the
905 * start and end - handle that here:
906 */
907
908 if (unlikely(bkey_cmp(search, &t->end) >= 0))
Kent Overstreetfafff812013-12-17 21:56:21 -0800909 return bset_bkey_last(t->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700910
911 if (unlikely(bkey_cmp(search, t->data->start) < 0))
912 return t->data->start;
913
Kent Overstreeta85e9682013-12-20 17:28:16 -0800914 i = bset_search_tree(t, search);
915 } else {
Kent Overstreetc052dd92013-11-11 17:35:24 -0800916 BUG_ON(!b->nsets &&
Kent Overstreeta85e9682013-12-20 17:28:16 -0800917 t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
918
919 i = bset_search_write_set(t, search);
920 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700921
Kent Overstreetc052dd92013-11-11 17:35:24 -0800922 if (btree_keys_expensive_checks(b)) {
923 BUG_ON(bset_written(b, t) &&
Kent Overstreet280481d2013-10-24 16:36:03 -0700924 i.l != t->data->start &&
925 bkey_cmp(tree_to_prev_bkey(t,
926 inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
927 search) > 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700928
Kent Overstreetfafff812013-12-17 21:56:21 -0800929 BUG_ON(i.r != bset_bkey_last(t->data) &&
Kent Overstreet280481d2013-10-24 16:36:03 -0700930 bkey_cmp(i.r, search) <= 0);
931 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700932
933 while (likely(i.l != i.r) &&
934 bkey_cmp(i.l, search) <= 0)
935 i.l = bkey_next(i.l);
936
937 return i.l;
938}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800939EXPORT_SYMBOL(__bch_bset_search);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700940
941/* Btree iterator */
942
Kent Overstreet911c9612013-07-28 18:35:09 -0700943typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
944 struct btree_iter_set);
945
Kent Overstreetcafe5632013-03-23 16:11:31 -0700946static inline bool btree_iter_cmp(struct btree_iter_set l,
947 struct btree_iter_set r)
948{
Kent Overstreet911c9612013-07-28 18:35:09 -0700949 return bkey_cmp(l.k, r.k) > 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700950}
951
952static inline bool btree_iter_end(struct btree_iter *iter)
953{
954 return !iter->used;
955}
956
957void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
958 struct bkey *end)
959{
960 if (k != end)
961 BUG_ON(!heap_add(iter,
962 ((struct btree_iter_set) { k, end }),
963 btree_iter_cmp));
964}
965
Kent Overstreetc052dd92013-11-11 17:35:24 -0800966static struct bkey *__bch_btree_iter_init(struct btree_keys *b,
Kent Overstreet911c9612013-07-28 18:35:09 -0700967 struct btree_iter *iter,
968 struct bkey *search,
969 struct bset_tree *start)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700970{
971 struct bkey *ret = NULL;
972 iter->size = ARRAY_SIZE(iter->data);
973 iter->used = 0;
974
Kent Overstreet280481d2013-10-24 16:36:03 -0700975#ifdef CONFIG_BCACHE_DEBUG
976 iter->b = b;
977#endif
978
Kent Overstreetc052dd92013-11-11 17:35:24 -0800979 for (; start <= bset_tree_last(b); start++) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700980 ret = bch_bset_search(b, start, search);
Kent Overstreetfafff812013-12-17 21:56:21 -0800981 bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700982 }
983
984 return ret;
985}
986
Kent Overstreetc052dd92013-11-11 17:35:24 -0800987struct bkey *bch_btree_iter_init(struct btree_keys *b,
Kent Overstreet911c9612013-07-28 18:35:09 -0700988 struct btree_iter *iter,
989 struct bkey *search)
990{
Kent Overstreetc052dd92013-11-11 17:35:24 -0800991 return __bch_btree_iter_init(b, iter, search, b->set);
Kent Overstreet911c9612013-07-28 18:35:09 -0700992}
Kent Overstreeta85e9682013-12-20 17:28:16 -0800993EXPORT_SYMBOL(bch_btree_iter_init);
Kent Overstreet911c9612013-07-28 18:35:09 -0700994
995static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
996 btree_iter_cmp_fn *cmp)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700997{
998 struct btree_iter_set unused;
999 struct bkey *ret = NULL;
1000
1001 if (!btree_iter_end(iter)) {
Kent Overstreet280481d2013-10-24 16:36:03 -07001002 bch_btree_iter_next_check(iter);
1003
Kent Overstreetcafe5632013-03-23 16:11:31 -07001004 ret = iter->data->k;
1005 iter->data->k = bkey_next(iter->data->k);
1006
1007 if (iter->data->k > iter->data->end) {
Kent Overstreetcc0f4ea2013-03-27 12:47:45 -07001008 WARN_ONCE(1, "bset was corrupt!\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -07001009 iter->data->k = iter->data->end;
1010 }
1011
1012 if (iter->data->k == iter->data->end)
Kent Overstreet911c9612013-07-28 18:35:09 -07001013 heap_pop(iter, unused, cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001014 else
Kent Overstreet911c9612013-07-28 18:35:09 -07001015 heap_sift(iter, 0, cmp);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001016 }
1017
1018 return ret;
1019}
1020
Kent Overstreet911c9612013-07-28 18:35:09 -07001021struct bkey *bch_btree_iter_next(struct btree_iter *iter)
1022{
1023 return __bch_btree_iter_next(iter, btree_iter_cmp);
1024
1025}
Kent Overstreeta85e9682013-12-20 17:28:16 -08001026EXPORT_SYMBOL(bch_btree_iter_next);
Kent Overstreet911c9612013-07-28 18:35:09 -07001027
Kent Overstreetcafe5632013-03-23 16:11:31 -07001028struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
Kent Overstreeta85e9682013-12-20 17:28:16 -08001029 struct btree_keys *b, ptr_filter_fn fn)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001030{
1031 struct bkey *ret;
1032
1033 do {
1034 ret = bch_btree_iter_next(iter);
1035 } while (ret && fn(b, ret));
1036
1037 return ret;
1038}
1039
Kent Overstreetcafe5632013-03-23 16:11:31 -07001040/* Mergesort */
1041
Kent Overstreet67539e82013-09-10 22:53:34 -07001042void bch_bset_sort_state_free(struct bset_sort_state *state)
1043{
1044 if (state->pool)
1045 mempool_destroy(state->pool);
1046}
1047
1048int bch_bset_sort_state_init(struct bset_sort_state *state, unsigned page_order)
1049{
1050 spin_lock_init(&state->time.lock);
1051
1052 state->page_order = page_order;
1053 state->crit_factor = int_sqrt(1 << page_order);
1054
1055 state->pool = mempool_create_page_pool(1, page_order);
1056 if (!state->pool)
1057 return -ENOMEM;
1058
1059 return 0;
1060}
Kent Overstreeta85e9682013-12-20 17:28:16 -08001061EXPORT_SYMBOL(bch_bset_sort_state_init);
Kent Overstreet67539e82013-09-10 22:53:34 -07001062
Kent Overstreeta85e9682013-12-20 17:28:16 -08001063static void btree_mergesort(struct btree_keys *b, struct bset *out,
Kent Overstreetcafe5632013-03-23 16:11:31 -07001064 struct btree_iter *iter,
1065 bool fixup, bool remove_stale)
1066{
Kent Overstreet911c9612013-07-28 18:35:09 -07001067 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001068 struct bkey *k, *last = NULL;
Kent Overstreetef71ec02013-12-17 17:51:02 -08001069 BKEY_PADDED(k) tmp;
Kent Overstreeta85e9682013-12-20 17:28:16 -08001070 bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale
Kent Overstreetcafe5632013-03-23 16:11:31 -07001071 ? bch_ptr_bad
1072 : bch_ptr_invalid;
1073
Kent Overstreet911c9612013-07-28 18:35:09 -07001074 /* Heapify the iterator, using our comparison function */
1075 for (i = iter->used / 2 - 1; i >= 0; --i)
Kent Overstreet65d45232013-12-20 17:22:05 -08001076 heap_sift(iter, i, b->ops->sort_cmp);
Kent Overstreet911c9612013-07-28 18:35:09 -07001077
Kent Overstreetcafe5632013-03-23 16:11:31 -07001078 while (!btree_iter_end(iter)) {
Kent Overstreet65d45232013-12-20 17:22:05 -08001079 if (b->ops->sort_fixup && fixup)
1080 k = b->ops->sort_fixup(iter, &tmp.k);
Kent Overstreetef71ec02013-12-17 17:51:02 -08001081 else
1082 k = NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001083
Kent Overstreetef71ec02013-12-17 17:51:02 -08001084 if (!k)
Kent Overstreet65d45232013-12-20 17:22:05 -08001085 k = __bch_btree_iter_next(iter, b->ops->sort_cmp);
Kent Overstreetef71ec02013-12-17 17:51:02 -08001086
Kent Overstreetcafe5632013-03-23 16:11:31 -07001087 if (bad(b, k))
1088 continue;
1089
1090 if (!last) {
1091 last = out->start;
1092 bkey_copy(last, k);
Kent Overstreet65d45232013-12-20 17:22:05 -08001093 } else if (!bch_bkey_try_merge(b, last, k)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001094 last = bkey_next(last);
1095 bkey_copy(last, k);
1096 }
1097 }
1098
1099 out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1100
1101 pr_debug("sorted %i keys", out->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001102}
1103
Kent Overstreeta85e9682013-12-20 17:28:16 -08001104static void __btree_sort(struct btree_keys *b, struct btree_iter *iter,
Kent Overstreet67539e82013-09-10 22:53:34 -07001105 unsigned start, unsigned order, bool fixup,
1106 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001107{
1108 uint64_t start_time;
Kent Overstreet0a451142013-12-18 00:01:06 -08001109 bool used_mempool = false;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001110 struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
1111 order);
1112 if (!out) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001113 BUG_ON(order > state->page_order);
1114
1115 out = page_address(mempool_alloc(state->pool, GFP_NOIO));
Kent Overstreet0a451142013-12-18 00:01:06 -08001116 used_mempool = true;
Kent Overstreeta85e9682013-12-20 17:28:16 -08001117 order = state->page_order;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001118 }
1119
1120 start_time = local_clock();
1121
Kent Overstreet67539e82013-09-10 22:53:34 -07001122 btree_mergesort(b, out, iter, fixup, false);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001123 b->nsets = start;
1124
Kent Overstreetcafe5632013-03-23 16:11:31 -07001125 if (!start && order == b->page_order) {
1126 /*
1127 * Our temporary buffer is the same size as the btree node's
1128 * buffer, we can just swap buffers instead of doing a big
1129 * memcpy()
1130 */
1131
Kent Overstreeta85e9682013-12-20 17:28:16 -08001132 out->magic = b->set->data->magic;
1133 out->seq = b->set->data->seq;
1134 out->version = b->set->data->version;
1135 swap(out, b->set->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001136 } else {
Kent Overstreeta85e9682013-12-20 17:28:16 -08001137 b->set[start].data->keys = out->keys;
1138 memcpy(b->set[start].data->start, out->start,
Kent Overstreetfafff812013-12-17 21:56:21 -08001139 (void *) bset_bkey_last(out) - (void *) out->start);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001140 }
1141
Kent Overstreet0a451142013-12-18 00:01:06 -08001142 if (used_mempool)
Kent Overstreet67539e82013-09-10 22:53:34 -07001143 mempool_free(virt_to_page(out), state->pool);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001144 else
1145 free_pages((unsigned long) out, order);
1146
Kent Overstreeta85e9682013-12-20 17:28:16 -08001147 bch_bset_build_written_tree(b);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001148
Kent Overstreet65d22e92013-07-31 00:03:54 -07001149 if (!start)
Kent Overstreet67539e82013-09-10 22:53:34 -07001150 bch_time_stats_update(&state->time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001151}
1152
Kent Overstreet67539e82013-09-10 22:53:34 -07001153void bch_btree_sort_partial(struct btree *b, unsigned start,
1154 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001155{
Kent Overstreeta85e9682013-12-20 17:28:16 -08001156 size_t order = b->keys.page_order, keys = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001157 struct btree_iter iter;
Kent Overstreetdc9d98d2013-12-17 23:47:33 -08001158 int oldsize = bch_count_data(&b->keys);
Kent Overstreet280481d2013-10-24 16:36:03 -07001159
Kent Overstreetc052dd92013-11-11 17:35:24 -08001160 __bch_btree_iter_init(&b->keys, &iter, NULL, &b->keys.set[start]);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001161
1162 if (start) {
1163 unsigned i;
1164
Kent Overstreeta85e9682013-12-20 17:28:16 -08001165 for (i = start; i <= b->keys.nsets; i++)
1166 keys += b->keys.set[i].data->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001167
Kent Overstreeta85e9682013-12-20 17:28:16 -08001168 order = roundup_pow_of_two(__set_bytes(b->keys.set->data,
Kent Overstreetb1a67b02013-03-25 11:46:44 -07001169 keys)) / PAGE_SIZE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001170 if (order)
1171 order = ilog2(order);
1172 }
1173
Kent Overstreeta85e9682013-12-20 17:28:16 -08001174 __btree_sort(&b->keys, &iter, start, order, false, state);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001175
Kent Overstreetdc9d98d2013-12-17 23:47:33 -08001176 EBUG_ON(b->written && oldsize >= 0 &&
1177 bch_count_data(&b->keys) != oldsize);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001178}
Kent Overstreet65d45232013-12-20 17:22:05 -08001179EXPORT_SYMBOL(bch_btree_sort_partial);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001180
Kent Overstreeta85e9682013-12-20 17:28:16 -08001181void bch_btree_sort_and_fix_extents(struct btree_keys *b,
1182 struct btree_iter *iter,
Kent Overstreet67539e82013-09-10 22:53:34 -07001183 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001184{
Kent Overstreet67539e82013-09-10 22:53:34 -07001185 __btree_sort(b, iter, 0, b->page_order, true, state);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001186}
1187
Kent Overstreet67539e82013-09-10 22:53:34 -07001188void bch_btree_sort_into(struct btree *b, struct btree *new,
1189 struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001190{
1191 uint64_t start_time = local_clock();
1192
1193 struct btree_iter iter;
Kent Overstreetc052dd92013-11-11 17:35:24 -08001194 bch_btree_iter_init(&b->keys, &iter, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001195
Kent Overstreeta85e9682013-12-20 17:28:16 -08001196 btree_mergesort(&b->keys, new->keys.set->data, &iter, false, true);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001197
Kent Overstreet67539e82013-09-10 22:53:34 -07001198 bch_time_stats_update(&state->time, start_time);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001199
Kent Overstreeta85e9682013-12-20 17:28:16 -08001200 new->keys.set->size = 0; // XXX: why?
Kent Overstreetcafe5632013-03-23 16:11:31 -07001201}
1202
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001203#define SORT_CRIT (4096 / sizeof(uint64_t))
1204
Kent Overstreet67539e82013-09-10 22:53:34 -07001205void bch_btree_sort_lazy(struct btree *b, struct bset_sort_state *state)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001206{
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001207 unsigned crit = SORT_CRIT;
1208 int i;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001209
Kent Overstreeta85e9682013-12-20 17:28:16 -08001210 b->keys.last_set_unwritten = 0;
1211
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001212 /* Don't sort if nothing to do */
Kent Overstreeta85e9682013-12-20 17:28:16 -08001213 if (!b->keys.nsets)
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001214 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001215
Kent Overstreeta85e9682013-12-20 17:28:16 -08001216 for (i = b->keys.nsets - 1; i >= 0; --i) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001217 crit *= state->crit_factor;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001218
Kent Overstreeta85e9682013-12-20 17:28:16 -08001219 if (b->keys.set[i].data->keys < crit) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001220 bch_btree_sort_partial(b, i, state);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001221 return;
1222 }
1223 }
1224
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001225 /* Sort if we'd overflow */
Kent Overstreeta85e9682013-12-20 17:28:16 -08001226 if (b->keys.nsets + 1 == MAX_BSETS) {
Kent Overstreet67539e82013-09-10 22:53:34 -07001227 bch_btree_sort(b, state);
Kent Overstreet6ded34d2013-05-11 15:59:37 -07001228 return;
1229 }
1230
1231out:
Kent Overstreeta85e9682013-12-20 17:28:16 -08001232 bch_bset_build_written_tree(&b->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001233}
Kent Overstreeta85e9682013-12-20 17:28:16 -08001234EXPORT_SYMBOL(bch_btree_sort_lazy);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001235
Kent Overstreetf67342d2013-11-11 19:25:55 -08001236void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001237{
Kent Overstreetcafe5632013-03-23 16:11:31 -07001238 unsigned i;
1239
Kent Overstreetf67342d2013-11-11 19:25:55 -08001240 for (i = 0; i <= b->nsets; i++) {
1241 struct bset_tree *t = &b->set[i];
Kent Overstreetcafe5632013-03-23 16:11:31 -07001242 size_t bytes = t->data->keys * sizeof(uint64_t);
1243 size_t j;
1244
Kent Overstreetf67342d2013-11-11 19:25:55 -08001245 if (bset_written(b, t)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001246 stats->sets_written++;
1247 stats->bytes_written += bytes;
1248
1249 stats->floats += t->size - 1;
1250
1251 for (j = 1; j < t->size; j++)
1252 if (t->tree[j].exponent == 127)
1253 stats->failed++;
1254 } else {
1255 stats->sets_unwritten++;
1256 stats->bytes_unwritten += bytes;
1257 }
1258 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001259}