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