blob: 151a4ab90dd415fe232a5322e23c68a9daa45e86 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
2 * bcache journalling code, for btree insertions
3 *
4 * Copyright 2012 Google, Inc.
5 */
6
7#include "bcache.h"
8#include "btree.h"
9#include "debug.h"
10#include "request.h"
11
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
25static void journal_read_endio(struct bio *bio, int error)
26{
27 struct closure *cl = bio->bi_private;
28 closure_put(cl);
29}
30
31static int journal_read_bucket(struct cache *ca, struct list_head *list,
32 struct btree_op *op, unsigned bucket_index)
33{
34 struct journal_device *ja = &ca->journal;
35 struct bio *bio = &ja->bio;
36
37 struct journal_replay *i;
38 struct jset *j, *data = ca->set->journal.w[0].data;
39 unsigned len, left, offset = 0;
40 int ret = 0;
41 sector_t bucket = bucket_to_sector(ca->set, ca->sb.d[bucket_index]);
42
43 pr_debug("reading %llu", (uint64_t) bucket);
44
45 while (offset < ca->sb.bucket_size) {
46reread: left = ca->sb.bucket_size - offset;
47 len = min_t(unsigned, left, PAGE_SECTORS * 8);
48
49 bio_reset(bio);
50 bio->bi_sector = bucket + offset;
51 bio->bi_bdev = ca->bdev;
52 bio->bi_rw = READ;
53 bio->bi_size = len << 9;
54
55 bio->bi_end_io = journal_read_endio;
56 bio->bi_private = &op->cl;
Kent Overstreet169ef1c2013-03-28 12:50:55 -060057 bch_bio_map(bio, data);
Kent Overstreetcafe5632013-03-23 16:11:31 -070058
59 closure_bio_submit(bio, &op->cl, ca);
60 closure_sync(&op->cl);
61
62 /* This function could be simpler now since we no longer write
63 * journal entries that overlap bucket boundaries; this means
64 * the start of a bucket will always have a valid journal entry
65 * if it has any journal entries at all.
66 */
67
68 j = data;
69 while (len) {
70 struct list_head *where;
71 size_t blocks, bytes = set_bytes(j);
72
73 if (j->magic != jset_magic(ca->set))
74 return ret;
75
76 if (bytes > left << 9)
77 return ret;
78
79 if (bytes > len << 9)
80 goto reread;
81
82 if (j->csum != csum_set(j))
83 return ret;
84
85 blocks = set_blocks(j, ca->set);
86
87 while (!list_empty(list)) {
88 i = list_first_entry(list,
89 struct journal_replay, list);
90 if (i->j.seq >= j->last_seq)
91 break;
92 list_del(&i->list);
93 kfree(i);
94 }
95
96 list_for_each_entry_reverse(i, list, list) {
97 if (j->seq == i->j.seq)
98 goto next_set;
99
100 if (j->seq < i->j.last_seq)
101 goto next_set;
102
103 if (j->seq > i->j.seq) {
104 where = &i->list;
105 goto add;
106 }
107 }
108
109 where = list;
110add:
111 i = kmalloc(offsetof(struct journal_replay, j) +
112 bytes, GFP_KERNEL);
113 if (!i)
114 return -ENOMEM;
115 memcpy(&i->j, j, bytes);
116 list_add(&i->list, where);
117 ret = 1;
118
119 ja->seq[bucket_index] = j->seq;
120next_set:
121 offset += blocks * ca->sb.block_size;
122 len -= blocks * ca->sb.block_size;
123 j = ((void *) j) + blocks * block_bytes(ca);
124 }
125 }
126
127 return ret;
128}
129
130int bch_journal_read(struct cache_set *c, struct list_head *list,
131 struct btree_op *op)
132{
133#define read_bucket(b) \
134 ({ \
135 int ret = journal_read_bucket(ca, list, op, b); \
136 __set_bit(b, bitmap); \
137 if (ret < 0) \
138 return ret; \
139 ret; \
140 })
141
142 struct cache *ca;
143 unsigned iter;
144
145 for_each_cache(ca, c, iter) {
146 struct journal_device *ja = &ca->journal;
147 unsigned long bitmap[SB_JOURNAL_BUCKETS / BITS_PER_LONG];
148 unsigned i, l, r, m;
149 uint64_t seq;
150
151 bitmap_zero(bitmap, SB_JOURNAL_BUCKETS);
152 pr_debug("%u journal buckets", ca->sb.njournal_buckets);
153
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700154 /*
155 * Read journal buckets ordered by golden ratio hash to quickly
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156 * find a sequence of buckets with valid journal entries
157 */
158 for (i = 0; i < ca->sb.njournal_buckets; i++) {
159 l = (i * 2654435769U) % ca->sb.njournal_buckets;
160
161 if (test_bit(l, bitmap))
162 break;
163
164 if (read_bucket(l))
165 goto bsearch;
166 }
167
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700168 /*
169 * If that fails, check all the buckets we haven't checked
Kent Overstreetcafe5632013-03-23 16:11:31 -0700170 * already
171 */
172 pr_debug("falling back to linear search");
173
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700174 for (l = find_first_zero_bit(bitmap, ca->sb.njournal_buckets);
175 l < ca->sb.njournal_buckets;
176 l = find_next_zero_bit(bitmap, ca->sb.njournal_buckets, l + 1))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700177 if (read_bucket(l))
178 goto bsearch;
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700179
180 if (list_empty(list))
181 continue;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700182bsearch:
183 /* Binary search */
184 m = r = find_next_bit(bitmap, ca->sb.njournal_buckets, l + 1);
185 pr_debug("starting binary search, l %u r %u", l, r);
186
187 while (l + 1 < r) {
Kent Overstreet5e20e8b2013-07-11 22:42:14 -0700188 seq = list_entry(list->prev, struct journal_replay,
189 list)->j.seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190
Kent Overstreet5e20e8b2013-07-11 22:42:14 -0700191 m = (l + r) >> 1;
192 read_bucket(m);
193
194 if (seq != list_entry(list->prev, struct journal_replay,
195 list)->j.seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700196 l = m;
197 else
198 r = m;
199 }
200
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700201 /*
202 * Read buckets in reverse order until we stop finding more
Kent Overstreetcafe5632013-03-23 16:11:31 -0700203 * journal entries
204 */
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700205 pr_debug("finishing up: m %u njournal_buckets %u",
206 m, ca->sb.njournal_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700207 l = m;
208
209 while (1) {
210 if (!l--)
211 l = ca->sb.njournal_buckets - 1;
212
213 if (l == m)
214 break;
215
216 if (test_bit(l, bitmap))
217 continue;
218
219 if (!read_bucket(l))
220 break;
221 }
222
223 seq = 0;
224
225 for (i = 0; i < ca->sb.njournal_buckets; i++)
226 if (ja->seq[i] > seq) {
227 seq = ja->seq[i];
228 ja->cur_idx = ja->discard_idx =
229 ja->last_idx = i;
230
231 }
232 }
233
Kent Overstreet808eea9d2013-09-23 23:17:29 -0700234 if (!list_empty(list))
235 c->journal.seq = list_entry(list->prev,
236 struct journal_replay,
237 list)->j.seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700238
239 return 0;
240#undef read_bucket
241}
242
243void bch_journal_mark(struct cache_set *c, struct list_head *list)
244{
245 atomic_t p = { 0 };
246 struct bkey *k;
247 struct journal_replay *i;
248 struct journal *j = &c->journal;
249 uint64_t last = j->seq;
250
251 /*
252 * journal.pin should never fill up - we never write a journal
253 * entry when it would fill up. But if for some reason it does, we
254 * iterate over the list in reverse order so that we can just skip that
255 * refcount instead of bugging.
256 */
257
258 list_for_each_entry_reverse(i, list, list) {
259 BUG_ON(last < i->j.seq);
260 i->pin = NULL;
261
262 while (last-- != i->j.seq)
263 if (fifo_free(&j->pin) > 1) {
264 fifo_push_front(&j->pin, p);
265 atomic_set(&fifo_front(&j->pin), 0);
266 }
267
268 if (fifo_free(&j->pin) > 1) {
269 fifo_push_front(&j->pin, p);
270 i->pin = &fifo_front(&j->pin);
271 atomic_set(i->pin, 1);
272 }
273
274 for (k = i->j.start;
275 k < end(&i->j);
276 k = bkey_next(k)) {
277 unsigned j;
278
279 for (j = 0; j < KEY_PTRS(k); j++) {
280 struct bucket *g = PTR_BUCKET(c, k, j);
281 atomic_inc(&g->pin);
282
283 if (g->prio == BTREE_PRIO &&
284 !ptr_stale(c, k, j))
285 g->prio = INITIAL_PRIO;
286 }
287
288 __bch_btree_mark_key(c, 0, k);
289 }
290 }
291}
292
293int bch_journal_replay(struct cache_set *s, struct list_head *list,
294 struct btree_op *op)
295{
296 int ret = 0, keys = 0, entries = 0;
297 struct bkey *k;
298 struct journal_replay *i =
299 list_entry(list->prev, struct journal_replay, list);
300
301 uint64_t start = i->j.last_seq, end = i->j.seq, n = start;
302
303 list_for_each_entry(i, list, list) {
304 BUG_ON(i->pin && atomic_read(i->pin) != 1);
305
306 if (n != i->j.seq)
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700307 pr_err(
308 "journal entries %llu-%llu missing! (replaying %llu-%llu)\n",
309 n, i->j.seq - 1, start, end);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700310
311 for (k = i->j.start;
312 k < end(&i->j);
313 k = bkey_next(k)) {
314 pr_debug("%s", pkey(k));
315 bkey_copy(op->keys.top, k);
316 bch_keylist_push(&op->keys);
317
318 op->journal = i->pin;
319 atomic_inc(op->journal);
320
321 ret = bch_btree_insert(op, s);
322 if (ret)
323 goto err;
324
325 BUG_ON(!bch_keylist_empty(&op->keys));
326 keys++;
327
328 cond_resched();
329 }
330
331 if (i->pin)
332 atomic_dec(i->pin);
333 n = i->j.seq + 1;
334 entries++;
335 }
336
337 pr_info("journal replay done, %i keys in %i entries, seq %llu",
338 keys, entries, end);
339
340 while (!list_empty(list)) {
341 i = list_first_entry(list, struct journal_replay, list);
342 list_del(&i->list);
343 kfree(i);
344 }
345err:
346 closure_sync(&op->cl);
347 return ret;
348}
349
350/* Journalling */
351
352static void btree_flush_write(struct cache_set *c)
353{
354 /*
355 * Try to find the btree node with that references the oldest journal
356 * entry, best is our current candidate and is locked if non NULL:
357 */
358 struct btree *b, *best = NULL;
359 unsigned iter;
360
361 for_each_cached_btree(b, c, iter) {
362 if (!down_write_trylock(&b->lock))
363 continue;
364
365 if (!btree_node_dirty(b) ||
366 !btree_current_write(b)->journal) {
367 rw_unlock(true, b);
368 continue;
369 }
370
371 if (!best)
372 best = b;
373 else if (journal_pin_cmp(c,
374 btree_current_write(best),
375 btree_current_write(b))) {
376 rw_unlock(true, best);
377 best = b;
378 } else
379 rw_unlock(true, b);
380 }
381
382 if (best)
383 goto out;
384
385 /* We can't find the best btree node, just pick the first */
386 list_for_each_entry(b, &c->btree_cache, list)
387 if (!b->level && btree_node_dirty(b)) {
388 best = b;
389 rw_lock(true, best, best->level);
390 goto found;
391 }
392
393out:
394 if (!best)
395 return;
396found:
397 if (btree_node_dirty(best))
398 bch_btree_write(best, true, NULL);
399 rw_unlock(true, best);
400}
401
402#define last_seq(j) ((j)->seq - fifo_used(&(j)->pin) + 1)
403
404static void journal_discard_endio(struct bio *bio, int error)
405{
406 struct journal_device *ja =
407 container_of(bio, struct journal_device, discard_bio);
408 struct cache *ca = container_of(ja, struct cache, journal);
409
410 atomic_set(&ja->discard_in_flight, DISCARD_DONE);
411
412 closure_wake_up(&ca->set->journal.wait);
413 closure_put(&ca->set->cl);
414}
415
416static void journal_discard_work(struct work_struct *work)
417{
418 struct journal_device *ja =
419 container_of(work, struct journal_device, discard_work);
420
421 submit_bio(0, &ja->discard_bio);
422}
423
424static void do_journal_discard(struct cache *ca)
425{
426 struct journal_device *ja = &ca->journal;
427 struct bio *bio = &ja->discard_bio;
428
429 if (!ca->discard) {
430 ja->discard_idx = ja->last_idx;
431 return;
432 }
433
Kent Overstreet0a22f482013-09-23 23:17:27 -0700434 switch (atomic_read(&ja->discard_in_flight)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700435 case DISCARD_IN_FLIGHT:
436 return;
437
438 case DISCARD_DONE:
439 ja->discard_idx = (ja->discard_idx + 1) %
440 ca->sb.njournal_buckets;
441
442 atomic_set(&ja->discard_in_flight, DISCARD_READY);
443 /* fallthrough */
444
445 case DISCARD_READY:
446 if (ja->discard_idx == ja->last_idx)
447 return;
448
449 atomic_set(&ja->discard_in_flight, DISCARD_IN_FLIGHT);
450
451 bio_init(bio);
452 bio->bi_sector = bucket_to_sector(ca->set,
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700453 ca->sb.d[ja->discard_idx]);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700454 bio->bi_bdev = ca->bdev;
455 bio->bi_rw = REQ_WRITE|REQ_DISCARD;
456 bio->bi_max_vecs = 1;
457 bio->bi_io_vec = bio->bi_inline_vecs;
458 bio->bi_size = bucket_bytes(ca);
459 bio->bi_end_io = journal_discard_endio;
460
461 closure_get(&ca->set->cl);
462 INIT_WORK(&ja->discard_work, journal_discard_work);
463 schedule_work(&ja->discard_work);
464 }
465}
466
467static void journal_reclaim(struct cache_set *c)
468{
469 struct bkey *k = &c->journal.key;
470 struct cache *ca;
471 uint64_t last_seq;
472 unsigned iter, n = 0;
473 atomic_t p;
474
475 while (!atomic_read(&fifo_front(&c->journal.pin)))
476 fifo_pop(&c->journal.pin, p);
477
478 last_seq = last_seq(&c->journal);
479
480 /* Update last_idx */
481
482 for_each_cache(ca, c, iter) {
483 struct journal_device *ja = &ca->journal;
484
485 while (ja->last_idx != ja->cur_idx &&
486 ja->seq[ja->last_idx] < last_seq)
487 ja->last_idx = (ja->last_idx + 1) %
488 ca->sb.njournal_buckets;
489 }
490
491 for_each_cache(ca, c, iter)
492 do_journal_discard(ca);
493
494 if (c->journal.blocks_free)
495 return;
496
497 /*
498 * Allocate:
499 * XXX: Sort by free journal space
500 */
501
502 for_each_cache(ca, c, iter) {
503 struct journal_device *ja = &ca->journal;
504 unsigned next = (ja->cur_idx + 1) % ca->sb.njournal_buckets;
505
506 /* No space available on this device */
507 if (next == ja->discard_idx)
508 continue;
509
510 ja->cur_idx = next;
511 k->ptr[n++] = PTR(0,
512 bucket_to_sector(c, ca->sb.d[ja->cur_idx]),
513 ca->sb.nr_this_dev);
514 }
515
516 bkey_init(k);
517 SET_KEY_PTRS(k, n);
518
519 if (n)
520 c->journal.blocks_free = c->sb.bucket_size >> c->block_bits;
521
522 if (!journal_full(&c->journal))
523 __closure_wake_up(&c->journal.wait);
524}
525
526void bch_journal_next(struct journal *j)
527{
528 atomic_t p = { 1 };
529
530 j->cur = (j->cur == j->w)
531 ? &j->w[1]
532 : &j->w[0];
533
534 /*
535 * The fifo_push() needs to happen at the same time as j->seq is
536 * incremented for last_seq() to be calculated correctly
537 */
538 BUG_ON(!fifo_push(&j->pin, p));
539 atomic_set(&fifo_back(&j->pin), 1);
540
541 j->cur->data->seq = ++j->seq;
542 j->cur->need_write = false;
543 j->cur->data->keys = 0;
544
545 if (fifo_full(&j->pin))
546 pr_debug("journal_pin full (%zu)", fifo_used(&j->pin));
547}
548
549static void journal_write_endio(struct bio *bio, int error)
550{
551 struct journal_write *w = bio->bi_private;
552
553 cache_set_err_on(error, w->c, "journal io error");
554 closure_put(&w->c->journal.io.cl);
555}
556
557static void journal_write(struct closure *);
558
559static void journal_write_done(struct closure *cl)
560{
561 struct journal *j = container_of(cl, struct journal, io.cl);
562 struct cache_set *c = container_of(j, struct cache_set, journal);
563
564 struct journal_write *w = (j->cur == j->w)
565 ? &j->w[1]
566 : &j->w[0];
567
568 __closure_wake_up(&w->wait);
569
570 if (c->journal_delay_ms)
571 closure_delay(&j->io, msecs_to_jiffies(c->journal_delay_ms));
572
573 continue_at(cl, journal_write, system_wq);
574}
575
576static void journal_write_unlocked(struct closure *cl)
Kent Overstreetc19ed232013-03-26 13:49:02 -0700577 __releases(c->journal.lock)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700578{
579 struct cache_set *c = container_of(cl, struct cache_set, journal.io.cl);
580 struct cache *ca;
581 struct journal_write *w = c->journal.cur;
582 struct bkey *k = &c->journal.key;
583 unsigned i, sectors = set_blocks(w->data, c) * c->sb.block_size;
584
585 struct bio *bio;
586 struct bio_list list;
587 bio_list_init(&list);
588
589 if (!w->need_write) {
590 /*
591 * XXX: have to unlock closure before we unlock journal lock,
592 * else we race with bch_journal(). But this way we race
593 * against cache set unregister. Doh.
594 */
595 set_closure_fn(cl, NULL, NULL);
596 closure_sub(cl, CLOSURE_RUNNING + 1);
597 spin_unlock(&c->journal.lock);
598 return;
599 } else if (journal_full(&c->journal)) {
600 journal_reclaim(c);
601 spin_unlock(&c->journal.lock);
602
603 btree_flush_write(c);
604 continue_at(cl, journal_write, system_wq);
605 }
606
607 c->journal.blocks_free -= set_blocks(w->data, c);
608
609 w->data->btree_level = c->root->level;
610
611 bkey_copy(&w->data->btree_root, &c->root->key);
612 bkey_copy(&w->data->uuid_bucket, &c->uuid_bucket);
613
614 for_each_cache(ca, c, i)
615 w->data->prio_bucket[ca->sb.nr_this_dev] = ca->prio_buckets[0];
616
617 w->data->magic = jset_magic(c);
618 w->data->version = BCACHE_JSET_VERSION;
619 w->data->last_seq = last_seq(&c->journal);
620 w->data->csum = csum_set(w->data);
621
622 for (i = 0; i < KEY_PTRS(k); i++) {
623 ca = PTR_CACHE(c, k, i);
624 bio = &ca->journal.bio;
625
626 atomic_long_add(sectors, &ca->meta_sectors_written);
627
628 bio_reset(bio);
629 bio->bi_sector = PTR_OFFSET(k, i);
630 bio->bi_bdev = ca->bdev;
Kent Overstreetae61fd442013-06-26 17:25:38 -0700631 bio->bi_rw = REQ_WRITE|REQ_SYNC|REQ_META|REQ_FLUSH|REQ_FUA;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700632 bio->bi_size = sectors << 9;
633
634 bio->bi_end_io = journal_write_endio;
635 bio->bi_private = w;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600636 bch_bio_map(bio, w->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700637
638 trace_bcache_journal_write(bio);
639 bio_list_add(&list, bio);
640
641 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + sectors);
642
643 ca->journal.seq[ca->journal.cur_idx] = w->data->seq;
644 }
645
646 atomic_dec_bug(&fifo_back(&c->journal.pin));
647 bch_journal_next(&c->journal);
648 journal_reclaim(c);
649
650 spin_unlock(&c->journal.lock);
651
652 while ((bio = bio_list_pop(&list)))
653 closure_bio_submit(bio, cl, c->cache[0]);
654
655 continue_at(cl, journal_write_done, NULL);
656}
657
658static void journal_write(struct closure *cl)
659{
660 struct cache_set *c = container_of(cl, struct cache_set, journal.io.cl);
661
662 spin_lock(&c->journal.lock);
663 journal_write_unlocked(cl);
664}
665
666static void __journal_try_write(struct cache_set *c, bool noflush)
Kent Overstreetc19ed232013-03-26 13:49:02 -0700667 __releases(c->journal.lock)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700668{
669 struct closure *cl = &c->journal.io.cl;
670
671 if (!closure_trylock(cl, &c->cl))
672 spin_unlock(&c->journal.lock);
673 else if (noflush && journal_full(&c->journal)) {
674 spin_unlock(&c->journal.lock);
675 continue_at(cl, journal_write, system_wq);
676 } else
677 journal_write_unlocked(cl);
678}
679
680#define journal_try_write(c) __journal_try_write(c, false)
681
682void bch_journal_meta(struct cache_set *c, struct closure *cl)
683{
684 struct journal_write *w;
685
686 if (CACHE_SYNC(&c->sb)) {
687 spin_lock(&c->journal.lock);
688
689 w = c->journal.cur;
690 w->need_write = true;
691
692 if (cl)
693 BUG_ON(!closure_wait(&w->wait, cl));
694
Kent Overstreet77dbabe2013-09-23 23:17:32 -0700695 closure_flush(&c->journal.io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700696 __journal_try_write(c, true);
697 }
698}
699
700/*
701 * Entry point to the journalling code - bio_insert() and btree_invalidate()
702 * pass bch_journal() a list of keys to be journalled, and then
703 * bch_journal() hands those same keys off to btree_insert_async()
704 */
705
706void bch_journal(struct closure *cl)
707{
708 struct btree_op *op = container_of(cl, struct btree_op, cl);
709 struct cache_set *c = op->c;
710 struct journal_write *w;
711 size_t b, n = ((uint64_t *) op->keys.top) - op->keys.list;
712
713 if (op->type != BTREE_INSERT ||
714 !CACHE_SYNC(&c->sb))
715 goto out;
716
717 /*
718 * If we're looping because we errored, might already be waiting on
719 * another journal write:
720 */
721 while (atomic_read(&cl->parent->remaining) & CLOSURE_WAITING)
722 closure_sync(cl->parent);
723
724 spin_lock(&c->journal.lock);
725
726 if (journal_full(&c->journal)) {
727 /* XXX: tracepoint */
728 closure_wait(&c->journal.wait, cl);
729
730 journal_reclaim(c);
731 spin_unlock(&c->journal.lock);
732
733 btree_flush_write(c);
734 continue_at(cl, bch_journal, bcache_wq);
735 }
736
737 w = c->journal.cur;
738 w->need_write = true;
739 b = __set_blocks(w->data, w->data->keys + n, c);
740
741 if (b * c->sb.block_size > PAGE_SECTORS << JSET_BITS ||
742 b > c->journal.blocks_free) {
743 /* XXX: If we were inserting so many keys that they won't fit in
744 * an _empty_ journal write, we'll deadlock. For now, handle
745 * this in bch_keylist_realloc() - but something to think about.
746 */
747 BUG_ON(!w->data->keys);
748
749 /* XXX: tracepoint */
750 BUG_ON(!closure_wait(&w->wait, cl));
751
752 closure_flush(&c->journal.io);
753
754 journal_try_write(c);
755 continue_at(cl, bch_journal, bcache_wq);
756 }
757
758 memcpy(end(w->data), op->keys.list, n * sizeof(uint64_t));
759 w->data->keys += n;
760
761 op->journal = &fifo_back(&c->journal.pin);
762 atomic_inc(op->journal);
763
764 if (op->flush_journal) {
765 closure_flush(&c->journal.io);
766 closure_wait(&w->wait, cl->parent);
767 }
768
769 journal_try_write(c);
770out:
771 bch_btree_insert_async(cl);
772}
773
774void bch_journal_free(struct cache_set *c)
775{
776 free_pages((unsigned long) c->journal.w[1].data, JSET_BITS);
777 free_pages((unsigned long) c->journal.w[0].data, JSET_BITS);
778 free_fifo(&c->journal.pin);
779}
780
781int bch_journal_alloc(struct cache_set *c)
782{
783 struct journal *j = &c->journal;
784
785 closure_init_unlocked(&j->io);
786 spin_lock_init(&j->lock);
787
788 c->journal_delay_ms = 100;
789
790 j->w[0].c = c;
791 j->w[1].c = c;
792
793 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
794 !(j->w[0].data = (void *) __get_free_pages(GFP_KERNEL, JSET_BITS)) ||
795 !(j->w[1].data = (void *) __get_free_pages(GFP_KERNEL, JSET_BITS)))
796 return -ENOMEM;
797
798 return 0;
799}