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