journal.c 18 KB

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