bset.c 27 KB

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