bset.c 27 KB

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