]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_types.h
Update bcachefs sources to 09a5465430 bcachefs: Don't need to walk inodes on clean...
[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         unsigned                u64s_reserved;
27         /* bloom filter: */
28         unsigned long           has_inode[1024 / sizeof(unsigned long)];
29 };
30
31 /*
32  * Something that makes a journal entry dirty - i.e. a btree node that has to be
33  * flushed:
34  */
35
36 struct journal_entry_pin_list {
37         struct list_head                list;
38         struct list_head                flushed;
39         atomic_t                        count;
40         struct bch_devs_list            devs;
41 };
42
43 struct journal;
44 struct journal_entry_pin;
45 typedef void (*journal_pin_flush_fn)(struct journal *j,
46                                 struct journal_entry_pin *, u64);
47
48 struct journal_entry_pin {
49         struct list_head                list;
50         journal_pin_flush_fn            flush;
51         u64                             seq;
52 };
53
54 /* corresponds to a btree node with a blacklisted bset: */
55 struct blacklisted_node {
56         __le64                  seq;
57         enum btree_id           btree_id;
58         struct bpos             pos;
59 };
60
61 struct journal_seq_blacklist {
62         struct list_head        list;
63         u64                     start;
64         u64                     end;
65
66         struct journal_entry_pin pin;
67
68         struct blacklisted_node *entries;
69         size_t                  nr_entries;
70 };
71
72 struct journal_res {
73         bool                    ref;
74         u8                      idx;
75         u16                     u64s;
76         u32                     offset;
77         u64                     seq;
78 };
79
80 union journal_res_state {
81         struct {
82                 atomic64_t      counter;
83         };
84
85         struct {
86                 u64             v;
87         };
88
89         struct {
90                 u64             cur_entry_offset:20,
91                                 idx:1,
92                                 prev_buf_unwritten:1,
93                                 buf0_count:21,
94                                 buf1_count:21;
95         };
96 };
97
98 /* bytes: */
99 #define JOURNAL_ENTRY_SIZE_MIN          (64U << 10) /* 64k */
100 #define JOURNAL_ENTRY_SIZE_MAX          (4U  << 20) /* 4M */
101
102 /*
103  * We stash some journal state as sentinal values in cur_entry_offset:
104  * note - cur_entry_offset is in units of u64s
105  */
106 #define JOURNAL_ENTRY_OFFSET_MAX        ((1U << 20) - 1)
107
108 #define JOURNAL_ENTRY_CLOSED_VAL        (JOURNAL_ENTRY_OFFSET_MAX - 1)
109 #define JOURNAL_ENTRY_ERROR_VAL         (JOURNAL_ENTRY_OFFSET_MAX)
110
111 /*
112  * JOURNAL_NEED_WRITE - current (pending) journal entry should be written ASAP,
113  * either because something's waiting on the write to complete or because it's
114  * been dirty too long and the timer's expired.
115  */
116
117 enum {
118         JOURNAL_REPLAY_DONE,
119         JOURNAL_STARTED,
120         JOURNAL_NEED_WRITE,
121         JOURNAL_NOT_EMPTY,
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         struct closure_waitlist async_wait;
147
148         struct closure          io;
149         struct delayed_work     write_work;
150
151         /* Sequence number of most recent journal entry (last entry in @pin) */
152         atomic64_t              seq;
153
154         /* seq, last_seq from the most recent journal entry successfully written */
155         u64                     seq_ondisk;
156         u64                     last_seq_ondisk;
157
158         /* Reserved space in journal entry to be used just prior to write */
159         unsigned                entry_u64s_reserved;
160
161         /*
162          * FIFO of journal entries whose btree updates have not yet been
163          * written out.
164          *
165          * Each entry is a reference count. The position in the FIFO is the
166          * entry's sequence number relative to @seq.
167          *
168          * The journal entry itself holds a reference count, put when the
169          * journal entry is written out. Each btree node modified by the journal
170          * entry also holds a reference count, put when the btree node is
171          * written.
172          *
173          * When a reference count reaches zero, the journal entry is no longer
174          * needed. When all journal entries in the oldest journal bucket are no
175          * longer needed, the bucket can be discarded and reused.
176          */
177         struct {
178                 u64 front, back, size, mask;
179                 struct journal_entry_pin_list *data;
180         }                       pin;
181
182         struct journal_entry_pin *flush_in_progress;
183         wait_queue_head_t       pin_flush_wait;
184
185         u64                     replay_journal_seq;
186
187         struct mutex            blacklist_lock;
188         struct list_head        seq_blacklist;
189         struct journal_seq_blacklist *new_blacklist;
190
191         struct write_point      wp;
192         spinlock_t              err_lock;
193
194         struct delayed_work     reclaim_work;
195         unsigned long           last_flushed;
196
197         /* protects advancing ja->last_idx: */
198         struct mutex            reclaim_lock;
199         unsigned                write_delay_ms;
200         unsigned                reclaim_delay_ms;
201
202         u64                     res_get_blocked_start;
203         u64                     need_write_time;
204         u64                     write_start_time;
205
206         struct time_stats       *write_time;
207         struct time_stats       *delay_time;
208         struct time_stats       *blocked_time;
209         struct time_stats       *flush_seq_time;
210
211 #ifdef CONFIG_DEBUG_LOCK_ALLOC
212         struct lockdep_map      res_map;
213 #endif
214 };
215
216 /*
217  * Embedded in struct bch_dev. First three fields refer to the array of journal
218  * buckets, in bch_sb.
219  */
220 struct journal_device {
221         /*
222          * For each journal bucket, contains the max sequence number of the
223          * journal writes it contains - so we know when a bucket can be reused.
224          */
225         u64                     *bucket_seq;
226
227         unsigned                sectors_free;
228
229         /* Journal bucket we're currently writing to */
230         unsigned                cur_idx;
231
232         /* Last journal bucket that still contains an open journal entry */
233
234         /*
235          * j->lock and j->reclaim_lock must both be held to modify, j->lock
236          * sufficient to read:
237          */
238         unsigned                last_idx;
239         unsigned                nr;
240         u64                     *buckets;
241
242         /* Bio for journal reads/writes to this device */
243         struct bio              *bio;
244
245         /* for bch_journal_read_device */
246         struct closure          read;
247 };
248
249 /*
250  * journal_entry_res - reserve space in every journal entry:
251  */
252 struct journal_entry_res {
253         unsigned                u64s;
254 };
255
256 #endif /* _BCACHEFS_JOURNAL_TYPES_H */