journal.c 18 KB

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