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