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 enum journal_pin_type {
48 JOURNAL_PIN_key_cache,
53 struct journal_entry_pin_list {
54 struct list_head list[JOURNAL_PIN_NR];
55 struct list_head flushed;
57 struct bch_devs_list devs;
61 struct journal_entry_pin;
62 typedef int (*journal_pin_flush_fn)(struct journal *j,
63 struct journal_entry_pin *, u64);
65 struct journal_entry_pin {
66 struct list_head list;
67 journal_pin_flush_fn flush;
79 union journal_res_state {
89 u64 cur_entry_offset:20,
100 #define JOURNAL_ENTRY_SIZE_MIN (64U << 10) /* 64k */
101 #define JOURNAL_ENTRY_SIZE_MAX (4U << 20) /* 4M */
104 * We stash some journal state as sentinal values in cur_entry_offset:
105 * note - cur_entry_offset is in units of u64s
107 #define JOURNAL_ENTRY_OFFSET_MAX ((1U << 20) - 1)
109 #define JOURNAL_ENTRY_CLOSED_VAL (JOURNAL_ENTRY_OFFSET_MAX - 1)
110 #define JOURNAL_ENTRY_ERROR_VAL (JOURNAL_ENTRY_OFFSET_MAX)
112 struct journal_space {
113 /* Units of 512 bytes sectors: */
114 unsigned next_entry; /* How big the next journal entry can be */
118 enum journal_space_from {
119 journal_space_discarded,
120 journal_space_clean_ondisk,
129 JOURNAL_MAY_SKIP_FLUSH,
130 JOURNAL_NEED_FLUSH_WRITE,
133 /* Reasons we may fail to get a journal reservation: */
134 #define JOURNAL_ERRORS() \
139 x(journal_pin_full) \
141 x(insufficient_devices)
143 enum journal_errors {
144 #define x(n) JOURNAL_ERR_##n,
149 typedef DARRAY(u64) darray_u64;
151 /* Embedded in struct bch_fs */
153 /* Fastpath stuff up front: */
156 union journal_res_state reservations;
157 enum bch_watermark watermark;
159 } __aligned(SMP_CACHE_BYTES);
163 /* Max size of current journal entry */
164 unsigned cur_entry_u64s;
165 unsigned cur_entry_sectors;
167 /* Reserved space in journal entry to be used just prior to write */
168 unsigned entry_u64s_reserved;
172 * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
173 * insufficient devices:
175 enum journal_errors cur_entry_error;
177 unsigned buf_size_want;
179 * We may queue up some things to be journalled (log messages) before
180 * the journal has actually started - stash them here:
182 darray_u64 early_journal_entries;
185 * Two journal entries -- one is currently open for new entries, the
186 * other is possibly being written out.
188 struct journal_buf buf[JOURNAL_BUF_NR];
192 /* if nonzero, we may not open a new journal entry: */
195 /* Used when waiting because the journal was full */
196 wait_queue_head_t wait;
197 struct closure_waitlist async_wait;
198 struct closure_waitlist preres_wait;
201 struct delayed_work write_work;
203 /* Sequence number of most recent journal entry (last entry in @pin) */
206 /* seq, last_seq from the most recent journal entry successfully written */
208 u64 flushed_seq_ondisk;
214 * FIFO of journal entries whose btree updates have not yet been
217 * Each entry is a reference count. The position in the FIFO is the
218 * entry's sequence number relative to @seq.
220 * The journal entry itself holds a reference count, put when the
221 * journal entry is written out. Each btree node modified by the journal
222 * entry also holds a reference count, put when the btree node is
225 * When a reference count reaches zero, the journal entry is no longer
226 * needed. When all journal entries in the oldest journal bucket are no
227 * longer needed, the bucket can be discarded and reused.
230 u64 front, back, size, mask;
231 struct journal_entry_pin_list *data;
234 struct journal_space space[journal_space_nr];
236 u64 replay_journal_seq;
237 u64 replay_journal_seq_end;
239 struct write_point wp;
242 struct mutex reclaim_lock;
244 * Used for waiting until journal reclaim has freed up space in the
247 wait_queue_head_t reclaim_wait;
248 struct task_struct *reclaim_thread;
250 unsigned long next_reclaim;
251 u64 nr_direct_reclaim;
252 u64 nr_background_reclaim;
254 unsigned long last_flushed;
255 struct journal_entry_pin *flush_in_progress;
256 bool flush_in_progress_dropped;
257 wait_queue_head_t pin_flush_wait;
259 /* protects advancing ja->discard_idx: */
260 struct mutex discard_lock;
263 unsigned long last_flush_write;
265 u64 write_start_time;
268 u64 nr_noflush_writes;
269 u64 entry_bytes_written;
271 u64 low_on_space_start;
272 u64 low_on_pin_start;
273 u64 max_in_flight_start;
275 struct bch2_time_stats *flush_write_time;
276 struct bch2_time_stats *noflush_write_time;
277 struct bch2_time_stats *flush_seq_time;
279 #ifdef CONFIG_DEBUG_LOCK_ALLOC
280 struct lockdep_map res_map;
282 } __aligned(SMP_CACHE_BYTES);
285 * Embedded in struct bch_dev. First three fields refer to the array of journal
286 * buckets, in bch_sb.
288 struct journal_device {
290 * For each journal bucket, contains the max sequence number of the
291 * journal writes it contains - so we know when a bucket can be reused.
295 unsigned sectors_free;
298 * discard_idx <= dirty_idx_ondisk <= dirty_idx <= cur_idx:
300 unsigned discard_idx; /* Next bucket to discard */
301 unsigned dirty_idx_ondisk;
303 unsigned cur_idx; /* Journal bucket we're currently writing to */
308 /* Bio for journal reads/writes to this device */
311 /* for bch_journal_read_device */
316 * journal_entry_res - reserve space in every journal entry:
318 struct journal_entry_res {
322 #endif /* _BCACHEFS_JOURNAL_TYPES_H */