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