blob: 563130c28142a613c83384eba3def1203eb75585 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001#ifndef _BCACHE_BSET_H
2#define _BCACHE_BSET_H
3
Kent Overstreetc37511b2013-04-26 15:39:55 -07004#include <linux/slab.h>
5
Kent Overstreet67539e82013-09-10 22:53:34 -07006#include "util.h" /* for time_stats */
7
Kent Overstreetcafe5632013-03-23 16:11:31 -07008/*
9 * BKEYS:
10 *
11 * A bkey contains a key, a size field, a variable number of pointers, and some
12 * ancillary flag bits.
13 *
14 * We use two different functions for validating bkeys, bch_ptr_invalid and
15 * bch_ptr_bad().
16 *
17 * bch_ptr_invalid() primarily filters out keys and pointers that would be
18 * invalid due to some sort of bug, whereas bch_ptr_bad() filters out keys and
19 * pointer that occur in normal practice but don't point to real data.
20 *
21 * The one exception to the rule that ptr_invalid() filters out invalid keys is
22 * that it also filters out keys of size 0 - these are keys that have been
23 * completely overwritten. It'd be safe to delete these in memory while leaving
24 * them on disk, just unnecessary work - so we filter them out when resorting
25 * instead.
26 *
27 * We can't filter out stale keys when we're resorting, because garbage
28 * collection needs to find them to ensure bucket gens don't wrap around -
29 * unless we're rewriting the btree node those stale keys still exist on disk.
30 *
31 * We also implement functions here for removing some number of sectors from the
32 * front or the back of a bkey - this is mainly used for fixing overlapping
33 * extents, by removing the overlapping sectors from the older key.
34 *
35 * BSETS:
36 *
37 * A bset is an array of bkeys laid out contiguously in memory in sorted order,
38 * along with a header. A btree node is made up of a number of these, written at
39 * different times.
40 *
41 * There could be many of them on disk, but we never allow there to be more than
42 * 4 in memory - we lazily resort as needed.
43 *
44 * We implement code here for creating and maintaining auxiliary search trees
45 * (described below) for searching an individial bset, and on top of that we
46 * implement a btree iterator.
47 *
48 * BTREE ITERATOR:
49 *
50 * Most of the code in bcache doesn't care about an individual bset - it needs
51 * to search entire btree nodes and iterate over them in sorted order.
52 *
53 * The btree iterator code serves both functions; it iterates through the keys
54 * in a btree node in sorted order, starting from either keys after a specific
55 * point (if you pass it a search key) or the start of the btree node.
56 *
57 * AUXILIARY SEARCH TREES:
58 *
59 * Since keys are variable length, we can't use a binary search on a bset - we
60 * wouldn't be able to find the start of the next key. But binary searches are
61 * slow anyways, due to terrible cache behaviour; bcache originally used binary
62 * searches and that code topped out at under 50k lookups/second.
63 *
64 * So we need to construct some sort of lookup table. Since we only insert keys
65 * into the last (unwritten) set, most of the keys within a given btree node are
66 * usually in sets that are mostly constant. We use two different types of
67 * lookup tables to take advantage of this.
68 *
69 * Both lookup tables share in common that they don't index every key in the
70 * set; they index one key every BSET_CACHELINE bytes, and then a linear search
71 * is used for the rest.
72 *
73 * For sets that have been written to disk and are no longer being inserted
74 * into, we construct a binary search tree in an array - traversing a binary
75 * search tree in an array gives excellent locality of reference and is very
76 * fast, since both children of any node are adjacent to each other in memory
77 * (and their grandchildren, and great grandchildren...) - this means
78 * prefetching can be used to great effect.
79 *
80 * It's quite useful performance wise to keep these nodes small - not just
81 * because they're more likely to be in L2, but also because we can prefetch
82 * more nodes on a single cacheline and thus prefetch more iterations in advance
83 * when traversing this tree.
84 *
85 * Nodes in the auxiliary search tree must contain both a key to compare against
86 * (we don't want to fetch the key from the set, that would defeat the purpose),
87 * and a pointer to the key. We use a few tricks to compress both of these.
88 *
89 * To compress the pointer, we take advantage of the fact that one node in the
90 * search tree corresponds to precisely BSET_CACHELINE bytes in the set. We have
91 * a function (to_inorder()) that takes the index of a node in a binary tree and
92 * returns what its index would be in an inorder traversal, so we only have to
93 * store the low bits of the offset.
94 *
95 * The key is 84 bits (KEY_DEV + key->key, the offset on the device). To
96 * compress that, we take advantage of the fact that when we're traversing the
97 * search tree at every iteration we know that both our search key and the key
98 * we're looking for lie within some range - bounded by our previous
99 * comparisons. (We special case the start of a search so that this is true even
100 * at the root of the tree).
101 *
102 * So we know the key we're looking for is between a and b, and a and b don't
103 * differ higher than bit 50, we don't need to check anything higher than bit
104 * 50.
105 *
106 * We don't usually need the rest of the bits, either; we only need enough bits
107 * to partition the key range we're currently checking. Consider key n - the
108 * key our auxiliary search tree node corresponds to, and key p, the key
109 * immediately preceding n. The lowest bit we need to store in the auxiliary
110 * search tree is the highest bit that differs between n and p.
111 *
112 * Note that this could be bit 0 - we might sometimes need all 80 bits to do the
113 * comparison. But we'd really like our nodes in the auxiliary search tree to be
114 * of fixed size.
115 *
116 * The solution is to make them fixed size, and when we're constructing a node
117 * check if p and n differed in the bits we needed them to. If they don't we
118 * flag that node, and when doing lookups we fallback to comparing against the
119 * real key. As long as this doesn't happen to often (and it seems to reliably
120 * happen a bit less than 1% of the time), we win - even on failures, that key
121 * is then more likely to be in cache than if we were doing binary searches all
122 * the way, since we're touching so much less memory.
123 *
124 * The keys in the auxiliary search tree are stored in (software) floating
125 * point, with an exponent and a mantissa. The exponent needs to be big enough
126 * to address all the bits in the original key, but the number of bits in the
127 * mantissa is somewhat arbitrary; more bits just gets us fewer failures.
128 *
129 * We need 7 bits for the exponent and 3 bits for the key's offset (since keys
130 * are 8 byte aligned); using 22 bits for the mantissa means a node is 4 bytes.
131 * We need one node per 128 bytes in the btree node, which means the auxiliary
132 * search trees take up 3% as much memory as the btree itself.
133 *
134 * Constructing these auxiliary search trees is moderately expensive, and we
135 * don't want to be constantly rebuilding the search tree for the last set
136 * whenever we insert another key into it. For the unwritten set, we use a much
137 * simpler lookup table - it's just a flat array, so index i in the lookup table
138 * corresponds to the i range of BSET_CACHELINE bytes in the set. Indexing
139 * within each byte range works the same as with the auxiliary search trees.
140 *
141 * These are much easier to keep up to date when we insert a key - we do it
142 * somewhat lazily; when we shift a key up we usually just increment the pointer
143 * to it, only when it would overflow do we go to the trouble of finding the
144 * first key in that range of bytes again.
145 */
146
Kent Overstreetee811282013-12-17 23:49:49 -0800147struct btree;
Kent Overstreeta85e9682013-12-20 17:28:16 -0800148struct btree_keys;
149struct btree_iter;
150struct btree_iter_set;
Kent Overstreetee811282013-12-17 23:49:49 -0800151struct bkey_float;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700152
Kent Overstreetc37511b2013-04-26 15:39:55 -0700153#define MAX_BSETS 4U
154
Kent Overstreetee811282013-12-17 23:49:49 -0800155struct bset_tree {
156 /*
157 * We construct a binary tree in an array as if the array
158 * started at 1, so that things line up on the same cachelines
159 * better: see comments in bset.c at cacheline_to_bkey() for
160 * details
161 */
162
163 /* size of the binary tree and prev array */
164 unsigned size;
165
166 /* function of size - precalculated for to_inorder() */
167 unsigned extra;
168
169 /* copy of the last key in the set */
170 struct bkey end;
171 struct bkey_float *tree;
172
173 /*
174 * The nodes in the bset tree point to specific keys - this
175 * array holds the sizes of the previous key.
176 *
177 * Conceptually it's a member of struct bkey_float, but we want
178 * to keep bkey_float to 4 bytes and prev isn't used in the fast
179 * path.
180 */
181 uint8_t *prev;
182
183 /* The actual btree node, with pointers to each sorted set */
184 struct bset *data;
185};
186
Kent Overstreeta85e9682013-12-20 17:28:16 -0800187struct btree_keys_ops {
188 bool (*sort_cmp)(struct btree_iter_set,
189 struct btree_iter_set);
190 struct bkey *(*sort_fixup)(struct btree_iter *, struct bkey *);
191 bool (*key_invalid)(struct btree_keys *,
192 const struct bkey *);
193 bool (*key_bad)(struct btree_keys *, const struct bkey *);
194 bool (*key_merge)(struct btree_keys *,
195 struct bkey *, struct bkey *);
196
197 /*
198 * Only used for deciding whether to use START_KEY(k) or just the key
199 * itself in a couple places
200 */
201 bool is_extents;
202};
203
204struct btree_keys {
205 const struct btree_keys_ops *ops;
206 uint8_t page_order;
207 uint8_t nsets;
208 unsigned last_set_unwritten:1;
209 bool *expensive_debug_checks;
210
211 /*
212 * Sets of sorted keys - the real btree node - plus a binary search tree
213 *
214 * set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
215 * to the memory we have allocated for this btree node. Additionally,
216 * set[0]->data points to the entire btree node as it exists on disk.
217 */
218 struct bset_tree set[MAX_BSETS];
219};
220
221static inline struct bset_tree *bset_tree_last(struct btree_keys *b)
222{
223 return b->set + b->nsets;
224}
225
226static inline bool bset_written(struct btree_keys *b, struct bset_tree *t)
227{
228 return t <= b->set + b->nsets - b->last_set_unwritten;
229}
230
231static inline bool bkey_written(struct btree_keys *b, struct bkey *k)
232{
233 return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
234}
235
236static inline unsigned bset_byte_offset(struct btree_keys *b, struct bset *i)
237{
238 return ((size_t) i) - ((size_t) b->set->data);
239}
240
241static inline unsigned bset_sector_offset(struct btree_keys *b, struct bset *i)
242{
243 return bset_byte_offset(b, i) >> 9;
244}
245
246static inline bool btree_keys_expensive_checks(struct btree_keys *b)
247{
248#ifdef CONFIG_BCACHE_DEBUG
249 return *b->expensive_debug_checks;
250#else
251 return false;
252#endif
253}
254
Kent Overstreetee811282013-12-17 23:49:49 -0800255#define __set_bytes(i, k) (sizeof(*(i)) + (k) * sizeof(uint64_t))
256#define set_bytes(i) __set_bytes(i, i->keys)
257
258#define __set_blocks(i, k, block_bytes) \
259 DIV_ROUND_UP(__set_bytes(i, k), block_bytes)
260#define set_blocks(i, block_bytes) \
261 __set_blocks(i, (i)->keys, block_bytes)
262
Kent Overstreet59158fd2013-11-11 19:03:54 -0800263static inline size_t bch_btree_keys_u64s_remaining(struct btree_keys *b)
264{
265 struct bset_tree *t = bset_tree_last(b);
266
267 BUG_ON((PAGE_SIZE << b->page_order) <
268 (bset_byte_offset(b, t->data) + set_bytes(t->data)));
269
270 if (!b->last_set_unwritten)
271 return 0;
272
273 return ((PAGE_SIZE << b->page_order) -
274 (bset_byte_offset(b, t->data) + set_bytes(t->data))) /
275 sizeof(u64);
276}
277
Kent Overstreeta85e9682013-12-20 17:28:16 -0800278static inline struct bset *bset_next_set(struct btree_keys *b,
279 unsigned block_bytes)
280{
281 struct bset *i = bset_tree_last(b)->data;
Kent Overstreetee811282013-12-17 23:49:49 -0800282
Kent Overstreeta85e9682013-12-20 17:28:16 -0800283 return ((void *) i) + roundup(set_bytes(i), block_bytes);
284}
285
286void bch_btree_keys_free(struct btree_keys *);
287int bch_btree_keys_alloc(struct btree_keys *, unsigned, gfp_t);
288void bch_btree_keys_init(struct btree_keys *, const struct btree_keys_ops *,
289 bool *);
290
291void bch_bset_init_next(struct btree_keys *, struct bset *, uint64_t);
292void bch_bset_build_written_tree(struct btree_keys *);
293void bch_bset_fix_invalidated_key(struct btree_keys *, struct bkey *);
294void bch_bset_insert(struct btree_keys *, struct bkey *, struct bkey *);
295
296/*
297 * Tries to merge l and r: l should be lower than r
298 * Returns true if we were able to merge. If we did merge, l will be the merged
299 * key, r will be untouched.
300 */
301static inline bool bch_bkey_try_merge(struct btree_keys *b,
302 struct bkey *l, struct bkey *r)
303{
304 return b->ops->key_merge ? b->ops->key_merge(b, l, r) : false;
305}
Kent Overstreetee811282013-12-17 23:49:49 -0800306
307/* Btree key iteration */
308
Kent Overstreetcafe5632013-03-23 16:11:31 -0700309struct btree_iter {
310 size_t size, used;
Kent Overstreet280481d2013-10-24 16:36:03 -0700311#ifdef CONFIG_BCACHE_DEBUG
Kent Overstreetc052dd92013-11-11 17:35:24 -0800312 struct btree_keys *b;
Kent Overstreet280481d2013-10-24 16:36:03 -0700313#endif
Kent Overstreetcafe5632013-03-23 16:11:31 -0700314 struct btree_iter_set {
315 struct bkey *k, *end;
316 } data[MAX_BSETS];
317};
318
Kent Overstreeta85e9682013-12-20 17:28:16 -0800319typedef bool (*ptr_filter_fn)(struct btree_keys *, const struct bkey *);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700320
Kent Overstreetee811282013-12-17 23:49:49 -0800321struct bkey *bch_btree_iter_next(struct btree_iter *);
322struct bkey *bch_btree_iter_next_filter(struct btree_iter *,
Kent Overstreeta85e9682013-12-20 17:28:16 -0800323 struct btree_keys *, ptr_filter_fn);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700324
Kent Overstreetee811282013-12-17 23:49:49 -0800325void bch_btree_iter_push(struct btree_iter *, struct bkey *, struct bkey *);
Kent Overstreetc052dd92013-11-11 17:35:24 -0800326struct bkey *bch_btree_iter_init(struct btree_keys *, struct btree_iter *,
Kent Overstreetee811282013-12-17 23:49:49 -0800327 struct bkey *);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700328
Kent Overstreetc052dd92013-11-11 17:35:24 -0800329struct bkey *__bch_bset_search(struct btree_keys *, struct bset_tree *,
330 const struct bkey *);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700331
Kent Overstreetee811282013-12-17 23:49:49 -0800332/*
333 * Returns the first key that is strictly greater than search
334 */
Kent Overstreetc052dd92013-11-11 17:35:24 -0800335static inline struct bkey *bch_bset_search(struct btree_keys *b,
336 struct bset_tree *t,
Kent Overstreetee811282013-12-17 23:49:49 -0800337 const struct bkey *search)
338{
339 return search ? __bch_bset_search(b, t, search) : t->data->start;
340}
Kent Overstreetcafe5632013-03-23 16:11:31 -0700341
Kent Overstreetc052dd92013-11-11 17:35:24 -0800342#define for_each_key_filter(b, k, iter, filter) \
343 for (bch_btree_iter_init((b), (iter), NULL); \
344 ((k) = bch_btree_iter_next_filter((iter), (b), filter));)
345
346#define for_each_key(b, k, iter) \
347 for (bch_btree_iter_init((b), (iter), NULL); \
348 ((k) = bch_btree_iter_next(iter));)
349
Kent Overstreet67539e82013-09-10 22:53:34 -0700350/* Sorting */
351
352struct bset_sort_state {
353 mempool_t *pool;
354
355 unsigned page_order;
356 unsigned crit_factor;
357
358 struct time_stats time;
359};
360
361void bch_bset_sort_state_free(struct bset_sort_state *);
362int bch_bset_sort_state_init(struct bset_sort_state *, unsigned);
363void bch_btree_sort_lazy(struct btree *, struct bset_sort_state *);
364void bch_btree_sort_into(struct btree *, struct btree *,
365 struct bset_sort_state *);
Kent Overstreeta85e9682013-12-20 17:28:16 -0800366void bch_btree_sort_and_fix_extents(struct btree_keys *, struct btree_iter *,
Kent Overstreet67539e82013-09-10 22:53:34 -0700367 struct bset_sort_state *);
368void bch_btree_sort_partial(struct btree *, unsigned,
369 struct bset_sort_state *);
370
371static inline void bch_btree_sort(struct btree *b,
372 struct bset_sort_state *state)
373{
374 bch_btree_sort_partial(b, 0, state);
375}
376
Kent Overstreetf67342d2013-11-11 19:25:55 -0800377struct bset_stats {
378 size_t sets_written, sets_unwritten;
379 size_t bytes_written, bytes_unwritten;
380 size_t floats, failed;
381};
382
383void bch_btree_keys_stats(struct btree_keys *, struct bset_stats *);
384
Kent Overstreetee811282013-12-17 23:49:49 -0800385/* Bkey utility code */
386
387#define bset_bkey_last(i) bkey_idx((struct bkey *) (i)->d, (i)->keys)
388
389static inline struct bkey *bset_bkey_idx(struct bset *i, unsigned idx)
390{
391 return bkey_idx(i->start, idx);
392}
393
394static inline void bkey_init(struct bkey *k)
395{
396 *k = ZERO_KEY;
397}
398
399static __always_inline int64_t bkey_cmp(const struct bkey *l,
400 const struct bkey *r)
401{
402 return unlikely(KEY_INODE(l) != KEY_INODE(r))
403 ? (int64_t) KEY_INODE(l) - (int64_t) KEY_INODE(r)
404 : (int64_t) KEY_OFFSET(l) - (int64_t) KEY_OFFSET(r);
405}
406
407void bch_bkey_copy_single_ptr(struct bkey *, const struct bkey *,
408 unsigned);
409bool __bch_cut_front(const struct bkey *, struct bkey *);
410bool __bch_cut_back(const struct bkey *, struct bkey *);
411
412static inline bool bch_cut_front(const struct bkey *where, struct bkey *k)
413{
414 BUG_ON(bkey_cmp(where, k) > 0);
415 return __bch_cut_front(where, k);
416}
417
418static inline bool bch_cut_back(const struct bkey *where, struct bkey *k)
419{
420 BUG_ON(bkey_cmp(where, &START_KEY(k)) < 0);
421 return __bch_cut_back(where, k);
422}
423
424#define PRECEDING_KEY(_k) \
425({ \
426 struct bkey *_ret = NULL; \
427 \
428 if (KEY_INODE(_k) || KEY_OFFSET(_k)) { \
429 _ret = &KEY(KEY_INODE(_k), KEY_OFFSET(_k), 0); \
430 \
431 if (!_ret->low) \
432 _ret->high--; \
433 _ret->low--; \
434 } \
435 \
436 _ret; \
437})
438
Kent Overstreeta85e9682013-12-20 17:28:16 -0800439static inline bool bch_ptr_invalid(struct btree_keys *b, const struct bkey *k)
440{
441 return b->ops->key_invalid(b, k);
442}
443
444static inline bool bch_ptr_bad(struct btree_keys *b, const struct bkey *k)
445{
446 return b->ops->key_bad(b, k);
447}
448
Kent Overstreetcafe5632013-03-23 16:11:31 -0700449/* Keylists */
450
451struct keylist {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700452 union {
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700453 struct bkey *keys;
454 uint64_t *keys_p;
455 };
456 union {
457 struct bkey *top;
458 uint64_t *top_p;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700459 };
460
461 /* Enough room for btree_split's keys without realloc */
462#define KEYLIST_INLINE 16
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700463 uint64_t inline_keys[KEYLIST_INLINE];
Kent Overstreetcafe5632013-03-23 16:11:31 -0700464};
465
466static inline void bch_keylist_init(struct keylist *l)
467{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700468 l->top_p = l->keys_p = l->inline_keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700469}
470
471static inline void bch_keylist_push(struct keylist *l)
472{
473 l->top = bkey_next(l->top);
474}
475
476static inline void bch_keylist_add(struct keylist *l, struct bkey *k)
477{
478 bkey_copy(l->top, k);
479 bch_keylist_push(l);
480}
481
482static inline bool bch_keylist_empty(struct keylist *l)
483{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700484 return l->top == l->keys;
485}
486
487static inline void bch_keylist_reset(struct keylist *l)
488{
489 l->top = l->keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700490}
491
492static inline void bch_keylist_free(struct keylist *l)
493{
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700494 if (l->keys_p != l->inline_keys)
495 kfree(l->keys_p);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700496}
497
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700498static inline size_t bch_keylist_nkeys(struct keylist *l)
499{
500 return l->top_p - l->keys_p;
501}
502
503static inline size_t bch_keylist_bytes(struct keylist *l)
504{
505 return bch_keylist_nkeys(l) * sizeof(uint64_t);
506}
507
Kent Overstreetcafe5632013-03-23 16:11:31 -0700508struct bkey *bch_keylist_pop(struct keylist *);
Kent Overstreet26c949f2013-09-10 18:41:15 -0700509void bch_keylist_pop_front(struct keylist *);
Kent Overstreet085d2a32013-11-11 18:20:51 -0800510int __bch_keylist_realloc(struct keylist *, unsigned);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700511
Kent Overstreetee811282013-12-17 23:49:49 -0800512struct cache_set;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700513const char *bch_ptr_status(struct cache_set *, const struct bkey *);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700514
Kent Overstreetcafe5632013-03-23 16:11:31 -0700515#endif