]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal.h
Update bcachefs sources to a5e71b8200 bcachefs: Allocator startup fixes/refactoring
[bcachefs-tools-debian] / libbcachefs / journal.h
1 #ifndef _BCACHEFS_JOURNAL_H
2 #define _BCACHEFS_JOURNAL_H
3
4 /*
5  * THE JOURNAL:
6  *
7  * The primary purpose of the journal is to log updates (insertions) to the
8  * b-tree, to avoid having to do synchronous updates to the b-tree on disk.
9  *
10  * Without the journal, the b-tree is always internally consistent on
11  * disk - and in fact, in the earliest incarnations bcache didn't have a journal
12  * but did handle unclean shutdowns by doing all index updates synchronously
13  * (with coalescing).
14  *
15  * Updates to interior nodes still happen synchronously and without the journal
16  * (for simplicity) - this may change eventually but updates to interior nodes
17  * are rare enough it's not a huge priority.
18  *
19  * This means the journal is relatively separate from the b-tree; it consists of
20  * just a list of keys and journal replay consists of just redoing those
21  * insertions in same order that they appear in the journal.
22  *
23  * PERSISTENCE:
24  *
25  * For synchronous updates (where we're waiting on the index update to hit
26  * disk), the journal entry will be written out immediately (or as soon as
27  * possible, if the write for the previous journal entry was still in flight).
28  *
29  * Synchronous updates are specified by passing a closure (@flush_cl) to
30  * bch2_btree_insert() or bch_btree_insert_node(), which then pass that parameter
31  * down to the journalling code. That closure will will wait on the journal
32  * write to complete (via closure_wait()).
33  *
34  * If the index update wasn't synchronous, the journal entry will be
35  * written out after 10 ms have elapsed, by default (the delay_ms field
36  * in struct journal).
37  *
38  * JOURNAL ENTRIES:
39  *
40  * A journal entry is variable size (struct jset), it's got a fixed length
41  * header and then a variable number of struct jset_entry entries.
42  *
43  * Journal entries are identified by monotonically increasing 64 bit sequence
44  * numbers - jset->seq; other places in the code refer to this sequence number.
45  *
46  * A jset_entry entry contains one or more bkeys (which is what gets inserted
47  * into the b-tree). We need a container to indicate which b-tree the key is
48  * for; also, the roots of the various b-trees are stored in jset_entry entries
49  * (one for each b-tree) - this lets us add new b-tree types without changing
50  * the on disk format.
51  *
52  * We also keep some things in the journal header that are logically part of the
53  * superblock - all the things that are frequently updated. This is for future
54  * bcache on raw flash support; the superblock (which will become another
55  * journal) can't be moved or wear leveled, so it contains just enough
56  * information to find the main journal, and the superblock only has to be
57  * rewritten when we want to move/wear level the main journal.
58  *
59  * JOURNAL LAYOUT ON DISK:
60  *
61  * The journal is written to a ringbuffer of buckets (which is kept in the
62  * superblock); the individual buckets are not necessarily contiguous on disk
63  * which means that journal entries are not allowed to span buckets, but also
64  * that we can resize the journal at runtime if desired (unimplemented).
65  *
66  * The journal buckets exist in the same pool as all the other buckets that are
67  * managed by the allocator and garbage collection - garbage collection marks
68  * the journal buckets as metadata buckets.
69  *
70  * OPEN/DIRTY JOURNAL ENTRIES:
71  *
72  * Open/dirty journal entries are journal entries that contain b-tree updates
73  * that have not yet been written out to the b-tree on disk. We have to track
74  * which journal entries are dirty, and we also have to avoid wrapping around
75  * the journal and overwriting old but still dirty journal entries with new
76  * journal entries.
77  *
78  * On disk, this is represented with the "last_seq" field of struct jset;
79  * last_seq is the first sequence number that journal replay has to replay.
80  *
81  * To avoid overwriting dirty journal entries on disk, we keep a mapping (in
82  * journal_device->seq) of for each journal bucket, the highest sequence number
83  * any journal entry it contains. Then, by comparing that against last_seq we
84  * can determine whether that journal bucket contains dirty journal entries or
85  * not.
86  *
87  * To track which journal entries are dirty, we maintain a fifo of refcounts
88  * (where each entry corresponds to a specific sequence number) - when a ref
89  * goes to 0, that journal entry is no longer dirty.
90  *
91  * Journalling of index updates is done at the same time as the b-tree itself is
92  * being modified (see btree_insert_key()); when we add the key to the journal
93  * the pending b-tree write takes a ref on the journal entry the key was added
94  * to. If a pending b-tree write would need to take refs on multiple dirty
95  * journal entries, it only keeps the ref on the oldest one (since a newer
96  * journal entry will still be replayed if an older entry was dirty).
97  *
98  * JOURNAL FILLING UP:
99  *
100  * There are two ways the journal could fill up; either we could run out of
101  * space to write to, or we could have too many open journal entries and run out
102  * of room in the fifo of refcounts. Since those refcounts are decremented
103  * without any locking we can't safely resize that fifo, so we handle it the
104  * same way.
105  *
106  * If the journal fills up, we start flushing dirty btree nodes until we can
107  * allocate space for a journal write again - preferentially flushing btree
108  * nodes that are pinning the oldest journal entries first.
109  */
110
111 #include <linux/hash.h>
112
113 #include "journal_types.h"
114
115 struct bch_fs;
116
117 static inline void journal_wake(struct journal *j)
118 {
119         wake_up(&j->wait);
120         closure_wake_up(&j->async_wait);
121 }
122
123 static inline struct journal_buf *journal_cur_buf(struct journal *j)
124 {
125         return j->buf + j->reservations.idx;
126 }
127
128 static inline struct journal_buf *journal_prev_buf(struct journal *j)
129 {
130         return j->buf + !j->reservations.idx;
131 }
132
133 /* Sequence number of oldest dirty journal entry */
134
135 static inline u64 journal_last_seq(struct journal *j)
136 {
137         return j->pin.front;
138 }
139
140 static inline u64 journal_cur_seq(struct journal *j)
141 {
142         BUG_ON(j->pin.back - 1 != atomic64_read(&j->seq));
143
144         return j->pin.back - 1;
145 }
146
147 u64 bch2_inode_journal_seq(struct journal *, u64);
148
149 static inline int journal_state_count(union journal_res_state s, int idx)
150 {
151         return idx == 0 ? s.buf0_count : s.buf1_count;
152 }
153
154 static inline void journal_state_inc(union journal_res_state *s)
155 {
156         s->buf0_count += s->idx == 0;
157         s->buf1_count += s->idx == 1;
158 }
159
160 static inline void bch2_journal_set_has_inode(struct journal *j,
161                                               struct journal_res *res,
162                                               u64 inum)
163 {
164         struct journal_buf *buf = &j->buf[res->idx];
165         unsigned long bit = hash_64(inum, ilog2(sizeof(buf->has_inode) * 8));
166
167         /* avoid atomic op if possible */
168         if (unlikely(!test_bit(bit, buf->has_inode)))
169                 set_bit(bit, buf->has_inode);
170 }
171
172 /*
173  * Amount of space that will be taken up by some keys in the journal (i.e.
174  * including the jset header)
175  */
176 static inline unsigned jset_u64s(unsigned u64s)
177 {
178         return u64s + sizeof(struct jset_entry) / sizeof(u64);
179 }
180
181 static inline int journal_entry_overhead(struct journal *j)
182 {
183         return sizeof(struct jset) / sizeof(u64) + j->entry_u64s_reserved;
184 }
185
186 static inline struct jset_entry *
187 bch2_journal_add_entry_noreservation(struct journal_buf *buf, size_t u64s)
188 {
189         struct jset *jset = buf->data;
190         struct jset_entry *entry = vstruct_idx(jset, le32_to_cpu(jset->u64s));
191
192         memset(entry, 0, sizeof(*entry));
193         entry->u64s = cpu_to_le16(u64s);
194
195         le32_add_cpu(&jset->u64s, jset_u64s(u64s));
196
197         return entry;
198 }
199
200 static inline void bch2_journal_add_entry(struct journal *j, struct journal_res *res,
201                                           unsigned type, enum btree_id id,
202                                           unsigned level,
203                                           const void *data, unsigned u64s)
204 {
205         struct journal_buf *buf = &j->buf[res->idx];
206         struct jset_entry *entry = vstruct_idx(buf->data, res->offset);
207         unsigned actual = jset_u64s(u64s);
208
209         EBUG_ON(!res->ref);
210         EBUG_ON(actual > res->u64s);
211
212         res->offset     += actual;
213         res->u64s       -= actual;
214
215         memset(entry, 0, sizeof(*entry));
216         entry->u64s     = cpu_to_le16(u64s);
217         entry->type     = type;
218         entry->btree_id = id;
219         entry->level    = level;
220         memcpy_u64s(entry->_data, data, u64s);
221 }
222
223 static inline void bch2_journal_add_keys(struct journal *j, struct journal_res *res,
224                                         enum btree_id id, const struct bkey_i *k)
225 {
226         bch2_journal_add_entry(j, res, BCH_JSET_ENTRY_btree_keys,
227                                id, 0, k, k->k.u64s);
228 }
229
230 void __bch2_journal_buf_put(struct journal *, bool);
231
232 static inline void bch2_journal_buf_put(struct journal *j, unsigned idx,
233                                        bool need_write_just_set)
234 {
235         union journal_res_state s;
236
237         s.v = atomic64_sub_return(((union journal_res_state) {
238                                     .buf0_count = idx == 0,
239                                     .buf1_count = idx == 1,
240                                     }).v, &j->reservations.counter);
241         if (!journal_state_count(s, idx)) {
242                 EBUG_ON(s.idx == idx || !s.prev_buf_unwritten);
243                 __bch2_journal_buf_put(j, need_write_just_set);
244         }
245 }
246
247 /*
248  * This function releases the journal write structure so other threads can
249  * then proceed to add their keys as well.
250  */
251 static inline void bch2_journal_res_put(struct journal *j,
252                                        struct journal_res *res)
253 {
254         if (!res->ref)
255                 return;
256
257         lock_release(&j->res_map, 0, _RET_IP_);
258
259         while (res->u64s)
260                 bch2_journal_add_entry(j, res,
261                                        BCH_JSET_ENTRY_btree_keys,
262                                        0, 0, NULL, 0);
263
264         bch2_journal_buf_put(j, res->idx, false);
265
266         res->ref = 0;
267 }
268
269 int bch2_journal_res_get_slowpath(struct journal *, struct journal_res *,
270                                   unsigned);
271
272 #define JOURNAL_RES_GET_NONBLOCK        (1 << 0)
273 #define JOURNAL_RES_GET_CHECK           (1 << 1)
274
275 static inline int journal_res_get_fast(struct journal *j,
276                                        struct journal_res *res,
277                                        unsigned flags)
278 {
279         union journal_res_state old, new;
280         u64 v = atomic64_read(&j->reservations.counter);
281
282         do {
283                 old.v = new.v = v;
284
285                 /*
286                  * Check if there is still room in the current journal
287                  * entry:
288                  */
289                 if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
290                         return 0;
291
292                 EBUG_ON(!journal_state_count(new, new.idx));
293
294                 if (flags & JOURNAL_RES_GET_CHECK)
295                         return 1;
296
297                 new.cur_entry_offset += res->u64s;
298                 journal_state_inc(&new);
299         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
300                                        old.v, new.v)) != old.v);
301
302         res->ref        = true;
303         res->idx        = old.idx;
304         res->offset     = old.cur_entry_offset;
305         res->seq        = le64_to_cpu(j->buf[old.idx].data->seq);
306         return 1;
307 }
308
309 static inline int bch2_journal_res_get(struct journal *j, struct journal_res *res,
310                                        unsigned u64s, unsigned flags)
311 {
312         int ret;
313
314         EBUG_ON(res->ref);
315         EBUG_ON(!test_bit(JOURNAL_STARTED, &j->flags));
316
317         res->u64s = u64s;
318
319         if (journal_res_get_fast(j, res, flags))
320                 goto out;
321
322         ret = bch2_journal_res_get_slowpath(j, res, flags);
323         if (ret)
324                 return ret;
325 out:
326         if (!(flags & JOURNAL_RES_GET_CHECK)) {
327                 lock_acquire_shared(&j->res_map, 0, 0, NULL, _THIS_IP_);
328                 EBUG_ON(!res->ref);
329         }
330         return 0;
331 }
332
333 /* journal_entry_res: */
334
335 void bch2_journal_entry_res_resize(struct journal *,
336                                    struct journal_entry_res *,
337                                    unsigned);
338
339 u64 bch2_journal_last_unwritten_seq(struct journal *);
340 int bch2_journal_open_seq_async(struct journal *, u64, struct closure *);
341
342 void bch2_journal_wait_on_seq(struct journal *, u64, struct closure *);
343 void bch2_journal_flush_seq_async(struct journal *, u64, struct closure *);
344 void bch2_journal_flush_async(struct journal *, struct closure *);
345 void bch2_journal_meta_async(struct journal *, struct closure *);
346
347 int bch2_journal_flush_seq(struct journal *, u64);
348 int bch2_journal_flush(struct journal *);
349 int bch2_journal_meta(struct journal *);
350
351 void bch2_journal_halt(struct journal *);
352
353 static inline int bch2_journal_error(struct journal *j)
354 {
355         return j->reservations.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL
356                 ? -EIO : 0;
357 }
358
359 struct bch_dev;
360
361 static inline bool journal_flushes_device(struct bch_dev *ca)
362 {
363         return true;
364 }
365
366 static inline void bch2_journal_set_replay_done(struct journal *j)
367 {
368         BUG_ON(!test_bit(JOURNAL_STARTED, &j->flags));
369         set_bit(JOURNAL_REPLAY_DONE, &j->flags);
370 }
371
372 void bch2_journal_unblock(struct journal *);
373 void bch2_journal_block(struct journal *);
374
375 ssize_t bch2_journal_print_debug(struct journal *, char *);
376 ssize_t bch2_journal_print_pins(struct journal *, char *);
377
378 int bch2_set_nr_journal_buckets(struct bch_fs *, struct bch_dev *,
379                                 unsigned nr);
380 int bch2_dev_journal_alloc(struct bch_dev *);
381
382 void bch2_dev_journal_stop(struct journal *, struct bch_dev *);
383 void bch2_fs_journal_stop(struct journal *);
384 void bch2_fs_journal_start(struct journal *);
385 void bch2_dev_journal_exit(struct bch_dev *);
386 int bch2_dev_journal_init(struct bch_dev *, struct bch_sb *);
387 void bch2_fs_journal_exit(struct journal *);
388 int bch2_fs_journal_init(struct journal *);
389
390 #endif /* _BCACHEFS_JOURNAL_H */