]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_types.h
Update bcachefs sources to da7fefde29 bcachefs: shim for userspace raid library
[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         u64                             seq;
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         JOURNAL_NOT_EMPTY,
121 };
122
123 /* Embedded in struct bch_fs */
124 struct journal {
125         /* Fastpath stuff up front: */
126
127         unsigned long           flags;
128
129         union journal_res_state reservations;
130         unsigned                cur_entry_u64s;
131         unsigned                prev_buf_sectors;
132         unsigned                cur_buf_sectors;
133         unsigned                buf_size_want;
134
135         /*
136          * Two journal entries -- one is currently open for new entries, the
137          * other is possibly being written out.
138          */
139         struct journal_buf      buf[2];
140
141         spinlock_t              lock;
142
143         /* Used when waiting because the journal was full */
144         wait_queue_head_t       wait;
145         struct closure_waitlist async_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         /* seq, last_seq from the most recent journal entry successfully written */
154         u64                     seq_ondisk;
155         u64                     last_seq_ondisk;
156
157         /*
158          * FIFO of journal entries whose btree updates have not yet been
159          * written out.
160          *
161          * Each entry is a reference count. The position in the FIFO is the
162          * entry's sequence number relative to @seq.
163          *
164          * The journal entry itself holds a reference count, put when the
165          * journal entry is written out. Each btree node modified by the journal
166          * entry also holds a reference count, put when the btree node is
167          * written.
168          *
169          * When a reference count reaches zero, the journal entry is no longer
170          * needed. When all journal entries in the oldest journal bucket are no
171          * longer needed, the bucket can be discarded and reused.
172          */
173         struct {
174                 u64 front, back, size, mask;
175                 struct journal_entry_pin_list *data;
176         }                       pin;
177
178         struct journal_entry_pin *flush_in_progress;
179         wait_queue_head_t       pin_flush_wait;
180
181         u64                     replay_journal_seq;
182
183         struct mutex            blacklist_lock;
184         struct list_head        seq_blacklist;
185         struct journal_seq_blacklist *new_blacklist;
186
187         struct write_point      wp;
188         spinlock_t              err_lock;
189
190         struct delayed_work     reclaim_work;
191         unsigned long           last_flushed;
192
193         /* protects advancing ja->last_idx: */
194         struct mutex            reclaim_lock;
195         unsigned                write_delay_ms;
196         unsigned                reclaim_delay_ms;
197
198         u64                     res_get_blocked_start;
199         u64                     need_write_time;
200         u64                     write_start_time;
201
202         struct time_stats       *write_time;
203         struct time_stats       *delay_time;
204         struct time_stats       *blocked_time;
205         struct time_stats       *flush_seq_time;
206
207 #ifdef CONFIG_DEBUG_LOCK_ALLOC
208         struct lockdep_map      res_map;
209 #endif
210 };
211
212 /*
213  * Embedded in struct bch_dev. First three fields refer to the array of journal
214  * buckets, in bch_sb.
215  */
216 struct journal_device {
217         /*
218          * For each journal bucket, contains the max sequence number of the
219          * journal writes it contains - so we know when a bucket can be reused.
220          */
221         u64                     *bucket_seq;
222
223         unsigned                sectors_free;
224
225         /* Journal bucket we're currently writing to */
226         unsigned                cur_idx;
227
228         /* Last journal bucket that still contains an open journal entry */
229
230         /*
231          * j->lock and j->reclaim_lock must both be held to modify, j->lock
232          * sufficient to read:
233          */
234         unsigned                last_idx;
235         unsigned                nr;
236         u64                     *buckets;
237
238         /* Bio for journal reads/writes to this device */
239         struct bio              *bio;
240
241         /* for bch_journal_read_device */
242         struct closure          read;
243 };
244
245 #endif /* _BCACHEFS_JOURNAL_TYPES_H */