18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * bcache journalling code, for btree insertions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2012 Google, Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "bcache.h"
98c2ecf20Sopenharmony_ci#include "btree.h"
108c2ecf20Sopenharmony_ci#include "debug.h"
118c2ecf20Sopenharmony_ci#include "extents.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <trace/events/bcache.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Journal replay/recovery:
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * This code is all driven from run_cache_set(); we first read the journal
198c2ecf20Sopenharmony_ci * entries, do some other stuff, then we mark all the keys in the journal
208c2ecf20Sopenharmony_ci * entries (same as garbage collection would), then we replay them - reinserting
218c2ecf20Sopenharmony_ci * them into the cache in precisely the same order as they appear in the
228c2ecf20Sopenharmony_ci * journal.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * We only journal keys that go in leaf nodes, which simplifies things quite a
258c2ecf20Sopenharmony_ci * bit.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic void journal_read_endio(struct bio *bio)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct closure *cl = bio->bi_private;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	closure_put(cl);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int journal_read_bucket(struct cache *ca, struct list_head *list,
368c2ecf20Sopenharmony_ci			       unsigned int bucket_index)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct journal_device *ja = &ca->journal;
398c2ecf20Sopenharmony_ci	struct bio *bio = &ja->bio;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	struct journal_replay *i;
428c2ecf20Sopenharmony_ci	struct jset *j, *data = ca->set->journal.w[0].data;
438c2ecf20Sopenharmony_ci	struct closure cl;
448c2ecf20Sopenharmony_ci	unsigned int len, left, offset = 0;
458c2ecf20Sopenharmony_ci	int ret = 0;
468c2ecf20Sopenharmony_ci	sector_t bucket = bucket_to_sector(ca->set, ca->sb.d[bucket_index]);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	closure_init_stack(&cl);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	pr_debug("reading %u\n", bucket_index);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	while (offset < ca->sb.bucket_size) {
538c2ecf20Sopenharmony_cireread:		left = ca->sb.bucket_size - offset;
548c2ecf20Sopenharmony_ci		len = min_t(unsigned int, left, PAGE_SECTORS << JSET_BITS);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		bio_reset(bio);
578c2ecf20Sopenharmony_ci		bio->bi_iter.bi_sector	= bucket + offset;
588c2ecf20Sopenharmony_ci		bio_set_dev(bio, ca->bdev);
598c2ecf20Sopenharmony_ci		bio->bi_iter.bi_size	= len << 9;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci		bio->bi_end_io	= journal_read_endio;
628c2ecf20Sopenharmony_ci		bio->bi_private = &cl;
638c2ecf20Sopenharmony_ci		bio_set_op_attrs(bio, REQ_OP_READ, 0);
648c2ecf20Sopenharmony_ci		bch_bio_map(bio, data);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci		closure_bio_submit(ca->set, bio, &cl);
678c2ecf20Sopenharmony_ci		closure_sync(&cl);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci		/* This function could be simpler now since we no longer write
708c2ecf20Sopenharmony_ci		 * journal entries that overlap bucket boundaries; this means
718c2ecf20Sopenharmony_ci		 * the start of a bucket will always have a valid journal entry
728c2ecf20Sopenharmony_ci		 * if it has any journal entries at all.
738c2ecf20Sopenharmony_ci		 */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		j = data;
768c2ecf20Sopenharmony_ci		while (len) {
778c2ecf20Sopenharmony_ci			struct list_head *where;
788c2ecf20Sopenharmony_ci			size_t blocks, bytes = set_bytes(j);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci			if (j->magic != jset_magic(&ca->sb)) {
818c2ecf20Sopenharmony_ci				pr_debug("%u: bad magic\n", bucket_index);
828c2ecf20Sopenharmony_ci				return ret;
838c2ecf20Sopenharmony_ci			}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci			if (bytes > left << 9 ||
868c2ecf20Sopenharmony_ci			    bytes > PAGE_SIZE << JSET_BITS) {
878c2ecf20Sopenharmony_ci				pr_info("%u: too big, %zu bytes, offset %u\n",
888c2ecf20Sopenharmony_ci					bucket_index, bytes, offset);
898c2ecf20Sopenharmony_ci				return ret;
908c2ecf20Sopenharmony_ci			}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci			if (bytes > len << 9)
938c2ecf20Sopenharmony_ci				goto reread;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci			if (j->csum != csum_set(j)) {
968c2ecf20Sopenharmony_ci				pr_info("%u: bad csum, %zu bytes, offset %u\n",
978c2ecf20Sopenharmony_ci					bucket_index, bytes, offset);
988c2ecf20Sopenharmony_ci				return ret;
998c2ecf20Sopenharmony_ci			}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci			blocks = set_blocks(j, block_bytes(ca));
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci			/*
1048c2ecf20Sopenharmony_ci			 * Nodes in 'list' are in linear increasing order of
1058c2ecf20Sopenharmony_ci			 * i->j.seq, the node on head has the smallest (oldest)
1068c2ecf20Sopenharmony_ci			 * journal seq, the node on tail has the biggest
1078c2ecf20Sopenharmony_ci			 * (latest) journal seq.
1088c2ecf20Sopenharmony_ci			 */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci			/*
1118c2ecf20Sopenharmony_ci			 * Check from the oldest jset for last_seq. If
1128c2ecf20Sopenharmony_ci			 * i->j.seq < j->last_seq, it means the oldest jset
1138c2ecf20Sopenharmony_ci			 * in list is expired and useless, remove it from
1148c2ecf20Sopenharmony_ci			 * this list. Otherwise, j is a condidate jset for
1158c2ecf20Sopenharmony_ci			 * further following checks.
1168c2ecf20Sopenharmony_ci			 */
1178c2ecf20Sopenharmony_ci			while (!list_empty(list)) {
1188c2ecf20Sopenharmony_ci				i = list_first_entry(list,
1198c2ecf20Sopenharmony_ci					struct journal_replay, list);
1208c2ecf20Sopenharmony_ci				if (i->j.seq >= j->last_seq)
1218c2ecf20Sopenharmony_ci					break;
1228c2ecf20Sopenharmony_ci				list_del(&i->list);
1238c2ecf20Sopenharmony_ci				kfree(i);
1248c2ecf20Sopenharmony_ci			}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci			/* iterate list in reverse order (from latest jset) */
1278c2ecf20Sopenharmony_ci			list_for_each_entry_reverse(i, list, list) {
1288c2ecf20Sopenharmony_ci				if (j->seq == i->j.seq)
1298c2ecf20Sopenharmony_ci					goto next_set;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci				/*
1328c2ecf20Sopenharmony_ci				 * if j->seq is less than any i->j.last_seq
1338c2ecf20Sopenharmony_ci				 * in list, j is an expired and useless jset.
1348c2ecf20Sopenharmony_ci				 */
1358c2ecf20Sopenharmony_ci				if (j->seq < i->j.last_seq)
1368c2ecf20Sopenharmony_ci					goto next_set;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci				/*
1398c2ecf20Sopenharmony_ci				 * 'where' points to first jset in list which
1408c2ecf20Sopenharmony_ci				 * is elder then j.
1418c2ecf20Sopenharmony_ci				 */
1428c2ecf20Sopenharmony_ci				if (j->seq > i->j.seq) {
1438c2ecf20Sopenharmony_ci					where = &i->list;
1448c2ecf20Sopenharmony_ci					goto add;
1458c2ecf20Sopenharmony_ci				}
1468c2ecf20Sopenharmony_ci			}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci			where = list;
1498c2ecf20Sopenharmony_ciadd:
1508c2ecf20Sopenharmony_ci			i = kmalloc(offsetof(struct journal_replay, j) +
1518c2ecf20Sopenharmony_ci				    bytes, GFP_KERNEL);
1528c2ecf20Sopenharmony_ci			if (!i)
1538c2ecf20Sopenharmony_ci				return -ENOMEM;
1548c2ecf20Sopenharmony_ci			memcpy(&i->j, j, bytes);
1558c2ecf20Sopenharmony_ci			/* Add to the location after 'where' points to */
1568c2ecf20Sopenharmony_ci			list_add(&i->list, where);
1578c2ecf20Sopenharmony_ci			ret = 1;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci			if (j->seq > ja->seq[bucket_index])
1608c2ecf20Sopenharmony_ci				ja->seq[bucket_index] = j->seq;
1618c2ecf20Sopenharmony_cinext_set:
1628c2ecf20Sopenharmony_ci			offset	+= blocks * ca->sb.block_size;
1638c2ecf20Sopenharmony_ci			len	-= blocks * ca->sb.block_size;
1648c2ecf20Sopenharmony_ci			j = ((void *) j) + blocks * block_bytes(ca);
1658c2ecf20Sopenharmony_ci		}
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	return ret;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciint bch_journal_read(struct cache_set *c, struct list_head *list)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci#define read_bucket(b)							\
1748c2ecf20Sopenharmony_ci	({								\
1758c2ecf20Sopenharmony_ci		ret = journal_read_bucket(ca, list, b);			\
1768c2ecf20Sopenharmony_ci		__set_bit(b, bitmap);					\
1778c2ecf20Sopenharmony_ci		if (ret < 0)						\
1788c2ecf20Sopenharmony_ci			return ret;					\
1798c2ecf20Sopenharmony_ci		ret;							\
1808c2ecf20Sopenharmony_ci	})
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	struct cache *ca = c->cache;
1838c2ecf20Sopenharmony_ci	int ret = 0;
1848c2ecf20Sopenharmony_ci	struct journal_device *ja = &ca->journal;
1858c2ecf20Sopenharmony_ci	DECLARE_BITMAP(bitmap, SB_JOURNAL_BUCKETS);
1868c2ecf20Sopenharmony_ci	unsigned int i, l, r, m;
1878c2ecf20Sopenharmony_ci	uint64_t seq;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	bitmap_zero(bitmap, SB_JOURNAL_BUCKETS);
1908c2ecf20Sopenharmony_ci	pr_debug("%u journal buckets\n", ca->sb.njournal_buckets);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/*
1938c2ecf20Sopenharmony_ci	 * Read journal buckets ordered by golden ratio hash to quickly
1948c2ecf20Sopenharmony_ci	 * find a sequence of buckets with valid journal entries
1958c2ecf20Sopenharmony_ci	 */
1968c2ecf20Sopenharmony_ci	for (i = 0; i < ca->sb.njournal_buckets; i++) {
1978c2ecf20Sopenharmony_ci		/*
1988c2ecf20Sopenharmony_ci		 * We must try the index l with ZERO first for
1998c2ecf20Sopenharmony_ci		 * correctness due to the scenario that the journal
2008c2ecf20Sopenharmony_ci		 * bucket is circular buffer which might have wrapped
2018c2ecf20Sopenharmony_ci		 */
2028c2ecf20Sopenharmony_ci		l = (i * 2654435769U) % ca->sb.njournal_buckets;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		if (test_bit(l, bitmap))
2058c2ecf20Sopenharmony_ci			break;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		if (read_bucket(l))
2088c2ecf20Sopenharmony_ci			goto bsearch;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/*
2128c2ecf20Sopenharmony_ci	 * If that fails, check all the buckets we haven't checked
2138c2ecf20Sopenharmony_ci	 * already
2148c2ecf20Sopenharmony_ci	 */
2158c2ecf20Sopenharmony_ci	pr_debug("falling back to linear search\n");
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	for_each_clear_bit(l, bitmap, ca->sb.njournal_buckets)
2188c2ecf20Sopenharmony_ci		if (read_bucket(l))
2198c2ecf20Sopenharmony_ci			goto bsearch;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* no journal entries on this device? */
2228c2ecf20Sopenharmony_ci	if (l == ca->sb.njournal_buckets)
2238c2ecf20Sopenharmony_ci		goto out;
2248c2ecf20Sopenharmony_cibsearch:
2258c2ecf20Sopenharmony_ci	BUG_ON(list_empty(list));
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* Binary search */
2288c2ecf20Sopenharmony_ci	m = l;
2298c2ecf20Sopenharmony_ci	r = find_next_bit(bitmap, ca->sb.njournal_buckets, l + 1);
2308c2ecf20Sopenharmony_ci	pr_debug("starting binary search, l %u r %u\n", l, r);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	while (l + 1 < r) {
2338c2ecf20Sopenharmony_ci		seq = list_entry(list->prev, struct journal_replay,
2348c2ecf20Sopenharmony_ci				 list)->j.seq;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci		m = (l + r) >> 1;
2378c2ecf20Sopenharmony_ci		read_bucket(m);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		if (seq != list_entry(list->prev, struct journal_replay,
2408c2ecf20Sopenharmony_ci				      list)->j.seq)
2418c2ecf20Sopenharmony_ci			l = m;
2428c2ecf20Sopenharmony_ci		else
2438c2ecf20Sopenharmony_ci			r = m;
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/*
2478c2ecf20Sopenharmony_ci	 * Read buckets in reverse order until we stop finding more
2488c2ecf20Sopenharmony_ci	 * journal entries
2498c2ecf20Sopenharmony_ci	 */
2508c2ecf20Sopenharmony_ci	pr_debug("finishing up: m %u njournal_buckets %u\n",
2518c2ecf20Sopenharmony_ci		 m, ca->sb.njournal_buckets);
2528c2ecf20Sopenharmony_ci	l = m;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	while (1) {
2558c2ecf20Sopenharmony_ci		if (!l--)
2568c2ecf20Sopenharmony_ci			l = ca->sb.njournal_buckets - 1;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		if (l == m)
2598c2ecf20Sopenharmony_ci			break;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		if (test_bit(l, bitmap))
2628c2ecf20Sopenharmony_ci			continue;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		if (!read_bucket(l))
2658c2ecf20Sopenharmony_ci			break;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	seq = 0;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	for (i = 0; i < ca->sb.njournal_buckets; i++)
2718c2ecf20Sopenharmony_ci		if (ja->seq[i] > seq) {
2728c2ecf20Sopenharmony_ci			seq = ja->seq[i];
2738c2ecf20Sopenharmony_ci			/*
2748c2ecf20Sopenharmony_ci			 * When journal_reclaim() goes to allocate for
2758c2ecf20Sopenharmony_ci			 * the first time, it'll use the bucket after
2768c2ecf20Sopenharmony_ci			 * ja->cur_idx
2778c2ecf20Sopenharmony_ci			 */
2788c2ecf20Sopenharmony_ci			ja->cur_idx = i;
2798c2ecf20Sopenharmony_ci			ja->last_idx = ja->discard_idx = (i + 1) %
2808c2ecf20Sopenharmony_ci				ca->sb.njournal_buckets;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ciout:
2858c2ecf20Sopenharmony_ci	if (!list_empty(list))
2868c2ecf20Sopenharmony_ci		c->journal.seq = list_entry(list->prev,
2878c2ecf20Sopenharmony_ci					    struct journal_replay,
2888c2ecf20Sopenharmony_ci					    list)->j.seq;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return 0;
2918c2ecf20Sopenharmony_ci#undef read_bucket
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_civoid bch_journal_mark(struct cache_set *c, struct list_head *list)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	atomic_t p = { 0 };
2978c2ecf20Sopenharmony_ci	struct bkey *k;
2988c2ecf20Sopenharmony_ci	struct journal_replay *i;
2998c2ecf20Sopenharmony_ci	struct journal *j = &c->journal;
3008c2ecf20Sopenharmony_ci	uint64_t last = j->seq;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/*
3038c2ecf20Sopenharmony_ci	 * journal.pin should never fill up - we never write a journal
3048c2ecf20Sopenharmony_ci	 * entry when it would fill up. But if for some reason it does, we
3058c2ecf20Sopenharmony_ci	 * iterate over the list in reverse order so that we can just skip that
3068c2ecf20Sopenharmony_ci	 * refcount instead of bugging.
3078c2ecf20Sopenharmony_ci	 */
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(i, list, list) {
3108c2ecf20Sopenharmony_ci		BUG_ON(last < i->j.seq);
3118c2ecf20Sopenharmony_ci		i->pin = NULL;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci		while (last-- != i->j.seq)
3148c2ecf20Sopenharmony_ci			if (fifo_free(&j->pin) > 1) {
3158c2ecf20Sopenharmony_ci				fifo_push_front(&j->pin, p);
3168c2ecf20Sopenharmony_ci				atomic_set(&fifo_front(&j->pin), 0);
3178c2ecf20Sopenharmony_ci			}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci		if (fifo_free(&j->pin) > 1) {
3208c2ecf20Sopenharmony_ci			fifo_push_front(&j->pin, p);
3218c2ecf20Sopenharmony_ci			i->pin = &fifo_front(&j->pin);
3228c2ecf20Sopenharmony_ci			atomic_set(i->pin, 1);
3238c2ecf20Sopenharmony_ci		}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci		for (k = i->j.start;
3268c2ecf20Sopenharmony_ci		     k < bset_bkey_last(&i->j);
3278c2ecf20Sopenharmony_ci		     k = bkey_next(k))
3288c2ecf20Sopenharmony_ci			if (!__bch_extent_invalid(c, k)) {
3298c2ecf20Sopenharmony_ci				unsigned int j;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci				for (j = 0; j < KEY_PTRS(k); j++)
3328c2ecf20Sopenharmony_ci					if (ptr_available(c, k, j))
3338c2ecf20Sopenharmony_ci						atomic_inc(&PTR_BUCKET(c, k, j)->pin);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci				bch_initial_mark_key(c, 0, k);
3368c2ecf20Sopenharmony_ci			}
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic bool is_discard_enabled(struct cache_set *s)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	struct cache *ca = s->cache;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (ca->discard)
3458c2ecf20Sopenharmony_ci		return true;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return false;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciint bch_journal_replay(struct cache_set *s, struct list_head *list)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	int ret = 0, keys = 0, entries = 0;
3538c2ecf20Sopenharmony_ci	struct bkey *k;
3548c2ecf20Sopenharmony_ci	struct journal_replay *i =
3558c2ecf20Sopenharmony_ci		list_entry(list->prev, struct journal_replay, list);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	uint64_t start = i->j.last_seq, end = i->j.seq, n = start;
3588c2ecf20Sopenharmony_ci	struct keylist keylist;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	list_for_each_entry(i, list, list) {
3618c2ecf20Sopenharmony_ci		BUG_ON(i->pin && atomic_read(i->pin) != 1);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		if (n != i->j.seq) {
3648c2ecf20Sopenharmony_ci			if (n == start && is_discard_enabled(s))
3658c2ecf20Sopenharmony_ci				pr_info("journal entries %llu-%llu may be discarded! (replaying %llu-%llu)\n",
3668c2ecf20Sopenharmony_ci					n, i->j.seq - 1, start, end);
3678c2ecf20Sopenharmony_ci			else {
3688c2ecf20Sopenharmony_ci				pr_err("journal entries %llu-%llu missing! (replaying %llu-%llu)\n",
3698c2ecf20Sopenharmony_ci					n, i->j.seq - 1, start, end);
3708c2ecf20Sopenharmony_ci				ret = -EIO;
3718c2ecf20Sopenharmony_ci				goto err;
3728c2ecf20Sopenharmony_ci			}
3738c2ecf20Sopenharmony_ci		}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		for (k = i->j.start;
3768c2ecf20Sopenharmony_ci		     k < bset_bkey_last(&i->j);
3778c2ecf20Sopenharmony_ci		     k = bkey_next(k)) {
3788c2ecf20Sopenharmony_ci			trace_bcache_journal_replay_key(k);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci			bch_keylist_init_single(&keylist, k);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci			ret = bch_btree_insert(s, &keylist, i->pin, NULL);
3838c2ecf20Sopenharmony_ci			if (ret)
3848c2ecf20Sopenharmony_ci				goto err;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci			BUG_ON(!bch_keylist_empty(&keylist));
3878c2ecf20Sopenharmony_ci			keys++;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci			cond_resched();
3908c2ecf20Sopenharmony_ci		}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		if (i->pin)
3938c2ecf20Sopenharmony_ci			atomic_dec(i->pin);
3948c2ecf20Sopenharmony_ci		n = i->j.seq + 1;
3958c2ecf20Sopenharmony_ci		entries++;
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	pr_info("journal replay done, %i keys in %i entries, seq %llu\n",
3998c2ecf20Sopenharmony_ci		keys, entries, end);
4008c2ecf20Sopenharmony_cierr:
4018c2ecf20Sopenharmony_ci	while (!list_empty(list)) {
4028c2ecf20Sopenharmony_ci		i = list_first_entry(list, struct journal_replay, list);
4038c2ecf20Sopenharmony_ci		list_del(&i->list);
4048c2ecf20Sopenharmony_ci		kfree(i);
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	return ret;
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_civoid bch_journal_space_reserve(struct journal *j)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	j->do_reserve = true;
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci/* Journalling */
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic void btree_flush_write(struct cache_set *c)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	struct btree *b, *t, *btree_nodes[BTREE_FLUSH_NR];
4208c2ecf20Sopenharmony_ci	unsigned int i, nr;
4218c2ecf20Sopenharmony_ci	int ref_nr;
4228c2ecf20Sopenharmony_ci	atomic_t *fifo_front_p, *now_fifo_front_p;
4238c2ecf20Sopenharmony_ci	size_t mask;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (c->journal.btree_flushing)
4268c2ecf20Sopenharmony_ci		return;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	spin_lock(&c->journal.flush_write_lock);
4298c2ecf20Sopenharmony_ci	if (c->journal.btree_flushing) {
4308c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.flush_write_lock);
4318c2ecf20Sopenharmony_ci		return;
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci	c->journal.btree_flushing = true;
4348c2ecf20Sopenharmony_ci	spin_unlock(&c->journal.flush_write_lock);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	/* get the oldest journal entry and check its refcount */
4378c2ecf20Sopenharmony_ci	spin_lock(&c->journal.lock);
4388c2ecf20Sopenharmony_ci	fifo_front_p = &fifo_front(&c->journal.pin);
4398c2ecf20Sopenharmony_ci	ref_nr = atomic_read(fifo_front_p);
4408c2ecf20Sopenharmony_ci	if (ref_nr <= 0) {
4418c2ecf20Sopenharmony_ci		/*
4428c2ecf20Sopenharmony_ci		 * do nothing if no btree node references
4438c2ecf20Sopenharmony_ci		 * the oldest journal entry
4448c2ecf20Sopenharmony_ci		 */
4458c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.lock);
4468c2ecf20Sopenharmony_ci		goto out;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci	spin_unlock(&c->journal.lock);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	mask = c->journal.pin.mask;
4518c2ecf20Sopenharmony_ci	nr = 0;
4528c2ecf20Sopenharmony_ci	atomic_long_inc(&c->flush_write);
4538c2ecf20Sopenharmony_ci	memset(btree_nodes, 0, sizeof(btree_nodes));
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	mutex_lock(&c->bucket_lock);
4568c2ecf20Sopenharmony_ci	list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) {
4578c2ecf20Sopenharmony_ci		/*
4588c2ecf20Sopenharmony_ci		 * It is safe to get now_fifo_front_p without holding
4598c2ecf20Sopenharmony_ci		 * c->journal.lock here, because we don't need to know
4608c2ecf20Sopenharmony_ci		 * the exactly accurate value, just check whether the
4618c2ecf20Sopenharmony_ci		 * front pointer of c->journal.pin is changed.
4628c2ecf20Sopenharmony_ci		 */
4638c2ecf20Sopenharmony_ci		now_fifo_front_p = &fifo_front(&c->journal.pin);
4648c2ecf20Sopenharmony_ci		/*
4658c2ecf20Sopenharmony_ci		 * If the oldest journal entry is reclaimed and front
4668c2ecf20Sopenharmony_ci		 * pointer of c->journal.pin changes, it is unnecessary
4678c2ecf20Sopenharmony_ci		 * to scan c->btree_cache anymore, just quit the loop and
4688c2ecf20Sopenharmony_ci		 * flush out what we have already.
4698c2ecf20Sopenharmony_ci		 */
4708c2ecf20Sopenharmony_ci		if (now_fifo_front_p != fifo_front_p)
4718c2ecf20Sopenharmony_ci			break;
4728c2ecf20Sopenharmony_ci		/*
4738c2ecf20Sopenharmony_ci		 * quit this loop if all matching btree nodes are
4748c2ecf20Sopenharmony_ci		 * scanned and record in btree_nodes[] already.
4758c2ecf20Sopenharmony_ci		 */
4768c2ecf20Sopenharmony_ci		ref_nr = atomic_read(fifo_front_p);
4778c2ecf20Sopenharmony_ci		if (nr >= ref_nr)
4788c2ecf20Sopenharmony_ci			break;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci		if (btree_node_journal_flush(b))
4818c2ecf20Sopenharmony_ci			pr_err("BUG: flush_write bit should not be set here!\n");
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci		mutex_lock(&b->write_lock);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		if (!btree_node_dirty(b)) {
4868c2ecf20Sopenharmony_ci			mutex_unlock(&b->write_lock);
4878c2ecf20Sopenharmony_ci			continue;
4888c2ecf20Sopenharmony_ci		}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		if (!btree_current_write(b)->journal) {
4918c2ecf20Sopenharmony_ci			mutex_unlock(&b->write_lock);
4928c2ecf20Sopenharmony_ci			continue;
4938c2ecf20Sopenharmony_ci		}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci		/*
4968c2ecf20Sopenharmony_ci		 * Only select the btree node which exactly references
4978c2ecf20Sopenharmony_ci		 * the oldest journal entry.
4988c2ecf20Sopenharmony_ci		 *
4998c2ecf20Sopenharmony_ci		 * If the journal entry pointed by fifo_front_p is
5008c2ecf20Sopenharmony_ci		 * reclaimed in parallel, don't worry:
5018c2ecf20Sopenharmony_ci		 * - the list_for_each_xxx loop will quit when checking
5028c2ecf20Sopenharmony_ci		 *   next now_fifo_front_p.
5038c2ecf20Sopenharmony_ci		 * - If there are matched nodes recorded in btree_nodes[],
5048c2ecf20Sopenharmony_ci		 *   they are clean now (this is why and how the oldest
5058c2ecf20Sopenharmony_ci		 *   journal entry can be reclaimed). These selected nodes
5068c2ecf20Sopenharmony_ci		 *   will be ignored and skipped in the folowing for-loop.
5078c2ecf20Sopenharmony_ci		 */
5088c2ecf20Sopenharmony_ci		if (((btree_current_write(b)->journal - fifo_front_p) &
5098c2ecf20Sopenharmony_ci		     mask) != 0) {
5108c2ecf20Sopenharmony_ci			mutex_unlock(&b->write_lock);
5118c2ecf20Sopenharmony_ci			continue;
5128c2ecf20Sopenharmony_ci		}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci		set_btree_node_journal_flush(b);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci		mutex_unlock(&b->write_lock);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci		btree_nodes[nr++] = b;
5198c2ecf20Sopenharmony_ci		/*
5208c2ecf20Sopenharmony_ci		 * To avoid holding c->bucket_lock too long time,
5218c2ecf20Sopenharmony_ci		 * only scan for BTREE_FLUSH_NR matched btree nodes
5228c2ecf20Sopenharmony_ci		 * at most. If there are more btree nodes reference
5238c2ecf20Sopenharmony_ci		 * the oldest journal entry, try to flush them next
5248c2ecf20Sopenharmony_ci		 * time when btree_flush_write() is called.
5258c2ecf20Sopenharmony_ci		 */
5268c2ecf20Sopenharmony_ci		if (nr == BTREE_FLUSH_NR)
5278c2ecf20Sopenharmony_ci			break;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci	mutex_unlock(&c->bucket_lock);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	for (i = 0; i < nr; i++) {
5328c2ecf20Sopenharmony_ci		b = btree_nodes[i];
5338c2ecf20Sopenharmony_ci		if (!b) {
5348c2ecf20Sopenharmony_ci			pr_err("BUG: btree_nodes[%d] is NULL\n", i);
5358c2ecf20Sopenharmony_ci			continue;
5368c2ecf20Sopenharmony_ci		}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci		/* safe to check without holding b->write_lock */
5398c2ecf20Sopenharmony_ci		if (!btree_node_journal_flush(b)) {
5408c2ecf20Sopenharmony_ci			pr_err("BUG: bnode %p: journal_flush bit cleaned\n", b);
5418c2ecf20Sopenharmony_ci			continue;
5428c2ecf20Sopenharmony_ci		}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci		mutex_lock(&b->write_lock);
5458c2ecf20Sopenharmony_ci		if (!btree_current_write(b)->journal) {
5468c2ecf20Sopenharmony_ci			clear_bit(BTREE_NODE_journal_flush, &b->flags);
5478c2ecf20Sopenharmony_ci			mutex_unlock(&b->write_lock);
5488c2ecf20Sopenharmony_ci			pr_debug("bnode %p: written by others\n", b);
5498c2ecf20Sopenharmony_ci			continue;
5508c2ecf20Sopenharmony_ci		}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		if (!btree_node_dirty(b)) {
5538c2ecf20Sopenharmony_ci			clear_bit(BTREE_NODE_journal_flush, &b->flags);
5548c2ecf20Sopenharmony_ci			mutex_unlock(&b->write_lock);
5558c2ecf20Sopenharmony_ci			pr_debug("bnode %p: dirty bit cleaned by others\n", b);
5568c2ecf20Sopenharmony_ci			continue;
5578c2ecf20Sopenharmony_ci		}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci		__bch_btree_node_write(b, NULL);
5608c2ecf20Sopenharmony_ci		clear_bit(BTREE_NODE_journal_flush, &b->flags);
5618c2ecf20Sopenharmony_ci		mutex_unlock(&b->write_lock);
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ciout:
5658c2ecf20Sopenharmony_ci	spin_lock(&c->journal.flush_write_lock);
5668c2ecf20Sopenharmony_ci	c->journal.btree_flushing = false;
5678c2ecf20Sopenharmony_ci	spin_unlock(&c->journal.flush_write_lock);
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci#define last_seq(j)	((j)->seq - fifo_used(&(j)->pin) + 1)
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic void journal_discard_endio(struct bio *bio)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	struct journal_device *ja =
5758c2ecf20Sopenharmony_ci		container_of(bio, struct journal_device, discard_bio);
5768c2ecf20Sopenharmony_ci	struct cache *ca = container_of(ja, struct cache, journal);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	atomic_set(&ja->discard_in_flight, DISCARD_DONE);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	closure_wake_up(&ca->set->journal.wait);
5818c2ecf20Sopenharmony_ci	closure_put(&ca->set->cl);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic void journal_discard_work(struct work_struct *work)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct journal_device *ja =
5878c2ecf20Sopenharmony_ci		container_of(work, struct journal_device, discard_work);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	submit_bio(&ja->discard_bio);
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic void do_journal_discard(struct cache *ca)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct journal_device *ja = &ca->journal;
5958c2ecf20Sopenharmony_ci	struct bio *bio = &ja->discard_bio;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (!ca->discard) {
5988c2ecf20Sopenharmony_ci		ja->discard_idx = ja->last_idx;
5998c2ecf20Sopenharmony_ci		return;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	switch (atomic_read(&ja->discard_in_flight)) {
6038c2ecf20Sopenharmony_ci	case DISCARD_IN_FLIGHT:
6048c2ecf20Sopenharmony_ci		return;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	case DISCARD_DONE:
6078c2ecf20Sopenharmony_ci		ja->discard_idx = (ja->discard_idx + 1) %
6088c2ecf20Sopenharmony_ci			ca->sb.njournal_buckets;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		atomic_set(&ja->discard_in_flight, DISCARD_READY);
6118c2ecf20Sopenharmony_ci		fallthrough;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	case DISCARD_READY:
6148c2ecf20Sopenharmony_ci		if (ja->discard_idx == ja->last_idx)
6158c2ecf20Sopenharmony_ci			return;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci		atomic_set(&ja->discard_in_flight, DISCARD_IN_FLIGHT);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci		bio_init(bio, bio->bi_inline_vecs, 1);
6208c2ecf20Sopenharmony_ci		bio_set_op_attrs(bio, REQ_OP_DISCARD, 0);
6218c2ecf20Sopenharmony_ci		bio->bi_iter.bi_sector	= bucket_to_sector(ca->set,
6228c2ecf20Sopenharmony_ci						ca->sb.d[ja->discard_idx]);
6238c2ecf20Sopenharmony_ci		bio_set_dev(bio, ca->bdev);
6248c2ecf20Sopenharmony_ci		bio->bi_iter.bi_size	= bucket_bytes(ca);
6258c2ecf20Sopenharmony_ci		bio->bi_end_io		= journal_discard_endio;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci		closure_get(&ca->set->cl);
6288c2ecf20Sopenharmony_ci		INIT_WORK(&ja->discard_work, journal_discard_work);
6298c2ecf20Sopenharmony_ci		queue_work(bch_journal_wq, &ja->discard_work);
6308c2ecf20Sopenharmony_ci	}
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic unsigned int free_journal_buckets(struct cache_set *c)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	struct journal *j = &c->journal;
6368c2ecf20Sopenharmony_ci	struct cache *ca = c->cache;
6378c2ecf20Sopenharmony_ci	struct journal_device *ja = &c->cache->journal;
6388c2ecf20Sopenharmony_ci	unsigned int n;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* In case njournal_buckets is not power of 2 */
6418c2ecf20Sopenharmony_ci	if (ja->cur_idx >= ja->discard_idx)
6428c2ecf20Sopenharmony_ci		n = ca->sb.njournal_buckets +  ja->discard_idx - ja->cur_idx;
6438c2ecf20Sopenharmony_ci	else
6448c2ecf20Sopenharmony_ci		n = ja->discard_idx - ja->cur_idx;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (n > (1 + j->do_reserve))
6478c2ecf20Sopenharmony_ci		return n - (1 + j->do_reserve);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	return 0;
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic void journal_reclaim(struct cache_set *c)
6538c2ecf20Sopenharmony_ci{
6548c2ecf20Sopenharmony_ci	struct bkey *k = &c->journal.key;
6558c2ecf20Sopenharmony_ci	struct cache *ca = c->cache;
6568c2ecf20Sopenharmony_ci	uint64_t last_seq;
6578c2ecf20Sopenharmony_ci	struct journal_device *ja = &ca->journal;
6588c2ecf20Sopenharmony_ci	atomic_t p __maybe_unused;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	atomic_long_inc(&c->reclaim);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	while (!atomic_read(&fifo_front(&c->journal.pin)))
6638c2ecf20Sopenharmony_ci		fifo_pop(&c->journal.pin, p);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	last_seq = last_seq(&c->journal);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	/* Update last_idx */
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	while (ja->last_idx != ja->cur_idx &&
6708c2ecf20Sopenharmony_ci	       ja->seq[ja->last_idx] < last_seq)
6718c2ecf20Sopenharmony_ci		ja->last_idx = (ja->last_idx + 1) %
6728c2ecf20Sopenharmony_ci			ca->sb.njournal_buckets;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	do_journal_discard(ca);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	if (c->journal.blocks_free)
6778c2ecf20Sopenharmony_ci		goto out;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	if (!free_journal_buckets(c))
6808c2ecf20Sopenharmony_ci		goto out;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	ja->cur_idx = (ja->cur_idx + 1) % ca->sb.njournal_buckets;
6838c2ecf20Sopenharmony_ci	k->ptr[0] = MAKE_PTR(0,
6848c2ecf20Sopenharmony_ci			     bucket_to_sector(c, ca->sb.d[ja->cur_idx]),
6858c2ecf20Sopenharmony_ci			     ca->sb.nr_this_dev);
6868c2ecf20Sopenharmony_ci	atomic_long_inc(&c->reclaimed_journal_buckets);
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	bkey_init(k);
6898c2ecf20Sopenharmony_ci	SET_KEY_PTRS(k, 1);
6908c2ecf20Sopenharmony_ci	c->journal.blocks_free = ca->sb.bucket_size >> c->block_bits;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ciout:
6938c2ecf20Sopenharmony_ci	if (!journal_full(&c->journal))
6948c2ecf20Sopenharmony_ci		__closure_wake_up(&c->journal.wait);
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_civoid bch_journal_next(struct journal *j)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	atomic_t p = { 1 };
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	j->cur = (j->cur == j->w)
7028c2ecf20Sopenharmony_ci		? &j->w[1]
7038c2ecf20Sopenharmony_ci		: &j->w[0];
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	/*
7068c2ecf20Sopenharmony_ci	 * The fifo_push() needs to happen at the same time as j->seq is
7078c2ecf20Sopenharmony_ci	 * incremented for last_seq() to be calculated correctly
7088c2ecf20Sopenharmony_ci	 */
7098c2ecf20Sopenharmony_ci	BUG_ON(!fifo_push(&j->pin, p));
7108c2ecf20Sopenharmony_ci	atomic_set(&fifo_back(&j->pin), 1);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	j->cur->data->seq	= ++j->seq;
7138c2ecf20Sopenharmony_ci	j->cur->dirty		= false;
7148c2ecf20Sopenharmony_ci	j->cur->need_write	= false;
7158c2ecf20Sopenharmony_ci	j->cur->data->keys	= 0;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (fifo_full(&j->pin))
7188c2ecf20Sopenharmony_ci		pr_debug("journal_pin full (%zu)\n", fifo_used(&j->pin));
7198c2ecf20Sopenharmony_ci}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_cistatic void journal_write_endio(struct bio *bio)
7228c2ecf20Sopenharmony_ci{
7238c2ecf20Sopenharmony_ci	struct journal_write *w = bio->bi_private;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	cache_set_err_on(bio->bi_status, w->c, "journal io error");
7268c2ecf20Sopenharmony_ci	closure_put(&w->c->journal.io);
7278c2ecf20Sopenharmony_ci}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_cistatic void journal_write(struct closure *cl);
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cistatic void journal_write_done(struct closure *cl)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	struct journal *j = container_of(cl, struct journal, io);
7348c2ecf20Sopenharmony_ci	struct journal_write *w = (j->cur == j->w)
7358c2ecf20Sopenharmony_ci		? &j->w[1]
7368c2ecf20Sopenharmony_ci		: &j->w[0];
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	__closure_wake_up(&w->wait);
7398c2ecf20Sopenharmony_ci	continue_at_nobarrier(cl, journal_write, bch_journal_wq);
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic void journal_write_unlock(struct closure *cl)
7438c2ecf20Sopenharmony_ci	__releases(&c->journal.lock)
7448c2ecf20Sopenharmony_ci{
7458c2ecf20Sopenharmony_ci	struct cache_set *c = container_of(cl, struct cache_set, journal.io);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	c->journal.io_in_flight = 0;
7488c2ecf20Sopenharmony_ci	spin_unlock(&c->journal.lock);
7498c2ecf20Sopenharmony_ci}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_cistatic void journal_write_unlocked(struct closure *cl)
7528c2ecf20Sopenharmony_ci	__releases(c->journal.lock)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	struct cache_set *c = container_of(cl, struct cache_set, journal.io);
7558c2ecf20Sopenharmony_ci	struct cache *ca = c->cache;
7568c2ecf20Sopenharmony_ci	struct journal_write *w = c->journal.cur;
7578c2ecf20Sopenharmony_ci	struct bkey *k = &c->journal.key;
7588c2ecf20Sopenharmony_ci	unsigned int i, sectors = set_blocks(w->data, block_bytes(ca)) *
7598c2ecf20Sopenharmony_ci		ca->sb.block_size;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	struct bio *bio;
7628c2ecf20Sopenharmony_ci	struct bio_list list;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	bio_list_init(&list);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	if (!w->need_write) {
7678c2ecf20Sopenharmony_ci		closure_return_with_destructor(cl, journal_write_unlock);
7688c2ecf20Sopenharmony_ci		return;
7698c2ecf20Sopenharmony_ci	} else if (journal_full(&c->journal)) {
7708c2ecf20Sopenharmony_ci		journal_reclaim(c);
7718c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.lock);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		btree_flush_write(c);
7748c2ecf20Sopenharmony_ci		continue_at(cl, journal_write, bch_journal_wq);
7758c2ecf20Sopenharmony_ci		return;
7768c2ecf20Sopenharmony_ci	}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	c->journal.blocks_free -= set_blocks(w->data, block_bytes(ca));
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	w->data->btree_level = c->root->level;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	bkey_copy(&w->data->btree_root, &c->root->key);
7838c2ecf20Sopenharmony_ci	bkey_copy(&w->data->uuid_bucket, &c->uuid_bucket);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	w->data->prio_bucket[ca->sb.nr_this_dev] = ca->prio_buckets[0];
7868c2ecf20Sopenharmony_ci	w->data->magic		= jset_magic(&ca->sb);
7878c2ecf20Sopenharmony_ci	w->data->version	= BCACHE_JSET_VERSION;
7888c2ecf20Sopenharmony_ci	w->data->last_seq	= last_seq(&c->journal);
7898c2ecf20Sopenharmony_ci	w->data->csum		= csum_set(w->data);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++) {
7928c2ecf20Sopenharmony_ci		ca = PTR_CACHE(c, k, i);
7938c2ecf20Sopenharmony_ci		bio = &ca->journal.bio;
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci		atomic_long_add(sectors, &ca->meta_sectors_written);
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci		bio_reset(bio);
7988c2ecf20Sopenharmony_ci		bio->bi_iter.bi_sector	= PTR_OFFSET(k, i);
7998c2ecf20Sopenharmony_ci		bio_set_dev(bio, ca->bdev);
8008c2ecf20Sopenharmony_ci		bio->bi_iter.bi_size = sectors << 9;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci		bio->bi_end_io	= journal_write_endio;
8038c2ecf20Sopenharmony_ci		bio->bi_private = w;
8048c2ecf20Sopenharmony_ci		bio_set_op_attrs(bio, REQ_OP_WRITE,
8058c2ecf20Sopenharmony_ci				 REQ_SYNC|REQ_META|REQ_PREFLUSH|REQ_FUA);
8068c2ecf20Sopenharmony_ci		bch_bio_map(bio, w->data);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci		trace_bcache_journal_write(bio, w->data->keys);
8098c2ecf20Sopenharmony_ci		bio_list_add(&list, bio);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci		SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + sectors);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci		ca->journal.seq[ca->journal.cur_idx] = w->data->seq;
8148c2ecf20Sopenharmony_ci	}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	/* If KEY_PTRS(k) == 0, this jset gets lost in air */
8178c2ecf20Sopenharmony_ci	BUG_ON(i == 0);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	atomic_dec_bug(&fifo_back(&c->journal.pin));
8208c2ecf20Sopenharmony_ci	bch_journal_next(&c->journal);
8218c2ecf20Sopenharmony_ci	journal_reclaim(c);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	spin_unlock(&c->journal.lock);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	while ((bio = bio_list_pop(&list)))
8268c2ecf20Sopenharmony_ci		closure_bio_submit(c, bio, cl);
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	continue_at(cl, journal_write_done, NULL);
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cistatic void journal_write(struct closure *cl)
8328c2ecf20Sopenharmony_ci{
8338c2ecf20Sopenharmony_ci	struct cache_set *c = container_of(cl, struct cache_set, journal.io);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	spin_lock(&c->journal.lock);
8368c2ecf20Sopenharmony_ci	journal_write_unlocked(cl);
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_cistatic void journal_try_write(struct cache_set *c)
8408c2ecf20Sopenharmony_ci	__releases(c->journal.lock)
8418c2ecf20Sopenharmony_ci{
8428c2ecf20Sopenharmony_ci	struct closure *cl = &c->journal.io;
8438c2ecf20Sopenharmony_ci	struct journal_write *w = c->journal.cur;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	w->need_write = true;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	if (!c->journal.io_in_flight) {
8488c2ecf20Sopenharmony_ci		c->journal.io_in_flight = 1;
8498c2ecf20Sopenharmony_ci		closure_call(cl, journal_write_unlocked, NULL, &c->cl);
8508c2ecf20Sopenharmony_ci	} else {
8518c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.lock);
8528c2ecf20Sopenharmony_ci	}
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cistatic struct journal_write *journal_wait_for_write(struct cache_set *c,
8568c2ecf20Sopenharmony_ci						    unsigned int nkeys)
8578c2ecf20Sopenharmony_ci	__acquires(&c->journal.lock)
8588c2ecf20Sopenharmony_ci{
8598c2ecf20Sopenharmony_ci	size_t sectors;
8608c2ecf20Sopenharmony_ci	struct closure cl;
8618c2ecf20Sopenharmony_ci	bool wait = false;
8628c2ecf20Sopenharmony_ci	struct cache *ca = c->cache;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	closure_init_stack(&cl);
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	spin_lock(&c->journal.lock);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	while (1) {
8698c2ecf20Sopenharmony_ci		struct journal_write *w = c->journal.cur;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci		sectors = __set_blocks(w->data, w->data->keys + nkeys,
8728c2ecf20Sopenharmony_ci				       block_bytes(ca)) * ca->sb.block_size;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci		if (sectors <= min_t(size_t,
8758c2ecf20Sopenharmony_ci				     c->journal.blocks_free * ca->sb.block_size,
8768c2ecf20Sopenharmony_ci				     PAGE_SECTORS << JSET_BITS))
8778c2ecf20Sopenharmony_ci			return w;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci		if (wait)
8808c2ecf20Sopenharmony_ci			closure_wait(&c->journal.wait, &cl);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci		if (!journal_full(&c->journal)) {
8838c2ecf20Sopenharmony_ci			if (wait)
8848c2ecf20Sopenharmony_ci				trace_bcache_journal_entry_full(c);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci			/*
8878c2ecf20Sopenharmony_ci			 * XXX: If we were inserting so many keys that they
8888c2ecf20Sopenharmony_ci			 * won't fit in an _empty_ journal write, we'll
8898c2ecf20Sopenharmony_ci			 * deadlock. For now, handle this in
8908c2ecf20Sopenharmony_ci			 * bch_keylist_realloc() - but something to think about.
8918c2ecf20Sopenharmony_ci			 */
8928c2ecf20Sopenharmony_ci			BUG_ON(!w->data->keys);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci			journal_try_write(c); /* unlocks */
8958c2ecf20Sopenharmony_ci		} else {
8968c2ecf20Sopenharmony_ci			if (wait)
8978c2ecf20Sopenharmony_ci				trace_bcache_journal_full(c);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci			journal_reclaim(c);
9008c2ecf20Sopenharmony_ci			spin_unlock(&c->journal.lock);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci			btree_flush_write(c);
9038c2ecf20Sopenharmony_ci		}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci		closure_sync(&cl);
9068c2ecf20Sopenharmony_ci		spin_lock(&c->journal.lock);
9078c2ecf20Sopenharmony_ci		wait = true;
9088c2ecf20Sopenharmony_ci	}
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_cistatic void journal_write_work(struct work_struct *work)
9128c2ecf20Sopenharmony_ci{
9138c2ecf20Sopenharmony_ci	struct cache_set *c = container_of(to_delayed_work(work),
9148c2ecf20Sopenharmony_ci					   struct cache_set,
9158c2ecf20Sopenharmony_ci					   journal.work);
9168c2ecf20Sopenharmony_ci	spin_lock(&c->journal.lock);
9178c2ecf20Sopenharmony_ci	if (c->journal.cur->dirty)
9188c2ecf20Sopenharmony_ci		journal_try_write(c);
9198c2ecf20Sopenharmony_ci	else
9208c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.lock);
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci/*
9248c2ecf20Sopenharmony_ci * Entry point to the journalling code - bio_insert() and btree_invalidate()
9258c2ecf20Sopenharmony_ci * pass bch_journal() a list of keys to be journalled, and then
9268c2ecf20Sopenharmony_ci * bch_journal() hands those same keys off to btree_insert_async()
9278c2ecf20Sopenharmony_ci */
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ciatomic_t *bch_journal(struct cache_set *c,
9308c2ecf20Sopenharmony_ci		      struct keylist *keys,
9318c2ecf20Sopenharmony_ci		      struct closure *parent)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct journal_write *w;
9348c2ecf20Sopenharmony_ci	atomic_t *ret;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	/* No journaling if CACHE_SET_IO_DISABLE set already */
9378c2ecf20Sopenharmony_ci	if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags)))
9388c2ecf20Sopenharmony_ci		return NULL;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	if (!CACHE_SYNC(&c->cache->sb))
9418c2ecf20Sopenharmony_ci		return NULL;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	w = journal_wait_for_write(c, bch_keylist_nkeys(keys));
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	memcpy(bset_bkey_last(w->data), keys->keys, bch_keylist_bytes(keys));
9468c2ecf20Sopenharmony_ci	w->data->keys += bch_keylist_nkeys(keys);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	ret = &fifo_back(&c->journal.pin);
9498c2ecf20Sopenharmony_ci	atomic_inc(ret);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	if (parent) {
9528c2ecf20Sopenharmony_ci		closure_wait(&w->wait, parent);
9538c2ecf20Sopenharmony_ci		journal_try_write(c);
9548c2ecf20Sopenharmony_ci	} else if (!w->dirty) {
9558c2ecf20Sopenharmony_ci		w->dirty = true;
9568c2ecf20Sopenharmony_ci		queue_delayed_work(bch_flush_wq, &c->journal.work,
9578c2ecf20Sopenharmony_ci				   msecs_to_jiffies(c->journal_delay_ms));
9588c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.lock);
9598c2ecf20Sopenharmony_ci	} else {
9608c2ecf20Sopenharmony_ci		spin_unlock(&c->journal.lock);
9618c2ecf20Sopenharmony_ci	}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	return ret;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_civoid bch_journal_meta(struct cache_set *c, struct closure *cl)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	struct keylist keys;
9708c2ecf20Sopenharmony_ci	atomic_t *ref;
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	bch_keylist_init(&keys);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	ref = bch_journal(c, &keys, cl);
9758c2ecf20Sopenharmony_ci	if (ref)
9768c2ecf20Sopenharmony_ci		atomic_dec_bug(ref);
9778c2ecf20Sopenharmony_ci}
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_civoid bch_journal_free(struct cache_set *c)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	free_pages((unsigned long) c->journal.w[1].data, JSET_BITS);
9828c2ecf20Sopenharmony_ci	free_pages((unsigned long) c->journal.w[0].data, JSET_BITS);
9838c2ecf20Sopenharmony_ci	free_fifo(&c->journal.pin);
9848c2ecf20Sopenharmony_ci}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ciint bch_journal_alloc(struct cache_set *c)
9878c2ecf20Sopenharmony_ci{
9888c2ecf20Sopenharmony_ci	struct journal *j = &c->journal;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	spin_lock_init(&j->lock);
9918c2ecf20Sopenharmony_ci	spin_lock_init(&j->flush_write_lock);
9928c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&j->work, journal_write_work);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	c->journal_delay_ms = 100;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	j->w[0].c = c;
9978c2ecf20Sopenharmony_ci	j->w[1].c = c;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
10008c2ecf20Sopenharmony_ci	    !(j->w[0].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)) ||
10018c2ecf20Sopenharmony_ci	    !(j->w[1].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)))
10028c2ecf20Sopenharmony_ci		return -ENOMEM;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	return 0;
10058c2ecf20Sopenharmony_ci}
1006