1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_JOURNAL_TYPES_H
3 #define _BCACHEFS_JOURNAL_TYPES_H
5 #include <linux/cache.h>
6 #include <linux/workqueue.h>
8 #include "alloc_types.h"
9 #include "super_types.h"
12 #define JOURNAL_BUF_BITS 2
13 #define JOURNAL_BUF_NR (1U << JOURNAL_BUF_BITS)
14 #define JOURNAL_BUF_MASK (JOURNAL_BUF_NR - 1)
17 * We put JOURNAL_BUF_NR of these in struct journal; we used them for writes to
18 * the journal that are being staged or in flight.
23 __BKEY_PADDED(key, BCH_REPLICAS_MAX);
24 struct bch_devs_list devs_written;
26 struct closure_waitlist wait;
27 u64 last_seq; /* copy of data->last_seq */
31 unsigned buf_size; /* size in bytes of @data */
32 unsigned sectors; /* maximum size for current entry */
33 unsigned disk_sectors; /* maximum size entry could have been, if
34 buf_size was bigger */
35 unsigned u64s_reserved;
36 bool noflush; /* write has already been kicked off, and was noflush */
37 bool must_flush; /* something wants a flush */
42 * Something that makes a journal entry dirty - i.e. a btree node that has to be
46 struct journal_entry_pin_list {
47 struct list_head list;
48 struct list_head key_cache_list;
49 struct list_head flushed;
51 struct bch_devs_list devs;
55 struct journal_entry_pin;
56 typedef int (*journal_pin_flush_fn)(struct journal *j,
57 struct journal_entry_pin *, u64);
59 struct journal_entry_pin {
60 struct list_head list;
61 journal_pin_flush_fn flush;
74 * For reserving space in the journal prior to getting a reservation on a
75 * particular journal entry:
77 struct journal_preres {
81 union journal_res_state {
91 u64 cur_entry_offset:20,
101 union journal_preres_state {
118 #define JOURNAL_ENTRY_SIZE_MIN (64U << 10) /* 64k */
119 #define JOURNAL_ENTRY_SIZE_MAX (4U << 20) /* 4M */
122 * We stash some journal state as sentinal values in cur_entry_offset:
123 * note - cur_entry_offset is in units of u64s
125 #define JOURNAL_ENTRY_OFFSET_MAX ((1U << 20) - 1)
127 #define JOURNAL_ENTRY_CLOSED_VAL (JOURNAL_ENTRY_OFFSET_MAX - 1)
128 #define JOURNAL_ENTRY_ERROR_VAL (JOURNAL_ENTRY_OFFSET_MAX)
130 struct journal_space {
131 /* Units of 512 bytes sectors: */
132 unsigned next_entry; /* How big the next journal entry can be */
136 enum journal_space_from {
137 journal_space_discarded,
138 journal_space_clean_ondisk,
147 JOURNAL_MAY_SKIP_FLUSH,
148 JOURNAL_NEED_FLUSH_WRITE,
151 #define JOURNAL_WATERMARKS() \
156 enum journal_watermark {
157 #define x(n) JOURNAL_WATERMARK_##n,
162 #define JOURNAL_WATERMARK_MASK 3
164 /* Reasons we may fail to get a journal reservation: */
165 #define JOURNAL_ERRORS() \
170 x(journal_pin_full) \
172 x(insufficient_devices)
174 enum journal_errors {
175 #define x(n) JOURNAL_ERR_##n,
180 /* Embedded in struct bch_fs */
182 /* Fastpath stuff up front: */
186 union journal_res_state reservations;
187 enum journal_watermark watermark;
189 /* Max size of current journal entry */
190 unsigned cur_entry_u64s;
191 unsigned cur_entry_sectors;
194 * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
195 * insufficient devices:
197 enum journal_errors cur_entry_error;
199 union journal_preres_state prereserved;
201 /* Reserved space in journal entry to be used just prior to write */
202 unsigned entry_u64s_reserved;
204 unsigned buf_size_want;
207 * Two journal entries -- one is currently open for new entries, the
208 * other is possibly being written out.
210 struct journal_buf buf[JOURNAL_BUF_NR];
214 /* if nonzero, we may not open a new journal entry: */
217 /* Used when waiting because the journal was full */
218 wait_queue_head_t wait;
219 struct closure_waitlist async_wait;
220 struct closure_waitlist preres_wait;
223 struct delayed_work write_work;
225 /* Sequence number of most recent journal entry (last entry in @pin) */
228 /* seq, last_seq from the most recent journal entry successfully written */
230 u64 flushed_seq_ondisk;
236 * FIFO of journal entries whose btree updates have not yet been
239 * Each entry is a reference count. The position in the FIFO is the
240 * entry's sequence number relative to @seq.
242 * The journal entry itself holds a reference count, put when the
243 * journal entry is written out. Each btree node modified by the journal
244 * entry also holds a reference count, put when the btree node is
247 * When a reference count reaches zero, the journal entry is no longer
248 * needed. When all journal entries in the oldest journal bucket are no
249 * longer needed, the bucket can be discarded and reused.
252 u64 front, back, size, mask;
253 struct journal_entry_pin_list *data;
256 struct journal_space space[journal_space_nr];
258 u64 replay_journal_seq;
259 u64 replay_journal_seq_end;
261 struct write_point wp;
264 struct mutex reclaim_lock;
266 * Used for waiting until journal reclaim has freed up space in the
269 wait_queue_head_t reclaim_wait;
270 struct task_struct *reclaim_thread;
272 unsigned long next_reclaim;
273 u64 nr_direct_reclaim;
274 u64 nr_background_reclaim;
276 unsigned long last_flushed;
277 struct journal_entry_pin *flush_in_progress;
278 bool flush_in_progress_dropped;
279 wait_queue_head_t pin_flush_wait;
281 /* protects advancing ja->discard_idx: */
282 struct mutex discard_lock;
285 unsigned long last_flush_write;
287 u64 res_get_blocked_start;
288 u64 write_start_time;
291 u64 nr_noflush_writes;
293 struct time_stats *flush_write_time;
294 struct time_stats *noflush_write_time;
295 struct time_stats *blocked_time;
296 struct time_stats *flush_seq_time;
298 #ifdef CONFIG_DEBUG_LOCK_ALLOC
299 struct lockdep_map res_map;
304 * Embedded in struct bch_dev. First three fields refer to the array of journal
305 * buckets, in bch_sb.
307 struct journal_device {
309 * For each journal bucket, contains the max sequence number of the
310 * journal writes it contains - so we know when a bucket can be reused.
314 unsigned sectors_free;
317 * discard_idx <= dirty_idx_ondisk <= dirty_idx <= cur_idx:
319 unsigned discard_idx; /* Next bucket to discard */
320 unsigned dirty_idx_ondisk;
322 unsigned cur_idx; /* Journal bucket we're currently writing to */
327 /* Bio for journal reads/writes to this device */
330 /* for bch_journal_read_device */
335 * journal_entry_res - reserve space in every journal entry:
337 struct journal_entry_res {
341 #endif /* _BCACHEFS_JOURNAL_TYPES_H */