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