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"
15 * We put two of these in struct journal; we used them for writes to the
16 * journal that are being staged or in flight.
23 struct closure_waitlist wait;
25 unsigned buf_size; /* size in bytes of @data */
26 unsigned sectors; /* maximum size for current entry */
27 unsigned disk_sectors; /* maximum size entry could have been, if
28 buf_size was bigger */
29 unsigned u64s_reserved;
31 unsigned long has_inode[1024 / sizeof(unsigned long)];
35 * Something that makes a journal entry dirty - i.e. a btree node that has to be
39 struct journal_entry_pin_list {
40 struct list_head list;
41 struct list_head flushed;
43 struct bch_devs_list devs;
47 struct journal_entry_pin;
48 typedef void (*journal_pin_flush_fn)(struct journal *j,
49 struct journal_entry_pin *, u64);
51 struct journal_entry_pin {
52 struct list_head list;
53 journal_pin_flush_fn flush;
66 * For reserving space in the journal prior to getting a reservation on a
67 * particular journal entry:
69 struct journal_preres {
73 union journal_res_state {
83 u64 cur_entry_offset:20,
91 union journal_preres_state {
107 #define JOURNAL_ENTRY_SIZE_MIN (64U << 10) /* 64k */
108 #define JOURNAL_ENTRY_SIZE_MAX (4U << 20) /* 4M */
111 * We stash some journal state as sentinal values in cur_entry_offset:
112 * note - cur_entry_offset is in units of u64s
114 #define JOURNAL_ENTRY_OFFSET_MAX ((1U << 20) - 1)
116 #define JOURNAL_ENTRY_CLOSED_VAL (JOURNAL_ENTRY_OFFSET_MAX - 1)
117 #define JOURNAL_ENTRY_ERROR_VAL (JOURNAL_ENTRY_OFFSET_MAX)
120 * JOURNAL_NEED_WRITE - current (pending) journal entry should be written ASAP,
121 * either because something's waiting on the write to complete or because it's
122 * been dirty too long and the timer's expired.
128 JOURNAL_RECLAIM_STARTED,
131 JOURNAL_MAY_GET_UNRESERVED,
134 /* Embedded in struct bch_fs */
136 /* Fastpath stuff up front: */
140 union journal_res_state reservations;
142 /* Max size of current journal entry */
143 unsigned cur_entry_u64s;
144 unsigned cur_entry_sectors;
147 * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
148 * insufficient devices:
152 union journal_preres_state prereserved;
154 /* Reserved space in journal entry to be used just prior to write */
155 unsigned entry_u64s_reserved;
157 unsigned buf_size_want;
160 * Two journal entries -- one is currently open for new entries, the
161 * other is possibly being written out.
163 struct journal_buf buf[2];
167 /* if nonzero, we may not open a new journal entry: */
170 /* Used when waiting because the journal was full */
171 wait_queue_head_t wait;
172 struct closure_waitlist async_wait;
173 struct closure_waitlist preres_wait;
176 struct delayed_work write_work;
178 /* Sequence number of most recent journal entry (last entry in @pin) */
181 /* seq, last_seq from the most recent journal entry successfully written */
186 * FIFO of journal entries whose btree updates have not yet been
189 * Each entry is a reference count. The position in the FIFO is the
190 * entry's sequence number relative to @seq.
192 * The journal entry itself holds a reference count, put when the
193 * journal entry is written out. Each btree node modified by the journal
194 * entry also holds a reference count, put when the btree node is
197 * When a reference count reaches zero, the journal entry is no longer
198 * needed. When all journal entries in the oldest journal bucket are no
199 * longer needed, the bucket can be discarded and reused.
202 u64 front, back, size, mask;
203 struct journal_entry_pin_list *data;
206 u64 replay_journal_seq;
207 u64 replay_journal_seq_end;
209 struct write_point wp;
212 struct delayed_work reclaim_work;
213 struct mutex reclaim_lock;
214 unsigned long last_flushed;
215 struct journal_entry_pin *flush_in_progress;
216 wait_queue_head_t pin_flush_wait;
218 /* protects advancing ja->discard_idx: */
219 struct mutex discard_lock;
222 unsigned write_delay_ms;
223 unsigned reclaim_delay_ms;
225 u64 res_get_blocked_start;
227 u64 write_start_time;
229 struct time_stats *write_time;
230 struct time_stats *delay_time;
231 struct time_stats *blocked_time;
232 struct time_stats *flush_seq_time;
234 #ifdef CONFIG_DEBUG_LOCK_ALLOC
235 struct lockdep_map res_map;
240 * Embedded in struct bch_dev. First three fields refer to the array of journal
241 * buckets, in bch_sb.
243 struct journal_device {
245 * For each journal bucket, contains the max sequence number of the
246 * journal writes it contains - so we know when a bucket can be reused.
250 unsigned sectors_free;
253 * discard_idx <= dirty_idx_ondisk <= dirty_idx <= cur_idx:
255 unsigned discard_idx; /* Next bucket to discard */
256 unsigned dirty_idx_ondisk;
258 unsigned cur_idx; /* Journal bucket we're currently writing to */
263 /* Bio for journal reads/writes to this device */
266 /* for bch_journal_read_device */
271 * journal_entry_res - reserve space in every journal entry:
273 struct journal_entry_res {
277 #endif /* _BCACHEFS_JOURNAL_TYPES_H */