xref: /linux-6.15/fs/bcachefs/journal_types.h (revision 2e4f982c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_JOURNAL_TYPES_H
3 #define _BCACHEFS_JOURNAL_TYPES_H
4 
5 #include <linux/cache.h>
6 #include <linux/workqueue.h>
7 
8 #include "alloc_types.h"
9 #include "super_types.h"
10 #include "fifo.h"
11 
12 /* btree write buffer steals 8 bits for its own purposes: */
13 #define JOURNAL_SEQ_MAX		((1ULL << 56) - 1)
14 
15 #define JOURNAL_BUF_BITS	2
16 #define JOURNAL_BUF_NR		(1U << JOURNAL_BUF_BITS)
17 #define JOURNAL_BUF_MASK	(JOURNAL_BUF_NR - 1)
18 
19 /*
20  * We put JOURNAL_BUF_NR of these in struct journal; we used them for writes to
21  * the journal that are being staged or in flight.
22  */
23 struct journal_buf {
24 	struct closure		io;
25 	struct jset		*data;
26 
27 	__BKEY_PADDED(key, BCH_REPLICAS_MAX);
28 	struct bch_devs_list	devs_written;
29 
30 	struct closure_waitlist	wait;
31 	u64			last_seq;	/* copy of data->last_seq */
32 	long			expires;
33 	u64			flush_time;
34 
35 	unsigned		buf_size;	/* size in bytes of @data */
36 	unsigned		sectors;	/* maximum size for current entry */
37 	unsigned		disk_sectors;	/* maximum size entry could have been, if
38 						   buf_size was bigger */
39 	unsigned		u64s_reserved;
40 	bool			noflush:1;	/* write has already been kicked off, and was noflush */
41 	bool			must_flush:1;	/* something wants a flush */
42 	bool			separate_flush:1;
43 	bool			need_flush_to_write_buffer:1;
44 	bool			write_started:1;
45 	bool			write_allocated:1;
46 	bool			write_done:1;
47 	u8			idx;
48 };
49 
50 /*
51  * Something that makes a journal entry dirty - i.e. a btree node that has to be
52  * flushed:
53  */
54 
55 enum journal_pin_type {
56 	JOURNAL_PIN_TYPE_btree,
57 	JOURNAL_PIN_TYPE_key_cache,
58 	JOURNAL_PIN_TYPE_other,
59 	JOURNAL_PIN_TYPE_NR,
60 };
61 
62 struct journal_entry_pin_list {
63 	struct list_head		unflushed[JOURNAL_PIN_TYPE_NR];
64 	struct list_head		flushed[JOURNAL_PIN_TYPE_NR];
65 	atomic_t			count;
66 	struct bch_devs_list		devs;
67 };
68 
69 struct journal;
70 struct journal_entry_pin;
71 typedef int (*journal_pin_flush_fn)(struct journal *j,
72 				struct journal_entry_pin *, u64);
73 
74 struct journal_entry_pin {
75 	struct list_head		list;
76 	journal_pin_flush_fn		flush;
77 	u64				seq;
78 };
79 
80 struct journal_res {
81 	bool			ref;
82 	u8			idx;
83 	u16			u64s;
84 	u32			offset;
85 	u64			seq;
86 };
87 
88 union journal_res_state {
89 	struct {
90 		atomic64_t	counter;
91 	};
92 
93 	struct {
94 		u64		v;
95 	};
96 
97 	struct {
98 		u64		cur_entry_offset:20,
99 				idx:2,
100 				unwritten_idx:2,
101 				buf0_count:10,
102 				buf1_count:10,
103 				buf2_count:10,
104 				buf3_count:10;
105 	};
106 };
107 
108 /* bytes: */
109 #define JOURNAL_ENTRY_SIZE_MIN		(64U << 10) /* 64k */
110 #define JOURNAL_ENTRY_SIZE_MAX		(4U  << 20) /* 4M */
111 
112 /*
113  * We stash some journal state as sentinal values in cur_entry_offset:
114  * note - cur_entry_offset is in units of u64s
115  */
116 #define JOURNAL_ENTRY_OFFSET_MAX	((1U << 20) - 1)
117 
118 #define JOURNAL_ENTRY_BLOCKED_VAL	(JOURNAL_ENTRY_OFFSET_MAX - 2)
119 #define JOURNAL_ENTRY_CLOSED_VAL	(JOURNAL_ENTRY_OFFSET_MAX - 1)
120 #define JOURNAL_ENTRY_ERROR_VAL		(JOURNAL_ENTRY_OFFSET_MAX)
121 
122 struct journal_space {
123 	/* Units of 512 bytes sectors: */
124 	unsigned	next_entry; /* How big the next journal entry can be */
125 	unsigned	total;
126 };
127 
128 enum journal_space_from {
129 	journal_space_discarded,
130 	journal_space_clean_ondisk,
131 	journal_space_clean,
132 	journal_space_total,
133 	journal_space_nr,
134 };
135 
136 #define JOURNAL_FLAGS()			\
137 	x(replay_done)			\
138 	x(running)			\
139 	x(may_skip_flush)		\
140 	x(need_flush_write)		\
141 	x(space_low)
142 
143 enum journal_flags {
144 #define x(n)	JOURNAL_##n,
145 	JOURNAL_FLAGS()
146 #undef x
147 };
148 
149 /* Reasons we may fail to get a journal reservation: */
150 #define JOURNAL_ERRORS()		\
151 	x(ok)				\
152 	x(retry)			\
153 	x(blocked)			\
154 	x(max_in_flight)		\
155 	x(journal_full)			\
156 	x(journal_pin_full)		\
157 	x(journal_stuck)		\
158 	x(insufficient_devices)
159 
160 enum journal_errors {
161 #define x(n)	JOURNAL_ERR_##n,
162 	JOURNAL_ERRORS()
163 #undef x
164 };
165 
166 typedef DARRAY(u64)		darray_u64;
167 
168 struct journal_bio {
169 	struct bch_dev		*ca;
170 	unsigned		buf_idx;
171 
172 	struct bio		bio;
173 };
174 
175 /* Embedded in struct bch_fs */
176 struct journal {
177 	/* Fastpath stuff up front: */
178 	struct {
179 
180 	union journal_res_state reservations;
181 	enum bch_watermark	watermark;
182 
183 	} __aligned(SMP_CACHE_BYTES);
184 
185 	unsigned long		flags;
186 
187 	/* Max size of current journal entry */
188 	unsigned		cur_entry_u64s;
189 	unsigned		cur_entry_sectors;
190 
191 	/* Reserved space in journal entry to be used just prior to write */
192 	unsigned		entry_u64s_reserved;
193 
194 
195 	/*
196 	 * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
197 	 * insufficient devices:
198 	 */
199 	enum journal_errors	cur_entry_error;
200 	unsigned		cur_entry_offset_if_blocked;
201 
202 	unsigned		buf_size_want;
203 	/*
204 	 * We may queue up some things to be journalled (log messages) before
205 	 * the journal has actually started - stash them here:
206 	 */
207 	darray_u64		early_journal_entries;
208 
209 	/*
210 	 * Protects journal_buf->data, when accessing without a jorunal
211 	 * reservation: for synchronization between the btree write buffer code
212 	 * and the journal write path:
213 	 */
214 	struct mutex		buf_lock;
215 	/*
216 	 * Two journal entries -- one is currently open for new entries, the
217 	 * other is possibly being written out.
218 	 */
219 	struct journal_buf	buf[JOURNAL_BUF_NR];
220 
221 	spinlock_t		lock;
222 
223 	/* if nonzero, we may not open a new journal entry: */
224 	unsigned		blocked;
225 
226 	/* Used when waiting because the journal was full */
227 	wait_queue_head_t	wait;
228 	struct closure_waitlist	async_wait;
229 	struct closure_waitlist	reclaim_flush_wait;
230 
231 	struct delayed_work	write_work;
232 	struct workqueue_struct *wq;
233 
234 	/* Sequence number of most recent journal entry (last entry in @pin) */
235 	atomic64_t		seq;
236 
237 	/* seq, last_seq from the most recent journal entry successfully written */
238 	u64			seq_ondisk;
239 	u64			flushed_seq_ondisk;
240 	u64			flushing_seq;
241 	u64			last_seq_ondisk;
242 	u64			err_seq;
243 	u64			last_empty_seq;
244 	u64			oldest_seq_found_ondisk;
245 
246 	/*
247 	 * FIFO of journal entries whose btree updates have not yet been
248 	 * written out.
249 	 *
250 	 * Each entry is a reference count. The position in the FIFO is the
251 	 * entry's sequence number relative to @seq.
252 	 *
253 	 * The journal entry itself holds a reference count, put when the
254 	 * journal entry is written out. Each btree node modified by the journal
255 	 * entry also holds a reference count, put when the btree node is
256 	 * written.
257 	 *
258 	 * When a reference count reaches zero, the journal entry is no longer
259 	 * needed. When all journal entries in the oldest journal bucket are no
260 	 * longer needed, the bucket can be discarded and reused.
261 	 */
262 	struct {
263 		u64 front, back, size, mask;
264 		struct journal_entry_pin_list *data;
265 	}			pin;
266 
267 	struct journal_space	space[journal_space_nr];
268 
269 	u64			replay_journal_seq;
270 	u64			replay_journal_seq_end;
271 
272 	struct write_point	wp;
273 	spinlock_t		err_lock;
274 
275 	struct mutex		reclaim_lock;
276 	/*
277 	 * Used for waiting until journal reclaim has freed up space in the
278 	 * journal:
279 	 */
280 	wait_queue_head_t	reclaim_wait;
281 	struct task_struct	*reclaim_thread;
282 	bool			reclaim_kicked;
283 	unsigned long		next_reclaim;
284 	u64			nr_direct_reclaim;
285 	u64			nr_background_reclaim;
286 
287 	unsigned long		last_flushed;
288 	struct journal_entry_pin *flush_in_progress;
289 	bool			flush_in_progress_dropped;
290 	wait_queue_head_t	pin_flush_wait;
291 
292 	/* protects advancing ja->discard_idx: */
293 	struct mutex		discard_lock;
294 	bool			can_discard;
295 
296 	unsigned long		last_flush_write;
297 
298 	u64			write_start_time;
299 
300 	u64			nr_flush_writes;
301 	u64			nr_noflush_writes;
302 	u64			entry_bytes_written;
303 
304 	struct bch2_time_stats	*flush_write_time;
305 	struct bch2_time_stats	*noflush_write_time;
306 	struct bch2_time_stats	*flush_seq_time;
307 
308 #ifdef CONFIG_DEBUG_LOCK_ALLOC
309 	struct lockdep_map	res_map;
310 #endif
311 } __aligned(SMP_CACHE_BYTES);
312 
313 /*
314  * Embedded in struct bch_dev. First three fields refer to the array of journal
315  * buckets, in bch_sb.
316  */
317 struct journal_device {
318 	/*
319 	 * For each journal bucket, contains the max sequence number of the
320 	 * journal writes it contains - so we know when a bucket can be reused.
321 	 */
322 	u64			*bucket_seq;
323 
324 	unsigned		sectors_free;
325 
326 	/*
327 	 * discard_idx <= dirty_idx_ondisk <= dirty_idx <= cur_idx:
328 	 */
329 	unsigned		discard_idx;		/* Next bucket to discard */
330 	unsigned		dirty_idx_ondisk;
331 	unsigned		dirty_idx;
332 	unsigned		cur_idx;		/* Journal bucket we're currently writing to */
333 	unsigned		nr;
334 
335 	u64			*buckets;
336 
337 	/* Bio for journal reads/writes to this device */
338 	struct journal_bio	*bio[JOURNAL_BUF_NR];
339 
340 	/* for bch_journal_read_device */
341 	struct closure		read;
342 	u64			highest_seq_found;
343 };
344 
345 /*
346  * journal_entry_res - reserve space in every journal entry:
347  */
348 struct journal_entry_res {
349 	unsigned		u64s;
350 };
351 
352 #endif /* _BCACHEFS_JOURNAL_TYPES_H */
353