journal.c 18 KB

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