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