journal.c 20 KB

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