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