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