journal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*
  2. * bcache journalling code, for btree insertions
  3. *
  4. * Copyright 2012 Google, Inc.
  5. */
  6. #include "bcache.h"
  7. #include "btree.h"
  8. #include "debug.h"
  9. #include "request.h"
  10. /*
  11. * Journal replay/recovery:
  12. *
  13. * This code is all driven from run_cache_set(); we first read the journal
  14. * entries, do some other stuff, then we mark all the keys in the journal
  15. * entries (same as garbage collection would), then we replay them - reinserting
  16. * them into the cache in precisely the same order as they appear in the
  17. * journal.
  18. *
  19. * We only journal keys that go in leaf nodes, which simplifies things quite a
  20. * bit.
  21. */
  22. static void journal_read_endio(struct bio *bio, int error)
  23. {
  24. struct closure *cl = bio->bi_private;
  25. closure_put(cl);
  26. }
  27. static int journal_read_bucket(struct cache *ca, struct list_head *list,
  28. struct btree_op *op, unsigned bucket_index)
  29. {
  30. struct journal_device *ja = &ca->journal;
  31. struct bio *bio = &ja->bio;
  32. struct journal_replay *i;
  33. struct jset *j, *data = ca->set->journal.w[0].data;
  34. unsigned len, left, offset = 0;
  35. int ret = 0;
  36. sector_t bucket = bucket_to_sector(ca->set, ca->sb.d[bucket_index]);
  37. pr_debug("reading %llu", (uint64_t) bucket);
  38. while (offset < ca->sb.bucket_size) {
  39. reread: left = ca->sb.bucket_size - offset;
  40. len = min_t(unsigned, left, PAGE_SECTORS * 8);
  41. bio_reset(bio);
  42. bio->bi_sector = bucket + offset;
  43. bio->bi_bdev = ca->bdev;
  44. bio->bi_rw = READ;
  45. bio->bi_size = len << 9;
  46. bio->bi_end_io = journal_read_endio;
  47. bio->bi_private = &op->cl;
  48. bch_bio_map(bio, data);
  49. closure_bio_submit(bio, &op->cl, ca);
  50. closure_sync(&op->cl);
  51. /* This function could be simpler now since we no longer write
  52. * journal entries that overlap bucket boundaries; this means
  53. * the start of a bucket will always have a valid journal entry
  54. * if it has any journal entries at all.
  55. */
  56. j = data;
  57. while (len) {
  58. struct list_head *where;
  59. size_t blocks, bytes = set_bytes(j);
  60. if (j->magic != jset_magic(ca->set))
  61. return ret;
  62. if (bytes > left << 9)
  63. return ret;
  64. if (bytes > len << 9)
  65. goto reread;
  66. if (j->csum != csum_set(j))
  67. return ret;
  68. blocks = set_blocks(j, ca->set);
  69. while (!list_empty(list)) {
  70. i = list_first_entry(list,
  71. struct journal_replay, list);
  72. if (i->j.seq >= j->last_seq)
  73. break;
  74. list_del(&i->list);
  75. kfree(i);
  76. }
  77. list_for_each_entry_reverse(i, list, list) {
  78. if (j->seq == i->j.seq)
  79. goto next_set;
  80. if (j->seq < i->j.last_seq)
  81. goto next_set;
  82. if (j->seq > i->j.seq) {
  83. where = &i->list;
  84. goto add;
  85. }
  86. }
  87. where = list;
  88. add:
  89. i = kmalloc(offsetof(struct journal_replay, j) +
  90. bytes, GFP_KERNEL);
  91. if (!i)
  92. return -ENOMEM;
  93. memcpy(&i->j, j, bytes);
  94. list_add(&i->list, where);
  95. ret = 1;
  96. ja->seq[bucket_index] = j->seq;
  97. next_set:
  98. offset += blocks * ca->sb.block_size;
  99. len -= blocks * ca->sb.block_size;
  100. j = ((void *) j) + blocks * block_bytes(ca);
  101. }
  102. }
  103. return ret;
  104. }
  105. int bch_journal_read(struct cache_set *c, struct list_head *list,
  106. struct btree_op *op)
  107. {
  108. #define read_bucket(b) \
  109. ({ \
  110. int ret = journal_read_bucket(ca, list, op, b); \
  111. __set_bit(b, bitmap); \
  112. if (ret < 0) \
  113. return ret; \
  114. ret; \
  115. })
  116. struct cache *ca;
  117. unsigned iter;
  118. for_each_cache(ca, c, iter) {
  119. struct journal_device *ja = &ca->journal;
  120. unsigned long bitmap[SB_JOURNAL_BUCKETS / BITS_PER_LONG];
  121. unsigned i, l, r, m;
  122. uint64_t seq;
  123. bitmap_zero(bitmap, SB_JOURNAL_BUCKETS);
  124. pr_debug("%u journal buckets", ca->sb.njournal_buckets);
  125. /* Read journal buckets ordered by golden ratio hash to quickly
  126. * find a sequence of buckets with valid journal entries
  127. */
  128. for (i = 0; i < ca->sb.njournal_buckets; i++) {
  129. l = (i * 2654435769U) % ca->sb.njournal_buckets;
  130. if (test_bit(l, bitmap))
  131. break;
  132. if (read_bucket(l))
  133. goto bsearch;
  134. }
  135. /* If that fails, check all the buckets we haven't checked
  136. * already
  137. */
  138. pr_debug("falling back to linear search");
  139. for (l = 0; l < ca->sb.njournal_buckets; l++) {
  140. if (test_bit(l, bitmap))
  141. continue;
  142. if (read_bucket(l))
  143. goto bsearch;
  144. }
  145. bsearch:
  146. /* Binary search */
  147. m = r = find_next_bit(bitmap, ca->sb.njournal_buckets, l + 1);
  148. pr_debug("starting binary search, l %u r %u", l, r);
  149. while (l + 1 < r) {
  150. m = (l + r) >> 1;
  151. if (read_bucket(m))
  152. l = m;
  153. else
  154. r = m;
  155. }
  156. /* Read buckets in reverse order until we stop finding more
  157. * journal entries
  158. */
  159. pr_debug("finishing up");
  160. l = m;
  161. while (1) {
  162. if (!l--)
  163. l = ca->sb.njournal_buckets - 1;
  164. if (l == m)
  165. break;
  166. if (test_bit(l, bitmap))
  167. continue;
  168. if (!read_bucket(l))
  169. break;
  170. }
  171. seq = 0;
  172. for (i = 0; i < ca->sb.njournal_buckets; i++)
  173. if (ja->seq[i] > seq) {
  174. seq = ja->seq[i];
  175. ja->cur_idx = ja->discard_idx =
  176. ja->last_idx = i;
  177. }
  178. }
  179. c->journal.seq = list_entry(list->prev,
  180. struct journal_replay,
  181. list)->j.seq;
  182. return 0;
  183. #undef read_bucket
  184. }
  185. void bch_journal_mark(struct cache_set *c, struct list_head *list)
  186. {
  187. atomic_t p = { 0 };
  188. struct bkey *k;
  189. struct journal_replay *i;
  190. struct journal *j = &c->journal;
  191. uint64_t last = j->seq;
  192. /*
  193. * journal.pin should never fill up - we never write a journal
  194. * entry when it would fill up. But if for some reason it does, we
  195. * iterate over the list in reverse order so that we can just skip that
  196. * refcount instead of bugging.
  197. */
  198. list_for_each_entry_reverse(i, list, list) {
  199. BUG_ON(last < i->j.seq);
  200. i->pin = NULL;
  201. while (last-- != i->j.seq)
  202. if (fifo_free(&j->pin) > 1) {
  203. fifo_push_front(&j->pin, p);
  204. atomic_set(&fifo_front(&j->pin), 0);
  205. }
  206. if (fifo_free(&j->pin) > 1) {
  207. fifo_push_front(&j->pin, p);
  208. i->pin = &fifo_front(&j->pin);
  209. atomic_set(i->pin, 1);
  210. }
  211. for (k = i->j.start;
  212. k < end(&i->j);
  213. k = bkey_next(k)) {
  214. unsigned j;
  215. for (j = 0; j < KEY_PTRS(k); j++) {
  216. struct bucket *g = PTR_BUCKET(c, k, j);
  217. atomic_inc(&g->pin);
  218. if (g->prio == BTREE_PRIO &&
  219. !ptr_stale(c, k, j))
  220. g->prio = INITIAL_PRIO;
  221. }
  222. __bch_btree_mark_key(c, 0, k);
  223. }
  224. }
  225. }
  226. int bch_journal_replay(struct cache_set *s, struct list_head *list,
  227. struct btree_op *op)
  228. {
  229. int ret = 0, keys = 0, entries = 0;
  230. struct bkey *k;
  231. struct journal_replay *i =
  232. list_entry(list->prev, struct journal_replay, list);
  233. uint64_t start = i->j.last_seq, end = i->j.seq, n = start;
  234. list_for_each_entry(i, list, list) {
  235. BUG_ON(i->pin && atomic_read(i->pin) != 1);
  236. if (n != i->j.seq)
  237. pr_err(
  238. "journal entries %llu-%llu missing! (replaying %llu-%llu)\n",
  239. n, i->j.seq - 1, start, end);
  240. for (k = i->j.start;
  241. k < end(&i->j);
  242. k = bkey_next(k)) {
  243. pr_debug("%s", pkey(k));
  244. bkey_copy(op->keys.top, k);
  245. bch_keylist_push(&op->keys);
  246. op->journal = i->pin;
  247. atomic_inc(op->journal);
  248. ret = bch_btree_insert(op, s);
  249. if (ret)
  250. goto err;
  251. BUG_ON(!bch_keylist_empty(&op->keys));
  252. keys++;
  253. cond_resched();
  254. }
  255. if (i->pin)
  256. atomic_dec(i->pin);
  257. n = i->j.seq + 1;
  258. entries++;
  259. }
  260. pr_info("journal replay done, %i keys in %i entries, seq %llu",
  261. keys, entries, end);
  262. while (!list_empty(list)) {
  263. i = list_first_entry(list, struct journal_replay, list);
  264. list_del(&i->list);
  265. kfree(i);
  266. }
  267. err:
  268. closure_sync(&op->cl);
  269. return ret;
  270. }
  271. /* Journalling */
  272. static void btree_flush_write(struct cache_set *c)
  273. {
  274. /*
  275. * Try to find the btree node with that references the oldest journal
  276. * entry, best is our current candidate and is locked if non NULL:
  277. */
  278. struct btree *b, *best = NULL;
  279. unsigned iter;
  280. for_each_cached_btree(b, c, iter) {
  281. if (!down_write_trylock(&b->lock))
  282. continue;
  283. if (!btree_node_dirty(b) ||
  284. !btree_current_write(b)->journal) {
  285. rw_unlock(true, b);
  286. continue;
  287. }
  288. if (!best)
  289. best = b;
  290. else if (journal_pin_cmp(c,
  291. btree_current_write(best),
  292. btree_current_write(b))) {
  293. rw_unlock(true, best);
  294. best = b;
  295. } else
  296. rw_unlock(true, b);
  297. }
  298. if (best)
  299. goto out;
  300. /* We can't find the best btree node, just pick the first */
  301. list_for_each_entry(b, &c->btree_cache, list)
  302. if (!b->level && btree_node_dirty(b)) {
  303. best = b;
  304. rw_lock(true, best, best->level);
  305. goto found;
  306. }
  307. out:
  308. if (!best)
  309. return;
  310. found:
  311. if (btree_node_dirty(best))
  312. bch_btree_write(best, true, NULL);
  313. rw_unlock(true, best);
  314. }
  315. #define last_seq(j) ((j)->seq - fifo_used(&(j)->pin) + 1)
  316. static void journal_discard_endio(struct bio *bio, int error)
  317. {
  318. struct journal_device *ja =
  319. container_of(bio, struct journal_device, discard_bio);
  320. struct cache *ca = container_of(ja, struct cache, journal);
  321. atomic_set(&ja->discard_in_flight, DISCARD_DONE);
  322. closure_wake_up(&ca->set->journal.wait);
  323. closure_put(&ca->set->cl);
  324. }
  325. static void journal_discard_work(struct work_struct *work)
  326. {
  327. struct journal_device *ja =
  328. container_of(work, struct journal_device, discard_work);
  329. submit_bio(0, &ja->discard_bio);
  330. }
  331. static void do_journal_discard(struct cache *ca)
  332. {
  333. struct journal_device *ja = &ca->journal;
  334. struct bio *bio = &ja->discard_bio;
  335. if (!ca->discard) {
  336. ja->discard_idx = ja->last_idx;
  337. return;
  338. }
  339. switch (atomic_read(&ja->discard_in_flight) == DISCARD_IN_FLIGHT) {
  340. case DISCARD_IN_FLIGHT:
  341. return;
  342. case DISCARD_DONE:
  343. ja->discard_idx = (ja->discard_idx + 1) %
  344. ca->sb.njournal_buckets;
  345. atomic_set(&ja->discard_in_flight, DISCARD_READY);
  346. /* fallthrough */
  347. case DISCARD_READY:
  348. if (ja->discard_idx == ja->last_idx)
  349. return;
  350. atomic_set(&ja->discard_in_flight, DISCARD_IN_FLIGHT);
  351. bio_init(bio);
  352. bio->bi_sector = bucket_to_sector(ca->set,
  353. ca->sb.d[ja->discard_idx]);
  354. bio->bi_bdev = ca->bdev;
  355. bio->bi_rw = REQ_WRITE|REQ_DISCARD;
  356. bio->bi_max_vecs = 1;
  357. bio->bi_io_vec = bio->bi_inline_vecs;
  358. bio->bi_size = bucket_bytes(ca);
  359. bio->bi_end_io = journal_discard_endio;
  360. closure_get(&ca->set->cl);
  361. INIT_WORK(&ja->discard_work, journal_discard_work);
  362. schedule_work(&ja->discard_work);
  363. }
  364. }
  365. static void journal_reclaim(struct cache_set *c)
  366. {
  367. struct bkey *k = &c->journal.key;
  368. struct cache *ca;
  369. uint64_t last_seq;
  370. unsigned iter, n = 0;
  371. atomic_t p;
  372. while (!atomic_read(&fifo_front(&c->journal.pin)))
  373. fifo_pop(&c->journal.pin, p);
  374. last_seq = last_seq(&c->journal);
  375. /* Update last_idx */
  376. for_each_cache(ca, c, iter) {
  377. struct journal_device *ja = &ca->journal;
  378. while (ja->last_idx != ja->cur_idx &&
  379. ja->seq[ja->last_idx] < last_seq)
  380. ja->last_idx = (ja->last_idx + 1) %
  381. ca->sb.njournal_buckets;
  382. }
  383. for_each_cache(ca, c, iter)
  384. do_journal_discard(ca);
  385. if (c->journal.blocks_free)
  386. return;
  387. /*
  388. * Allocate:
  389. * XXX: Sort by free journal space
  390. */
  391. for_each_cache(ca, c, iter) {
  392. struct journal_device *ja = &ca->journal;
  393. unsigned next = (ja->cur_idx + 1) % ca->sb.njournal_buckets;
  394. /* No space available on this device */
  395. if (next == ja->discard_idx)
  396. continue;
  397. ja->cur_idx = next;
  398. k->ptr[n++] = PTR(0,
  399. bucket_to_sector(c, ca->sb.d[ja->cur_idx]),
  400. ca->sb.nr_this_dev);
  401. }
  402. bkey_init(k);
  403. SET_KEY_PTRS(k, n);
  404. if (n)
  405. c->journal.blocks_free = c->sb.bucket_size >> c->block_bits;
  406. if (!journal_full(&c->journal))
  407. __closure_wake_up(&c->journal.wait);
  408. }
  409. void bch_journal_next(struct journal *j)
  410. {
  411. atomic_t p = { 1 };
  412. j->cur = (j->cur == j->w)
  413. ? &j->w[1]
  414. : &j->w[0];
  415. /*
  416. * The fifo_push() needs to happen at the same time as j->seq is
  417. * incremented for last_seq() to be calculated correctly
  418. */
  419. BUG_ON(!fifo_push(&j->pin, p));
  420. atomic_set(&fifo_back(&j->pin), 1);
  421. j->cur->data->seq = ++j->seq;
  422. j->cur->need_write = false;
  423. j->cur->data->keys = 0;
  424. if (fifo_full(&j->pin))
  425. pr_debug("journal_pin full (%zu)", fifo_used(&j->pin));
  426. }
  427. static void journal_write_endio(struct bio *bio, int error)
  428. {
  429. struct journal_write *w = bio->bi_private;
  430. cache_set_err_on(error, w->c, "journal io error");
  431. closure_put(&w->c->journal.io.cl);
  432. }
  433. static void journal_write(struct closure *);
  434. static void journal_write_done(struct closure *cl)
  435. {
  436. struct journal *j = container_of(cl, struct journal, io.cl);
  437. struct cache_set *c = container_of(j, struct cache_set, journal);
  438. struct journal_write *w = (j->cur == j->w)
  439. ? &j->w[1]
  440. : &j->w[0];
  441. __closure_wake_up(&w->wait);
  442. if (c->journal_delay_ms)
  443. closure_delay(&j->io, msecs_to_jiffies(c->journal_delay_ms));
  444. continue_at(cl, journal_write, system_wq);
  445. }
  446. static void journal_write_unlocked(struct closure *cl)
  447. __releases(c->journal.lock)
  448. {
  449. struct cache_set *c = container_of(cl, struct cache_set, journal.io.cl);
  450. struct cache *ca;
  451. struct journal_write *w = c->journal.cur;
  452. struct bkey *k = &c->journal.key;
  453. unsigned i, sectors = set_blocks(w->data, c) * c->sb.block_size;
  454. struct bio *bio;
  455. struct bio_list list;
  456. bio_list_init(&list);
  457. if (!w->need_write) {
  458. /*
  459. * XXX: have to unlock closure before we unlock journal lock,
  460. * else we race with bch_journal(). But this way we race
  461. * against cache set unregister. Doh.
  462. */
  463. set_closure_fn(cl, NULL, NULL);
  464. closure_sub(cl, CLOSURE_RUNNING + 1);
  465. spin_unlock(&c->journal.lock);
  466. return;
  467. } else if (journal_full(&c->journal)) {
  468. journal_reclaim(c);
  469. spin_unlock(&c->journal.lock);
  470. btree_flush_write(c);
  471. continue_at(cl, journal_write, system_wq);
  472. }
  473. c->journal.blocks_free -= set_blocks(w->data, c);
  474. w->data->btree_level = c->root->level;
  475. bkey_copy(&w->data->btree_root, &c->root->key);
  476. bkey_copy(&w->data->uuid_bucket, &c->uuid_bucket);
  477. for_each_cache(ca, c, i)
  478. w->data->prio_bucket[ca->sb.nr_this_dev] = ca->prio_buckets[0];
  479. w->data->magic = jset_magic(c);
  480. w->data->version = BCACHE_JSET_VERSION;
  481. w->data->last_seq = last_seq(&c->journal);
  482. w->data->csum = csum_set(w->data);
  483. for (i = 0; i < KEY_PTRS(k); i++) {
  484. ca = PTR_CACHE(c, k, i);
  485. bio = &ca->journal.bio;
  486. atomic_long_add(sectors, &ca->meta_sectors_written);
  487. bio_reset(bio);
  488. bio->bi_sector = PTR_OFFSET(k, i);
  489. bio->bi_bdev = ca->bdev;
  490. bio->bi_rw = REQ_WRITE|REQ_SYNC|REQ_META|REQ_FLUSH;
  491. bio->bi_size = sectors << 9;
  492. bio->bi_end_io = journal_write_endio;
  493. bio->bi_private = w;
  494. bch_bio_map(bio, w->data);
  495. trace_bcache_journal_write(bio);
  496. bio_list_add(&list, bio);
  497. SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + sectors);
  498. ca->journal.seq[ca->journal.cur_idx] = w->data->seq;
  499. }
  500. atomic_dec_bug(&fifo_back(&c->journal.pin));
  501. bch_journal_next(&c->journal);
  502. journal_reclaim(c);
  503. spin_unlock(&c->journal.lock);
  504. while ((bio = bio_list_pop(&list)))
  505. closure_bio_submit(bio, cl, c->cache[0]);
  506. continue_at(cl, journal_write_done, NULL);
  507. }
  508. static void journal_write(struct closure *cl)
  509. {
  510. struct cache_set *c = container_of(cl, struct cache_set, journal.io.cl);
  511. spin_lock(&c->journal.lock);
  512. journal_write_unlocked(cl);
  513. }
  514. static void __journal_try_write(struct cache_set *c, bool noflush)
  515. __releases(c->journal.lock)
  516. {
  517. struct closure *cl = &c->journal.io.cl;
  518. if (!closure_trylock(cl, &c->cl))
  519. spin_unlock(&c->journal.lock);
  520. else if (noflush && journal_full(&c->journal)) {
  521. spin_unlock(&c->journal.lock);
  522. continue_at(cl, journal_write, system_wq);
  523. } else
  524. journal_write_unlocked(cl);
  525. }
  526. #define journal_try_write(c) __journal_try_write(c, false)
  527. void bch_journal_meta(struct cache_set *c, struct closure *cl)
  528. {
  529. struct journal_write *w;
  530. if (CACHE_SYNC(&c->sb)) {
  531. spin_lock(&c->journal.lock);
  532. w = c->journal.cur;
  533. w->need_write = true;
  534. if (cl)
  535. BUG_ON(!closure_wait(&w->wait, cl));
  536. __journal_try_write(c, true);
  537. }
  538. }
  539. /*
  540. * Entry point to the journalling code - bio_insert() and btree_invalidate()
  541. * pass bch_journal() a list of keys to be journalled, and then
  542. * bch_journal() hands those same keys off to btree_insert_async()
  543. */
  544. void bch_journal(struct closure *cl)
  545. {
  546. struct btree_op *op = container_of(cl, struct btree_op, cl);
  547. struct cache_set *c = op->c;
  548. struct journal_write *w;
  549. size_t b, n = ((uint64_t *) op->keys.top) - op->keys.list;
  550. if (op->type != BTREE_INSERT ||
  551. !CACHE_SYNC(&c->sb))
  552. goto out;
  553. /*
  554. * If we're looping because we errored, might already be waiting on
  555. * another journal write:
  556. */
  557. while (atomic_read(&cl->parent->remaining) & CLOSURE_WAITING)
  558. closure_sync(cl->parent);
  559. spin_lock(&c->journal.lock);
  560. if (journal_full(&c->journal)) {
  561. /* XXX: tracepoint */
  562. closure_wait(&c->journal.wait, cl);
  563. journal_reclaim(c);
  564. spin_unlock(&c->journal.lock);
  565. btree_flush_write(c);
  566. continue_at(cl, bch_journal, bcache_wq);
  567. }
  568. w = c->journal.cur;
  569. w->need_write = true;
  570. b = __set_blocks(w->data, w->data->keys + n, c);
  571. if (b * c->sb.block_size > PAGE_SECTORS << JSET_BITS ||
  572. b > c->journal.blocks_free) {
  573. /* XXX: If we were inserting so many keys that they won't fit in
  574. * an _empty_ journal write, we'll deadlock. For now, handle
  575. * this in bch_keylist_realloc() - but something to think about.
  576. */
  577. BUG_ON(!w->data->keys);
  578. /* XXX: tracepoint */
  579. BUG_ON(!closure_wait(&w->wait, cl));
  580. closure_flush(&c->journal.io);
  581. journal_try_write(c);
  582. continue_at(cl, bch_journal, bcache_wq);
  583. }
  584. memcpy(end(w->data), op->keys.list, n * sizeof(uint64_t));
  585. w->data->keys += n;
  586. op->journal = &fifo_back(&c->journal.pin);
  587. atomic_inc(op->journal);
  588. if (op->flush_journal) {
  589. closure_flush(&c->journal.io);
  590. closure_wait(&w->wait, cl->parent);
  591. }
  592. journal_try_write(c);
  593. out:
  594. bch_btree_insert_async(cl);
  595. }
  596. void bch_journal_free(struct cache_set *c)
  597. {
  598. free_pages((unsigned long) c->journal.w[1].data, JSET_BITS);
  599. free_pages((unsigned long) c->journal.w[0].data, JSET_BITS);
  600. free_fifo(&c->journal.pin);
  601. }
  602. int bch_journal_alloc(struct cache_set *c)
  603. {
  604. struct journal *j = &c->journal;
  605. closure_init_unlocked(&j->io);
  606. spin_lock_init(&j->lock);
  607. c->journal_delay_ms = 100;
  608. j->w[0].c = c;
  609. j->w[1].c = c;
  610. if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
  611. !(j->w[0].data = (void *) __get_free_pages(GFP_KERNEL, JSET_BITS)) ||
  612. !(j->w[1].data = (void *) __get_free_pages(GFP_KERNEL, JSET_BITS)))
  613. return -ENOMEM;
  614. return 0;
  615. }