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