xref: /linux/drivers/md/bcache/bset.c (revision e601757102cfd3eeae068f53b3bc1234f3a2b2e9)
1cafe5635SKent Overstreet /*
2cafe5635SKent Overstreet  * Code for working with individual keys, and sorted sets of keys with in a
3cafe5635SKent Overstreet  * btree node
4cafe5635SKent Overstreet  *
5cafe5635SKent Overstreet  * Copyright 2012 Google, Inc.
6cafe5635SKent Overstreet  */
7cafe5635SKent Overstreet 
889ebb4a2SKent Overstreet #define pr_fmt(fmt) "bcache: %s() " fmt "\n", __func__
989ebb4a2SKent Overstreet 
1089ebb4a2SKent Overstreet #include "util.h"
1189ebb4a2SKent Overstreet #include "bset.h"
12cafe5635SKent Overstreet 
13dc9d98d6SKent Overstreet #include <linux/console.h>
14*e6017571SIngo Molnar #include <linux/sched/clock.h>
15cafe5635SKent Overstreet #include <linux/random.h>
16cd953ed0SGeert Uytterhoeven #include <linux/prefetch.h>
17cafe5635SKent Overstreet 
18dc9d98d6SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
19dc9d98d6SKent Overstreet 
20dc9d98d6SKent Overstreet void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned set)
21dc9d98d6SKent Overstreet {
22dc9d98d6SKent Overstreet 	struct bkey *k, *next;
23dc9d98d6SKent Overstreet 
24dc9d98d6SKent Overstreet 	for (k = i->start; k < bset_bkey_last(i); k = next) {
25dc9d98d6SKent Overstreet 		next = bkey_next(k);
26dc9d98d6SKent Overstreet 
2785cbe1f8SKent Overstreet 		printk(KERN_ERR "block %u key %u/%u: ", set,
2885cbe1f8SKent Overstreet 		       (unsigned) ((u64 *) k - i->d), i->keys);
29dc9d98d6SKent Overstreet 
30dc9d98d6SKent Overstreet 		if (b->ops->key_dump)
31dc9d98d6SKent Overstreet 			b->ops->key_dump(b, k);
32dc9d98d6SKent Overstreet 		else
33dc9d98d6SKent Overstreet 			printk("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
34dc9d98d6SKent Overstreet 
35dc9d98d6SKent Overstreet 		if (next < bset_bkey_last(i) &&
36dc9d98d6SKent Overstreet 		    bkey_cmp(k, b->ops->is_extents ?
37dc9d98d6SKent Overstreet 			     &START_KEY(next) : next) > 0)
38dc9d98d6SKent Overstreet 			printk(KERN_ERR "Key skipped backwards\n");
39dc9d98d6SKent Overstreet 	}
40dc9d98d6SKent Overstreet }
41dc9d98d6SKent Overstreet 
42dc9d98d6SKent Overstreet void bch_dump_bucket(struct btree_keys *b)
43dc9d98d6SKent Overstreet {
44dc9d98d6SKent Overstreet 	unsigned i;
45dc9d98d6SKent Overstreet 
46dc9d98d6SKent Overstreet 	console_lock();
47dc9d98d6SKent Overstreet 	for (i = 0; i <= b->nsets; i++)
48dc9d98d6SKent Overstreet 		bch_dump_bset(b, b->set[i].data,
49dc9d98d6SKent Overstreet 			      bset_sector_offset(b, b->set[i].data));
50dc9d98d6SKent Overstreet 	console_unlock();
51dc9d98d6SKent Overstreet }
52dc9d98d6SKent Overstreet 
53dc9d98d6SKent Overstreet int __bch_count_data(struct btree_keys *b)
54dc9d98d6SKent Overstreet {
55dc9d98d6SKent Overstreet 	unsigned ret = 0;
56dc9d98d6SKent Overstreet 	struct btree_iter iter;
57dc9d98d6SKent Overstreet 	struct bkey *k;
58dc9d98d6SKent Overstreet 
59dc9d98d6SKent Overstreet 	if (b->ops->is_extents)
60dc9d98d6SKent Overstreet 		for_each_key(b, k, &iter)
61dc9d98d6SKent Overstreet 			ret += KEY_SIZE(k);
62dc9d98d6SKent Overstreet 	return ret;
63dc9d98d6SKent Overstreet }
64dc9d98d6SKent Overstreet 
65dc9d98d6SKent Overstreet void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)
66dc9d98d6SKent Overstreet {
67dc9d98d6SKent Overstreet 	va_list args;
68dc9d98d6SKent Overstreet 	struct bkey *k, *p = NULL;
69dc9d98d6SKent Overstreet 	struct btree_iter iter;
70dc9d98d6SKent Overstreet 	const char *err;
71dc9d98d6SKent Overstreet 
72dc9d98d6SKent Overstreet 	for_each_key(b, k, &iter) {
73dc9d98d6SKent Overstreet 		if (b->ops->is_extents) {
74dc9d98d6SKent Overstreet 			err = "Keys out of order";
75dc9d98d6SKent Overstreet 			if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
76dc9d98d6SKent Overstreet 				goto bug;
77dc9d98d6SKent Overstreet 
78dc9d98d6SKent Overstreet 			if (bch_ptr_invalid(b, k))
79dc9d98d6SKent Overstreet 				continue;
80dc9d98d6SKent Overstreet 
81dc9d98d6SKent Overstreet 			err =  "Overlapping keys";
82dc9d98d6SKent Overstreet 			if (p && bkey_cmp(p, &START_KEY(k)) > 0)
83dc9d98d6SKent Overstreet 				goto bug;
84dc9d98d6SKent Overstreet 		} else {
85dc9d98d6SKent Overstreet 			if (bch_ptr_bad(b, k))
86dc9d98d6SKent Overstreet 				continue;
87dc9d98d6SKent Overstreet 
88dc9d98d6SKent Overstreet 			err = "Duplicate keys";
89dc9d98d6SKent Overstreet 			if (p && !bkey_cmp(p, k))
90dc9d98d6SKent Overstreet 				goto bug;
91dc9d98d6SKent Overstreet 		}
92dc9d98d6SKent Overstreet 		p = k;
93dc9d98d6SKent Overstreet 	}
94dc9d98d6SKent Overstreet #if 0
95dc9d98d6SKent Overstreet 	err = "Key larger than btree node key";
96dc9d98d6SKent Overstreet 	if (p && bkey_cmp(p, &b->key) > 0)
97dc9d98d6SKent Overstreet 		goto bug;
98dc9d98d6SKent Overstreet #endif
99dc9d98d6SKent Overstreet 	return;
100dc9d98d6SKent Overstreet bug:
101dc9d98d6SKent Overstreet 	bch_dump_bucket(b);
102dc9d98d6SKent Overstreet 
103dc9d98d6SKent Overstreet 	va_start(args, fmt);
104dc9d98d6SKent Overstreet 	vprintk(fmt, args);
105dc9d98d6SKent Overstreet 	va_end(args);
106dc9d98d6SKent Overstreet 
107dc9d98d6SKent Overstreet 	panic("bch_check_keys error:  %s:\n", err);
108dc9d98d6SKent Overstreet }
109dc9d98d6SKent Overstreet 
110dc9d98d6SKent Overstreet static void bch_btree_iter_next_check(struct btree_iter *iter)
111dc9d98d6SKent Overstreet {
112dc9d98d6SKent Overstreet 	struct bkey *k = iter->data->k, *next = bkey_next(k);
113dc9d98d6SKent Overstreet 
114dc9d98d6SKent Overstreet 	if (next < iter->data->end &&
115dc9d98d6SKent Overstreet 	    bkey_cmp(k, iter->b->ops->is_extents ?
116dc9d98d6SKent Overstreet 		     &START_KEY(next) : next) > 0) {
117dc9d98d6SKent Overstreet 		bch_dump_bucket(iter->b);
118dc9d98d6SKent Overstreet 		panic("Key skipped backwards\n");
119dc9d98d6SKent Overstreet 	}
120dc9d98d6SKent Overstreet }
121dc9d98d6SKent Overstreet 
122dc9d98d6SKent Overstreet #else
123dc9d98d6SKent Overstreet 
124dc9d98d6SKent Overstreet static inline void bch_btree_iter_next_check(struct btree_iter *iter) {}
125dc9d98d6SKent Overstreet 
126dc9d98d6SKent Overstreet #endif
127dc9d98d6SKent Overstreet 
128cafe5635SKent Overstreet /* Keylists */
129cafe5635SKent Overstreet 
130085d2a3dSKent Overstreet int __bch_keylist_realloc(struct keylist *l, unsigned u64s)
131cafe5635SKent Overstreet {
132c2f95ae2SKent Overstreet 	size_t oldsize = bch_keylist_nkeys(l);
133085d2a3dSKent Overstreet 	size_t newsize = oldsize + u64s;
134c2f95ae2SKent Overstreet 	uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
135c2f95ae2SKent Overstreet 	uint64_t *new_keys;
136cafe5635SKent Overstreet 
137cafe5635SKent Overstreet 	newsize = roundup_pow_of_two(newsize);
138cafe5635SKent Overstreet 
139cafe5635SKent Overstreet 	if (newsize <= KEYLIST_INLINE ||
140cafe5635SKent Overstreet 	    roundup_pow_of_two(oldsize) == newsize)
141cafe5635SKent Overstreet 		return 0;
142cafe5635SKent Overstreet 
143c2f95ae2SKent Overstreet 	new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
144cafe5635SKent Overstreet 
145c2f95ae2SKent Overstreet 	if (!new_keys)
146cafe5635SKent Overstreet 		return -ENOMEM;
147cafe5635SKent Overstreet 
148c2f95ae2SKent Overstreet 	if (!old_keys)
149c2f95ae2SKent Overstreet 		memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
150cafe5635SKent Overstreet 
151c2f95ae2SKent Overstreet 	l->keys_p = new_keys;
152c2f95ae2SKent Overstreet 	l->top_p = new_keys + oldsize;
153cafe5635SKent Overstreet 
154cafe5635SKent Overstreet 	return 0;
155cafe5635SKent Overstreet }
156cafe5635SKent Overstreet 
157cafe5635SKent Overstreet struct bkey *bch_keylist_pop(struct keylist *l)
158cafe5635SKent Overstreet {
159c2f95ae2SKent Overstreet 	struct bkey *k = l->keys;
160cafe5635SKent Overstreet 
161cafe5635SKent Overstreet 	if (k == l->top)
162cafe5635SKent Overstreet 		return NULL;
163cafe5635SKent Overstreet 
164cafe5635SKent Overstreet 	while (bkey_next(k) != l->top)
165cafe5635SKent Overstreet 		k = bkey_next(k);
166cafe5635SKent Overstreet 
167cafe5635SKent Overstreet 	return l->top = k;
168cafe5635SKent Overstreet }
169cafe5635SKent Overstreet 
17026c949f8SKent Overstreet void bch_keylist_pop_front(struct keylist *l)
17126c949f8SKent Overstreet {
172c2f95ae2SKent Overstreet 	l->top_p -= bkey_u64s(l->keys);
17326c949f8SKent Overstreet 
174c2f95ae2SKent Overstreet 	memmove(l->keys,
175c2f95ae2SKent Overstreet 		bkey_next(l->keys),
176c2f95ae2SKent Overstreet 		bch_keylist_bytes(l));
17726c949f8SKent Overstreet }
17826c949f8SKent Overstreet 
179cafe5635SKent Overstreet /* Key/pointer manipulation */
180cafe5635SKent Overstreet 
181cafe5635SKent Overstreet void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
182cafe5635SKent Overstreet 			      unsigned i)
183cafe5635SKent Overstreet {
184cafe5635SKent Overstreet 	BUG_ON(i > KEY_PTRS(src));
185cafe5635SKent Overstreet 
186cafe5635SKent Overstreet 	/* Only copy the header, key, and one pointer. */
187cafe5635SKent Overstreet 	memcpy(dest, src, 2 * sizeof(uint64_t));
188cafe5635SKent Overstreet 	dest->ptr[0] = src->ptr[i];
189cafe5635SKent Overstreet 	SET_KEY_PTRS(dest, 1);
190cafe5635SKent Overstreet 	/* We didn't copy the checksum so clear that bit. */
191cafe5635SKent Overstreet 	SET_KEY_CSUM(dest, 0);
192cafe5635SKent Overstreet }
193cafe5635SKent Overstreet 
194cafe5635SKent Overstreet bool __bch_cut_front(const struct bkey *where, struct bkey *k)
195cafe5635SKent Overstreet {
196cafe5635SKent Overstreet 	unsigned i, len = 0;
197cafe5635SKent Overstreet 
198cafe5635SKent Overstreet 	if (bkey_cmp(where, &START_KEY(k)) <= 0)
199cafe5635SKent Overstreet 		return false;
200cafe5635SKent Overstreet 
201cafe5635SKent Overstreet 	if (bkey_cmp(where, k) < 0)
202cafe5635SKent Overstreet 		len = KEY_OFFSET(k) - KEY_OFFSET(where);
203cafe5635SKent Overstreet 	else
204cafe5635SKent Overstreet 		bkey_copy_key(k, where);
205cafe5635SKent Overstreet 
206cafe5635SKent Overstreet 	for (i = 0; i < KEY_PTRS(k); i++)
207cafe5635SKent Overstreet 		SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
208cafe5635SKent Overstreet 
209cafe5635SKent Overstreet 	BUG_ON(len > KEY_SIZE(k));
210cafe5635SKent Overstreet 	SET_KEY_SIZE(k, len);
211cafe5635SKent Overstreet 	return true;
212cafe5635SKent Overstreet }
213cafe5635SKent Overstreet 
214cafe5635SKent Overstreet bool __bch_cut_back(const struct bkey *where, struct bkey *k)
215cafe5635SKent Overstreet {
216cafe5635SKent Overstreet 	unsigned len = 0;
217cafe5635SKent Overstreet 
218cafe5635SKent Overstreet 	if (bkey_cmp(where, k) >= 0)
219cafe5635SKent Overstreet 		return false;
220cafe5635SKent Overstreet 
221cafe5635SKent Overstreet 	BUG_ON(KEY_INODE(where) != KEY_INODE(k));
222cafe5635SKent Overstreet 
223cafe5635SKent Overstreet 	if (bkey_cmp(where, &START_KEY(k)) > 0)
224cafe5635SKent Overstreet 		len = KEY_OFFSET(where) - KEY_START(k);
225cafe5635SKent Overstreet 
226cafe5635SKent Overstreet 	bkey_copy_key(k, where);
227cafe5635SKent Overstreet 
228cafe5635SKent Overstreet 	BUG_ON(len > KEY_SIZE(k));
229cafe5635SKent Overstreet 	SET_KEY_SIZE(k, len);
230cafe5635SKent Overstreet 	return true;
231cafe5635SKent Overstreet }
232cafe5635SKent Overstreet 
233ee811287SKent Overstreet /* Auxiliary search trees */
234ee811287SKent Overstreet 
235ee811287SKent Overstreet /* 32 bits total: */
236ee811287SKent Overstreet #define BKEY_MID_BITS		3
237ee811287SKent Overstreet #define BKEY_EXPONENT_BITS	7
238ee811287SKent Overstreet #define BKEY_MANTISSA_BITS	(32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
239ee811287SKent Overstreet #define BKEY_MANTISSA_MASK	((1 << BKEY_MANTISSA_BITS) - 1)
240ee811287SKent Overstreet 
241ee811287SKent Overstreet struct bkey_float {
242ee811287SKent Overstreet 	unsigned	exponent:BKEY_EXPONENT_BITS;
243ee811287SKent Overstreet 	unsigned	m:BKEY_MID_BITS;
244ee811287SKent Overstreet 	unsigned	mantissa:BKEY_MANTISSA_BITS;
245ee811287SKent Overstreet } __packed;
246ee811287SKent Overstreet 
247ee811287SKent Overstreet /*
248ee811287SKent Overstreet  * BSET_CACHELINE was originally intended to match the hardware cacheline size -
249ee811287SKent Overstreet  * it used to be 64, but I realized the lookup code would touch slightly less
250ee811287SKent Overstreet  * memory if it was 128.
251ee811287SKent Overstreet  *
252ee811287SKent Overstreet  * It definites the number of bytes (in struct bset) per struct bkey_float in
253ee811287SKent Overstreet  * the auxiliar search tree - when we're done searching the bset_float tree we
254ee811287SKent Overstreet  * have this many bytes left that we do a linear search over.
255ee811287SKent Overstreet  *
256ee811287SKent Overstreet  * Since (after level 5) every level of the bset_tree is on a new cacheline,
257ee811287SKent Overstreet  * we're touching one fewer cacheline in the bset tree in exchange for one more
258ee811287SKent Overstreet  * cacheline in the linear search - but the linear search might stop before it
259ee811287SKent Overstreet  * gets to the second cacheline.
260ee811287SKent Overstreet  */
261ee811287SKent Overstreet 
262ee811287SKent Overstreet #define BSET_CACHELINE		128
263ee811287SKent Overstreet 
264ee811287SKent Overstreet /* Space required for the btree node keys */
265a85e968eSKent Overstreet static inline size_t btree_keys_bytes(struct btree_keys *b)
266ee811287SKent Overstreet {
267ee811287SKent Overstreet 	return PAGE_SIZE << b->page_order;
268ee811287SKent Overstreet }
269ee811287SKent Overstreet 
270a85e968eSKent Overstreet static inline size_t btree_keys_cachelines(struct btree_keys *b)
271ee811287SKent Overstreet {
272ee811287SKent Overstreet 	return btree_keys_bytes(b) / BSET_CACHELINE;
273ee811287SKent Overstreet }
274ee811287SKent Overstreet 
275ee811287SKent Overstreet /* Space required for the auxiliary search trees */
276a85e968eSKent Overstreet static inline size_t bset_tree_bytes(struct btree_keys *b)
277ee811287SKent Overstreet {
278ee811287SKent Overstreet 	return btree_keys_cachelines(b) * sizeof(struct bkey_float);
279ee811287SKent Overstreet }
280ee811287SKent Overstreet 
281ee811287SKent Overstreet /* Space required for the prev pointers */
282a85e968eSKent Overstreet static inline size_t bset_prev_bytes(struct btree_keys *b)
283ee811287SKent Overstreet {
284ee811287SKent Overstreet 	return btree_keys_cachelines(b) * sizeof(uint8_t);
285ee811287SKent Overstreet }
286ee811287SKent Overstreet 
287ee811287SKent Overstreet /* Memory allocation */
288ee811287SKent Overstreet 
289a85e968eSKent Overstreet void bch_btree_keys_free(struct btree_keys *b)
290ee811287SKent Overstreet {
291a85e968eSKent Overstreet 	struct bset_tree *t = b->set;
292ee811287SKent Overstreet 
293ee811287SKent Overstreet 	if (bset_prev_bytes(b) < PAGE_SIZE)
294ee811287SKent Overstreet 		kfree(t->prev);
295ee811287SKent Overstreet 	else
296ee811287SKent Overstreet 		free_pages((unsigned long) t->prev,
297ee811287SKent Overstreet 			   get_order(bset_prev_bytes(b)));
298ee811287SKent Overstreet 
299ee811287SKent Overstreet 	if (bset_tree_bytes(b) < PAGE_SIZE)
300ee811287SKent Overstreet 		kfree(t->tree);
301ee811287SKent Overstreet 	else
302ee811287SKent Overstreet 		free_pages((unsigned long) t->tree,
303ee811287SKent Overstreet 			   get_order(bset_tree_bytes(b)));
304ee811287SKent Overstreet 
305ee811287SKent Overstreet 	free_pages((unsigned long) t->data, b->page_order);
306ee811287SKent Overstreet 
307ee811287SKent Overstreet 	t->prev = NULL;
308ee811287SKent Overstreet 	t->tree = NULL;
309ee811287SKent Overstreet 	t->data = NULL;
310ee811287SKent Overstreet }
311a85e968eSKent Overstreet EXPORT_SYMBOL(bch_btree_keys_free);
312ee811287SKent Overstreet 
313a85e968eSKent Overstreet int bch_btree_keys_alloc(struct btree_keys *b, unsigned page_order, gfp_t gfp)
314ee811287SKent Overstreet {
315a85e968eSKent Overstreet 	struct bset_tree *t = b->set;
316ee811287SKent Overstreet 
317ee811287SKent Overstreet 	BUG_ON(t->data);
318ee811287SKent Overstreet 
319ee811287SKent Overstreet 	b->page_order = page_order;
320ee811287SKent Overstreet 
321ee811287SKent Overstreet 	t->data = (void *) __get_free_pages(gfp, b->page_order);
322ee811287SKent Overstreet 	if (!t->data)
323ee811287SKent Overstreet 		goto err;
324ee811287SKent Overstreet 
325ee811287SKent Overstreet 	t->tree = bset_tree_bytes(b) < PAGE_SIZE
326ee811287SKent Overstreet 		? kmalloc(bset_tree_bytes(b), gfp)
327ee811287SKent Overstreet 		: (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
328ee811287SKent Overstreet 	if (!t->tree)
329ee811287SKent Overstreet 		goto err;
330ee811287SKent Overstreet 
331ee811287SKent Overstreet 	t->prev = bset_prev_bytes(b) < PAGE_SIZE
332ee811287SKent Overstreet 		? kmalloc(bset_prev_bytes(b), gfp)
333ee811287SKent Overstreet 		: (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
334ee811287SKent Overstreet 	if (!t->prev)
335ee811287SKent Overstreet 		goto err;
336ee811287SKent Overstreet 
337ee811287SKent Overstreet 	return 0;
338ee811287SKent Overstreet err:
339ee811287SKent Overstreet 	bch_btree_keys_free(b);
340ee811287SKent Overstreet 	return -ENOMEM;
341ee811287SKent Overstreet }
342a85e968eSKent Overstreet EXPORT_SYMBOL(bch_btree_keys_alloc);
343a85e968eSKent Overstreet 
344a85e968eSKent Overstreet void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
345a85e968eSKent Overstreet 			 bool *expensive_debug_checks)
346a85e968eSKent Overstreet {
347a85e968eSKent Overstreet 	unsigned i;
348a85e968eSKent Overstreet 
349a85e968eSKent Overstreet 	b->ops = ops;
350a85e968eSKent Overstreet 	b->expensive_debug_checks = expensive_debug_checks;
351a85e968eSKent Overstreet 	b->nsets = 0;
352a85e968eSKent Overstreet 	b->last_set_unwritten = 0;
353a85e968eSKent Overstreet 
354a85e968eSKent Overstreet 	/* XXX: shouldn't be needed */
355a85e968eSKent Overstreet 	for (i = 0; i < MAX_BSETS; i++)
356a85e968eSKent Overstreet 		b->set[i].size = 0;
357a85e968eSKent Overstreet 	/*
358a85e968eSKent Overstreet 	 * Second loop starts at 1 because b->keys[0]->data is the memory we
359a85e968eSKent Overstreet 	 * allocated
360a85e968eSKent Overstreet 	 */
361a85e968eSKent Overstreet 	for (i = 1; i < MAX_BSETS; i++)
362a85e968eSKent Overstreet 		b->set[i].data = NULL;
363a85e968eSKent Overstreet }
364a85e968eSKent Overstreet EXPORT_SYMBOL(bch_btree_keys_init);
365ee811287SKent Overstreet 
366cafe5635SKent Overstreet /* Binary tree stuff for auxiliary search trees */
367cafe5635SKent Overstreet 
368cafe5635SKent Overstreet static unsigned inorder_next(unsigned j, unsigned size)
369cafe5635SKent Overstreet {
370cafe5635SKent Overstreet 	if (j * 2 + 1 < size) {
371cafe5635SKent Overstreet 		j = j * 2 + 1;
372cafe5635SKent Overstreet 
373cafe5635SKent Overstreet 		while (j * 2 < size)
374cafe5635SKent Overstreet 			j *= 2;
375cafe5635SKent Overstreet 	} else
376cafe5635SKent Overstreet 		j >>= ffz(j) + 1;
377cafe5635SKent Overstreet 
378cafe5635SKent Overstreet 	return j;
379cafe5635SKent Overstreet }
380cafe5635SKent Overstreet 
381cafe5635SKent Overstreet static unsigned inorder_prev(unsigned j, unsigned size)
382cafe5635SKent Overstreet {
383cafe5635SKent Overstreet 	if (j * 2 < size) {
384cafe5635SKent Overstreet 		j = j * 2;
385cafe5635SKent Overstreet 
386cafe5635SKent Overstreet 		while (j * 2 + 1 < size)
387cafe5635SKent Overstreet 			j = j * 2 + 1;
388cafe5635SKent Overstreet 	} else
389cafe5635SKent Overstreet 		j >>= ffs(j);
390cafe5635SKent Overstreet 
391cafe5635SKent Overstreet 	return j;
392cafe5635SKent Overstreet }
393cafe5635SKent Overstreet 
394cafe5635SKent Overstreet /* I have no idea why this code works... and I'm the one who wrote it
395cafe5635SKent Overstreet  *
396cafe5635SKent Overstreet  * However, I do know what it does:
397cafe5635SKent Overstreet  * Given a binary tree constructed in an array (i.e. how you normally implement
398cafe5635SKent Overstreet  * a heap), it converts a node in the tree - referenced by array index - to the
399cafe5635SKent Overstreet  * index it would have if you did an inorder traversal.
400cafe5635SKent Overstreet  *
401cafe5635SKent Overstreet  * Also tested for every j, size up to size somewhere around 6 million.
402cafe5635SKent Overstreet  *
403cafe5635SKent Overstreet  * The binary tree starts at array index 1, not 0
404cafe5635SKent Overstreet  * extra is a function of size:
405cafe5635SKent Overstreet  *   extra = (size - rounddown_pow_of_two(size - 1)) << 1;
406cafe5635SKent Overstreet  */
407cafe5635SKent Overstreet static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
408cafe5635SKent Overstreet {
409cafe5635SKent Overstreet 	unsigned b = fls(j);
410cafe5635SKent Overstreet 	unsigned shift = fls(size - 1) - b;
411cafe5635SKent Overstreet 
412cafe5635SKent Overstreet 	j  ^= 1U << (b - 1);
413cafe5635SKent Overstreet 	j <<= 1;
414cafe5635SKent Overstreet 	j  |= 1;
415cafe5635SKent Overstreet 	j <<= shift;
416cafe5635SKent Overstreet 
417cafe5635SKent Overstreet 	if (j > extra)
418cafe5635SKent Overstreet 		j -= (j - extra) >> 1;
419cafe5635SKent Overstreet 
420cafe5635SKent Overstreet 	return j;
421cafe5635SKent Overstreet }
422cafe5635SKent Overstreet 
423cafe5635SKent Overstreet static unsigned to_inorder(unsigned j, struct bset_tree *t)
424cafe5635SKent Overstreet {
425cafe5635SKent Overstreet 	return __to_inorder(j, t->size, t->extra);
426cafe5635SKent Overstreet }
427cafe5635SKent Overstreet 
428cafe5635SKent Overstreet static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
429cafe5635SKent Overstreet {
430cafe5635SKent Overstreet 	unsigned shift;
431cafe5635SKent Overstreet 
432cafe5635SKent Overstreet 	if (j > extra)
433cafe5635SKent Overstreet 		j += j - extra;
434cafe5635SKent Overstreet 
435cafe5635SKent Overstreet 	shift = ffs(j);
436cafe5635SKent Overstreet 
437cafe5635SKent Overstreet 	j >>= shift;
438cafe5635SKent Overstreet 	j  |= roundup_pow_of_two(size) >> shift;
439cafe5635SKent Overstreet 
440cafe5635SKent Overstreet 	return j;
441cafe5635SKent Overstreet }
442cafe5635SKent Overstreet 
443cafe5635SKent Overstreet static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
444cafe5635SKent Overstreet {
445cafe5635SKent Overstreet 	return __inorder_to_tree(j, t->size, t->extra);
446cafe5635SKent Overstreet }
447cafe5635SKent Overstreet 
448cafe5635SKent Overstreet #if 0
449cafe5635SKent Overstreet void inorder_test(void)
450cafe5635SKent Overstreet {
451cafe5635SKent Overstreet 	unsigned long done = 0;
452cafe5635SKent Overstreet 	ktime_t start = ktime_get();
453cafe5635SKent Overstreet 
454cafe5635SKent Overstreet 	for (unsigned size = 2;
455cafe5635SKent Overstreet 	     size < 65536000;
456cafe5635SKent Overstreet 	     size++) {
457cafe5635SKent Overstreet 		unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
458cafe5635SKent Overstreet 		unsigned i = 1, j = rounddown_pow_of_two(size - 1);
459cafe5635SKent Overstreet 
460cafe5635SKent Overstreet 		if (!(size % 4096))
461cafe5635SKent Overstreet 			printk(KERN_NOTICE "loop %u, %llu per us\n", size,
462cafe5635SKent Overstreet 			       done / ktime_us_delta(ktime_get(), start));
463cafe5635SKent Overstreet 
464cafe5635SKent Overstreet 		while (1) {
465cafe5635SKent Overstreet 			if (__inorder_to_tree(i, size, extra) != j)
466cafe5635SKent Overstreet 				panic("size %10u j %10u i %10u", size, j, i);
467cafe5635SKent Overstreet 
468cafe5635SKent Overstreet 			if (__to_inorder(j, size, extra) != i)
469cafe5635SKent Overstreet 				panic("size %10u j %10u i %10u", size, j, i);
470cafe5635SKent Overstreet 
471cafe5635SKent Overstreet 			if (j == rounddown_pow_of_two(size) - 1)
472cafe5635SKent Overstreet 				break;
473cafe5635SKent Overstreet 
474cafe5635SKent Overstreet 			BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
475cafe5635SKent Overstreet 
476cafe5635SKent Overstreet 			j = inorder_next(j, size);
477cafe5635SKent Overstreet 			i++;
478cafe5635SKent Overstreet 		}
479cafe5635SKent Overstreet 
480cafe5635SKent Overstreet 		done += size - 1;
481cafe5635SKent Overstreet 	}
482cafe5635SKent Overstreet }
483cafe5635SKent Overstreet #endif
484cafe5635SKent Overstreet 
485cafe5635SKent Overstreet /*
48648a73025SPhil Viana  * Cacheline/offset <-> bkey pointer arithmetic:
487cafe5635SKent Overstreet  *
488cafe5635SKent Overstreet  * t->tree is a binary search tree in an array; each node corresponds to a key
489cafe5635SKent Overstreet  * in one cacheline in t->set (BSET_CACHELINE bytes).
490cafe5635SKent Overstreet  *
491cafe5635SKent Overstreet  * This means we don't have to store the full index of the key that a node in
492cafe5635SKent Overstreet  * the binary tree points to; to_inorder() gives us the cacheline, and then
493cafe5635SKent Overstreet  * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
494cafe5635SKent Overstreet  *
49548a73025SPhil Viana  * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
496cafe5635SKent Overstreet  * make this work.
497cafe5635SKent Overstreet  *
498cafe5635SKent Overstreet  * To construct the bfloat for an arbitrary key we need to know what the key
499cafe5635SKent Overstreet  * immediately preceding it is: we have to check if the two keys differ in the
500cafe5635SKent Overstreet  * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
501cafe5635SKent Overstreet  * of the previous key so we can walk backwards to it from t->tree[j]'s key.
502cafe5635SKent Overstreet  */
503cafe5635SKent Overstreet 
504cafe5635SKent Overstreet static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
505cafe5635SKent Overstreet 				      unsigned offset)
506cafe5635SKent Overstreet {
507cafe5635SKent Overstreet 	return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
508cafe5635SKent Overstreet }
509cafe5635SKent Overstreet 
510cafe5635SKent Overstreet static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
511cafe5635SKent Overstreet {
512cafe5635SKent Overstreet 	return ((void *) k - (void *) t->data) / BSET_CACHELINE;
513cafe5635SKent Overstreet }
514cafe5635SKent Overstreet 
5159dd6358aSKent Overstreet static unsigned bkey_to_cacheline_offset(struct bset_tree *t,
5169dd6358aSKent Overstreet 					 unsigned cacheline,
5179dd6358aSKent Overstreet 					 struct bkey *k)
518cafe5635SKent Overstreet {
5199dd6358aSKent Overstreet 	return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0);
520cafe5635SKent Overstreet }
521cafe5635SKent Overstreet 
522cafe5635SKent Overstreet static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
523cafe5635SKent Overstreet {
524cafe5635SKent Overstreet 	return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
525cafe5635SKent Overstreet }
526cafe5635SKent Overstreet 
527cafe5635SKent Overstreet static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
528cafe5635SKent Overstreet {
529cafe5635SKent Overstreet 	return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
530cafe5635SKent Overstreet }
531cafe5635SKent Overstreet 
532cafe5635SKent Overstreet /*
533cafe5635SKent Overstreet  * For the write set - the one we're currently inserting keys into - we don't
534cafe5635SKent Overstreet  * maintain a full search tree, we just keep a simple lookup table in t->prev.
535cafe5635SKent Overstreet  */
536cafe5635SKent Overstreet static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
537cafe5635SKent Overstreet {
538cafe5635SKent Overstreet 	return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
539cafe5635SKent Overstreet }
540cafe5635SKent Overstreet 
541cafe5635SKent Overstreet static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
542cafe5635SKent Overstreet {
543cafe5635SKent Overstreet 	low >>= shift;
544cafe5635SKent Overstreet 	low  |= (high << 1) << (63U - shift);
545cafe5635SKent Overstreet 	return low;
546cafe5635SKent Overstreet }
547cafe5635SKent Overstreet 
548cafe5635SKent Overstreet static inline unsigned bfloat_mantissa(const struct bkey *k,
549cafe5635SKent Overstreet 				       struct bkey_float *f)
550cafe5635SKent Overstreet {
551cafe5635SKent Overstreet 	const uint64_t *p = &k->low - (f->exponent >> 6);
552cafe5635SKent Overstreet 	return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
553cafe5635SKent Overstreet }
554cafe5635SKent Overstreet 
555cafe5635SKent Overstreet static void make_bfloat(struct bset_tree *t, unsigned j)
556cafe5635SKent Overstreet {
557cafe5635SKent Overstreet 	struct bkey_float *f = &t->tree[j];
558cafe5635SKent Overstreet 	struct bkey *m = tree_to_bkey(t, j);
559cafe5635SKent Overstreet 	struct bkey *p = tree_to_prev_bkey(t, j);
560cafe5635SKent Overstreet 
561cafe5635SKent Overstreet 	struct bkey *l = is_power_of_2(j)
562cafe5635SKent Overstreet 		? t->data->start
563cafe5635SKent Overstreet 		: tree_to_prev_bkey(t, j >> ffs(j));
564cafe5635SKent Overstreet 
565cafe5635SKent Overstreet 	struct bkey *r = is_power_of_2(j + 1)
566fafff81cSKent Overstreet 		? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
567cafe5635SKent Overstreet 		: tree_to_bkey(t, j >> (ffz(j) + 1));
568cafe5635SKent Overstreet 
569cafe5635SKent Overstreet 	BUG_ON(m < l || m > r);
570cafe5635SKent Overstreet 	BUG_ON(bkey_next(p) != m);
571cafe5635SKent Overstreet 
572cafe5635SKent Overstreet 	if (KEY_INODE(l) != KEY_INODE(r))
573cafe5635SKent Overstreet 		f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
574cafe5635SKent Overstreet 	else
575cafe5635SKent Overstreet 		f->exponent = fls64(r->low ^ l->low);
576cafe5635SKent Overstreet 
577cafe5635SKent Overstreet 	f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
578cafe5635SKent Overstreet 
579cafe5635SKent Overstreet 	/*
580cafe5635SKent Overstreet 	 * Setting f->exponent = 127 flags this node as failed, and causes the
581cafe5635SKent Overstreet 	 * lookup code to fall back to comparing against the original key.
582cafe5635SKent Overstreet 	 */
583cafe5635SKent Overstreet 
584cafe5635SKent Overstreet 	if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
585cafe5635SKent Overstreet 		f->mantissa = bfloat_mantissa(m, f) - 1;
586cafe5635SKent Overstreet 	else
587cafe5635SKent Overstreet 		f->exponent = 127;
588cafe5635SKent Overstreet }
589cafe5635SKent Overstreet 
590a85e968eSKent Overstreet static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t)
591cafe5635SKent Overstreet {
592a85e968eSKent Overstreet 	if (t != b->set) {
593cafe5635SKent Overstreet 		unsigned j = roundup(t[-1].size,
594cafe5635SKent Overstreet 				     64 / sizeof(struct bkey_float));
595cafe5635SKent Overstreet 
596cafe5635SKent Overstreet 		t->tree = t[-1].tree + j;
597cafe5635SKent Overstreet 		t->prev = t[-1].prev + j;
598cafe5635SKent Overstreet 	}
599cafe5635SKent Overstreet 
600a85e968eSKent Overstreet 	while (t < b->set + MAX_BSETS)
601cafe5635SKent Overstreet 		t++->size = 0;
602cafe5635SKent Overstreet }
603cafe5635SKent Overstreet 
604a85e968eSKent Overstreet static void bch_bset_build_unwritten_tree(struct btree_keys *b)
605cafe5635SKent Overstreet {
606ee811287SKent Overstreet 	struct bset_tree *t = bset_tree_last(b);
607cafe5635SKent Overstreet 
608a85e968eSKent Overstreet 	BUG_ON(b->last_set_unwritten);
609a85e968eSKent Overstreet 	b->last_set_unwritten = 1;
610a85e968eSKent Overstreet 
611cafe5635SKent Overstreet 	bset_alloc_tree(b, t);
612cafe5635SKent Overstreet 
613a85e968eSKent Overstreet 	if (t->tree != b->set->tree + btree_keys_cachelines(b)) {
6149dd6358aSKent Overstreet 		t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start);
615cafe5635SKent Overstreet 		t->size = 1;
616cafe5635SKent Overstreet 	}
617cafe5635SKent Overstreet }
618cafe5635SKent Overstreet 
619a85e968eSKent Overstreet void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic)
620ee811287SKent Overstreet {
621a85e968eSKent Overstreet 	if (i != b->set->data) {
622a85e968eSKent Overstreet 		b->set[++b->nsets].data = i;
623a85e968eSKent Overstreet 		i->seq = b->set->data->seq;
624ee811287SKent Overstreet 	} else
625ee811287SKent Overstreet 		get_random_bytes(&i->seq, sizeof(uint64_t));
626ee811287SKent Overstreet 
627ee811287SKent Overstreet 	i->magic	= magic;
628ee811287SKent Overstreet 	i->version	= 0;
629ee811287SKent Overstreet 	i->keys		= 0;
630ee811287SKent Overstreet 
631ee811287SKent Overstreet 	bch_bset_build_unwritten_tree(b);
632ee811287SKent Overstreet }
633a85e968eSKent Overstreet EXPORT_SYMBOL(bch_bset_init_next);
634ee811287SKent Overstreet 
635a85e968eSKent Overstreet void bch_bset_build_written_tree(struct btree_keys *b)
636cafe5635SKent Overstreet {
637ee811287SKent Overstreet 	struct bset_tree *t = bset_tree_last(b);
6389dd6358aSKent Overstreet 	struct bkey *prev = NULL, *k = t->data->start;
639cafe5635SKent Overstreet 	unsigned j, cacheline = 1;
640cafe5635SKent Overstreet 
641a85e968eSKent Overstreet 	b->last_set_unwritten = 0;
642a85e968eSKent Overstreet 
643cafe5635SKent Overstreet 	bset_alloc_tree(b, t);
644cafe5635SKent Overstreet 
645cafe5635SKent Overstreet 	t->size = min_t(unsigned,
646fafff81cSKent Overstreet 			bkey_to_cacheline(t, bset_bkey_last(t->data)),
647a85e968eSKent Overstreet 			b->set->tree + btree_keys_cachelines(b) - t->tree);
648cafe5635SKent Overstreet 
649cafe5635SKent Overstreet 	if (t->size < 2) {
650cafe5635SKent Overstreet 		t->size = 0;
651cafe5635SKent Overstreet 		return;
652cafe5635SKent Overstreet 	}
653cafe5635SKent Overstreet 
654cafe5635SKent Overstreet 	t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
655cafe5635SKent Overstreet 
656cafe5635SKent Overstreet 	/* First we figure out where the first key in each cacheline is */
657cafe5635SKent Overstreet 	for (j = inorder_next(0, t->size);
658cafe5635SKent Overstreet 	     j;
659cafe5635SKent Overstreet 	     j = inorder_next(j, t->size)) {
6609dd6358aSKent Overstreet 		while (bkey_to_cacheline(t, k) < cacheline)
6619dd6358aSKent Overstreet 			prev = k, k = bkey_next(k);
662cafe5635SKent Overstreet 
6639dd6358aSKent Overstreet 		t->prev[j] = bkey_u64s(prev);
6649dd6358aSKent Overstreet 		t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k);
665cafe5635SKent Overstreet 	}
666cafe5635SKent Overstreet 
667fafff81cSKent Overstreet 	while (bkey_next(k) != bset_bkey_last(t->data))
668cafe5635SKent Overstreet 		k = bkey_next(k);
669cafe5635SKent Overstreet 
670cafe5635SKent Overstreet 	t->end = *k;
671cafe5635SKent Overstreet 
672cafe5635SKent Overstreet 	/* Then we build the tree */
673cafe5635SKent Overstreet 	for (j = inorder_next(0, t->size);
674cafe5635SKent Overstreet 	     j;
675cafe5635SKent Overstreet 	     j = inorder_next(j, t->size))
676cafe5635SKent Overstreet 		make_bfloat(t, j);
677cafe5635SKent Overstreet }
678a85e968eSKent Overstreet EXPORT_SYMBOL(bch_bset_build_written_tree);
679cafe5635SKent Overstreet 
680829a60b9SKent Overstreet /* Insert */
681829a60b9SKent Overstreet 
682a85e968eSKent Overstreet void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k)
683cafe5635SKent Overstreet {
684cafe5635SKent Overstreet 	struct bset_tree *t;
685cafe5635SKent Overstreet 	unsigned inorder, j = 1;
686cafe5635SKent Overstreet 
687a85e968eSKent Overstreet 	for (t = b->set; t <= bset_tree_last(b); t++)
688fafff81cSKent Overstreet 		if (k < bset_bkey_last(t->data))
689cafe5635SKent Overstreet 			goto found_set;
690cafe5635SKent Overstreet 
691cafe5635SKent Overstreet 	BUG();
692cafe5635SKent Overstreet found_set:
693cafe5635SKent Overstreet 	if (!t->size || !bset_written(b, t))
694cafe5635SKent Overstreet 		return;
695cafe5635SKent Overstreet 
696cafe5635SKent Overstreet 	inorder = bkey_to_cacheline(t, k);
697cafe5635SKent Overstreet 
698cafe5635SKent Overstreet 	if (k == t->data->start)
699cafe5635SKent Overstreet 		goto fix_left;
700cafe5635SKent Overstreet 
701fafff81cSKent Overstreet 	if (bkey_next(k) == bset_bkey_last(t->data)) {
702cafe5635SKent Overstreet 		t->end = *k;
703cafe5635SKent Overstreet 		goto fix_right;
704cafe5635SKent Overstreet 	}
705cafe5635SKent Overstreet 
706cafe5635SKent Overstreet 	j = inorder_to_tree(inorder, t);
707cafe5635SKent Overstreet 
708cafe5635SKent Overstreet 	if (j &&
709cafe5635SKent Overstreet 	    j < t->size &&
710cafe5635SKent Overstreet 	    k == tree_to_bkey(t, j))
711cafe5635SKent Overstreet fix_left:	do {
712cafe5635SKent Overstreet 			make_bfloat(t, j);
713cafe5635SKent Overstreet 			j = j * 2;
714cafe5635SKent Overstreet 		} while (j < t->size);
715cafe5635SKent Overstreet 
716cafe5635SKent Overstreet 	j = inorder_to_tree(inorder + 1, t);
717cafe5635SKent Overstreet 
718cafe5635SKent Overstreet 	if (j &&
719cafe5635SKent Overstreet 	    j < t->size &&
720cafe5635SKent Overstreet 	    k == tree_to_prev_bkey(t, j))
721cafe5635SKent Overstreet fix_right:	do {
722cafe5635SKent Overstreet 			make_bfloat(t, j);
723cafe5635SKent Overstreet 			j = j * 2 + 1;
724cafe5635SKent Overstreet 		} while (j < t->size);
725cafe5635SKent Overstreet }
726a85e968eSKent Overstreet EXPORT_SYMBOL(bch_bset_fix_invalidated_key);
727cafe5635SKent Overstreet 
728a85e968eSKent Overstreet static void bch_bset_fix_lookup_table(struct btree_keys *b,
729ee811287SKent Overstreet 				      struct bset_tree *t,
730ee811287SKent Overstreet 				      struct bkey *k)
731cafe5635SKent Overstreet {
732cafe5635SKent Overstreet 	unsigned shift = bkey_u64s(k);
733cafe5635SKent Overstreet 	unsigned j = bkey_to_cacheline(t, k);
734cafe5635SKent Overstreet 
735cafe5635SKent Overstreet 	/* We're getting called from btree_split() or btree_gc, just bail out */
736cafe5635SKent Overstreet 	if (!t->size)
737cafe5635SKent Overstreet 		return;
738cafe5635SKent Overstreet 
739cafe5635SKent Overstreet 	/* k is the key we just inserted; we need to find the entry in the
740cafe5635SKent Overstreet 	 * lookup table for the first key that is strictly greater than k:
741cafe5635SKent Overstreet 	 * it's either k's cacheline or the next one
742cafe5635SKent Overstreet 	 */
7439dd6358aSKent Overstreet 	while (j < t->size &&
744cafe5635SKent Overstreet 	       table_to_bkey(t, j) <= k)
745cafe5635SKent Overstreet 		j++;
746cafe5635SKent Overstreet 
747cafe5635SKent Overstreet 	/* Adjust all the lookup table entries, and find a new key for any that
748cafe5635SKent Overstreet 	 * have gotten too big
749cafe5635SKent Overstreet 	 */
750cafe5635SKent Overstreet 	for (; j < t->size; j++) {
751cafe5635SKent Overstreet 		t->prev[j] += shift;
752cafe5635SKent Overstreet 
753cafe5635SKent Overstreet 		if (t->prev[j] > 7) {
754cafe5635SKent Overstreet 			k = table_to_bkey(t, j - 1);
755cafe5635SKent Overstreet 
756cafe5635SKent Overstreet 			while (k < cacheline_to_bkey(t, j, 0))
757cafe5635SKent Overstreet 				k = bkey_next(k);
758cafe5635SKent Overstreet 
7599dd6358aSKent Overstreet 			t->prev[j] = bkey_to_cacheline_offset(t, j, k);
760cafe5635SKent Overstreet 		}
761cafe5635SKent Overstreet 	}
762cafe5635SKent Overstreet 
763a85e968eSKent Overstreet 	if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree)
764cafe5635SKent Overstreet 		return;
765cafe5635SKent Overstreet 
766cafe5635SKent Overstreet 	/* Possibly add a new entry to the end of the lookup table */
767cafe5635SKent Overstreet 
768cafe5635SKent Overstreet 	for (k = table_to_bkey(t, t->size - 1);
769fafff81cSKent Overstreet 	     k != bset_bkey_last(t->data);
770cafe5635SKent Overstreet 	     k = bkey_next(k))
771cafe5635SKent Overstreet 		if (t->size == bkey_to_cacheline(t, k)) {
7729dd6358aSKent Overstreet 			t->prev[t->size] = bkey_to_cacheline_offset(t, t->size, k);
773cafe5635SKent Overstreet 			t->size++;
774cafe5635SKent Overstreet 		}
775cafe5635SKent Overstreet }
776cafe5635SKent Overstreet 
7770f49cf3dSNicholas Swenson /*
7780f49cf3dSNicholas Swenson  * Tries to merge l and r: l should be lower than r
7790f49cf3dSNicholas Swenson  * Returns true if we were able to merge. If we did merge, l will be the merged
7800f49cf3dSNicholas Swenson  * key, r will be untouched.
7810f49cf3dSNicholas Swenson  */
7820f49cf3dSNicholas Swenson bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r)
7830f49cf3dSNicholas Swenson {
7840f49cf3dSNicholas Swenson 	if (!b->ops->key_merge)
7850f49cf3dSNicholas Swenson 		return false;
7860f49cf3dSNicholas Swenson 
7870f49cf3dSNicholas Swenson 	/*
7880f49cf3dSNicholas Swenson 	 * Generic header checks
7890f49cf3dSNicholas Swenson 	 * Assumes left and right are in order
7900f49cf3dSNicholas Swenson 	 * Left and right must be exactly aligned
7910f49cf3dSNicholas Swenson 	 */
7923bdad1e4SNicholas Swenson 	if (!bch_bkey_equal_header(l, r) ||
7930f49cf3dSNicholas Swenson 	     bkey_cmp(l, &START_KEY(r)))
7940f49cf3dSNicholas Swenson 		return false;
7950f49cf3dSNicholas Swenson 
7960f49cf3dSNicholas Swenson 	return b->ops->key_merge(b, l, r);
7970f49cf3dSNicholas Swenson }
7980f49cf3dSNicholas Swenson EXPORT_SYMBOL(bch_bkey_try_merge);
7990f49cf3dSNicholas Swenson 
800a85e968eSKent Overstreet void bch_bset_insert(struct btree_keys *b, struct bkey *where,
801ee811287SKent Overstreet 		     struct bkey *insert)
802cafe5635SKent Overstreet {
803ee811287SKent Overstreet 	struct bset_tree *t = bset_tree_last(b);
804cafe5635SKent Overstreet 
805a85e968eSKent Overstreet 	BUG_ON(!b->last_set_unwritten);
806ee811287SKent Overstreet 	BUG_ON(bset_byte_offset(b, t->data) +
807ee811287SKent Overstreet 	       __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
808ee811287SKent Overstreet 	       PAGE_SIZE << b->page_order);
809cafe5635SKent Overstreet 
810ee811287SKent Overstreet 	memmove((uint64_t *) where + bkey_u64s(insert),
811ee811287SKent Overstreet 		where,
812ee811287SKent Overstreet 		(void *) bset_bkey_last(t->data) - (void *) where);
813cafe5635SKent Overstreet 
814ee811287SKent Overstreet 	t->data->keys += bkey_u64s(insert);
815ee811287SKent Overstreet 	bkey_copy(where, insert);
816ee811287SKent Overstreet 	bch_bset_fix_lookup_table(b, t, where);
817cafe5635SKent Overstreet }
818a85e968eSKent Overstreet EXPORT_SYMBOL(bch_bset_insert);
819cafe5635SKent Overstreet 
820829a60b9SKent Overstreet unsigned bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
821829a60b9SKent Overstreet 			      struct bkey *replace_key)
822829a60b9SKent Overstreet {
823829a60b9SKent Overstreet 	unsigned status = BTREE_INSERT_STATUS_NO_INSERT;
824829a60b9SKent Overstreet 	struct bset *i = bset_tree_last(b)->data;
825829a60b9SKent Overstreet 	struct bkey *m, *prev = NULL;
826829a60b9SKent Overstreet 	struct btree_iter iter;
827829a60b9SKent Overstreet 
828829a60b9SKent Overstreet 	BUG_ON(b->ops->is_extents && !KEY_SIZE(k));
829829a60b9SKent Overstreet 
830829a60b9SKent Overstreet 	m = bch_btree_iter_init(b, &iter, b->ops->is_extents
831829a60b9SKent Overstreet 				? PRECEDING_KEY(&START_KEY(k))
832829a60b9SKent Overstreet 				: PRECEDING_KEY(k));
833829a60b9SKent Overstreet 
834829a60b9SKent Overstreet 	if (b->ops->insert_fixup(b, k, &iter, replace_key))
835829a60b9SKent Overstreet 		return status;
836829a60b9SKent Overstreet 
837829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_INSERT;
838829a60b9SKent Overstreet 
839829a60b9SKent Overstreet 	while (m != bset_bkey_last(i) &&
840829a60b9SKent Overstreet 	       bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0)
841829a60b9SKent Overstreet 		prev = m, m = bkey_next(m);
842829a60b9SKent Overstreet 
843829a60b9SKent Overstreet 	/* prev is in the tree, if we merge we're done */
844829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_BACK_MERGE;
845829a60b9SKent Overstreet 	if (prev &&
846829a60b9SKent Overstreet 	    bch_bkey_try_merge(b, prev, k))
847829a60b9SKent Overstreet 		goto merged;
848829a60b9SKent Overstreet #if 0
849829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_OVERWROTE;
850829a60b9SKent Overstreet 	if (m != bset_bkey_last(i) &&
851829a60b9SKent Overstreet 	    KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
852829a60b9SKent Overstreet 		goto copy;
853829a60b9SKent Overstreet #endif
854829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_FRONT_MERGE;
855829a60b9SKent Overstreet 	if (m != bset_bkey_last(i) &&
856829a60b9SKent Overstreet 	    bch_bkey_try_merge(b, k, m))
857829a60b9SKent Overstreet 		goto copy;
858829a60b9SKent Overstreet 
859829a60b9SKent Overstreet 	bch_bset_insert(b, m, k);
860829a60b9SKent Overstreet copy:	bkey_copy(m, k);
861829a60b9SKent Overstreet merged:
862829a60b9SKent Overstreet 	return status;
863829a60b9SKent Overstreet }
864829a60b9SKent Overstreet EXPORT_SYMBOL(bch_btree_insert_key);
865829a60b9SKent Overstreet 
866829a60b9SKent Overstreet /* Lookup */
867829a60b9SKent Overstreet 
868cafe5635SKent Overstreet struct bset_search_iter {
869cafe5635SKent Overstreet 	struct bkey *l, *r;
870cafe5635SKent Overstreet };
871cafe5635SKent Overstreet 
872a85e968eSKent Overstreet static struct bset_search_iter bset_search_write_set(struct bset_tree *t,
873cafe5635SKent Overstreet 						     const struct bkey *search)
874cafe5635SKent Overstreet {
875cafe5635SKent Overstreet 	unsigned li = 0, ri = t->size;
876cafe5635SKent Overstreet 
877cafe5635SKent Overstreet 	while (li + 1 != ri) {
878cafe5635SKent Overstreet 		unsigned m = (li + ri) >> 1;
879cafe5635SKent Overstreet 
880cafe5635SKent Overstreet 		if (bkey_cmp(table_to_bkey(t, m), search) > 0)
881cafe5635SKent Overstreet 			ri = m;
882cafe5635SKent Overstreet 		else
883cafe5635SKent Overstreet 			li = m;
884cafe5635SKent Overstreet 	}
885cafe5635SKent Overstreet 
886cafe5635SKent Overstreet 	return (struct bset_search_iter) {
887cafe5635SKent Overstreet 		table_to_bkey(t, li),
888fafff81cSKent Overstreet 		ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
889cafe5635SKent Overstreet 	};
890cafe5635SKent Overstreet }
891cafe5635SKent Overstreet 
892a85e968eSKent Overstreet static struct bset_search_iter bset_search_tree(struct bset_tree *t,
893cafe5635SKent Overstreet 						const struct bkey *search)
894cafe5635SKent Overstreet {
895cafe5635SKent Overstreet 	struct bkey *l, *r;
896cafe5635SKent Overstreet 	struct bkey_float *f;
897cafe5635SKent Overstreet 	unsigned inorder, j, n = 1;
898cafe5635SKent Overstreet 
899cafe5635SKent Overstreet 	do {
900cafe5635SKent Overstreet 		unsigned p = n << 4;
901cafe5635SKent Overstreet 		p &= ((int) (p - t->size)) >> 31;
902cafe5635SKent Overstreet 
903cafe5635SKent Overstreet 		prefetch(&t->tree[p]);
904cafe5635SKent Overstreet 
905cafe5635SKent Overstreet 		j = n;
906cafe5635SKent Overstreet 		f = &t->tree[j];
907cafe5635SKent Overstreet 
908cafe5635SKent Overstreet 		/*
909cafe5635SKent Overstreet 		 * n = (f->mantissa > bfloat_mantissa())
910cafe5635SKent Overstreet 		 *	? j * 2
911cafe5635SKent Overstreet 		 *	: j * 2 + 1;
912cafe5635SKent Overstreet 		 *
913cafe5635SKent Overstreet 		 * We need to subtract 1 from f->mantissa for the sign bit trick
914cafe5635SKent Overstreet 		 * to work  - that's done in make_bfloat()
915cafe5635SKent Overstreet 		 */
916cafe5635SKent Overstreet 		if (likely(f->exponent != 127))
917cafe5635SKent Overstreet 			n = j * 2 + (((unsigned)
918cafe5635SKent Overstreet 				      (f->mantissa -
919cafe5635SKent Overstreet 				       bfloat_mantissa(search, f))) >> 31);
920cafe5635SKent Overstreet 		else
921cafe5635SKent Overstreet 			n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
922cafe5635SKent Overstreet 				? j * 2
923cafe5635SKent Overstreet 				: j * 2 + 1;
924cafe5635SKent Overstreet 	} while (n < t->size);
925cafe5635SKent Overstreet 
926cafe5635SKent Overstreet 	inorder = to_inorder(j, t);
927cafe5635SKent Overstreet 
928cafe5635SKent Overstreet 	/*
929cafe5635SKent Overstreet 	 * n would have been the node we recursed to - the low bit tells us if
930cafe5635SKent Overstreet 	 * we recursed left or recursed right.
931cafe5635SKent Overstreet 	 */
932cafe5635SKent Overstreet 	if (n & 1) {
933cafe5635SKent Overstreet 		l = cacheline_to_bkey(t, inorder, f->m);
934cafe5635SKent Overstreet 
935cafe5635SKent Overstreet 		if (++inorder != t->size) {
936cafe5635SKent Overstreet 			f = &t->tree[inorder_next(j, t->size)];
937cafe5635SKent Overstreet 			r = cacheline_to_bkey(t, inorder, f->m);
938cafe5635SKent Overstreet 		} else
939fafff81cSKent Overstreet 			r = bset_bkey_last(t->data);
940cafe5635SKent Overstreet 	} else {
941cafe5635SKent Overstreet 		r = cacheline_to_bkey(t, inorder, f->m);
942cafe5635SKent Overstreet 
943cafe5635SKent Overstreet 		if (--inorder) {
944cafe5635SKent Overstreet 			f = &t->tree[inorder_prev(j, t->size)];
945cafe5635SKent Overstreet 			l = cacheline_to_bkey(t, inorder, f->m);
946cafe5635SKent Overstreet 		} else
947cafe5635SKent Overstreet 			l = t->data->start;
948cafe5635SKent Overstreet 	}
949cafe5635SKent Overstreet 
950cafe5635SKent Overstreet 	return (struct bset_search_iter) {l, r};
951cafe5635SKent Overstreet }
952cafe5635SKent Overstreet 
953c052dd9aSKent Overstreet struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
954cafe5635SKent Overstreet 			       const struct bkey *search)
955cafe5635SKent Overstreet {
956cafe5635SKent Overstreet 	struct bset_search_iter i;
957cafe5635SKent Overstreet 
958cafe5635SKent Overstreet 	/*
959cafe5635SKent Overstreet 	 * First, we search for a cacheline, then lastly we do a linear search
960cafe5635SKent Overstreet 	 * within that cacheline.
961cafe5635SKent Overstreet 	 *
962cafe5635SKent Overstreet 	 * To search for the cacheline, there's three different possibilities:
963cafe5635SKent Overstreet 	 *  * The set is too small to have a search tree, so we just do a linear
964cafe5635SKent Overstreet 	 *    search over the whole set.
965cafe5635SKent Overstreet 	 *  * The set is the one we're currently inserting into; keeping a full
966cafe5635SKent Overstreet 	 *    auxiliary search tree up to date would be too expensive, so we
967cafe5635SKent Overstreet 	 *    use a much simpler lookup table to do a binary search -
968cafe5635SKent Overstreet 	 *    bset_search_write_set().
969cafe5635SKent Overstreet 	 *  * Or we use the auxiliary search tree we constructed earlier -
970cafe5635SKent Overstreet 	 *    bset_search_tree()
971cafe5635SKent Overstreet 	 */
972cafe5635SKent Overstreet 
973cafe5635SKent Overstreet 	if (unlikely(!t->size)) {
974cafe5635SKent Overstreet 		i.l = t->data->start;
975fafff81cSKent Overstreet 		i.r = bset_bkey_last(t->data);
976c052dd9aSKent Overstreet 	} else if (bset_written(b, t)) {
977cafe5635SKent Overstreet 		/*
978cafe5635SKent Overstreet 		 * Each node in the auxiliary search tree covers a certain range
979cafe5635SKent Overstreet 		 * of bits, and keys above and below the set it covers might
980cafe5635SKent Overstreet 		 * differ outside those bits - so we have to special case the
981cafe5635SKent Overstreet 		 * start and end - handle that here:
982cafe5635SKent Overstreet 		 */
983cafe5635SKent Overstreet 
984cafe5635SKent Overstreet 		if (unlikely(bkey_cmp(search, &t->end) >= 0))
985fafff81cSKent Overstreet 			return bset_bkey_last(t->data);
986cafe5635SKent Overstreet 
987cafe5635SKent Overstreet 		if (unlikely(bkey_cmp(search, t->data->start) < 0))
988cafe5635SKent Overstreet 			return t->data->start;
989cafe5635SKent Overstreet 
990a85e968eSKent Overstreet 		i = bset_search_tree(t, search);
991a85e968eSKent Overstreet 	} else {
992c052dd9aSKent Overstreet 		BUG_ON(!b->nsets &&
993a85e968eSKent Overstreet 		       t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
994a85e968eSKent Overstreet 
995a85e968eSKent Overstreet 		i = bset_search_write_set(t, search);
996a85e968eSKent Overstreet 	}
997cafe5635SKent Overstreet 
998c052dd9aSKent Overstreet 	if (btree_keys_expensive_checks(b)) {
999c052dd9aSKent Overstreet 		BUG_ON(bset_written(b, t) &&
1000cafe5635SKent Overstreet 		       i.l != t->data->start &&
1001cafe5635SKent Overstreet 		       bkey_cmp(tree_to_prev_bkey(t,
1002cafe5635SKent Overstreet 			  inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
1003cafe5635SKent Overstreet 				search) > 0);
1004cafe5635SKent Overstreet 
1005fafff81cSKent Overstreet 		BUG_ON(i.r != bset_bkey_last(t->data) &&
1006cafe5635SKent Overstreet 		       bkey_cmp(i.r, search) <= 0);
1007280481d0SKent Overstreet 	}
1008cafe5635SKent Overstreet 
1009cafe5635SKent Overstreet 	while (likely(i.l != i.r) &&
1010cafe5635SKent Overstreet 	       bkey_cmp(i.l, search) <= 0)
1011cafe5635SKent Overstreet 		i.l = bkey_next(i.l);
1012cafe5635SKent Overstreet 
1013cafe5635SKent Overstreet 	return i.l;
1014cafe5635SKent Overstreet }
1015a85e968eSKent Overstreet EXPORT_SYMBOL(__bch_bset_search);
1016cafe5635SKent Overstreet 
1017cafe5635SKent Overstreet /* Btree iterator */
1018cafe5635SKent Overstreet 
1019911c9610SKent Overstreet typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
1020911c9610SKent Overstreet 				 struct btree_iter_set);
1021911c9610SKent Overstreet 
1022cafe5635SKent Overstreet static inline bool btree_iter_cmp(struct btree_iter_set l,
1023cafe5635SKent Overstreet 				  struct btree_iter_set r)
1024cafe5635SKent Overstreet {
1025911c9610SKent Overstreet 	return bkey_cmp(l.k, r.k) > 0;
1026cafe5635SKent Overstreet }
1027cafe5635SKent Overstreet 
1028cafe5635SKent Overstreet static inline bool btree_iter_end(struct btree_iter *iter)
1029cafe5635SKent Overstreet {
1030cafe5635SKent Overstreet 	return !iter->used;
1031cafe5635SKent Overstreet }
1032cafe5635SKent Overstreet 
1033cafe5635SKent Overstreet void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
1034cafe5635SKent Overstreet 			 struct bkey *end)
1035cafe5635SKent Overstreet {
1036cafe5635SKent Overstreet 	if (k != end)
1037cafe5635SKent Overstreet 		BUG_ON(!heap_add(iter,
1038cafe5635SKent Overstreet 				 ((struct btree_iter_set) { k, end }),
1039cafe5635SKent Overstreet 				 btree_iter_cmp));
1040cafe5635SKent Overstreet }
1041cafe5635SKent Overstreet 
1042c052dd9aSKent Overstreet static struct bkey *__bch_btree_iter_init(struct btree_keys *b,
1043911c9610SKent Overstreet 					  struct btree_iter *iter,
1044911c9610SKent Overstreet 					  struct bkey *search,
1045911c9610SKent Overstreet 					  struct bset_tree *start)
1046cafe5635SKent Overstreet {
1047cafe5635SKent Overstreet 	struct bkey *ret = NULL;
1048cafe5635SKent Overstreet 	iter->size = ARRAY_SIZE(iter->data);
1049cafe5635SKent Overstreet 	iter->used = 0;
1050cafe5635SKent Overstreet 
1051280481d0SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
1052280481d0SKent Overstreet 	iter->b = b;
1053280481d0SKent Overstreet #endif
1054280481d0SKent Overstreet 
1055c052dd9aSKent Overstreet 	for (; start <= bset_tree_last(b); start++) {
1056cafe5635SKent Overstreet 		ret = bch_bset_search(b, start, search);
1057fafff81cSKent Overstreet 		bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
1058cafe5635SKent Overstreet 	}
1059cafe5635SKent Overstreet 
1060cafe5635SKent Overstreet 	return ret;
1061cafe5635SKent Overstreet }
1062cafe5635SKent Overstreet 
1063c052dd9aSKent Overstreet struct bkey *bch_btree_iter_init(struct btree_keys *b,
1064911c9610SKent Overstreet 				 struct btree_iter *iter,
1065911c9610SKent Overstreet 				 struct bkey *search)
1066911c9610SKent Overstreet {
1067c052dd9aSKent Overstreet 	return __bch_btree_iter_init(b, iter, search, b->set);
1068911c9610SKent Overstreet }
1069a85e968eSKent Overstreet EXPORT_SYMBOL(bch_btree_iter_init);
1070911c9610SKent Overstreet 
1071911c9610SKent Overstreet static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
1072911c9610SKent Overstreet 						 btree_iter_cmp_fn *cmp)
1073cafe5635SKent Overstreet {
1074cafe5635SKent Overstreet 	struct btree_iter_set unused;
1075cafe5635SKent Overstreet 	struct bkey *ret = NULL;
1076cafe5635SKent Overstreet 
1077cafe5635SKent Overstreet 	if (!btree_iter_end(iter)) {
1078280481d0SKent Overstreet 		bch_btree_iter_next_check(iter);
1079280481d0SKent Overstreet 
1080cafe5635SKent Overstreet 		ret = iter->data->k;
1081cafe5635SKent Overstreet 		iter->data->k = bkey_next(iter->data->k);
1082cafe5635SKent Overstreet 
1083cafe5635SKent Overstreet 		if (iter->data->k > iter->data->end) {
1084cc0f4eaaSKent Overstreet 			WARN_ONCE(1, "bset was corrupt!\n");
1085cafe5635SKent Overstreet 			iter->data->k = iter->data->end;
1086cafe5635SKent Overstreet 		}
1087cafe5635SKent Overstreet 
1088cafe5635SKent Overstreet 		if (iter->data->k == iter->data->end)
1089911c9610SKent Overstreet 			heap_pop(iter, unused, cmp);
1090cafe5635SKent Overstreet 		else
1091911c9610SKent Overstreet 			heap_sift(iter, 0, cmp);
1092cafe5635SKent Overstreet 	}
1093cafe5635SKent Overstreet 
1094cafe5635SKent Overstreet 	return ret;
1095cafe5635SKent Overstreet }
1096cafe5635SKent Overstreet 
1097911c9610SKent Overstreet struct bkey *bch_btree_iter_next(struct btree_iter *iter)
1098911c9610SKent Overstreet {
1099911c9610SKent Overstreet 	return __bch_btree_iter_next(iter, btree_iter_cmp);
1100911c9610SKent Overstreet 
1101911c9610SKent Overstreet }
1102a85e968eSKent Overstreet EXPORT_SYMBOL(bch_btree_iter_next);
1103911c9610SKent Overstreet 
1104cafe5635SKent Overstreet struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
1105a85e968eSKent Overstreet 					struct btree_keys *b, ptr_filter_fn fn)
1106cafe5635SKent Overstreet {
1107cafe5635SKent Overstreet 	struct bkey *ret;
1108cafe5635SKent Overstreet 
1109cafe5635SKent Overstreet 	do {
1110cafe5635SKent Overstreet 		ret = bch_btree_iter_next(iter);
1111cafe5635SKent Overstreet 	} while (ret && fn(b, ret));
1112cafe5635SKent Overstreet 
1113cafe5635SKent Overstreet 	return ret;
1114cafe5635SKent Overstreet }
1115cafe5635SKent Overstreet 
1116cafe5635SKent Overstreet /* Mergesort */
1117cafe5635SKent Overstreet 
111867539e85SKent Overstreet void bch_bset_sort_state_free(struct bset_sort_state *state)
111967539e85SKent Overstreet {
112067539e85SKent Overstreet 	if (state->pool)
112167539e85SKent Overstreet 		mempool_destroy(state->pool);
112267539e85SKent Overstreet }
112367539e85SKent Overstreet 
112467539e85SKent Overstreet int bch_bset_sort_state_init(struct bset_sort_state *state, unsigned page_order)
112567539e85SKent Overstreet {
112667539e85SKent Overstreet 	spin_lock_init(&state->time.lock);
112767539e85SKent Overstreet 
112867539e85SKent Overstreet 	state->page_order = page_order;
112967539e85SKent Overstreet 	state->crit_factor = int_sqrt(1 << page_order);
113067539e85SKent Overstreet 
113167539e85SKent Overstreet 	state->pool = mempool_create_page_pool(1, page_order);
113267539e85SKent Overstreet 	if (!state->pool)
113367539e85SKent Overstreet 		return -ENOMEM;
113467539e85SKent Overstreet 
113567539e85SKent Overstreet 	return 0;
113667539e85SKent Overstreet }
1137a85e968eSKent Overstreet EXPORT_SYMBOL(bch_bset_sort_state_init);
113867539e85SKent Overstreet 
1139a85e968eSKent Overstreet static void btree_mergesort(struct btree_keys *b, struct bset *out,
1140cafe5635SKent Overstreet 			    struct btree_iter *iter,
1141cafe5635SKent Overstreet 			    bool fixup, bool remove_stale)
1142cafe5635SKent Overstreet {
1143911c9610SKent Overstreet 	int i;
1144cafe5635SKent Overstreet 	struct bkey *k, *last = NULL;
1145ef71ec00SKent Overstreet 	BKEY_PADDED(k) tmp;
1146a85e968eSKent Overstreet 	bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale
1147cafe5635SKent Overstreet 		? bch_ptr_bad
1148cafe5635SKent Overstreet 		: bch_ptr_invalid;
1149cafe5635SKent Overstreet 
1150911c9610SKent Overstreet 	/* Heapify the iterator, using our comparison function */
1151911c9610SKent Overstreet 	for (i = iter->used / 2 - 1; i >= 0; --i)
115265d45231SKent Overstreet 		heap_sift(iter, i, b->ops->sort_cmp);
1153911c9610SKent Overstreet 
1154cafe5635SKent Overstreet 	while (!btree_iter_end(iter)) {
115565d45231SKent Overstreet 		if (b->ops->sort_fixup && fixup)
115665d45231SKent Overstreet 			k = b->ops->sort_fixup(iter, &tmp.k);
1157ef71ec00SKent Overstreet 		else
1158ef71ec00SKent Overstreet 			k = NULL;
1159cafe5635SKent Overstreet 
1160ef71ec00SKent Overstreet 		if (!k)
116165d45231SKent Overstreet 			k = __bch_btree_iter_next(iter, b->ops->sort_cmp);
1162ef71ec00SKent Overstreet 
1163cafe5635SKent Overstreet 		if (bad(b, k))
1164cafe5635SKent Overstreet 			continue;
1165cafe5635SKent Overstreet 
1166cafe5635SKent Overstreet 		if (!last) {
1167cafe5635SKent Overstreet 			last = out->start;
1168cafe5635SKent Overstreet 			bkey_copy(last, k);
116965d45231SKent Overstreet 		} else if (!bch_bkey_try_merge(b, last, k)) {
1170cafe5635SKent Overstreet 			last = bkey_next(last);
1171cafe5635SKent Overstreet 			bkey_copy(last, k);
1172cafe5635SKent Overstreet 		}
1173cafe5635SKent Overstreet 	}
1174cafe5635SKent Overstreet 
1175cafe5635SKent Overstreet 	out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1176cafe5635SKent Overstreet 
1177cafe5635SKent Overstreet 	pr_debug("sorted %i keys", out->keys);
1178cafe5635SKent Overstreet }
1179cafe5635SKent Overstreet 
1180a85e968eSKent Overstreet static void __btree_sort(struct btree_keys *b, struct btree_iter *iter,
118167539e85SKent Overstreet 			 unsigned start, unsigned order, bool fixup,
118267539e85SKent Overstreet 			 struct bset_sort_state *state)
1183cafe5635SKent Overstreet {
1184cafe5635SKent Overstreet 	uint64_t start_time;
11850a451145SKent Overstreet 	bool used_mempool = false;
1186501d52a9SKent Overstreet 	struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOWAIT,
1187cafe5635SKent Overstreet 						     order);
1188cafe5635SKent Overstreet 	if (!out) {
11893572324aSKent Overstreet 		struct page *outp;
11903572324aSKent Overstreet 
119167539e85SKent Overstreet 		BUG_ON(order > state->page_order);
119267539e85SKent Overstreet 
11933572324aSKent Overstreet 		outp = mempool_alloc(state->pool, GFP_NOIO);
11943572324aSKent Overstreet 		out = page_address(outp);
11950a451145SKent Overstreet 		used_mempool = true;
1196a85e968eSKent Overstreet 		order = state->page_order;
1197cafe5635SKent Overstreet 	}
1198cafe5635SKent Overstreet 
1199cafe5635SKent Overstreet 	start_time = local_clock();
1200cafe5635SKent Overstreet 
120167539e85SKent Overstreet 	btree_mergesort(b, out, iter, fixup, false);
1202cafe5635SKent Overstreet 	b->nsets = start;
1203cafe5635SKent Overstreet 
1204cafe5635SKent Overstreet 	if (!start && order == b->page_order) {
1205cafe5635SKent Overstreet 		/*
1206cafe5635SKent Overstreet 		 * Our temporary buffer is the same size as the btree node's
1207cafe5635SKent Overstreet 		 * buffer, we can just swap buffers instead of doing a big
1208cafe5635SKent Overstreet 		 * memcpy()
1209cafe5635SKent Overstreet 		 */
1210cafe5635SKent Overstreet 
1211a85e968eSKent Overstreet 		out->magic	= b->set->data->magic;
1212a85e968eSKent Overstreet 		out->seq	= b->set->data->seq;
1213a85e968eSKent Overstreet 		out->version	= b->set->data->version;
1214a85e968eSKent Overstreet 		swap(out, b->set->data);
1215cafe5635SKent Overstreet 	} else {
1216a85e968eSKent Overstreet 		b->set[start].data->keys = out->keys;
1217a85e968eSKent Overstreet 		memcpy(b->set[start].data->start, out->start,
1218fafff81cSKent Overstreet 		       (void *) bset_bkey_last(out) - (void *) out->start);
1219cafe5635SKent Overstreet 	}
1220cafe5635SKent Overstreet 
12210a451145SKent Overstreet 	if (used_mempool)
122267539e85SKent Overstreet 		mempool_free(virt_to_page(out), state->pool);
1223cafe5635SKent Overstreet 	else
1224cafe5635SKent Overstreet 		free_pages((unsigned long) out, order);
1225cafe5635SKent Overstreet 
1226a85e968eSKent Overstreet 	bch_bset_build_written_tree(b);
1227cafe5635SKent Overstreet 
122865d22e91SKent Overstreet 	if (!start)
122967539e85SKent Overstreet 		bch_time_stats_update(&state->time, start_time);
1230cafe5635SKent Overstreet }
1231cafe5635SKent Overstreet 
123289ebb4a2SKent Overstreet void bch_btree_sort_partial(struct btree_keys *b, unsigned start,
123367539e85SKent Overstreet 			    struct bset_sort_state *state)
1234cafe5635SKent Overstreet {
123589ebb4a2SKent Overstreet 	size_t order = b->page_order, keys = 0;
1236cafe5635SKent Overstreet 	struct btree_iter iter;
123789ebb4a2SKent Overstreet 	int oldsize = bch_count_data(b);
1238280481d0SKent Overstreet 
123989ebb4a2SKent Overstreet 	__bch_btree_iter_init(b, &iter, NULL, &b->set[start]);
1240cafe5635SKent Overstreet 
1241cafe5635SKent Overstreet 	if (start) {
1242cafe5635SKent Overstreet 		unsigned i;
1243cafe5635SKent Overstreet 
124489ebb4a2SKent Overstreet 		for (i = start; i <= b->nsets; i++)
124589ebb4a2SKent Overstreet 			keys += b->set[i].data->keys;
1246cafe5635SKent Overstreet 
124789ebb4a2SKent Overstreet 		order = get_order(__set_bytes(b->set->data, keys));
1248cafe5635SKent Overstreet 	}
1249cafe5635SKent Overstreet 
125089ebb4a2SKent Overstreet 	__btree_sort(b, &iter, start, order, false, state);
1251cafe5635SKent Overstreet 
125289ebb4a2SKent Overstreet 	EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize);
1253cafe5635SKent Overstreet }
125465d45231SKent Overstreet EXPORT_SYMBOL(bch_btree_sort_partial);
1255cafe5635SKent Overstreet 
1256a85e968eSKent Overstreet void bch_btree_sort_and_fix_extents(struct btree_keys *b,
1257a85e968eSKent Overstreet 				    struct btree_iter *iter,
125867539e85SKent Overstreet 				    struct bset_sort_state *state)
1259cafe5635SKent Overstreet {
126067539e85SKent Overstreet 	__btree_sort(b, iter, 0, b->page_order, true, state);
1261cafe5635SKent Overstreet }
1262cafe5635SKent Overstreet 
126389ebb4a2SKent Overstreet void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
126467539e85SKent Overstreet 			 struct bset_sort_state *state)
1265cafe5635SKent Overstreet {
1266cafe5635SKent Overstreet 	uint64_t start_time = local_clock();
1267cafe5635SKent Overstreet 
1268cafe5635SKent Overstreet 	struct btree_iter iter;
126989ebb4a2SKent Overstreet 	bch_btree_iter_init(b, &iter, NULL);
1270cafe5635SKent Overstreet 
127189ebb4a2SKent Overstreet 	btree_mergesort(b, new->set->data, &iter, false, true);
1272cafe5635SKent Overstreet 
127367539e85SKent Overstreet 	bch_time_stats_update(&state->time, start_time);
1274cafe5635SKent Overstreet 
127589ebb4a2SKent Overstreet 	new->set->size = 0; // XXX: why?
1276cafe5635SKent Overstreet }
1277cafe5635SKent Overstreet 
12786ded34d1SKent Overstreet #define SORT_CRIT	(4096 / sizeof(uint64_t))
12796ded34d1SKent Overstreet 
128089ebb4a2SKent Overstreet void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state)
1281cafe5635SKent Overstreet {
12826ded34d1SKent Overstreet 	unsigned crit = SORT_CRIT;
12836ded34d1SKent Overstreet 	int i;
1284cafe5635SKent Overstreet 
12856ded34d1SKent Overstreet 	/* Don't sort if nothing to do */
128689ebb4a2SKent Overstreet 	if (!b->nsets)
12876ded34d1SKent Overstreet 		goto out;
1288cafe5635SKent Overstreet 
128989ebb4a2SKent Overstreet 	for (i = b->nsets - 1; i >= 0; --i) {
129067539e85SKent Overstreet 		crit *= state->crit_factor;
12916ded34d1SKent Overstreet 
129289ebb4a2SKent Overstreet 		if (b->set[i].data->keys < crit) {
129367539e85SKent Overstreet 			bch_btree_sort_partial(b, i, state);
12946ded34d1SKent Overstreet 			return;
12956ded34d1SKent Overstreet 		}
1296cafe5635SKent Overstreet 	}
1297cafe5635SKent Overstreet 
12986ded34d1SKent Overstreet 	/* Sort if we'd overflow */
129989ebb4a2SKent Overstreet 	if (b->nsets + 1 == MAX_BSETS) {
130067539e85SKent Overstreet 		bch_btree_sort(b, state);
13016ded34d1SKent Overstreet 		return;
13026ded34d1SKent Overstreet 	}
13036ded34d1SKent Overstreet 
13046ded34d1SKent Overstreet out:
130589ebb4a2SKent Overstreet 	bch_bset_build_written_tree(b);
1306cafe5635SKent Overstreet }
1307a85e968eSKent Overstreet EXPORT_SYMBOL(bch_btree_sort_lazy);
1308cafe5635SKent Overstreet 
1309f67342ddSKent Overstreet void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats)
1310cafe5635SKent Overstreet {
1311cafe5635SKent Overstreet 	unsigned i;
1312cafe5635SKent Overstreet 
1313f67342ddSKent Overstreet 	for (i = 0; i <= b->nsets; i++) {
1314f67342ddSKent Overstreet 		struct bset_tree *t = &b->set[i];
1315cafe5635SKent Overstreet 		size_t bytes = t->data->keys * sizeof(uint64_t);
1316cafe5635SKent Overstreet 		size_t j;
1317cafe5635SKent Overstreet 
1318f67342ddSKent Overstreet 		if (bset_written(b, t)) {
1319cafe5635SKent Overstreet 			stats->sets_written++;
1320cafe5635SKent Overstreet 			stats->bytes_written += bytes;
1321cafe5635SKent Overstreet 
1322cafe5635SKent Overstreet 			stats->floats += t->size - 1;
1323cafe5635SKent Overstreet 
1324cafe5635SKent Overstreet 			for (j = 1; j < t->size; j++)
1325cafe5635SKent Overstreet 				if (t->tree[j].exponent == 127)
1326cafe5635SKent Overstreet 					stats->failed++;
1327cafe5635SKent Overstreet 		} else {
1328cafe5635SKent Overstreet 			stats->sets_unwritten++;
1329cafe5635SKent Overstreet 			stats->bytes_unwritten += bytes;
1330cafe5635SKent Overstreet 		}
1331cafe5635SKent Overstreet 	}
1332cafe5635SKent Overstreet }
1333