bset.c 27 KB

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