journal.c 22 KB

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