1 // SPDX-License-Identifier: GPL-2.0
3 * bcache journalling code, for btree insertions
5 * Copyright 2012 Google, Inc.
13 #include <trace/events/bcache.h>
16 * Journal replay/recovery:
18 * This code is all driven from run_cache_set(); we first read the journal
19 * entries, do some other stuff, then we mark all the keys in the journal
20 * entries (same as garbage collection would), then we replay them - reinserting
21 * them into the cache in precisely the same order as they appear in the
24 * We only journal keys that go in leaf nodes, which simplifies things quite a
28 static void journal_read_endio(struct bio *bio)
30 struct closure *cl = bio->bi_private;
35 static int journal_read_bucket(struct cache *ca, struct list_head *list,
36 unsigned int bucket_index)
38 struct journal_device *ja = &ca->journal;
39 struct bio *bio = &ja->bio;
41 struct journal_replay *i;
42 struct jset *j, *data = ca->set->journal.w[0].data;
44 unsigned int len, left, offset = 0;
46 sector_t bucket = bucket_to_sector(ca->set, ca->sb.d[bucket_index]);
48 closure_init_stack(&cl);
50 pr_debug("reading %u\n", bucket_index);
52 while (offset < ca->sb.bucket_size) {
53 reread: left = ca->sb.bucket_size - offset;
54 len = min_t(unsigned int, left, PAGE_SECTORS << JSET_BITS);
56 bio_reset(bio, ca->bdev, REQ_OP_READ);
57 bio->bi_iter.bi_sector = bucket + offset;
58 bio->bi_iter.bi_size = len << 9;
60 bio->bi_end_io = journal_read_endio;
61 bio->bi_private = &cl;
62 bch_bio_map(bio, data);
64 closure_bio_submit(ca->set, bio, &cl);
67 /* This function could be simpler now since we no longer write
68 * journal entries that overlap bucket boundaries; this means
69 * the start of a bucket will always have a valid journal entry
70 * if it has any journal entries at all.
75 struct list_head *where;
76 size_t blocks, bytes = set_bytes(j);
78 if (j->magic != jset_magic(&ca->sb)) {
79 pr_debug("%u: bad magic\n", bucket_index);
83 if (bytes > left << 9 ||
84 bytes > PAGE_SIZE << JSET_BITS) {
85 pr_info("%u: too big, %zu bytes, offset %u\n",
86 bucket_index, bytes, offset);
93 if (j->csum != csum_set(j)) {
94 pr_info("%u: bad csum, %zu bytes, offset %u\n",
95 bucket_index, bytes, offset);
99 blocks = set_blocks(j, block_bytes(ca));
102 * Nodes in 'list' are in linear increasing order of
103 * i->j.seq, the node on head has the smallest (oldest)
104 * journal seq, the node on tail has the biggest
105 * (latest) journal seq.
109 * Check from the oldest jset for last_seq. If
110 * i->j.seq < j->last_seq, it means the oldest jset
111 * in list is expired and useless, remove it from
112 * this list. Otherwise, j is a candidate jset for
113 * further following checks.
115 while (!list_empty(list)) {
116 i = list_first_entry(list,
117 struct journal_replay, list);
118 if (i->j.seq >= j->last_seq)
124 /* iterate list in reverse order (from latest jset) */
125 list_for_each_entry_reverse(i, list, list) {
126 if (j->seq == i->j.seq)
130 * if j->seq is less than any i->j.last_seq
131 * in list, j is an expired and useless jset.
133 if (j->seq < i->j.last_seq)
137 * 'where' points to first jset in list which
140 if (j->seq > i->j.seq) {
148 i = kmalloc(offsetof(struct journal_replay, j) +
152 memcpy(&i->j, j, bytes);
153 /* Add to the location after 'where' points to */
154 list_add(&i->list, where);
157 if (j->seq > ja->seq[bucket_index])
158 ja->seq[bucket_index] = j->seq;
160 offset += blocks * ca->sb.block_size;
161 len -= blocks * ca->sb.block_size;
162 j = ((void *) j) + blocks * block_bytes(ca);
169 int bch_journal_read(struct cache_set *c, struct list_head *list)
171 #define read_bucket(b) \
173 ret = journal_read_bucket(ca, list, b); \
174 __set_bit(b, bitmap); \
180 struct cache *ca = c->cache;
182 struct journal_device *ja = &ca->journal;
183 DECLARE_BITMAP(bitmap, SB_JOURNAL_BUCKETS);
184 unsigned int i, l, r, m;
187 bitmap_zero(bitmap, SB_JOURNAL_BUCKETS);
188 pr_debug("%u journal buckets\n", ca->sb.njournal_buckets);
191 * Read journal buckets ordered by golden ratio hash to quickly
192 * find a sequence of buckets with valid journal entries
194 for (i = 0; i < ca->sb.njournal_buckets; i++) {
196 * We must try the index l with ZERO first for
197 * correctness due to the scenario that the journal
198 * bucket is circular buffer which might have wrapped
200 l = (i * 2654435769U) % ca->sb.njournal_buckets;
202 if (test_bit(l, bitmap))
210 * If that fails, check all the buckets we haven't checked
213 pr_debug("falling back to linear search\n");
215 for_each_clear_bit(l, bitmap, ca->sb.njournal_buckets)
219 /* no journal entries on this device? */
220 if (l == ca->sb.njournal_buckets)
223 BUG_ON(list_empty(list));
227 r = find_next_bit(bitmap, ca->sb.njournal_buckets, l + 1);
228 pr_debug("starting binary search, l %u r %u\n", l, r);
231 seq = list_entry(list->prev, struct journal_replay,
237 if (seq != list_entry(list->prev, struct journal_replay,
245 * Read buckets in reverse order until we stop finding more
248 pr_debug("finishing up: m %u njournal_buckets %u\n",
249 m, ca->sb.njournal_buckets);
254 l = ca->sb.njournal_buckets - 1;
259 if (test_bit(l, bitmap))
268 for (i = 0; i < ca->sb.njournal_buckets; i++)
269 if (ja->seq[i] > seq) {
272 * When journal_reclaim() goes to allocate for
273 * the first time, it'll use the bucket after
277 ja->last_idx = ja->discard_idx = (i + 1) %
278 ca->sb.njournal_buckets;
283 if (!list_empty(list))
284 c->journal.seq = list_entry(list->prev,
285 struct journal_replay,
292 void bch_journal_mark(struct cache_set *c, struct list_head *list)
296 struct journal_replay *i;
297 struct journal *j = &c->journal;
298 uint64_t last = j->seq;
301 * journal.pin should never fill up - we never write a journal
302 * entry when it would fill up. But if for some reason it does, we
303 * iterate over the list in reverse order so that we can just skip that
304 * refcount instead of bugging.
307 list_for_each_entry_reverse(i, list, list) {
308 BUG_ON(last < i->j.seq);
311 while (last-- != i->j.seq)
312 if (fifo_free(&j->pin) > 1) {
313 fifo_push_front(&j->pin, p);
314 atomic_set(&fifo_front(&j->pin), 0);
317 if (fifo_free(&j->pin) > 1) {
318 fifo_push_front(&j->pin, p);
319 i->pin = &fifo_front(&j->pin);
320 atomic_set(i->pin, 1);
324 k < bset_bkey_last(&i->j);
326 if (!__bch_extent_invalid(c, k)) {
329 for (j = 0; j < KEY_PTRS(k); j++)
330 if (ptr_available(c, k, j))
331 atomic_inc(&PTR_BUCKET(c, k, j)->pin);
333 bch_initial_mark_key(c, 0, k);
338 static bool is_discard_enabled(struct cache_set *s)
340 struct cache *ca = s->cache;
348 int bch_journal_replay(struct cache_set *s, struct list_head *list)
350 int ret = 0, keys = 0, entries = 0;
352 struct journal_replay *i =
353 list_entry(list->prev, struct journal_replay, list);
355 uint64_t start = i->j.last_seq, end = i->j.seq, n = start;
356 struct keylist keylist;
358 list_for_each_entry(i, list, list) {
359 BUG_ON(i->pin && atomic_read(i->pin) != 1);
362 if (n == start && is_discard_enabled(s))
363 pr_info("journal entries %llu-%llu may be discarded! (replaying %llu-%llu)\n",
364 n, i->j.seq - 1, start, end);
366 pr_err("journal entries %llu-%llu missing! (replaying %llu-%llu)\n",
367 n, i->j.seq - 1, start, end);
374 k < bset_bkey_last(&i->j);
376 trace_bcache_journal_replay_key(k);
378 bch_keylist_init_single(&keylist, k);
380 ret = bch_btree_insert(s, &keylist, i->pin, NULL);
384 BUG_ON(!bch_keylist_empty(&keylist));
396 pr_info("journal replay done, %i keys in %i entries, seq %llu\n",
399 while (!list_empty(list)) {
400 i = list_first_entry(list, struct journal_replay, list);
410 static void btree_flush_write(struct cache_set *c)
412 struct btree *b, *t, *btree_nodes[BTREE_FLUSH_NR];
415 atomic_t *fifo_front_p, *now_fifo_front_p;
418 if (c->journal.btree_flushing)
421 spin_lock(&c->journal.flush_write_lock);
422 if (c->journal.btree_flushing) {
423 spin_unlock(&c->journal.flush_write_lock);
426 c->journal.btree_flushing = true;
427 spin_unlock(&c->journal.flush_write_lock);
429 /* get the oldest journal entry and check its refcount */
430 spin_lock(&c->journal.lock);
431 fifo_front_p = &fifo_front(&c->journal.pin);
432 ref_nr = atomic_read(fifo_front_p);
435 * do nothing if no btree node references
436 * the oldest journal entry
438 spin_unlock(&c->journal.lock);
441 spin_unlock(&c->journal.lock);
443 mask = c->journal.pin.mask;
445 atomic_long_inc(&c->flush_write);
446 memset(btree_nodes, 0, sizeof(btree_nodes));
448 mutex_lock(&c->bucket_lock);
449 list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) {
451 * It is safe to get now_fifo_front_p without holding
452 * c->journal.lock here, because we don't need to know
453 * the exactly accurate value, just check whether the
454 * front pointer of c->journal.pin is changed.
456 now_fifo_front_p = &fifo_front(&c->journal.pin);
458 * If the oldest journal entry is reclaimed and front
459 * pointer of c->journal.pin changes, it is unnecessary
460 * to scan c->btree_cache anymore, just quit the loop and
461 * flush out what we have already.
463 if (now_fifo_front_p != fifo_front_p)
466 * quit this loop if all matching btree nodes are
467 * scanned and record in btree_nodes[] already.
469 ref_nr = atomic_read(fifo_front_p);
473 if (btree_node_journal_flush(b))
474 pr_err("BUG: flush_write bit should not be set here!\n");
476 mutex_lock(&b->write_lock);
478 if (!btree_node_dirty(b)) {
479 mutex_unlock(&b->write_lock);
483 if (!btree_current_write(b)->journal) {
484 mutex_unlock(&b->write_lock);
489 * Only select the btree node which exactly references
490 * the oldest journal entry.
492 * If the journal entry pointed by fifo_front_p is
493 * reclaimed in parallel, don't worry:
494 * - the list_for_each_xxx loop will quit when checking
495 * next now_fifo_front_p.
496 * - If there are matched nodes recorded in btree_nodes[],
497 * they are clean now (this is why and how the oldest
498 * journal entry can be reclaimed). These selected nodes
499 * will be ignored and skipped in the following for-loop.
501 if (((btree_current_write(b)->journal - fifo_front_p) &
503 mutex_unlock(&b->write_lock);
507 set_btree_node_journal_flush(b);
509 mutex_unlock(&b->write_lock);
511 btree_nodes[nr++] = b;
513 * To avoid holding c->bucket_lock too long time,
514 * only scan for BTREE_FLUSH_NR matched btree nodes
515 * at most. If there are more btree nodes reference
516 * the oldest journal entry, try to flush them next
517 * time when btree_flush_write() is called.
519 if (nr == BTREE_FLUSH_NR)
522 mutex_unlock(&c->bucket_lock);
524 for (i = 0; i < nr; i++) {
527 pr_err("BUG: btree_nodes[%d] is NULL\n", i);
531 /* safe to check without holding b->write_lock */
532 if (!btree_node_journal_flush(b)) {
533 pr_err("BUG: bnode %p: journal_flush bit cleaned\n", b);
537 mutex_lock(&b->write_lock);
538 if (!btree_current_write(b)->journal) {
539 clear_bit(BTREE_NODE_journal_flush, &b->flags);
540 mutex_unlock(&b->write_lock);
541 pr_debug("bnode %p: written by others\n", b);
545 if (!btree_node_dirty(b)) {
546 clear_bit(BTREE_NODE_journal_flush, &b->flags);
547 mutex_unlock(&b->write_lock);
548 pr_debug("bnode %p: dirty bit cleaned by others\n", b);
552 __bch_btree_node_write(b, NULL);
553 clear_bit(BTREE_NODE_journal_flush, &b->flags);
554 mutex_unlock(&b->write_lock);
558 spin_lock(&c->journal.flush_write_lock);
559 c->journal.btree_flushing = false;
560 spin_unlock(&c->journal.flush_write_lock);
563 #define last_seq(j) ((j)->seq - fifo_used(&(j)->pin) + 1)
565 static void journal_discard_endio(struct bio *bio)
567 struct journal_device *ja =
568 container_of(bio, struct journal_device, discard_bio);
569 struct cache *ca = container_of(ja, struct cache, journal);
571 atomic_set(&ja->discard_in_flight, DISCARD_DONE);
573 closure_wake_up(&ca->set->journal.wait);
574 closure_put(&ca->set->cl);
577 static void journal_discard_work(struct work_struct *work)
579 struct journal_device *ja =
580 container_of(work, struct journal_device, discard_work);
582 submit_bio(&ja->discard_bio);
585 static void do_journal_discard(struct cache *ca)
587 struct journal_device *ja = &ca->journal;
588 struct bio *bio = &ja->discard_bio;
591 ja->discard_idx = ja->last_idx;
595 switch (atomic_read(&ja->discard_in_flight)) {
596 case DISCARD_IN_FLIGHT:
600 ja->discard_idx = (ja->discard_idx + 1) %
601 ca->sb.njournal_buckets;
603 atomic_set(&ja->discard_in_flight, DISCARD_READY);
607 if (ja->discard_idx == ja->last_idx)
610 atomic_set(&ja->discard_in_flight, DISCARD_IN_FLIGHT);
612 bio_init(bio, ca->bdev, bio->bi_inline_vecs, 1, REQ_OP_DISCARD);
613 bio->bi_iter.bi_sector = bucket_to_sector(ca->set,
614 ca->sb.d[ja->discard_idx]);
615 bio->bi_iter.bi_size = bucket_bytes(ca);
616 bio->bi_end_io = journal_discard_endio;
618 closure_get(&ca->set->cl);
619 INIT_WORK(&ja->discard_work, journal_discard_work);
620 queue_work(bch_journal_wq, &ja->discard_work);
624 static void journal_reclaim(struct cache_set *c)
626 struct bkey *k = &c->journal.key;
627 struct cache *ca = c->cache;
630 struct journal_device *ja = &ca->journal;
631 atomic_t p __maybe_unused;
633 atomic_long_inc(&c->reclaim);
635 while (!atomic_read(&fifo_front(&c->journal.pin)))
636 fifo_pop(&c->journal.pin, p);
638 last_seq = last_seq(&c->journal);
640 /* Update last_idx */
642 while (ja->last_idx != ja->cur_idx &&
643 ja->seq[ja->last_idx] < last_seq)
644 ja->last_idx = (ja->last_idx + 1) %
645 ca->sb.njournal_buckets;
647 do_journal_discard(ca);
649 if (c->journal.blocks_free)
652 next = (ja->cur_idx + 1) % ca->sb.njournal_buckets;
653 /* No space available on this device */
654 if (next == ja->discard_idx)
658 k->ptr[0] = MAKE_PTR(0,
659 bucket_to_sector(c, ca->sb.d[ja->cur_idx]),
661 atomic_long_inc(&c->reclaimed_journal_buckets);
665 c->journal.blocks_free = ca->sb.bucket_size >> c->block_bits;
668 if (!journal_full(&c->journal))
669 __closure_wake_up(&c->journal.wait);
672 void bch_journal_next(struct journal *j)
676 j->cur = (j->cur == j->w)
681 * The fifo_push() needs to happen at the same time as j->seq is
682 * incremented for last_seq() to be calculated correctly
684 BUG_ON(!fifo_push(&j->pin, p));
685 atomic_set(&fifo_back(&j->pin), 1);
687 j->cur->data->seq = ++j->seq;
688 j->cur->dirty = false;
689 j->cur->need_write = false;
690 j->cur->data->keys = 0;
692 if (fifo_full(&j->pin))
693 pr_debug("journal_pin full (%zu)\n", fifo_used(&j->pin));
696 static void journal_write_endio(struct bio *bio)
698 struct journal_write *w = bio->bi_private;
700 cache_set_err_on(bio->bi_status, w->c, "journal io error");
701 closure_put(&w->c->journal.io);
704 static void journal_write(struct closure *cl);
706 static void journal_write_done(struct closure *cl)
708 struct journal *j = container_of(cl, struct journal, io);
709 struct journal_write *w = (j->cur == j->w)
713 __closure_wake_up(&w->wait);
714 continue_at_nobarrier(cl, journal_write, bch_journal_wq);
717 static void journal_write_unlock(struct closure *cl)
718 __releases(&c->journal.lock)
720 struct cache_set *c = container_of(cl, struct cache_set, journal.io);
722 c->journal.io_in_flight = 0;
723 spin_unlock(&c->journal.lock);
726 static void journal_write_unlocked(struct closure *cl)
727 __releases(c->journal.lock)
729 struct cache_set *c = container_of(cl, struct cache_set, journal.io);
730 struct cache *ca = c->cache;
731 struct journal_write *w = c->journal.cur;
732 struct bkey *k = &c->journal.key;
733 unsigned int i, sectors = set_blocks(w->data, block_bytes(ca)) *
737 struct bio_list list;
739 bio_list_init(&list);
741 if (!w->need_write) {
742 closure_return_with_destructor(cl, journal_write_unlock);
744 } else if (journal_full(&c->journal)) {
746 spin_unlock(&c->journal.lock);
748 btree_flush_write(c);
749 continue_at(cl, journal_write, bch_journal_wq);
753 c->journal.blocks_free -= set_blocks(w->data, block_bytes(ca));
755 w->data->btree_level = c->root->level;
757 bkey_copy(&w->data->btree_root, &c->root->key);
758 bkey_copy(&w->data->uuid_bucket, &c->uuid_bucket);
760 w->data->prio_bucket[ca->sb.nr_this_dev] = ca->prio_buckets[0];
761 w->data->magic = jset_magic(&ca->sb);
762 w->data->version = BCACHE_JSET_VERSION;
763 w->data->last_seq = last_seq(&c->journal);
764 w->data->csum = csum_set(w->data);
766 for (i = 0; i < KEY_PTRS(k); i++) {
768 bio = &ca->journal.bio;
770 atomic_long_add(sectors, &ca->meta_sectors_written);
772 bio_reset(bio, ca->bdev, REQ_OP_WRITE |
773 REQ_SYNC | REQ_META | REQ_PREFLUSH | REQ_FUA);
774 bio->bi_iter.bi_sector = PTR_OFFSET(k, i);
775 bio->bi_iter.bi_size = sectors << 9;
777 bio->bi_end_io = journal_write_endio;
779 bch_bio_map(bio, w->data);
781 trace_bcache_journal_write(bio, w->data->keys);
782 bio_list_add(&list, bio);
784 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + sectors);
786 ca->journal.seq[ca->journal.cur_idx] = w->data->seq;
789 /* If KEY_PTRS(k) == 0, this jset gets lost in air */
792 atomic_dec_bug(&fifo_back(&c->journal.pin));
793 bch_journal_next(&c->journal);
796 spin_unlock(&c->journal.lock);
798 while ((bio = bio_list_pop(&list)))
799 closure_bio_submit(c, bio, cl);
801 continue_at(cl, journal_write_done, NULL);
804 static void journal_write(struct closure *cl)
806 struct cache_set *c = container_of(cl, struct cache_set, journal.io);
808 spin_lock(&c->journal.lock);
809 journal_write_unlocked(cl);
812 static void journal_try_write(struct cache_set *c)
813 __releases(c->journal.lock)
815 struct closure *cl = &c->journal.io;
816 struct journal_write *w = c->journal.cur;
818 w->need_write = true;
820 if (!c->journal.io_in_flight) {
821 c->journal.io_in_flight = 1;
822 closure_call(cl, journal_write_unlocked, NULL, &c->cl);
824 spin_unlock(&c->journal.lock);
828 static struct journal_write *journal_wait_for_write(struct cache_set *c,
830 __acquires(&c->journal.lock)
835 struct cache *ca = c->cache;
837 closure_init_stack(&cl);
839 spin_lock(&c->journal.lock);
842 struct journal_write *w = c->journal.cur;
844 sectors = __set_blocks(w->data, w->data->keys + nkeys,
845 block_bytes(ca)) * ca->sb.block_size;
847 if (sectors <= min_t(size_t,
848 c->journal.blocks_free * ca->sb.block_size,
849 PAGE_SECTORS << JSET_BITS))
853 closure_wait(&c->journal.wait, &cl);
855 if (!journal_full(&c->journal)) {
857 trace_bcache_journal_entry_full(c);
860 * XXX: If we were inserting so many keys that they
861 * won't fit in an _empty_ journal write, we'll
862 * deadlock. For now, handle this in
863 * bch_keylist_realloc() - but something to think about.
865 BUG_ON(!w->data->keys);
867 journal_try_write(c); /* unlocks */
870 trace_bcache_journal_full(c);
873 spin_unlock(&c->journal.lock);
875 btree_flush_write(c);
879 spin_lock(&c->journal.lock);
884 static void journal_write_work(struct work_struct *work)
886 struct cache_set *c = container_of(to_delayed_work(work),
889 spin_lock(&c->journal.lock);
890 if (c->journal.cur->dirty)
891 journal_try_write(c);
893 spin_unlock(&c->journal.lock);
897 * Entry point to the journalling code - bio_insert() and btree_invalidate()
898 * pass bch_journal() a list of keys to be journalled, and then
899 * bch_journal() hands those same keys off to btree_insert_async()
902 atomic_t *bch_journal(struct cache_set *c,
903 struct keylist *keys,
904 struct closure *parent)
906 struct journal_write *w;
909 /* No journaling if CACHE_SET_IO_DISABLE set already */
910 if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags)))
913 if (!CACHE_SYNC(&c->cache->sb))
916 w = journal_wait_for_write(c, bch_keylist_nkeys(keys));
918 memcpy(bset_bkey_last(w->data), keys->keys, bch_keylist_bytes(keys));
919 w->data->keys += bch_keylist_nkeys(keys);
921 ret = &fifo_back(&c->journal.pin);
925 closure_wait(&w->wait, parent);
926 journal_try_write(c);
927 } else if (!w->dirty) {
929 queue_delayed_work(bch_flush_wq, &c->journal.work,
930 msecs_to_jiffies(c->journal_delay_ms));
931 spin_unlock(&c->journal.lock);
933 spin_unlock(&c->journal.lock);
940 void bch_journal_meta(struct cache_set *c, struct closure *cl)
945 bch_keylist_init(&keys);
947 ref = bch_journal(c, &keys, cl);
952 void bch_journal_free(struct cache_set *c)
954 free_pages((unsigned long) c->journal.w[1].data, JSET_BITS);
955 free_pages((unsigned long) c->journal.w[0].data, JSET_BITS);
956 free_fifo(&c->journal.pin);
959 int bch_journal_alloc(struct cache_set *c)
961 struct journal *j = &c->journal;
963 spin_lock_init(&j->lock);
964 spin_lock_init(&j->flush_write_lock);
965 INIT_DELAYED_WORK(&j->work, journal_write_work);
967 c->journal_delay_ms = 100;
972 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
973 !(j->w[0].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)) ||
974 !(j->w[1].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)))