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