1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _BCACHE_BSET_H
3*4882a593Smuzhiyun #define _BCACHE_BSET_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/bcache.h>
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include "util.h" /* for time_stats */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * BKEYS:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * A bkey contains a key, a size field, a variable number of pointers, and some
15*4882a593Smuzhiyun * ancillary flag bits.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * We use two different functions for validating bkeys, bch_ptr_invalid and
18*4882a593Smuzhiyun * bch_ptr_bad().
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * bch_ptr_invalid() primarily filters out keys and pointers that would be
21*4882a593Smuzhiyun * invalid due to some sort of bug, whereas bch_ptr_bad() filters out keys and
22*4882a593Smuzhiyun * pointer that occur in normal practice but don't point to real data.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * The one exception to the rule that ptr_invalid() filters out invalid keys is
25*4882a593Smuzhiyun * that it also filters out keys of size 0 - these are keys that have been
26*4882a593Smuzhiyun * completely overwritten. It'd be safe to delete these in memory while leaving
27*4882a593Smuzhiyun * them on disk, just unnecessary work - so we filter them out when resorting
28*4882a593Smuzhiyun * instead.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * We can't filter out stale keys when we're resorting, because garbage
31*4882a593Smuzhiyun * collection needs to find them to ensure bucket gens don't wrap around -
32*4882a593Smuzhiyun * unless we're rewriting the btree node those stale keys still exist on disk.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * We also implement functions here for removing some number of sectors from the
35*4882a593Smuzhiyun * front or the back of a bkey - this is mainly used for fixing overlapping
36*4882a593Smuzhiyun * extents, by removing the overlapping sectors from the older key.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * BSETS:
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * A bset is an array of bkeys laid out contiguously in memory in sorted order,
41*4882a593Smuzhiyun * along with a header. A btree node is made up of a number of these, written at
42*4882a593Smuzhiyun * different times.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * There could be many of them on disk, but we never allow there to be more than
45*4882a593Smuzhiyun * 4 in memory - we lazily resort as needed.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * We implement code here for creating and maintaining auxiliary search trees
48*4882a593Smuzhiyun * (described below) for searching an individial bset, and on top of that we
49*4882a593Smuzhiyun * implement a btree iterator.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * BTREE ITERATOR:
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * Most of the code in bcache doesn't care about an individual bset - it needs
54*4882a593Smuzhiyun * to search entire btree nodes and iterate over them in sorted order.
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * The btree iterator code serves both functions; it iterates through the keys
57*4882a593Smuzhiyun * in a btree node in sorted order, starting from either keys after a specific
58*4882a593Smuzhiyun * point (if you pass it a search key) or the start of the btree node.
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * AUXILIARY SEARCH TREES:
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * Since keys are variable length, we can't use a binary search on a bset - we
63*4882a593Smuzhiyun * wouldn't be able to find the start of the next key. But binary searches are
64*4882a593Smuzhiyun * slow anyways, due to terrible cache behaviour; bcache originally used binary
65*4882a593Smuzhiyun * searches and that code topped out at under 50k lookups/second.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * So we need to construct some sort of lookup table. Since we only insert keys
68*4882a593Smuzhiyun * into the last (unwritten) set, most of the keys within a given btree node are
69*4882a593Smuzhiyun * usually in sets that are mostly constant. We use two different types of
70*4882a593Smuzhiyun * lookup tables to take advantage of this.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * Both lookup tables share in common that they don't index every key in the
73*4882a593Smuzhiyun * set; they index one key every BSET_CACHELINE bytes, and then a linear search
74*4882a593Smuzhiyun * is used for the rest.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * For sets that have been written to disk and are no longer being inserted
77*4882a593Smuzhiyun * into, we construct a binary search tree in an array - traversing a binary
78*4882a593Smuzhiyun * search tree in an array gives excellent locality of reference and is very
79*4882a593Smuzhiyun * fast, since both children of any node are adjacent to each other in memory
80*4882a593Smuzhiyun * (and their grandchildren, and great grandchildren...) - this means
81*4882a593Smuzhiyun * prefetching can be used to great effect.
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * It's quite useful performance wise to keep these nodes small - not just
84*4882a593Smuzhiyun * because they're more likely to be in L2, but also because we can prefetch
85*4882a593Smuzhiyun * more nodes on a single cacheline and thus prefetch more iterations in advance
86*4882a593Smuzhiyun * when traversing this tree.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * Nodes in the auxiliary search tree must contain both a key to compare against
89*4882a593Smuzhiyun * (we don't want to fetch the key from the set, that would defeat the purpose),
90*4882a593Smuzhiyun * and a pointer to the key. We use a few tricks to compress both of these.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * To compress the pointer, we take advantage of the fact that one node in the
93*4882a593Smuzhiyun * search tree corresponds to precisely BSET_CACHELINE bytes in the set. We have
94*4882a593Smuzhiyun * a function (to_inorder()) that takes the index of a node in a binary tree and
95*4882a593Smuzhiyun * returns what its index would be in an inorder traversal, so we only have to
96*4882a593Smuzhiyun * store the low bits of the offset.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * The key is 84 bits (KEY_DEV + key->key, the offset on the device). To
99*4882a593Smuzhiyun * compress that, we take advantage of the fact that when we're traversing the
100*4882a593Smuzhiyun * search tree at every iteration we know that both our search key and the key
101*4882a593Smuzhiyun * we're looking for lie within some range - bounded by our previous
102*4882a593Smuzhiyun * comparisons. (We special case the start of a search so that this is true even
103*4882a593Smuzhiyun * at the root of the tree).
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * So we know the key we're looking for is between a and b, and a and b don't
106*4882a593Smuzhiyun * differ higher than bit 50, we don't need to check anything higher than bit
107*4882a593Smuzhiyun * 50.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * We don't usually need the rest of the bits, either; we only need enough bits
110*4882a593Smuzhiyun * to partition the key range we're currently checking. Consider key n - the
111*4882a593Smuzhiyun * key our auxiliary search tree node corresponds to, and key p, the key
112*4882a593Smuzhiyun * immediately preceding n. The lowest bit we need to store in the auxiliary
113*4882a593Smuzhiyun * search tree is the highest bit that differs between n and p.
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * Note that this could be bit 0 - we might sometimes need all 80 bits to do the
116*4882a593Smuzhiyun * comparison. But we'd really like our nodes in the auxiliary search tree to be
117*4882a593Smuzhiyun * of fixed size.
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * The solution is to make them fixed size, and when we're constructing a node
120*4882a593Smuzhiyun * check if p and n differed in the bits we needed them to. If they don't we
121*4882a593Smuzhiyun * flag that node, and when doing lookups we fallback to comparing against the
122*4882a593Smuzhiyun * real key. As long as this doesn't happen to often (and it seems to reliably
123*4882a593Smuzhiyun * happen a bit less than 1% of the time), we win - even on failures, that key
124*4882a593Smuzhiyun * is then more likely to be in cache than if we were doing binary searches all
125*4882a593Smuzhiyun * the way, since we're touching so much less memory.
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * The keys in the auxiliary search tree are stored in (software) floating
128*4882a593Smuzhiyun * point, with an exponent and a mantissa. The exponent needs to be big enough
129*4882a593Smuzhiyun * to address all the bits in the original key, but the number of bits in the
130*4882a593Smuzhiyun * mantissa is somewhat arbitrary; more bits just gets us fewer failures.
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * We need 7 bits for the exponent and 3 bits for the key's offset (since keys
133*4882a593Smuzhiyun * are 8 byte aligned); using 22 bits for the mantissa means a node is 4 bytes.
134*4882a593Smuzhiyun * We need one node per 128 bytes in the btree node, which means the auxiliary
135*4882a593Smuzhiyun * search trees take up 3% as much memory as the btree itself.
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * Constructing these auxiliary search trees is moderately expensive, and we
138*4882a593Smuzhiyun * don't want to be constantly rebuilding the search tree for the last set
139*4882a593Smuzhiyun * whenever we insert another key into it. For the unwritten set, we use a much
140*4882a593Smuzhiyun * simpler lookup table - it's just a flat array, so index i in the lookup table
141*4882a593Smuzhiyun * corresponds to the i range of BSET_CACHELINE bytes in the set. Indexing
142*4882a593Smuzhiyun * within each byte range works the same as with the auxiliary search trees.
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * These are much easier to keep up to date when we insert a key - we do it
145*4882a593Smuzhiyun * somewhat lazily; when we shift a key up we usually just increment the pointer
146*4882a593Smuzhiyun * to it, only when it would overflow do we go to the trouble of finding the
147*4882a593Smuzhiyun * first key in that range of bytes again.
148*4882a593Smuzhiyun */
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun struct btree_keys;
151*4882a593Smuzhiyun struct btree_iter;
152*4882a593Smuzhiyun struct btree_iter_set;
153*4882a593Smuzhiyun struct bkey_float;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun #define MAX_BSETS 4U
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun struct bset_tree {
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * We construct a binary tree in an array as if the array
160*4882a593Smuzhiyun * started at 1, so that things line up on the same cachelines
161*4882a593Smuzhiyun * better: see comments in bset.c at cacheline_to_bkey() for
162*4882a593Smuzhiyun * details
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* size of the binary tree and prev array */
166*4882a593Smuzhiyun unsigned int size;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* function of size - precalculated for to_inorder() */
169*4882a593Smuzhiyun unsigned int extra;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* copy of the last key in the set */
172*4882a593Smuzhiyun struct bkey end;
173*4882a593Smuzhiyun struct bkey_float *tree;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /*
176*4882a593Smuzhiyun * The nodes in the bset tree point to specific keys - this
177*4882a593Smuzhiyun * array holds the sizes of the previous key.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * Conceptually it's a member of struct bkey_float, but we want
180*4882a593Smuzhiyun * to keep bkey_float to 4 bytes and prev isn't used in the fast
181*4882a593Smuzhiyun * path.
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun uint8_t *prev;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* The actual btree node, with pointers to each sorted set */
186*4882a593Smuzhiyun struct bset *data;
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun struct btree_keys_ops {
190*4882a593Smuzhiyun bool (*sort_cmp)(struct btree_iter_set l,
191*4882a593Smuzhiyun struct btree_iter_set r);
192*4882a593Smuzhiyun struct bkey *(*sort_fixup)(struct btree_iter *iter,
193*4882a593Smuzhiyun struct bkey *tmp);
194*4882a593Smuzhiyun bool (*insert_fixup)(struct btree_keys *b,
195*4882a593Smuzhiyun struct bkey *insert,
196*4882a593Smuzhiyun struct btree_iter *iter,
197*4882a593Smuzhiyun struct bkey *replace_key);
198*4882a593Smuzhiyun bool (*key_invalid)(struct btree_keys *bk,
199*4882a593Smuzhiyun const struct bkey *k);
200*4882a593Smuzhiyun bool (*key_bad)(struct btree_keys *bk,
201*4882a593Smuzhiyun const struct bkey *k);
202*4882a593Smuzhiyun bool (*key_merge)(struct btree_keys *bk,
203*4882a593Smuzhiyun struct bkey *l, struct bkey *r);
204*4882a593Smuzhiyun void (*key_to_text)(char *buf,
205*4882a593Smuzhiyun size_t size,
206*4882a593Smuzhiyun const struct bkey *k);
207*4882a593Smuzhiyun void (*key_dump)(struct btree_keys *keys,
208*4882a593Smuzhiyun const struct bkey *k);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun * Only used for deciding whether to use START_KEY(k) or just the key
212*4882a593Smuzhiyun * itself in a couple places
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun bool is_extents;
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun struct btree_keys {
218*4882a593Smuzhiyun const struct btree_keys_ops *ops;
219*4882a593Smuzhiyun uint8_t page_order;
220*4882a593Smuzhiyun uint8_t nsets;
221*4882a593Smuzhiyun unsigned int last_set_unwritten:1;
222*4882a593Smuzhiyun bool *expensive_debug_checks;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun * Sets of sorted keys - the real btree node - plus a binary search tree
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
228*4882a593Smuzhiyun * to the memory we have allocated for this btree node. Additionally,
229*4882a593Smuzhiyun * set[0]->data points to the entire btree node as it exists on disk.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun struct bset_tree set[MAX_BSETS];
232*4882a593Smuzhiyun };
233*4882a593Smuzhiyun
bset_tree_last(struct btree_keys * b)234*4882a593Smuzhiyun static inline struct bset_tree *bset_tree_last(struct btree_keys *b)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun return b->set + b->nsets;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
bset_written(struct btree_keys * b,struct bset_tree * t)239*4882a593Smuzhiyun static inline bool bset_written(struct btree_keys *b, struct bset_tree *t)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun return t <= b->set + b->nsets - b->last_set_unwritten;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
bkey_written(struct btree_keys * b,struct bkey * k)244*4882a593Smuzhiyun static inline bool bkey_written(struct btree_keys *b, struct bkey *k)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
bset_byte_offset(struct btree_keys * b,struct bset * i)249*4882a593Smuzhiyun static inline unsigned int bset_byte_offset(struct btree_keys *b,
250*4882a593Smuzhiyun struct bset *i)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun return ((size_t) i) - ((size_t) b->set->data);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
bset_sector_offset(struct btree_keys * b,struct bset * i)255*4882a593Smuzhiyun static inline unsigned int bset_sector_offset(struct btree_keys *b,
256*4882a593Smuzhiyun struct bset *i)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun return bset_byte_offset(b, i) >> 9;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun #define __set_bytes(i, k) (sizeof(*(i)) + (k) * sizeof(uint64_t))
262*4882a593Smuzhiyun #define set_bytes(i) __set_bytes(i, i->keys)
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun #define __set_blocks(i, k, block_bytes) \
265*4882a593Smuzhiyun DIV_ROUND_UP(__set_bytes(i, k), block_bytes)
266*4882a593Smuzhiyun #define set_blocks(i, block_bytes) \
267*4882a593Smuzhiyun __set_blocks(i, (i)->keys, block_bytes)
268*4882a593Smuzhiyun
bch_btree_keys_u64s_remaining(struct btree_keys * b)269*4882a593Smuzhiyun static inline size_t bch_btree_keys_u64s_remaining(struct btree_keys *b)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun struct bset_tree *t = bset_tree_last(b);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun BUG_ON((PAGE_SIZE << b->page_order) <
274*4882a593Smuzhiyun (bset_byte_offset(b, t->data) + set_bytes(t->data)));
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (!b->last_set_unwritten)
277*4882a593Smuzhiyun return 0;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun return ((PAGE_SIZE << b->page_order) -
280*4882a593Smuzhiyun (bset_byte_offset(b, t->data) + set_bytes(t->data))) /
281*4882a593Smuzhiyun sizeof(u64);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
bset_next_set(struct btree_keys * b,unsigned int block_bytes)284*4882a593Smuzhiyun static inline struct bset *bset_next_set(struct btree_keys *b,
285*4882a593Smuzhiyun unsigned int block_bytes)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun struct bset *i = bset_tree_last(b)->data;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun return ((void *) i) + roundup(set_bytes(i), block_bytes);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun void bch_btree_keys_free(struct btree_keys *b);
293*4882a593Smuzhiyun int bch_btree_keys_alloc(struct btree_keys *b, unsigned int page_order,
294*4882a593Smuzhiyun gfp_t gfp);
295*4882a593Smuzhiyun void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
296*4882a593Smuzhiyun bool *expensive_debug_checks);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic);
299*4882a593Smuzhiyun void bch_bset_build_written_tree(struct btree_keys *b);
300*4882a593Smuzhiyun void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k);
301*4882a593Smuzhiyun bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r);
302*4882a593Smuzhiyun void bch_bset_insert(struct btree_keys *b, struct bkey *where,
303*4882a593Smuzhiyun struct bkey *insert);
304*4882a593Smuzhiyun unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
305*4882a593Smuzhiyun struct bkey *replace_key);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun enum {
308*4882a593Smuzhiyun BTREE_INSERT_STATUS_NO_INSERT = 0,
309*4882a593Smuzhiyun BTREE_INSERT_STATUS_INSERT,
310*4882a593Smuzhiyun BTREE_INSERT_STATUS_BACK_MERGE,
311*4882a593Smuzhiyun BTREE_INSERT_STATUS_OVERWROTE,
312*4882a593Smuzhiyun BTREE_INSERT_STATUS_FRONT_MERGE,
313*4882a593Smuzhiyun };
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /* Btree key iteration */
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun struct btree_iter {
318*4882a593Smuzhiyun size_t size, used;
319*4882a593Smuzhiyun #ifdef CONFIG_BCACHE_DEBUG
320*4882a593Smuzhiyun struct btree_keys *b;
321*4882a593Smuzhiyun #endif
322*4882a593Smuzhiyun struct btree_iter_set {
323*4882a593Smuzhiyun struct bkey *k, *end;
324*4882a593Smuzhiyun } data[MAX_BSETS];
325*4882a593Smuzhiyun };
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun typedef bool (*ptr_filter_fn)(struct btree_keys *b, const struct bkey *k);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun struct bkey *bch_btree_iter_next(struct btree_iter *iter);
330*4882a593Smuzhiyun struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
331*4882a593Smuzhiyun struct btree_keys *b,
332*4882a593Smuzhiyun ptr_filter_fn fn);
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
335*4882a593Smuzhiyun struct bkey *end);
336*4882a593Smuzhiyun struct bkey *bch_btree_iter_init(struct btree_keys *b,
337*4882a593Smuzhiyun struct btree_iter *iter,
338*4882a593Smuzhiyun struct bkey *search);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
341*4882a593Smuzhiyun const struct bkey *search);
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun * Returns the first key that is strictly greater than search
345*4882a593Smuzhiyun */
bch_bset_search(struct btree_keys * b,struct bset_tree * t,const struct bkey * search)346*4882a593Smuzhiyun static inline struct bkey *bch_bset_search(struct btree_keys *b,
347*4882a593Smuzhiyun struct bset_tree *t,
348*4882a593Smuzhiyun const struct bkey *search)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun return search ? __bch_bset_search(b, t, search) : t->data->start;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun #define for_each_key_filter(b, k, iter, filter) \
354*4882a593Smuzhiyun for (bch_btree_iter_init((b), (iter), NULL); \
355*4882a593Smuzhiyun ((k) = bch_btree_iter_next_filter((iter), (b), filter));)
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun #define for_each_key(b, k, iter) \
358*4882a593Smuzhiyun for (bch_btree_iter_init((b), (iter), NULL); \
359*4882a593Smuzhiyun ((k) = bch_btree_iter_next(iter));)
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /* Sorting */
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun struct bset_sort_state {
364*4882a593Smuzhiyun mempool_t pool;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun unsigned int page_order;
367*4882a593Smuzhiyun unsigned int crit_factor;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun struct time_stats time;
370*4882a593Smuzhiyun };
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun void bch_bset_sort_state_free(struct bset_sort_state *state);
373*4882a593Smuzhiyun int bch_bset_sort_state_init(struct bset_sort_state *state,
374*4882a593Smuzhiyun unsigned int page_order);
375*4882a593Smuzhiyun void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state);
376*4882a593Smuzhiyun void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
377*4882a593Smuzhiyun struct bset_sort_state *state);
378*4882a593Smuzhiyun void bch_btree_sort_and_fix_extents(struct btree_keys *b,
379*4882a593Smuzhiyun struct btree_iter *iter,
380*4882a593Smuzhiyun struct bset_sort_state *state);
381*4882a593Smuzhiyun void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,
382*4882a593Smuzhiyun struct bset_sort_state *state);
383*4882a593Smuzhiyun
bch_btree_sort(struct btree_keys * b,struct bset_sort_state * state)384*4882a593Smuzhiyun static inline void bch_btree_sort(struct btree_keys *b,
385*4882a593Smuzhiyun struct bset_sort_state *state)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun bch_btree_sort_partial(b, 0, state);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun struct bset_stats {
391*4882a593Smuzhiyun size_t sets_written, sets_unwritten;
392*4882a593Smuzhiyun size_t bytes_written, bytes_unwritten;
393*4882a593Smuzhiyun size_t floats, failed;
394*4882a593Smuzhiyun };
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *state);
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun /* Bkey utility code */
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun #define bset_bkey_last(i) bkey_idx((struct bkey *) (i)->d, \
401*4882a593Smuzhiyun (unsigned int)(i)->keys)
402*4882a593Smuzhiyun
bset_bkey_idx(struct bset * i,unsigned int idx)403*4882a593Smuzhiyun static inline struct bkey *bset_bkey_idx(struct bset *i, unsigned int idx)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun return bkey_idx(i->start, idx);
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
bkey_init(struct bkey * k)408*4882a593Smuzhiyun static inline void bkey_init(struct bkey *k)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun *k = ZERO_KEY;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
bkey_cmp(const struct bkey * l,const struct bkey * r)413*4882a593Smuzhiyun static __always_inline int64_t bkey_cmp(const struct bkey *l,
414*4882a593Smuzhiyun const struct bkey *r)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun return unlikely(KEY_INODE(l) != KEY_INODE(r))
417*4882a593Smuzhiyun ? (int64_t) KEY_INODE(l) - (int64_t) KEY_INODE(r)
418*4882a593Smuzhiyun : (int64_t) KEY_OFFSET(l) - (int64_t) KEY_OFFSET(r);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
422*4882a593Smuzhiyun unsigned int i);
423*4882a593Smuzhiyun bool __bch_cut_front(const struct bkey *where, struct bkey *k);
424*4882a593Smuzhiyun bool __bch_cut_back(const struct bkey *where, struct bkey *k);
425*4882a593Smuzhiyun
bch_cut_front(const struct bkey * where,struct bkey * k)426*4882a593Smuzhiyun static inline bool bch_cut_front(const struct bkey *where, struct bkey *k)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun BUG_ON(bkey_cmp(where, k) > 0);
429*4882a593Smuzhiyun return __bch_cut_front(where, k);
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
bch_cut_back(const struct bkey * where,struct bkey * k)432*4882a593Smuzhiyun static inline bool bch_cut_back(const struct bkey *where, struct bkey *k)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun BUG_ON(bkey_cmp(where, &START_KEY(k)) < 0);
435*4882a593Smuzhiyun return __bch_cut_back(where, k);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun /*
439*4882a593Smuzhiyun * Pointer '*preceding_key_p' points to a memory object to store preceding
440*4882a593Smuzhiyun * key of k. If the preceding key does not exist, set '*preceding_key_p' to
441*4882a593Smuzhiyun * NULL. So the caller of preceding_key() needs to take care of memory
442*4882a593Smuzhiyun * which '*preceding_key_p' pointed to before calling preceding_key().
443*4882a593Smuzhiyun * Currently the only caller of preceding_key() is bch_btree_insert_key(),
444*4882a593Smuzhiyun * and it points to an on-stack variable, so the memory release is handled
445*4882a593Smuzhiyun * by stackframe itself.
446*4882a593Smuzhiyun */
preceding_key(struct bkey * k,struct bkey ** preceding_key_p)447*4882a593Smuzhiyun static inline void preceding_key(struct bkey *k, struct bkey **preceding_key_p)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun if (KEY_INODE(k) || KEY_OFFSET(k)) {
450*4882a593Smuzhiyun (**preceding_key_p) = KEY(KEY_INODE(k), KEY_OFFSET(k), 0);
451*4882a593Smuzhiyun if (!(*preceding_key_p)->low)
452*4882a593Smuzhiyun (*preceding_key_p)->high--;
453*4882a593Smuzhiyun (*preceding_key_p)->low--;
454*4882a593Smuzhiyun } else {
455*4882a593Smuzhiyun (*preceding_key_p) = NULL;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
bch_ptr_invalid(struct btree_keys * b,const struct bkey * k)459*4882a593Smuzhiyun static inline bool bch_ptr_invalid(struct btree_keys *b, const struct bkey *k)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun return b->ops->key_invalid(b, k);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun
bch_ptr_bad(struct btree_keys * b,const struct bkey * k)464*4882a593Smuzhiyun static inline bool bch_ptr_bad(struct btree_keys *b, const struct bkey *k)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun return b->ops->key_bad(b, k);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
bch_bkey_to_text(struct btree_keys * b,char * buf,size_t size,const struct bkey * k)469*4882a593Smuzhiyun static inline void bch_bkey_to_text(struct btree_keys *b, char *buf,
470*4882a593Smuzhiyun size_t size, const struct bkey *k)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun return b->ops->key_to_text(buf, size, k);
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
bch_bkey_equal_header(const struct bkey * l,const struct bkey * r)475*4882a593Smuzhiyun static inline bool bch_bkey_equal_header(const struct bkey *l,
476*4882a593Smuzhiyun const struct bkey *r)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun return (KEY_DIRTY(l) == KEY_DIRTY(r) &&
479*4882a593Smuzhiyun KEY_PTRS(l) == KEY_PTRS(r) &&
480*4882a593Smuzhiyun KEY_CSUM(l) == KEY_CSUM(r));
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /* Keylists */
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun struct keylist {
486*4882a593Smuzhiyun union {
487*4882a593Smuzhiyun struct bkey *keys;
488*4882a593Smuzhiyun uint64_t *keys_p;
489*4882a593Smuzhiyun };
490*4882a593Smuzhiyun union {
491*4882a593Smuzhiyun struct bkey *top;
492*4882a593Smuzhiyun uint64_t *top_p;
493*4882a593Smuzhiyun };
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /* Enough room for btree_split's keys without realloc */
496*4882a593Smuzhiyun #define KEYLIST_INLINE 16
497*4882a593Smuzhiyun uint64_t inline_keys[KEYLIST_INLINE];
498*4882a593Smuzhiyun };
499*4882a593Smuzhiyun
bch_keylist_init(struct keylist * l)500*4882a593Smuzhiyun static inline void bch_keylist_init(struct keylist *l)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun l->top_p = l->keys_p = l->inline_keys;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
bch_keylist_init_single(struct keylist * l,struct bkey * k)505*4882a593Smuzhiyun static inline void bch_keylist_init_single(struct keylist *l, struct bkey *k)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun l->keys = k;
508*4882a593Smuzhiyun l->top = bkey_next(k);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
bch_keylist_push(struct keylist * l)511*4882a593Smuzhiyun static inline void bch_keylist_push(struct keylist *l)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun l->top = bkey_next(l->top);
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
bch_keylist_add(struct keylist * l,struct bkey * k)516*4882a593Smuzhiyun static inline void bch_keylist_add(struct keylist *l, struct bkey *k)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun bkey_copy(l->top, k);
519*4882a593Smuzhiyun bch_keylist_push(l);
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
bch_keylist_empty(struct keylist * l)522*4882a593Smuzhiyun static inline bool bch_keylist_empty(struct keylist *l)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun return l->top == l->keys;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun
bch_keylist_reset(struct keylist * l)527*4882a593Smuzhiyun static inline void bch_keylist_reset(struct keylist *l)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun l->top = l->keys;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
bch_keylist_free(struct keylist * l)532*4882a593Smuzhiyun static inline void bch_keylist_free(struct keylist *l)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun if (l->keys_p != l->inline_keys)
535*4882a593Smuzhiyun kfree(l->keys_p);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
bch_keylist_nkeys(struct keylist * l)538*4882a593Smuzhiyun static inline size_t bch_keylist_nkeys(struct keylist *l)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun return l->top_p - l->keys_p;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
bch_keylist_bytes(struct keylist * l)543*4882a593Smuzhiyun static inline size_t bch_keylist_bytes(struct keylist *l)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun return bch_keylist_nkeys(l) * sizeof(uint64_t);
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun struct bkey *bch_keylist_pop(struct keylist *l);
549*4882a593Smuzhiyun void bch_keylist_pop_front(struct keylist *l);
550*4882a593Smuzhiyun int __bch_keylist_realloc(struct keylist *l, unsigned int u64s);
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /* Debug stuff */
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun #ifdef CONFIG_BCACHE_DEBUG
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun int __bch_count_data(struct btree_keys *b);
557*4882a593Smuzhiyun void __printf(2, 3) __bch_check_keys(struct btree_keys *b,
558*4882a593Smuzhiyun const char *fmt,
559*4882a593Smuzhiyun ...);
560*4882a593Smuzhiyun void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set);
561*4882a593Smuzhiyun void bch_dump_bucket(struct btree_keys *b);
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun #else
564*4882a593Smuzhiyun
__bch_count_data(struct btree_keys * b)565*4882a593Smuzhiyun static inline int __bch_count_data(struct btree_keys *b) { return -1; }
566*4882a593Smuzhiyun static inline void __printf(2, 3)
__bch_check_keys(struct btree_keys * b,const char * fmt,...)567*4882a593Smuzhiyun __bch_check_keys(struct btree_keys *b, const char *fmt, ...) {}
bch_dump_bucket(struct btree_keys * b)568*4882a593Smuzhiyun static inline void bch_dump_bucket(struct btree_keys *b) {}
569*4882a593Smuzhiyun void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set);
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun #endif
572*4882a593Smuzhiyun
btree_keys_expensive_checks(struct btree_keys * b)573*4882a593Smuzhiyun static inline bool btree_keys_expensive_checks(struct btree_keys *b)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun #ifdef CONFIG_BCACHE_DEBUG
576*4882a593Smuzhiyun return *b->expensive_debug_checks;
577*4882a593Smuzhiyun #else
578*4882a593Smuzhiyun return false;
579*4882a593Smuzhiyun #endif
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
bch_count_data(struct btree_keys * b)582*4882a593Smuzhiyun static inline int bch_count_data(struct btree_keys *b)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun return btree_keys_expensive_checks(b) ? __bch_count_data(b) : -1;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun #define bch_check_keys(b, ...) \
588*4882a593Smuzhiyun do { \
589*4882a593Smuzhiyun if (btree_keys_expensive_checks(b)) \
590*4882a593Smuzhiyun __bch_check_keys(b, __VA_ARGS__); \
591*4882a593Smuzhiyun } while (0)
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun #endif
594