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