xref: /linux/drivers/md/bcache/bset.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cafe5635SKent Overstreet /*
3cafe5635SKent Overstreet  * Code for working with individual keys, and sorted sets of keys with in a
4cafe5635SKent Overstreet  * btree node
5cafe5635SKent Overstreet  *
6cafe5635SKent Overstreet  * Copyright 2012 Google, Inc.
7cafe5635SKent Overstreet  */
8cafe5635SKent Overstreet 
946f5aa88SJoe Perches #define pr_fmt(fmt) "bcache: %s() " fmt, __func__
1089ebb4a2SKent Overstreet 
1189ebb4a2SKent Overstreet #include "util.h"
1289ebb4a2SKent Overstreet #include "bset.h"
13cafe5635SKent Overstreet 
14dc9d98d6SKent Overstreet #include <linux/console.h>
15e6017571SIngo Molnar #include <linux/sched/clock.h>
16cafe5635SKent Overstreet #include <linux/random.h>
17cd953ed0SGeert Uytterhoeven #include <linux/prefetch.h>
18cafe5635SKent Overstreet 
19dc9d98d6SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
20dc9d98d6SKent Overstreet 
bch_dump_bset(struct btree_keys * b,struct bset * i,unsigned int set)216f10f7d1SColy Li void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set)
22dc9d98d6SKent Overstreet {
23dc9d98d6SKent Overstreet 	struct bkey *k, *next;
24dc9d98d6SKent Overstreet 
25dc9d98d6SKent Overstreet 	for (k = i->start; k < bset_bkey_last(i); k = next) {
26dc9d98d6SKent Overstreet 		next = bkey_next(k);
27dc9d98d6SKent Overstreet 
286ae63e35SColy Li 		pr_err("block %u key %u/%u: ", set,
296f10f7d1SColy Li 		       (unsigned int) ((u64 *) k - i->d), i->keys);
30dc9d98d6SKent Overstreet 
31dc9d98d6SKent Overstreet 		if (b->ops->key_dump)
32dc9d98d6SKent Overstreet 			b->ops->key_dump(b, k);
33dc9d98d6SKent Overstreet 		else
3446f5aa88SJoe Perches 			pr_cont("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
35dc9d98d6SKent Overstreet 
36dc9d98d6SKent Overstreet 		if (next < bset_bkey_last(i) &&
37dc9d98d6SKent Overstreet 		    bkey_cmp(k, b->ops->is_extents ?
38dc9d98d6SKent Overstreet 			     &START_KEY(next) : next) > 0)
396ae63e35SColy Li 			pr_err("Key skipped backwards\n");
40dc9d98d6SKent Overstreet 	}
41dc9d98d6SKent Overstreet }
42dc9d98d6SKent Overstreet 
bch_dump_bucket(struct btree_keys * b)43dc9d98d6SKent Overstreet void bch_dump_bucket(struct btree_keys *b)
44dc9d98d6SKent Overstreet {
456f10f7d1SColy Li 	unsigned int i;
46dc9d98d6SKent Overstreet 
47dc9d98d6SKent Overstreet 	console_lock();
48dc9d98d6SKent Overstreet 	for (i = 0; i <= b->nsets; i++)
49dc9d98d6SKent Overstreet 		bch_dump_bset(b, b->set[i].data,
50dc9d98d6SKent Overstreet 			      bset_sector_offset(b, b->set[i].data));
51dc9d98d6SKent Overstreet 	console_unlock();
52dc9d98d6SKent Overstreet }
53dc9d98d6SKent Overstreet 
__bch_count_data(struct btree_keys * b)54dc9d98d6SKent Overstreet int __bch_count_data(struct btree_keys *b)
55dc9d98d6SKent Overstreet {
566f10f7d1SColy Li 	unsigned int ret = 0;
57*866898efSKuan-Wei Chiu 	struct btree_iter iter;
58dc9d98d6SKent Overstreet 	struct bkey *k;
59dc9d98d6SKent Overstreet 
60*866898efSKuan-Wei Chiu 	min_heap_init(&iter.heap, NULL, MAX_BSETS);
61*866898efSKuan-Wei Chiu 
62dc9d98d6SKent Overstreet 	if (b->ops->is_extents)
63dc9d98d6SKent Overstreet 		for_each_key(b, k, &iter)
64dc9d98d6SKent Overstreet 			ret += KEY_SIZE(k);
65dc9d98d6SKent Overstreet 	return ret;
66dc9d98d6SKent Overstreet }
67dc9d98d6SKent Overstreet 
__bch_check_keys(struct btree_keys * b,const char * fmt,...)68dc9d98d6SKent Overstreet void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)
69dc9d98d6SKent Overstreet {
70dc9d98d6SKent Overstreet 	va_list args;
71dc9d98d6SKent Overstreet 	struct bkey *k, *p = NULL;
72*866898efSKuan-Wei Chiu 	struct btree_iter iter;
73dc9d98d6SKent Overstreet 	const char *err;
74dc9d98d6SKent Overstreet 
75*866898efSKuan-Wei Chiu 	min_heap_init(&iter.heap, NULL, MAX_BSETS);
76*866898efSKuan-Wei Chiu 
77dc9d98d6SKent Overstreet 	for_each_key(b, k, &iter) {
78dc9d98d6SKent Overstreet 		if (b->ops->is_extents) {
79dc9d98d6SKent Overstreet 			err = "Keys out of order";
80dc9d98d6SKent Overstreet 			if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
81dc9d98d6SKent Overstreet 				goto bug;
82dc9d98d6SKent Overstreet 
83dc9d98d6SKent Overstreet 			if (bch_ptr_invalid(b, k))
84dc9d98d6SKent Overstreet 				continue;
85dc9d98d6SKent Overstreet 
86dc9d98d6SKent Overstreet 			err =  "Overlapping keys";
87dc9d98d6SKent Overstreet 			if (p && bkey_cmp(p, &START_KEY(k)) > 0)
88dc9d98d6SKent Overstreet 				goto bug;
89dc9d98d6SKent Overstreet 		} else {
90dc9d98d6SKent Overstreet 			if (bch_ptr_bad(b, k))
91dc9d98d6SKent Overstreet 				continue;
92dc9d98d6SKent Overstreet 
93dc9d98d6SKent Overstreet 			err = "Duplicate keys";
94dc9d98d6SKent Overstreet 			if (p && !bkey_cmp(p, k))
95dc9d98d6SKent Overstreet 				goto bug;
96dc9d98d6SKent Overstreet 		}
97dc9d98d6SKent Overstreet 		p = k;
98dc9d98d6SKent Overstreet 	}
99dc9d98d6SKent Overstreet #if 0
100dc9d98d6SKent Overstreet 	err = "Key larger than btree node key";
101dc9d98d6SKent Overstreet 	if (p && bkey_cmp(p, &b->key) > 0)
102dc9d98d6SKent Overstreet 		goto bug;
103dc9d98d6SKent Overstreet #endif
104dc9d98d6SKent Overstreet 	return;
105dc9d98d6SKent Overstreet bug:
106dc9d98d6SKent Overstreet 	bch_dump_bucket(b);
107dc9d98d6SKent Overstreet 
108dc9d98d6SKent Overstreet 	va_start(args, fmt);
109dc9d98d6SKent Overstreet 	vprintk(fmt, args);
110dc9d98d6SKent Overstreet 	va_end(args);
111dc9d98d6SKent Overstreet 
112dc9d98d6SKent Overstreet 	panic("bch_check_keys error:  %s:\n", err);
113dc9d98d6SKent Overstreet }
114dc9d98d6SKent Overstreet 
bch_btree_iter_next_check(struct btree_iter * iter)115dc9d98d6SKent Overstreet static void bch_btree_iter_next_check(struct btree_iter *iter)
116dc9d98d6SKent Overstreet {
117*866898efSKuan-Wei Chiu 	struct bkey *k = iter->heap.data->k, *next = bkey_next(k);
118dc9d98d6SKent Overstreet 
119*866898efSKuan-Wei Chiu 	if (next < iter->heap.data->end &&
120dc9d98d6SKent Overstreet 	    bkey_cmp(k, iter->b->ops->is_extents ?
121dc9d98d6SKent Overstreet 		     &START_KEY(next) : next) > 0) {
122dc9d98d6SKent Overstreet 		bch_dump_bucket(iter->b);
123dc9d98d6SKent Overstreet 		panic("Key skipped backwards\n");
124dc9d98d6SKent Overstreet 	}
125dc9d98d6SKent Overstreet }
126dc9d98d6SKent Overstreet 
127dc9d98d6SKent Overstreet #else
128dc9d98d6SKent Overstreet 
bch_btree_iter_next_check(struct btree_iter * iter)129dc9d98d6SKent Overstreet static inline void bch_btree_iter_next_check(struct btree_iter *iter) {}
130dc9d98d6SKent Overstreet 
131dc9d98d6SKent Overstreet #endif
132dc9d98d6SKent Overstreet 
133cafe5635SKent Overstreet /* Keylists */
134cafe5635SKent Overstreet 
__bch_keylist_realloc(struct keylist * l,unsigned int u64s)1356f10f7d1SColy Li int __bch_keylist_realloc(struct keylist *l, unsigned int u64s)
136cafe5635SKent Overstreet {
137c2f95ae2SKent Overstreet 	size_t oldsize = bch_keylist_nkeys(l);
138085d2a3dSKent Overstreet 	size_t newsize = oldsize + u64s;
139c2f95ae2SKent Overstreet 	uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
140c2f95ae2SKent Overstreet 	uint64_t *new_keys;
141cafe5635SKent Overstreet 
142cafe5635SKent Overstreet 	newsize = roundup_pow_of_two(newsize);
143cafe5635SKent Overstreet 
144cafe5635SKent Overstreet 	if (newsize <= KEYLIST_INLINE ||
145cafe5635SKent Overstreet 	    roundup_pow_of_two(oldsize) == newsize)
146cafe5635SKent Overstreet 		return 0;
147cafe5635SKent Overstreet 
148c2f95ae2SKent Overstreet 	new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
149cafe5635SKent Overstreet 
150c2f95ae2SKent Overstreet 	if (!new_keys)
151cafe5635SKent Overstreet 		return -ENOMEM;
152cafe5635SKent Overstreet 
153c2f95ae2SKent Overstreet 	if (!old_keys)
154c2f95ae2SKent Overstreet 		memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
155cafe5635SKent Overstreet 
156c2f95ae2SKent Overstreet 	l->keys_p = new_keys;
157c2f95ae2SKent Overstreet 	l->top_p = new_keys + oldsize;
158cafe5635SKent Overstreet 
159cafe5635SKent Overstreet 	return 0;
160cafe5635SKent Overstreet }
161cafe5635SKent Overstreet 
16206c1526dSColy Li /* Pop the top key of keylist by pointing l->top to its previous key */
bch_keylist_pop(struct keylist * l)163cafe5635SKent Overstreet struct bkey *bch_keylist_pop(struct keylist *l)
164cafe5635SKent Overstreet {
165c2f95ae2SKent Overstreet 	struct bkey *k = l->keys;
166cafe5635SKent Overstreet 
167cafe5635SKent Overstreet 	if (k == l->top)
168cafe5635SKent Overstreet 		return NULL;
169cafe5635SKent Overstreet 
170cafe5635SKent Overstreet 	while (bkey_next(k) != l->top)
171cafe5635SKent Overstreet 		k = bkey_next(k);
172cafe5635SKent Overstreet 
173cafe5635SKent Overstreet 	return l->top = k;
174cafe5635SKent Overstreet }
175cafe5635SKent Overstreet 
17606c1526dSColy Li /* Pop the bottom key of keylist and update l->top_p */
bch_keylist_pop_front(struct keylist * l)17726c949f8SKent Overstreet void bch_keylist_pop_front(struct keylist *l)
17826c949f8SKent Overstreet {
179c2f95ae2SKent Overstreet 	l->top_p -= bkey_u64s(l->keys);
18026c949f8SKent Overstreet 
181c2f95ae2SKent Overstreet 	memmove(l->keys,
182c2f95ae2SKent Overstreet 		bkey_next(l->keys),
183c2f95ae2SKent Overstreet 		bch_keylist_bytes(l));
18426c949f8SKent Overstreet }
18526c949f8SKent Overstreet 
186cafe5635SKent Overstreet /* Key/pointer manipulation */
187cafe5635SKent Overstreet 
bch_bkey_copy_single_ptr(struct bkey * dest,const struct bkey * src,unsigned int i)188cafe5635SKent Overstreet void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
1896f10f7d1SColy Li 			      unsigned int i)
190cafe5635SKent Overstreet {
191cafe5635SKent Overstreet 	BUG_ON(i > KEY_PTRS(src));
192cafe5635SKent Overstreet 
193cafe5635SKent Overstreet 	/* Only copy the header, key, and one pointer. */
194cafe5635SKent Overstreet 	memcpy(dest, src, 2 * sizeof(uint64_t));
195cafe5635SKent Overstreet 	dest->ptr[0] = src->ptr[i];
196cafe5635SKent Overstreet 	SET_KEY_PTRS(dest, 1);
197cafe5635SKent Overstreet 	/* We didn't copy the checksum so clear that bit. */
198cafe5635SKent Overstreet 	SET_KEY_CSUM(dest, 0);
199cafe5635SKent Overstreet }
200cafe5635SKent Overstreet 
__bch_cut_front(const struct bkey * where,struct bkey * k)201cafe5635SKent Overstreet bool __bch_cut_front(const struct bkey *where, struct bkey *k)
202cafe5635SKent Overstreet {
2036f10f7d1SColy Li 	unsigned int i, len = 0;
204cafe5635SKent Overstreet 
205cafe5635SKent Overstreet 	if (bkey_cmp(where, &START_KEY(k)) <= 0)
206cafe5635SKent Overstreet 		return false;
207cafe5635SKent Overstreet 
208cafe5635SKent Overstreet 	if (bkey_cmp(where, k) < 0)
209cafe5635SKent Overstreet 		len = KEY_OFFSET(k) - KEY_OFFSET(where);
210cafe5635SKent Overstreet 	else
211cafe5635SKent Overstreet 		bkey_copy_key(k, where);
212cafe5635SKent Overstreet 
213cafe5635SKent Overstreet 	for (i = 0; i < KEY_PTRS(k); i++)
214cafe5635SKent Overstreet 		SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
215cafe5635SKent Overstreet 
216cafe5635SKent Overstreet 	BUG_ON(len > KEY_SIZE(k));
217cafe5635SKent Overstreet 	SET_KEY_SIZE(k, len);
218cafe5635SKent Overstreet 	return true;
219cafe5635SKent Overstreet }
220cafe5635SKent Overstreet 
__bch_cut_back(const struct bkey * where,struct bkey * k)221cafe5635SKent Overstreet bool __bch_cut_back(const struct bkey *where, struct bkey *k)
222cafe5635SKent Overstreet {
2236f10f7d1SColy Li 	unsigned int len = 0;
224cafe5635SKent Overstreet 
225cafe5635SKent Overstreet 	if (bkey_cmp(where, k) >= 0)
226cafe5635SKent Overstreet 		return false;
227cafe5635SKent Overstreet 
228cafe5635SKent Overstreet 	BUG_ON(KEY_INODE(where) != KEY_INODE(k));
229cafe5635SKent Overstreet 
230cafe5635SKent Overstreet 	if (bkey_cmp(where, &START_KEY(k)) > 0)
231cafe5635SKent Overstreet 		len = KEY_OFFSET(where) - KEY_START(k);
232cafe5635SKent Overstreet 
233cafe5635SKent Overstreet 	bkey_copy_key(k, where);
234cafe5635SKent Overstreet 
235cafe5635SKent Overstreet 	BUG_ON(len > KEY_SIZE(k));
236cafe5635SKent Overstreet 	SET_KEY_SIZE(k, len);
237cafe5635SKent Overstreet 	return true;
238cafe5635SKent Overstreet }
239cafe5635SKent Overstreet 
240ee811287SKent Overstreet /* Auxiliary search trees */
241ee811287SKent Overstreet 
242ee811287SKent Overstreet /* 32 bits total: */
243ee811287SKent Overstreet #define BKEY_MID_BITS		3
244ee811287SKent Overstreet #define BKEY_EXPONENT_BITS	7
245ee811287SKent Overstreet #define BKEY_MANTISSA_BITS	(32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
246ee811287SKent Overstreet #define BKEY_MANTISSA_MASK	((1 << BKEY_MANTISSA_BITS) - 1)
247ee811287SKent Overstreet 
248ee811287SKent Overstreet struct bkey_float {
2496f10f7d1SColy Li 	unsigned int	exponent:BKEY_EXPONENT_BITS;
2506f10f7d1SColy Li 	unsigned int	m:BKEY_MID_BITS;
2516f10f7d1SColy Li 	unsigned int	mantissa:BKEY_MANTISSA_BITS;
252ee811287SKent Overstreet } __packed;
253ee811287SKent Overstreet 
254ee811287SKent Overstreet /*
255ee811287SKent Overstreet  * BSET_CACHELINE was originally intended to match the hardware cacheline size -
256ee811287SKent Overstreet  * it used to be 64, but I realized the lookup code would touch slightly less
257ee811287SKent Overstreet  * memory if it was 128.
258ee811287SKent Overstreet  *
259ee811287SKent Overstreet  * It definites the number of bytes (in struct bset) per struct bkey_float in
260ee811287SKent Overstreet  * the auxiliar search tree - when we're done searching the bset_float tree we
261ee811287SKent Overstreet  * have this many bytes left that we do a linear search over.
262ee811287SKent Overstreet  *
263ee811287SKent Overstreet  * Since (after level 5) every level of the bset_tree is on a new cacheline,
264ee811287SKent Overstreet  * we're touching one fewer cacheline in the bset tree in exchange for one more
265ee811287SKent Overstreet  * cacheline in the linear search - but the linear search might stop before it
266ee811287SKent Overstreet  * gets to the second cacheline.
267ee811287SKent Overstreet  */
268ee811287SKent Overstreet 
269ee811287SKent Overstreet #define BSET_CACHELINE		128
270ee811287SKent Overstreet 
271ee811287SKent Overstreet /* Space required for the btree node keys */
btree_keys_bytes(struct btree_keys * b)272a85e968eSKent Overstreet static inline size_t btree_keys_bytes(struct btree_keys *b)
273ee811287SKent Overstreet {
274ee811287SKent Overstreet 	return PAGE_SIZE << b->page_order;
275ee811287SKent Overstreet }
276ee811287SKent Overstreet 
btree_keys_cachelines(struct btree_keys * b)277a85e968eSKent Overstreet static inline size_t btree_keys_cachelines(struct btree_keys *b)
278ee811287SKent Overstreet {
279ee811287SKent Overstreet 	return btree_keys_bytes(b) / BSET_CACHELINE;
280ee811287SKent Overstreet }
281ee811287SKent Overstreet 
282ee811287SKent Overstreet /* Space required for the auxiliary search trees */
bset_tree_bytes(struct btree_keys * b)283a85e968eSKent Overstreet static inline size_t bset_tree_bytes(struct btree_keys *b)
284ee811287SKent Overstreet {
285ee811287SKent Overstreet 	return btree_keys_cachelines(b) * sizeof(struct bkey_float);
286ee811287SKent Overstreet }
287ee811287SKent Overstreet 
288ee811287SKent Overstreet /* Space required for the prev pointers */
bset_prev_bytes(struct btree_keys * b)289a85e968eSKent Overstreet static inline size_t bset_prev_bytes(struct btree_keys *b)
290ee811287SKent Overstreet {
291ee811287SKent Overstreet 	return btree_keys_cachelines(b) * sizeof(uint8_t);
292ee811287SKent Overstreet }
293ee811287SKent Overstreet 
294ee811287SKent Overstreet /* Memory allocation */
295ee811287SKent Overstreet 
bch_btree_keys_free(struct btree_keys * b)296a85e968eSKent Overstreet void bch_btree_keys_free(struct btree_keys *b)
297ee811287SKent Overstreet {
298a85e968eSKent Overstreet 	struct bset_tree *t = b->set;
299ee811287SKent Overstreet 
300ee811287SKent Overstreet 	if (bset_prev_bytes(b) < PAGE_SIZE)
301ee811287SKent Overstreet 		kfree(t->prev);
302ee811287SKent Overstreet 	else
303ee811287SKent Overstreet 		free_pages((unsigned long) t->prev,
304ee811287SKent Overstreet 			   get_order(bset_prev_bytes(b)));
305ee811287SKent Overstreet 
306ee811287SKent Overstreet 	if (bset_tree_bytes(b) < PAGE_SIZE)
307ee811287SKent Overstreet 		kfree(t->tree);
308ee811287SKent Overstreet 	else
309ee811287SKent Overstreet 		free_pages((unsigned long) t->tree,
310ee811287SKent Overstreet 			   get_order(bset_tree_bytes(b)));
311ee811287SKent Overstreet 
312ee811287SKent Overstreet 	free_pages((unsigned long) t->data, b->page_order);
313ee811287SKent Overstreet 
314ee811287SKent Overstreet 	t->prev = NULL;
315ee811287SKent Overstreet 	t->tree = NULL;
316ee811287SKent Overstreet 	t->data = NULL;
317ee811287SKent Overstreet }
318ee811287SKent Overstreet 
bch_btree_keys_alloc(struct btree_keys * b,unsigned int page_order,gfp_t gfp)319b0d30981SColy Li int bch_btree_keys_alloc(struct btree_keys *b,
320b0d30981SColy Li 			 unsigned int page_order,
321b0d30981SColy Li 			 gfp_t gfp)
322ee811287SKent Overstreet {
323a85e968eSKent Overstreet 	struct bset_tree *t = b->set;
324ee811287SKent Overstreet 
325ee811287SKent Overstreet 	BUG_ON(t->data);
326ee811287SKent Overstreet 
327ee811287SKent Overstreet 	b->page_order = page_order;
328ee811287SKent Overstreet 
3295fe48867SColy Li 	t->data = (void *) __get_free_pages(__GFP_COMP|gfp, b->page_order);
330ee811287SKent Overstreet 	if (!t->data)
331ee811287SKent Overstreet 		goto err;
332ee811287SKent Overstreet 
333ee811287SKent Overstreet 	t->tree = bset_tree_bytes(b) < PAGE_SIZE
334ee811287SKent Overstreet 		? kmalloc(bset_tree_bytes(b), gfp)
335ee811287SKent Overstreet 		: (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
336ee811287SKent Overstreet 	if (!t->tree)
337ee811287SKent Overstreet 		goto err;
338ee811287SKent Overstreet 
339ee811287SKent Overstreet 	t->prev = bset_prev_bytes(b) < PAGE_SIZE
340ee811287SKent Overstreet 		? kmalloc(bset_prev_bytes(b), gfp)
341ee811287SKent Overstreet 		: (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
342ee811287SKent Overstreet 	if (!t->prev)
343ee811287SKent Overstreet 		goto err;
344ee811287SKent Overstreet 
345ee811287SKent Overstreet 	return 0;
346ee811287SKent Overstreet err:
347ee811287SKent Overstreet 	bch_btree_keys_free(b);
348ee811287SKent Overstreet 	return -ENOMEM;
349ee811287SKent Overstreet }
350a85e968eSKent Overstreet 
bch_btree_keys_init(struct btree_keys * b,const struct btree_keys_ops * ops,bool * expensive_debug_checks)351a85e968eSKent Overstreet void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
352a85e968eSKent Overstreet 			 bool *expensive_debug_checks)
353a85e968eSKent Overstreet {
354a85e968eSKent Overstreet 	b->ops = ops;
355a85e968eSKent Overstreet 	b->expensive_debug_checks = expensive_debug_checks;
356a85e968eSKent Overstreet 	b->nsets = 0;
357a85e968eSKent Overstreet 	b->last_set_unwritten = 0;
358a85e968eSKent Overstreet 
359a85e968eSKent Overstreet 	/*
360bd9026c8SColy Li 	 * struct btree_keys in embedded in struct btree, and struct
361bd9026c8SColy Li 	 * bset_tree is embedded into struct btree_keys. They are all
362bd9026c8SColy Li 	 * initialized as 0 by kzalloc() in mca_bucket_alloc(), and
363bd9026c8SColy Li 	 * b->set[0].data is allocated in bch_btree_keys_alloc(), so we
364bd9026c8SColy Li 	 * don't have to initiate b->set[].size and b->set[].data here
365bd9026c8SColy Li 	 * any more.
366a85e968eSKent Overstreet 	 */
367a85e968eSKent Overstreet }
368ee811287SKent Overstreet 
369cafe5635SKent Overstreet /* Binary tree stuff for auxiliary search trees */
370cafe5635SKent Overstreet 
371b467a6acSColy Li /*
372b467a6acSColy Li  * return array index next to j when does in-order traverse
373b467a6acSColy Li  * of a binary tree which is stored in a linear array
374b467a6acSColy Li  */
inorder_next(unsigned int j,unsigned int size)3756f10f7d1SColy Li static unsigned int inorder_next(unsigned int j, unsigned int size)
376cafe5635SKent Overstreet {
377cafe5635SKent Overstreet 	if (j * 2 + 1 < size) {
378cafe5635SKent Overstreet 		j = j * 2 + 1;
379cafe5635SKent Overstreet 
380cafe5635SKent Overstreet 		while (j * 2 < size)
381cafe5635SKent Overstreet 			j *= 2;
382cafe5635SKent Overstreet 	} else
383cafe5635SKent Overstreet 		j >>= ffz(j) + 1;
384cafe5635SKent Overstreet 
385cafe5635SKent Overstreet 	return j;
386cafe5635SKent Overstreet }
387cafe5635SKent Overstreet 
388b467a6acSColy Li /*
389b467a6acSColy Li  * return array index previous to j when does in-order traverse
390b467a6acSColy Li  * of a binary tree which is stored in a linear array
391b467a6acSColy Li  */
inorder_prev(unsigned int j,unsigned int size)3926f10f7d1SColy Li static unsigned int inorder_prev(unsigned int j, unsigned int size)
393cafe5635SKent Overstreet {
394cafe5635SKent Overstreet 	if (j * 2 < size) {
395cafe5635SKent Overstreet 		j = j * 2;
396cafe5635SKent Overstreet 
397cafe5635SKent Overstreet 		while (j * 2 + 1 < size)
398cafe5635SKent Overstreet 			j = j * 2 + 1;
399cafe5635SKent Overstreet 	} else
400cafe5635SKent Overstreet 		j >>= ffs(j);
401cafe5635SKent Overstreet 
402cafe5635SKent Overstreet 	return j;
403cafe5635SKent Overstreet }
404cafe5635SKent Overstreet 
4053be11dbaSColy Li /*
4063be11dbaSColy Li  * I have no idea why this code works... and I'm the one who wrote it
407cafe5635SKent Overstreet  *
408cafe5635SKent Overstreet  * However, I do know what it does:
409cafe5635SKent Overstreet  * Given a binary tree constructed in an array (i.e. how you normally implement
410cafe5635SKent Overstreet  * a heap), it converts a node in the tree - referenced by array index - to the
411cafe5635SKent Overstreet  * index it would have if you did an inorder traversal.
412cafe5635SKent Overstreet  *
413cafe5635SKent Overstreet  * Also tested for every j, size up to size somewhere around 6 million.
414cafe5635SKent Overstreet  *
415cafe5635SKent Overstreet  * The binary tree starts at array index 1, not 0
416cafe5635SKent Overstreet  * extra is a function of size:
417cafe5635SKent Overstreet  *   extra = (size - rounddown_pow_of_two(size - 1)) << 1;
418cafe5635SKent Overstreet  */
__to_inorder(unsigned int j,unsigned int size,unsigned int extra)4196f10f7d1SColy Li static unsigned int __to_inorder(unsigned int j,
4206f10f7d1SColy Li 				  unsigned int size,
4216f10f7d1SColy Li 				  unsigned int extra)
422cafe5635SKent Overstreet {
4236f10f7d1SColy Li 	unsigned int b = fls(j);
4246f10f7d1SColy Li 	unsigned int shift = fls(size - 1) - b;
425cafe5635SKent Overstreet 
426cafe5635SKent Overstreet 	j  ^= 1U << (b - 1);
427cafe5635SKent Overstreet 	j <<= 1;
428cafe5635SKent Overstreet 	j  |= 1;
429cafe5635SKent Overstreet 	j <<= shift;
430cafe5635SKent Overstreet 
431cafe5635SKent Overstreet 	if (j > extra)
432cafe5635SKent Overstreet 		j -= (j - extra) >> 1;
433cafe5635SKent Overstreet 
434cafe5635SKent Overstreet 	return j;
435cafe5635SKent Overstreet }
436cafe5635SKent Overstreet 
437b467a6acSColy Li /*
438b467a6acSColy Li  * Return the cacheline index in bset_tree->data, where j is index
439b467a6acSColy Li  * from a linear array which stores the auxiliar binary tree
440b467a6acSColy Li  */
to_inorder(unsigned int j,struct bset_tree * t)4416f10f7d1SColy Li static unsigned int to_inorder(unsigned int j, struct bset_tree *t)
442cafe5635SKent Overstreet {
443cafe5635SKent Overstreet 	return __to_inorder(j, t->size, t->extra);
444cafe5635SKent Overstreet }
445cafe5635SKent Overstreet 
__inorder_to_tree(unsigned int j,unsigned int size,unsigned int extra)4466f10f7d1SColy Li static unsigned int __inorder_to_tree(unsigned int j,
4476f10f7d1SColy Li 				      unsigned int size,
4486f10f7d1SColy Li 				      unsigned int extra)
449cafe5635SKent Overstreet {
4506f10f7d1SColy Li 	unsigned int shift;
451cafe5635SKent Overstreet 
452cafe5635SKent Overstreet 	if (j > extra)
453cafe5635SKent Overstreet 		j += j - extra;
454cafe5635SKent Overstreet 
455cafe5635SKent Overstreet 	shift = ffs(j);
456cafe5635SKent Overstreet 
457cafe5635SKent Overstreet 	j >>= shift;
458cafe5635SKent Overstreet 	j  |= roundup_pow_of_two(size) >> shift;
459cafe5635SKent Overstreet 
460cafe5635SKent Overstreet 	return j;
461cafe5635SKent Overstreet }
462cafe5635SKent Overstreet 
463b467a6acSColy Li /*
464b467a6acSColy Li  * Return an index from a linear array which stores the auxiliar binary
465b467a6acSColy Li  * tree, j is the cacheline index of t->data.
466b467a6acSColy Li  */
inorder_to_tree(unsigned int j,struct bset_tree * t)4676f10f7d1SColy Li static unsigned int inorder_to_tree(unsigned int j, struct bset_tree *t)
468cafe5635SKent Overstreet {
469cafe5635SKent Overstreet 	return __inorder_to_tree(j, t->size, t->extra);
470cafe5635SKent Overstreet }
471cafe5635SKent Overstreet 
472cafe5635SKent Overstreet #if 0
473cafe5635SKent Overstreet void inorder_test(void)
474cafe5635SKent Overstreet {
475cafe5635SKent Overstreet 	unsigned long done = 0;
476cafe5635SKent Overstreet 	ktime_t start = ktime_get();
477cafe5635SKent Overstreet 
4786f10f7d1SColy Li 	for (unsigned int size = 2;
479cafe5635SKent Overstreet 	     size < 65536000;
480cafe5635SKent Overstreet 	     size++) {
481b0d30981SColy Li 		unsigned int extra =
482b0d30981SColy Li 			(size - rounddown_pow_of_two(size - 1)) << 1;
4836f10f7d1SColy Li 		unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
484cafe5635SKent Overstreet 
485cafe5635SKent Overstreet 		if (!(size % 4096))
4866ae63e35SColy Li 			pr_notice("loop %u, %llu per us\n", size,
487cafe5635SKent Overstreet 			       done / ktime_us_delta(ktime_get(), start));
488cafe5635SKent Overstreet 
489cafe5635SKent Overstreet 		while (1) {
490cafe5635SKent Overstreet 			if (__inorder_to_tree(i, size, extra) != j)
491cafe5635SKent Overstreet 				panic("size %10u j %10u i %10u", size, j, i);
492cafe5635SKent Overstreet 
493cafe5635SKent Overstreet 			if (__to_inorder(j, size, extra) != i)
494cafe5635SKent Overstreet 				panic("size %10u j %10u i %10u", size, j, i);
495cafe5635SKent Overstreet 
496cafe5635SKent Overstreet 			if (j == rounddown_pow_of_two(size) - 1)
497cafe5635SKent Overstreet 				break;
498cafe5635SKent Overstreet 
499cafe5635SKent Overstreet 			BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
500cafe5635SKent Overstreet 
501cafe5635SKent Overstreet 			j = inorder_next(j, size);
502cafe5635SKent Overstreet 			i++;
503cafe5635SKent Overstreet 		}
504cafe5635SKent Overstreet 
505cafe5635SKent Overstreet 		done += size - 1;
506cafe5635SKent Overstreet 	}
507cafe5635SKent Overstreet }
508cafe5635SKent Overstreet #endif
509cafe5635SKent Overstreet 
510cafe5635SKent Overstreet /*
51148a73025SPhil Viana  * Cacheline/offset <-> bkey pointer arithmetic:
512cafe5635SKent Overstreet  *
513cafe5635SKent Overstreet  * t->tree is a binary search tree in an array; each node corresponds to a key
514cafe5635SKent Overstreet  * in one cacheline in t->set (BSET_CACHELINE bytes).
515cafe5635SKent Overstreet  *
516cafe5635SKent Overstreet  * This means we don't have to store the full index of the key that a node in
517cafe5635SKent Overstreet  * the binary tree points to; to_inorder() gives us the cacheline, and then
518cafe5635SKent Overstreet  * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
519cafe5635SKent Overstreet  *
52048a73025SPhil Viana  * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
521cafe5635SKent Overstreet  * make this work.
522cafe5635SKent Overstreet  *
523cafe5635SKent Overstreet  * To construct the bfloat for an arbitrary key we need to know what the key
524cafe5635SKent Overstreet  * immediately preceding it is: we have to check if the two keys differ in the
525cafe5635SKent Overstreet  * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
526cafe5635SKent Overstreet  * of the previous key so we can walk backwards to it from t->tree[j]'s key.
527cafe5635SKent Overstreet  */
528cafe5635SKent Overstreet 
cacheline_to_bkey(struct bset_tree * t,unsigned int cacheline,unsigned int offset)5296f10f7d1SColy Li static struct bkey *cacheline_to_bkey(struct bset_tree *t,
5306f10f7d1SColy Li 				      unsigned int cacheline,
5316f10f7d1SColy Li 				      unsigned int offset)
532cafe5635SKent Overstreet {
533cafe5635SKent Overstreet 	return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
534cafe5635SKent Overstreet }
535cafe5635SKent Overstreet 
bkey_to_cacheline(struct bset_tree * t,struct bkey * k)5366f10f7d1SColy Li static unsigned int bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
537cafe5635SKent Overstreet {
538cafe5635SKent Overstreet 	return ((void *) k - (void *) t->data) / BSET_CACHELINE;
539cafe5635SKent Overstreet }
540cafe5635SKent Overstreet 
bkey_to_cacheline_offset(struct bset_tree * t,unsigned int cacheline,struct bkey * k)5416f10f7d1SColy Li static unsigned int bkey_to_cacheline_offset(struct bset_tree *t,
5426f10f7d1SColy Li 					 unsigned int cacheline,
5439dd6358aSKent Overstreet 					 struct bkey *k)
544cafe5635SKent Overstreet {
5459dd6358aSKent Overstreet 	return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0);
546cafe5635SKent Overstreet }
547cafe5635SKent Overstreet 
tree_to_bkey(struct bset_tree * t,unsigned int j)5486f10f7d1SColy Li static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned int j)
549cafe5635SKent Overstreet {
550cafe5635SKent Overstreet 	return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
551cafe5635SKent Overstreet }
552cafe5635SKent Overstreet 
tree_to_prev_bkey(struct bset_tree * t,unsigned int j)5536f10f7d1SColy Li static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned int j)
554cafe5635SKent Overstreet {
555cafe5635SKent Overstreet 	return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
556cafe5635SKent Overstreet }
557cafe5635SKent Overstreet 
558cafe5635SKent Overstreet /*
559cafe5635SKent Overstreet  * For the write set - the one we're currently inserting keys into - we don't
560cafe5635SKent Overstreet  * maintain a full search tree, we just keep a simple lookup table in t->prev.
561cafe5635SKent Overstreet  */
table_to_bkey(struct bset_tree * t,unsigned int cacheline)5626f10f7d1SColy Li static struct bkey *table_to_bkey(struct bset_tree *t, unsigned int cacheline)
563cafe5635SKent Overstreet {
564cafe5635SKent Overstreet 	return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
565cafe5635SKent Overstreet }
566cafe5635SKent Overstreet 
shrd128(uint64_t high,uint64_t low,uint8_t shift)567cafe5635SKent Overstreet static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
568cafe5635SKent Overstreet {
569cafe5635SKent Overstreet 	low >>= shift;
570cafe5635SKent Overstreet 	low  |= (high << 1) << (63U - shift);
571cafe5635SKent Overstreet 	return low;
572cafe5635SKent Overstreet }
573cafe5635SKent Overstreet 
574b467a6acSColy Li /*
575b467a6acSColy Li  * Calculate mantissa value for struct bkey_float.
576b467a6acSColy Li  * If most significant bit of f->exponent is not set, then
577b467a6acSColy Li  *  - f->exponent >> 6 is 0
578b467a6acSColy Li  *  - p[0] points to bkey->low
579b467a6acSColy Li  *  - p[-1] borrows bits from KEY_INODE() of bkey->high
580b467a6acSColy Li  * if most isgnificant bits of f->exponent is set, then
581b467a6acSColy Li  *  - f->exponent >> 6 is 1
582b467a6acSColy Li  *  - p[0] points to bits from KEY_INODE() of bkey->high
583b467a6acSColy Li  *  - p[-1] points to other bits from KEY_INODE() of
584b467a6acSColy Li  *    bkey->high too.
585b467a6acSColy Li  * See make_bfloat() to check when most significant bit of f->exponent
586b467a6acSColy Li  * is set or not.
587b467a6acSColy Li  */
bfloat_mantissa(const struct bkey * k,struct bkey_float * f)5886f10f7d1SColy Li static inline unsigned int bfloat_mantissa(const struct bkey *k,
589cafe5635SKent Overstreet 				       struct bkey_float *f)
590cafe5635SKent Overstreet {
591cafe5635SKent Overstreet 	const uint64_t *p = &k->low - (f->exponent >> 6);
5921fae7cf0SColy Li 
593cafe5635SKent Overstreet 	return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
594cafe5635SKent Overstreet }
595cafe5635SKent Overstreet 
make_bfloat(struct bset_tree * t,unsigned int j)5966f10f7d1SColy Li static void make_bfloat(struct bset_tree *t, unsigned int j)
597cafe5635SKent Overstreet {
598cafe5635SKent Overstreet 	struct bkey_float *f = &t->tree[j];
599cafe5635SKent Overstreet 	struct bkey *m = tree_to_bkey(t, j);
600cafe5635SKent Overstreet 	struct bkey *p = tree_to_prev_bkey(t, j);
601cafe5635SKent Overstreet 
602cafe5635SKent Overstreet 	struct bkey *l = is_power_of_2(j)
603cafe5635SKent Overstreet 		? t->data->start
604cafe5635SKent Overstreet 		: tree_to_prev_bkey(t, j >> ffs(j));
605cafe5635SKent Overstreet 
606cafe5635SKent Overstreet 	struct bkey *r = is_power_of_2(j + 1)
607fafff81cSKent Overstreet 		? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
608cafe5635SKent Overstreet 		: tree_to_bkey(t, j >> (ffz(j) + 1));
609cafe5635SKent Overstreet 
610cafe5635SKent Overstreet 	BUG_ON(m < l || m > r);
611cafe5635SKent Overstreet 	BUG_ON(bkey_next(p) != m);
612cafe5635SKent Overstreet 
613b467a6acSColy Li 	/*
614b467a6acSColy Li 	 * If l and r have different KEY_INODE values (different backing
615b467a6acSColy Li 	 * device), f->exponent records how many least significant bits
616b467a6acSColy Li 	 * are different in KEY_INODE values and sets most significant
617b467a6acSColy Li 	 * bits to 1 (by +64).
618b467a6acSColy Li 	 * If l and r have same KEY_INODE value, f->exponent records
619b467a6acSColy Li 	 * how many different bits in least significant bits of bkey->low.
620b467a6acSColy Li 	 * See bfloat_mantiss() how the most significant bit of
621b467a6acSColy Li 	 * f->exponent is used to calculate bfloat mantissa value.
622b467a6acSColy Li 	 */
623cafe5635SKent Overstreet 	if (KEY_INODE(l) != KEY_INODE(r))
624cafe5635SKent Overstreet 		f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
625cafe5635SKent Overstreet 	else
626cafe5635SKent Overstreet 		f->exponent = fls64(r->low ^ l->low);
627cafe5635SKent Overstreet 
628cafe5635SKent Overstreet 	f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
629cafe5635SKent Overstreet 
630cafe5635SKent Overstreet 	/*
631cafe5635SKent Overstreet 	 * Setting f->exponent = 127 flags this node as failed, and causes the
632cafe5635SKent Overstreet 	 * lookup code to fall back to comparing against the original key.
633cafe5635SKent Overstreet 	 */
634cafe5635SKent Overstreet 
635cafe5635SKent Overstreet 	if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
636cafe5635SKent Overstreet 		f->mantissa = bfloat_mantissa(m, f) - 1;
637cafe5635SKent Overstreet 	else
638cafe5635SKent Overstreet 		f->exponent = 127;
639cafe5635SKent Overstreet }
640cafe5635SKent Overstreet 
bset_alloc_tree(struct btree_keys * b,struct bset_tree * t)641a85e968eSKent Overstreet static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t)
642cafe5635SKent Overstreet {
643a85e968eSKent Overstreet 	if (t != b->set) {
6446f10f7d1SColy Li 		unsigned int j = roundup(t[-1].size,
645cafe5635SKent Overstreet 				     64 / sizeof(struct bkey_float));
646cafe5635SKent Overstreet 
647cafe5635SKent Overstreet 		t->tree = t[-1].tree + j;
648cafe5635SKent Overstreet 		t->prev = t[-1].prev + j;
649cafe5635SKent Overstreet 	}
650cafe5635SKent Overstreet 
651a85e968eSKent Overstreet 	while (t < b->set + MAX_BSETS)
652cafe5635SKent Overstreet 		t++->size = 0;
653cafe5635SKent Overstreet }
654cafe5635SKent Overstreet 
bch_bset_build_unwritten_tree(struct btree_keys * b)655a85e968eSKent Overstreet static void bch_bset_build_unwritten_tree(struct btree_keys *b)
656cafe5635SKent Overstreet {
657ee811287SKent Overstreet 	struct bset_tree *t = bset_tree_last(b);
658cafe5635SKent Overstreet 
659a85e968eSKent Overstreet 	BUG_ON(b->last_set_unwritten);
660a85e968eSKent Overstreet 	b->last_set_unwritten = 1;
661a85e968eSKent Overstreet 
662cafe5635SKent Overstreet 	bset_alloc_tree(b, t);
663cafe5635SKent Overstreet 
664a85e968eSKent Overstreet 	if (t->tree != b->set->tree + btree_keys_cachelines(b)) {
6659dd6358aSKent Overstreet 		t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start);
666cafe5635SKent Overstreet 		t->size = 1;
667cafe5635SKent Overstreet 	}
668cafe5635SKent Overstreet }
669cafe5635SKent Overstreet 
bch_bset_init_next(struct btree_keys * b,struct bset * i,uint64_t magic)670a85e968eSKent Overstreet void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic)
671ee811287SKent Overstreet {
672a85e968eSKent Overstreet 	if (i != b->set->data) {
673a85e968eSKent Overstreet 		b->set[++b->nsets].data = i;
674a85e968eSKent Overstreet 		i->seq = b->set->data->seq;
675ee811287SKent Overstreet 	} else
676ee811287SKent Overstreet 		get_random_bytes(&i->seq, sizeof(uint64_t));
677ee811287SKent Overstreet 
678ee811287SKent Overstreet 	i->magic	= magic;
679ee811287SKent Overstreet 	i->version	= 0;
680ee811287SKent Overstreet 	i->keys		= 0;
681ee811287SKent Overstreet 
682ee811287SKent Overstreet 	bch_bset_build_unwritten_tree(b);
683ee811287SKent Overstreet }
684ee811287SKent Overstreet 
685b467a6acSColy Li /*
686b467a6acSColy Li  * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to
687b467a6acSColy Li  * accelerate bkey search in a btree node (pointed by bset_tree->data in
688b467a6acSColy Li  * memory). After search in the auxiliar tree by calling bset_search_tree(),
689b467a6acSColy Li  * a struct bset_search_iter is returned which indicates range [l, r] from
690b467a6acSColy Li  * bset_tree->data where the searching bkey might be inside. Then a followed
691b467a6acSColy Li  * linear comparison does the exact search, see __bch_bset_search() for how
692b467a6acSColy Li  * the auxiliary tree is used.
693b467a6acSColy Li  */
bch_bset_build_written_tree(struct btree_keys * b)694a85e968eSKent Overstreet void bch_bset_build_written_tree(struct btree_keys *b)
695cafe5635SKent Overstreet {
696ee811287SKent Overstreet 	struct bset_tree *t = bset_tree_last(b);
6979dd6358aSKent Overstreet 	struct bkey *prev = NULL, *k = t->data->start;
6986f10f7d1SColy Li 	unsigned int j, cacheline = 1;
699cafe5635SKent Overstreet 
700a85e968eSKent Overstreet 	b->last_set_unwritten = 0;
701a85e968eSKent Overstreet 
702cafe5635SKent Overstreet 	bset_alloc_tree(b, t);
703cafe5635SKent Overstreet 
7046f10f7d1SColy Li 	t->size = min_t(unsigned int,
705fafff81cSKent Overstreet 			bkey_to_cacheline(t, bset_bkey_last(t->data)),
706a85e968eSKent Overstreet 			b->set->tree + btree_keys_cachelines(b) - t->tree);
707cafe5635SKent Overstreet 
708cafe5635SKent Overstreet 	if (t->size < 2) {
709cafe5635SKent Overstreet 		t->size = 0;
710cafe5635SKent Overstreet 		return;
711cafe5635SKent Overstreet 	}
712cafe5635SKent Overstreet 
713cafe5635SKent Overstreet 	t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
714cafe5635SKent Overstreet 
715cafe5635SKent Overstreet 	/* First we figure out where the first key in each cacheline is */
716cafe5635SKent Overstreet 	for (j = inorder_next(0, t->size);
717cafe5635SKent Overstreet 	     j;
718cafe5635SKent Overstreet 	     j = inorder_next(j, t->size)) {
7196751c1e3SJoe Perches 		while (bkey_to_cacheline(t, k) < cacheline) {
7206751c1e3SJoe Perches 			prev = k;
7216751c1e3SJoe Perches 			k = bkey_next(k);
7226751c1e3SJoe Perches 		}
723cafe5635SKent Overstreet 
7249dd6358aSKent Overstreet 		t->prev[j] = bkey_u64s(prev);
7259dd6358aSKent Overstreet 		t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k);
726cafe5635SKent Overstreet 	}
727cafe5635SKent Overstreet 
728fafff81cSKent Overstreet 	while (bkey_next(k) != bset_bkey_last(t->data))
729cafe5635SKent Overstreet 		k = bkey_next(k);
730cafe5635SKent Overstreet 
731cafe5635SKent Overstreet 	t->end = *k;
732cafe5635SKent Overstreet 
733cafe5635SKent Overstreet 	/* Then we build the tree */
734cafe5635SKent Overstreet 	for (j = inorder_next(0, t->size);
735cafe5635SKent Overstreet 	     j;
736cafe5635SKent Overstreet 	     j = inorder_next(j, t->size))
737cafe5635SKent Overstreet 		make_bfloat(t, j);
738cafe5635SKent Overstreet }
739cafe5635SKent Overstreet 
740829a60b9SKent Overstreet /* Insert */
741829a60b9SKent Overstreet 
bch_bset_fix_invalidated_key(struct btree_keys * b,struct bkey * k)742a85e968eSKent Overstreet void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k)
743cafe5635SKent Overstreet {
744cafe5635SKent Overstreet 	struct bset_tree *t;
7456f10f7d1SColy Li 	unsigned int inorder, j = 1;
746cafe5635SKent Overstreet 
747a85e968eSKent Overstreet 	for (t = b->set; t <= bset_tree_last(b); t++)
748fafff81cSKent Overstreet 		if (k < bset_bkey_last(t->data))
749cafe5635SKent Overstreet 			goto found_set;
750cafe5635SKent Overstreet 
751cafe5635SKent Overstreet 	BUG();
752cafe5635SKent Overstreet found_set:
753cafe5635SKent Overstreet 	if (!t->size || !bset_written(b, t))
754cafe5635SKent Overstreet 		return;
755cafe5635SKent Overstreet 
756cafe5635SKent Overstreet 	inorder = bkey_to_cacheline(t, k);
757cafe5635SKent Overstreet 
758cafe5635SKent Overstreet 	if (k == t->data->start)
759cafe5635SKent Overstreet 		goto fix_left;
760cafe5635SKent Overstreet 
761fafff81cSKent Overstreet 	if (bkey_next(k) == bset_bkey_last(t->data)) {
762cafe5635SKent Overstreet 		t->end = *k;
763cafe5635SKent Overstreet 		goto fix_right;
764cafe5635SKent Overstreet 	}
765cafe5635SKent Overstreet 
766cafe5635SKent Overstreet 	j = inorder_to_tree(inorder, t);
767cafe5635SKent Overstreet 
768cafe5635SKent Overstreet 	if (j &&
769cafe5635SKent Overstreet 	    j < t->size &&
770cafe5635SKent Overstreet 	    k == tree_to_bkey(t, j))
771cafe5635SKent Overstreet fix_left:	do {
772cafe5635SKent Overstreet 			make_bfloat(t, j);
773cafe5635SKent Overstreet 			j = j * 2;
774cafe5635SKent Overstreet 		} while (j < t->size);
775cafe5635SKent Overstreet 
776cafe5635SKent Overstreet 	j = inorder_to_tree(inorder + 1, t);
777cafe5635SKent Overstreet 
778cafe5635SKent Overstreet 	if (j &&
779cafe5635SKent Overstreet 	    j < t->size &&
780cafe5635SKent Overstreet 	    k == tree_to_prev_bkey(t, j))
781cafe5635SKent Overstreet fix_right:	do {
782cafe5635SKent Overstreet 			make_bfloat(t, j);
783cafe5635SKent Overstreet 			j = j * 2 + 1;
784cafe5635SKent Overstreet 		} while (j < t->size);
785cafe5635SKent Overstreet }
786cafe5635SKent Overstreet 
bch_bset_fix_lookup_table(struct btree_keys * b,struct bset_tree * t,struct bkey * k)787a85e968eSKent Overstreet static void bch_bset_fix_lookup_table(struct btree_keys *b,
788ee811287SKent Overstreet 				      struct bset_tree *t,
789ee811287SKent Overstreet 				      struct bkey *k)
790cafe5635SKent Overstreet {
7916f10f7d1SColy Li 	unsigned int shift = bkey_u64s(k);
7926f10f7d1SColy Li 	unsigned int j = bkey_to_cacheline(t, k);
793cafe5635SKent Overstreet 
794cafe5635SKent Overstreet 	/* We're getting called from btree_split() or btree_gc, just bail out */
795cafe5635SKent Overstreet 	if (!t->size)
796cafe5635SKent Overstreet 		return;
797cafe5635SKent Overstreet 
7983be11dbaSColy Li 	/*
7993be11dbaSColy Li 	 * k is the key we just inserted; we need to find the entry in the
800cafe5635SKent Overstreet 	 * lookup table for the first key that is strictly greater than k:
801cafe5635SKent Overstreet 	 * it's either k's cacheline or the next one
802cafe5635SKent Overstreet 	 */
8039dd6358aSKent Overstreet 	while (j < t->size &&
804cafe5635SKent Overstreet 	       table_to_bkey(t, j) <= k)
805cafe5635SKent Overstreet 		j++;
806cafe5635SKent Overstreet 
8073be11dbaSColy Li 	/*
8083be11dbaSColy Li 	 * Adjust all the lookup table entries, and find a new key for any that
809cafe5635SKent Overstreet 	 * have gotten too big
810cafe5635SKent Overstreet 	 */
811cafe5635SKent Overstreet 	for (; j < t->size; j++) {
812cafe5635SKent Overstreet 		t->prev[j] += shift;
813cafe5635SKent Overstreet 
814cafe5635SKent Overstreet 		if (t->prev[j] > 7) {
815cafe5635SKent Overstreet 			k = table_to_bkey(t, j - 1);
816cafe5635SKent Overstreet 
817cafe5635SKent Overstreet 			while (k < cacheline_to_bkey(t, j, 0))
818cafe5635SKent Overstreet 				k = bkey_next(k);
819cafe5635SKent Overstreet 
8209dd6358aSKent Overstreet 			t->prev[j] = bkey_to_cacheline_offset(t, j, k);
821cafe5635SKent Overstreet 		}
822cafe5635SKent Overstreet 	}
823cafe5635SKent Overstreet 
824a85e968eSKent Overstreet 	if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree)
825cafe5635SKent Overstreet 		return;
826cafe5635SKent Overstreet 
827cafe5635SKent Overstreet 	/* Possibly add a new entry to the end of the lookup table */
828cafe5635SKent Overstreet 
829cafe5635SKent Overstreet 	for (k = table_to_bkey(t, t->size - 1);
830fafff81cSKent Overstreet 	     k != bset_bkey_last(t->data);
831cafe5635SKent Overstreet 	     k = bkey_next(k))
832cafe5635SKent Overstreet 		if (t->size == bkey_to_cacheline(t, k)) {
833b0d30981SColy Li 			t->prev[t->size] =
834b0d30981SColy Li 				bkey_to_cacheline_offset(t, t->size, k);
835cafe5635SKent Overstreet 			t->size++;
836cafe5635SKent Overstreet 		}
837cafe5635SKent Overstreet }
838cafe5635SKent Overstreet 
8390f49cf3dSNicholas Swenson /*
8400f49cf3dSNicholas Swenson  * Tries to merge l and r: l should be lower than r
8410f49cf3dSNicholas Swenson  * Returns true if we were able to merge. If we did merge, l will be the merged
8420f49cf3dSNicholas Swenson  * key, r will be untouched.
8430f49cf3dSNicholas Swenson  */
bch_bkey_try_merge(struct btree_keys * b,struct bkey * l,struct bkey * r)8440f49cf3dSNicholas Swenson bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r)
8450f49cf3dSNicholas Swenson {
8460f49cf3dSNicholas Swenson 	if (!b->ops->key_merge)
8470f49cf3dSNicholas Swenson 		return false;
8480f49cf3dSNicholas Swenson 
8490f49cf3dSNicholas Swenson 	/*
8500f49cf3dSNicholas Swenson 	 * Generic header checks
8510f49cf3dSNicholas Swenson 	 * Assumes left and right are in order
8520f49cf3dSNicholas Swenson 	 * Left and right must be exactly aligned
8530f49cf3dSNicholas Swenson 	 */
8543bdad1e4SNicholas Swenson 	if (!bch_bkey_equal_header(l, r) ||
8550f49cf3dSNicholas Swenson 	     bkey_cmp(l, &START_KEY(r)))
8560f49cf3dSNicholas Swenson 		return false;
8570f49cf3dSNicholas Swenson 
8580f49cf3dSNicholas Swenson 	return b->ops->key_merge(b, l, r);
8590f49cf3dSNicholas Swenson }
8600f49cf3dSNicholas Swenson 
bch_bset_insert(struct btree_keys * b,struct bkey * where,struct bkey * insert)861a85e968eSKent Overstreet void bch_bset_insert(struct btree_keys *b, struct bkey *where,
862ee811287SKent Overstreet 		     struct bkey *insert)
863cafe5635SKent Overstreet {
864ee811287SKent Overstreet 	struct bset_tree *t = bset_tree_last(b);
865cafe5635SKent Overstreet 
866a85e968eSKent Overstreet 	BUG_ON(!b->last_set_unwritten);
867ee811287SKent Overstreet 	BUG_ON(bset_byte_offset(b, t->data) +
868ee811287SKent Overstreet 	       __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
869ee811287SKent Overstreet 	       PAGE_SIZE << b->page_order);
870cafe5635SKent Overstreet 
871ee811287SKent Overstreet 	memmove((uint64_t *) where + bkey_u64s(insert),
872ee811287SKent Overstreet 		where,
873ee811287SKent Overstreet 		(void *) bset_bkey_last(t->data) - (void *) where);
874cafe5635SKent Overstreet 
875ee811287SKent Overstreet 	t->data->keys += bkey_u64s(insert);
876ee811287SKent Overstreet 	bkey_copy(where, insert);
877ee811287SKent Overstreet 	bch_bset_fix_lookup_table(b, t, where);
878cafe5635SKent Overstreet }
879cafe5635SKent Overstreet 
bch_btree_insert_key(struct btree_keys * b,struct bkey * k,struct bkey * replace_key)8806f10f7d1SColy Li unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
881829a60b9SKent Overstreet 			      struct bkey *replace_key)
882829a60b9SKent Overstreet {
8836f10f7d1SColy Li 	unsigned int status = BTREE_INSERT_STATUS_NO_INSERT;
884829a60b9SKent Overstreet 	struct bset *i = bset_tree_last(b)->data;
885829a60b9SKent Overstreet 	struct bkey *m, *prev = NULL;
886*866898efSKuan-Wei Chiu 	struct btree_iter iter;
88731b90956SColy Li 	struct bkey preceding_key_on_stack = ZERO_KEY;
88831b90956SColy Li 	struct bkey *preceding_key_p = &preceding_key_on_stack;
889829a60b9SKent Overstreet 
890829a60b9SKent Overstreet 	BUG_ON(b->ops->is_extents && !KEY_SIZE(k));
891829a60b9SKent Overstreet 
892*866898efSKuan-Wei Chiu 	min_heap_init(&iter.heap, NULL, MAX_BSETS);
893*866898efSKuan-Wei Chiu 
89431b90956SColy Li 	/*
89531b90956SColy Li 	 * If k has preceding key, preceding_key_p will be set to address
89631b90956SColy Li 	 *  of k's preceding key; otherwise preceding_key_p will be set
89731b90956SColy Li 	 * to NULL inside preceding_key().
89831b90956SColy Li 	 */
89931b90956SColy Li 	if (b->ops->is_extents)
90031b90956SColy Li 		preceding_key(&START_KEY(k), &preceding_key_p);
90131b90956SColy Li 	else
90231b90956SColy Li 		preceding_key(k, &preceding_key_p);
90331b90956SColy Li 
904*866898efSKuan-Wei Chiu 	m = bch_btree_iter_init(b, &iter, preceding_key_p);
905829a60b9SKent Overstreet 
906*866898efSKuan-Wei Chiu 	if (b->ops->insert_fixup(b, k, &iter, replace_key))
907829a60b9SKent Overstreet 		return status;
908829a60b9SKent Overstreet 
909829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_INSERT;
910829a60b9SKent Overstreet 
911829a60b9SKent Overstreet 	while (m != bset_bkey_last(i) &&
9126751c1e3SJoe Perches 	       bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0) {
9136751c1e3SJoe Perches 		prev = m;
9146751c1e3SJoe Perches 		m = bkey_next(m);
9156751c1e3SJoe Perches 	}
916829a60b9SKent Overstreet 
917829a60b9SKent Overstreet 	/* prev is in the tree, if we merge we're done */
918829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_BACK_MERGE;
919829a60b9SKent Overstreet 	if (prev &&
920829a60b9SKent Overstreet 	    bch_bkey_try_merge(b, prev, k))
921829a60b9SKent Overstreet 		goto merged;
922829a60b9SKent Overstreet #if 0
923829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_OVERWROTE;
924829a60b9SKent Overstreet 	if (m != bset_bkey_last(i) &&
925829a60b9SKent Overstreet 	    KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
926829a60b9SKent Overstreet 		goto copy;
927829a60b9SKent Overstreet #endif
928829a60b9SKent Overstreet 	status = BTREE_INSERT_STATUS_FRONT_MERGE;
929829a60b9SKent Overstreet 	if (m != bset_bkey_last(i) &&
930829a60b9SKent Overstreet 	    bch_bkey_try_merge(b, k, m))
931829a60b9SKent Overstreet 		goto copy;
932829a60b9SKent Overstreet 
933829a60b9SKent Overstreet 	bch_bset_insert(b, m, k);
934829a60b9SKent Overstreet copy:	bkey_copy(m, k);
935829a60b9SKent Overstreet merged:
936829a60b9SKent Overstreet 	return status;
937829a60b9SKent Overstreet }
938829a60b9SKent Overstreet 
939829a60b9SKent Overstreet /* Lookup */
940829a60b9SKent Overstreet 
941cafe5635SKent Overstreet struct bset_search_iter {
942cafe5635SKent Overstreet 	struct bkey *l, *r;
943cafe5635SKent Overstreet };
944cafe5635SKent Overstreet 
bset_search_write_set(struct bset_tree * t,const struct bkey * search)945a85e968eSKent Overstreet static struct bset_search_iter bset_search_write_set(struct bset_tree *t,
946cafe5635SKent Overstreet 						     const struct bkey *search)
947cafe5635SKent Overstreet {
9486f10f7d1SColy Li 	unsigned int li = 0, ri = t->size;
949cafe5635SKent Overstreet 
950cafe5635SKent Overstreet 	while (li + 1 != ri) {
9516f10f7d1SColy Li 		unsigned int m = (li + ri) >> 1;
952cafe5635SKent Overstreet 
953cafe5635SKent Overstreet 		if (bkey_cmp(table_to_bkey(t, m), search) > 0)
954cafe5635SKent Overstreet 			ri = m;
955cafe5635SKent Overstreet 		else
956cafe5635SKent Overstreet 			li = m;
957cafe5635SKent Overstreet 	}
958cafe5635SKent Overstreet 
959cafe5635SKent Overstreet 	return (struct bset_search_iter) {
960cafe5635SKent Overstreet 		table_to_bkey(t, li),
961fafff81cSKent Overstreet 		ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
962cafe5635SKent Overstreet 	};
963cafe5635SKent Overstreet }
964cafe5635SKent Overstreet 
bset_search_tree(struct bset_tree * t,const struct bkey * search)965a85e968eSKent Overstreet static struct bset_search_iter bset_search_tree(struct bset_tree *t,
966cafe5635SKent Overstreet 						const struct bkey *search)
967cafe5635SKent Overstreet {
968cafe5635SKent Overstreet 	struct bkey *l, *r;
969cafe5635SKent Overstreet 	struct bkey_float *f;
9706f10f7d1SColy Li 	unsigned int inorder, j, n = 1;
971cafe5635SKent Overstreet 
972cafe5635SKent Overstreet 	do {
9736f10f7d1SColy Li 		unsigned int p = n << 4;
9741fae7cf0SColy Li 
975f960facbSColy Li 		if (p < t->size)
976cafe5635SKent Overstreet 			prefetch(&t->tree[p]);
977cafe5635SKent Overstreet 
978cafe5635SKent Overstreet 		j = n;
979cafe5635SKent Overstreet 		f = &t->tree[j];
980cafe5635SKent Overstreet 
981944a4f34SColy Li 		if (likely(f->exponent != 127)) {
982944a4f34SColy Li 			if (f->mantissa >= bfloat_mantissa(search, f))
983944a4f34SColy Li 				n = j * 2;
984cafe5635SKent Overstreet 			else
985944a4f34SColy Li 				n = j * 2 + 1;
986944a4f34SColy Li 		} else {
987944a4f34SColy Li 			if (bkey_cmp(tree_to_bkey(t, j), search) > 0)
988944a4f34SColy Li 				n = j * 2;
989944a4f34SColy Li 			else
990944a4f34SColy Li 				n = j * 2 + 1;
991944a4f34SColy Li 		}
992cafe5635SKent Overstreet 	} while (n < t->size);
993cafe5635SKent Overstreet 
994cafe5635SKent Overstreet 	inorder = to_inorder(j, t);
995cafe5635SKent Overstreet 
996cafe5635SKent Overstreet 	/*
997cafe5635SKent Overstreet 	 * n would have been the node we recursed to - the low bit tells us if
998cafe5635SKent Overstreet 	 * we recursed left or recursed right.
999cafe5635SKent Overstreet 	 */
1000cafe5635SKent Overstreet 	if (n & 1) {
1001cafe5635SKent Overstreet 		l = cacheline_to_bkey(t, inorder, f->m);
1002cafe5635SKent Overstreet 
1003cafe5635SKent Overstreet 		if (++inorder != t->size) {
1004cafe5635SKent Overstreet 			f = &t->tree[inorder_next(j, t->size)];
1005cafe5635SKent Overstreet 			r = cacheline_to_bkey(t, inorder, f->m);
1006cafe5635SKent Overstreet 		} else
1007fafff81cSKent Overstreet 			r = bset_bkey_last(t->data);
1008cafe5635SKent Overstreet 	} else {
1009cafe5635SKent Overstreet 		r = cacheline_to_bkey(t, inorder, f->m);
1010cafe5635SKent Overstreet 
1011cafe5635SKent Overstreet 		if (--inorder) {
1012cafe5635SKent Overstreet 			f = &t->tree[inorder_prev(j, t->size)];
1013cafe5635SKent Overstreet 			l = cacheline_to_bkey(t, inorder, f->m);
1014cafe5635SKent Overstreet 		} else
1015cafe5635SKent Overstreet 			l = t->data->start;
1016cafe5635SKent Overstreet 	}
1017cafe5635SKent Overstreet 
1018cafe5635SKent Overstreet 	return (struct bset_search_iter) {l, r};
1019cafe5635SKent Overstreet }
1020cafe5635SKent Overstreet 
__bch_bset_search(struct btree_keys * b,struct bset_tree * t,const struct bkey * search)1021c052dd9aSKent Overstreet struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
1022cafe5635SKent Overstreet 			       const struct bkey *search)
1023cafe5635SKent Overstreet {
1024cafe5635SKent Overstreet 	struct bset_search_iter i;
1025cafe5635SKent Overstreet 
1026cafe5635SKent Overstreet 	/*
1027cafe5635SKent Overstreet 	 * First, we search for a cacheline, then lastly we do a linear search
1028cafe5635SKent Overstreet 	 * within that cacheline.
1029cafe5635SKent Overstreet 	 *
1030cafe5635SKent Overstreet 	 * To search for the cacheline, there's three different possibilities:
1031cafe5635SKent Overstreet 	 *  * The set is too small to have a search tree, so we just do a linear
1032cafe5635SKent Overstreet 	 *    search over the whole set.
1033cafe5635SKent Overstreet 	 *  * The set is the one we're currently inserting into; keeping a full
1034cafe5635SKent Overstreet 	 *    auxiliary search tree up to date would be too expensive, so we
1035cafe5635SKent Overstreet 	 *    use a much simpler lookup table to do a binary search -
1036cafe5635SKent Overstreet 	 *    bset_search_write_set().
1037cafe5635SKent Overstreet 	 *  * Or we use the auxiliary search tree we constructed earlier -
1038cafe5635SKent Overstreet 	 *    bset_search_tree()
1039cafe5635SKent Overstreet 	 */
1040cafe5635SKent Overstreet 
1041cafe5635SKent Overstreet 	if (unlikely(!t->size)) {
1042cafe5635SKent Overstreet 		i.l = t->data->start;
1043fafff81cSKent Overstreet 		i.r = bset_bkey_last(t->data);
1044c052dd9aSKent Overstreet 	} else if (bset_written(b, t)) {
1045cafe5635SKent Overstreet 		/*
1046cafe5635SKent Overstreet 		 * Each node in the auxiliary search tree covers a certain range
1047cafe5635SKent Overstreet 		 * of bits, and keys above and below the set it covers might
1048cafe5635SKent Overstreet 		 * differ outside those bits - so we have to special case the
1049cafe5635SKent Overstreet 		 * start and end - handle that here:
1050cafe5635SKent Overstreet 		 */
1051cafe5635SKent Overstreet 
1052cafe5635SKent Overstreet 		if (unlikely(bkey_cmp(search, &t->end) >= 0))
1053fafff81cSKent Overstreet 			return bset_bkey_last(t->data);
1054cafe5635SKent Overstreet 
1055cafe5635SKent Overstreet 		if (unlikely(bkey_cmp(search, t->data->start) < 0))
1056cafe5635SKent Overstreet 			return t->data->start;
1057cafe5635SKent Overstreet 
1058a85e968eSKent Overstreet 		i = bset_search_tree(t, search);
1059a85e968eSKent Overstreet 	} else {
1060c052dd9aSKent Overstreet 		BUG_ON(!b->nsets &&
1061a85e968eSKent Overstreet 		       t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
1062a85e968eSKent Overstreet 
1063a85e968eSKent Overstreet 		i = bset_search_write_set(t, search);
1064a85e968eSKent Overstreet 	}
1065cafe5635SKent Overstreet 
1066c052dd9aSKent Overstreet 	if (btree_keys_expensive_checks(b)) {
1067c052dd9aSKent Overstreet 		BUG_ON(bset_written(b, t) &&
1068cafe5635SKent Overstreet 		       i.l != t->data->start &&
1069cafe5635SKent Overstreet 		       bkey_cmp(tree_to_prev_bkey(t,
1070cafe5635SKent Overstreet 			  inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
1071cafe5635SKent Overstreet 				search) > 0);
1072cafe5635SKent Overstreet 
1073fafff81cSKent Overstreet 		BUG_ON(i.r != bset_bkey_last(t->data) &&
1074cafe5635SKent Overstreet 		       bkey_cmp(i.r, search) <= 0);
1075280481d0SKent Overstreet 	}
1076cafe5635SKent Overstreet 
1077cafe5635SKent Overstreet 	while (likely(i.l != i.r) &&
1078cafe5635SKent Overstreet 	       bkey_cmp(i.l, search) <= 0)
1079cafe5635SKent Overstreet 		i.l = bkey_next(i.l);
1080cafe5635SKent Overstreet 
1081cafe5635SKent Overstreet 	return i.l;
1082cafe5635SKent Overstreet }
1083cafe5635SKent Overstreet 
1084cafe5635SKent Overstreet /* Btree iterator */
1085cafe5635SKent Overstreet 
1086*866898efSKuan-Wei Chiu typedef bool (new_btree_iter_cmp_fn)(const void *, const void *, void *);
1087911c9610SKent Overstreet 
new_btree_iter_cmp(const void * l,const void * r,void __always_unused * args)1088*866898efSKuan-Wei Chiu static inline bool new_btree_iter_cmp(const void *l, const void *r, void __always_unused *args)
1089cafe5635SKent Overstreet {
1090*866898efSKuan-Wei Chiu 	const struct btree_iter_set *_l = l;
1091*866898efSKuan-Wei Chiu 	const struct btree_iter_set *_r = r;
1092*866898efSKuan-Wei Chiu 
1093*866898efSKuan-Wei Chiu 	return bkey_cmp(_l->k, _r->k) <= 0;
1094*866898efSKuan-Wei Chiu }
1095*866898efSKuan-Wei Chiu 
new_btree_iter_swap(void * iter1,void * iter2,void __always_unused * args)1096*866898efSKuan-Wei Chiu static inline void new_btree_iter_swap(void *iter1, void *iter2, void __always_unused *args)
1097*866898efSKuan-Wei Chiu {
1098*866898efSKuan-Wei Chiu 	struct btree_iter_set *_iter1 = iter1;
1099*866898efSKuan-Wei Chiu 	struct btree_iter_set *_iter2 = iter2;
1100*866898efSKuan-Wei Chiu 
1101*866898efSKuan-Wei Chiu 	swap(*_iter1, *_iter2);
1102cafe5635SKent Overstreet }
1103cafe5635SKent Overstreet 
btree_iter_end(struct btree_iter * iter)1104cafe5635SKent Overstreet static inline bool btree_iter_end(struct btree_iter *iter)
1105cafe5635SKent Overstreet {
1106*866898efSKuan-Wei Chiu 	return !iter->heap.nr;
1107cafe5635SKent Overstreet }
1108cafe5635SKent Overstreet 
bch_btree_iter_push(struct btree_iter * iter,struct bkey * k,struct bkey * end)1109cafe5635SKent Overstreet void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
1110cafe5635SKent Overstreet 			 struct bkey *end)
1111cafe5635SKent Overstreet {
1112*866898efSKuan-Wei Chiu 	const struct min_heap_callbacks callbacks = {
1113*866898efSKuan-Wei Chiu 		.less = new_btree_iter_cmp,
1114*866898efSKuan-Wei Chiu 		.swp = new_btree_iter_swap,
1115*866898efSKuan-Wei Chiu 	};
1116*866898efSKuan-Wei Chiu 
1117cafe5635SKent Overstreet 	if (k != end)
1118*866898efSKuan-Wei Chiu 		BUG_ON(!min_heap_push(&iter->heap,
1119*866898efSKuan-Wei Chiu 				 &((struct btree_iter_set) { k, end }),
1120*866898efSKuan-Wei Chiu 				 &callbacks,
1121*866898efSKuan-Wei Chiu 				 NULL));
1122cafe5635SKent Overstreet }
1123cafe5635SKent Overstreet 
__bch_btree_iter_init(struct btree_keys * b,struct btree_iter * iter,struct bkey * search,struct bset_tree * start)1124*866898efSKuan-Wei Chiu static struct bkey *__bch_btree_iter_init(struct btree_keys *b,
1125*866898efSKuan-Wei Chiu 					  struct btree_iter *iter,
1126911c9610SKent Overstreet 					  struct bkey *search,
1127911c9610SKent Overstreet 					  struct bset_tree *start)
1128cafe5635SKent Overstreet {
1129cafe5635SKent Overstreet 	struct bkey *ret = NULL;
11301fae7cf0SColy Li 
1131*866898efSKuan-Wei Chiu 	iter->heap.size = ARRAY_SIZE(iter->heap.preallocated);
1132*866898efSKuan-Wei Chiu 	iter->heap.nr = 0;
1133cafe5635SKent Overstreet 
1134280481d0SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
1135*866898efSKuan-Wei Chiu 	iter->b = b;
1136280481d0SKent Overstreet #endif
1137280481d0SKent Overstreet 
1138c052dd9aSKent Overstreet 	for (; start <= bset_tree_last(b); start++) {
1139cafe5635SKent Overstreet 		ret = bch_bset_search(b, start, search);
1140*866898efSKuan-Wei Chiu 		bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
1141cafe5635SKent Overstreet 	}
1142cafe5635SKent Overstreet 
1143cafe5635SKent Overstreet 	return ret;
1144cafe5635SKent Overstreet }
1145cafe5635SKent Overstreet 
bch_btree_iter_init(struct btree_keys * b,struct btree_iter * iter,struct bkey * search)1146*866898efSKuan-Wei Chiu struct bkey *bch_btree_iter_init(struct btree_keys *b,
1147*866898efSKuan-Wei Chiu 				 struct btree_iter *iter,
1148911c9610SKent Overstreet 				 struct bkey *search)
1149911c9610SKent Overstreet {
1150*866898efSKuan-Wei Chiu 	return __bch_btree_iter_init(b, iter, search, b->set);
1151911c9610SKent Overstreet }
1152911c9610SKent Overstreet 
__bch_btree_iter_next(struct btree_iter * iter,new_btree_iter_cmp_fn * cmp)1153911c9610SKent Overstreet static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
1154*866898efSKuan-Wei Chiu 						 new_btree_iter_cmp_fn *cmp)
1155cafe5635SKent Overstreet {
115642361469SBart Van Assche 	struct btree_iter_set b __maybe_unused;
1157cafe5635SKent Overstreet 	struct bkey *ret = NULL;
1158*866898efSKuan-Wei Chiu 	const struct min_heap_callbacks callbacks = {
1159*866898efSKuan-Wei Chiu 		.less = cmp,
1160*866898efSKuan-Wei Chiu 		.swp = new_btree_iter_swap,
1161*866898efSKuan-Wei Chiu 	};
1162cafe5635SKent Overstreet 
1163cafe5635SKent Overstreet 	if (!btree_iter_end(iter)) {
1164280481d0SKent Overstreet 		bch_btree_iter_next_check(iter);
1165280481d0SKent Overstreet 
1166*866898efSKuan-Wei Chiu 		ret = iter->heap.data->k;
1167*866898efSKuan-Wei Chiu 		iter->heap.data->k = bkey_next(iter->heap.data->k);
1168cafe5635SKent Overstreet 
1169*866898efSKuan-Wei Chiu 		if (iter->heap.data->k > iter->heap.data->end) {
1170cc0f4eaaSKent Overstreet 			WARN_ONCE(1, "bset was corrupt!\n");
1171*866898efSKuan-Wei Chiu 			iter->heap.data->k = iter->heap.data->end;
1172cafe5635SKent Overstreet 		}
1173cafe5635SKent Overstreet 
1174*866898efSKuan-Wei Chiu 		if (iter->heap.data->k == iter->heap.data->end) {
1175*866898efSKuan-Wei Chiu 			if (iter->heap.nr) {
1176*866898efSKuan-Wei Chiu 				b = min_heap_peek(&iter->heap)[0];
1177*866898efSKuan-Wei Chiu 				min_heap_pop(&iter->heap, &callbacks, NULL);
1178*866898efSKuan-Wei Chiu 			}
1179*866898efSKuan-Wei Chiu 		}
1180cafe5635SKent Overstreet 		else
1181*866898efSKuan-Wei Chiu 			min_heap_sift_down(&iter->heap, 0, &callbacks, NULL);
1182cafe5635SKent Overstreet 	}
1183cafe5635SKent Overstreet 
1184cafe5635SKent Overstreet 	return ret;
1185cafe5635SKent Overstreet }
1186cafe5635SKent Overstreet 
bch_btree_iter_next(struct btree_iter * iter)1187911c9610SKent Overstreet struct bkey *bch_btree_iter_next(struct btree_iter *iter)
1188911c9610SKent Overstreet {
1189*866898efSKuan-Wei Chiu 	return __bch_btree_iter_next(iter, new_btree_iter_cmp);
1190911c9610SKent Overstreet 
1191911c9610SKent Overstreet }
1192911c9610SKent Overstreet 
bch_btree_iter_next_filter(struct btree_iter * iter,struct btree_keys * b,ptr_filter_fn fn)1193cafe5635SKent Overstreet struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
1194a85e968eSKent Overstreet 					struct btree_keys *b, ptr_filter_fn fn)
1195cafe5635SKent Overstreet {
1196cafe5635SKent Overstreet 	struct bkey *ret;
1197cafe5635SKent Overstreet 
1198cafe5635SKent Overstreet 	do {
1199cafe5635SKent Overstreet 		ret = bch_btree_iter_next(iter);
1200cafe5635SKent Overstreet 	} while (ret && fn(b, ret));
1201cafe5635SKent Overstreet 
1202cafe5635SKent Overstreet 	return ret;
1203cafe5635SKent Overstreet }
1204cafe5635SKent Overstreet 
1205cafe5635SKent Overstreet /* Mergesort */
1206cafe5635SKent Overstreet 
bch_bset_sort_state_free(struct bset_sort_state * state)120767539e85SKent Overstreet void bch_bset_sort_state_free(struct bset_sort_state *state)
120867539e85SKent Overstreet {
1209d19936a2SKent Overstreet 	mempool_exit(&state->pool);
121067539e85SKent Overstreet }
121167539e85SKent Overstreet 
bch_bset_sort_state_init(struct bset_sort_state * state,unsigned int page_order)12126f10f7d1SColy Li int bch_bset_sort_state_init(struct bset_sort_state *state,
12136f10f7d1SColy Li 			     unsigned int page_order)
121467539e85SKent Overstreet {
121567539e85SKent Overstreet 	spin_lock_init(&state->time.lock);
121667539e85SKent Overstreet 
121767539e85SKent Overstreet 	state->page_order = page_order;
121867539e85SKent Overstreet 	state->crit_factor = int_sqrt(1 << page_order);
121967539e85SKent Overstreet 
1220d19936a2SKent Overstreet 	return mempool_init_page_pool(&state->pool, 1, page_order);
122167539e85SKent Overstreet }
122267539e85SKent Overstreet 
btree_mergesort(struct btree_keys * b,struct bset * out,struct btree_iter * iter,bool fixup,bool remove_stale)1223a85e968eSKent Overstreet static void btree_mergesort(struct btree_keys *b, struct bset *out,
1224cafe5635SKent Overstreet 			    struct btree_iter *iter,
1225cafe5635SKent Overstreet 			    bool fixup, bool remove_stale)
1226cafe5635SKent Overstreet {
1227cafe5635SKent Overstreet 	struct bkey *k, *last = NULL;
1228ef71ec00SKent Overstreet 	BKEY_PADDED(k) tmp;
1229a85e968eSKent Overstreet 	bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale
1230cafe5635SKent Overstreet 		? bch_ptr_bad
1231cafe5635SKent Overstreet 		: bch_ptr_invalid;
1232*866898efSKuan-Wei Chiu 	const struct min_heap_callbacks callbacks = {
1233*866898efSKuan-Wei Chiu 		.less = b->ops->sort_cmp,
1234*866898efSKuan-Wei Chiu 		.swp = new_btree_iter_swap,
1235*866898efSKuan-Wei Chiu 	};
1236cafe5635SKent Overstreet 
1237911c9610SKent Overstreet 	/* Heapify the iterator, using our comparison function */
1238*866898efSKuan-Wei Chiu 	min_heapify_all(&iter->heap, &callbacks, NULL);
1239911c9610SKent Overstreet 
1240cafe5635SKent Overstreet 	while (!btree_iter_end(iter)) {
124165d45231SKent Overstreet 		if (b->ops->sort_fixup && fixup)
124265d45231SKent Overstreet 			k = b->ops->sort_fixup(iter, &tmp.k);
1243ef71ec00SKent Overstreet 		else
1244ef71ec00SKent Overstreet 			k = NULL;
1245cafe5635SKent Overstreet 
1246ef71ec00SKent Overstreet 		if (!k)
124765d45231SKent Overstreet 			k = __bch_btree_iter_next(iter, b->ops->sort_cmp);
1248ef71ec00SKent Overstreet 
1249cafe5635SKent Overstreet 		if (bad(b, k))
1250cafe5635SKent Overstreet 			continue;
1251cafe5635SKent Overstreet 
1252cafe5635SKent Overstreet 		if (!last) {
1253cafe5635SKent Overstreet 			last = out->start;
1254cafe5635SKent Overstreet 			bkey_copy(last, k);
125565d45231SKent Overstreet 		} else if (!bch_bkey_try_merge(b, last, k)) {
1256cafe5635SKent Overstreet 			last = bkey_next(last);
1257cafe5635SKent Overstreet 			bkey_copy(last, k);
1258cafe5635SKent Overstreet 		}
1259cafe5635SKent Overstreet 	}
1260cafe5635SKent Overstreet 
1261cafe5635SKent Overstreet 	out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1262cafe5635SKent Overstreet 
126346f5aa88SJoe Perches 	pr_debug("sorted %i keys\n", out->keys);
1264cafe5635SKent Overstreet }
1265cafe5635SKent Overstreet 
__btree_sort(struct btree_keys * b,struct btree_iter * iter,unsigned int start,unsigned int order,bool fixup,struct bset_sort_state * state)1266a85e968eSKent Overstreet static void __btree_sort(struct btree_keys *b, struct btree_iter *iter,
12676f10f7d1SColy Li 			 unsigned int start, unsigned int order, bool fixup,
126867539e85SKent Overstreet 			 struct bset_sort_state *state)
1269cafe5635SKent Overstreet {
1270cafe5635SKent Overstreet 	uint64_t start_time;
12710a451145SKent Overstreet 	bool used_mempool = false;
1272501d52a9SKent Overstreet 	struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOWAIT,
1273cafe5635SKent Overstreet 						     order);
1274cafe5635SKent Overstreet 	if (!out) {
12753572324aSKent Overstreet 		struct page *outp;
12763572324aSKent Overstreet 
127767539e85SKent Overstreet 		BUG_ON(order > state->page_order);
127867539e85SKent Overstreet 
1279d19936a2SKent Overstreet 		outp = mempool_alloc(&state->pool, GFP_NOIO);
12803572324aSKent Overstreet 		out = page_address(outp);
12810a451145SKent Overstreet 		used_mempool = true;
1282a85e968eSKent Overstreet 		order = state->page_order;
1283cafe5635SKent Overstreet 	}
1284cafe5635SKent Overstreet 
1285cafe5635SKent Overstreet 	start_time = local_clock();
1286cafe5635SKent Overstreet 
128767539e85SKent Overstreet 	btree_mergesort(b, out, iter, fixup, false);
1288cafe5635SKent Overstreet 	b->nsets = start;
1289cafe5635SKent Overstreet 
1290cafe5635SKent Overstreet 	if (!start && order == b->page_order) {
1291cafe5635SKent Overstreet 		/*
1292cafe5635SKent Overstreet 		 * Our temporary buffer is the same size as the btree node's
1293cafe5635SKent Overstreet 		 * buffer, we can just swap buffers instead of doing a big
1294cafe5635SKent Overstreet 		 * memcpy()
12957a0bc2a8SColy Li 		 *
12967a0bc2a8SColy Li 		 * Don't worry event 'out' is allocated from mempool, it can
12977a0bc2a8SColy Li 		 * still be swapped here. Because state->pool is a page mempool
129811e529ccSJules Maselbas 		 * created by mempool_init_page_pool(), which allocates
12997a0bc2a8SColy Li 		 * pages by alloc_pages() indeed.
1300cafe5635SKent Overstreet 		 */
1301cafe5635SKent Overstreet 
1302a85e968eSKent Overstreet 		out->magic	= b->set->data->magic;
1303a85e968eSKent Overstreet 		out->seq	= b->set->data->seq;
1304a85e968eSKent Overstreet 		out->version	= b->set->data->version;
1305a85e968eSKent Overstreet 		swap(out, b->set->data);
1306cafe5635SKent Overstreet 	} else {
1307a85e968eSKent Overstreet 		b->set[start].data->keys = out->keys;
1308a85e968eSKent Overstreet 		memcpy(b->set[start].data->start, out->start,
1309fafff81cSKent Overstreet 		       (void *) bset_bkey_last(out) - (void *) out->start);
1310cafe5635SKent Overstreet 	}
1311cafe5635SKent Overstreet 
13120a451145SKent Overstreet 	if (used_mempool)
1313d19936a2SKent Overstreet 		mempool_free(virt_to_page(out), &state->pool);
1314cafe5635SKent Overstreet 	else
1315cafe5635SKent Overstreet 		free_pages((unsigned long) out, order);
1316cafe5635SKent Overstreet 
1317a85e968eSKent Overstreet 	bch_bset_build_written_tree(b);
1318cafe5635SKent Overstreet 
131965d22e91SKent Overstreet 	if (!start)
132067539e85SKent Overstreet 		bch_time_stats_update(&state->time, start_time);
1321cafe5635SKent Overstreet }
1322cafe5635SKent Overstreet 
bch_btree_sort_partial(struct btree_keys * b,unsigned int start,struct bset_sort_state * state)13236f10f7d1SColy Li void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,
132467539e85SKent Overstreet 			    struct bset_sort_state *state)
1325cafe5635SKent Overstreet {
132689ebb4a2SKent Overstreet 	size_t order = b->page_order, keys = 0;
1327*866898efSKuan-Wei Chiu 	struct btree_iter iter;
132889ebb4a2SKent Overstreet 	int oldsize = bch_count_data(b);
1329280481d0SKent Overstreet 
1330*866898efSKuan-Wei Chiu 	min_heap_init(&iter.heap, NULL, MAX_BSETS);
1331*866898efSKuan-Wei Chiu 	__bch_btree_iter_init(b, &iter, NULL, &b->set[start]);
1332cafe5635SKent Overstreet 
1333cafe5635SKent Overstreet 	if (start) {
13346f10f7d1SColy Li 		unsigned int i;
1335cafe5635SKent Overstreet 
133689ebb4a2SKent Overstreet 		for (i = start; i <= b->nsets; i++)
133789ebb4a2SKent Overstreet 			keys += b->set[i].data->keys;
1338cafe5635SKent Overstreet 
133989ebb4a2SKent Overstreet 		order = get_order(__set_bytes(b->set->data, keys));
1340cafe5635SKent Overstreet 	}
1341cafe5635SKent Overstreet 
1342*866898efSKuan-Wei Chiu 	__btree_sort(b, &iter, start, order, false, state);
1343cafe5635SKent Overstreet 
134489ebb4a2SKent Overstreet 	EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize);
1345cafe5635SKent Overstreet }
1346cafe5635SKent Overstreet 
bch_btree_sort_and_fix_extents(struct btree_keys * b,struct btree_iter * iter,struct bset_sort_state * state)1347a85e968eSKent Overstreet void bch_btree_sort_and_fix_extents(struct btree_keys *b,
1348a85e968eSKent Overstreet 				    struct btree_iter *iter,
134967539e85SKent Overstreet 				    struct bset_sort_state *state)
1350cafe5635SKent Overstreet {
135167539e85SKent Overstreet 	__btree_sort(b, iter, 0, b->page_order, true, state);
1352cafe5635SKent Overstreet }
1353cafe5635SKent Overstreet 
bch_btree_sort_into(struct btree_keys * b,struct btree_keys * new,struct bset_sort_state * state)135489ebb4a2SKent Overstreet void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
135567539e85SKent Overstreet 			 struct bset_sort_state *state)
1356cafe5635SKent Overstreet {
1357cafe5635SKent Overstreet 	uint64_t start_time = local_clock();
1358*866898efSKuan-Wei Chiu 	struct btree_iter iter;
13591fae7cf0SColy Li 
1360*866898efSKuan-Wei Chiu 	min_heap_init(&iter.heap, NULL, MAX_BSETS);
1361cafe5635SKent Overstreet 
1362*866898efSKuan-Wei Chiu 	bch_btree_iter_init(b, &iter, NULL);
1363*866898efSKuan-Wei Chiu 
1364*866898efSKuan-Wei Chiu 	btree_mergesort(b, new->set->data, &iter, false, true);
1365cafe5635SKent Overstreet 
136667539e85SKent Overstreet 	bch_time_stats_update(&state->time, start_time);
1367cafe5635SKent Overstreet 
136889ebb4a2SKent Overstreet 	new->set->size = 0; // XXX: why?
1369cafe5635SKent Overstreet }
1370cafe5635SKent Overstreet 
13716ded34d1SKent Overstreet #define SORT_CRIT	(4096 / sizeof(uint64_t))
13726ded34d1SKent Overstreet 
bch_btree_sort_lazy(struct btree_keys * b,struct bset_sort_state * state)137389ebb4a2SKent Overstreet void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state)
1374cafe5635SKent Overstreet {
13756f10f7d1SColy Li 	unsigned int crit = SORT_CRIT;
13766ded34d1SKent Overstreet 	int i;
1377cafe5635SKent Overstreet 
13786ded34d1SKent Overstreet 	/* Don't sort if nothing to do */
137989ebb4a2SKent Overstreet 	if (!b->nsets)
13806ded34d1SKent Overstreet 		goto out;
1381cafe5635SKent Overstreet 
138289ebb4a2SKent Overstreet 	for (i = b->nsets - 1; i >= 0; --i) {
138367539e85SKent Overstreet 		crit *= state->crit_factor;
13846ded34d1SKent Overstreet 
138589ebb4a2SKent Overstreet 		if (b->set[i].data->keys < crit) {
138667539e85SKent Overstreet 			bch_btree_sort_partial(b, i, state);
13876ded34d1SKent Overstreet 			return;
13886ded34d1SKent Overstreet 		}
1389cafe5635SKent Overstreet 	}
1390cafe5635SKent Overstreet 
13916ded34d1SKent Overstreet 	/* Sort if we'd overflow */
139289ebb4a2SKent Overstreet 	if (b->nsets + 1 == MAX_BSETS) {
139367539e85SKent Overstreet 		bch_btree_sort(b, state);
13946ded34d1SKent Overstreet 		return;
13956ded34d1SKent Overstreet 	}
13966ded34d1SKent Overstreet 
13976ded34d1SKent Overstreet out:
139889ebb4a2SKent Overstreet 	bch_bset_build_written_tree(b);
1399cafe5635SKent Overstreet }
1400cafe5635SKent Overstreet 
bch_btree_keys_stats(struct btree_keys * b,struct bset_stats * stats)1401f67342ddSKent Overstreet void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats)
1402cafe5635SKent Overstreet {
14036f10f7d1SColy Li 	unsigned int i;
1404cafe5635SKent Overstreet 
1405f67342ddSKent Overstreet 	for (i = 0; i <= b->nsets; i++) {
1406f67342ddSKent Overstreet 		struct bset_tree *t = &b->set[i];
1407cafe5635SKent Overstreet 		size_t bytes = t->data->keys * sizeof(uint64_t);
1408cafe5635SKent Overstreet 		size_t j;
1409cafe5635SKent Overstreet 
1410f67342ddSKent Overstreet 		if (bset_written(b, t)) {
1411cafe5635SKent Overstreet 			stats->sets_written++;
1412cafe5635SKent Overstreet 			stats->bytes_written += bytes;
1413cafe5635SKent Overstreet 
1414cafe5635SKent Overstreet 			stats->floats += t->size - 1;
1415cafe5635SKent Overstreet 
1416cafe5635SKent Overstreet 			for (j = 1; j < t->size; j++)
1417cafe5635SKent Overstreet 				if (t->tree[j].exponent == 127)
1418cafe5635SKent Overstreet 					stats->failed++;
1419cafe5635SKent Overstreet 		} else {
1420cafe5635SKent Overstreet 			stats->sets_unwritten++;
1421cafe5635SKent Overstreet 			stats->bytes_unwritten += bytes;
1422cafe5635SKent Overstreet 		}
1423cafe5635SKent Overstreet 	}
1424cafe5635SKent Overstreet }
1425