journal.c 18 KB

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