]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/journal.h
Delete more unused shim code, update bcache code
[bcachefs-tools-debian] / libbcache / journal.h
1 #ifndef _BCACHE_JOURNAL_H
2 #define _BCACHE_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  * bch_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 //#include "super-io.h"
115
116 /*
117  * Only used for holding the journal entries we read in btree_journal_read()
118  * during cache_registration
119  */
120 struct journal_replay {
121         struct list_head        list;
122         struct jset             j;
123 };
124
125 #define JOURNAL_PIN     ((32 * 1024) - 1)
126
127 static inline bool journal_pin_active(struct journal_entry_pin *pin)
128 {
129         return pin->pin_list != NULL;
130 }
131
132 void bch_journal_pin_add(struct journal *, struct journal_entry_pin *,
133                          journal_pin_flush_fn);
134 void bch_journal_pin_drop(struct journal *, struct journal_entry_pin *);
135 void bch_journal_pin_add_if_older(struct journal *,
136                                   struct journal_entry_pin *,
137                                   struct journal_entry_pin *,
138                                   journal_pin_flush_fn);
139
140 struct closure;
141 struct cache_set;
142 struct keylist;
143
144 struct bkey_i *bch_journal_find_btree_root(struct cache_set *, struct jset *,
145                                            enum btree_id, unsigned *);
146
147 int bch_journal_seq_should_ignore(struct cache_set *, u64, struct btree *);
148
149 u64 bch_inode_journal_seq(struct journal *, u64);
150
151 static inline int journal_state_count(union journal_res_state s, int idx)
152 {
153         return idx == 0 ? s.buf0_count : s.buf1_count;
154 }
155
156 static inline void journal_state_inc(union journal_res_state *s)
157 {
158         s->buf0_count += s->idx == 0;
159         s->buf1_count += s->idx == 1;
160 }
161
162 static inline void bch_journal_set_has_inode(struct journal_buf *buf, u64 inum)
163 {
164         set_bit(hash_64(inum, ilog2(sizeof(buf->has_inode) * 8)), buf->has_inode);
165 }
166
167 /*
168  * Amount of space that will be taken up by some keys in the journal (i.e.
169  * including the jset header)
170  */
171 static inline unsigned jset_u64s(unsigned u64s)
172 {
173         return u64s + sizeof(struct jset_entry) / sizeof(u64);
174 }
175
176 static inline void bch_journal_add_entry_at(struct journal_buf *buf,
177                                             const void *data, size_t u64s,
178                                             unsigned type, enum btree_id id,
179                                             unsigned level, unsigned offset)
180 {
181         struct jset_entry *entry = vstruct_idx(buf->data, offset);
182
183         entry->u64s = cpu_to_le16(u64s);
184         entry->btree_id = id;
185         entry->level = level;
186         entry->flags = 0;
187         SET_JOURNAL_ENTRY_TYPE(entry, type);
188
189         memcpy_u64s(entry->_data, data, u64s);
190 }
191
192 static inline void bch_journal_add_keys(struct journal *j, struct journal_res *res,
193                                         enum btree_id id, const struct bkey_i *k)
194 {
195         struct journal_buf *buf = &j->buf[res->idx];
196         unsigned actual = jset_u64s(k->k.u64s);
197
198         EBUG_ON(!res->ref);
199         BUG_ON(actual > res->u64s);
200
201         bch_journal_set_has_inode(buf, k->k.p.inode);
202
203         bch_journal_add_entry_at(buf, k, k->k.u64s,
204                                  JOURNAL_ENTRY_BTREE_KEYS, id,
205                                  0, res->offset);
206
207         res->offset     += actual;
208         res->u64s       -= actual;
209 }
210
211 void bch_journal_buf_put_slowpath(struct journal *, bool);
212
213 static inline void bch_journal_buf_put(struct journal *j, unsigned idx,
214                                        bool need_write_just_set)
215 {
216         union journal_res_state s;
217
218         s.v = atomic64_sub_return(((union journal_res_state) {
219                                     .buf0_count = idx == 0,
220                                     .buf1_count = idx == 1,
221                                     }).v, &j->reservations.counter);
222
223         EBUG_ON(s.idx != idx && !s.prev_buf_unwritten);
224
225         /*
226          * Do not initiate a journal write if the journal is in an error state
227          * (previous journal entry write may have failed)
228          */
229         if (s.idx != idx &&
230             !journal_state_count(s, idx) &&
231             s.cur_entry_offset != JOURNAL_ENTRY_ERROR_VAL)
232                 bch_journal_buf_put_slowpath(j, need_write_just_set);
233 }
234
235 /*
236  * This function releases the journal write structure so other threads can
237  * then proceed to add their keys as well.
238  */
239 static inline void bch_journal_res_put(struct journal *j,
240                                        struct journal_res *res)
241 {
242         if (!res->ref)
243                 return;
244
245         lock_release(&j->res_map, 0, _RET_IP_);
246
247         while (res->u64s) {
248                 bch_journal_add_entry_at(&j->buf[res->idx], NULL, 0,
249                                          JOURNAL_ENTRY_BTREE_KEYS,
250                                          0, 0, res->offset);
251                 res->offset     += jset_u64s(0);
252                 res->u64s       -= jset_u64s(0);
253         }
254
255         bch_journal_buf_put(j, res->idx, false);
256
257         res->ref = 0;
258 }
259
260 int bch_journal_res_get_slowpath(struct journal *, struct journal_res *,
261                                  unsigned, unsigned);
262
263 static inline int journal_res_get_fast(struct journal *j,
264                                        struct journal_res *res,
265                                        unsigned u64s_min,
266                                        unsigned u64s_max)
267 {
268         union journal_res_state old, new;
269         u64 v = atomic64_read(&j->reservations.counter);
270
271         do {
272                 old.v = new.v = v;
273
274                 /*
275                  * Check if there is still room in the current journal
276                  * entry:
277                  */
278                 if (old.cur_entry_offset + u64s_min > j->cur_entry_u64s)
279                         return 0;
280
281                 res->offset     = old.cur_entry_offset;
282                 res->u64s       = min(u64s_max, j->cur_entry_u64s -
283                                       old.cur_entry_offset);
284
285                 journal_state_inc(&new);
286                 new.cur_entry_offset += res->u64s;
287         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
288                                        old.v, new.v)) != old.v);
289
290         res->ref = true;
291         res->idx = new.idx;
292         res->seq = le64_to_cpu(j->buf[res->idx].data->seq);
293         return 1;
294 }
295
296 static inline int bch_journal_res_get(struct journal *j, struct journal_res *res,
297                                       unsigned u64s_min, unsigned u64s_max)
298 {
299         int ret;
300
301         EBUG_ON(res->ref);
302         EBUG_ON(u64s_max < u64s_min);
303
304         if (journal_res_get_fast(j, res, u64s_min, u64s_max))
305                 goto out;
306
307         ret = bch_journal_res_get_slowpath(j, res, u64s_min, u64s_max);
308         if (ret)
309                 return ret;
310 out:
311         lock_acquire_shared(&j->res_map, 0, 0, NULL, _THIS_IP_);
312         EBUG_ON(!res->ref);
313         return 0;
314 }
315
316 void bch_journal_wait_on_seq(struct journal *, u64, struct closure *);
317 void bch_journal_flush_seq_async(struct journal *, u64, struct closure *);
318 void bch_journal_flush_async(struct journal *, struct closure *);
319 void bch_journal_meta_async(struct journal *, struct closure *);
320
321 int bch_journal_flush_seq(struct journal *, u64);
322 int bch_journal_flush(struct journal *);
323 int bch_journal_meta(struct journal *);
324
325 void bch_journal_halt(struct journal *);
326
327 static inline int bch_journal_error(struct journal *j)
328 {
329         return j->reservations.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL
330                 ? -EIO : 0;
331 }
332
333 static inline bool is_journal_device(struct cache *ca)
334 {
335         return ca->mi.state == BCH_MEMBER_STATE_ACTIVE && ca->mi.tier == 0;
336 }
337
338 static inline bool journal_flushes_device(struct cache *ca)
339 {
340         return true;
341 }
342
343 void bch_journal_start(struct cache_set *);
344 void bch_journal_mark(struct cache_set *, struct list_head *);
345 void bch_journal_entries_free(struct list_head *);
346 int bch_journal_read(struct cache_set *, struct list_head *);
347 int bch_journal_replay(struct cache_set *, struct list_head *);
348
349 static inline void bch_journal_set_replay_done(struct journal *j)
350 {
351         spin_lock(&j->lock);
352         BUG_ON(!test_bit(JOURNAL_STARTED, &j->flags));
353
354         set_bit(JOURNAL_REPLAY_DONE, &j->flags);
355         j->cur_pin_list = &fifo_peek_back(&j->pin);
356         spin_unlock(&j->lock);
357 }
358
359 void bch_journal_free(struct journal *);
360 int bch_journal_alloc(struct journal *, unsigned);
361
362 ssize_t bch_journal_print_debug(struct journal *, char *);
363
364 int bch_dev_journal_alloc(struct cache *);
365
366 static inline unsigned bch_nr_journal_buckets(struct bch_sb_field_journal *j)
367 {
368         return j
369                 ? (__le64 *) vstruct_end(&j->field) - j->buckets
370                 : 0;
371 }
372
373 int bch_journal_move(struct cache *);
374
375 void bch_journal_free_cache(struct cache *);
376 int bch_journal_init_cache(struct cache *);
377
378 #endif /* _BCACHE_JOURNAL_H */