bset.c 27 KB

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