bset.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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. unsigned int i;
  274. b->ops = ops;
  275. b->expensive_debug_checks = expensive_debug_checks;
  276. b->nsets = 0;
  277. b->last_set_unwritten = 0;
  278. /* XXX: shouldn't be needed */
  279. for (i = 0; i < MAX_BSETS; i++)
  280. b->set[i].size = 0;
  281. /*
  282. * Second loop starts at 1 because b->keys[0]->data is the memory we
  283. * allocated
  284. */
  285. for (i = 1; i < MAX_BSETS; i++)
  286. b->set[i].data = NULL;
  287. }
  288. EXPORT_SYMBOL(bch_btree_keys_init);
  289. /* Binary tree stuff for auxiliary search trees */
  290. /*
  291. * return array index next to j when does in-order traverse
  292. * of a binary tree which is stored in a linear array
  293. */
  294. static unsigned int inorder_next(unsigned int j, unsigned int size)
  295. {
  296. if (j * 2 + 1 < size) {
  297. j = j * 2 + 1;
  298. while (j * 2 < size)
  299. j *= 2;
  300. } else
  301. j >>= ffz(j) + 1;
  302. return j;
  303. }
  304. /*
  305. * return array index previous to j when does in-order traverse
  306. * of a binary tree which is stored in a linear array
  307. */
  308. static unsigned int inorder_prev(unsigned int j, unsigned int size)
  309. {
  310. if (j * 2 < size) {
  311. j = j * 2;
  312. while (j * 2 + 1 < size)
  313. j = j * 2 + 1;
  314. } else
  315. j >>= ffs(j);
  316. return j;
  317. }
  318. /*
  319. * I have no idea why this code works... and I'm the one who wrote it
  320. *
  321. * However, I do know what it does:
  322. * Given a binary tree constructed in an array (i.e. how you normally implement
  323. * a heap), it converts a node in the tree - referenced by array index - to the
  324. * index it would have if you did an inorder traversal.
  325. *
  326. * Also tested for every j, size up to size somewhere around 6 million.
  327. *
  328. * The binary tree starts at array index 1, not 0
  329. * extra is a function of size:
  330. * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
  331. */
  332. static unsigned int __to_inorder(unsigned int j,
  333. unsigned int size,
  334. unsigned int extra)
  335. {
  336. unsigned int b = fls(j);
  337. unsigned int shift = fls(size - 1) - b;
  338. j ^= 1U << (b - 1);
  339. j <<= 1;
  340. j |= 1;
  341. j <<= shift;
  342. if (j > extra)
  343. j -= (j - extra) >> 1;
  344. return j;
  345. }
  346. /*
  347. * Return the cacheline index in bset_tree->data, where j is index
  348. * from a linear array which stores the auxiliar binary tree
  349. */
  350. static unsigned int to_inorder(unsigned int j, struct bset_tree *t)
  351. {
  352. return __to_inorder(j, t->size, t->extra);
  353. }
  354. static unsigned int __inorder_to_tree(unsigned int j,
  355. unsigned int size,
  356. unsigned int extra)
  357. {
  358. unsigned int shift;
  359. if (j > extra)
  360. j += j - extra;
  361. shift = ffs(j);
  362. j >>= shift;
  363. j |= roundup_pow_of_two(size) >> shift;
  364. return j;
  365. }
  366. /*
  367. * Return an index from a linear array which stores the auxiliar binary
  368. * tree, j is the cacheline index of t->data.
  369. */
  370. static unsigned int inorder_to_tree(unsigned int j, struct bset_tree *t)
  371. {
  372. return __inorder_to_tree(j, t->size, t->extra);
  373. }
  374. #if 0
  375. void inorder_test(void)
  376. {
  377. unsigned long done = 0;
  378. ktime_t start = ktime_get();
  379. for (unsigned int size = 2;
  380. size < 65536000;
  381. size++) {
  382. unsigned int extra =
  383. (size - rounddown_pow_of_two(size - 1)) << 1;
  384. unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
  385. if (!(size % 4096))
  386. pr_notice("loop %u, %llu per us\n", size,
  387. done / ktime_us_delta(ktime_get(), start));
  388. while (1) {
  389. if (__inorder_to_tree(i, size, extra) != j)
  390. panic("size %10u j %10u i %10u", size, j, i);
  391. if (__to_inorder(j, size, extra) != i)
  392. panic("size %10u j %10u i %10u", size, j, i);
  393. if (j == rounddown_pow_of_two(size) - 1)
  394. break;
  395. BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
  396. j = inorder_next(j, size);
  397. i++;
  398. }
  399. done += size - 1;
  400. }
  401. }
  402. #endif
  403. /*
  404. * Cacheline/offset <-> bkey pointer arithmetic:
  405. *
  406. * t->tree is a binary search tree in an array; each node corresponds to a key
  407. * in one cacheline in t->set (BSET_CACHELINE bytes).
  408. *
  409. * This means we don't have to store the full index of the key that a node in
  410. * the binary tree points to; to_inorder() gives us the cacheline, and then
  411. * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
  412. *
  413. * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
  414. * make this work.
  415. *
  416. * To construct the bfloat for an arbitrary key we need to know what the key
  417. * immediately preceding it is: we have to check if the two keys differ in the
  418. * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
  419. * of the previous key so we can walk backwards to it from t->tree[j]'s key.
  420. */
  421. static struct bkey *cacheline_to_bkey(struct bset_tree *t,
  422. unsigned int cacheline,
  423. unsigned int offset)
  424. {
  425. return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
  426. }
  427. static unsigned int bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
  428. {
  429. return ((void *) k - (void *) t->data) / BSET_CACHELINE;
  430. }
  431. static unsigned int bkey_to_cacheline_offset(struct bset_tree *t,
  432. unsigned int cacheline,
  433. struct bkey *k)
  434. {
  435. return (u64 *) k - (u64 *) cacheline_to_bkey(t, cacheline, 0);
  436. }
  437. static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned int j)
  438. {
  439. return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
  440. }
  441. static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned int j)
  442. {
  443. return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
  444. }
  445. /*
  446. * For the write set - the one we're currently inserting keys into - we don't
  447. * maintain a full search tree, we just keep a simple lookup table in t->prev.
  448. */
  449. static struct bkey *table_to_bkey(struct bset_tree *t, unsigned int cacheline)
  450. {
  451. return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
  452. }
  453. static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
  454. {
  455. low >>= shift;
  456. low |= (high << 1) << (63U - shift);
  457. return low;
  458. }
  459. /*
  460. * Calculate mantissa value for struct bkey_float.
  461. * If most significant bit of f->exponent is not set, then
  462. * - f->exponent >> 6 is 0
  463. * - p[0] points to bkey->low
  464. * - p[-1] borrows bits from KEY_INODE() of bkey->high
  465. * if most isgnificant bits of f->exponent is set, then
  466. * - f->exponent >> 6 is 1
  467. * - p[0] points to bits from KEY_INODE() of bkey->high
  468. * - p[-1] points to other bits from KEY_INODE() of
  469. * bkey->high too.
  470. * See make_bfloat() to check when most significant bit of f->exponent
  471. * is set or not.
  472. */
  473. static inline unsigned int bfloat_mantissa(const struct bkey *k,
  474. struct bkey_float *f)
  475. {
  476. const uint64_t *p = &k->low - (f->exponent >> 6);
  477. return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
  478. }
  479. static void make_bfloat(struct bset_tree *t, unsigned int j)
  480. {
  481. struct bkey_float *f = &t->tree[j];
  482. struct bkey *m = tree_to_bkey(t, j);
  483. struct bkey *p = tree_to_prev_bkey(t, j);
  484. struct bkey *l = is_power_of_2(j)
  485. ? t->data->start
  486. : tree_to_prev_bkey(t, j >> ffs(j));
  487. struct bkey *r = is_power_of_2(j + 1)
  488. ? bset_bkey_idx(t->data, t->data->keys - bkey_u64s(&t->end))
  489. : tree_to_bkey(t, j >> (ffz(j) + 1));
  490. BUG_ON(m < l || m > r);
  491. BUG_ON(bkey_next(p) != m);
  492. /*
  493. * If l and r have different KEY_INODE values (different backing
  494. * device), f->exponent records how many least significant bits
  495. * are different in KEY_INODE values and sets most significant
  496. * bits to 1 (by +64).
  497. * If l and r have same KEY_INODE value, f->exponent records
  498. * how many different bits in least significant bits of bkey->low.
  499. * See bfloat_mantiss() how the most significant bit of
  500. * f->exponent is used to calculate bfloat mantissa value.
  501. */
  502. if (KEY_INODE(l) != KEY_INODE(r))
  503. f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
  504. else
  505. f->exponent = fls64(r->low ^ l->low);
  506. f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
  507. /*
  508. * Setting f->exponent = 127 flags this node as failed, and causes the
  509. * lookup code to fall back to comparing against the original key.
  510. */
  511. if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
  512. f->mantissa = bfloat_mantissa(m, f) - 1;
  513. else
  514. f->exponent = 127;
  515. }
  516. static void bset_alloc_tree(struct btree_keys *b, struct bset_tree *t)
  517. {
  518. if (t != b->set) {
  519. unsigned int j = roundup(t[-1].size,
  520. 64 / sizeof(struct bkey_float));
  521. t->tree = t[-1].tree + j;
  522. t->prev = t[-1].prev + j;
  523. }
  524. while (t < b->set + MAX_BSETS)
  525. t++->size = 0;
  526. }
  527. static void bch_bset_build_unwritten_tree(struct btree_keys *b)
  528. {
  529. struct bset_tree *t = bset_tree_last(b);
  530. BUG_ON(b->last_set_unwritten);
  531. b->last_set_unwritten = 1;
  532. bset_alloc_tree(b, t);
  533. if (t->tree != b->set->tree + btree_keys_cachelines(b)) {
  534. t->prev[0] = bkey_to_cacheline_offset(t, 0, t->data->start);
  535. t->size = 1;
  536. }
  537. }
  538. void bch_bset_init_next(struct btree_keys *b, struct bset *i, uint64_t magic)
  539. {
  540. if (i != b->set->data) {
  541. b->set[++b->nsets].data = i;
  542. i->seq = b->set->data->seq;
  543. } else
  544. get_random_bytes(&i->seq, sizeof(uint64_t));
  545. i->magic = magic;
  546. i->version = 0;
  547. i->keys = 0;
  548. bch_bset_build_unwritten_tree(b);
  549. }
  550. EXPORT_SYMBOL(bch_bset_init_next);
  551. /*
  552. * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to
  553. * accelerate bkey search in a btree node (pointed by bset_tree->data in
  554. * memory). After search in the auxiliar tree by calling bset_search_tree(),
  555. * a struct bset_search_iter is returned which indicates range [l, r] from
  556. * bset_tree->data where the searching bkey might be inside. Then a followed
  557. * linear comparison does the exact search, see __bch_bset_search() for how
  558. * the auxiliary tree is used.
  559. */
  560. void bch_bset_build_written_tree(struct btree_keys *b)
  561. {
  562. struct bset_tree *t = bset_tree_last(b);
  563. struct bkey *prev = NULL, *k = t->data->start;
  564. unsigned int j, cacheline = 1;
  565. b->last_set_unwritten = 0;
  566. bset_alloc_tree(b, t);
  567. t->size = min_t(unsigned int,
  568. bkey_to_cacheline(t, bset_bkey_last(t->data)),
  569. b->set->tree + btree_keys_cachelines(b) - t->tree);
  570. if (t->size < 2) {
  571. t->size = 0;
  572. return;
  573. }
  574. t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
  575. /* First we figure out where the first key in each cacheline is */
  576. for (j = inorder_next(0, t->size);
  577. j;
  578. j = inorder_next(j, t->size)) {
  579. while (bkey_to_cacheline(t, k) < cacheline)
  580. prev = k, k = bkey_next(k);
  581. t->prev[j] = bkey_u64s(prev);
  582. t->tree[j].m = bkey_to_cacheline_offset(t, cacheline++, k);
  583. }
  584. while (bkey_next(k) != bset_bkey_last(t->data))
  585. k = bkey_next(k);
  586. t->end = *k;
  587. /* Then we build the tree */
  588. for (j = inorder_next(0, t->size);
  589. j;
  590. j = inorder_next(j, t->size))
  591. make_bfloat(t, j);
  592. }
  593. EXPORT_SYMBOL(bch_bset_build_written_tree);
  594. /* Insert */
  595. void bch_bset_fix_invalidated_key(struct btree_keys *b, struct bkey *k)
  596. {
  597. struct bset_tree *t;
  598. unsigned int inorder, j = 1;
  599. for (t = b->set; t <= bset_tree_last(b); t++)
  600. if (k < bset_bkey_last(t->data))
  601. goto found_set;
  602. BUG();
  603. found_set:
  604. if (!t->size || !bset_written(b, t))
  605. return;
  606. inorder = bkey_to_cacheline(t, k);
  607. if (k == t->data->start)
  608. goto fix_left;
  609. if (bkey_next(k) == bset_bkey_last(t->data)) {
  610. t->end = *k;
  611. goto fix_right;
  612. }
  613. j = inorder_to_tree(inorder, t);
  614. if (j &&
  615. j < t->size &&
  616. k == tree_to_bkey(t, j))
  617. fix_left: do {
  618. make_bfloat(t, j);
  619. j = j * 2;
  620. } while (j < t->size);
  621. j = inorder_to_tree(inorder + 1, t);
  622. if (j &&
  623. j < t->size &&
  624. k == tree_to_prev_bkey(t, j))
  625. fix_right: do {
  626. make_bfloat(t, j);
  627. j = j * 2 + 1;
  628. } while (j < t->size);
  629. }
  630. EXPORT_SYMBOL(bch_bset_fix_invalidated_key);
  631. static void bch_bset_fix_lookup_table(struct btree_keys *b,
  632. struct bset_tree *t,
  633. struct bkey *k)
  634. {
  635. unsigned int shift = bkey_u64s(k);
  636. unsigned int j = bkey_to_cacheline(t, k);
  637. /* We're getting called from btree_split() or btree_gc, just bail out */
  638. if (!t->size)
  639. return;
  640. /*
  641. * k is the key we just inserted; we need to find the entry in the
  642. * lookup table for the first key that is strictly greater than k:
  643. * it's either k's cacheline or the next one
  644. */
  645. while (j < t->size &&
  646. table_to_bkey(t, j) <= k)
  647. j++;
  648. /*
  649. * Adjust all the lookup table entries, and find a new key for any that
  650. * have gotten too big
  651. */
  652. for (; j < t->size; j++) {
  653. t->prev[j] += shift;
  654. if (t->prev[j] > 7) {
  655. k = table_to_bkey(t, j - 1);
  656. while (k < cacheline_to_bkey(t, j, 0))
  657. k = bkey_next(k);
  658. t->prev[j] = bkey_to_cacheline_offset(t, j, k);
  659. }
  660. }
  661. if (t->size == b->set->tree + btree_keys_cachelines(b) - t->tree)
  662. return;
  663. /* Possibly add a new entry to the end of the lookup table */
  664. for (k = table_to_bkey(t, t->size - 1);
  665. k != bset_bkey_last(t->data);
  666. k = bkey_next(k))
  667. if (t->size == bkey_to_cacheline(t, k)) {
  668. t->prev[t->size] =
  669. bkey_to_cacheline_offset(t, t->size, k);
  670. t->size++;
  671. }
  672. }
  673. /*
  674. * Tries to merge l and r: l should be lower than r
  675. * Returns true if we were able to merge. If we did merge, l will be the merged
  676. * key, r will be untouched.
  677. */
  678. bool bch_bkey_try_merge(struct btree_keys *b, struct bkey *l, struct bkey *r)
  679. {
  680. if (!b->ops->key_merge)
  681. return false;
  682. /*
  683. * Generic header checks
  684. * Assumes left and right are in order
  685. * Left and right must be exactly aligned
  686. */
  687. if (!bch_bkey_equal_header(l, r) ||
  688. bkey_cmp(l, &START_KEY(r)))
  689. return false;
  690. return b->ops->key_merge(b, l, r);
  691. }
  692. EXPORT_SYMBOL(bch_bkey_try_merge);
  693. void bch_bset_insert(struct btree_keys *b, struct bkey *where,
  694. struct bkey *insert)
  695. {
  696. struct bset_tree *t = bset_tree_last(b);
  697. BUG_ON(!b->last_set_unwritten);
  698. BUG_ON(bset_byte_offset(b, t->data) +
  699. __set_bytes(t->data, t->data->keys + bkey_u64s(insert)) >
  700. PAGE_SIZE << b->page_order);
  701. memmove((uint64_t *) where + bkey_u64s(insert),
  702. where,
  703. (void *) bset_bkey_last(t->data) - (void *) where);
  704. t->data->keys += bkey_u64s(insert);
  705. bkey_copy(where, insert);
  706. bch_bset_fix_lookup_table(b, t, where);
  707. }
  708. EXPORT_SYMBOL(bch_bset_insert);
  709. unsigned int bch_btree_insert_key(struct btree_keys *b, struct bkey *k,
  710. struct bkey *replace_key)
  711. {
  712. unsigned int status = BTREE_INSERT_STATUS_NO_INSERT;
  713. struct bset *i = bset_tree_last(b)->data;
  714. struct bkey *m, *prev = NULL;
  715. struct btree_iter iter;
  716. struct bkey preceding_key_on_stack = ZERO_KEY;
  717. struct bkey *preceding_key_p = &preceding_key_on_stack;
  718. BUG_ON(b->ops->is_extents && !KEY_SIZE(k));
  719. /*
  720. * If k has preceding key, preceding_key_p will be set to address
  721. * of k's preceding key; otherwise preceding_key_p will be set
  722. * to NULL inside preceding_key().
  723. */
  724. if (b->ops->is_extents)
  725. preceding_key(&START_KEY(k), &preceding_key_p);
  726. else
  727. preceding_key(k, &preceding_key_p);
  728. m = bch_btree_iter_init(b, &iter, preceding_key_p);
  729. if (b->ops->insert_fixup(b, k, &iter, replace_key))
  730. return status;
  731. status = BTREE_INSERT_STATUS_INSERT;
  732. while (m != bset_bkey_last(i) &&
  733. bkey_cmp(k, b->ops->is_extents ? &START_KEY(m) : m) > 0)
  734. prev = m, m = bkey_next(m);
  735. /* prev is in the tree, if we merge we're done */
  736. status = BTREE_INSERT_STATUS_BACK_MERGE;
  737. if (prev &&
  738. bch_bkey_try_merge(b, prev, k))
  739. goto merged;
  740. #if 0
  741. status = BTREE_INSERT_STATUS_OVERWROTE;
  742. if (m != bset_bkey_last(i) &&
  743. KEY_PTRS(m) == KEY_PTRS(k) && !KEY_SIZE(m))
  744. goto copy;
  745. #endif
  746. status = BTREE_INSERT_STATUS_FRONT_MERGE;
  747. if (m != bset_bkey_last(i) &&
  748. bch_bkey_try_merge(b, k, m))
  749. goto copy;
  750. bch_bset_insert(b, m, k);
  751. copy: bkey_copy(m, k);
  752. merged:
  753. return status;
  754. }
  755. EXPORT_SYMBOL(bch_btree_insert_key);
  756. /* Lookup */
  757. struct bset_search_iter {
  758. struct bkey *l, *r;
  759. };
  760. static struct bset_search_iter bset_search_write_set(struct bset_tree *t,
  761. const struct bkey *search)
  762. {
  763. unsigned int li = 0, ri = t->size;
  764. while (li + 1 != ri) {
  765. unsigned int m = (li + ri) >> 1;
  766. if (bkey_cmp(table_to_bkey(t, m), search) > 0)
  767. ri = m;
  768. else
  769. li = m;
  770. }
  771. return (struct bset_search_iter) {
  772. table_to_bkey(t, li),
  773. ri < t->size ? table_to_bkey(t, ri) : bset_bkey_last(t->data)
  774. };
  775. }
  776. static struct bset_search_iter bset_search_tree(struct bset_tree *t,
  777. const struct bkey *search)
  778. {
  779. struct bkey *l, *r;
  780. struct bkey_float *f;
  781. unsigned int inorder, j, n = 1;
  782. do {
  783. /*
  784. * A bit trick here.
  785. * If p < t->size, (int)(p - t->size) is a minus value and
  786. * the most significant bit is set, right shifting 31 bits
  787. * gets 1. If p >= t->size, the most significant bit is
  788. * not set, right shifting 31 bits gets 0.
  789. * So the following 2 lines equals to
  790. * if (p >= t->size)
  791. * p = 0;
  792. * but a branch instruction is avoided.
  793. */
  794. unsigned int p = n << 4;
  795. p &= ((int) (p - t->size)) >> 31;
  796. prefetch(&t->tree[p]);
  797. j = n;
  798. f = &t->tree[j];
  799. /*
  800. * Similar bit trick, use subtract operation to avoid a branch
  801. * instruction.
  802. *
  803. * n = (f->mantissa > bfloat_mantissa())
  804. * ? j * 2
  805. * : j * 2 + 1;
  806. *
  807. * We need to subtract 1 from f->mantissa for the sign bit trick
  808. * to work - that's done in make_bfloat()
  809. */
  810. if (likely(f->exponent != 127))
  811. n = j * 2 + (((unsigned int)
  812. (f->mantissa -
  813. bfloat_mantissa(search, f))) >> 31);
  814. else
  815. n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
  816. ? j * 2
  817. : j * 2 + 1;
  818. } while (n < t->size);
  819. inorder = to_inorder(j, t);
  820. /*
  821. * n would have been the node we recursed to - the low bit tells us if
  822. * we recursed left or recursed right.
  823. */
  824. if (n & 1) {
  825. l = cacheline_to_bkey(t, inorder, f->m);
  826. if (++inorder != t->size) {
  827. f = &t->tree[inorder_next(j, t->size)];
  828. r = cacheline_to_bkey(t, inorder, f->m);
  829. } else
  830. r = bset_bkey_last(t->data);
  831. } else {
  832. r = cacheline_to_bkey(t, inorder, f->m);
  833. if (--inorder) {
  834. f = &t->tree[inorder_prev(j, t->size)];
  835. l = cacheline_to_bkey(t, inorder, f->m);
  836. } else
  837. l = t->data->start;
  838. }
  839. return (struct bset_search_iter) {l, r};
  840. }
  841. struct bkey *__bch_bset_search(struct btree_keys *b, struct bset_tree *t,
  842. const struct bkey *search)
  843. {
  844. struct bset_search_iter i;
  845. /*
  846. * First, we search for a cacheline, then lastly we do a linear search
  847. * within that cacheline.
  848. *
  849. * To search for the cacheline, there's three different possibilities:
  850. * * The set is too small to have a search tree, so we just do a linear
  851. * search over the whole set.
  852. * * The set is the one we're currently inserting into; keeping a full
  853. * auxiliary search tree up to date would be too expensive, so we
  854. * use a much simpler lookup table to do a binary search -
  855. * bset_search_write_set().
  856. * * Or we use the auxiliary search tree we constructed earlier -
  857. * bset_search_tree()
  858. */
  859. if (unlikely(!t->size)) {
  860. i.l = t->data->start;
  861. i.r = bset_bkey_last(t->data);
  862. } else if (bset_written(b, t)) {
  863. /*
  864. * Each node in the auxiliary search tree covers a certain range
  865. * of bits, and keys above and below the set it covers might
  866. * differ outside those bits - so we have to special case the
  867. * start and end - handle that here:
  868. */
  869. if (unlikely(bkey_cmp(search, &t->end) >= 0))
  870. return bset_bkey_last(t->data);
  871. if (unlikely(bkey_cmp(search, t->data->start) < 0))
  872. return t->data->start;
  873. i = bset_search_tree(t, search);
  874. } else {
  875. BUG_ON(!b->nsets &&
  876. t->size < bkey_to_cacheline(t, bset_bkey_last(t->data)));
  877. i = bset_search_write_set(t, search);
  878. }
  879. if (btree_keys_expensive_checks(b)) {
  880. BUG_ON(bset_written(b, t) &&
  881. i.l != t->data->start &&
  882. bkey_cmp(tree_to_prev_bkey(t,
  883. inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
  884. search) > 0);
  885. BUG_ON(i.r != bset_bkey_last(t->data) &&
  886. bkey_cmp(i.r, search) <= 0);
  887. }
  888. while (likely(i.l != i.r) &&
  889. bkey_cmp(i.l, search) <= 0)
  890. i.l = bkey_next(i.l);
  891. return i.l;
  892. }
  893. EXPORT_SYMBOL(__bch_bset_search);
  894. /* Btree iterator */
  895. typedef bool (btree_iter_cmp_fn)(struct btree_iter_set,
  896. struct btree_iter_set);
  897. static inline bool btree_iter_cmp(struct btree_iter_set l,
  898. struct btree_iter_set r)
  899. {
  900. return bkey_cmp(l.k, r.k) > 0;
  901. }
  902. static inline bool btree_iter_end(struct btree_iter *iter)
  903. {
  904. return !iter->used;
  905. }
  906. void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
  907. struct bkey *end)
  908. {
  909. if (k != end)
  910. BUG_ON(!heap_add(iter,
  911. ((struct btree_iter_set) { k, end }),
  912. btree_iter_cmp));
  913. }
  914. static struct bkey *__bch_btree_iter_init(struct btree_keys *b,
  915. struct btree_iter *iter,
  916. struct bkey *search,
  917. struct bset_tree *start)
  918. {
  919. struct bkey *ret = NULL;
  920. iter->size = ARRAY_SIZE(iter->data);
  921. iter->used = 0;
  922. #ifdef CONFIG_BCACHE_DEBUG
  923. iter->b = b;
  924. #endif
  925. for (; start <= bset_tree_last(b); start++) {
  926. ret = bch_bset_search(b, start, search);
  927. bch_btree_iter_push(iter, ret, bset_bkey_last(start->data));
  928. }
  929. return ret;
  930. }
  931. struct bkey *bch_btree_iter_init(struct btree_keys *b,
  932. struct btree_iter *iter,
  933. struct bkey *search)
  934. {
  935. return __bch_btree_iter_init(b, iter, search, b->set);
  936. }
  937. EXPORT_SYMBOL(bch_btree_iter_init);
  938. static inline struct bkey *__bch_btree_iter_next(struct btree_iter *iter,
  939. btree_iter_cmp_fn *cmp)
  940. {
  941. struct btree_iter_set b __maybe_unused;
  942. struct bkey *ret = NULL;
  943. if (!btree_iter_end(iter)) {
  944. bch_btree_iter_next_check(iter);
  945. ret = iter->data->k;
  946. iter->data->k = bkey_next(iter->data->k);
  947. if (iter->data->k > iter->data->end) {
  948. WARN_ONCE(1, "bset was corrupt!\n");
  949. iter->data->k = iter->data->end;
  950. }
  951. if (iter->data->k == iter->data->end)
  952. heap_pop(iter, b, cmp);
  953. else
  954. heap_sift(iter, 0, cmp);
  955. }
  956. return ret;
  957. }
  958. struct bkey *bch_btree_iter_next(struct btree_iter *iter)
  959. {
  960. return __bch_btree_iter_next(iter, btree_iter_cmp);
  961. }
  962. EXPORT_SYMBOL(bch_btree_iter_next);
  963. struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
  964. struct btree_keys *b, ptr_filter_fn fn)
  965. {
  966. struct bkey *ret;
  967. do {
  968. ret = bch_btree_iter_next(iter);
  969. } while (ret && fn(b, ret));
  970. return ret;
  971. }
  972. /* Mergesort */
  973. void bch_bset_sort_state_free(struct bset_sort_state *state)
  974. {
  975. mempool_exit(&state->pool);
  976. }
  977. int bch_bset_sort_state_init(struct bset_sort_state *state,
  978. unsigned int page_order)
  979. {
  980. spin_lock_init(&state->time.lock);
  981. state->page_order = page_order;
  982. state->crit_factor = int_sqrt(1 << page_order);
  983. return mempool_init_page_pool(&state->pool, 1, page_order);
  984. }
  985. EXPORT_SYMBOL(bch_bset_sort_state_init);
  986. static void btree_mergesort(struct btree_keys *b, struct bset *out,
  987. struct btree_iter *iter,
  988. bool fixup, bool remove_stale)
  989. {
  990. int i;
  991. struct bkey *k, *last = NULL;
  992. BKEY_PADDED(k) tmp;
  993. bool (*bad)(struct btree_keys *, const struct bkey *) = remove_stale
  994. ? bch_ptr_bad
  995. : bch_ptr_invalid;
  996. /* Heapify the iterator, using our comparison function */
  997. for (i = iter->used / 2 - 1; i >= 0; --i)
  998. heap_sift(iter, i, b->ops->sort_cmp);
  999. while (!btree_iter_end(iter)) {
  1000. if (b->ops->sort_fixup && fixup)
  1001. k = b->ops->sort_fixup(iter, &tmp.k);
  1002. else
  1003. k = NULL;
  1004. if (!k)
  1005. k = __bch_btree_iter_next(iter, b->ops->sort_cmp);
  1006. if (bad(b, k))
  1007. continue;
  1008. if (!last) {
  1009. last = out->start;
  1010. bkey_copy(last, k);
  1011. } else if (!bch_bkey_try_merge(b, last, k)) {
  1012. last = bkey_next(last);
  1013. bkey_copy(last, k);
  1014. }
  1015. }
  1016. out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
  1017. pr_debug("sorted %i keys", out->keys);
  1018. }
  1019. static void __btree_sort(struct btree_keys *b, struct btree_iter *iter,
  1020. unsigned int start, unsigned int order, bool fixup,
  1021. struct bset_sort_state *state)
  1022. {
  1023. uint64_t start_time;
  1024. bool used_mempool = false;
  1025. struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOWAIT,
  1026. order);
  1027. if (!out) {
  1028. struct page *outp;
  1029. BUG_ON(order > state->page_order);
  1030. outp = mempool_alloc(&state->pool, GFP_NOIO);
  1031. out = page_address(outp);
  1032. used_mempool = true;
  1033. order = state->page_order;
  1034. }
  1035. start_time = local_clock();
  1036. btree_mergesort(b, out, iter, fixup, false);
  1037. b->nsets = start;
  1038. if (!start && order == b->page_order) {
  1039. /*
  1040. * Our temporary buffer is the same size as the btree node's
  1041. * buffer, we can just swap buffers instead of doing a big
  1042. * memcpy()
  1043. */
  1044. out->magic = b->set->data->magic;
  1045. out->seq = b->set->data->seq;
  1046. out->version = b->set->data->version;
  1047. swap(out, b->set->data);
  1048. } else {
  1049. b->set[start].data->keys = out->keys;
  1050. memcpy(b->set[start].data->start, out->start,
  1051. (void *) bset_bkey_last(out) - (void *) out->start);
  1052. }
  1053. if (used_mempool)
  1054. mempool_free(virt_to_page(out), &state->pool);
  1055. else
  1056. free_pages((unsigned long) out, order);
  1057. bch_bset_build_written_tree(b);
  1058. if (!start)
  1059. bch_time_stats_update(&state->time, start_time);
  1060. }
  1061. void bch_btree_sort_partial(struct btree_keys *b, unsigned int start,
  1062. struct bset_sort_state *state)
  1063. {
  1064. size_t order = b->page_order, keys = 0;
  1065. struct btree_iter iter;
  1066. int oldsize = bch_count_data(b);
  1067. __bch_btree_iter_init(b, &iter, NULL, &b->set[start]);
  1068. if (start) {
  1069. unsigned int i;
  1070. for (i = start; i <= b->nsets; i++)
  1071. keys += b->set[i].data->keys;
  1072. order = get_order(__set_bytes(b->set->data, keys));
  1073. }
  1074. __btree_sort(b, &iter, start, order, false, state);
  1075. EBUG_ON(oldsize >= 0 && bch_count_data(b) != oldsize);
  1076. }
  1077. EXPORT_SYMBOL(bch_btree_sort_partial);
  1078. void bch_btree_sort_and_fix_extents(struct btree_keys *b,
  1079. struct btree_iter *iter,
  1080. struct bset_sort_state *state)
  1081. {
  1082. __btree_sort(b, iter, 0, b->page_order, true, state);
  1083. }
  1084. void bch_btree_sort_into(struct btree_keys *b, struct btree_keys *new,
  1085. struct bset_sort_state *state)
  1086. {
  1087. uint64_t start_time = local_clock();
  1088. struct btree_iter iter;
  1089. bch_btree_iter_init(b, &iter, NULL);
  1090. btree_mergesort(b, new->set->data, &iter, false, true);
  1091. bch_time_stats_update(&state->time, start_time);
  1092. new->set->size = 0; // XXX: why?
  1093. }
  1094. #define SORT_CRIT (4096 / sizeof(uint64_t))
  1095. void bch_btree_sort_lazy(struct btree_keys *b, struct bset_sort_state *state)
  1096. {
  1097. unsigned int crit = SORT_CRIT;
  1098. int i;
  1099. /* Don't sort if nothing to do */
  1100. if (!b->nsets)
  1101. goto out;
  1102. for (i = b->nsets - 1; i >= 0; --i) {
  1103. crit *= state->crit_factor;
  1104. if (b->set[i].data->keys < crit) {
  1105. bch_btree_sort_partial(b, i, state);
  1106. return;
  1107. }
  1108. }
  1109. /* Sort if we'd overflow */
  1110. if (b->nsets + 1 == MAX_BSETS) {
  1111. bch_btree_sort(b, state);
  1112. return;
  1113. }
  1114. out:
  1115. bch_bset_build_written_tree(b);
  1116. }
  1117. EXPORT_SYMBOL(bch_btree_sort_lazy);
  1118. void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *stats)
  1119. {
  1120. unsigned int i;
  1121. for (i = 0; i <= b->nsets; i++) {
  1122. struct bset_tree *t = &b->set[i];
  1123. size_t bytes = t->data->keys * sizeof(uint64_t);
  1124. size_t j;
  1125. if (bset_written(b, t)) {
  1126. stats->sets_written++;
  1127. stats->bytes_written += bytes;
  1128. stats->floats += t->size - 1;
  1129. for (j = 1; j < t->size; j++)
  1130. if (t->tree[j].exponent == 127)
  1131. stats->failed++;
  1132. } else {
  1133. stats->sets_unwritten++;
  1134. stats->bytes_unwritten += bytes;
  1135. }
  1136. }
  1137. }