bset.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * Code for working with individual keys, and sorted sets of keys with in a
  3. * btree node
  4. *
  5. * Copyright 2012 Google, Inc.
  6. */
  7. #include "bcache.h"
  8. #include "btree.h"
  9. #include "debug.h"
  10. #include <linux/random.h>
  11. #include <linux/prefetch.h>
  12. /* Keylists */
  13. int bch_keylist_realloc(struct keylist *l, int nptrs, struct cache_set *c)
  14. {
  15. size_t oldsize = bch_keylist_nkeys(l);
  16. size_t newsize = oldsize + 2 + nptrs;
  17. uint64_t *old_keys = l->keys_p == l->inline_keys ? NULL : l->keys_p;
  18. uint64_t *new_keys;
  19. /* The journalling code doesn't handle the case where the keys to insert
  20. * is bigger than an empty write: If we just return -ENOMEM here,
  21. * bio_insert() and bio_invalidate() will insert the keys created so far
  22. * and finish the rest when the keylist is empty.
  23. */
  24. if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
  25. return -ENOMEM;
  26. newsize = roundup_pow_of_two(newsize);
  27. if (newsize <= KEYLIST_INLINE ||
  28. roundup_pow_of_two(oldsize) == newsize)
  29. return 0;
  30. new_keys = krealloc(old_keys, sizeof(uint64_t) * newsize, GFP_NOIO);
  31. if (!new_keys)
  32. return -ENOMEM;
  33. if (!old_keys)
  34. memcpy(new_keys, l->inline_keys, sizeof(uint64_t) * oldsize);
  35. l->keys_p = new_keys;
  36. l->top_p = new_keys + oldsize;
  37. return 0;
  38. }
  39. struct bkey *bch_keylist_pop(struct keylist *l)
  40. {
  41. struct bkey *k = l->keys;
  42. if (k == l->top)
  43. return NULL;
  44. while (bkey_next(k) != l->top)
  45. k = bkey_next(k);
  46. return l->top = k;
  47. }
  48. void bch_keylist_pop_front(struct keylist *l)
  49. {
  50. l->top_p -= bkey_u64s(l->keys);
  51. memmove(l->keys,
  52. bkey_next(l->keys),
  53. bch_keylist_bytes(l));
  54. }
  55. /* Pointer validation */
  56. static bool __ptr_invalid(struct cache_set *c, const struct bkey *k)
  57. {
  58. unsigned i;
  59. for (i = 0; i < KEY_PTRS(k); i++)
  60. if (ptr_available(c, k, i)) {
  61. struct cache *ca = PTR_CACHE(c, k, i);
  62. size_t bucket = PTR_BUCKET_NR(c, k, i);
  63. size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
  64. if (KEY_SIZE(k) + r > c->sb.bucket_size ||
  65. bucket < ca->sb.first_bucket ||
  66. bucket >= ca->sb.nbuckets)
  67. return true;
  68. }
  69. return false;
  70. }
  71. bool bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
  72. {
  73. char buf[80];
  74. if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
  75. goto bad;
  76. if (__ptr_invalid(c, k))
  77. goto bad;
  78. return false;
  79. bad:
  80. bch_bkey_to_text(buf, sizeof(buf), k);
  81. cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k));
  82. return true;
  83. }
  84. bool bch_extent_ptr_invalid(struct cache_set *c, const struct bkey *k)
  85. {
  86. char buf[80];
  87. if (!KEY_SIZE(k))
  88. return true;
  89. if (KEY_SIZE(k) > KEY_OFFSET(k))
  90. goto bad;
  91. if (__ptr_invalid(c, k))
  92. goto bad;
  93. return false;
  94. bad:
  95. bch_bkey_to_text(buf, sizeof(buf), k);
  96. cache_bug(c, "spotted extent %s: %s", buf, bch_ptr_status(c, k));
  97. return true;
  98. }
  99. static bool ptr_bad_expensive_checks(struct btree *b, const struct bkey *k,
  100. unsigned ptr)
  101. {
  102. struct bucket *g = PTR_BUCKET(b->c, k, ptr);
  103. char buf[80];
  104. if (mutex_trylock(&b->c->bucket_lock)) {
  105. if (b->level) {
  106. if (KEY_DIRTY(k) ||
  107. g->prio != BTREE_PRIO ||
  108. (b->c->gc_mark_valid &&
  109. GC_MARK(g) != GC_MARK_METADATA))
  110. goto err;
  111. } else {
  112. if (g->prio == BTREE_PRIO)
  113. goto err;
  114. if (KEY_DIRTY(k) &&
  115. b->c->gc_mark_valid &&
  116. GC_MARK(g) != GC_MARK_DIRTY)
  117. goto err;
  118. }
  119. mutex_unlock(&b->c->bucket_lock);
  120. }
  121. return false;
  122. err:
  123. mutex_unlock(&b->c->bucket_lock);
  124. bch_bkey_to_text(buf, sizeof(buf), k);
  125. btree_bug(b,
  126. "inconsistent pointer %s: bucket %zu pin %i prio %i gen %i last_gc %i mark %llu gc_gen %i",
  127. buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
  128. g->prio, g->gen, g->last_gc, GC_MARK(g), g->gc_gen);
  129. return true;
  130. }
  131. bool bch_ptr_bad(struct btree *b, const struct bkey *k)
  132. {
  133. struct bucket *g;
  134. unsigned i, stale;
  135. if (!bkey_cmp(k, &ZERO_KEY) ||
  136. !KEY_PTRS(k) ||
  137. bch_ptr_invalid(b, k))
  138. return true;
  139. for (i = 0; i < KEY_PTRS(k); i++) {
  140. if (!ptr_available(b->c, k, i))
  141. return true;
  142. g = PTR_BUCKET(b->c, k, i);
  143. stale = ptr_stale(b->c, k, i);
  144. btree_bug_on(stale > 96, b,
  145. "key too stale: %i, need_gc %u",
  146. stale, b->c->need_gc);
  147. btree_bug_on(stale && KEY_DIRTY(k) && KEY_SIZE(k),
  148. b, "stale dirty pointer");
  149. if (stale)
  150. return true;
  151. if (expensive_debug_checks(b->c) &&
  152. ptr_bad_expensive_checks(b, k, i))
  153. return true;
  154. }
  155. return false;
  156. }
  157. /* Key/pointer manipulation */
  158. void bch_bkey_copy_single_ptr(struct bkey *dest, const struct bkey *src,
  159. unsigned i)
  160. {
  161. BUG_ON(i > KEY_PTRS(src));
  162. /* Only copy the header, key, and one pointer. */
  163. memcpy(dest, src, 2 * sizeof(uint64_t));
  164. dest->ptr[0] = src->ptr[i];
  165. SET_KEY_PTRS(dest, 1);
  166. /* We didn't copy the checksum so clear that bit. */
  167. SET_KEY_CSUM(dest, 0);
  168. }
  169. bool __bch_cut_front(const struct bkey *where, struct bkey *k)
  170. {
  171. unsigned i, len = 0;
  172. if (bkey_cmp(where, &START_KEY(k)) <= 0)
  173. return false;
  174. if (bkey_cmp(where, k) < 0)
  175. len = KEY_OFFSET(k) - KEY_OFFSET(where);
  176. else
  177. bkey_copy_key(k, where);
  178. for (i = 0; i < KEY_PTRS(k); i++)
  179. SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + KEY_SIZE(k) - len);
  180. BUG_ON(len > KEY_SIZE(k));
  181. SET_KEY_SIZE(k, len);
  182. return true;
  183. }
  184. bool __bch_cut_back(const struct bkey *where, struct bkey *k)
  185. {
  186. unsigned len = 0;
  187. if (bkey_cmp(where, k) >= 0)
  188. return false;
  189. BUG_ON(KEY_INODE(where) != KEY_INODE(k));
  190. if (bkey_cmp(where, &START_KEY(k)) > 0)
  191. len = KEY_OFFSET(where) - KEY_START(k);
  192. bkey_copy_key(k, where);
  193. BUG_ON(len > KEY_SIZE(k));
  194. SET_KEY_SIZE(k, len);
  195. return true;
  196. }
  197. static uint64_t merge_chksums(struct bkey *l, struct bkey *r)
  198. {
  199. return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
  200. ~((uint64_t)1 << 63);
  201. }
  202. /* Tries to merge l and r: l should be lower than r
  203. * Returns true if we were able to merge. If we did merge, l will be the merged
  204. * key, r will be untouched.
  205. */
  206. bool bch_bkey_try_merge(struct btree *b, struct bkey *l, struct bkey *r)
  207. {
  208. unsigned i;
  209. if (key_merging_disabled(b->c))
  210. return false;
  211. if (KEY_PTRS(l) != KEY_PTRS(r) ||
  212. KEY_DIRTY(l) != KEY_DIRTY(r) ||
  213. bkey_cmp(l, &START_KEY(r)))
  214. return false;
  215. for (i = 0; i < KEY_PTRS(l); i++)
  216. if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
  217. PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
  218. return false;
  219. /* Keys with no pointers aren't restricted to one bucket and could
  220. * overflow KEY_SIZE
  221. */
  222. if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
  223. SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
  224. SET_KEY_SIZE(l, USHRT_MAX);
  225. bch_cut_front(l, r);
  226. return false;
  227. }
  228. if (KEY_CSUM(l)) {
  229. if (KEY_CSUM(r))
  230. l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
  231. else
  232. SET_KEY_CSUM(l, 0);
  233. }
  234. SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
  235. SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
  236. return true;
  237. }
  238. /* Binary tree stuff for auxiliary search trees */
  239. static unsigned inorder_next(unsigned j, unsigned size)
  240. {
  241. if (j * 2 + 1 < size) {
  242. j = j * 2 + 1;
  243. while (j * 2 < size)
  244. j *= 2;
  245. } else
  246. j >>= ffz(j) + 1;
  247. return j;
  248. }
  249. static unsigned inorder_prev(unsigned j, unsigned size)
  250. {
  251. if (j * 2 < size) {
  252. j = j * 2;
  253. while (j * 2 + 1 < size)
  254. j = j * 2 + 1;
  255. } else
  256. j >>= ffs(j);
  257. return j;
  258. }
  259. /* I have no idea why this code works... and I'm the one who wrote it
  260. *
  261. * However, I do know what it does:
  262. * Given a binary tree constructed in an array (i.e. how you normally implement
  263. * a heap), it converts a node in the tree - referenced by array index - to the
  264. * index it would have if you did an inorder traversal.
  265. *
  266. * Also tested for every j, size up to size somewhere around 6 million.
  267. *
  268. * The binary tree starts at array index 1, not 0
  269. * extra is a function of size:
  270. * extra = (size - rounddown_pow_of_two(size - 1)) << 1;
  271. */
  272. static unsigned __to_inorder(unsigned j, unsigned size, unsigned extra)
  273. {
  274. unsigned b = fls(j);
  275. unsigned shift = fls(size - 1) - b;
  276. j ^= 1U << (b - 1);
  277. j <<= 1;
  278. j |= 1;
  279. j <<= shift;
  280. if (j > extra)
  281. j -= (j - extra) >> 1;
  282. return j;
  283. }
  284. static unsigned to_inorder(unsigned j, struct bset_tree *t)
  285. {
  286. return __to_inorder(j, t->size, t->extra);
  287. }
  288. static unsigned __inorder_to_tree(unsigned j, unsigned size, unsigned extra)
  289. {
  290. unsigned shift;
  291. if (j > extra)
  292. j += j - extra;
  293. shift = ffs(j);
  294. j >>= shift;
  295. j |= roundup_pow_of_two(size) >> shift;
  296. return j;
  297. }
  298. static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
  299. {
  300. return __inorder_to_tree(j, t->size, t->extra);
  301. }
  302. #if 0
  303. void inorder_test(void)
  304. {
  305. unsigned long done = 0;
  306. ktime_t start = ktime_get();
  307. for (unsigned size = 2;
  308. size < 65536000;
  309. size++) {
  310. unsigned extra = (size - rounddown_pow_of_two(size - 1)) << 1;
  311. unsigned i = 1, j = rounddown_pow_of_two(size - 1);
  312. if (!(size % 4096))
  313. printk(KERN_NOTICE "loop %u, %llu per us\n", size,
  314. done / ktime_us_delta(ktime_get(), start));
  315. while (1) {
  316. if (__inorder_to_tree(i, size, extra) != j)
  317. panic("size %10u j %10u i %10u", size, j, i);
  318. if (__to_inorder(j, size, extra) != i)
  319. panic("size %10u j %10u i %10u", size, j, i);
  320. if (j == rounddown_pow_of_two(size) - 1)
  321. break;
  322. BUG_ON(inorder_prev(inorder_next(j, size), size) != j);
  323. j = inorder_next(j, size);
  324. i++;
  325. }
  326. done += size - 1;
  327. }
  328. }
  329. #endif
  330. /*
  331. * Cacheline/offset <-> bkey pointer arithmetic:
  332. *
  333. * t->tree is a binary search tree in an array; each node corresponds to a key
  334. * in one cacheline in t->set (BSET_CACHELINE bytes).
  335. *
  336. * This means we don't have to store the full index of the key that a node in
  337. * the binary tree points to; to_inorder() gives us the cacheline, and then
  338. * bkey_float->m gives us the offset within that cacheline, in units of 8 bytes.
  339. *
  340. * cacheline_to_bkey() and friends abstract out all the pointer arithmetic to
  341. * make this work.
  342. *
  343. * To construct the bfloat for an arbitrary key we need to know what the key
  344. * immediately preceding it is: we have to check if the two keys differ in the
  345. * bits we're going to store in bkey_float->mantissa. t->prev[j] stores the size
  346. * of the previous key so we can walk backwards to it from t->tree[j]'s key.
  347. */
  348. static struct bkey *cacheline_to_bkey(struct bset_tree *t, unsigned cacheline,
  349. unsigned offset)
  350. {
  351. return ((void *) t->data) + cacheline * BSET_CACHELINE + offset * 8;
  352. }
  353. static unsigned bkey_to_cacheline(struct bset_tree *t, struct bkey *k)
  354. {
  355. return ((void *) k - (void *) t->data) / BSET_CACHELINE;
  356. }
  357. static unsigned bkey_to_cacheline_offset(struct bkey *k)
  358. {
  359. return ((size_t) k & (BSET_CACHELINE - 1)) / sizeof(uint64_t);
  360. }
  361. static struct bkey *tree_to_bkey(struct bset_tree *t, unsigned j)
  362. {
  363. return cacheline_to_bkey(t, to_inorder(j, t), t->tree[j].m);
  364. }
  365. static struct bkey *tree_to_prev_bkey(struct bset_tree *t, unsigned j)
  366. {
  367. return (void *) (((uint64_t *) tree_to_bkey(t, j)) - t->prev[j]);
  368. }
  369. /*
  370. * For the write set - the one we're currently inserting keys into - we don't
  371. * maintain a full search tree, we just keep a simple lookup table in t->prev.
  372. */
  373. static struct bkey *table_to_bkey(struct bset_tree *t, unsigned cacheline)
  374. {
  375. return cacheline_to_bkey(t, cacheline, t->prev[cacheline]);
  376. }
  377. static inline uint64_t shrd128(uint64_t high, uint64_t low, uint8_t shift)
  378. {
  379. #ifdef CONFIG_X86_64
  380. asm("shrd %[shift],%[high],%[low]"
  381. : [low] "+Rm" (low)
  382. : [high] "R" (high),
  383. [shift] "ci" (shift)
  384. : "cc");
  385. #else
  386. low >>= shift;
  387. low |= (high << 1) << (63U - shift);
  388. #endif
  389. return low;
  390. }
  391. static inline unsigned bfloat_mantissa(const struct bkey *k,
  392. struct bkey_float *f)
  393. {
  394. const uint64_t *p = &k->low - (f->exponent >> 6);
  395. return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
  396. }
  397. static void make_bfloat(struct bset_tree *t, unsigned j)
  398. {
  399. struct bkey_float *f = &t->tree[j];
  400. struct bkey *m = tree_to_bkey(t, j);
  401. struct bkey *p = tree_to_prev_bkey(t, j);
  402. struct bkey *l = is_power_of_2(j)
  403. ? t->data->start
  404. : tree_to_prev_bkey(t, j >> ffs(j));
  405. struct bkey *r = is_power_of_2(j + 1)
  406. ? node(t->data, t->data->keys - bkey_u64s(&t->end))
  407. : tree_to_bkey(t, j >> (ffz(j) + 1));
  408. BUG_ON(m < l || m > r);
  409. BUG_ON(bkey_next(p) != m);
  410. if (KEY_INODE(l) != KEY_INODE(r))
  411. f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
  412. else
  413. f->exponent = fls64(r->low ^ l->low);
  414. f->exponent = max_t(int, f->exponent - BKEY_MANTISSA_BITS, 0);
  415. /*
  416. * Setting f->exponent = 127 flags this node as failed, and causes the
  417. * lookup code to fall back to comparing against the original key.
  418. */
  419. if (bfloat_mantissa(m, f) != bfloat_mantissa(p, f))
  420. f->mantissa = bfloat_mantissa(m, f) - 1;
  421. else
  422. f->exponent = 127;
  423. }
  424. static void bset_alloc_tree(struct btree *b, struct bset_tree *t)
  425. {
  426. if (t != b->sets) {
  427. unsigned j = roundup(t[-1].size,
  428. 64 / sizeof(struct bkey_float));
  429. t->tree = t[-1].tree + j;
  430. t->prev = t[-1].prev + j;
  431. }
  432. while (t < b->sets + MAX_BSETS)
  433. t++->size = 0;
  434. }
  435. static void bset_build_unwritten_tree(struct btree *b)
  436. {
  437. struct bset_tree *t = b->sets + b->nsets;
  438. bset_alloc_tree(b, t);
  439. if (t->tree != b->sets->tree + bset_tree_space(b)) {
  440. t->prev[0] = bkey_to_cacheline_offset(t->data->start);
  441. t->size = 1;
  442. }
  443. }
  444. static void bset_build_written_tree(struct btree *b)
  445. {
  446. struct bset_tree *t = b->sets + b->nsets;
  447. struct bkey *k = t->data->start;
  448. unsigned j, cacheline = 1;
  449. bset_alloc_tree(b, t);
  450. t->size = min_t(unsigned,
  451. bkey_to_cacheline(t, end(t->data)),
  452. b->sets->tree + bset_tree_space(b) - t->tree);
  453. if (t->size < 2) {
  454. t->size = 0;
  455. return;
  456. }
  457. t->extra = (t->size - rounddown_pow_of_two(t->size - 1)) << 1;
  458. /* First we figure out where the first key in each cacheline is */
  459. for (j = inorder_next(0, t->size);
  460. j;
  461. j = inorder_next(j, t->size)) {
  462. while (bkey_to_cacheline(t, k) != cacheline)
  463. k = bkey_next(k);
  464. t->prev[j] = bkey_u64s(k);
  465. k = bkey_next(k);
  466. cacheline++;
  467. t->tree[j].m = bkey_to_cacheline_offset(k);
  468. }
  469. while (bkey_next(k) != end(t->data))
  470. k = bkey_next(k);
  471. t->end = *k;
  472. /* Then we build the tree */
  473. for (j = inorder_next(0, t->size);
  474. j;
  475. j = inorder_next(j, t->size))
  476. make_bfloat(t, j);
  477. }
  478. void bch_bset_fix_invalidated_key(struct btree *b, struct bkey *k)
  479. {
  480. struct bset_tree *t;
  481. unsigned inorder, j = 1;
  482. for (t = b->sets; t <= &b->sets[b->nsets]; t++)
  483. if (k < end(t->data))
  484. goto found_set;
  485. BUG();
  486. found_set:
  487. if (!t->size || !bset_written(b, t))
  488. return;
  489. inorder = bkey_to_cacheline(t, k);
  490. if (k == t->data->start)
  491. goto fix_left;
  492. if (bkey_next(k) == end(t->data)) {
  493. t->end = *k;
  494. goto fix_right;
  495. }
  496. j = inorder_to_tree(inorder, t);
  497. if (j &&
  498. j < t->size &&
  499. k == tree_to_bkey(t, j))
  500. fix_left: do {
  501. make_bfloat(t, j);
  502. j = j * 2;
  503. } while (j < t->size);
  504. j = inorder_to_tree(inorder + 1, t);
  505. if (j &&
  506. j < t->size &&
  507. k == tree_to_prev_bkey(t, j))
  508. fix_right: do {
  509. make_bfloat(t, j);
  510. j = j * 2 + 1;
  511. } while (j < t->size);
  512. }
  513. void bch_bset_fix_lookup_table(struct btree *b, struct bkey *k)
  514. {
  515. struct bset_tree *t = &b->sets[b->nsets];
  516. unsigned shift = bkey_u64s(k);
  517. unsigned j = bkey_to_cacheline(t, k);
  518. /* We're getting called from btree_split() or btree_gc, just bail out */
  519. if (!t->size)
  520. return;
  521. /* k is the key we just inserted; we need to find the entry in the
  522. * lookup table for the first key that is strictly greater than k:
  523. * it's either k's cacheline or the next one
  524. */
  525. if (j < t->size &&
  526. table_to_bkey(t, j) <= k)
  527. j++;
  528. /* Adjust all the lookup table entries, and find a new key for any that
  529. * have gotten too big
  530. */
  531. for (; j < t->size; j++) {
  532. t->prev[j] += shift;
  533. if (t->prev[j] > 7) {
  534. k = table_to_bkey(t, j - 1);
  535. while (k < cacheline_to_bkey(t, j, 0))
  536. k = bkey_next(k);
  537. t->prev[j] = bkey_to_cacheline_offset(k);
  538. }
  539. }
  540. if (t->size == b->sets->tree + bset_tree_space(b) - t->tree)
  541. return;
  542. /* Possibly add a new entry to the end of the lookup table */
  543. for (k = table_to_bkey(t, t->size - 1);
  544. k != end(t->data);
  545. k = bkey_next(k))
  546. if (t->size == bkey_to_cacheline(t, k)) {
  547. t->prev[t->size] = bkey_to_cacheline_offset(k);
  548. t->size++;
  549. }
  550. }
  551. void bch_bset_init_next(struct btree *b)
  552. {
  553. struct bset *i = write_block(b);
  554. if (i != b->sets[0].data) {
  555. b->sets[++b->nsets].data = i;
  556. i->seq = b->sets[0].data->seq;
  557. } else
  558. get_random_bytes(&i->seq, sizeof(uint64_t));
  559. i->magic = bset_magic(&b->c->sb);
  560. i->version = 0;
  561. i->keys = 0;
  562. bset_build_unwritten_tree(b);
  563. }
  564. struct bset_search_iter {
  565. struct bkey *l, *r;
  566. };
  567. static struct bset_search_iter bset_search_write_set(struct btree *b,
  568. struct bset_tree *t,
  569. const struct bkey *search)
  570. {
  571. unsigned li = 0, ri = t->size;
  572. BUG_ON(!b->nsets &&
  573. t->size < bkey_to_cacheline(t, end(t->data)));
  574. while (li + 1 != ri) {
  575. unsigned m = (li + ri) >> 1;
  576. if (bkey_cmp(table_to_bkey(t, m), search) > 0)
  577. ri = m;
  578. else
  579. li = m;
  580. }
  581. return (struct bset_search_iter) {
  582. table_to_bkey(t, li),
  583. ri < t->size ? table_to_bkey(t, ri) : end(t->data)
  584. };
  585. }
  586. static struct bset_search_iter bset_search_tree(struct btree *b,
  587. struct bset_tree *t,
  588. const struct bkey *search)
  589. {
  590. struct bkey *l, *r;
  591. struct bkey_float *f;
  592. unsigned inorder, j, n = 1;
  593. do {
  594. unsigned p = n << 4;
  595. p &= ((int) (p - t->size)) >> 31;
  596. prefetch(&t->tree[p]);
  597. j = n;
  598. f = &t->tree[j];
  599. /*
  600. * n = (f->mantissa > bfloat_mantissa())
  601. * ? j * 2
  602. * : j * 2 + 1;
  603. *
  604. * We need to subtract 1 from f->mantissa for the sign bit trick
  605. * to work - that's done in make_bfloat()
  606. */
  607. if (likely(f->exponent != 127))
  608. n = j * 2 + (((unsigned)
  609. (f->mantissa -
  610. bfloat_mantissa(search, f))) >> 31);
  611. else
  612. n = (bkey_cmp(tree_to_bkey(t, j), search) > 0)
  613. ? j * 2
  614. : j * 2 + 1;
  615. } while (n < t->size);
  616. inorder = to_inorder(j, t);
  617. /*
  618. * n would have been the node we recursed to - the low bit tells us if
  619. * we recursed left or recursed right.
  620. */
  621. if (n & 1) {
  622. l = cacheline_to_bkey(t, inorder, f->m);
  623. if (++inorder != t->size) {
  624. f = &t->tree[inorder_next(j, t->size)];
  625. r = cacheline_to_bkey(t, inorder, f->m);
  626. } else
  627. r = end(t->data);
  628. } else {
  629. r = cacheline_to_bkey(t, inorder, f->m);
  630. if (--inorder) {
  631. f = &t->tree[inorder_prev(j, t->size)];
  632. l = cacheline_to_bkey(t, inorder, f->m);
  633. } else
  634. l = t->data->start;
  635. }
  636. return (struct bset_search_iter) {l, r};
  637. }
  638. struct bkey *__bch_bset_search(struct btree *b, struct bset_tree *t,
  639. const struct bkey *search)
  640. {
  641. struct bset_search_iter i;
  642. /*
  643. * First, we search for a cacheline, then lastly we do a linear search
  644. * within that cacheline.
  645. *
  646. * To search for the cacheline, there's three different possibilities:
  647. * * The set is too small to have a search tree, so we just do a linear
  648. * search over the whole set.
  649. * * The set is the one we're currently inserting into; keeping a full
  650. * auxiliary search tree up to date would be too expensive, so we
  651. * use a much simpler lookup table to do a binary search -
  652. * bset_search_write_set().
  653. * * Or we use the auxiliary search tree we constructed earlier -
  654. * bset_search_tree()
  655. */
  656. if (unlikely(!t->size)) {
  657. i.l = t->data->start;
  658. i.r = end(t->data);
  659. } else if (bset_written(b, t)) {
  660. /*
  661. * Each node in the auxiliary search tree covers a certain range
  662. * of bits, and keys above and below the set it covers might
  663. * differ outside those bits - so we have to special case the
  664. * start and end - handle that here:
  665. */
  666. if (unlikely(bkey_cmp(search, &t->end) >= 0))
  667. return end(t->data);
  668. if (unlikely(bkey_cmp(search, t->data->start) < 0))
  669. return t->data->start;
  670. i = bset_search_tree(b, t, search);
  671. } else
  672. i = bset_search_write_set(b, t, search);
  673. if (expensive_debug_checks(b->c)) {
  674. BUG_ON(bset_written(b, t) &&
  675. i.l != t->data->start &&
  676. bkey_cmp(tree_to_prev_bkey(t,
  677. inorder_to_tree(bkey_to_cacheline(t, i.l), t)),
  678. search) > 0);
  679. BUG_ON(i.r != end(t->data) &&
  680. bkey_cmp(i.r, search) <= 0);
  681. }
  682. while (likely(i.l != i.r) &&
  683. bkey_cmp(i.l, search) <= 0)
  684. i.l = bkey_next(i.l);
  685. return i.l;
  686. }
  687. /* Btree iterator */
  688. /*
  689. * Returns true if l > r - unless l == r, in which case returns true if l is
  690. * older than r.
  691. *
  692. * Necessary for btree_sort_fixup() - if there are multiple keys that compare
  693. * equal in different sets, we have to process them newest to oldest.
  694. */
  695. static inline bool btree_iter_cmp(struct btree_iter_set l,
  696. struct btree_iter_set r)
  697. {
  698. int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
  699. return c ? c > 0 : l.k < r.k;
  700. }
  701. static inline bool btree_iter_end(struct btree_iter *iter)
  702. {
  703. return !iter->used;
  704. }
  705. void bch_btree_iter_push(struct btree_iter *iter, struct bkey *k,
  706. struct bkey *end)
  707. {
  708. if (k != end)
  709. BUG_ON(!heap_add(iter,
  710. ((struct btree_iter_set) { k, end }),
  711. btree_iter_cmp));
  712. }
  713. struct bkey *__bch_btree_iter_init(struct btree *b, struct btree_iter *iter,
  714. struct bkey *search, struct bset_tree *start)
  715. {
  716. struct bkey *ret = NULL;
  717. iter->size = ARRAY_SIZE(iter->data);
  718. iter->used = 0;
  719. #ifdef CONFIG_BCACHE_DEBUG
  720. iter->b = b;
  721. #endif
  722. for (; start <= &b->sets[b->nsets]; start++) {
  723. ret = bch_bset_search(b, start, search);
  724. bch_btree_iter_push(iter, ret, end(start->data));
  725. }
  726. return ret;
  727. }
  728. struct bkey *bch_btree_iter_next(struct btree_iter *iter)
  729. {
  730. struct btree_iter_set unused;
  731. struct bkey *ret = NULL;
  732. if (!btree_iter_end(iter)) {
  733. bch_btree_iter_next_check(iter);
  734. ret = iter->data->k;
  735. iter->data->k = bkey_next(iter->data->k);
  736. if (iter->data->k > iter->data->end) {
  737. WARN_ONCE(1, "bset was corrupt!\n");
  738. iter->data->k = iter->data->end;
  739. }
  740. if (iter->data->k == iter->data->end)
  741. heap_pop(iter, unused, btree_iter_cmp);
  742. else
  743. heap_sift(iter, 0, btree_iter_cmp);
  744. }
  745. return ret;
  746. }
  747. struct bkey *bch_btree_iter_next_filter(struct btree_iter *iter,
  748. struct btree *b, ptr_filter_fn fn)
  749. {
  750. struct bkey *ret;
  751. do {
  752. ret = bch_btree_iter_next(iter);
  753. } while (ret && fn(b, ret));
  754. return ret;
  755. }
  756. /* Mergesort */
  757. static void sort_key_next(struct btree_iter *iter,
  758. struct btree_iter_set *i)
  759. {
  760. i->k = bkey_next(i->k);
  761. if (i->k == i->end)
  762. *i = iter->data[--iter->used];
  763. }
  764. static void btree_sort_fixup(struct btree_iter *iter)
  765. {
  766. while (iter->used > 1) {
  767. struct btree_iter_set *top = iter->data, *i = top + 1;
  768. if (iter->used > 2 &&
  769. btree_iter_cmp(i[0], i[1]))
  770. i++;
  771. if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
  772. break;
  773. if (!KEY_SIZE(i->k)) {
  774. sort_key_next(iter, i);
  775. heap_sift(iter, i - top, btree_iter_cmp);
  776. continue;
  777. }
  778. if (top->k > i->k) {
  779. if (bkey_cmp(top->k, i->k) >= 0)
  780. sort_key_next(iter, i);
  781. else
  782. bch_cut_front(top->k, i->k);
  783. heap_sift(iter, i - top, btree_iter_cmp);
  784. } else {
  785. /* can't happen because of comparison func */
  786. BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
  787. bch_cut_back(&START_KEY(i->k), top->k);
  788. }
  789. }
  790. }
  791. static void btree_mergesort(struct btree *b, struct bset *out,
  792. struct btree_iter *iter,
  793. bool fixup, bool remove_stale)
  794. {
  795. struct bkey *k, *last = NULL;
  796. bool (*bad)(struct btree *, const struct bkey *) = remove_stale
  797. ? bch_ptr_bad
  798. : bch_ptr_invalid;
  799. while (!btree_iter_end(iter)) {
  800. if (fixup && !b->level)
  801. btree_sort_fixup(iter);
  802. k = bch_btree_iter_next(iter);
  803. if (bad(b, k))
  804. continue;
  805. if (!last) {
  806. last = out->start;
  807. bkey_copy(last, k);
  808. } else if (b->level ||
  809. !bch_bkey_try_merge(b, last, k)) {
  810. last = bkey_next(last);
  811. bkey_copy(last, k);
  812. }
  813. }
  814. out->keys = last ? (uint64_t *) bkey_next(last) - out->d : 0;
  815. pr_debug("sorted %i keys", out->keys);
  816. }
  817. static void __btree_sort(struct btree *b, struct btree_iter *iter,
  818. unsigned start, unsigned order, bool fixup)
  819. {
  820. uint64_t start_time;
  821. bool remove_stale = !b->written;
  822. struct bset *out = (void *) __get_free_pages(__GFP_NOWARN|GFP_NOIO,
  823. order);
  824. if (!out) {
  825. mutex_lock(&b->c->sort_lock);
  826. out = b->c->sort;
  827. order = ilog2(bucket_pages(b->c));
  828. }
  829. start_time = local_clock();
  830. btree_mergesort(b, out, iter, fixup, remove_stale);
  831. b->nsets = start;
  832. if (!fixup && !start && b->written)
  833. bch_btree_verify(b, out);
  834. if (!start && order == b->page_order) {
  835. /*
  836. * Our temporary buffer is the same size as the btree node's
  837. * buffer, we can just swap buffers instead of doing a big
  838. * memcpy()
  839. */
  840. out->magic = bset_magic(&b->c->sb);
  841. out->seq = b->sets[0].data->seq;
  842. out->version = b->sets[0].data->version;
  843. swap(out, b->sets[0].data);
  844. if (b->c->sort == b->sets[0].data)
  845. b->c->sort = out;
  846. } else {
  847. b->sets[start].data->keys = out->keys;
  848. memcpy(b->sets[start].data->start, out->start,
  849. (void *) end(out) - (void *) out->start);
  850. }
  851. if (out == b->c->sort)
  852. mutex_unlock(&b->c->sort_lock);
  853. else
  854. free_pages((unsigned long) out, order);
  855. if (b->written)
  856. bset_build_written_tree(b);
  857. if (!start) {
  858. spin_lock(&b->c->sort_time_lock);
  859. bch_time_stats_update(&b->c->sort_time, start_time);
  860. spin_unlock(&b->c->sort_time_lock);
  861. }
  862. }
  863. void bch_btree_sort_partial(struct btree *b, unsigned start)
  864. {
  865. size_t order = b->page_order, keys = 0;
  866. struct btree_iter iter;
  867. int oldsize = bch_count_data(b);
  868. __bch_btree_iter_init(b, &iter, NULL, &b->sets[start]);
  869. BUG_ON(b->sets[b->nsets].data == write_block(b) &&
  870. (b->sets[b->nsets].size || b->nsets));
  871. if (start) {
  872. unsigned i;
  873. for (i = start; i <= b->nsets; i++)
  874. keys += b->sets[i].data->keys;
  875. order = roundup_pow_of_two(__set_bytes(b->sets->data,
  876. keys)) / PAGE_SIZE;
  877. if (order)
  878. order = ilog2(order);
  879. }
  880. __btree_sort(b, &iter, start, order, false);
  881. EBUG_ON(b->written && oldsize >= 0 && bch_count_data(b) != oldsize);
  882. }
  883. void bch_btree_sort_and_fix_extents(struct btree *b, struct btree_iter *iter)
  884. {
  885. BUG_ON(!b->written);
  886. __btree_sort(b, iter, 0, b->page_order, true);
  887. }
  888. void bch_btree_sort_into(struct btree *b, struct btree *new)
  889. {
  890. uint64_t start_time = local_clock();
  891. struct btree_iter iter;
  892. bch_btree_iter_init(b, &iter, NULL);
  893. btree_mergesort(b, new->sets->data, &iter, false, true);
  894. spin_lock(&b->c->sort_time_lock);
  895. bch_time_stats_update(&b->c->sort_time, start_time);
  896. spin_unlock(&b->c->sort_time_lock);
  897. bkey_copy_key(&new->key, &b->key);
  898. new->sets->size = 0;
  899. }
  900. #define SORT_CRIT (4096 / sizeof(uint64_t))
  901. void bch_btree_sort_lazy(struct btree *b)
  902. {
  903. unsigned crit = SORT_CRIT;
  904. int i;
  905. /* Don't sort if nothing to do */
  906. if (!b->nsets)
  907. goto out;
  908. /* If not a leaf node, always sort */
  909. if (b->level) {
  910. bch_btree_sort(b);
  911. return;
  912. }
  913. for (i = b->nsets - 1; i >= 0; --i) {
  914. crit *= b->c->sort_crit_factor;
  915. if (b->sets[i].data->keys < crit) {
  916. bch_btree_sort_partial(b, i);
  917. return;
  918. }
  919. }
  920. /* Sort if we'd overflow */
  921. if (b->nsets + 1 == MAX_BSETS) {
  922. bch_btree_sort(b);
  923. return;
  924. }
  925. out:
  926. bset_build_written_tree(b);
  927. }
  928. /* Sysfs stuff */
  929. struct bset_stats {
  930. struct btree_op op;
  931. size_t nodes;
  932. size_t sets_written, sets_unwritten;
  933. size_t bytes_written, bytes_unwritten;
  934. size_t floats, failed;
  935. };
  936. static int btree_bset_stats(struct btree_op *op, struct btree *b)
  937. {
  938. struct bset_stats *stats = container_of(op, struct bset_stats, op);
  939. unsigned i;
  940. stats->nodes++;
  941. for (i = 0; i <= b->nsets; i++) {
  942. struct bset_tree *t = &b->sets[i];
  943. size_t bytes = t->data->keys * sizeof(uint64_t);
  944. size_t j;
  945. if (bset_written(b, t)) {
  946. stats->sets_written++;
  947. stats->bytes_written += bytes;
  948. stats->floats += t->size - 1;
  949. for (j = 1; j < t->size; j++)
  950. if (t->tree[j].exponent == 127)
  951. stats->failed++;
  952. } else {
  953. stats->sets_unwritten++;
  954. stats->bytes_unwritten += bytes;
  955. }
  956. }
  957. return MAP_CONTINUE;
  958. }
  959. int bch_bset_print_stats(struct cache_set *c, char *buf)
  960. {
  961. struct bset_stats t;
  962. int ret;
  963. memset(&t, 0, sizeof(struct bset_stats));
  964. bch_btree_op_init(&t.op, -1);
  965. ret = bch_btree_map_nodes(&t.op, c, &ZERO_KEY, btree_bset_stats);
  966. if (ret < 0)
  967. return ret;
  968. return snprintf(buf, PAGE_SIZE,
  969. "btree nodes: %zu\n"
  970. "written sets: %zu\n"
  971. "unwritten sets: %zu\n"
  972. "written key bytes: %zu\n"
  973. "unwritten key bytes: %zu\n"
  974. "floats: %zu\n"
  975. "failed: %zu\n",
  976. t.nodes,
  977. t.sets_written, t.sets_unwritten,
  978. t.bytes_written, t.bytes_unwritten,
  979. t.floats, t.failed);
  980. }