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