xref: /OK3568_Linux_fs/kernel/drivers/md/bcache/journal.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _BCACHE_JOURNAL_H
3*4882a593Smuzhiyun #define _BCACHE_JOURNAL_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun  * THE JOURNAL:
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * The journal is treated as a circular buffer of buckets - a journal entry
9*4882a593Smuzhiyun  * never spans two buckets. This means (not implemented yet) we can resize the
10*4882a593Smuzhiyun  * journal at runtime, and will be needed for bcache on raw flash support.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Journal entries contain a list of keys, ordered by the time they were
13*4882a593Smuzhiyun  * inserted; thus journal replay just has to reinsert the keys.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * We also keep some things in the journal header that are logically part of the
16*4882a593Smuzhiyun  * superblock - all the things that are frequently updated. This is for future
17*4882a593Smuzhiyun  * bcache on raw flash support; the superblock (which will become another
18*4882a593Smuzhiyun  * journal) can't be moved or wear leveled, so it contains just enough
19*4882a593Smuzhiyun  * information to find the main journal, and the superblock only has to be
20*4882a593Smuzhiyun  * rewritten when we want to move/wear level the main journal.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Currently, we don't journal BTREE_REPLACE operations - this will hopefully be
23*4882a593Smuzhiyun  * fixed eventually. This isn't a bug - BTREE_REPLACE is used for insertions
24*4882a593Smuzhiyun  * from cache misses, which don't have to be journaled, and for writeback and
25*4882a593Smuzhiyun  * moving gc we work around it by flushing the btree to disk before updating the
26*4882a593Smuzhiyun  * gc information. But it is a potential issue with incremental garbage
27*4882a593Smuzhiyun  * collection, and it's fragile.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * OPEN JOURNAL ENTRIES:
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * Each journal entry contains, in the header, the sequence number of the last
32*4882a593Smuzhiyun  * journal entry still open - i.e. that has keys that haven't been flushed to
33*4882a593Smuzhiyun  * disk in the btree.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * We track this by maintaining a refcount for every open journal entry, in a
36*4882a593Smuzhiyun  * fifo; each entry in the fifo corresponds to a particular journal
37*4882a593Smuzhiyun  * entry/sequence number. When the refcount at the tail of the fifo goes to
38*4882a593Smuzhiyun  * zero, we pop it off - thus, the size of the fifo tells us the number of open
39*4882a593Smuzhiyun  * journal entries
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * We take a refcount on a journal entry when we add some keys to a journal
42*4882a593Smuzhiyun  * entry that we're going to insert (held by struct btree_op), and then when we
43*4882a593Smuzhiyun  * insert those keys into the btree the btree write we're setting up takes a
44*4882a593Smuzhiyun  * copy of that refcount (held by struct btree_write). That refcount is dropped
45*4882a593Smuzhiyun  * when the btree write completes.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * A struct btree_write can only hold a refcount on a single journal entry, but
48*4882a593Smuzhiyun  * might contain keys for many journal entries - we handle this by making sure
49*4882a593Smuzhiyun  * it always has a refcount on the _oldest_ journal entry of all the journal
50*4882a593Smuzhiyun  * entries it has keys for.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * JOURNAL RECLAIM:
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * As mentioned previously, our fifo of refcounts tells us the number of open
55*4882a593Smuzhiyun  * journal entries; from that and the current journal sequence number we compute
56*4882a593Smuzhiyun  * last_seq - the oldest journal entry we still need. We write last_seq in each
57*4882a593Smuzhiyun  * journal entry, and we also have to keep track of where it exists on disk so
58*4882a593Smuzhiyun  * we don't overwrite it when we loop around the journal.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * To do that we track, for each journal bucket, the sequence number of the
61*4882a593Smuzhiyun  * newest journal entry it contains - if we don't need that journal entry we
62*4882a593Smuzhiyun  * don't need anything in that bucket anymore. From that we track the last
63*4882a593Smuzhiyun  * journal bucket we still need; all this is tracked in struct journal_device
64*4882a593Smuzhiyun  * and updated by journal_reclaim().
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * JOURNAL FILLING UP:
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * There are two ways the journal could fill up; either we could run out of
69*4882a593Smuzhiyun  * space to write to, or we could have too many open journal entries and run out
70*4882a593Smuzhiyun  * of room in the fifo of refcounts. Since those refcounts are decremented
71*4882a593Smuzhiyun  * without any locking we can't safely resize that fifo, so we handle it the
72*4882a593Smuzhiyun  * same way.
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * If the journal fills up, we start flushing dirty btree nodes until we can
75*4882a593Smuzhiyun  * allocate space for a journal write again - preferentially flushing btree
76*4882a593Smuzhiyun  * nodes that are pinning the oldest journal entries first.
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun  * Only used for holding the journal entries we read in btree_journal_read()
81*4882a593Smuzhiyun  * during cache_registration
82*4882a593Smuzhiyun  */
83*4882a593Smuzhiyun struct journal_replay {
84*4882a593Smuzhiyun 	struct list_head	list;
85*4882a593Smuzhiyun 	atomic_t		*pin;
86*4882a593Smuzhiyun 	struct jset		j;
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun  * We put two of these in struct journal; we used them for writes to the
91*4882a593Smuzhiyun  * journal that are being staged or in flight.
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun struct journal_write {
94*4882a593Smuzhiyun 	struct jset		*data;
95*4882a593Smuzhiyun #define JSET_BITS		3
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	struct cache_set	*c;
98*4882a593Smuzhiyun 	struct closure_waitlist	wait;
99*4882a593Smuzhiyun 	bool			dirty;
100*4882a593Smuzhiyun 	bool			need_write;
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Embedded in struct cache_set */
104*4882a593Smuzhiyun struct journal {
105*4882a593Smuzhiyun 	spinlock_t		lock;
106*4882a593Smuzhiyun 	spinlock_t		flush_write_lock;
107*4882a593Smuzhiyun 	bool			btree_flushing;
108*4882a593Smuzhiyun 	bool			do_reserve;
109*4882a593Smuzhiyun 	/* used when waiting because the journal was full */
110*4882a593Smuzhiyun 	struct closure_waitlist	wait;
111*4882a593Smuzhiyun 	struct closure		io;
112*4882a593Smuzhiyun 	int			io_in_flight;
113*4882a593Smuzhiyun 	struct delayed_work	work;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/* Number of blocks free in the bucket(s) we're currently writing to */
116*4882a593Smuzhiyun 	unsigned int		blocks_free;
117*4882a593Smuzhiyun 	uint64_t		seq;
118*4882a593Smuzhiyun 	DECLARE_FIFO(atomic_t, pin);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	BKEY_PADDED(key);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	struct journal_write	w[2], *cur;
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun  * Embedded in struct cache. First three fields refer to the array of journal
127*4882a593Smuzhiyun  * buckets, in cache_sb.
128*4882a593Smuzhiyun  */
129*4882a593Smuzhiyun struct journal_device {
130*4882a593Smuzhiyun 	/*
131*4882a593Smuzhiyun 	 * For each journal bucket, contains the max sequence number of the
132*4882a593Smuzhiyun 	 * journal writes it contains - so we know when a bucket can be reused.
133*4882a593Smuzhiyun 	 */
134*4882a593Smuzhiyun 	uint64_t		seq[SB_JOURNAL_BUCKETS];
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* Journal bucket we're currently writing to */
137*4882a593Smuzhiyun 	unsigned int		cur_idx;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* Last journal bucket that still contains an open journal entry */
140*4882a593Smuzhiyun 	unsigned int		last_idx;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* Next journal bucket to be discarded */
143*4882a593Smuzhiyun 	unsigned int		discard_idx;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun #define DISCARD_READY		0
146*4882a593Smuzhiyun #define DISCARD_IN_FLIGHT	1
147*4882a593Smuzhiyun #define DISCARD_DONE		2
148*4882a593Smuzhiyun 	/* 1 - discard in flight, -1 - discard completed */
149*4882a593Smuzhiyun 	atomic_t		discard_in_flight;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	struct work_struct	discard_work;
152*4882a593Smuzhiyun 	struct bio		discard_bio;
153*4882a593Smuzhiyun 	struct bio_vec		discard_bv;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/* Bio for journal reads/writes to this device */
156*4882a593Smuzhiyun 	struct bio		bio;
157*4882a593Smuzhiyun 	struct bio_vec		bv[8];
158*4882a593Smuzhiyun };
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun #define BTREE_FLUSH_NR	8
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #define journal_pin_cmp(c, l, r)				\
163*4882a593Smuzhiyun 	(fifo_idx(&(c)->journal.pin, (l)) > fifo_idx(&(c)->journal.pin, (r)))
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun #define JOURNAL_PIN	20000
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun #define journal_full(j)						\
168*4882a593Smuzhiyun 	(!(j)->blocks_free || fifo_free(&(j)->pin) <= 1)
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun struct closure;
171*4882a593Smuzhiyun struct cache_set;
172*4882a593Smuzhiyun struct btree_op;
173*4882a593Smuzhiyun struct keylist;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun atomic_t *bch_journal(struct cache_set *c,
176*4882a593Smuzhiyun 		      struct keylist *keys,
177*4882a593Smuzhiyun 		      struct closure *parent);
178*4882a593Smuzhiyun void bch_journal_next(struct journal *j);
179*4882a593Smuzhiyun void bch_journal_mark(struct cache_set *c, struct list_head *list);
180*4882a593Smuzhiyun void bch_journal_meta(struct cache_set *c, struct closure *cl);
181*4882a593Smuzhiyun int bch_journal_read(struct cache_set *c, struct list_head *list);
182*4882a593Smuzhiyun int bch_journal_replay(struct cache_set *c, struct list_head *list);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun void bch_journal_free(struct cache_set *c);
185*4882a593Smuzhiyun int bch_journal_alloc(struct cache_set *c);
186*4882a593Smuzhiyun void bch_journal_space_reserve(struct journal *j);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun #endif /* _BCACHE_JOURNAL_H */
189