]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_types.h
Update bcachefs sources to e82e656279 bcachefs: Cleanups for building in userspace
[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         struct closure_waitlist wait;
21
22         unsigned                size;
23         unsigned                disk_sectors;
24         /* bloom filter: */
25         unsigned long           has_inode[1024 / sizeof(unsigned long)];
26 };
27
28 /*
29  * Something that makes a journal entry dirty - i.e. a btree node that has to be
30  * flushed:
31  */
32
33 struct journal_entry_pin_list {
34         struct list_head                list;
35         struct list_head                flushed;
36         atomic_t                        count;
37 };
38
39 struct journal;
40 struct journal_entry_pin;
41 typedef void (*journal_pin_flush_fn)(struct journal *j,
42                                 struct journal_entry_pin *, u64);
43
44 struct journal_entry_pin {
45         struct list_head                list;
46         journal_pin_flush_fn            flush;
47         struct journal_entry_pin_list   *pin_list;
48 };
49
50 /* corresponds to a btree node with a blacklisted bset: */
51 struct blacklisted_node {
52         __le64                  seq;
53         enum btree_id           btree_id;
54         struct bpos             pos;
55 };
56
57 struct journal_seq_blacklist {
58         struct list_head        list;
59         u64                     seq;
60         bool                    written;
61         struct journal_entry_pin pin;
62
63         struct blacklisted_node *entries;
64         size_t                  nr_entries;
65 };
66
67 struct journal_res {
68         bool                    ref;
69         u8                      idx;
70         u16                     u64s;
71         u32                     offset;
72         u64                     seq;
73 };
74
75 union journal_res_state {
76         struct {
77                 atomic64_t      counter;
78         };
79
80         struct {
81                 u64             v;
82         };
83
84         struct {
85                 u64             cur_entry_offset:20,
86                                 idx:1,
87                                 prev_buf_unwritten:1,
88                                 buf0_count:21,
89                                 buf1_count:21;
90         };
91 };
92
93 /* bytes: */
94 #define JOURNAL_ENTRY_SIZE_MIN          (64U << 10) /* 64k */
95 #define JOURNAL_ENTRY_SIZE_MAX          (4U  << 20) /* 4M */
96
97 /*
98  * We stash some journal state as sentinal values in cur_entry_offset:
99  * note - cur_entry_offset is in units of u64s
100  */
101 #define JOURNAL_ENTRY_OFFSET_MAX        ((1U << 20) - 1)
102
103 #define JOURNAL_ENTRY_CLOSED_VAL        (JOURNAL_ENTRY_OFFSET_MAX - 1)
104 #define JOURNAL_ENTRY_ERROR_VAL         (JOURNAL_ENTRY_OFFSET_MAX)
105
106 /*
107  * JOURNAL_NEED_WRITE - current (pending) journal entry should be written ASAP,
108  * either because something's waiting on the write to complete or because it's
109  * been dirty too long and the timer's expired.
110  */
111
112 enum {
113         JOURNAL_REPLAY_DONE,
114         JOURNAL_STARTED,
115         JOURNAL_NEED_WRITE,
116 };
117
118 /* Embedded in struct bch_fs */
119 struct journal {
120         /* Fastpath stuff up front: */
121
122         unsigned long           flags;
123
124         union journal_res_state reservations;
125         unsigned                cur_entry_u64s;
126         unsigned                prev_buf_sectors;
127         unsigned                cur_buf_sectors;
128         unsigned                buf_size_want;
129
130         /*
131          * Two journal entries -- one is currently open for new entries, the
132          * other is possibly being written out.
133          */
134         struct journal_buf      buf[2];
135
136         spinlock_t              lock;
137
138         /* Used when waiting because the journal was full */
139         wait_queue_head_t       wait;
140
141         struct closure          io;
142         struct delayed_work     write_work;
143         unsigned long           replicas_failed;
144
145         /* Sequence number of most recent journal entry (last entry in @pin) */
146         atomic64_t              seq;
147
148         /* last_seq from the most recent journal entry written */
149         u64                     last_seq_ondisk;
150
151         /*
152          * FIFO of journal entries whose btree updates have not yet been
153          * written out.
154          *
155          * Each entry is a reference count. The position in the FIFO is the
156          * entry's sequence number relative to @seq.
157          *
158          * The journal entry itself holds a reference count, put when the
159          * journal entry is written out. Each btree node modified by the journal
160          * entry also holds a reference count, put when the btree node is
161          * written.
162          *
163          * When a reference count reaches zero, the journal entry is no longer
164          * needed. When all journal entries in the oldest journal bucket are no
165          * longer needed, the bucket can be discarded and reused.
166          */
167         DECLARE_FIFO(struct journal_entry_pin_list, pin);
168         struct journal_entry_pin_list *replay_pin_list;
169
170         /*
171          * Protects the pin lists - the fifo itself is still protected by
172          * j->lock though:
173          */
174         spinlock_t              pin_lock;
175
176         struct mutex            blacklist_lock;
177         struct list_head        seq_blacklist;
178
179         BKEY_PADDED(key);
180         struct write_point      wp;
181
182         struct delayed_work     reclaim_work;
183         unsigned long           last_flushed;
184
185         /* protects advancing ja->last_idx: */
186         struct mutex            reclaim_lock;
187         unsigned                write_delay_ms;
188         unsigned                reclaim_delay_ms;
189
190         u64                     res_get_blocked_start;
191         u64                     need_write_time;
192         u64                     write_start_time;
193
194         struct time_stats       *write_time;
195         struct time_stats       *delay_time;
196         struct time_stats       *blocked_time;
197         struct time_stats       *flush_seq_time;
198
199 #ifdef CONFIG_DEBUG_LOCK_ALLOC
200         struct lockdep_map      res_map;
201 #endif
202 };
203
204 /*
205  * Embedded in struct bch_dev. First three fields refer to the array of journal
206  * buckets, in bch_sb.
207  */
208 struct journal_device {
209         /*
210          * For each journal bucket, contains the max sequence number of the
211          * journal writes it contains - so we know when a bucket can be reused.
212          */
213         u64                     *bucket_seq;
214
215         unsigned                sectors_free;
216
217         /* Journal bucket we're currently writing to */
218         unsigned                cur_idx;
219
220         /* Last journal bucket that still contains an open journal entry */
221
222         /*
223          * j->lock and j->reclaim_lock must both be held to modify, j->lock
224          * sufficient to read:
225          */
226         unsigned                last_idx;
227         unsigned                nr;
228         u64                     *buckets;
229
230         /* Bio for journal reads/writes to this device */
231         struct bio              *bio;
232         u8                      ptr_idx;
233
234         /* for bch_journal_read_device */
235         struct closure          read;
236 };
237
238 #endif /* _BCACHEFS_JOURNAL_TYPES_H */