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