bset.c 33 KB

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