journal.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. /*
  83. * Nodes in 'list' are in linear increasing order of
  84. * i->j.seq, the node on head has the smallest (oldest)
  85. * journal seq, the node on tail has the biggest
  86. * (latest) journal seq.
  87. */
  88. /*
  89. * Check from the oldest jset for last_seq. If
  90. * i->j.seq < j->last_seq, it means the oldest jset
  91. * in list is expired and useless, remove it from
  92. * this list. Otherwise, j is a condidate jset for
  93. * further following checks.
  94. */
  95. while (!list_empty(list)) {
  96. i = list_first_entry(list,
  97. struct journal_replay, list);
  98. if (i->j.seq >= j->last_seq)
  99. break;
  100. list_del(&i->list);
  101. kfree(i);
  102. }
  103. /* iterate list in reverse order (from latest jset) */
  104. list_for_each_entry_reverse(i, list, list) {
  105. if (j->seq == i->j.seq)
  106. goto next_set;
  107. /*
  108. * if j->seq is less than any i->j.last_seq
  109. * in list, j is an expired and useless jset.
  110. */
  111. if (j->seq < i->j.last_seq)
  112. goto next_set;
  113. /*
  114. * 'where' points to first jset in list which
  115. * is elder then j.
  116. */
  117. if (j->seq > i->j.seq) {
  118. where = &i->list;
  119. goto add;
  120. }
  121. }
  122. where = list;
  123. add:
  124. i = kmalloc(offsetof(struct journal_replay, j) +
  125. bytes, GFP_KERNEL);
  126. if (!i)
  127. return -ENOMEM;
  128. memcpy(&i->j, j, bytes);
  129. /* Add to the location after 'where' points to */
  130. list_add(&i->list, where);
  131. ret = 1;
  132. if (j->seq > ja->seq[bucket_index])
  133. ja->seq[bucket_index] = j->seq;
  134. next_set:
  135. offset += blocks * ca->sb.block_size;
  136. len -= blocks * ca->sb.block_size;
  137. j = ((void *) j) + blocks * block_bytes(ca);
  138. }
  139. }
  140. return ret;
  141. }
  142. int bch_journal_read(struct cache_set *c, struct list_head *list)
  143. {
  144. #define read_bucket(b) \
  145. ({ \
  146. ret = journal_read_bucket(ca, list, b); \
  147. __set_bit(b, bitmap); \
  148. if (ret < 0) \
  149. return ret; \
  150. ret; \
  151. })
  152. struct cache *ca;
  153. unsigned int iter;
  154. int ret = 0;
  155. for_each_cache(ca, c, iter) {
  156. struct journal_device *ja = &ca->journal;
  157. DECLARE_BITMAP(bitmap, SB_JOURNAL_BUCKETS);
  158. unsigned int i, l, r, m;
  159. uint64_t seq;
  160. bitmap_zero(bitmap, SB_JOURNAL_BUCKETS);
  161. pr_debug("%u journal buckets", ca->sb.njournal_buckets);
  162. /*
  163. * Read journal buckets ordered by golden ratio hash to quickly
  164. * find a sequence of buckets with valid journal entries
  165. */
  166. for (i = 0; i < ca->sb.njournal_buckets; i++) {
  167. /*
  168. * We must try the index l with ZERO first for
  169. * correctness due to the scenario that the journal
  170. * bucket is circular buffer which might have wrapped
  171. */
  172. l = (i * 2654435769U) % ca->sb.njournal_buckets;
  173. if (test_bit(l, bitmap))
  174. break;
  175. if (read_bucket(l))
  176. goto bsearch;
  177. }
  178. /*
  179. * If that fails, check all the buckets we haven't checked
  180. * already
  181. */
  182. pr_debug("falling back to linear search");
  183. for (l = find_first_zero_bit(bitmap, ca->sb.njournal_buckets);
  184. l < ca->sb.njournal_buckets;
  185. l = find_next_zero_bit(bitmap, ca->sb.njournal_buckets,
  186. l + 1))
  187. if (read_bucket(l))
  188. goto bsearch;
  189. /* no journal entries on this device? */
  190. if (l == ca->sb.njournal_buckets)
  191. continue;
  192. bsearch:
  193. BUG_ON(list_empty(list));
  194. /* Binary search */
  195. m = l;
  196. r = find_next_bit(bitmap, ca->sb.njournal_buckets, l + 1);
  197. pr_debug("starting binary search, l %u r %u", l, r);
  198. while (l + 1 < r) {
  199. seq = list_entry(list->prev, struct journal_replay,
  200. list)->j.seq;
  201. m = (l + r) >> 1;
  202. read_bucket(m);
  203. if (seq != list_entry(list->prev, struct journal_replay,
  204. list)->j.seq)
  205. l = m;
  206. else
  207. r = m;
  208. }
  209. /*
  210. * Read buckets in reverse order until we stop finding more
  211. * journal entries
  212. */
  213. pr_debug("finishing up: m %u njournal_buckets %u",
  214. m, ca->sb.njournal_buckets);
  215. l = m;
  216. while (1) {
  217. if (!l--)
  218. l = ca->sb.njournal_buckets - 1;
  219. if (l == m)
  220. break;
  221. if (test_bit(l, bitmap))
  222. continue;
  223. if (!read_bucket(l))
  224. break;
  225. }
  226. seq = 0;
  227. for (i = 0; i < ca->sb.njournal_buckets; i++)
  228. if (ja->seq[i] > seq) {
  229. seq = ja->seq[i];
  230. /*
  231. * When journal_reclaim() goes to allocate for
  232. * the first time, it'll use the bucket after
  233. * ja->cur_idx
  234. */
  235. ja->cur_idx = i;
  236. ja->last_idx = ja->discard_idx = (i + 1) %
  237. ca->sb.njournal_buckets;
  238. }
  239. }
  240. if (!list_empty(list))
  241. c->journal.seq = list_entry(list->prev,
  242. struct journal_replay,
  243. list)->j.seq;
  244. return 0;
  245. #undef read_bucket
  246. }
  247. void bch_journal_mark(struct cache_set *c, struct list_head *list)
  248. {
  249. atomic_t p = { 0 };
  250. struct bkey *k;
  251. struct journal_replay *i;
  252. struct journal *j = &c->journal;
  253. uint64_t last = j->seq;
  254. /*
  255. * journal.pin should never fill up - we never write a journal
  256. * entry when it would fill up. But if for some reason it does, we
  257. * iterate over the list in reverse order so that we can just skip that
  258. * refcount instead of bugging.
  259. */
  260. list_for_each_entry_reverse(i, list, list) {
  261. BUG_ON(last < i->j.seq);
  262. i->pin = NULL;
  263. while (last-- != i->j.seq)
  264. if (fifo_free(&j->pin) > 1) {
  265. fifo_push_front(&j->pin, p);
  266. atomic_set(&fifo_front(&j->pin), 0);
  267. }
  268. if (fifo_free(&j->pin) > 1) {
  269. fifo_push_front(&j->pin, p);
  270. i->pin = &fifo_front(&j->pin);
  271. atomic_set(i->pin, 1);
  272. }
  273. for (k = i->j.start;
  274. k < bset_bkey_last(&i->j);
  275. k = bkey_next(k))
  276. if (!__bch_extent_invalid(c, k)) {
  277. unsigned int j;
  278. for (j = 0; j < KEY_PTRS(k); j++)
  279. if (ptr_available(c, k, j))
  280. atomic_inc(&PTR_BUCKET(c, k, j)->pin);
  281. bch_initial_mark_key(c, 0, k);
  282. }
  283. }
  284. }
  285. static bool is_discard_enabled(struct cache_set *s)
  286. {
  287. struct cache *ca;
  288. unsigned int i;
  289. for_each_cache(ca, s, i)
  290. if (ca->discard)
  291. return true;
  292. return false;
  293. }
  294. int bch_journal_replay(struct cache_set *s, struct list_head *list)
  295. {
  296. int ret = 0, keys = 0, entries = 0;
  297. struct bkey *k;
  298. struct journal_replay *i =
  299. list_entry(list->prev, struct journal_replay, list);
  300. uint64_t start = i->j.last_seq, end = i->j.seq, n = start;
  301. struct keylist keylist;
  302. list_for_each_entry(i, list, list) {
  303. BUG_ON(i->pin && atomic_read(i->pin) != 1);
  304. if (n != i->j.seq) {
  305. if (n == start && is_discard_enabled(s))
  306. pr_info("bcache: journal entries %llu-%llu may be discarded! (replaying %llu-%llu)",
  307. n, i->j.seq - 1, start, end);
  308. else {
  309. pr_err("bcache: journal entries %llu-%llu missing! (replaying %llu-%llu)",
  310. n, i->j.seq - 1, start, end);
  311. ret = -EIO;
  312. goto err;
  313. }
  314. }
  315. for (k = i->j.start;
  316. k < bset_bkey_last(&i->j);
  317. k = bkey_next(k)) {
  318. trace_bcache_journal_replay_key(k);
  319. bch_keylist_init_single(&keylist, k);
  320. ret = bch_btree_insert(s, &keylist, i->pin, NULL);
  321. if (ret)
  322. goto err;
  323. BUG_ON(!bch_keylist_empty(&keylist));
  324. keys++;
  325. cond_resched();
  326. }
  327. if (i->pin)
  328. atomic_dec(i->pin);
  329. n = i->j.seq + 1;
  330. entries++;
  331. }
  332. pr_info("journal replay done, %i keys in %i entries, seq %llu",
  333. keys, entries, end);
  334. err:
  335. while (!list_empty(list)) {
  336. i = list_first_entry(list, struct journal_replay, list);
  337. list_del(&i->list);
  338. kfree(i);
  339. }
  340. return ret;
  341. }
  342. /* Journalling */
  343. #define nr_to_fifo_front(p, front_p, mask) (((p) - (front_p)) & (mask))
  344. static void btree_flush_write(struct cache_set *c)
  345. {
  346. struct btree *b, *t, *btree_nodes[BTREE_FLUSH_NR];
  347. unsigned int i, nr;
  348. int ref_nr;
  349. atomic_t *fifo_front_p, *now_fifo_front_p;
  350. size_t mask;
  351. if (c->journal.btree_flushing)
  352. return;
  353. spin_lock(&c->journal.flush_write_lock);
  354. if (c->journal.btree_flushing) {
  355. spin_unlock(&c->journal.flush_write_lock);
  356. return;
  357. }
  358. c->journal.btree_flushing = true;
  359. spin_unlock(&c->journal.flush_write_lock);
  360. /* get the oldest journal entry and check its refcount */
  361. spin_lock(&c->journal.lock);
  362. fifo_front_p = &fifo_front(&c->journal.pin);
  363. ref_nr = atomic_read(fifo_front_p);
  364. if (ref_nr <= 0) {
  365. /*
  366. * do nothing if no btree node references
  367. * the oldest journal entry
  368. */
  369. spin_unlock(&c->journal.lock);
  370. goto out;
  371. }
  372. spin_unlock(&c->journal.lock);
  373. mask = c->journal.pin.mask;
  374. nr = 0;
  375. atomic_long_inc(&c->flush_write);
  376. memset(btree_nodes, 0, sizeof(btree_nodes));
  377. mutex_lock(&c->bucket_lock);
  378. list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) {
  379. /*
  380. * It is safe to get now_fifo_front_p without holding
  381. * c->journal.lock here, because we don't need to know
  382. * the exactly accurate value, just check whether the
  383. * front pointer of c->journal.pin is changed.
  384. */
  385. now_fifo_front_p = &fifo_front(&c->journal.pin);
  386. /*
  387. * If the oldest journal entry is reclaimed and front
  388. * pointer of c->journal.pin changes, it is unnecessary
  389. * to scan c->btree_cache anymore, just quit the loop and
  390. * flush out what we have already.
  391. */
  392. if (now_fifo_front_p != fifo_front_p)
  393. break;
  394. /*
  395. * quit this loop if all matching btree nodes are
  396. * scanned and record in btree_nodes[] already.
  397. */
  398. ref_nr = atomic_read(fifo_front_p);
  399. if (nr >= ref_nr)
  400. break;
  401. if (btree_node_journal_flush(b))
  402. pr_err("BUG: flush_write bit should not be set here!");
  403. mutex_lock(&b->write_lock);
  404. if (!btree_node_dirty(b)) {
  405. mutex_unlock(&b->write_lock);
  406. continue;
  407. }
  408. if (!btree_current_write(b)->journal) {
  409. mutex_unlock(&b->write_lock);
  410. continue;
  411. }
  412. /*
  413. * Only select the btree node which exactly references
  414. * the oldest journal entry.
  415. *
  416. * If the journal entry pointed by fifo_front_p is
  417. * reclaimed in parallel, don't worry:
  418. * - the list_for_each_xxx loop will quit when checking
  419. * next now_fifo_front_p.
  420. * - If there are matched nodes recorded in btree_nodes[],
  421. * they are clean now (this is why and how the oldest
  422. * journal entry can be reclaimed). These selected nodes
  423. * will be ignored and skipped in the folowing for-loop.
  424. */
  425. if (nr_to_fifo_front(btree_current_write(b)->journal,
  426. fifo_front_p,
  427. mask) != 0) {
  428. mutex_unlock(&b->write_lock);
  429. continue;
  430. }
  431. set_btree_node_journal_flush(b);
  432. mutex_unlock(&b->write_lock);
  433. btree_nodes[nr++] = b;
  434. /*
  435. * To avoid holding c->bucket_lock too long time,
  436. * only scan for BTREE_FLUSH_NR matched btree nodes
  437. * at most. If there are more btree nodes reference
  438. * the oldest journal entry, try to flush them next
  439. * time when btree_flush_write() is called.
  440. */
  441. if (nr == BTREE_FLUSH_NR)
  442. break;
  443. }
  444. mutex_unlock(&c->bucket_lock);
  445. for (i = 0; i < nr; i++) {
  446. b = btree_nodes[i];
  447. if (!b) {
  448. pr_err("BUG: btree_nodes[%d] is NULL", i);
  449. continue;
  450. }
  451. /* safe to check without holding b->write_lock */
  452. if (!btree_node_journal_flush(b)) {
  453. pr_err("BUG: bnode %p: journal_flush bit cleaned", b);
  454. continue;
  455. }
  456. mutex_lock(&b->write_lock);
  457. if (!btree_current_write(b)->journal) {
  458. clear_bit(BTREE_NODE_journal_flush, &b->flags);
  459. mutex_unlock(&b->write_lock);
  460. pr_debug("bnode %p: written by others", b);
  461. continue;
  462. }
  463. if (!btree_node_dirty(b)) {
  464. clear_bit(BTREE_NODE_journal_flush, &b->flags);
  465. mutex_unlock(&b->write_lock);
  466. pr_debug("bnode %p: dirty bit cleaned by others", b);
  467. continue;
  468. }
  469. __bch_btree_node_write(b, NULL);
  470. clear_bit(BTREE_NODE_journal_flush, &b->flags);
  471. mutex_unlock(&b->write_lock);
  472. }
  473. out:
  474. spin_lock(&c->journal.flush_write_lock);
  475. c->journal.btree_flushing = false;
  476. spin_unlock(&c->journal.flush_write_lock);
  477. }
  478. #define last_seq(j) ((j)->seq - fifo_used(&(j)->pin) + 1)
  479. static void journal_discard_endio(struct bio *bio)
  480. {
  481. struct journal_device *ja =
  482. container_of(bio, struct journal_device, discard_bio);
  483. struct cache *ca = container_of(ja, struct cache, journal);
  484. atomic_set(&ja->discard_in_flight, DISCARD_DONE);
  485. closure_wake_up(&ca->set->journal.wait);
  486. closure_put(&ca->set->cl);
  487. }
  488. static void journal_discard_work(struct work_struct *work)
  489. {
  490. struct journal_device *ja =
  491. container_of(work, struct journal_device, discard_work);
  492. submit_bio(&ja->discard_bio);
  493. }
  494. static void do_journal_discard(struct cache *ca)
  495. {
  496. struct journal_device *ja = &ca->journal;
  497. struct bio *bio = &ja->discard_bio;
  498. if (!ca->discard) {
  499. ja->discard_idx = ja->last_idx;
  500. return;
  501. }
  502. switch (atomic_read(&ja->discard_in_flight)) {
  503. case DISCARD_IN_FLIGHT:
  504. return;
  505. case DISCARD_DONE:
  506. ja->discard_idx = (ja->discard_idx + 1) %
  507. ca->sb.njournal_buckets;
  508. atomic_set(&ja->discard_in_flight, DISCARD_READY);
  509. /* fallthrough */
  510. case DISCARD_READY:
  511. if (ja->discard_idx == ja->last_idx)
  512. return;
  513. atomic_set(&ja->discard_in_flight, DISCARD_IN_FLIGHT);
  514. bio_init(bio, bio->bi_inline_vecs, 1);
  515. bio_set_op_attrs(bio, REQ_OP_DISCARD, 0);
  516. bio->bi_iter.bi_sector = bucket_to_sector(ca->set,
  517. ca->sb.d[ja->discard_idx]);
  518. bio_set_dev(bio, ca->bdev);
  519. bio->bi_iter.bi_size = bucket_bytes(ca);
  520. bio->bi_end_io = journal_discard_endio;
  521. closure_get(&ca->set->cl);
  522. INIT_WORK(&ja->discard_work, journal_discard_work);
  523. queue_work(bch_journal_wq, &ja->discard_work);
  524. }
  525. }
  526. static void journal_reclaim(struct cache_set *c)
  527. {
  528. struct bkey *k = &c->journal.key;
  529. struct cache *ca;
  530. uint64_t last_seq;
  531. unsigned int iter, n = 0;
  532. atomic_t p __maybe_unused;
  533. atomic_long_inc(&c->reclaim);
  534. while (!atomic_read(&fifo_front(&c->journal.pin)))
  535. fifo_pop(&c->journal.pin, p);
  536. last_seq = last_seq(&c->journal);
  537. /* Update last_idx */
  538. for_each_cache(ca, c, iter) {
  539. struct journal_device *ja = &ca->journal;
  540. while (ja->last_idx != ja->cur_idx &&
  541. ja->seq[ja->last_idx] < last_seq)
  542. ja->last_idx = (ja->last_idx + 1) %
  543. ca->sb.njournal_buckets;
  544. }
  545. for_each_cache(ca, c, iter)
  546. do_journal_discard(ca);
  547. if (c->journal.blocks_free)
  548. goto out;
  549. /*
  550. * Allocate:
  551. * XXX: Sort by free journal space
  552. */
  553. for_each_cache(ca, c, iter) {
  554. struct journal_device *ja = &ca->journal;
  555. unsigned int next = (ja->cur_idx + 1) % ca->sb.njournal_buckets;
  556. /* No space available on this device */
  557. if (next == ja->discard_idx)
  558. continue;
  559. ja->cur_idx = next;
  560. k->ptr[n++] = MAKE_PTR(0,
  561. bucket_to_sector(c, ca->sb.d[ja->cur_idx]),
  562. ca->sb.nr_this_dev);
  563. atomic_long_inc(&c->reclaimed_journal_buckets);
  564. }
  565. if (n) {
  566. bkey_init(k);
  567. SET_KEY_PTRS(k, n);
  568. c->journal.blocks_free = c->sb.bucket_size >> c->block_bits;
  569. }
  570. out:
  571. if (!journal_full(&c->journal))
  572. __closure_wake_up(&c->journal.wait);
  573. }
  574. void bch_journal_next(struct journal *j)
  575. {
  576. atomic_t p = { 1 };
  577. j->cur = (j->cur == j->w)
  578. ? &j->w[1]
  579. : &j->w[0];
  580. /*
  581. * The fifo_push() needs to happen at the same time as j->seq is
  582. * incremented for last_seq() to be calculated correctly
  583. */
  584. BUG_ON(!fifo_push(&j->pin, p));
  585. atomic_set(&fifo_back(&j->pin), 1);
  586. j->cur->data->seq = ++j->seq;
  587. j->cur->dirty = false;
  588. j->cur->need_write = false;
  589. j->cur->data->keys = 0;
  590. if (fifo_full(&j->pin))
  591. pr_debug("journal_pin full (%zu)", fifo_used(&j->pin));
  592. }
  593. static void journal_write_endio(struct bio *bio)
  594. {
  595. struct journal_write *w = bio->bi_private;
  596. cache_set_err_on(bio->bi_status, w->c, "journal io error");
  597. closure_put(&w->c->journal.io);
  598. }
  599. static void journal_write(struct closure *cl);
  600. static void journal_write_done(struct closure *cl)
  601. {
  602. struct journal *j = container_of(cl, struct journal, io);
  603. struct journal_write *w = (j->cur == j->w)
  604. ? &j->w[1]
  605. : &j->w[0];
  606. __closure_wake_up(&w->wait);
  607. continue_at_nobarrier(cl, journal_write, bch_journal_wq);
  608. }
  609. static void journal_write_unlock(struct closure *cl)
  610. __releases(&c->journal.lock)
  611. {
  612. struct cache_set *c = container_of(cl, struct cache_set, journal.io);
  613. c->journal.io_in_flight = 0;
  614. spin_unlock(&c->journal.lock);
  615. }
  616. static void journal_write_unlocked(struct closure *cl)
  617. __releases(c->journal.lock)
  618. {
  619. struct cache_set *c = container_of(cl, struct cache_set, journal.io);
  620. struct cache *ca;
  621. struct journal_write *w = c->journal.cur;
  622. struct bkey *k = &c->journal.key;
  623. unsigned int i, sectors = set_blocks(w->data, block_bytes(c)) *
  624. c->sb.block_size;
  625. struct bio *bio;
  626. struct bio_list list;
  627. bio_list_init(&list);
  628. if (!w->need_write) {
  629. closure_return_with_destructor(cl, journal_write_unlock);
  630. return;
  631. } else if (journal_full(&c->journal)) {
  632. journal_reclaim(c);
  633. spin_unlock(&c->journal.lock);
  634. btree_flush_write(c);
  635. continue_at(cl, journal_write, bch_journal_wq);
  636. return;
  637. }
  638. c->journal.blocks_free -= set_blocks(w->data, block_bytes(c));
  639. w->data->btree_level = c->root->level;
  640. bkey_copy(&w->data->btree_root, &c->root->key);
  641. bkey_copy(&w->data->uuid_bucket, &c->uuid_bucket);
  642. for_each_cache(ca, c, i)
  643. w->data->prio_bucket[ca->sb.nr_this_dev] = ca->prio_buckets[0];
  644. w->data->magic = jset_magic(&c->sb);
  645. w->data->version = BCACHE_JSET_VERSION;
  646. w->data->last_seq = last_seq(&c->journal);
  647. w->data->csum = csum_set(w->data);
  648. for (i = 0; i < KEY_PTRS(k); i++) {
  649. ca = PTR_CACHE(c, k, i);
  650. bio = &ca->journal.bio;
  651. atomic_long_add(sectors, &ca->meta_sectors_written);
  652. bio_reset(bio);
  653. bio->bi_iter.bi_sector = PTR_OFFSET(k, i);
  654. bio_set_dev(bio, ca->bdev);
  655. bio->bi_iter.bi_size = sectors << 9;
  656. bio->bi_end_io = journal_write_endio;
  657. bio->bi_private = w;
  658. bio_set_op_attrs(bio, REQ_OP_WRITE,
  659. REQ_SYNC|REQ_META|REQ_PREFLUSH|REQ_FUA);
  660. bch_bio_map(bio, w->data);
  661. trace_bcache_journal_write(bio, w->data->keys);
  662. bio_list_add(&list, bio);
  663. SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + sectors);
  664. ca->journal.seq[ca->journal.cur_idx] = w->data->seq;
  665. }
  666. /* If KEY_PTRS(k) == 0, this jset gets lost in air */
  667. BUG_ON(i == 0);
  668. atomic_dec_bug(&fifo_back(&c->journal.pin));
  669. bch_journal_next(&c->journal);
  670. journal_reclaim(c);
  671. spin_unlock(&c->journal.lock);
  672. while ((bio = bio_list_pop(&list)))
  673. closure_bio_submit(c, bio, cl);
  674. continue_at(cl, journal_write_done, NULL);
  675. }
  676. static void journal_write(struct closure *cl)
  677. {
  678. struct cache_set *c = container_of(cl, struct cache_set, journal.io);
  679. spin_lock(&c->journal.lock);
  680. journal_write_unlocked(cl);
  681. }
  682. static void journal_try_write(struct cache_set *c)
  683. __releases(c->journal.lock)
  684. {
  685. struct closure *cl = &c->journal.io;
  686. struct journal_write *w = c->journal.cur;
  687. w->need_write = true;
  688. if (!c->journal.io_in_flight) {
  689. c->journal.io_in_flight = 1;
  690. closure_call(cl, journal_write_unlocked, NULL, &c->cl);
  691. } else {
  692. spin_unlock(&c->journal.lock);
  693. }
  694. }
  695. static struct journal_write *journal_wait_for_write(struct cache_set *c,
  696. unsigned int nkeys)
  697. __acquires(&c->journal.lock)
  698. {
  699. size_t sectors;
  700. struct closure cl;
  701. bool wait = false;
  702. closure_init_stack(&cl);
  703. spin_lock(&c->journal.lock);
  704. while (1) {
  705. struct journal_write *w = c->journal.cur;
  706. sectors = __set_blocks(w->data, w->data->keys + nkeys,
  707. block_bytes(c)) * c->sb.block_size;
  708. if (sectors <= min_t(size_t,
  709. c->journal.blocks_free * c->sb.block_size,
  710. PAGE_SECTORS << JSET_BITS))
  711. return w;
  712. if (wait)
  713. closure_wait(&c->journal.wait, &cl);
  714. if (!journal_full(&c->journal)) {
  715. if (wait)
  716. trace_bcache_journal_entry_full(c);
  717. /*
  718. * XXX: If we were inserting so many keys that they
  719. * won't fit in an _empty_ journal write, we'll
  720. * deadlock. For now, handle this in
  721. * bch_keylist_realloc() - but something to think about.
  722. */
  723. BUG_ON(!w->data->keys);
  724. journal_try_write(c); /* unlocks */
  725. } else {
  726. if (wait)
  727. trace_bcache_journal_full(c);
  728. journal_reclaim(c);
  729. spin_unlock(&c->journal.lock);
  730. btree_flush_write(c);
  731. }
  732. closure_sync(&cl);
  733. spin_lock(&c->journal.lock);
  734. wait = true;
  735. }
  736. }
  737. static void journal_write_work(struct work_struct *work)
  738. {
  739. struct cache_set *c = container_of(to_delayed_work(work),
  740. struct cache_set,
  741. journal.work);
  742. spin_lock(&c->journal.lock);
  743. if (c->journal.cur->dirty)
  744. journal_try_write(c);
  745. else
  746. spin_unlock(&c->journal.lock);
  747. }
  748. /*
  749. * Entry point to the journalling code - bio_insert() and btree_invalidate()
  750. * pass bch_journal() a list of keys to be journalled, and then
  751. * bch_journal() hands those same keys off to btree_insert_async()
  752. */
  753. atomic_t *bch_journal(struct cache_set *c,
  754. struct keylist *keys,
  755. struct closure *parent)
  756. {
  757. struct journal_write *w;
  758. atomic_t *ret;
  759. /* No journaling if CACHE_SET_IO_DISABLE set already */
  760. if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags)))
  761. return NULL;
  762. if (!CACHE_SYNC(&c->sb))
  763. return NULL;
  764. w = journal_wait_for_write(c, bch_keylist_nkeys(keys));
  765. memcpy(bset_bkey_last(w->data), keys->keys, bch_keylist_bytes(keys));
  766. w->data->keys += bch_keylist_nkeys(keys);
  767. ret = &fifo_back(&c->journal.pin);
  768. atomic_inc(ret);
  769. if (parent) {
  770. closure_wait(&w->wait, parent);
  771. journal_try_write(c);
  772. } else if (!w->dirty) {
  773. w->dirty = true;
  774. queue_delayed_work(bch_flush_wq, &c->journal.work,
  775. msecs_to_jiffies(c->journal_delay_ms));
  776. spin_unlock(&c->journal.lock);
  777. } else {
  778. spin_unlock(&c->journal.lock);
  779. }
  780. return ret;
  781. }
  782. void bch_journal_meta(struct cache_set *c, struct closure *cl)
  783. {
  784. struct keylist keys;
  785. atomic_t *ref;
  786. bch_keylist_init(&keys);
  787. ref = bch_journal(c, &keys, cl);
  788. if (ref)
  789. atomic_dec_bug(ref);
  790. }
  791. void bch_journal_free(struct cache_set *c)
  792. {
  793. free_pages((unsigned long) c->journal.w[1].data, JSET_BITS);
  794. free_pages((unsigned long) c->journal.w[0].data, JSET_BITS);
  795. free_fifo(&c->journal.pin);
  796. }
  797. int bch_journal_alloc(struct cache_set *c)
  798. {
  799. struct journal *j = &c->journal;
  800. spin_lock_init(&j->lock);
  801. spin_lock_init(&j->flush_write_lock);
  802. INIT_DELAYED_WORK(&j->work, journal_write_work);
  803. c->journal_delay_ms = 100;
  804. j->w[0].c = c;
  805. j->w[1].c = c;
  806. if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
  807. !(j->w[0].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)) ||
  808. !(j->w[1].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)))
  809. return -ENOMEM;
  810. return 0;
  811. }