bset.c 27 KB

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