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*6479b9f4SMatthew Mirvish struct btree_iter_stack iter;
58dc9d98d6SKent Overstreet struct bkey *k;
59dc9d98d6SKent Overstreet
60dc9d98d6SKent Overstreet if (b->ops->is_extents)
61dc9d98d6SKent Overstreet for_each_key(b, k, &iter)
62dc9d98d6SKent Overstreet ret += KEY_SIZE(k);
63dc9d98d6SKent Overstreet return ret;
64dc9d98d6SKent Overstreet }
65dc9d98d6SKent Overstreet
__bch_check_keys(struct btree_keys * b,const char * fmt,...)66dc9d98d6SKent Overstreet void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)
67dc9d98d6SKent Overstreet {
68dc9d98d6SKent Overstreet va_list args;
69dc9d98d6SKent Overstreet struct bkey *k, *p = NULL;
70*6479b9f4SMatthew Mirvish struct btree_iter_stack iter;
71dc9d98d6SKent Overstreet const char *err;
72dc9d98d6SKent Overstreet
73dc9d98d6SKent Overstreet for_each_key(b, k, &iter) {
74dc9d98d6SKent Overstreet if (b->ops->is_extents) {
75dc9d98d6SKent Overstreet err = "Keys out of order";
76dc9d98d6SKent Overstreet if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
77dc9d98d6SKent Overstreet goto bug;
78dc9d98d6SKent Overstreet
79dc9d98d6SKent Overstreet if (bch_ptr_invalid(b, k))
80dc9d98d6SKent Overstreet continue;
81dc9d98d6SKent Overstreet
82dc9d98d6SKent Overstreet err = "Overlapping keys";
83dc9d98d6SKent Overstreet if (p && bkey_cmp(p, &START_KEY(k)) > 0)
84dc9d98d6SKent Overstreet goto bug;
85dc9d98d6SKent Overstreet } else {
86dc9d98d6SKent Overstreet if (bch_ptr_bad(b, k))
87dc9d98d6SKent Overstreet continue;
88dc9d98d6SKent Overstreet
89dc9d98d6SKent Overstreet err = "Duplicate keys";
90dc9d98d6SKent Overstreet if (p && !bkey_cmp(p, k))
91dc9d98d6SKent Overstreet goto bug;
92dc9d98d6SKent Overstreet }
93dc9d98d6SKent Overstreet p = k;
94dc9d98d6SKent Overstreet }
95dc9d98d6SKent Overstreet #if 0
96dc9d98d6SKent Overstreet err = "Key larger than btree node key";
97dc9d98d6SKent Overstreet if (p && bkey_cmp(p, &b->key) > 0)
98dc9d98d6SKent Overstreet goto bug;
99dc9d98d6SKent Overstreet #endif
100dc9d98d6SKent Overstreet return;
101dc9d98d6SKent Overstreet bug:
102dc9d98d6SKent Overstreet bch_dump_bucket(b);
103dc9d98d6SKent Overstreet
104dc9d98d6SKent Overstreet va_start(args, fmt);
105dc9d98d6SKent Overstreet vprintk(fmt, args);
106dc9d98d6SKent Overstreet va_end(args);
107dc9d98d6SKent Overstreet
108dc9d98d6SKent Overstreet panic("bch_check_keys error: %s:\n", err);
109dc9d98d6SKent Overstreet }
110dc9d98d6SKent Overstreet
bch_btree_iter_next_check(struct btree_iter * iter)111dc9d98d6SKent Overstreet static void bch_btree_iter_next_check(struct btree_iter *iter)
112dc9d98d6SKent Overstreet {
113dc9d98d6SKent Overstreet struct bkey *k = iter->data->k, *next = bkey_next(k);
114dc9d98d6SKent Overstreet
115dc9d98d6SKent Overstreet if (next < iter->data->end &&
116dc9d98d6SKent Overstreet bkey_cmp(k, iter->b->ops->is_extents ?
117dc9d98d6SKent Overstreet &START_KEY(next) : next) > 0) {
118dc9d98d6SKent Overstreet bch_dump_bucket(iter->b);
119dc9d98d6SKent Overstreet panic("Key skipped backwards\n");
120dc9d98d6SKent Overstreet }
121dc9d98d6SKent Overstreet }
122dc9d98d6SKent Overstreet
123dc9d98d6SKent Overstreet #else
124dc9d98d6SKent Overstreet
bch_btree_iter_next_check(struct btree_iter * iter)125dc9d98d6SKent Overstreet static inline void bch_btree_iter_next_check(struct btree_iter *iter) {}
126dc9d98d6SKent Overstreet
127dc9d98d6SKent Overstreet #endif
128dc9d98d6SKent Overstreet
129cafe5635SKent Overstreet /* Keylists */
130cafe5635SKent Overstreet
__bch_keylist_realloc(struct keylist * l,unsigned int u64s)1316f10f7d1SColy Li int __bch_keylist_realloc(struct keylist *l, unsigned int u64s)
132cafe5635SKent Overstreet {
133c2f95ae2SKent Overstreet size_t oldsize = bch_keylist_nkeys(l);
134085d2a3dSKent Overstreet size_t newsize = oldsize + u64s;
135c2f95ae2SKent Overstreet uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
136c2f95ae2SKent Overstreet uint64_t *new_keys;
137cafe5635SKent Overstreet
138cafe5635SKent Overstreet newsize = roundup_pow_of_two(newsize);
139cafe5635SKent Overstreet
140cafe5635SKent Overstreet if (newsize <= KEYLIST_INLINE ||
141cafe5635SKent Overstreet roundup_pow_of_two(oldsize) == newsize)
142cafe5635SKent Overstreet return 0;
143cafe5635SKent Overstreet
144c2f95ae2SKent Overstreet new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
145cafe5635SKent Overstreet
146c2f95ae2SKent Overstreet if (!new_keys)
147cafe5635SKent Overstreet return -ENOMEM;
148cafe5635SKent Overstreet
149c2f95ae2SKent Overstreet if (!old_keys)
150c2f95ae2SKent Overstreet memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
151cafe5635SKent Overstreet
152c2f95ae2SKent Overstreet l->keys_p = new_keys;
153c2f95ae2SKent Overstreet l->top_p = new_keys + oldsize;
154cafe5635SKent Overstreet
155cafe5635SKent Overstreet return 0;
156cafe5635SKent Overstreet }
157cafe5635SKent Overstreet
15806c1526dSColy Li /* Pop the top key of keylist by pointing l->top to its previous key */
bch_keylist_pop(struct keylist * l)159cafe5635SKent Overstreet struct bkey *bch_keylist_pop(struct keylist *l)
160cafe5635SKent Overstreet {
161c2f95ae2SKent Overstreet struct bkey *k = l->keys;
162cafe5635SKent Overstreet
163cafe5635SKent Overstreet if (k == l->top)
164cafe5635SKent Overstreet return NULL;
165cafe5635SKent Overstreet
166cafe5635SKent Overstreet while (bkey_next(k) != l->top)
167cafe5635SKent Overstreet k = bkey_next(k);
168cafe5635SKent Overstreet
169cafe5635SKent Overstreet return l->top = k;
170cafe5635SKent Overstreet }
171cafe5635SKent Overstreet
17206c1526dSColy Li /* Pop the bottom key of keylist and update l->top_p */
bch_keylist_pop_front(struct keylist * l)17326c949f8SKent Overstreet void bch_keylist_pop_front(struct keylist *l)
17426c949f8SKent Overstreet {
175c2f95ae2SKent Overstreet l->top_p -= bkey_u64s(l->keys);
17626c949f8SKent Overstreet
177c2f95ae2SKent Overstreet memmove(l->keys,
178c2f95ae2SKent Overstreet bkey_next(l->keys),
179c2f95ae2SKent Overstreet bch_keylist_bytes(l));
18026c949f8SKent Overstreet }
18126c949f8SKent Overstreet
182cafe5635SKent Overstreet /* Key/pointer manipulation */
183cafe5635SKent Overstreet
bch_bkey_copy_single_ptr(struct bkey * dest,const struct bkey * src,unsigned int i)184cafe5635SKent Overstreet void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
1856f10f7d1SColy Li unsigned int i)
186cafe5635SKent Overstreet {
187cafe5635SKent Overstreet BUG_ON(i > KEY_PTRS(src));
188cafe5635SKent Overstreet
189cafe5635SKent Overstreet /* Only copy the header, key, and one pointer. */
190cafe5635SKent Overstreet memcpy(dest, src, 2 * sizeof(uint64_t));
191cafe5635SKent Overstreet dest->ptr[0] = src->ptr[i];
192cafe5635SKent Overstreet SET_KEY_PTRS(dest, 1);
193cafe5635SKent Overstreet /* We didn't copy the checksum so clear that bit. */
194cafe5635SKent Overstreet SET_KEY_CSUM(dest, 0);
195cafe5635SKent Overstreet }
196cafe5635SKent Overstreet
__bch_cut_front(const struct bkey * where,struct bkey * k)197cafe5635SKent Overstreet bool __bch_cut_front(const struct bkey *where, struct bkey *k)
198cafe5635SKent Overstreet {
1996f10f7d1SColy Li unsigned int i, len = 0;
200cafe5635SKent Overstreet
201cafe5635SKent Overstreet if (bkey_cmp(where, &START_KEY(k)) <= 0)
202cafe5635SKent Overstreet return false;
203cafe5635SKent Overstreet
204cafe5635SKent Overstreet if (bkey_cmp(where, k) < 0)
205cafe5635SKent Overstreet len = KEY_OFFSET(k) - KEY_OFFSET(where);
206cafe5635SKent Overstreet else
207cafe5635SKent Overstreet bkey_copy_key(k, where);
208cafe5635SKent Overstreet
209cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(k); i++)
210cafe5635SKent Overstreet SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
211cafe5635SKent Overstreet
212cafe5635SKent Overstreet BUG_ON(len > KEY_SIZE(k));
213cafe5635SKent Overstreet SET_KEY_SIZE(k, len);
214cafe5635SKent Overstreet return true;
215cafe5635SKent Overstreet }
216cafe5635SKent Overstreet
__bch_cut_back(const struct bkey * where,struct bkey * k)217cafe5635SKent Overstreet bool __bch_cut_back(const struct bkey *where, struct bkey *k)
218cafe5635SKent Overstreet {
2196f10f7d1SColy Li unsigned int len = 0;
220cafe5635SKent Overstreet
221cafe5635SKent Overstreet if (bkey_cmp(where, k) >= 0)
222cafe5635SKent Overstreet return false;
223cafe5635SKent Overstreet
224cafe5635SKent Overstreet BUG_ON(KEY_INODE(where) != KEY_INODE(k));
225cafe5635SKent Overstreet
226cafe5635SKent Overstreet if (bkey_cmp(where, &START_KEY(k)) > 0)
227cafe5635SKent Overstreet len = KEY_OFFSET(where) - KEY_START(k);
228cafe5635SKent Overstreet
229cafe5635SKent Overstreet bkey_copy_key(k, where);
230cafe5635SKent Overstreet
231cafe5635SKent Overstreet BUG_ON(len > KEY_SIZE(k));
232cafe5635SKent Overstreet SET_KEY_SIZE(k, len);
233cafe5635SKent Overstreet return true;
234cafe5635SKent Overstreet }
235cafe5635SKent Overstreet
236ee811287SKent Overstreet /* Auxiliary search trees */
237ee811287SKent Overstreet
238ee811287SKent Overstreet /* 32 bits total: */
239ee811287SKent Overstreet #define BKEY_MID_BITS 3
240ee811287SKent Overstreet #define BKEY_EXPONENT_BITS 7
241ee811287SKent Overstreet #define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
242ee811287SKent Overstreet #define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1)
243ee811287SKent Overstreet
244ee811287SKent Overstreet struct bkey_float {
2456f10f7d1SColy Li unsigned int exponent:BKEY_EXPONENT_BITS;
2466f10f7d1SColy Li unsigned int m:BKEY_MID_BITS;
2476f10f7d1SColy Li unsigned int mantissa:BKEY_MANTISSA_BITS;
248ee811287SKent Overstreet } __packed;
249ee811287SKent Overstreet
250ee811287SKent Overstreet /*
251ee811287SKent Overstreet * BSET_CACHELINE was originally intended to match the hardware cacheline size -
252ee811287SKent Overstreet * it used to be 64, but I realized the lookup code would touch slightly less
253ee811287SKent Overstreet * memory if it was 128.
254ee811287SKent Overstreet *
255ee811287SKent Overstreet * It definites the number of bytes (in struct bset) per struct bkey_float in
256ee811287SKent Overstreet * the auxiliar search tree - when we're done searching the bset_float tree we
257ee811287SKent Overstreet * have this many bytes left that we do a linear search over.
258ee811287SKent Overstreet *
259ee811287SKent Overstreet * Since (after level 5) every level of the bset_tree is on a new cacheline,
260ee811287SKent Overstreet * we're touching one fewer cacheline in the bset tree in exchange for one more
261ee811287SKent Overstreet * cacheline in the linear search - but the linear search might stop before it
262ee811287SKent Overstreet * gets to the second cacheline.
263ee811287SKent Overstreet */
264ee811287SKent Overstreet
265ee811287SKent Overstreet #define BSET_CACHELINE 128
266ee811287SKent Overstreet
267ee811287SKent Overstreet /* Space required for the btree node keys */
btree_keys_bytes(struct btree_keys * b)268a85e968eSKent Overstreet static inline size_t btree_keys_bytes(struct btree_keys *b)
269ee811287SKent Overstreet {
270ee811287SKent Overstreet return PAGE_SIZE << b->page_order;
271ee811287SKent Overstreet }
272ee811287SKent Overstreet
btree_keys_cachelines(struct btree_keys * b)273a85e968eSKent Overstreet static inline size_t btree_keys_cachelines(struct btree_keys *b)
274ee811287SKent Overstreet {
275ee811287SKent Overstreet return btree_keys_bytes(b) / BSET_CACHELINE;
276ee811287SKent Overstreet }
277ee811287SKent Overstreet
278ee811287SKent Overstreet /* Space required for the auxiliary search trees */
bset_tree_bytes(struct btree_keys * b)279a85e968eSKent Overstreet static inline size_t bset_tree_bytes(struct btree_keys *b)
280ee811287SKent Overstreet {
281ee811287SKent Overstreet return btree_keys_cachelines(b) * sizeof(struct bkey_float);
282ee811287SKent Overstreet }
283ee811287SKent Overstreet
284ee811287SKent Overstreet /* Space required for the prev pointers */
bset_prev_bytes(struct btree_keys * b)285a85e968eSKent Overstreet static inline size_t bset_prev_bytes(struct btree_keys *b)
286ee811287SKent Overstreet {
287ee811287SKent Overstreet return btree_keys_cachelines(b) * sizeof(uint8_t);
288ee811287SKent Overstreet }
289ee811287SKent Overstreet
290ee811287SKent Overstreet /* Memory allocation */
291ee811287SKent Overstreet
bch_btree_keys_free(struct btree_keys * b)292a85e968eSKent Overstreet void bch_btree_keys_free(struct btree_keys *b)
293ee811287SKent Overstreet {
294a85e968eSKent Overstreet struct bset_tree *t = b->set;
295ee811287SKent Overstreet
296ee811287SKent Overstreet if (bset_prev_bytes(b) < PAGE_SIZE)
297ee811287SKent Overstreet kfree(t->prev);
298ee811287SKent Overstreet else
299ee811287SKent Overstreet free_pages((unsigned long) t->prev,
300ee811287SKent Overstreet get_order(bset_prev_bytes(b)));
301ee811287SKent Overstreet
302ee811287SKent Overstreet if (bset_tree_bytes(b) < PAGE_SIZE)
303ee811287SKent Overstreet kfree(t->tree);
304ee811287SKent Overstreet else
305ee811287SKent Overstreet free_pages((unsigned long) t->tree,
306ee811287SKent Overstreet get_order(bset_tree_bytes(b)));
307ee811287SKent Overstreet
308ee811287SKent Overstreet free_pages((unsigned long) t->data, b->page_order);
309ee811287SKent Overstreet
310ee811287SKent Overstreet t->prev = NULL;
311ee811287SKent Overstreet t->tree = NULL;
312ee811287SKent Overstreet t->data = NULL;
313ee811287SKent Overstreet }
314ee811287SKent Overstreet
bch_btree_keys_alloc(struct btree_keys * b,unsigned int page_order,gfp_t gfp)315b0d30981SColy Li int bch_btree_keys_alloc(struct btree_keys *b,
316b0d30981SColy Li unsigned int page_order,
317b0d30981SColy Li gfp_t gfp)
318ee811287SKent Overstreet {
319a85e968eSKent Overstreet struct bset_tree *t = b->set;
320ee811287SKent Overstreet
321ee811287SKent Overstreet BUG_ON(t->data);
322ee811287SKent Overstreet
323ee811287SKent Overstreet b->page_order = page_order;
324ee811287SKent Overstreet
3255fe48867SColy Li t->data = (void *) __get_free_pages(__GFP_COMP|gfp, b->page_order);
326ee811287SKent Overstreet if (!t->data)
327ee811287SKent Overstreet goto err;
328ee811287SKent Overstreet
329ee811287SKent Overstreet t->tree = bset_tree_bytes(b) < PAGE_SIZE
330ee811287SKent Overstreet ? kmalloc(bset_tree_bytes(b), gfp)
331ee811287SKent Overstreet : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
332ee811287SKent Overstreet if (!t->tree)
333ee811287SKent Overstreet goto err;
334ee811287SKent Overstreet
335ee811287SKent Overstreet t->prev = bset_prev_bytes(b) < PAGE_SIZE
336ee811287SKent Overstreet ? kmalloc(bset_prev_bytes(b), gfp)
337ee811287SKent Overstreet : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
338ee811287SKent Overstreet if (!t->prev)
339ee811287SKent Overstreet goto err;
340ee811287SKent Overstreet
341ee811287SKent Overstreet return 0;
342ee811287SKent Overstreet err:
343ee811287SKent Overstreet bch_btree_keys_free(b);
344ee811287SKent Overstreet return -ENOMEM;
345ee811287SKent Overstreet }
346a85e968eSKent Overstreet
bch_btree_keys_init(struct btree_keys * b,const struct btree_keys_ops * ops,bool * expensive_debug_checks)347a85e968eSKent Overstreet void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
348a85e968eSKent Overstreet bool *expensive_debug_checks)
349a85e968eSKent Overstreet {
350a85e968eSKent Overstreet b->ops = ops;
351a85e968eSKent Overstreet b->expensive_debug_checks = expensive_debug_checks;
352a85e968eSKent Overstreet b->nsets = 0;
353a85e968eSKent Overstreet b->last_set_unwritten = 0;
354a85e968eSKent Overstreet
355a85e968eSKent Overstreet /*
356bd9026c8SColy Li * struct btree_keys in embedded in struct btree, and struct
357bd9026c8SColy Li * bset_tree is embedded into struct btree_keys. They are all
358bd9026c8SColy Li * initialized as 0 by kzalloc() in mca_bucket_alloc(), and
359bd9026c8SColy Li * b->set[0].data is allocated in bch_btree_keys_alloc(), so we
360bd9026c8SColy Li * don't have to initiate b->set[].size and b->set[].data here
361bd9026c8SColy Li * any more.
362a85e968eSKent Overstreet */
363a85e968eSKent Overstreet }
364ee811287SKent Overstreet
365cafe5635SKent Overstreet /* Binary tree stuff for auxiliary search trees */
366cafe5635SKent Overstreet
367b467a6acSColy Li /*
368b467a6acSColy Li * return array index next to j when does in-order traverse
369b467a6acSColy Li * of a binary tree which is stored in a linear array
370b467a6acSColy Li */
inorder_next(unsigned int j,unsigned int size)3716f10f7d1SColy Li static unsigned int inorder_next(unsigned int j, unsigned int size)
372cafe5635SKent Overstreet {
373cafe5635SKent Overstreet if (j * 2 + 1 < size) {
374cafe5635SKent Overstreet j = j * 2 + 1;
375cafe5635SKent Overstreet
376cafe5635SKent Overstreet while (j * 2 < size)
377cafe5635SKent Overstreet j *= 2;
378cafe5635SKent Overstreet } else
379cafe5635SKent Overstreet j >>= ffz(j) + 1;
380cafe5635SKent Overstreet
381cafe5635SKent Overstreet return j;
382cafe5635SKent Overstreet }
383cafe5635SKent Overstreet
384b467a6acSColy Li /*
385b467a6acSColy Li * return array index previous to j when does in-order traverse
386b467a6acSColy Li * of a binary tree which is stored in a linear array
387b467a6acSColy Li */
inorder_prev(unsigned int j,unsigned int size)3886f10f7d1SColy Li static unsigned int inorder_prev(unsigned int j, unsigned int size)
389cafe5635SKent Overstreet {
390cafe5635SKent Overstreet if (j * 2 < size) {
391cafe5635SKent Overstreet j = j * 2;
392cafe5635SKent Overstreet
393cafe5635SKent Overstreet while (j * 2 + 1 < size)
394cafe5635SKent Overstreet j = j * 2 + 1;
395cafe5635SKent Overstreet } else
396cafe5635SKent Overstreet j >>= ffs(j);
397cafe5635SKent Overstreet
398cafe5635SKent Overstreet return j;
399cafe5635SKent Overstreet }
400cafe5635SKent Overstreet
4013be11dbaSColy Li /*
4023be11dbaSColy Li * I have no idea why this code works... and I'm the one who wrote it
403cafe5635SKent Overstreet *
404cafe5635SKent Overstreet * However, I do know what it does:
405cafe5635SKent Overstreet * Given a binary tree constructed in an array (i.e. how you normally implement
406cafe5635SKent Overstreet * a heap), it converts a node in the tree - referenced by array index - to the
407cafe5635SKent Overstreet * index it would have if you did an inorder traversal.
408cafe5635SKent Overstreet *
409cafe5635SKent Overstreet * Also tested for every j, size up to size somewhere around 6 million.
410cafe5635SKent Overstreet *
411cafe5635SKent Overstreet * The binary tree starts at array index 1, not 0
412cafe5635SKent Overstreet * extra is a function of size:
413cafe5635SKent Overstreet * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
414cafe5635SKent Overstreet */
__to_inorder(unsigned int j,unsigned int size,unsigned int extra)4156f10f7d1SColy Li static unsigned int __to_inorder(unsigned int j,
4166f10f7d1SColy Li unsigned int size,
4176f10f7d1SColy Li unsigned int extra)
418cafe5635SKent Overstreet {
4196f10f7d1SColy Li unsigned int b = fls(j);
4206f10f7d1SColy Li unsigned int shift = fls(size - 1) - b;
421cafe5635SKent Overstreet
422cafe5635SKent Overstreet j ^= 1U << (b - 1);
423cafe5635SKent Overstreet j <<= 1;
424cafe5635SKent Overstreet j |= 1;
425cafe5635SKent Overstreet j <<= shift;
426cafe5635SKent Overstreet
427cafe5635SKent Overstreet if (j > extra)
428cafe5635SKent Overstreet j -= (j - extra) >> 1;
429cafe5635SKent Overstreet
430cafe5635SKent Overstreet return j;
431cafe5635SKent Overstreet }
432cafe5635SKent Overstreet
433b467a6acSColy Li /*
434b467a6acSColy Li * Return the cacheline index in bset_tree->data, where j is index
435b467a6acSColy Li * from a linear array which stores the auxiliar binary tree
436b467a6acSColy Li */
to_inorder(unsigned int j,struct bset_tree * t)4376f10f7d1SColy Li static unsigned int to_inorder(unsigned int j, struct bset_tree *t)
438cafe5635SKent Overstreet {
439cafe5635SKent Overstreet return __to_inorder(j, t->size, t->extra);
440cafe5635SKent Overstreet }
441cafe5635SKent Overstreet
__inorder_to_tree(unsigned int j,unsigned int size,unsigned int extra)4426f10f7d1SColy Li static unsigned int __inorder_to_tree(unsigned int j,
4436f10f7d1SColy Li unsigned int size,
4446f10f7d1SColy Li unsigned int extra)
445cafe5635SKent Overstreet {
4466f10f7d1SColy Li unsigned int shift;
447cafe5635SKent Overstreet
448cafe5635SKent Overstreet if (j > extra)
449cafe5635SKent Overstreet j += j - extra;
450cafe5635SKent Overstreet
451cafe5635SKent Overstreet shift = ffs(j);
452cafe5635SKent Overstreet
453cafe5635SKent Overstreet j >>= shift;
454cafe5635SKent Overstreet j |= roundup_pow_of_two(size) >> shift;
455cafe5635SKent Overstreet
456cafe5635SKent Overstreet return j;
457cafe5635SKent Overstreet }
458cafe5635SKent Overstreet
459b467a6acSColy Li /*
460b467a6acSColy Li * Return an index from a linear array which stores the auxiliar binary
461b467a6acSColy Li * tree, j is the cacheline index of t->data.
462b467a6acSColy Li */
inorder_to_tree(unsigned int j,struct bset_tree * t)4636f10f7d1SColy Li static unsigned int inorder_to_tree(unsigned int j, struct bset_tree *t)
464cafe5635SKent Overstreet {
465cafe5635SKent Overstreet return __inorder_to_tree(j, t->size, t->extra);
466cafe5635SKent Overstreet }
467cafe5635SKent Overstreet
468cafe5635SKent Overstreet #if 0
469cafe5635SKent Overstreet void inorder_test(void)
470cafe5635SKent Overstreet {
471cafe5635SKent Overstreet unsigned long done = 0;
472cafe5635SKent Overstreet ktime_t start = ktime_get();
473cafe5635SKent Overstreet
4746f10f7d1SColy Li for (unsigned int size = 2;
475cafe5635SKent Overstreet size < 65536000;
476cafe5635SKent Overstreet size++) {
477b0d30981SColy Li unsigned int extra =
478b0d30981SColy Li (size - rounddown_pow_of_two(size - 1)) << 1;
4796f10f7d1SColy Li unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
480cafe5635SKent Overstreet
481cafe5635SKent Overstreet if (!(size % 4096))
4826ae63e35SColy Li pr_notice("loop %u, %llu per us\n", size,
483cafe5635SKent Overstreet done / ktime_us_delta(ktime_get(), start));
484cafe5635SKent Overstreet
485cafe5635SKent Overstreet while (1) {
486cafe5635SKent Overstreet if (__inorder_to_tree(i, size, extra) != j)
487cafe5635SKent Overstreet panic("size %10u j %10u i %10u", size, j, i);
488cafe5635SKent Overstreet
489cafe5635SKent Overstreet if (__to_inorder(j, size, extra) != i)
490cafe5635SKent Overstreet panic("size %10u j %10u i %10u", size, j, i);
491cafe5635SKent Overstreet
492cafe5635SKent Overstreet if (j == rounddown_pow_of_two(size) - 1)
493cafe5635SKent Overstreet break;
494cafe5635SKent Overstreet
495cafe5635SKent Overstreet BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
496cafe5635SKent Overstreet
497cafe5635SKent Overstreet j = inorder_next(j, size);
498cafe5635SKent Overstreet i++;
499cafe5635SKent Overstreet }
500cafe5635SKent Overstreet
501cafe5635SKent Overstreet done += size - 1;
502cafe5635SKent Overstreet }
503cafe5635SKent Overstreet }
504cafe5635SKent Overstreet #endif
505cafe5635SKent Overstreet
506cafe5635SKent Overstreet /*
50748a73025SPhil Viana * Cacheline/offset <-> bkey pointer arithmetic:
508cafe5635SKent Overstreet *
509cafe5635SKent Overstreet * t->tree is a binary search tree in an array; each node corresponds to a key
510cafe5635SKent Overstreet * in one cacheline in t->set (BSET_CACHELINE bytes).
511cafe5635SKent Overstreet *
512cafe5635SKent Overstreet * This means we don't have to store the full index of the key that a node in
513cafe5635SKent Overstreet * the binary tree points to; to_inorder() gives us the cacheline, and then
514cafe5635SKent Overstreet * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
515cafe5635SKent Overstreet *
51648a73025SPhil Viana * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
517cafe5635SKent Overstreet * make this work.
518cafe5635SKent Overstreet *
519cafe5635SKent Overstreet * To construct the bfloat for an arbitrary key we need to know what the key
520cafe5635SKent Overstreet * immediately preceding it is: we have to check if the two keys differ in the
521cafe5635SKent Overstreet * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
522cafe5635SKent Overstreet * of the previous key so we can walk backwards to it from t->tree[j]'s key.
523cafe5635SKent Overstreet */
524cafe5635SKent Overstreet
cacheline_to_bkey(struct bset_tree * t,unsigned int cacheline,unsigned int offset)5256f10f7d1SColy Li static struct bkey *cacheline_to_bkey(struct bset_tree *t,
5266f10f7d1SColy Li unsigned int cacheline,
5276f10f7d1SColy Li unsigned int offset)
528cafe5635SKent Overstreet {
529cafe5635SKent Overstreet return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
530cafe5635SKent Overstreet }
531cafe5635SKent Overstreet
bkey_to_cacheline(struct bset_tree * t,struct bkey * k)5326f10f7d1SColy Li static unsigned int bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
533cafe5635SKent Overstreet {
534cafe5635SKent Overstreet return ((void *) k - (void *) t->data) / BSET_CACHELINE;
535cafe5635SKent Overstreet }
536cafe5635SKent Overstreet
bkey_to_cacheline_offset(struct bset_tree * t,unsigned int cacheline,struct bkey * k)5376f10f7d1SColy Li static unsigned int bkey_to_cacheline_offset(struct bset_tree *t,
5386f10f7d1SColy Li unsigned int cacheline,
5399dd6358aSKent Overstreet struct bkey *k)
540cafe5635SKent Overstreet {
5419dd6358aSKent Overstreet return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0);
542cafe5635SKent Overstreet }
543cafe5635SKent Overstreet
tree_to_bkey(struct bset_tree * t,unsigned int j)5446f10f7d1SColy Li static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned int j)
545cafe5635SKent Overstreet {
546cafe5635SKent Overstreet return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
547cafe5635SKent Overstreet }
548cafe5635SKent Overstreet
tree_to_prev_bkey(struct bset_tree * t,unsigned int j)5496f10f7d1SColy Li static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned int j)
550cafe5635SKent Overstreet {
551cafe5635SKent Overstreet return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
552cafe5635SKent Overstreet }
553cafe5635SKent Overstreet
554cafe5635SKent Overstreet /*
555cafe5635SKent Overstreet * For the write set - the one we're currently inserting keys into - we don't
556cafe5635SKent Overstreet * maintain a full search tree, we just keep a simple lookup table in t->prev.
557cafe5635SKent Overstreet */
table_to_bkey(struct bset_tree * t,unsigned int cacheline)5586f10f7d1SColy Li static struct bkey *table_to_bkey(struct bset_tree *t, unsigned int cacheline)
559cafe5635SKent Overstreet {
560cafe5635SKent Overstreet return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
561cafe5635SKent Overstreet }
562cafe5635SKent Overstreet
shrd128(uint64_t high,uint64_t low,uint8_t shift)563cafe5635SKent Overstreet static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
564cafe5635SKent Overstreet {
565cafe5635SKent Overstreet low >>= shift;
566cafe5635SKent Overstreet low |= (high << 1) << (63U - shift);
567cafe5635SKent Overstreet return low;
568cafe5635SKent Overstreet }
569cafe5635SKent Overstreet
570b467a6acSColy Li /*
571b467a6acSColy Li * Calculate mantissa value for struct bkey_float.
572b467a6acSColy Li * If most significant bit of f->exponent is not set, then
573b467a6acSColy Li * - f->exponent >> 6 is 0
574b467a6acSColy Li * - p[0] points to bkey->low
575b467a6acSColy Li * - p[-1] borrows bits from KEY_INODE() of bkey->high
576b467a6acSColy Li * if most isgnificant bits of f->exponent is set, then
577b467a6acSColy Li * - f->exponent >> 6 is 1
578b467a6acSColy Li * - p[0] points to bits from KEY_INODE() of bkey->high
579b467a6acSColy Li * - p[-1] points to other bits from KEY_INODE() of
580b467a6acSColy Li * bkey->high too.
581b467a6acSColy Li * See make_bfloat() to check when most significant bit of f->exponent
582b467a6acSColy Li * is set or not.
583b467a6acSColy Li */
bfloat_mantissa(const struct bkey * k,struct bkey_float * f)5846f10f7d1SColy Li static inline unsigned int bfloat_mantissa(const struct bkey *k,
585cafe5635SKent Overstreet struct bkey_float *f)
586cafe5635SKent Overstreet {
587cafe5635SKent Overstreet const uint64_t *p = &k->low - (f->exponent >> 6);
5881fae7cf0SColy Li
589cafe5635SKent Overstreet return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
590cafe5635SKent Overstreet }
591cafe5635SKent Overstreet
make_bfloat(struct bset_tree * t,unsigned int j)5926f10f7d1SColy Li static void make_bfloat(struct bset_tree *t, unsigned int j)
593cafe5635SKent Overstreet {
594cafe5635SKent Overstreet struct bkey_float *f = &t->tree[j];
595cafe5635SKent Overstreet struct bkey *m = tree_to_bkey(t, j);
596cafe5635SKent Overstreet struct bkey *p = tree_to_prev_bkey(t, j);
597cafe5635SKent Overstreet
598cafe5635SKent Overstreet struct bkey *l = is_power_of_2(j)
599cafe5635SKent Overstreet ? t->data->start
600cafe5635SKent Overstreet : tree_to_prev_bkey(t, j >> ffs(j));
601cafe5635SKent Overstreet
602cafe5635SKent Overstreet struct bkey *r = is_power_of_2(j + 1)
603fafff81cSKent Overstreet ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
604cafe5635SKent Overstreet : tree_to_bkey(t, j >> (ffz(j) + 1));
605cafe5635SKent Overstreet
606cafe5635SKent Overstreet BUG_ON(m < l || m > r);
607cafe5635SKent Overstreet BUG_ON(bkey_next(p) != m);
608cafe5635SKent Overstreet
609b467a6acSColy Li /*
610b467a6acSColy Li * If l and r have different KEY_INODE values (different backing
611b467a6acSColy Li * device), f->exponent records how many least significant bits
612b467a6acSColy Li * are different in KEY_INODE values and sets most significant
613b467a6acSColy Li * bits to 1 (by +64).
614b467a6acSColy Li * If l and r have same KEY_INODE value, f->exponent records
615b467a6acSColy Li * how many different bits in least significant bits of bkey->low.
616b467a6acSColy Li * See bfloat_mantiss() how the most significant bit of
617b467a6acSColy Li * f->exponent is used to calculate bfloat mantissa value.
618b467a6acSColy Li */
619cafe5635SKent Overstreet if (KEY_INODE(l) != KEY_INODE(r))
620cafe5635SKent Overstreet f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
621cafe5635SKent Overstreet else
622cafe5635SKent Overstreet f->exponent = fls64(r->low ^ l->low);
623cafe5635SKent Overstreet
624cafe5635SKent Overstreet f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
625cafe5635SKent Overstreet
626cafe5635SKent Overstreet /*
627cafe5635SKent Overstreet * Setting f->exponent = 127 flags this node as failed, and causes the
628cafe5635SKent Overstreet * lookup code to fall back to comparing against the original key.
629cafe5635SKent Overstreet */
630cafe5635SKent Overstreet
631cafe5635SKent Overstreet if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
632cafe5635SKent Overstreet f->mantissa = bfloat_mantissa(m, f) - 1;
633cafe5635SKent Overstreet else
634cafe5635SKent Overstreet f->exponent = 127;
635cafe5635SKent Overstreet }
636cafe5635SKent Overstreet
bset_alloc_tree(struct btree_keys * b,struct bset_tree * t)637a85e968eSKent Overstreet static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t)
638cafe5635SKent Overstreet {
639a85e968eSKent Overstreet if (t != b->set) {
6406f10f7d1SColy Li unsigned int j = roundup(t[-1].size,
641cafe5635SKent Overstreet 64 / sizeof(struct bkey_float));
642cafe5635SKent Overstreet
643cafe5635SKent Overstreet t->tree = t[-1].tree + j;
644cafe5635SKent Overstreet t->prev = t[-1].prev + j;
645cafe5635SKent Overstreet }
646cafe5635SKent Overstreet
647a85e968eSKent Overstreet while (t < b->set + MAX_BSETS)
648cafe5635SKent Overstreet t++->size = 0;
649cafe5635SKent Overstreet }
650cafe5635SKent Overstreet
bch_bset_build_unwritten_tree(struct btree_keys * b)651a85e968eSKent Overstreet static void bch_bset_build_unwritten_tree(struct btree_keys *b)
652cafe5635SKent Overstreet {
653ee811287SKent Overstreet struct bset_tree *t = bset_tree_last(b);
654cafe5635SKent Overstreet
655a85e968eSKent Overstreet BUG_ON(b->last_set_unwritten);
656a85e968eSKent Overstreet b->last_set_unwritten = 1;
657a85e968eSKent Overstreet
658cafe5635SKent Overstreet bset_alloc_tree(b, t);
659cafe5635SKent Overstreet
660a85e968eSKent Overstreet if (t->tree != b->set->tree + btree_keys_cachelines(b)) {
6619dd6358aSKent Overstreet t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start);
662cafe5635SKent Overstreet t->size = 1;
663cafe5635SKent Overstreet }
664cafe5635SKent Overstreet }
665cafe5635SKent Overstreet
bch_bset_init_next(struct btree_keys * b,struct bset * i,uint64_t magic)666a85e968eSKent Overstreet void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic)
667ee811287SKent Overstreet {
668a85e968eSKent Overstreet if (i != b->set->data) {
669a85e968eSKent Overstreet b->set[++b->nsets].data = i;
670a85e968eSKent Overstreet i->seq = b->set->data->seq;
671ee811287SKent Overstreet } else
672ee811287SKent Overstreet get_random_bytes(&i->seq, sizeof(uint64_t));
673ee811287SKent Overstreet
674ee811287SKent Overstreet i->magic = magic;
675ee811287SKent Overstreet i->version = 0;
676ee811287SKent Overstreet i->keys = 0;
677ee811287SKent Overstreet
678ee811287SKent Overstreet bch_bset_build_unwritten_tree(b);
679ee811287SKent Overstreet }
680ee811287SKent Overstreet
681b467a6acSColy Li /*
682b467a6acSColy Li * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to
683b467a6acSColy Li * accelerate bkey search in a btree node (pointed by bset_tree->data in
684b467a6acSColy Li * memory). After search in the auxiliar tree by calling bset_search_tree(),
685b467a6acSColy Li * a struct bset_search_iter is returned which indicates range [l, r] from
686b467a6acSColy Li * bset_tree->data where the searching bkey might be inside. Then a followed
687b467a6acSColy Li * linear comparison does the exact search, see __bch_bset_search() for how
688b467a6acSColy Li * the auxiliary tree is used.
689b467a6acSColy Li */
bch_bset_build_written_tree(struct btree_keys * b)690a85e968eSKent Overstreet void bch_bset_build_written_tree(struct btree_keys *b)
691cafe5635SKent Overstreet {
692ee811287SKent Overstreet struct bset_tree *t = bset_tree_last(b);
6939dd6358aSKent Overstreet struct bkey *prev = NULL, *k = t->data->start;
6946f10f7d1SColy Li unsigned int j, cacheline = 1;
695cafe5635SKent Overstreet
696a85e968eSKent Overstreet b->last_set_unwritten = 0;
697a85e968eSKent Overstreet
698cafe5635SKent Overstreet bset_alloc_tree(b, t);
699cafe5635SKent Overstreet
7006f10f7d1SColy Li t->size = min_t(unsigned int,
701fafff81cSKent Overstreet bkey_to_cacheline(t, bset_bkey_last(t->data)),
702a85e968eSKent Overstreet b->set->tree + btree_keys_cachelines(b) - t->tree);
703cafe5635SKent Overstreet
704cafe5635SKent Overstreet if (t->size < 2) {
705cafe5635SKent Overstreet t->size = 0;
706cafe5635SKent Overstreet return;
707cafe5635SKent Overstreet }
708cafe5635SKent Overstreet
709cafe5635SKent Overstreet t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
710cafe5635SKent Overstreet
711cafe5635SKent Overstreet /* First we figure out where the first key in each cacheline is */
712cafe5635SKent Overstreet for (j = inorder_next(0, t->size);
713cafe5635SKent Overstreet j;
714cafe5635SKent Overstreet j = inorder_next(j, t->size)) {
7156751c1e3SJoe Perches while (bkey_to_cacheline(t, k) < cacheline) {
7166751c1e3SJoe Perches prev = k;
7176751c1e3SJoe Perches k = bkey_next(k);
7186751c1e3SJoe Perches }
719cafe5635SKent Overstreet
7209dd6358aSKent Overstreet t->prev[j] = bkey_u64s(prev);
7219dd6358aSKent Overstreet t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k);
722cafe5635SKent Overstreet }
723cafe5635SKent Overstreet
724fafff81cSKent Overstreet while (bkey_next(k) != bset_bkey_last(t->data))
725cafe5635SKent Overstreet k = bkey_next(k);
726cafe5635SKent Overstreet
727cafe5635SKent Overstreet t->end = *k;
728cafe5635SKent Overstreet
729cafe5635SKent Overstreet /* Then we build the tree */
730cafe5635SKent Overstreet for (j = inorder_next(0, t->size);
731cafe5635SKent Overstreet j;
732cafe5635SKent Overstreet j = inorder_next(j, t->size))
733cafe5635SKent Overstreet make_bfloat(t, j);
734cafe5635SKent Overstreet }
735cafe5635SKent Overstreet
736829a60b9SKent Overstreet /* Insert */
737829a60b9SKent Overstreet
bch_bset_fix_invalidated_key(struct btree_keys * b,struct bkey * k)738a85e968eSKent Overstreet void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k)
739cafe5635SKent Overstreet {
740cafe5635SKent Overstreet struct bset_tree *t;
7416f10f7d1SColy Li unsigned int inorder, j = 1;
742cafe5635SKent Overstreet
743a85e968eSKent Overstreet for (t = b->set; t <= bset_tree_last(b); t++)
744fafff81cSKent Overstreet if (k < bset_bkey_last(t->data))
745cafe5635SKent Overstreet goto found_set;
746cafe5635SKent Overstreet
747cafe5635SKent Overstreet BUG();
748cafe5635SKent Overstreet found_set:
749cafe5635SKent Overstreet if (!t->size || !bset_written(b, t))
750cafe5635SKent Overstreet return;
751cafe5635SKent Overstreet
752cafe5635SKent Overstreet inorder = bkey_to_cacheline(t, k);
753cafe5635SKent Overstreet
754cafe5635SKent Overstreet if (k == t->data->start)
755cafe5635SKent Overstreet goto fix_left;
756cafe5635SKent Overstreet
757fafff81cSKent Overstreet if (bkey_next(k) == bset_bkey_last(t->data)) {
758cafe5635SKent Overstreet t->end = *k;
759cafe5635SKent Overstreet goto fix_right;
760cafe5635SKent Overstreet }
761cafe5635SKent Overstreet
762cafe5635SKent Overstreet j = inorder_to_tree(inorder, t);
763cafe5635SKent Overstreet
764cafe5635SKent Overstreet if (j &&
765cafe5635SKent Overstreet j < t->size &&
766cafe5635SKent Overstreet k == tree_to_bkey(t, j))
767cafe5635SKent Overstreet fix_left: do {
768cafe5635SKent Overstreet make_bfloat(t, j);
769cafe5635SKent Overstreet j = j * 2;
770cafe5635SKent Overstreet } while (j < t->size);
771cafe5635SKent Overstreet
772cafe5635SKent Overstreet j = inorder_to_tree(inorder + 1, t);
773cafe5635SKent Overstreet
774cafe5635SKent Overstreet if (j &&
775cafe5635SKent Overstreet j < t->size &&
776cafe5635SKent Overstreet k == tree_to_prev_bkey(t, j))
777cafe5635SKent Overstreet fix_right: do {
778cafe5635SKent Overstreet make_bfloat(t, j);
779cafe5635SKent Overstreet j = j * 2 + 1;
780cafe5635SKent Overstreet } while (j < t->size);
781cafe5635SKent Overstreet }
782cafe5635SKent Overstreet
bch_bset_fix_lookup_table(struct btree_keys * b,struct bset_tree * t,struct bkey * k)783a85e968eSKent Overstreet static void bch_bset_fix_lookup_table(struct btree_keys *b,
784ee811287SKent Overstreet struct bset_tree *t,
785ee811287SKent Overstreet struct bkey *k)
786cafe5635SKent Overstreet {
7876f10f7d1SColy Li unsigned int shift = bkey_u64s(k);
7886f10f7d1SColy Li unsigned int j = bkey_to_cacheline(t, k);
789cafe5635SKent Overstreet
790cafe5635SKent Overstreet /* We're getting called from btree_split() or btree_gc, just bail out */
791cafe5635SKent Overstreet if (!t->size)
792cafe5635SKent Overstreet return;
793cafe5635SKent Overstreet
7943be11dbaSColy Li /*
7953be11dbaSColy Li * k is the key we just inserted; we need to find the entry in the
796cafe5635SKent Overstreet * lookup table for the first key that is strictly greater than k:
797cafe5635SKent Overstreet * it's either k's cacheline or the next one
798cafe5635SKent Overstreet */
7999dd6358aSKent Overstreet while (j < t->size &&
800cafe5635SKent Overstreet table_to_bkey(t, j) <= k)
801cafe5635SKent Overstreet j++;
802cafe5635SKent Overstreet
8033be11dbaSColy Li /*
8043be11dbaSColy Li * Adjust all the lookup table entries, and find a new key for any that
805cafe5635SKent Overstreet * have gotten too big
806cafe5635SKent Overstreet */
807cafe5635SKent Overstreet for (; j < t->size; j++) {
808cafe5635SKent Overstreet t->prev[j] += shift;
809cafe5635SKent Overstreet
810cafe5635SKent Overstreet if (t->prev[j] > 7) {
811cafe5635SKent Overstreet k = table_to_bkey(t, j - 1);
812cafe5635SKent Overstreet
813cafe5635SKent Overstreet while (k < cacheline_to_bkey(t, j, 0))
814cafe5635SKent Overstreet k = bkey_next(k);
815cafe5635SKent Overstreet
8169dd6358aSKent Overstreet t->prev[j] = bkey_to_cacheline_offset(t, j, k);
817cafe5635SKent Overstreet }
818cafe5635SKent Overstreet }
819cafe5635SKent Overstreet
820a85e968eSKent Overstreet if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree)
821cafe5635SKent Overstreet return;
822cafe5635SKent Overstreet
823cafe5635SKent Overstreet /* Possibly add a new entry to the end of the lookup table */
824cafe5635SKent Overstreet
825cafe5635SKent Overstreet for (k = table_to_bkey(t, t->size - 1);
826fafff81cSKent Overstreet k != bset_bkey_last(t->data);
827cafe5635SKent Overstreet k = bkey_next(k))
828cafe5635SKent Overstreet if (t->size == bkey_to_cacheline(t, k)) {
829b0d30981SColy Li t->prev[t->size] =
830b0d30981SColy Li bkey_to_cacheline_offset(t, t->size, k);
831cafe5635SKent Overstreet t->size++;
832cafe5635SKent Overstreet }
833cafe5635SKent Overstreet }
834cafe5635SKent Overstreet
8350f49cf3dSNicholas Swenson /*
8360f49cf3dSNicholas Swenson * Tries to merge l and r: l should be lower than r
8370f49cf3dSNicholas Swenson * Returns true if we were able to merge. If we did merge, l will be the merged
8380f49cf3dSNicholas Swenson * key, r will be untouched.
8390f49cf3dSNicholas Swenson */
bch_bkey_try_merge(struct btree_keys * b,struct bkey * l,struct bkey * r)8400f49cf3dSNicholas Swenson bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r)
8410f49cf3dSNicholas Swenson {
8420f49cf3dSNicholas Swenson if (!b->ops->key_merge)
8430f49cf3dSNicholas Swenson return false;
8440f49cf3dSNicholas Swenson
8450f49cf3dSNicholas Swenson /*
8460f49cf3dSNicholas Swenson * Generic header checks
8470f49cf3dSNicholas Swenson * Assumes left and right are in order
8480f49cf3dSNicholas Swenson * Left and right must be exactly aligned
8490f49cf3dSNicholas Swenson */
8503bdad1e4SNicholas Swenson if (!bch_bkey_equal_header(l, r) ||
8510f49cf3dSNicholas Swenson bkey_cmp(l, &START_KEY(r)))
8520f49cf3dSNicholas Swenson return false;
8530f49cf3dSNicholas Swenson
8540f49cf3dSNicholas Swenson return b->ops->key_merge(b, l, r);
8550f49cf3dSNicholas Swenson }
8560f49cf3dSNicholas Swenson
bch_bset_insert(struct btree_keys * b,struct bkey * where,struct bkey * insert)857a85e968eSKent Overstreet void bch_bset_insert(struct btree_keys *b, struct bkey *where,
858ee811287SKent Overstreet struct bkey *insert)
859cafe5635SKent Overstreet {
860ee811287SKent Overstreet struct bset_tree *t = bset_tree_last(b);
861cafe5635SKent Overstreet
862a85e968eSKent Overstreet BUG_ON(!b->last_set_unwritten);
863ee811287SKent Overstreet BUG_ON(bset_byte_offset(b, t->data) +
864ee811287SKent Overstreet __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
865ee811287SKent Overstreet PAGE_SIZE << b->page_order);
866cafe5635SKent Overstreet
867ee811287SKent Overstreet memmove((uint64_t *) where + bkey_u64s(insert),
868ee811287SKent Overstreet where,
869ee811287SKent Overstreet (void *) bset_bkey_last(t->data) - (void *) where);
870cafe5635SKent Overstreet
871ee811287SKent Overstreet t->data->keys += bkey_u64s(insert);
872ee811287SKent Overstreet bkey_copy(where, insert);
873ee811287SKent Overstreet bch_bset_fix_lookup_table(b, t, where);
874cafe5635SKent Overstreet }
875cafe5635SKent Overstreet
bch_btree_insert_key(struct btree_keys * b,struct bkey * k,struct bkey * replace_key)8766f10f7d1SColy Li unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
877829a60b9SKent Overstreet struct bkey *replace_key)
878829a60b9SKent Overstreet {
8796f10f7d1SColy Li unsigned int status = BTREE_INSERT_STATUS_NO_INSERT;
880829a60b9SKent Overstreet struct bset *i = bset_tree_last(b)->data;
881829a60b9SKent Overstreet struct bkey *m, *prev = NULL;
882*6479b9f4SMatthew Mirvish struct btree_iter_stack iter;
88331b90956SColy Li struct bkey preceding_key_on_stack = ZERO_KEY;
88431b90956SColy Li struct bkey *preceding_key_p = &preceding_key_on_stack;
885829a60b9SKent Overstreet
886829a60b9SKent Overstreet BUG_ON(b->ops->is_extents && !KEY_SIZE(k));
887829a60b9SKent Overstreet
88831b90956SColy Li /*
88931b90956SColy Li * If k has preceding key, preceding_key_p will be set to address
89031b90956SColy Li * of k's preceding key; otherwise preceding_key_p will be set
89131b90956SColy Li * to NULL inside preceding_key().
89231b90956SColy Li */
89331b90956SColy Li if (b->ops->is_extents)
89431b90956SColy Li preceding_key(&START_KEY(k), &preceding_key_p);
89531b90956SColy Li else
89631b90956SColy Li preceding_key(k, &preceding_key_p);
89731b90956SColy Li
898*6479b9f4SMatthew Mirvish m = bch_btree_iter_stack_init(b, &iter, preceding_key_p);
899829a60b9SKent Overstreet
900*6479b9f4SMatthew Mirvish if (b->ops->insert_fixup(b, k, &iter.iter, replace_key))
901829a60b9SKent Overstreet return status;
902829a60b9SKent Overstreet
903829a60b9SKent Overstreet status = BTREE_INSERT_STATUS_INSERT;
904829a60b9SKent Overstreet
905829a60b9SKent Overstreet while (m != bset_bkey_last(i) &&
9066751c1e3SJoe Perches bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0) {
9076751c1e3SJoe Perches prev = m;
9086751c1e3SJoe Perches m = bkey_next(m);
9096751c1e3SJoe Perches }
910829a60b9SKent Overstreet
911829a60b9SKent Overstreet /* prev is in the tree, if we merge we're done */
912829a60b9SKent Overstreet status = BTREE_INSERT_STATUS_BACK_MERGE;
913829a60b9SKent Overstreet if (prev &&
914829a60b9SKent Overstreet bch_bkey_try_merge(b, prev, k))
915829a60b9SKent Overstreet goto merged;
916829a60b9SKent Overstreet #if 0
917829a60b9SKent Overstreet status = BTREE_INSERT_STATUS_OVERWROTE;
918829a60b9SKent Overstreet if (m != bset_bkey_last(i) &&
919829a60b9SKent Overstreet KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
920829a60b9SKent Overstreet goto copy;
921829a60b9SKent Overstreet #endif
922829a60b9SKent Overstreet status = BTREE_INSERT_STATUS_FRONT_MERGE;
923829a60b9SKent Overstreet if (m != bset_bkey_last(i) &&
924829a60b9SKent Overstreet bch_bkey_try_merge(b, k, m))
925829a60b9SKent Overstreet goto copy;
926829a60b9SKent Overstreet
927829a60b9SKent Overstreet bch_bset_insert(b, m, k);
928829a60b9SKent Overstreet copy: bkey_copy(m, k);
929829a60b9SKent Overstreet merged:
930829a60b9SKent Overstreet return status;
931829a60b9SKent Overstreet }
932829a60b9SKent Overstreet
933829a60b9SKent Overstreet /* Lookup */
934829a60b9SKent Overstreet
935cafe5635SKent Overstreet struct bset_search_iter {
936cafe5635SKent Overstreet struct bkey *l, *r;
937cafe5635SKent Overstreet };
938cafe5635SKent Overstreet
bset_search_write_set(struct bset_tree * t,const struct bkey * search)939a85e968eSKent Overstreet static struct bset_search_iter bset_search_write_set(struct bset_tree *t,
940cafe5635SKent Overstreet const struct bkey *search)
941cafe5635SKent Overstreet {
9426f10f7d1SColy Li unsigned int li = 0, ri = t->size;
943cafe5635SKent Overstreet
944cafe5635SKent Overstreet while (li + 1 != ri) {
9456f10f7d1SColy Li unsigned int m = (li + ri) >> 1;
946cafe5635SKent Overstreet
947cafe5635SKent Overstreet if (bkey_cmp(table_to_bkey(t, m), search) > 0)
948cafe5635SKent Overstreet ri = m;
949cafe5635SKent Overstreet else
950cafe5635SKent Overstreet li = m;
951cafe5635SKent Overstreet }
952cafe5635SKent Overstreet
953cafe5635SKent Overstreet return (struct bset_search_iter) {
954cafe5635SKent Overstreet table_to_bkey(t, li),
955fafff81cSKent Overstreet ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
956cafe5635SKent Overstreet };
957cafe5635SKent Overstreet }
958cafe5635SKent Overstreet
bset_search_tree(struct bset_tree * t,const struct bkey * search)959a85e968eSKent Overstreet static struct bset_search_iter bset_search_tree(struct bset_tree *t,
960cafe5635SKent Overstreet const struct bkey *search)
961cafe5635SKent Overstreet {
962cafe5635SKent Overstreet struct bkey *l, *r;
963cafe5635SKent Overstreet struct bkey_float *f;
9646f10f7d1SColy Li unsigned int inorder, j, n = 1;
965cafe5635SKent Overstreet
966cafe5635SKent Overstreet do {
9676f10f7d1SColy Li unsigned int p = n << 4;
9681fae7cf0SColy Li
969f960facbSColy Li if (p < t->size)
970cafe5635SKent Overstreet prefetch(&t->tree[p]);
971cafe5635SKent Overstreet
972cafe5635SKent Overstreet j = n;
973cafe5635SKent Overstreet f = &t->tree[j];
974cafe5635SKent Overstreet
975944a4f34SColy Li if (likely(f->exponent != 127)) {
976944a4f34SColy Li if (f->mantissa >= bfloat_mantissa(search, f))
977944a4f34SColy Li n = j * 2;
978cafe5635SKent Overstreet else
979944a4f34SColy Li n = j * 2 + 1;
980944a4f34SColy Li } else {
981944a4f34SColy Li if (bkey_cmp(tree_to_bkey(t, j), search) > 0)
982944a4f34SColy Li n = j * 2;
983944a4f34SColy Li else
984944a4f34SColy Li n = j * 2 + 1;
985944a4f34SColy Li }
986cafe5635SKent Overstreet } while (n < t->size);
987cafe5635SKent Overstreet
988cafe5635SKent Overstreet inorder = to_inorder(j, t);
989cafe5635SKent Overstreet
990cafe5635SKent Overstreet /*
991cafe5635SKent Overstreet * n would have been the node we recursed to - the low bit tells us if
992cafe5635SKent Overstreet * we recursed left or recursed right.
993cafe5635SKent Overstreet */
994cafe5635SKent Overstreet if (n & 1) {
995cafe5635SKent Overstreet l = cacheline_to_bkey(t, inorder, f->m);
996cafe5635SKent Overstreet
997cafe5635SKent Overstreet if (++inorder != t->size) {
998cafe5635SKent Overstreet f = &t->tree[inorder_next(j, t->size)];
999cafe5635SKent Overstreet r = cacheline_to_bkey(t, inorder, f->m);
1000cafe5635SKent Overstreet } else
1001fafff81cSKent Overstreet r = bset_bkey_last(t->data);
1002cafe5635SKent Overstreet } else {
1003cafe5635SKent Overstreet r = cacheline_to_bkey(t, inorder, f->m);
1004cafe5635SKent Overstreet
1005cafe5635SKent Overstreet if (--inorder) {
1006cafe5635SKent Overstreet f = &t->tree[inorder_prev(j, t->size)];
1007cafe5635SKent Overstreet l = cacheline_to_bkey(t, inorder, f->m);
1008cafe5635SKent Overstreet } else
1009cafe5635SKent Overstreet l = t->data->start;
1010cafe5635SKent Overstreet }
1011cafe5635SKent Overstreet
1012cafe5635SKent Overstreet return (struct bset_search_iter) {l, r};
1013cafe5635SKent Overstreet }
1014cafe5635SKent Overstreet
__bch_bset_search(struct btree_keys * b,struct bset_tree * t,const struct bkey * search)1015c052dd9aSKent Overstreet struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
1016cafe5635SKent Overstreet const struct bkey *search)
1017cafe5635SKent Overstreet {
1018cafe5635SKent Overstreet struct bset_search_iter i;
1019cafe5635SKent Overstreet
1020cafe5635SKent Overstreet /*
1021cafe5635SKent Overstreet * First, we search for a cacheline, then lastly we do a linear search
1022cafe5635SKent Overstreet * within that cacheline.
1023cafe5635SKent Overstreet *
1024cafe5635SKent Overstreet * To search for the cacheline, there's three different possibilities:
1025cafe5635SKent Overstreet * * The set is too small to have a search tree, so we just do a linear
1026cafe5635SKent Overstreet * search over the whole set.
1027cafe5635SKent Overstreet * * The set is the one we're currently inserting into; keeping a full
1028cafe5635SKent Overstreet * auxiliary search tree up to date would be too expensive, so we
1029cafe5635SKent Overstreet * use a much simpler lookup table to do a binary search -
1030cafe5635SKent Overstreet * bset_search_write_set().
1031cafe5635SKent Overstreet * * Or we use the auxiliary search tree we constructed earlier -
1032cafe5635SKent Overstreet * bset_search_tree()
1033cafe5635SKent Overstreet */
1034cafe5635SKent Overstreet
1035cafe5635SKent Overstreet if (unlikely(!t->size)) {
1036cafe5635SKent Overstreet i.l = t->data->start;
1037fafff81cSKent Overstreet i.r = bset_bkey_last(t->data);
1038c052dd9aSKent Overstreet } else if (bset_written(b, t)) {
1039cafe5635SKent Overstreet /*
1040cafe5635SKent Overstreet * Each node in the auxiliary search tree covers a certain range
1041cafe5635SKent Overstreet * of bits, and keys above and below the set it covers might
1042cafe5635SKent Overstreet * differ outside those bits - so we have to special case the
1043cafe5635SKent Overstreet * start and end - handle that here:
1044cafe5635SKent Overstreet */
1045cafe5635SKent Overstreet
1046cafe5635SKent Overstreet if (unlikely(bkey_cmp(search, &t->end) >= 0))
1047fafff81cSKent Overstreet return bset_bkey_last(t->data);
1048cafe5635SKent Overstreet
1049cafe5635SKent Overstreet if (unlikely(bkey_cmp(search, t->data->start) < 0))
1050cafe5635SKent Overstreet return t->data->start;
1051cafe5635SKent Overstreet
1052a85e968eSKent Overstreet i = bset_search_tree(t, search);
1053a85e968eSKent Overstreet } else {
1054c052dd9aSKent Overstreet BUG_ON(!b->nsets &&
1055a85e968eSKent Overstreet t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
1056a85e968eSKent Overstreet
1057a85e968eSKent Overstreet i = bset_search_write_set(t, search);
1058a85e968eSKent Overstreet }
1059cafe5635SKent Overstreet
1060c052dd9aSKent Overstreet if (btree_keys_expensive_checks(b)) {
1061c052dd9aSKent Overstreet BUG_ON(bset_written(b, t) &&
1062cafe5635SKent Overstreet i.l != t->data->start &&
1063cafe5635SKent Overstreet bkey_cmp(tree_to_prev_bkey(t,
1064cafe5635SKent Overstreet inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
1065cafe5635SKent Overstreet search) > 0);
1066cafe5635SKent Overstreet
1067fafff81cSKent Overstreet BUG_ON(i.r != bset_bkey_last(t->data) &&
1068cafe5635SKent Overstreet bkey_cmp(i.r, search) <= 0);
1069280481d0SKent Overstreet }
1070cafe5635SKent Overstreet
1071cafe5635SKent Overstreet while (likely(i.l != i.r) &&
1072cafe5635SKent Overstreet bkey_cmp(i.l, search) <= 0)
1073cafe5635SKent Overstreet i.l = bkey_next(i.l);
1074cafe5635SKent Overstreet
1075cafe5635SKent Overstreet return i.l;
1076cafe5635SKent Overstreet }
1077cafe5635SKent Overstreet
1078cafe5635SKent Overstreet /* Btree iterator */
1079cafe5635SKent Overstreet
1080911c9610SKent Overstreet typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
1081911c9610SKent Overstreet struct btree_iter_set);
1082911c9610SKent Overstreet
btree_iter_cmp(struct btree_iter_set l,struct btree_iter_set r)1083cafe5635SKent Overstreet static inline bool btree_iter_cmp(struct btree_iter_set l,
1084cafe5635SKent Overstreet struct btree_iter_set r)
1085cafe5635SKent Overstreet {
1086911c9610SKent Overstreet return bkey_cmp(l.k, r.k) > 0;
1087cafe5635SKent Overstreet }
1088cafe5635SKent Overstreet
btree_iter_end(struct btree_iter * iter)1089cafe5635SKent Overstreet static inline bool btree_iter_end(struct btree_iter *iter)
1090cafe5635SKent Overstreet {
1091cafe5635SKent Overstreet return !iter->used;
1092cafe5635SKent Overstreet }
1093cafe5635SKent Overstreet
bch_btree_iter_push(struct btree_iter * iter,struct bkey * k,struct bkey * end)1094cafe5635SKent Overstreet void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
1095cafe5635SKent Overstreet struct bkey *end)
1096cafe5635SKent Overstreet {
1097cafe5635SKent Overstreet if (k != end)
1098cafe5635SKent Overstreet BUG_ON(!heap_add(iter,
1099cafe5635SKent Overstreet ((struct btree_iter_set) { k, end }),
1100cafe5635SKent Overstreet btree_iter_cmp));
1101cafe5635SKent Overstreet }
1102cafe5635SKent Overstreet
__bch_btree_iter_stack_init(struct btree_keys * b,struct btree_iter_stack * iter,struct bkey * search,struct bset_tree * start)1103*6479b9f4SMatthew Mirvish static struct bkey *__bch_btree_iter_stack_init(struct btree_keys *b,
1104*6479b9f4SMatthew Mirvish struct btree_iter_stack *iter,
1105911c9610SKent Overstreet struct bkey *search,
1106911c9610SKent Overstreet struct bset_tree *start)
1107cafe5635SKent Overstreet {
1108cafe5635SKent Overstreet struct bkey *ret = NULL;
11091fae7cf0SColy Li
1110*6479b9f4SMatthew Mirvish iter->iter.size = ARRAY_SIZE(iter->stack_data);
1111*6479b9f4SMatthew Mirvish iter->iter.used = 0;
1112cafe5635SKent Overstreet
1113280481d0SKent Overstreet #ifdef CONFIG_BCACHE_DEBUG
1114*6479b9f4SMatthew Mirvish iter->iter.b = b;
1115280481d0SKent Overstreet #endif
1116280481d0SKent Overstreet
1117c052dd9aSKent Overstreet for (; start <= bset_tree_last(b); start++) {
1118cafe5635SKent Overstreet ret = bch_bset_search(b, start, search);
1119*6479b9f4SMatthew Mirvish bch_btree_iter_push(&iter->iter, ret, bset_bkey_last(start->data));
1120cafe5635SKent Overstreet }
1121cafe5635SKent Overstreet
1122cafe5635SKent Overstreet return ret;
1123cafe5635SKent Overstreet }
1124cafe5635SKent Overstreet
bch_btree_iter_stack_init(struct btree_keys * b,struct btree_iter_stack * iter,struct bkey * search)1125*6479b9f4SMatthew Mirvish struct bkey *bch_btree_iter_stack_init(struct btree_keys *b,
1126*6479b9f4SMatthew Mirvish struct btree_iter_stack *iter,
1127911c9610SKent Overstreet struct bkey *search)
1128911c9610SKent Overstreet {
1129*6479b9f4SMatthew Mirvish return __bch_btree_iter_stack_init(b, iter, search, b->set);
1130911c9610SKent Overstreet }
1131911c9610SKent Overstreet
__bch_btree_iter_next(struct btree_iter * iter,btree_iter_cmp_fn * cmp)1132911c9610SKent Overstreet static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
1133911c9610SKent Overstreet btree_iter_cmp_fn *cmp)
1134cafe5635SKent Overstreet {
113542361469SBart Van Assche struct btree_iter_set b __maybe_unused;
1136cafe5635SKent Overstreet struct bkey *ret = NULL;
1137cafe5635SKent Overstreet
1138cafe5635SKent Overstreet if (!btree_iter_end(iter)) {
1139280481d0SKent Overstreet bch_btree_iter_next_check(iter);
1140280481d0SKent Overstreet
1141cafe5635SKent Overstreet ret = iter->data->k;
1142cafe5635SKent Overstreet iter->data->k = bkey_next(iter->data->k);
1143cafe5635SKent Overstreet
1144cafe5635SKent Overstreet if (iter->data->k > iter->data->end) {
1145cc0f4eaaSKent Overstreet WARN_ONCE(1, "bset was corrupt!\n");
1146cafe5635SKent Overstreet iter->data->k = iter->data->end;
1147cafe5635SKent Overstreet }
1148cafe5635SKent Overstreet
1149cafe5635SKent Overstreet if (iter->data->k == iter->data->end)
115042361469SBart Van Assche heap_pop(iter, b, cmp);
1151cafe5635SKent Overstreet else
1152911c9610SKent Overstreet heap_sift(iter, 0, cmp);
1153cafe5635SKent Overstreet }
1154cafe5635SKent Overstreet
1155cafe5635SKent Overstreet return ret;
1156cafe5635SKent Overstreet }
1157cafe5635SKent Overstreet
bch_btree_iter_next(struct btree_iter * iter)1158911c9610SKent Overstreet struct bkey *bch_btree_iter_next(struct btree_iter *iter)
1159911c9610SKent Overstreet {
1160911c9610SKent Overstreet return __bch_btree_iter_next(iter, btree_iter_cmp);
1161911c9610SKent Overstreet
1162911c9610SKent Overstreet }
1163911c9610SKent Overstreet
bch_btree_iter_next_filter(struct btree_iter * iter,struct btree_keys * b,ptr_filter_fn fn)1164cafe5635SKent Overstreet struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
1165a85e968eSKent Overstreet struct btree_keys *b, ptr_filter_fn fn)
1166cafe5635SKent Overstreet {
1167cafe5635SKent Overstreet struct bkey *ret;
1168cafe5635SKent Overstreet
1169cafe5635SKent Overstreet do {
1170cafe5635SKent Overstreet ret = bch_btree_iter_next(iter);
1171cafe5635SKent Overstreet } while (ret && fn(b, ret));
1172cafe5635SKent Overstreet
1173cafe5635SKent Overstreet return ret;
1174cafe5635SKent Overstreet }
1175cafe5635SKent Overstreet
1176cafe5635SKent Overstreet /* Mergesort */
1177cafe5635SKent Overstreet
bch_bset_sort_state_free(struct bset_sort_state * state)117867539e85SKent Overstreet void bch_bset_sort_state_free(struct bset_sort_state *state)
117967539e85SKent Overstreet {
1180d19936a2SKent Overstreet mempool_exit(&state->pool);
118167539e85SKent Overstreet }
118267539e85SKent Overstreet
bch_bset_sort_state_init(struct bset_sort_state * state,unsigned int page_order)11836f10f7d1SColy Li int bch_bset_sort_state_init(struct bset_sort_state *state,
11846f10f7d1SColy Li unsigned int page_order)
118567539e85SKent Overstreet {
118667539e85SKent Overstreet spin_lock_init(&state->time.lock);
118767539e85SKent Overstreet
118867539e85SKent Overstreet state->page_order = page_order;
118967539e85SKent Overstreet state->crit_factor = int_sqrt(1 << page_order);
119067539e85SKent Overstreet
1191d19936a2SKent Overstreet return mempool_init_page_pool(&state->pool, 1, page_order);
119267539e85SKent Overstreet }
119367539e85SKent Overstreet
btree_mergesort(struct btree_keys * b,struct bset * out,struct btree_iter * iter,bool fixup,bool remove_stale)1194a85e968eSKent Overstreet static void btree_mergesort(struct btree_keys *b, struct bset *out,
1195cafe5635SKent Overstreet struct btree_iter *iter,
1196cafe5635SKent Overstreet bool fixup, bool remove_stale)
1197cafe5635SKent Overstreet {
1198911c9610SKent Overstreet int i;
1199cafe5635SKent Overstreet struct bkey *k, *last = NULL;
1200ef71ec00SKent Overstreet BKEY_PADDED(k) tmp;
1201a85e968eSKent Overstreet bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale
1202cafe5635SKent Overstreet ? bch_ptr_bad
1203cafe5635SKent Overstreet : bch_ptr_invalid;
1204cafe5635SKent Overstreet
1205911c9610SKent Overstreet /* Heapify the iterator, using our comparison function */
1206911c9610SKent Overstreet for (i = iter->used / 2 - 1; i >= 0; --i)
120765d45231SKent Overstreet heap_sift(iter, i, b->ops->sort_cmp);
1208911c9610SKent Overstreet
1209cafe5635SKent Overstreet while (!btree_iter_end(iter)) {
121065d45231SKent Overstreet if (b->ops->sort_fixup && fixup)
121165d45231SKent Overstreet k = b->ops->sort_fixup(iter, &tmp.k);
1212ef71ec00SKent Overstreet else
1213ef71ec00SKent Overstreet k = NULL;
1214cafe5635SKent Overstreet
1215ef71ec00SKent Overstreet if (!k)
121665d45231SKent Overstreet k = __bch_btree_iter_next(iter, b->ops->sort_cmp);
1217ef71ec00SKent Overstreet
1218cafe5635SKent Overstreet if (bad(b, k))
1219cafe5635SKent Overstreet continue;
1220cafe5635SKent Overstreet
1221cafe5635SKent Overstreet if (!last) {
1222cafe5635SKent Overstreet last = out->start;
1223cafe5635SKent Overstreet bkey_copy(last, k);
122465d45231SKent Overstreet } else if (!bch_bkey_try_merge(b, last, k)) {
1225cafe5635SKent Overstreet last = bkey_next(last);
1226cafe5635SKent Overstreet bkey_copy(last, k);
1227cafe5635SKent Overstreet }
1228cafe5635SKent Overstreet }
1229cafe5635SKent Overstreet
1230cafe5635SKent Overstreet out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
1231cafe5635SKent Overstreet
123246f5aa88SJoe Perches pr_debug("sorted %i keys\n", out->keys);
1233cafe5635SKent Overstreet }
1234cafe5635SKent Overstreet
__btree_sort(struct btree_keys * b,struct btree_iter * iter,unsigned int start,unsigned int order,bool fixup,struct bset_sort_state * state)1235a85e968eSKent Overstreet static void __btree_sort(struct btree_keys *b, struct btree_iter *iter,
12366f10f7d1SColy Li unsigned int start, unsigned int order, bool fixup,
123767539e85SKent Overstreet struct bset_sort_state *state)
1238cafe5635SKent Overstreet {
1239cafe5635SKent Overstreet uint64_t start_time;
12400a451145SKent Overstreet bool used_mempool = false;
1241501d52a9SKent Overstreet struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOWAIT,
1242cafe5635SKent Overstreet order);
1243cafe5635SKent Overstreet if (!out) {
12443572324aSKent Overstreet struct page *outp;
12453572324aSKent Overstreet
124667539e85SKent Overstreet BUG_ON(order > state->page_order);
124767539e85SKent Overstreet
1248d19936a2SKent Overstreet outp = mempool_alloc(&state->pool, GFP_NOIO);
12493572324aSKent Overstreet out = page_address(outp);
12500a451145SKent Overstreet used_mempool = true;
1251a85e968eSKent Overstreet order = state->page_order;
1252cafe5635SKent Overstreet }
1253cafe5635SKent Overstreet
1254cafe5635SKent Overstreet start_time = local_clock();
1255cafe5635SKent Overstreet
125667539e85SKent Overstreet btree_mergesort(b, out, iter, fixup, false);
1257cafe5635SKent Overstreet b->nsets = start;
1258cafe5635SKent Overstreet
1259cafe5635SKent Overstreet if (!start && order == b->page_order) {
1260cafe5635SKent Overstreet /*
1261cafe5635SKent Overstreet * Our temporary buffer is the same size as the btree node's
1262cafe5635SKent Overstreet * buffer, we can just swap buffers instead of doing a big
1263cafe5635SKent Overstreet * memcpy()
12647a0bc2a8SColy Li *
12657a0bc2a8SColy Li * Don't worry event 'out' is allocated from mempool, it can
12667a0bc2a8SColy Li * still be swapped here. Because state->pool is a page mempool
126711e529ccSJules Maselbas * created by mempool_init_page_pool(), which allocates
12687a0bc2a8SColy Li * pages by alloc_pages() indeed.
1269cafe5635SKent Overstreet */
1270cafe5635SKent Overstreet
1271a85e968eSKent Overstreet out->magic = b->set->data->magic;
1272a85e968eSKent Overstreet out->seq = b->set->data->seq;
1273a85e968eSKent Overstreet out->version = b->set->data->version;
1274a85e968eSKent Overstreet swap(out, b->set->data);
1275cafe5635SKent Overstreet } else {
1276a85e968eSKent Overstreet b->set[start].data->keys = out->keys;
1277a85e968eSKent Overstreet memcpy(b->set[start].data->start, out->start,
1278fafff81cSKent Overstreet (void *) bset_bkey_last(out) - (void *) out->start);
1279cafe5635SKent Overstreet }
1280cafe5635SKent Overstreet
12810a451145SKent Overstreet if (used_mempool)
1282d19936a2SKent Overstreet mempool_free(virt_to_page(out), &state->pool);
1283cafe5635SKent Overstreet else
1284cafe5635SKent Overstreet free_pages((unsigned long) out, order);
1285cafe5635SKent Overstreet
1286a85e968eSKent Overstreet bch_bset_build_written_tree(b);
1287cafe5635SKent Overstreet
128865d22e91SKent Overstreet if (!start)
128967539e85SKent Overstreet bch_time_stats_update(&state->time, start_time);
1290cafe5635SKent Overstreet }
1291cafe5635SKent Overstreet
bch_btree_sort_partial(struct btree_keys * b,unsigned int start,struct bset_sort_state * state)12926f10f7d1SColy Li void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,
129367539e85SKent Overstreet struct bset_sort_state *state)
1294cafe5635SKent Overstreet {
129589ebb4a2SKent Overstreet size_t order = b->page_order, keys = 0;
1296*6479b9f4SMatthew Mirvish struct btree_iter_stack iter;
129789ebb4a2SKent Overstreet int oldsize = bch_count_data(b);
1298280481d0SKent Overstreet
1299*6479b9f4SMatthew Mirvish __bch_btree_iter_stack_init(b, &iter, NULL, &b->set[start]);
1300cafe5635SKent Overstreet
1301cafe5635SKent Overstreet if (start) {
13026f10f7d1SColy Li unsigned int i;
1303cafe5635SKent Overstreet
130489ebb4a2SKent Overstreet for (i = start; i <= b->nsets; i++)
130589ebb4a2SKent Overstreet keys += b->set[i].data->keys;
1306cafe5635SKent Overstreet
130789ebb4a2SKent Overstreet order = get_order(__set_bytes(b->set->data, keys));
1308cafe5635SKent Overstreet }
1309cafe5635SKent Overstreet
1310*6479b9f4SMatthew Mirvish __btree_sort(b, &iter.iter, start, order, false, state);
1311cafe5635SKent Overstreet
131289ebb4a2SKent Overstreet EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize);
1313cafe5635SKent Overstreet }
1314cafe5635SKent Overstreet
bch_btree_sort_and_fix_extents(struct btree_keys * b,struct btree_iter * iter,struct bset_sort_state * state)1315a85e968eSKent Overstreet void bch_btree_sort_and_fix_extents(struct btree_keys *b,
1316a85e968eSKent Overstreet struct btree_iter *iter,
131767539e85SKent Overstreet struct bset_sort_state *state)
1318cafe5635SKent Overstreet {
131967539e85SKent Overstreet __btree_sort(b, iter, 0, b->page_order, true, state);
1320cafe5635SKent Overstreet }
1321cafe5635SKent Overstreet
bch_btree_sort_into(struct btree_keys * b,struct btree_keys * new,struct bset_sort_state * state)132289ebb4a2SKent Overstreet void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
132367539e85SKent Overstreet struct bset_sort_state *state)
1324cafe5635SKent Overstreet {
1325cafe5635SKent Overstreet uint64_t start_time = local_clock();
1326*6479b9f4SMatthew Mirvish struct btree_iter_stack iter;
13271fae7cf0SColy Li
1328*6479b9f4SMatthew Mirvish bch_btree_iter_stack_init(b, &iter, NULL);
1329cafe5635SKent Overstreet
1330*6479b9f4SMatthew Mirvish btree_mergesort(b, new->set->data, &iter.iter, false, true);
1331cafe5635SKent Overstreet
133267539e85SKent Overstreet bch_time_stats_update(&state->time, start_time);
1333cafe5635SKent Overstreet
133489ebb4a2SKent Overstreet new->set->size = 0; // XXX: why?
1335cafe5635SKent Overstreet }
1336cafe5635SKent Overstreet
13376ded34d1SKent Overstreet #define SORT_CRIT (4096 / sizeof(uint64_t))
13386ded34d1SKent Overstreet
bch_btree_sort_lazy(struct btree_keys * b,struct bset_sort_state * state)133989ebb4a2SKent Overstreet void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state)
1340cafe5635SKent Overstreet {
13416f10f7d1SColy Li unsigned int crit = SORT_CRIT;
13426ded34d1SKent Overstreet int i;
1343cafe5635SKent Overstreet
13446ded34d1SKent Overstreet /* Don't sort if nothing to do */
134589ebb4a2SKent Overstreet if (!b->nsets)
13466ded34d1SKent Overstreet goto out;
1347cafe5635SKent Overstreet
134889ebb4a2SKent Overstreet for (i = b->nsets - 1; i >= 0; --i) {
134967539e85SKent Overstreet crit *= state->crit_factor;
13506ded34d1SKent Overstreet
135189ebb4a2SKent Overstreet if (b->set[i].data->keys < crit) {
135267539e85SKent Overstreet bch_btree_sort_partial(b, i, state);
13536ded34d1SKent Overstreet return;
13546ded34d1SKent Overstreet }
1355cafe5635SKent Overstreet }
1356cafe5635SKent Overstreet
13576ded34d1SKent Overstreet /* Sort if we'd overflow */
135889ebb4a2SKent Overstreet if (b->nsets + 1 == MAX_BSETS) {
135967539e85SKent Overstreet bch_btree_sort(b, state);
13606ded34d1SKent Overstreet return;
13616ded34d1SKent Overstreet }
13626ded34d1SKent Overstreet
13636ded34d1SKent Overstreet out:
136489ebb4a2SKent Overstreet bch_bset_build_written_tree(b);
1365cafe5635SKent Overstreet }
1366cafe5635SKent Overstreet
bch_btree_keys_stats(struct btree_keys * b,struct bset_stats * stats)1367f67342ddSKent Overstreet void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats)
1368cafe5635SKent Overstreet {
13696f10f7d1SColy Li unsigned int i;
1370cafe5635SKent Overstreet
1371f67342ddSKent Overstreet for (i = 0; i <= b->nsets; i++) {
1372f67342ddSKent Overstreet struct bset_tree *t = &b->set[i];
1373cafe5635SKent Overstreet size_t bytes = t->data->keys * sizeof(uint64_t);
1374cafe5635SKent Overstreet size_t j;
1375cafe5635SKent Overstreet
1376f67342ddSKent Overstreet if (bset_written(b, t)) {
1377cafe5635SKent Overstreet stats->sets_written++;
1378cafe5635SKent Overstreet stats->bytes_written += bytes;
1379cafe5635SKent Overstreet
1380cafe5635SKent Overstreet stats->floats += t->size - 1;
1381cafe5635SKent Overstreet
1382cafe5635SKent Overstreet for (j = 1; j < t->size; j++)
1383cafe5635SKent Overstreet if (t->tree[j].exponent == 127)
1384cafe5635SKent Overstreet stats->failed++;
1385cafe5635SKent Overstreet } else {
1386cafe5635SKent Overstreet stats->sets_unwritten++;
1387cafe5635SKent Overstreet stats->bytes_unwritten += bytes;
1388cafe5635SKent Overstreet }
1389cafe5635SKent Overstreet }
1390cafe5635SKent Overstreet }
1391