bset.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _BCACHE_BSET_H
  3. #define _BCACHE_BSET_H
  4. #include <linux/bcache.h>
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include "util.h" /* for time_stats */
  8. /*
  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. struct btree_keys;
  147. struct btree_iter;
  148. struct btree_iter_set;
  149. struct bkey_float;
  150. #define MAX_BSETS 4U
  151. struct bset_tree {
  152. /*
  153. * We construct a binary tree in an array as if the array
  154. * started at 1, so that things line up on the same cachelines
  155. * better: see comments in bset.c at cacheline_to_bkey() for
  156. * details
  157. */
  158. /* size of the binary tree and prev array */
  159. unsigned int size;
  160. /* function of size - precalculated for to_inorder() */
  161. unsigned int extra;
  162. /* copy of the last key in the set */
  163. struct bkey end;
  164. struct bkey_float *tree;
  165. /*
  166. * The nodes in the bset tree point to specific keys - this
  167. * array holds the sizes of the previous key.
  168. *
  169. * Conceptually it's a member of struct bkey_float, but we want
  170. * to keep bkey_float to 4 bytes and prev isn't used in the fast
  171. * path.
  172. */
  173. uint8_t *prev;
  174. /* The actual btree node, with pointers to each sorted set */
  175. struct bset *data;
  176. };
  177. struct btree_keys_ops {
  178. bool (*sort_cmp)(struct btree_iter_set l,
  179. struct btree_iter_set r);
  180. struct bkey *(*sort_fixup)(struct btree_iter *iter,
  181. struct bkey *tmp);
  182. bool (*insert_fixup)(struct btree_keys *b,
  183. struct bkey *insert,
  184. struct btree_iter *iter,
  185. struct bkey *replace_key);
  186. bool (*key_invalid)(struct btree_keys *bk,
  187. const struct bkey *k);
  188. bool (*key_bad)(struct btree_keys *bk,
  189. const struct bkey *k);
  190. bool (*key_merge)(struct btree_keys *bk,
  191. struct bkey *l, struct bkey *r);
  192. void (*key_to_text)(char *buf,
  193. size_t size,
  194. const struct bkey *k);
  195. void (*key_dump)(struct btree_keys *keys,
  196. const struct bkey *k);
  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. struct btree_keys {
  204. const struct btree_keys_ops *ops;
  205. uint8_t page_order;
  206. uint8_t nsets;
  207. unsigned int last_set_unwritten:1;
  208. bool *expensive_debug_checks;
  209. /*
  210. * Sets of sorted keys - the real btree node - plus a binary search tree
  211. *
  212. * set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
  213. * to the memory we have allocated for this btree node. Additionally,
  214. * set[0]->data points to the entire btree node as it exists on disk.
  215. */
  216. struct bset_tree set[MAX_BSETS];
  217. };
  218. static inline struct bset_tree *bset_tree_last(struct btree_keys *b)
  219. {
  220. return b->set + b->nsets;
  221. }
  222. static inline bool bset_written(struct btree_keys *b, struct bset_tree *t)
  223. {
  224. return t <= b->set + b->nsets - b->last_set_unwritten;
  225. }
  226. static inline bool bkey_written(struct btree_keys *b, struct bkey *k)
  227. {
  228. return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
  229. }
  230. static inline unsigned int bset_byte_offset(struct btree_keys *b,
  231. struct bset *i)
  232. {
  233. return ((size_t) i) - ((size_t) b->set->data);
  234. }
  235. static inline unsigned int bset_sector_offset(struct btree_keys *b,
  236. struct bset *i)
  237. {
  238. return bset_byte_offset(b, i) >> 9;
  239. }
  240. #define __set_bytes(i, k) (sizeof(*(i)) + (k) * sizeof(uint64_t))
  241. #define set_bytes(i) __set_bytes(i, i->keys)
  242. #define __set_blocks(i, k, block_bytes) \
  243. DIV_ROUND_UP(__set_bytes(i, k), block_bytes)
  244. #define set_blocks(i, block_bytes) \
  245. __set_blocks(i, (i)->keys, block_bytes)
  246. static inline size_t bch_btree_keys_u64s_remaining(struct btree_keys *b)
  247. {
  248. struct bset_tree *t = bset_tree_last(b);
  249. BUG_ON((PAGE_SIZE << b->page_order) <
  250. (bset_byte_offset(b, t->data) + set_bytes(t->data)));
  251. if (!b->last_set_unwritten)
  252. return 0;
  253. return ((PAGE_SIZE << b->page_order) -
  254. (bset_byte_offset(b, t->data) + set_bytes(t->data))) /
  255. sizeof(u64);
  256. }
  257. static inline struct bset *bset_next_set(struct btree_keys *b,
  258. unsigned int block_bytes)
  259. {
  260. struct bset *i = bset_tree_last(b)->data;
  261. return ((void *) i) + roundup(set_bytes(i), block_bytes);
  262. }
  263. void bch_btree_keys_free(struct btree_keys *b);
  264. int bch_btree_keys_alloc(struct btree_keys *b, unsigned int page_order,
  265. gfp_t gfp);
  266. void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
  267. bool *expensive_debug_checks);
  268. void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic);
  269. void bch_bset_build_written_tree(struct btree_keys *b);
  270. void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k);
  271. bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r);
  272. void bch_bset_insert(struct btree_keys *b, struct bkey *where,
  273. struct bkey *insert);
  274. unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
  275. struct bkey *replace_key);
  276. enum {
  277. BTREE_INSERT_STATUS_NO_INSERT = 0,
  278. BTREE_INSERT_STATUS_INSERT,
  279. BTREE_INSERT_STATUS_BACK_MERGE,
  280. BTREE_INSERT_STATUS_OVERWROTE,
  281. BTREE_INSERT_STATUS_FRONT_MERGE,
  282. };
  283. /* Btree key iteration */
  284. struct btree_iter {
  285. size_t size, used;
  286. #ifdef CONFIG_BCACHE_DEBUG
  287. struct btree_keys *b;
  288. #endif
  289. struct btree_iter_set {
  290. struct bkey *k, *end;
  291. } data[MAX_BSETS];
  292. };
  293. typedef bool (*ptr_filter_fn)(struct btree_keys *b, const struct bkey *k);
  294. struct bkey *bch_btree_iter_next(struct btree_iter *iter);
  295. struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
  296. struct btree_keys *b,
  297. ptr_filter_fn fn);
  298. void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
  299. struct bkey *end);
  300. struct bkey *bch_btree_iter_init(struct btree_keys *b,
  301. struct btree_iter *iter,
  302. struct bkey *search);
  303. struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
  304. const struct bkey *search);
  305. /*
  306. * Returns the first key that is strictly greater than search
  307. */
  308. static inline struct bkey *bch_bset_search(struct btree_keys *b,
  309. struct bset_tree *t,
  310. const struct bkey *search)
  311. {
  312. return search ? __bch_bset_search(b, t, search) : t->data->start;
  313. }
  314. #define for_each_key_filter(b, k, iter, filter) \
  315. for (bch_btree_iter_init((b), (iter), NULL); \
  316. ((k) = bch_btree_iter_next_filter((iter), (b), filter));)
  317. #define for_each_key(b, k, iter) \
  318. for (bch_btree_iter_init((b), (iter), NULL); \
  319. ((k) = bch_btree_iter_next(iter));)
  320. /* Sorting */
  321. struct bset_sort_state {
  322. mempool_t pool;
  323. unsigned int page_order;
  324. unsigned int crit_factor;
  325. struct time_stats time;
  326. };
  327. void bch_bset_sort_state_free(struct bset_sort_state *state);
  328. int bch_bset_sort_state_init(struct bset_sort_state *state,
  329. unsigned int page_order);
  330. void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state);
  331. void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
  332. struct bset_sort_state *state);
  333. void bch_btree_sort_and_fix_extents(struct btree_keys *b,
  334. struct btree_iter *iter,
  335. struct bset_sort_state *state);
  336. void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,
  337. struct bset_sort_state *state);
  338. static inline void bch_btree_sort(struct btree_keys *b,
  339. struct bset_sort_state *state)
  340. {
  341. bch_btree_sort_partial(b, 0, state);
  342. }
  343. struct bset_stats {
  344. size_t sets_written, sets_unwritten;
  345. size_t bytes_written, bytes_unwritten;
  346. size_t floats, failed;
  347. };
  348. void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *state);
  349. /* Bkey utility code */
  350. #define bset_bkey_last(i) bkey_idx((struct bkey *) (i)->d, \
  351. (unsigned int)(i)->keys)
  352. static inline struct bkey *bset_bkey_idx(struct bset *i, unsigned int idx)
  353. {
  354. return bkey_idx(i->start, idx);
  355. }
  356. static inline void bkey_init(struct bkey *k)
  357. {
  358. *k = ZERO_KEY;
  359. }
  360. static __always_inline int64_t bkey_cmp(const struct bkey *l,
  361. const struct bkey *r)
  362. {
  363. return unlikely(KEY_INODE(l) != KEY_INODE(r))
  364. ? (int64_t) KEY_INODE(l) - (int64_t) KEY_INODE(r)
  365. : (int64_t) KEY_OFFSET(l) - (int64_t) KEY_OFFSET(r);
  366. }
  367. void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
  368. unsigned int i);
  369. bool __bch_cut_front(const struct bkey *where, struct bkey *k);
  370. bool __bch_cut_back(const struct bkey *where, struct bkey *k);
  371. static inline bool bch_cut_front(const struct bkey *where, struct bkey *k)
  372. {
  373. BUG_ON(bkey_cmp(where, k) > 0);
  374. return __bch_cut_front(where, k);
  375. }
  376. static inline bool bch_cut_back(const struct bkey *where, struct bkey *k)
  377. {
  378. BUG_ON(bkey_cmp(where, &START_KEY(k)) < 0);
  379. return __bch_cut_back(where, k);
  380. }
  381. /*
  382. * Pointer '*preceding_key_p' points to a memory object to store preceding
  383. * key of k. If the preceding key does not exist, set '*preceding_key_p' to
  384. * NULL. So the caller of preceding_key() needs to take care of memory
  385. * which '*preceding_key_p' pointed to before calling preceding_key().
  386. * Currently the only caller of preceding_key() is bch_btree_insert_key(),
  387. * and it points to an on-stack variable, so the memory release is handled
  388. * by stackframe itself.
  389. */
  390. static inline void preceding_key(struct bkey *k, struct bkey **preceding_key_p)
  391. {
  392. if (KEY_INODE(k) || KEY_OFFSET(k)) {
  393. (**preceding_key_p) = KEY(KEY_INODE(k), KEY_OFFSET(k), 0);
  394. if (!(*preceding_key_p)->low)
  395. (*preceding_key_p)->high--;
  396. (*preceding_key_p)->low--;
  397. } else {
  398. (*preceding_key_p) = NULL;
  399. }
  400. }
  401. static inline bool bch_ptr_invalid(struct btree_keys *b, const struct bkey *k)
  402. {
  403. return b->ops->key_invalid(b, k);
  404. }
  405. static inline bool bch_ptr_bad(struct btree_keys *b, const struct bkey *k)
  406. {
  407. return b->ops->key_bad(b, k);
  408. }
  409. static inline void bch_bkey_to_text(struct btree_keys *b, char *buf,
  410. size_t size, const struct bkey *k)
  411. {
  412. return b->ops->key_to_text(buf, size, k);
  413. }
  414. static inline bool bch_bkey_equal_header(const struct bkey *l,
  415. const struct bkey *r)
  416. {
  417. return (KEY_DIRTY(l) == KEY_DIRTY(r) &&
  418. KEY_PTRS(l) == KEY_PTRS(r) &&
  419. KEY_CSUM(l) == KEY_CSUM(r));
  420. }
  421. /* Keylists */
  422. struct keylist {
  423. union {
  424. struct bkey *keys;
  425. uint64_t *keys_p;
  426. };
  427. union {
  428. struct bkey *top;
  429. uint64_t *top_p;
  430. };
  431. /* Enough room for btree_split's keys without realloc */
  432. #define KEYLIST_INLINE 16
  433. uint64_t inline_keys[KEYLIST_INLINE];
  434. };
  435. static inline void bch_keylist_init(struct keylist *l)
  436. {
  437. l->top_p = l->keys_p = l->inline_keys;
  438. }
  439. static inline void bch_keylist_init_single(struct keylist *l, struct bkey *k)
  440. {
  441. l->keys = k;
  442. l->top = bkey_next(k);
  443. }
  444. static inline void bch_keylist_push(struct keylist *l)
  445. {
  446. l->top = bkey_next(l->top);
  447. }
  448. static inline void bch_keylist_add(struct keylist *l, struct bkey *k)
  449. {
  450. bkey_copy(l->top, k);
  451. bch_keylist_push(l);
  452. }
  453. static inline bool bch_keylist_empty(struct keylist *l)
  454. {
  455. return l->top == l->keys;
  456. }
  457. static inline void bch_keylist_reset(struct keylist *l)
  458. {
  459. l->top = l->keys;
  460. }
  461. static inline void bch_keylist_free(struct keylist *l)
  462. {
  463. if (l->keys_p != l->inline_keys)
  464. kfree(l->keys_p);
  465. }
  466. static inline size_t bch_keylist_nkeys(struct keylist *l)
  467. {
  468. return l->top_p - l->keys_p;
  469. }
  470. static inline size_t bch_keylist_bytes(struct keylist *l)
  471. {
  472. return bch_keylist_nkeys(l) * sizeof(uint64_t);
  473. }
  474. struct bkey *bch_keylist_pop(struct keylist *l);
  475. void bch_keylist_pop_front(struct keylist *l);
  476. int __bch_keylist_realloc(struct keylist *l, unsigned int u64s);
  477. /* Debug stuff */
  478. #ifdef CONFIG_BCACHE_DEBUG
  479. int __bch_count_data(struct btree_keys *b);
  480. void __printf(2, 3) __bch_check_keys(struct btree_keys *b,
  481. const char *fmt,
  482. ...);
  483. void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set);
  484. void bch_dump_bucket(struct btree_keys *b);
  485. #else
  486. static inline int __bch_count_data(struct btree_keys *b) { return -1; }
  487. static inline void __printf(2, 3)
  488. __bch_check_keys(struct btree_keys *b, const char *fmt, ...) {}
  489. static inline void bch_dump_bucket(struct btree_keys *b) {}
  490. void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set);
  491. #endif
  492. static inline bool btree_keys_expensive_checks(struct btree_keys *b)
  493. {
  494. #ifdef CONFIG_BCACHE_DEBUG
  495. return *b->expensive_debug_checks;
  496. #else
  497. return false;
  498. #endif
  499. }
  500. static inline int bch_count_data(struct btree_keys *b)
  501. {
  502. return btree_keys_expensive_checks(b) ? __bch_count_data(b) : -1;
  503. }
  504. #define bch_check_keys(b, ...) \
  505. do { \
  506. if (btree_keys_expensive_checks(b)) \
  507. __bch_check_keys(b, __VA_ARGS__); \
  508. } while (0)
  509. #endif