]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/journal_types.h
bcache in userspace; userspace fsck
[bcachefs-tools-debian] / libbcache / journal_types.h
1 #ifndef _BCACHE_JOURNAL_TYPES_H
2 #define _BCACHE_JOURNAL_TYPES_H
3
4 #include <linux/cache.h>
5 #include <linux/workqueue.h>
6
7 #include "alloc_types.h"
8 #include "fifo.h"
9
10 struct journal_res;
11
12 /*
13  * We put two of these in struct journal; we used them for writes to the
14  * journal that are being staged or in flight.
15  */
16 struct journal_buf {
17         struct jset             *data;
18         struct closure_waitlist wait;
19
20         /*
21          * ugh, prio_buckets are stupid - need to convert them to new
22          * transaction machinery when it arrives
23          */
24         unsigned                nr_prio_buckets;
25
26         /* bloom filter: */
27         unsigned long           has_inode[1024 / sizeof(unsigned long)];
28 };
29
30 /*
31  * Something that makes a journal entry dirty - i.e. a btree node that has to be
32  * flushed:
33  */
34
35 struct journal_entry_pin_list {
36         struct list_head                list;
37         atomic_t                        count;
38 };
39
40 struct journal;
41 struct journal_entry_pin;
42 typedef void (*journal_pin_flush_fn)(struct journal *j, struct journal_entry_pin *);
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 /* 4 mb, in bytes: */
94 #define JOURNAL_ENTRY_SIZE_MAX          (4U << 20)
95
96 /*
97  * We stash some journal state as sentinal values in cur_entry_offset:
98  */
99 #define JOURNAL_ENTRY_OFFSET_MAX        ((1U << 20) - 1)
100
101 #define JOURNAL_ENTRY_CLOSED_VAL        (JOURNAL_ENTRY_OFFSET_MAX - 1)
102 #define JOURNAL_ENTRY_ERROR_VAL         (JOURNAL_ENTRY_OFFSET_MAX)
103
104 /*
105  * JOURNAL_NEED_WRITE - current (pending) journal entry should be written ASAP,
106  * either because something's waiting on the write to complete or because it's
107  * been dirty too long and the timer's expired.
108  */
109
110 enum {
111         JOURNAL_REPLAY_DONE,
112         JOURNAL_STARTED,
113         JOURNAL_NEED_WRITE,
114 };
115
116 /* Embedded in struct cache_set */
117 struct journal {
118         /* Fastpath stuff up front: */
119
120         unsigned long           flags;
121
122         union journal_res_state reservations;
123         unsigned                cur_entry_u64s;
124         unsigned                prev_buf_sectors;
125         unsigned                cur_buf_sectors;
126         unsigned                entry_size_max; /* bytes */
127
128         /*
129          * Two journal entries -- one is currently open for new entries, the
130          * other is possibly being written out.
131          */
132         struct journal_buf      buf[2];
133
134         spinlock_t              lock;
135
136         /* Used when waiting because the journal was full */
137         wait_queue_head_t       wait;
138
139         struct closure          io;
140         struct delayed_work     write_work;
141
142         /* Sequence number of most recent journal entry (last entry in @pin) */
143         atomic64_t              seq;
144
145         /* last_seq from the most recent journal entry written */
146         u64                     last_seq_ondisk;
147
148         /*
149          * FIFO of journal entries whose btree updates have not yet been
150          * written out.
151          *
152          * Each entry is a reference count. The position in the FIFO is the
153          * entry's sequence number relative to @seq.
154          *
155          * The journal entry itself holds a reference count, put when the
156          * journal entry is written out. Each btree node modified by the journal
157          * entry also holds a reference count, put when the btree node is
158          * written.
159          *
160          * When a reference count reaches zero, the journal entry is no longer
161          * needed. When all journal entries in the oldest journal bucket are no
162          * longer needed, the bucket can be discarded and reused.
163          */
164         DECLARE_FIFO(struct journal_entry_pin_list, pin);
165         struct journal_entry_pin_list *cur_pin_list;
166
167         /*
168          * Protects the pin lists - the fifo itself is still protected by
169          * j->lock though:
170          */
171         spinlock_t              pin_lock;
172
173         struct mutex            blacklist_lock;
174         struct list_head        seq_blacklist;
175
176         BKEY_PADDED(key);
177         struct cache_group      devs;
178
179         struct delayed_work     reclaim_work;
180         unsigned long           last_flushed;
181
182         /* protects advancing ja->last_idx: */
183         struct mutex            reclaim_lock;
184
185         /*
186          * ugh: need to get prio_buckets converted over to the eventual new
187          * transaction machinery
188          */
189         __le64                  prio_buckets[MAX_CACHES_PER_SET];
190         unsigned                nr_prio_buckets;
191
192         unsigned                write_delay_ms;
193         unsigned                reclaim_delay_ms;
194
195         u64                     res_get_blocked_start;
196         u64                     need_write_time;
197         u64                     write_start_time;
198
199         struct time_stats       *write_time;
200         struct time_stats       *delay_time;
201         struct time_stats       *blocked_time;
202         struct time_stats       *flush_seq_time;
203
204 #ifdef CONFIG_DEBUG_LOCK_ALLOC
205         struct lockdep_map      res_map;
206 #endif
207 };
208
209 /*
210  * Embedded in struct cache. First three fields refer to the array of journal
211  * buckets, in cache_sb.
212  */
213 struct journal_device {
214         /*
215          * For each journal bucket, contains the max sequence number of the
216          * journal writes it contains - so we know when a bucket can be reused.
217          */
218         u64                     *bucket_seq;
219
220         unsigned                sectors_free;
221
222         /* Journal bucket we're currently writing to */
223         unsigned                cur_idx;
224
225         /* Last journal bucket that still contains an open journal entry */
226
227         /*
228          * j->lock and j->reclaim_lock must both be held to modify, j->lock
229          * sufficient to read:
230          */
231         unsigned                last_idx;
232
233         /* Bio for journal reads/writes to this device */
234         struct bio              *bio;
235
236         /* for bch_journal_read_device */
237         struct closure          read;
238 };
239
240 #endif /* _BCACHE_JOURNAL_TYPES_H */