]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_types.h
Update bcachefs sources to b6d5426551 bcachefs: Fix bch2_btree_iter_peek_with_updates()
[bcachefs-tools-debian] / libbcachefs / journal_types.h
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 #define JOURNAL_BUF_BITS        2
13 #define JOURNAL_BUF_NR          (1U << JOURNAL_BUF_BITS)
14 #define JOURNAL_BUF_MASK        (JOURNAL_BUF_NR - 1)
15
16 /*
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.
19  */
20 struct journal_buf {
21         struct jset             *data;
22
23         __BKEY_PADDED(key, BCH_REPLICAS_MAX);
24
25         struct closure_waitlist wait;
26         u64                     last_seq;       /* copy of data->last_seq */
27
28         unsigned                buf_size;       /* size in bytes of @data */
29         unsigned                sectors;        /* maximum size for current entry */
30         unsigned                disk_sectors;   /* maximum size entry could have been, if
31                                                    buf_size was bigger */
32         unsigned                u64s_reserved;
33         bool                    noflush;        /* write has already been kicked off, and was noflush */
34         bool                    must_flush;     /* something wants a flush */
35         bool                    separate_flush;
36         /* bloom filter: */
37         unsigned long           has_inode[1024 / sizeof(unsigned long)];
38 };
39
40 /*
41  * Something that makes a journal entry dirty - i.e. a btree node that has to be
42  * flushed:
43  */
44
45 struct journal_entry_pin_list {
46         struct list_head                list;
47         struct list_head                key_cache_list;
48         struct list_head                flushed;
49         atomic_t                        count;
50         struct bch_devs_list            devs;
51 };
52
53 struct journal;
54 struct journal_entry_pin;
55 typedef int (*journal_pin_flush_fn)(struct journal *j,
56                                 struct journal_entry_pin *, u64);
57
58 struct journal_entry_pin {
59         struct list_head                list;
60         journal_pin_flush_fn            flush;
61         u64                             seq;
62 };
63
64 struct journal_res {
65         bool                    ref;
66         u8                      idx;
67         u16                     u64s;
68         u32                     offset;
69         u64                     seq;
70 };
71
72 /*
73  * For reserving space in the journal prior to getting a reservation on a
74  * particular journal entry:
75  */
76 struct journal_preres {
77         unsigned                u64s;
78 };
79
80 union journal_res_state {
81         struct {
82                 atomic64_t      counter;
83         };
84
85         struct {
86                 u64             v;
87         };
88
89         struct {
90                 u64             cur_entry_offset:20,
91                                 idx:2,
92                                 unwritten_idx:2,
93                                 buf0_count:10,
94                                 buf1_count:10,
95                                 buf2_count:10,
96                                 buf3_count:10;
97         };
98 };
99
100 union journal_preres_state {
101         struct {
102                 atomic64_t      counter;
103         };
104
105         struct {
106                 u64             v;
107         };
108
109         struct {
110                 u64             waiting:1,
111                                 reserved:31,
112                                 remaining:32;
113         };
114 };
115
116 /* bytes: */
117 #define JOURNAL_ENTRY_SIZE_MIN          (64U << 10) /* 64k */
118 #define JOURNAL_ENTRY_SIZE_MAX          (4U  << 20) /* 4M */
119
120 /*
121  * We stash some journal state as sentinal values in cur_entry_offset:
122  * note - cur_entry_offset is in units of u64s
123  */
124 #define JOURNAL_ENTRY_OFFSET_MAX        ((1U << 20) - 1)
125
126 #define JOURNAL_ENTRY_CLOSED_VAL        (JOURNAL_ENTRY_OFFSET_MAX - 1)
127 #define JOURNAL_ENTRY_ERROR_VAL         (JOURNAL_ENTRY_OFFSET_MAX)
128
129 struct journal_space {
130         /* Units of 512 bytes sectors: */
131         unsigned        next_entry; /* How big the next journal entry can be */
132         unsigned        total;
133 };
134
135 enum journal_space_from {
136         journal_space_discarded,
137         journal_space_clean_ondisk,
138         journal_space_clean,
139         journal_space_total,
140         journal_space_nr,
141 };
142
143 /*
144  * JOURNAL_NEED_WRITE - current (pending) journal entry should be written ASAP,
145  * either because something's waiting on the write to complete or because it's
146  * been dirty too long and the timer's expired.
147  */
148
149 enum {
150         JOURNAL_REPLAY_DONE,
151         JOURNAL_STARTED,
152         JOURNAL_RECLAIM_STARTED,
153         JOURNAL_NEED_WRITE,
154         JOURNAL_MAY_GET_UNRESERVED,
155         JOURNAL_MAY_SKIP_FLUSH,
156 };
157
158 /* Embedded in struct bch_fs */
159 struct journal {
160         /* Fastpath stuff up front: */
161
162         unsigned long           flags;
163
164         union journal_res_state reservations;
165
166         /* Max size of current journal entry */
167         unsigned                cur_entry_u64s;
168         unsigned                cur_entry_sectors;
169
170         /*
171          * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
172          * insufficient devices:
173          */
174         enum {
175                 cur_entry_ok,
176                 cur_entry_blocked,
177                 cur_entry_journal_full,
178                 cur_entry_journal_pin_full,
179                 cur_entry_journal_stuck,
180                 cur_entry_insufficient_devices,
181         }                       cur_entry_error;
182
183         union journal_preres_state prereserved;
184
185         /* Reserved space in journal entry to be used just prior to write */
186         unsigned                entry_u64s_reserved;
187
188         unsigned                buf_size_want;
189
190         /*
191          * Two journal entries -- one is currently open for new entries, the
192          * other is possibly being written out.
193          */
194         struct journal_buf      buf[JOURNAL_BUF_NR];
195
196         spinlock_t              lock;
197
198         /* if nonzero, we may not open a new journal entry: */
199         unsigned                blocked;
200
201         /* Used when waiting because the journal was full */
202         wait_queue_head_t       wait;
203         struct closure_waitlist async_wait;
204         struct closure_waitlist preres_wait;
205
206         struct closure          io;
207         struct delayed_work     write_work;
208
209         /* Sequence number of most recent journal entry (last entry in @pin) */
210         atomic64_t              seq;
211
212         /* seq, last_seq from the most recent journal entry successfully written */
213         u64                     seq_ondisk;
214         u64                     flushed_seq_ondisk;
215         u64                     last_seq_ondisk;
216         u64                     err_seq;
217         u64                     last_empty_seq;
218
219         /*
220          * FIFO of journal entries whose btree updates have not yet been
221          * written out.
222          *
223          * Each entry is a reference count. The position in the FIFO is the
224          * entry's sequence number relative to @seq.
225          *
226          * The journal entry itself holds a reference count, put when the
227          * journal entry is written out. Each btree node modified by the journal
228          * entry also holds a reference count, put when the btree node is
229          * written.
230          *
231          * When a reference count reaches zero, the journal entry is no longer
232          * needed. When all journal entries in the oldest journal bucket are no
233          * longer needed, the bucket can be discarded and reused.
234          */
235         struct {
236                 u64 front, back, size, mask;
237                 struct journal_entry_pin_list *data;
238         }                       pin;
239
240         struct journal_space    space[journal_space_nr];
241
242         u64                     replay_journal_seq;
243         u64                     replay_journal_seq_end;
244
245         struct write_point      wp;
246         spinlock_t              err_lock;
247
248         struct mutex            reclaim_lock;
249         wait_queue_head_t       reclaim_wait;
250         struct task_struct      *reclaim_thread;
251         bool                    reclaim_kicked;
252         unsigned long           next_reclaim;
253         u64                     nr_direct_reclaim;
254         u64                     nr_background_reclaim;
255
256         unsigned long           last_flushed;
257         struct journal_entry_pin *flush_in_progress;
258         bool                    flush_in_progress_dropped;
259         wait_queue_head_t       pin_flush_wait;
260
261         /* protects advancing ja->discard_idx: */
262         struct mutex            discard_lock;
263         bool                    can_discard;
264
265         unsigned                write_delay_ms;
266         unsigned                reclaim_delay_ms;
267         unsigned long           last_flush_write;
268
269         u64                     res_get_blocked_start;
270         u64                     need_write_time;
271         u64                     write_start_time;
272
273         u64                     nr_flush_writes;
274         u64                     nr_noflush_writes;
275
276         struct time_stats       *write_time;
277         struct time_stats       *delay_time;
278         struct time_stats       *blocked_time;
279         struct time_stats       *flush_seq_time;
280
281 #ifdef CONFIG_DEBUG_LOCK_ALLOC
282         struct lockdep_map      res_map;
283 #endif
284 };
285
286 /*
287  * Embedded in struct bch_dev. First three fields refer to the array of journal
288  * buckets, in bch_sb.
289  */
290 struct journal_device {
291         /*
292          * For each journal bucket, contains the max sequence number of the
293          * journal writes it contains - so we know when a bucket can be reused.
294          */
295         u64                     *bucket_seq;
296
297         unsigned                sectors_free;
298
299         /*
300          * discard_idx <= dirty_idx_ondisk <= dirty_idx <= cur_idx:
301          */
302         unsigned                discard_idx;            /* Next bucket to discard */
303         unsigned                dirty_idx_ondisk;
304         unsigned                dirty_idx;
305         unsigned                cur_idx;                /* Journal bucket we're currently writing to */
306         unsigned                nr;
307
308         u64                     *buckets;
309
310         /* Bio for journal reads/writes to this device */
311         struct bio              *bio;
312
313         /* for bch_journal_read_device */
314         struct closure          read;
315 };
316
317 /*
318  * journal_entry_res - reserve space in every journal entry:
319  */
320 struct journal_entry_res {
321         unsigned                u64s;
322 };
323
324 #endif /* _BCACHEFS_JOURNAL_TYPES_H */