bset.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Code for working with individual keys, and sorted sets of keys with in a
  4. * btree node
  5. *
  6. * Copyright 2012 Google, Inc.
  7. */
  8. #define pr_fmt(fmt) "bcache: %s() " fmt "\n", __func__
  9. #include "util.h"
  10. #include "bset.h"
  11. #include <linux/console.h>
  12. #include <linux/sched/clock.h>
  13. #include <linux/random.h>
  14. #include <linux/prefetch.h>
  15. #ifdef CONFIG_BCACHE_DEBUG
  16. void bch_dump_bset(struct btree_keys *b, struct bset *i, unsigned int set)
  17. {
  18. struct bkey *k, *next;
  19. for (k = i->start; k < bset_bkey_last(i); k = next) {
  20. next = bkey_next(k);
  21. pr_err("block %u key %u/%u: ", set,
  22. (unsigned int) ((u64 *) k - i->d), i->keys);
  23. if (b->ops->key_dump)
  24. b->ops->key_dump(b, k);
  25. else
  26. pr_err("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
  27. if (next < bset_bkey_last(i) &&
  28. bkey_cmp(k, b->ops->is_extents ?
  29. &START_KEY(next) : next) > 0)
  30. pr_err("Key skipped backwards\n");
  31. }
  32. }
  33. void bch_dump_bucket(struct btree_keys *b)
  34. {
  35. unsigned int i;
  36. console_lock();
  37. for (i = 0; i <= b->nsets; i++)
  38. bch_dump_bset(b, b->set[i].data,
  39. bset_sector_offset(b, b->set[i].data));
  40. console_unlock();
  41. }
  42. int __bch_count_data(struct btree_keys *b)
  43. {
  44. unsigned int ret = 0;
  45. struct btree_iter iter;
  46. struct bkey *k;
  47. if (b->ops->is_extents)
  48. for_each_key(b, k, &iter)
  49. ret += KEY_SIZE(k);
  50. return ret;
  51. }
  52. void __bch_check_keys(struct btree_keys *b, const char *fmt, ...)
  53. {
  54. va_list args;
  55. struct bkey *k, *p = NULL;
  56. struct btree_iter iter;
  57. const char *err;
  58. for_each_key(b, k, &iter) {
  59. if (b->ops->is_extents) {
  60. err = "Keys out of order";
  61. if (p && bkey_cmp(&START_KEY(p), &START_KEY(k)) > 0)
  62. goto bug;
  63. if (bch_ptr_invalid(b, k))
  64. continue;
  65. err = "Overlapping keys";
  66. if (p && bkey_cmp(p, &START_KEY(k)) > 0)
  67. goto bug;
  68. } else {
  69. if (bch_ptr_bad(b, k))
  70. continue;
  71. err = "Duplicate keys";
  72. if (p && !bkey_cmp(p, k))
  73. goto bug;
  74. }
  75. p = k;
  76. }
  77. #if 0
  78. err = "Key larger than btree node key";
  79. if (p && bkey_cmp(p, &b->key) > 0)
  80. goto bug;
  81. #endif
  82. return;
  83. bug:
  84. bch_dump_bucket(b);
  85. va_start(args, fmt);
  86. vprintk(fmt, args);
  87. va_end(args);
  88. panic("bch_check_keys error: %s:\n", err);
  89. }
  90. static void bch_btree_iter_next_check(struct btree_iter *iter)
  91. {
  92. struct bkey *k = iter->data->k, *next = bkey_next(k);
  93. if (next < iter->data->end &&
  94. bkey_cmp(k, iter->b->ops->is_extents ?
  95. &START_KEY(next) : next) > 0) {
  96. bch_dump_bucket(iter->b);
  97. panic("Key skipped backwards\n");
  98. }
  99. }
  100. #else
  101. static inline void bch_btree_iter_next_check(struct btree_iter *iter) {}
  102. #endif
  103. /* Keylists */
  104. int __bch_keylist_realloc(struct keylist *l, unsigned int u64s)
  105. {
  106. size_t oldsize = bch_keylist_nkeys(l);
  107. size_t newsize = oldsize + u64s;
  108. uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
  109. uint64_t *new_keys;
  110. newsize = roundup_pow_of_two(newsize);
  111. if (newsize <= KEYLIST_INLINE ||
  112. roundup_pow_of_two(oldsize) == newsize)
  113. return 0;
  114. new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
  115. if (!new_keys)
  116. return -ENOMEM;
  117. if (!old_keys)
  118. memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
  119. l->keys_p = new_keys;
  120. l->top_p = new_keys + oldsize;
  121. return 0;
  122. }
  123. struct bkey *bch_keylist_pop(struct keylist *l)
  124. {
  125. struct bkey *k = l->keys;
  126. if (k == l->top)
  127. return NULL;
  128. while (bkey_next(k) != l->top)
  129. k = bkey_next(k);
  130. return l->top = k;
  131. }
  132. void bch_keylist_pop_front(struct keylist *l)
  133. {
  134. l->top_p -= bkey_u64s(l->keys);
  135. memmove(l->keys,
  136. bkey_next(l->keys),
  137. bch_keylist_bytes(l));
  138. }
  139. /* Key/pointer manipulation */
  140. void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
  141. unsigned int i)
  142. {
  143. BUG_ON(i > KEY_PTRS(src));
  144. /* Only copy the header, key, and one pointer. */
  145. memcpy(dest, src, 2 * sizeof(uint64_t));
  146. dest->ptr[0] = src->ptr[i];
  147. SET_KEY_PTRS(dest, 1);
  148. /* We didn't copy the checksum so clear that bit. */
  149. SET_KEY_CSUM(dest, 0);
  150. }
  151. bool __bch_cut_front(const struct bkey *where, struct bkey *k)
  152. {
  153. unsigned int i, len = 0;
  154. if (bkey_cmp(where, &START_KEY(k)) <= 0)
  155. return false;
  156. if (bkey_cmp(where, k) < 0)
  157. len = KEY_OFFSET(k) - KEY_OFFSET(where);
  158. else
  159. bkey_copy_key(k, where);
  160. for (i = 0; i < KEY_PTRS(k); i++)
  161. SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
  162. BUG_ON(len > KEY_SIZE(k));
  163. SET_KEY_SIZE(k, len);
  164. return true;
  165. }
  166. bool __bch_cut_back(const struct bkey *where, struct bkey *k)
  167. {
  168. unsigned int len = 0;
  169. if (bkey_cmp(where, k) >= 0)
  170. return false;
  171. BUG_ON(KEY_INODE(where) != KEY_INODE(k));
  172. if (bkey_cmp(where, &START_KEY(k)) > 0)
  173. len = KEY_OFFSET(where) - KEY_START(k);
  174. bkey_copy_key(k, where);
  175. BUG_ON(len > KEY_SIZE(k));
  176. SET_KEY_SIZE(k, len);
  177. return true;
  178. }
  179. /* Auxiliary search trees */
  180. /* 32 bits total: */
  181. #define BKEY_MID_BITS 3
  182. #define BKEY_EXPONENT_BITS 7
  183. #define BKEY_MANTISSA_BITS (32 - BKEY_MID_BITS - BKEY_EXPONENT_BITS)
  184. #define BKEY_MANTISSA_MASK ((1 << BKEY_MANTISSA_BITS) - 1)
  185. struct bkey_float {
  186. unsigned int exponent:BKEY_EXPONENT_BITS;
  187. unsigned int m:BKEY_MID_BITS;
  188. unsigned int mantissa:BKEY_MANTISSA_BITS;
  189. } __packed;
  190. /*
  191. * BSET_CACHELINE was originally intended to match the hardware cacheline size -
  192. * it used to be 64, but I realized the lookup code would touch slightly less
  193. * memory if it was 128.
  194. *
  195. * It definites the number of bytes (in struct bset) per struct bkey_float in
  196. * the auxiliar search tree - when we're done searching the bset_float tree we
  197. * have this many bytes left that we do a linear search over.
  198. *
  199. * Since (after level 5) every level of the bset_tree is on a new cacheline,
  200. * we're touching one fewer cacheline in the bset tree in exchange for one more
  201. * cacheline in the linear search - but the linear search might stop before it
  202. * gets to the second cacheline.
  203. */
  204. #define BSET_CACHELINE 128
  205. /* Space required for the btree node keys */
  206. static inline size_t btree_keys_bytes(struct btree_keys *b)
  207. {
  208. return PAGE_SIZE << b->page_order;
  209. }
  210. static inline size_t btree_keys_cachelines(struct btree_keys *b)
  211. {
  212. return btree_keys_bytes(b) / BSET_CACHELINE;
  213. }
  214. /* Space required for the auxiliary search trees */
  215. static inline size_t bset_tree_bytes(struct btree_keys *b)
  216. {
  217. return btree_keys_cachelines(b) * sizeof(struct bkey_float);
  218. }
  219. /* Space required for the prev pointers */
  220. static inline size_t bset_prev_bytes(struct btree_keys *b)
  221. {
  222. return btree_keys_cachelines(b) * sizeof(uint8_t);
  223. }
  224. /* Memory allocation */
  225. void bch_btree_keys_free(struct btree_keys *b)
  226. {
  227. struct bset_tree *t = b->set;
  228. if (bset_prev_bytes(b) < PAGE_SIZE)
  229. kfree(t->prev);
  230. else
  231. free_pages((unsigned long) t->prev,
  232. get_order(bset_prev_bytes(b)));
  233. if (bset_tree_bytes(b) < PAGE_SIZE)
  234. kfree(t->tree);
  235. else
  236. free_pages((unsigned long) t->tree,
  237. get_order(bset_tree_bytes(b)));
  238. free_pages((unsigned long) t->data, b->page_order);
  239. t->prev = NULL;
  240. t->tree = NULL;
  241. t->data = NULL;
  242. }
  243. EXPORT_SYMBOL(bch_btree_keys_free);
  244. int bch_btree_keys_alloc(struct btree_keys *b,
  245. unsigned int page_order,
  246. gfp_t gfp)
  247. {
  248. struct bset_tree *t = b->set;
  249. BUG_ON(t->data);
  250. b->page_order = page_order;
  251. t->data = (void *) __get_free_pages(__GFP_COMP|gfp, b->page_order);
  252. if (!t->data)
  253. goto err;
  254. t->tree = bset_tree_bytes(b) < PAGE_SIZE
  255. ? kmalloc(bset_tree_bytes(b), gfp)
  256. : (void *) __get_free_pages(gfp, get_order(bset_tree_bytes(b)));
  257. if (!t->tree)
  258. goto err;
  259. t->prev = bset_prev_bytes(b) < PAGE_SIZE
  260. ? kmalloc(bset_prev_bytes(b), gfp)
  261. : (void *) __get_free_pages(gfp, get_order(bset_prev_bytes(b)));
  262. if (!t->prev)
  263. goto err;
  264. return 0;
  265. err:
  266. bch_btree_keys_free(b);
  267. return -ENOMEM;
  268. }
  269. EXPORT_SYMBOL(bch_btree_keys_alloc);
  270. void bch_btree_keys_init(struct btree_keys *b, const struct btree_keys_ops *ops,
  271. bool *expensive_debug_checks)
  272. {
  273. b->ops = ops;
  274. b->expensive_debug_checks = expensive_debug_checks;
  275. b->nsets = 0;
  276. b->last_set_unwritten = 0;
  277. /*
  278. * struct btree_keys in embedded in struct btree, and struct
  279. * bset_tree is embedded into struct btree_keys. They are all
  280. * initialized as 0 by kzalloc() in mca_bucket_alloc(), and
  281. * b->set[0].data is allocated in bch_btree_keys_alloc(), so we
  282. * don't have to initiate b->set[].size and b->set[].data here
  283. * any more.
  284. */
  285. }
  286. EXPORT_SYMBOL(bch_btree_keys_init);
  287. /* Binary tree stuff for auxiliary search trees */
  288. /*
  289. * return array index next to j when does in-order traverse
  290. * of a binary tree which is stored in a linear array
  291. */
  292. static unsigned int inorder_next(unsigned int j, unsigned int size)
  293. {
  294. if (j * 2 + 1 < size) {
  295. j = j * 2 + 1;
  296. while (j * 2 < size)
  297. j *= 2;
  298. } else
  299. j >>= ffz(j) + 1;
  300. return j;
  301. }
  302. /*
  303. * return array index previous to j when does in-order traverse
  304. * of a binary tree which is stored in a linear array
  305. */
  306. static unsigned int inorder_prev(unsigned int j, unsigned int size)
  307. {
  308. if (j * 2 < size) {
  309. j = j * 2;
  310. while (j * 2 + 1 < size)
  311. j = j * 2 + 1;
  312. } else
  313. j >>= ffs(j);
  314. return j;
  315. }
  316. /*
  317. * I have no idea why this code works... and I'm the one who wrote it
  318. *
  319. * However, I do know what it does:
  320. * Given a binary tree constructed in an array (i.e. how you normally implement
  321. * a heap), it converts a node in the tree - referenced by array index - to the
  322. * index it would have if you did an inorder traversal.
  323. *
  324. * Also tested for every j, size up to size somewhere around 6 million.
  325. *
  326. * The binary tree starts at array index 1, not 0
  327. * extra is a function of size:
  328. * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
  329. */
  330. static unsigned int __to_inorder(unsigned int j,
  331. unsigned int size,
  332. unsigned int extra)
  333. {
  334. unsigned int b = fls(j);
  335. unsigned int shift = fls(size - 1) - b;
  336. j ^= 1U << (b - 1);
  337. j <<= 1;
  338. j |= 1;
  339. j <<= shift;
  340. if (j > extra)
  341. j -= (j - extra) >> 1;
  342. return j;
  343. }
  344. /*
  345. * Return the cacheline index in bset_tree->data, where j is index
  346. * from a linear array which stores the auxiliar binary tree
  347. */
  348. static unsigned int to_inorder(unsigned int j, struct bset_tree *t)
  349. {
  350. return __to_inorder(j, t->size, t->extra);
  351. }
  352. static unsigned int __inorder_to_tree(unsigned int j,
  353. unsigned int size,
  354. unsigned int extra)
  355. {
  356. unsigned int shift;
  357. if (j > extra)
  358. j += j - extra;
  359. shift = ffs(j);
  360. j >>= shift;
  361. j |= roundup_pow_of_two(size) >> shift;
  362. return j;
  363. }
  364. /*
  365. * Return an index from a linear array which stores the auxiliar binary
  366. * tree, j is the cacheline index of t->data.
  367. */
  368. static unsigned int inorder_to_tree(unsigned int j, struct bset_tree *t)
  369. {
  370. return __inorder_to_tree(j, t->size, t->extra);
  371. }
  372. #if 0
  373. void inorder_test(void)
  374. {
  375. unsigned long done = 0;
  376. ktime_t start = ktime_get();
  377. for (unsigned int size = 2;
  378. size < 65536000;
  379. size++) {
  380. unsigned int extra =
  381. (size - rounddown_pow_of_two(size - 1)) << 1;
  382. unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
  383. if (!(size % 4096))
  384. pr_notice("loop %u, %llu per us\n", size,
  385. done / ktime_us_delta(ktime_get(), start));
  386. while (1) {
  387. if (__inorder_to_tree(i, size, extra) != j)
  388. panic("size %10u j %10u i %10u", size, j, i);
  389. if (__to_inorder(j, size, extra) != i)
  390. panic("size %10u j %10u i %10u", size, j, i);
  391. if (j == rounddown_pow_of_two(size) - 1)
  392. break;
  393. BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
  394. j = inorder_next(j, size);
  395. i++;
  396. }
  397. done += size - 1;
  398. }
  399. }
  400. #endif
  401. /*
  402. * Cacheline/offset <-> bkey pointer arithmetic:
  403. *
  404. * t->tree is a binary search tree in an array; each node corresponds to a key
  405. * in one cacheline in t->set (BSET_CACHELINE bytes).
  406. *
  407. * This means we don't have to store the full index of the key that a node in
  408. * the binary tree points to; to_inorder() gives us the cacheline, and then
  409. * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
  410. *
  411. * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
  412. * make this work.
  413. *
  414. * To construct the bfloat for an arbitrary key we need to know what the key
  415. * immediately preceding it is: we have to check if the two keys differ in the
  416. * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
  417. * of the previous key so we can walk backwards to it from t->tree[j]'s key.
  418. */
  419. static struct bkey *cacheline_to_bkey(struct bset_tree *t,
  420. unsigned int cacheline,
  421. unsigned int offset)
  422. {
  423. return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
  424. }
  425. static unsigned int bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
  426. {
  427. return ((void *) k - (void *) t->data) / BSET_CACHELINE;
  428. }
  429. static unsigned int bkey_to_cacheline_offset(struct bset_tree *t,
  430. unsigned int cacheline,
  431. struct bkey *k)
  432. {
  433. return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0);
  434. }
  435. static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned int j)
  436. {
  437. return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
  438. }
  439. static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned int j)
  440. {
  441. return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
  442. }
  443. /*
  444. * For the write set - the one we're currently inserting keys into - we don't
  445. * maintain a full search tree, we just keep a simple lookup table in t->prev.
  446. */
  447. static struct bkey *table_to_bkey(struct bset_tree *t, unsigned int cacheline)
  448. {
  449. return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
  450. }
  451. static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
  452. {
  453. low >>= shift;
  454. low |= (high << 1) << (63U - shift);
  455. return low;
  456. }
  457. /*
  458. * Calculate mantissa value for struct bkey_float.
  459. * If most significant bit of f->exponent is not set, then
  460. * - f->exponent >> 6 is 0
  461. * - p[0] points to bkey->low
  462. * - p[-1] borrows bits from KEY_INODE() of bkey->high
  463. * if most isgnificant bits of f->exponent is set, then
  464. * - f->exponent >> 6 is 1
  465. * - p[0] points to bits from KEY_INODE() of bkey->high
  466. * - p[-1] points to other bits from KEY_INODE() of
  467. * bkey->high too.
  468. * See make_bfloat() to check when most significant bit of f->exponent
  469. * is set or not.
  470. */
  471. static inline unsigned int bfloat_mantissa(const struct bkey *k,
  472. struct bkey_float *f)
  473. {
  474. const uint64_t *p = &k->low - (f->exponent >> 6);
  475. return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
  476. }
  477. static void make_bfloat(struct bset_tree *t, unsigned int j)
  478. {
  479. struct bkey_float *f = &t->tree[j];
  480. struct bkey *m = tree_to_bkey(t, j);
  481. struct bkey *p = tree_to_prev_bkey(t, j);
  482. struct bkey *l = is_power_of_2(j)
  483. ? t->data->start
  484. : tree_to_prev_bkey(t, j >> ffs(j));
  485. struct bkey *r = is_power_of_2(j + 1)
  486. ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
  487. : tree_to_bkey(t, j >> (ffz(j) + 1));
  488. BUG_ON(m < l || m > r);
  489. BUG_ON(bkey_next(p) != m);
  490. /*
  491. * If l and r have different KEY_INODE values (different backing
  492. * device), f->exponent records how many least significant bits
  493. * are different in KEY_INODE values and sets most significant
  494. * bits to 1 (by +64).
  495. * If l and r have same KEY_INODE value, f->exponent records
  496. * how many different bits in least significant bits of bkey->low.
  497. * See bfloat_mantiss() how the most significant bit of
  498. * f->exponent is used to calculate bfloat mantissa value.
  499. */
  500. if (KEY_INODE(l) != KEY_INODE(r))
  501. f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
  502. else
  503. f->exponent = fls64(r->low ^ l->low);
  504. f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
  505. /*
  506. * Setting f->exponent = 127 flags this node as failed, and causes the
  507. * lookup code to fall back to comparing against the original key.
  508. */
  509. if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
  510. f->mantissa = bfloat_mantissa(m, f) - 1;
  511. else
  512. f->exponent = 127;
  513. }
  514. static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t)
  515. {
  516. if (t != b->set) {
  517. unsigned int j = roundup(t[-1].size,
  518. 64 / sizeof(struct bkey_float));
  519. t->tree = t[-1].tree + j;
  520. t->prev = t[-1].prev + j;
  521. }
  522. while (t < b->set + MAX_BSETS)
  523. t++->size = 0;
  524. }
  525. static void bch_bset_build_unwritten_tree(struct btree_keys *b)
  526. {
  527. struct bset_tree *t = bset_tree_last(b);
  528. BUG_ON(b->last_set_unwritten);
  529. b->last_set_unwritten = 1;
  530. bset_alloc_tree(b, t);
  531. if (t->tree != b->set->tree + btree_keys_cachelines(b)) {
  532. t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start);
  533. t->size = 1;
  534. }
  535. }
  536. void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic)
  537. {
  538. if (i != b->set->data) {
  539. b->set[++b->nsets].data = i;
  540. i->seq = b->set->data->seq;
  541. } else
  542. get_random_bytes(&i->seq, sizeof(uint64_t));
  543. i->magic = magic;
  544. i->version = 0;
  545. i->keys = 0;
  546. bch_bset_build_unwritten_tree(b);
  547. }
  548. EXPORT_SYMBOL(bch_bset_init_next);
  549. /*
  550. * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to
  551. * accelerate bkey search in a btree node (pointed by bset_tree->data in
  552. * memory). After search in the auxiliar tree by calling bset_search_tree(),
  553. * a struct bset_search_iter is returned which indicates range [l, r] from
  554. * bset_tree->data where the searching bkey might be inside. Then a followed
  555. * linear comparison does the exact search, see __bch_bset_search() for how
  556. * the auxiliary tree is used.
  557. */
  558. void bch_bset_build_written_tree(struct btree_keys *b)
  559. {
  560. struct bset_tree *t = bset_tree_last(b);
  561. struct bkey *prev = NULL, *k = t->data->start;
  562. unsigned int j, cacheline = 1;
  563. b->last_set_unwritten = 0;
  564. bset_alloc_tree(b, t);
  565. t->size = min_t(unsigned int,
  566. bkey_to_cacheline(t, bset_bkey_last(t->data)),
  567. b->set->tree + btree_keys_cachelines(b) - t->tree);
  568. if (t->size < 2) {
  569. t->size = 0;
  570. return;
  571. }
  572. t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
  573. /* First we figure out where the first key in each cacheline is */
  574. for (j = inorder_next(0, t->size);
  575. j;
  576. j = inorder_next(j, t->size)) {
  577. while (bkey_to_cacheline(t, k) < cacheline)
  578. prev = k, k = bkey_next(k);
  579. t->prev[j] = bkey_u64s(prev);
  580. t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k);
  581. }
  582. while (bkey_next(k) != bset_bkey_last(t->data))
  583. k = bkey_next(k);
  584. t->end = *k;
  585. /* Then we build the tree */
  586. for (j = inorder_next(0, t->size);
  587. j;
  588. j = inorder_next(j, t->size))
  589. make_bfloat(t, j);
  590. }
  591. EXPORT_SYMBOL(bch_bset_build_written_tree);
  592. /* Insert */
  593. void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k)
  594. {
  595. struct bset_tree *t;
  596. unsigned int inorder, j = 1;
  597. for (t = b->set; t <= bset_tree_last(b); t++)
  598. if (k < bset_bkey_last(t->data))
  599. goto found_set;
  600. BUG();
  601. found_set:
  602. if (!t->size || !bset_written(b, t))
  603. return;
  604. inorder = bkey_to_cacheline(t, k);
  605. if (k == t->data->start)
  606. goto fix_left;
  607. if (bkey_next(k) == bset_bkey_last(t->data)) {
  608. t->end = *k;
  609. goto fix_right;
  610. }
  611. j = inorder_to_tree(inorder, t);
  612. if (j &&
  613. j < t->size &&
  614. k == tree_to_bkey(t, j))
  615. fix_left: do {
  616. make_bfloat(t, j);
  617. j = j * 2;
  618. } while (j < t->size);
  619. j = inorder_to_tree(inorder + 1, t);
  620. if (j &&
  621. j < t->size &&
  622. k == tree_to_prev_bkey(t, j))
  623. fix_right: do {
  624. make_bfloat(t, j);
  625. j = j * 2 + 1;
  626. } while (j < t->size);
  627. }
  628. EXPORT_SYMBOL(bch_bset_fix_invalidated_key);
  629. static void bch_bset_fix_lookup_table(struct btree_keys *b,
  630. struct bset_tree *t,
  631. struct bkey *k)
  632. {
  633. unsigned int shift = bkey_u64s(k);
  634. unsigned int j = bkey_to_cacheline(t, k);
  635. /* We're getting called from btree_split() or btree_gc, just bail out */
  636. if (!t->size)
  637. return;
  638. /*
  639. * k is the key we just inserted; we need to find the entry in the
  640. * lookup table for the first key that is strictly greater than k:
  641. * it's either k's cacheline or the next one
  642. */
  643. while (j < t->size &&
  644. table_to_bkey(t, j) <= k)
  645. j++;
  646. /*
  647. * Adjust all the lookup table entries, and find a new key for any that
  648. * have gotten too big
  649. */
  650. for (; j < t->size; j++) {
  651. t->prev[j] += shift;
  652. if (t->prev[j] > 7) {
  653. k = table_to_bkey(t, j - 1);
  654. while (k < cacheline_to_bkey(t, j, 0))
  655. k = bkey_next(k);
  656. t->prev[j] = bkey_to_cacheline_offset(t, j, k);
  657. }
  658. }
  659. if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree)
  660. return;
  661. /* Possibly add a new entry to the end of the lookup table */
  662. for (k = table_to_bkey(t, t->size - 1);
  663. k != bset_bkey_last(t->data);
  664. k = bkey_next(k))
  665. if (t->size == bkey_to_cacheline(t, k)) {
  666. t->prev[t->size] =
  667. bkey_to_cacheline_offset(t, t->size, k);
  668. t->size++;
  669. }
  670. }
  671. /*
  672. * Tries to merge l and r: l should be lower than r
  673. * Returns true if we were able to merge. If we did merge, l will be the merged
  674. * key, r will be untouched.
  675. */
  676. bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r)
  677. {
  678. if (!b->ops->key_merge)
  679. return false;
  680. /*
  681. * Generic header checks
  682. * Assumes left and right are in order
  683. * Left and right must be exactly aligned
  684. */
  685. if (!bch_bkey_equal_header(l, r) ||
  686. bkey_cmp(l, &START_KEY(r)))
  687. return false;
  688. return b->ops->key_merge(b, l, r);
  689. }
  690. EXPORT_SYMBOL(bch_bkey_try_merge);
  691. void bch_bset_insert(struct btree_keys *b, struct bkey *where,
  692. struct bkey *insert)
  693. {
  694. struct bset_tree *t = bset_tree_last(b);
  695. BUG_ON(!b->last_set_unwritten);
  696. BUG_ON(bset_byte_offset(b, t->data) +
  697. __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
  698. PAGE_SIZE << b->page_order);
  699. memmove((uint64_t *) where + bkey_u64s(insert),
  700. where,
  701. (void *) bset_bkey_last(t->data) - (void *) where);
  702. t->data->keys += bkey_u64s(insert);
  703. bkey_copy(where, insert);
  704. bch_bset_fix_lookup_table(b, t, where);
  705. }
  706. EXPORT_SYMBOL(bch_bset_insert);
  707. unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
  708. struct bkey *replace_key)
  709. {
  710. unsigned int status = BTREE_INSERT_STATUS_NO_INSERT;
  711. struct bset *i = bset_tree_last(b)->data;
  712. struct bkey *m, *prev = NULL;
  713. struct btree_iter iter;
  714. struct bkey preceding_key_on_stack = ZERO_KEY;
  715. struct bkey *preceding_key_p = &preceding_key_on_stack;
  716. BUG_ON(b->ops->is_extents && !KEY_SIZE(k));
  717. /*
  718. * If k has preceding key, preceding_key_p will be set to address
  719. * of k's preceding key; otherwise preceding_key_p will be set
  720. * to NULL inside preceding_key().
  721. */
  722. if (b->ops->is_extents)
  723. preceding_key(&START_KEY(k), &preceding_key_p);
  724. else
  725. preceding_key(k, &preceding_key_p);
  726. m = bch_btree_iter_init(b, &iter, preceding_key_p);
  727. if (b->ops->insert_fixup(b, k, &iter, replace_key))
  728. return status;
  729. status = BTREE_INSERT_STATUS_INSERT;
  730. while (m != bset_bkey_last(i) &&
  731. bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0)
  732. prev = m, m = bkey_next(m);
  733. /* prev is in the tree, if we merge we're done */
  734. status = BTREE_INSERT_STATUS_BACK_MERGE;
  735. if (prev &&
  736. bch_bkey_try_merge(b, prev, k))
  737. goto merged;
  738. #if 0
  739. status = BTREE_INSERT_STATUS_OVERWROTE;
  740. if (m != bset_bkey_last(i) &&
  741. KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
  742. goto copy;
  743. #endif
  744. status = BTREE_INSERT_STATUS_FRONT_MERGE;
  745. if (m != bset_bkey_last(i) &&
  746. bch_bkey_try_merge(b, k, m))
  747. goto copy;
  748. bch_bset_insert(b, m, k);
  749. copy: bkey_copy(m, k);
  750. merged:
  751. return status;
  752. }
  753. EXPORT_SYMBOL(bch_btree_insert_key);
  754. /* Lookup */
  755. struct bset_search_iter {
  756. struct bkey *l, *r;
  757. };
  758. static struct bset_search_iter bset_search_write_set(struct bset_tree *t,
  759. const struct bkey *search)
  760. {
  761. unsigned int li = 0, ri = t->size;
  762. while (li + 1 != ri) {
  763. unsigned int m = (li + ri) >> 1;
  764. if (bkey_cmp(table_to_bkey(t, m), search) > 0)
  765. ri = m;
  766. else
  767. li = m;
  768. }
  769. return (struct bset_search_iter) {
  770. table_to_bkey(t, li),
  771. ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
  772. };
  773. }
  774. static struct bset_search_iter bset_search_tree(struct bset_tree *t,
  775. const struct bkey *search)
  776. {
  777. struct bkey *l, *r;
  778. struct bkey_float *f;
  779. unsigned int inorder, j, n = 1;
  780. do {
  781. unsigned int p = n << 4;
  782. if (p < t->size)
  783. prefetch(&t->tree[p]);
  784. j = n;
  785. f = &t->tree[j];
  786. if (likely(f->exponent != 127)) {
  787. if (f->mantissa >= bfloat_mantissa(search, f))
  788. n = j * 2;
  789. else
  790. n = j * 2 + 1;
  791. } else {
  792. if (bkey_cmp(tree_to_bkey(t, j), search) > 0)
  793. n = j * 2;
  794. else
  795. n = j * 2 + 1;
  796. }
  797. } while (n < t->size);
  798. inorder = to_inorder(j, t);
  799. /*
  800. * n would have been the node we recursed to - the low bit tells us if
  801. * we recursed left or recursed right.
  802. */
  803. if (n & 1) {
  804. l = cacheline_to_bkey(t, inorder, f->m);
  805. if (++inorder != t->size) {
  806. f = &t->tree[inorder_next(j, t->size)];
  807. r = cacheline_to_bkey(t, inorder, f->m);
  808. } else
  809. r = bset_bkey_last(t->data);
  810. } else {
  811. r = cacheline_to_bkey(t, inorder, f->m);
  812. if (--inorder) {
  813. f = &t->tree[inorder_prev(j, t->size)];
  814. l = cacheline_to_bkey(t, inorder, f->m);
  815. } else
  816. l = t->data->start;
  817. }
  818. return (struct bset_search_iter) {l, r};
  819. }
  820. struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
  821. const struct bkey *search)
  822. {
  823. struct bset_search_iter i;
  824. /*
  825. * First, we search for a cacheline, then lastly we do a linear search
  826. * within that cacheline.
  827. *
  828. * To search for the cacheline, there's three different possibilities:
  829. * * The set is too small to have a search tree, so we just do a linear
  830. * search over the whole set.
  831. * * The set is the one we're currently inserting into; keeping a full
  832. * auxiliary search tree up to date would be too expensive, so we
  833. * use a much simpler lookup table to do a binary search -
  834. * bset_search_write_set().
  835. * * Or we use the auxiliary search tree we constructed earlier -
  836. * bset_search_tree()
  837. */
  838. if (unlikely(!t->size)) {
  839. i.l = t->data->start;
  840. i.r = bset_bkey_last(t->data);
  841. } else if (bset_written(b, t)) {
  842. /*
  843. * Each node in the auxiliary search tree covers a certain range
  844. * of bits, and keys above and below the set it covers might
  845. * differ outside those bits - so we have to special case the
  846. * start and end - handle that here:
  847. */
  848. if (unlikely(bkey_cmp(search, &t->end) >= 0))
  849. return bset_bkey_last(t->data);
  850. if (unlikely(bkey_cmp(search, t->data->start) < 0))
  851. return t->data->start;
  852. i = bset_search_tree(t, search);
  853. } else {
  854. BUG_ON(!b->nsets &&
  855. t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
  856. i = bset_search_write_set(t, search);
  857. }
  858. if (btree_keys_expensive_checks(b)) {
  859. BUG_ON(bset_written(b, t) &&
  860. i.l != t->data->start &&
  861. bkey_cmp(tree_to_prev_bkey(t,
  862. inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
  863. search) > 0);
  864. BUG_ON(i.r != bset_bkey_last(t->data) &&
  865. bkey_cmp(i.r, search) <= 0);
  866. }
  867. while (likely(i.l != i.r) &&
  868. bkey_cmp(i.l, search) <= 0)
  869. i.l = bkey_next(i.l);
  870. return i.l;
  871. }
  872. EXPORT_SYMBOL(__bch_bset_search);
  873. /* Btree iterator */
  874. typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
  875. struct btree_iter_set);
  876. static inline bool btree_iter_cmp(struct btree_iter_set l,
  877. struct btree_iter_set r)
  878. {
  879. return bkey_cmp(l.k, r.k) > 0;
  880. }
  881. static inline bool btree_iter_end(struct btree_iter *iter)
  882. {
  883. return !iter->used;
  884. }
  885. void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
  886. struct bkey *end)
  887. {
  888. if (k != end)
  889. BUG_ON(!heap_add(iter,
  890. ((struct btree_iter_set) { k, end }),
  891. btree_iter_cmp));
  892. }
  893. static struct bkey *__bch_btree_iter_init(struct btree_keys *b,
  894. struct btree_iter *iter,
  895. struct bkey *search,
  896. struct bset_tree *start)
  897. {
  898. struct bkey *ret = NULL;
  899. iter->size = ARRAY_SIZE(iter->data);
  900. iter->used = 0;
  901. #ifdef CONFIG_BCACHE_DEBUG
  902. iter->b = b;
  903. #endif
  904. for (; start <= bset_tree_last(b); start++) {
  905. ret = bch_bset_search(b, start, search);
  906. bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
  907. }
  908. return ret;
  909. }
  910. struct bkey *bch_btree_iter_init(struct btree_keys *b,
  911. struct btree_iter *iter,
  912. struct bkey *search)
  913. {
  914. return __bch_btree_iter_init(b, iter, search, b->set);
  915. }
  916. EXPORT_SYMBOL(bch_btree_iter_init);
  917. static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
  918. btree_iter_cmp_fn *cmp)
  919. {
  920. struct btree_iter_set b __maybe_unused;
  921. struct bkey *ret = NULL;
  922. if (!btree_iter_end(iter)) {
  923. bch_btree_iter_next_check(iter);
  924. ret = iter->data->k;
  925. iter->data->k = bkey_next(iter->data->k);
  926. if (iter->data->k > iter->data->end) {
  927. WARN_ONCE(1, "bset was corrupt!\n");
  928. iter->data->k = iter->data->end;
  929. }
  930. if (iter->data->k == iter->data->end)
  931. heap_pop(iter, b, cmp);
  932. else
  933. heap_sift(iter, 0, cmp);
  934. }
  935. return ret;
  936. }
  937. struct bkey *bch_btree_iter_next(struct btree_iter *iter)
  938. {
  939. return __bch_btree_iter_next(iter, btree_iter_cmp);
  940. }
  941. EXPORT_SYMBOL(bch_btree_iter_next);
  942. struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
  943. struct btree_keys *b, ptr_filter_fn fn)
  944. {
  945. struct bkey *ret;
  946. do {
  947. ret = bch_btree_iter_next(iter);
  948. } while (ret && fn(b, ret));
  949. return ret;
  950. }
  951. /* Mergesort */
  952. void bch_bset_sort_state_free(struct bset_sort_state *state)
  953. {
  954. mempool_exit(&state->pool);
  955. }
  956. int bch_bset_sort_state_init(struct bset_sort_state *state,
  957. unsigned int page_order)
  958. {
  959. spin_lock_init(&state->time.lock);
  960. state->page_order = page_order;
  961. state->crit_factor = int_sqrt(1 << page_order);
  962. return mempool_init_page_pool(&state->pool, 1, page_order);
  963. }
  964. EXPORT_SYMBOL(bch_bset_sort_state_init);
  965. static void btree_mergesort(struct btree_keys *b, struct bset *out,
  966. struct btree_iter *iter,
  967. bool fixup, bool remove_stale)
  968. {
  969. int i;
  970. struct bkey *k, *last = NULL;
  971. BKEY_PADDED(k) tmp;
  972. bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale
  973. ? bch_ptr_bad
  974. : bch_ptr_invalid;
  975. /* Heapify the iterator, using our comparison function */
  976. for (i = iter->used / 2 - 1; i >= 0; --i)
  977. heap_sift(iter, i, b->ops->sort_cmp);
  978. while (!btree_iter_end(iter)) {
  979. if (b->ops->sort_fixup && fixup)
  980. k = b->ops->sort_fixup(iter, &tmp.k);
  981. else
  982. k = NULL;
  983. if (!k)
  984. k = __bch_btree_iter_next(iter, b->ops->sort_cmp);
  985. if (bad(b, k))
  986. continue;
  987. if (!last) {
  988. last = out->start;
  989. bkey_copy(last, k);
  990. } else if (!bch_bkey_try_merge(b, last, k)) {
  991. last = bkey_next(last);
  992. bkey_copy(last, k);
  993. }
  994. }
  995. out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
  996. pr_debug("sorted %i keys", out->keys);
  997. }
  998. static void __btree_sort(struct btree_keys *b, struct btree_iter *iter,
  999. unsigned int start, unsigned int order, bool fixup,
  1000. struct bset_sort_state *state)
  1001. {
  1002. uint64_t start_time;
  1003. bool used_mempool = false;
  1004. struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOWAIT,
  1005. order);
  1006. if (!out) {
  1007. struct page *outp;
  1008. BUG_ON(order > state->page_order);
  1009. outp = mempool_alloc(&state->pool, GFP_NOIO);
  1010. out = page_address(outp);
  1011. used_mempool = true;
  1012. order = state->page_order;
  1013. }
  1014. start_time = local_clock();
  1015. btree_mergesort(b, out, iter, fixup, false);
  1016. b->nsets = start;
  1017. if (!start && order == b->page_order) {
  1018. /*
  1019. * Our temporary buffer is the same size as the btree node's
  1020. * buffer, we can just swap buffers instead of doing a big
  1021. * memcpy()
  1022. */
  1023. out->magic = b->set->data->magic;
  1024. out->seq = b->set->data->seq;
  1025. out->version = b->set->data->version;
  1026. swap(out, b->set->data);
  1027. } else {
  1028. b->set[start].data->keys = out->keys;
  1029. memcpy(b->set[start].data->start, out->start,
  1030. (void *) bset_bkey_last(out) - (void *) out->start);
  1031. }
  1032. if (used_mempool)
  1033. mempool_free(virt_to_page(out), &state->pool);
  1034. else
  1035. free_pages((unsigned long) out, order);
  1036. bch_bset_build_written_tree(b);
  1037. if (!start)
  1038. bch_time_stats_update(&state->time, start_time);
  1039. }
  1040. void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,
  1041. struct bset_sort_state *state)
  1042. {
  1043. size_t order = b->page_order, keys = 0;
  1044. struct btree_iter iter;
  1045. int oldsize = bch_count_data(b);
  1046. __bch_btree_iter_init(b, &iter, NULL, &b->set[start]);
  1047. if (start) {
  1048. unsigned int i;
  1049. for (i = start; i <= b->nsets; i++)
  1050. keys += b->set[i].data->keys;
  1051. order = get_order(__set_bytes(b->set->data, keys));
  1052. }
  1053. __btree_sort(b, &iter, start, order, false, state);
  1054. EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize);
  1055. }
  1056. EXPORT_SYMBOL(bch_btree_sort_partial);
  1057. void bch_btree_sort_and_fix_extents(struct btree_keys *b,
  1058. struct btree_iter *iter,
  1059. struct bset_sort_state *state)
  1060. {
  1061. __btree_sort(b, iter, 0, b->page_order, true, state);
  1062. }
  1063. void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
  1064. struct bset_sort_state *state)
  1065. {
  1066. uint64_t start_time = local_clock();
  1067. struct btree_iter iter;
  1068. bch_btree_iter_init(b, &iter, NULL);
  1069. btree_mergesort(b, new->set->data, &iter, false, true);
  1070. bch_time_stats_update(&state->time, start_time);
  1071. new->set->size = 0; // XXX: why?
  1072. }
  1073. #define SORT_CRIT (4096 / sizeof(uint64_t))
  1074. void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state)
  1075. {
  1076. unsigned int crit = SORT_CRIT;
  1077. int i;
  1078. /* Don't sort if nothing to do */
  1079. if (!b->nsets)
  1080. goto out;
  1081. for (i = b->nsets - 1; i >= 0; --i) {
  1082. crit *= state->crit_factor;
  1083. if (b->set[i].data->keys < crit) {
  1084. bch_btree_sort_partial(b, i, state);
  1085. return;
  1086. }
  1087. }
  1088. /* Sort if we'd overflow */
  1089. if (b->nsets + 1 == MAX_BSETS) {
  1090. bch_btree_sort(b, state);
  1091. return;
  1092. }
  1093. out:
  1094. bch_bset_build_written_tree(b);
  1095. }
  1096. EXPORT_SYMBOL(bch_btree_sort_lazy);
  1097. void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats)
  1098. {
  1099. unsigned int i;
  1100. for (i = 0; i <= b->nsets; i++) {
  1101. struct bset_tree *t = &b->set[i];
  1102. size_t bytes = t->data->keys * sizeof(uint64_t);
  1103. size_t j;
  1104. if (bset_written(b, t)) {
  1105. stats->sets_written++;
  1106. stats->bytes_written += bytes;
  1107. stats->floats += t->size - 1;
  1108. for (j = 1; j < t->size; j++)
  1109. if (t->tree[j].exponent == 127)
  1110. stats->failed++;
  1111. } else {
  1112. stats->sets_unwritten++;
  1113. stats->bytes_unwritten += bytes;
  1114. }
  1115. }
  1116. }