bset.c 27 KB

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