]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_types.h
Update bcachefs sources to a5e71b8200 bcachefs: Allocator startup fixes/refactoring
[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 /* corresponds to a btree node with a blacklisted bset: */
57 struct blacklisted_node {
58         __le64                  seq;
59         enum btree_id           btree_id;
60         struct bpos             pos;
61 };
62
63 struct journal_seq_blacklist {
64         struct list_head        list;
65         u64                     start;
66         u64                     end;
67
68         struct journal_entry_pin pin;
69
70         struct blacklisted_node *entries;
71         size_t                  nr_entries;
72 };
73
74 struct journal_res {
75         bool                    ref;
76         u8                      idx;
77         u16                     u64s;
78         u32                     offset;
79         u64                     seq;
80 };
81
82 union journal_res_state {
83         struct {
84                 atomic64_t      counter;
85         };
86
87         struct {
88                 u64             v;
89         };
90
91         struct {
92                 u64             cur_entry_offset:20,
93                                 idx:1,
94                                 prev_buf_unwritten:1,
95                                 buf0_count:21,
96                                 buf1_count:21;
97         };
98 };
99
100 /* bytes: */
101 #define JOURNAL_ENTRY_SIZE_MIN          (64U << 10) /* 64k */
102 #define JOURNAL_ENTRY_SIZE_MAX          (4U  << 20) /* 4M */
103
104 /*
105  * We stash some journal state as sentinal values in cur_entry_offset:
106  * note - cur_entry_offset is in units of u64s
107  */
108 #define JOURNAL_ENTRY_OFFSET_MAX        ((1U << 20) - 1)
109
110 #define JOURNAL_ENTRY_CLOSED_VAL        (JOURNAL_ENTRY_OFFSET_MAX - 1)
111 #define JOURNAL_ENTRY_ERROR_VAL         (JOURNAL_ENTRY_OFFSET_MAX)
112
113 /*
114  * JOURNAL_NEED_WRITE - current (pending) journal entry should be written ASAP,
115  * either because something's waiting on the write to complete or because it's
116  * been dirty too long and the timer's expired.
117  */
118
119 enum {
120         JOURNAL_REPLAY_DONE,
121         JOURNAL_STARTED,
122         JOURNAL_NEED_WRITE,
123         JOURNAL_NOT_EMPTY,
124 };
125
126 /* Embedded in struct bch_fs */
127 struct journal {
128         /* Fastpath stuff up front: */
129
130         unsigned long           flags;
131
132         union journal_res_state reservations;
133
134         /* Max size of current journal entry */
135         unsigned                cur_entry_u64s;
136         unsigned                cur_entry_sectors;
137
138         /*
139          * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
140          * insufficient devices:
141          */
142         int                     cur_entry_error;
143
144         /* Reserved space in journal entry to be used just prior to write */
145         unsigned                entry_u64s_reserved;
146
147         unsigned                buf_size_want;
148
149         /*
150          * Two journal entries -- one is currently open for new entries, the
151          * other is possibly being written out.
152          */
153         struct journal_buf      buf[2];
154
155         spinlock_t              lock;
156
157         /* if nonzero, we may not open a new journal entry: */
158         unsigned                blocked;
159
160         /* Used when waiting because the journal was full */
161         wait_queue_head_t       wait;
162         struct closure_waitlist async_wait;
163
164         struct closure          io;
165         struct delayed_work     write_work;
166
167         /* Sequence number of most recent journal entry (last entry in @pin) */
168         atomic64_t              seq;
169
170         /* seq, last_seq from the most recent journal entry successfully written */
171         u64                     seq_ondisk;
172         u64                     last_seq_ondisk;
173
174         /*
175          * FIFO of journal entries whose btree updates have not yet been
176          * written out.
177          *
178          * Each entry is a reference count. The position in the FIFO is the
179          * entry's sequence number relative to @seq.
180          *
181          * The journal entry itself holds a reference count, put when the
182          * journal entry is written out. Each btree node modified by the journal
183          * entry also holds a reference count, put when the btree node is
184          * written.
185          *
186          * When a reference count reaches zero, the journal entry is no longer
187          * needed. When all journal entries in the oldest journal bucket are no
188          * longer needed, the bucket can be discarded and reused.
189          */
190         struct {
191                 u64 front, back, size, mask;
192                 struct journal_entry_pin_list *data;
193         }                       pin;
194
195         struct journal_entry_pin *flush_in_progress;
196         wait_queue_head_t       pin_flush_wait;
197
198         u64                     replay_journal_seq;
199
200         struct mutex            blacklist_lock;
201         struct list_head        seq_blacklist;
202         struct journal_seq_blacklist *new_blacklist;
203
204         struct write_point      wp;
205         spinlock_t              err_lock;
206
207         struct delayed_work     reclaim_work;
208         unsigned long           last_flushed;
209
210         /* protects advancing ja->last_idx: */
211         struct mutex            reclaim_lock;
212         unsigned                write_delay_ms;
213         unsigned                reclaim_delay_ms;
214
215         u64                     res_get_blocked_start;
216         u64                     need_write_time;
217         u64                     write_start_time;
218
219         struct time_stats       *write_time;
220         struct time_stats       *delay_time;
221         struct time_stats       *blocked_time;
222         struct time_stats       *flush_seq_time;
223
224 #ifdef CONFIG_DEBUG_LOCK_ALLOC
225         struct lockdep_map      res_map;
226 #endif
227 };
228
229 /*
230  * Embedded in struct bch_dev. First three fields refer to the array of journal
231  * buckets, in bch_sb.
232  */
233 struct journal_device {
234         /*
235          * For each journal bucket, contains the max sequence number of the
236          * journal writes it contains - so we know when a bucket can be reused.
237          */
238         u64                     *bucket_seq;
239
240         unsigned                sectors_free;
241
242         /* Journal bucket we're currently writing to */
243         unsigned                cur_idx;
244
245         /* Last journal bucket that still contains an open journal entry */
246
247         /*
248          * j->lock and j->reclaim_lock must both be held to modify, j->lock
249          * sufficient to read:
250          */
251         unsigned                last_idx;
252         unsigned                nr;
253         u64                     *buckets;
254
255         /* Bio for journal reads/writes to this device */
256         struct bio              *bio;
257
258         /* for bch_journal_read_device */
259         struct closure          read;
260 };
261
262 /*
263  * journal_entry_res - reserve space in every journal entry:
264  */
265 struct journal_entry_res {
266         unsigned                u64s;
267 };
268
269 #endif /* _BCACHEFS_JOURNAL_TYPES_H */