]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_io.c
Update bcachefs sources to 6ddf061e68 bcachefs: Use a genradix for reading journal...
[bcachefs-tools-debian] / libbcachefs / journal_io.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
5 #include "btree_io.h"
6 #include "btree_update_interior.h"
7 #include "buckets.h"
8 #include "checksum.h"
9 #include "disk_groups.h"
10 #include "error.h"
11 #include "io.h"
12 #include "journal.h"
13 #include "journal_io.h"
14 #include "journal_reclaim.h"
15 #include "journal_seq_blacklist.h"
16 #include "replicas.h"
17
18 #include <trace/events/bcachefs.h>
19
20 static inline u32 journal_entry_radix_idx(struct bch_fs *c,
21                                           struct jset *j)
22 {
23         return (le64_to_cpu(j->seq) - c->journal_entries_base_seq) & (~0U >> 1);
24 }
25
26 static void __journal_replay_free(struct bch_fs *c,
27                                   struct journal_replay *i)
28 {
29         struct journal_replay **p =
30                 genradix_ptr(&c->journal_entries, journal_entry_radix_idx(c, &i->j));
31
32         BUG_ON(*p != i);
33         *p = NULL;
34         kvpfree(i, offsetof(struct journal_replay, j) +
35                 vstruct_bytes(&i->j));
36 }
37
38 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
39 {
40         i->ignore = true;
41
42         if (!c->opts.read_entire_journal)
43                 __journal_replay_free(c, i);
44 }
45
46 struct journal_list {
47         struct closure          cl;
48         struct mutex            lock;
49         int                     ret;
50 };
51
52 #define JOURNAL_ENTRY_ADD_OK            0
53 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE  5
54
55 /*
56  * Given a journal entry we just read, add it to the list of journal entries to
57  * be replayed:
58  */
59 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
60                              struct journal_ptr entry_ptr,
61                              struct journal_list *jlist, struct jset *j,
62                              bool bad)
63 {
64         struct genradix_iter iter;
65         struct journal_replay **_i, *i, *dup;
66         struct journal_ptr *ptr;
67         size_t bytes = vstruct_bytes(j);
68         u64 last_seq = 0;
69         int ret = JOURNAL_ENTRY_ADD_OK;
70
71         /*
72          * Xarrays are indexed by a ulong, not a u64, so we can't index them by
73          * sequence number directly:
74          * Assume instead that they will all fall within the range of +-2billion
75          * of the filrst one we find.
76          */
77         if (!c->journal_entries_base_seq)
78                 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
79
80 #if 0
81         list_for_each_entry_reverse(i, jlist->head, list) {
82                 if (!JSET_NO_FLUSH(&i->j)) {
83                         last_seq = le64_to_cpu(i->j.last_seq);
84                         break;
85                 }
86         }
87 #endif
88
89         /* Is this entry older than the range we need? */
90         if (!c->opts.read_entire_journal &&
91             le64_to_cpu(j->seq) < last_seq) {
92                 ret = JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
93                 goto out;
94         }
95
96         /* Drop entries we don't need anymore */
97         if (!JSET_NO_FLUSH(j)) {
98                 genradix_for_each(&c->journal_entries, iter, _i) {
99                         i = *_i;
100
101                         if (!i)
102                                 continue;
103
104                         if (le64_to_cpu(i->j.seq) >= le64_to_cpu(j->last_seq))
105                                 break;
106                         journal_replay_free(c, i);
107                 }
108         }
109
110         _i = genradix_ptr(&c->journal_entries, journal_entry_radix_idx(c, j));
111         dup = _i ? *_i : NULL;
112
113         /*
114          * Duplicate journal entries? If so we want the one that didn't have a
115          * checksum error:
116          */
117         if (dup) {
118                 if (dup->bad) {
119                         /* we'll replace @dup: */
120                 } else if (bad) {
121                         i = dup;
122                         goto found;
123                 } else {
124                         fsck_err_on(bytes != vstruct_bytes(&dup->j) ||
125                                     memcmp(j, &dup->j, bytes), c,
126                                     "found duplicate but non identical journal entries (seq %llu)",
127                                     le64_to_cpu(j->seq));
128                         i = dup;
129                         goto found;
130                 }
131         }
132
133         i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
134         if (!i) {
135                 ret = -ENOMEM;
136                 goto out;
137         }
138
139         i->nr_ptrs       = 0;
140         i->bad          = bad;
141         i->ignore       = false;
142         memcpy(&i->j, j, bytes);
143
144         if (dup) {
145                 i->nr_ptrs = dup->nr_ptrs;
146                 memcpy(i->ptrs, dup->ptrs, sizeof(dup->ptrs));
147                 __journal_replay_free(c, dup);
148         }
149
150         _i = genradix_ptr_alloc(&c->journal_entries,
151                                 journal_entry_radix_idx(c, &i->j),
152                                 GFP_KERNEL);
153         if (!_i) {
154                 bch_err(c, "failed to allocate c->journal_entries entry");
155                 ret = -ENOMEM;
156                 goto out;
157         }
158
159         *_i = i;
160 found:
161         for (ptr = i->ptrs; ptr < i->ptrs + i->nr_ptrs; ptr++) {
162                 if (ptr->dev == ca->dev_idx) {
163                         bch_err(c, "duplicate journal entry %llu on same device",
164                                 le64_to_cpu(i->j.seq));
165                         goto out;
166                 }
167         }
168
169         if (i->nr_ptrs >= ARRAY_SIZE(i->ptrs)) {
170                 bch_err(c, "found too many copies of journal entry %llu",
171                         le64_to_cpu(i->j.seq));
172                 goto out;
173         }
174
175         i->ptrs[i->nr_ptrs++] = entry_ptr;
176 out:
177 fsck_err:
178         return ret;
179 }
180
181 static struct nonce journal_nonce(const struct jset *jset)
182 {
183         return (struct nonce) {{
184                 [0] = 0,
185                 [1] = ((__le32 *) &jset->seq)[0],
186                 [2] = ((__le32 *) &jset->seq)[1],
187                 [3] = BCH_NONCE_JOURNAL,
188         }};
189 }
190
191 /* this fills in a range with empty jset_entries: */
192 static void journal_entry_null_range(void *start, void *end)
193 {
194         struct jset_entry *entry;
195
196         for (entry = start; entry != end; entry = vstruct_next(entry))
197                 memset(entry, 0, sizeof(*entry));
198 }
199
200 #define JOURNAL_ENTRY_REREAD    5
201 #define JOURNAL_ENTRY_NONE      6
202 #define JOURNAL_ENTRY_BAD       7
203
204 #define journal_entry_err(c, msg, ...)                                  \
205 ({                                                                      \
206         switch (write) {                                                \
207         case READ:                                                      \
208                 mustfix_fsck_err(c, msg, ##__VA_ARGS__);                \
209                 break;                                                  \
210         case WRITE:                                                     \
211                 bch_err(c, "corrupt metadata before write:\n"           \
212                         msg, ##__VA_ARGS__);                            \
213                 if (bch2_fs_inconsistent(c)) {                          \
214                         ret = BCH_FSCK_ERRORS_NOT_FIXED;                \
215                         goto fsck_err;                                  \
216                 }                                                       \
217                 break;                                                  \
218         }                                                               \
219         true;                                                           \
220 })
221
222 #define journal_entry_err_on(cond, c, msg, ...)                         \
223         ((cond) ? journal_entry_err(c, msg, ##__VA_ARGS__) : false)
224
225 #define FSCK_DELETED_KEY        5
226
227 static int journal_validate_key(struct bch_fs *c, const char *where,
228                                 struct jset_entry *entry,
229                                 unsigned level, enum btree_id btree_id,
230                                 struct bkey_i *k, const char *type,
231                                 unsigned version, int big_endian, int write)
232 {
233         void *next = vstruct_next(entry);
234         struct printbuf buf = PRINTBUF;
235         int ret = 0;
236
237         if (journal_entry_err_on(!k->k.u64s, c,
238                         "invalid %s in %s entry offset %zi/%u: k->u64s 0",
239                         type, where,
240                         (u64 *) k - entry->_data,
241                         le16_to_cpu(entry->u64s))) {
242                 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
243                 journal_entry_null_range(vstruct_next(entry), next);
244                 return FSCK_DELETED_KEY;
245         }
246
247         if (journal_entry_err_on((void *) bkey_next(k) >
248                                 (void *) vstruct_next(entry), c,
249                         "invalid %s in %s entry offset %zi/%u: extends past end of journal entry",
250                         type, where,
251                         (u64 *) k - entry->_data,
252                         le16_to_cpu(entry->u64s))) {
253                 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
254                 journal_entry_null_range(vstruct_next(entry), next);
255                 return FSCK_DELETED_KEY;
256         }
257
258         if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT, c,
259                         "invalid %s in %s entry offset %zi/%u: bad format %u",
260                         type, where,
261                         (u64 *) k - entry->_data,
262                         le16_to_cpu(entry->u64s),
263                         k->k.format)) {
264                 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
265                 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
266                 journal_entry_null_range(vstruct_next(entry), next);
267                 return FSCK_DELETED_KEY;
268         }
269
270         if (!write)
271                 bch2_bkey_compat(level, btree_id, version, big_endian,
272                                  write, NULL, bkey_to_packed(k));
273
274         if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
275                               __btree_node_type(level, btree_id), write, &buf)) {
276                 printbuf_reset(&buf);
277                 pr_buf(&buf, "invalid %s in %s entry offset %zi/%u:",
278                        type, where,
279                        (u64 *) k - entry->_data,
280                        le16_to_cpu(entry->u64s));
281                 pr_newline(&buf);
282                 pr_indent_push(&buf, 2);
283
284                 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
285                 pr_newline(&buf);
286                 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
287                                   __btree_node_type(level, btree_id), write, &buf);
288
289                 mustfix_fsck_err(c, "%s", buf.buf);
290
291                 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
292                 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
293                 journal_entry_null_range(vstruct_next(entry), next);
294
295                 printbuf_exit(&buf);
296                 return FSCK_DELETED_KEY;
297         }
298
299         if (write)
300                 bch2_bkey_compat(level, btree_id, version, big_endian,
301                                  write, NULL, bkey_to_packed(k));
302 fsck_err:
303         printbuf_exit(&buf);
304         return ret;
305 }
306
307 static int journal_entry_btree_keys_validate(struct bch_fs *c,
308                                              const char *where,
309                                              struct jset_entry *entry,
310                                              unsigned version, int big_endian, int write)
311 {
312         struct bkey_i *k = entry->start;
313
314         while (k != vstruct_last(entry)) {
315                 int ret = journal_validate_key(c, where, entry,
316                                                entry->level,
317                                                entry->btree_id,
318                                                k, "key", version, big_endian, write);
319                 if (ret == FSCK_DELETED_KEY)
320                         continue;
321
322                 k = bkey_next(k);
323         }
324
325         return 0;
326 }
327
328 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
329                                              struct jset_entry *entry)
330 {
331         struct bkey_i *k;
332         bool first = true;
333
334         vstruct_for_each(entry, k) {
335                 if (!first) {
336                         pr_newline(out);
337                         pr_buf(out, "%s: ", bch2_jset_entry_types[entry->type]);
338                 }
339                 pr_buf(out, "btree=%s l=%u ", bch2_btree_ids[entry->btree_id], entry->level);
340                 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
341                 first = false;
342         }
343 }
344
345 static int journal_entry_btree_root_validate(struct bch_fs *c,
346                                              const char *where,
347                                              struct jset_entry *entry,
348                                              unsigned version, int big_endian, int write)
349 {
350         struct bkey_i *k = entry->start;
351         int ret = 0;
352
353         if (journal_entry_err_on(!entry->u64s ||
354                                  le16_to_cpu(entry->u64s) != k->k.u64s, c,
355                                  "invalid btree root journal entry: wrong number of keys")) {
356                 void *next = vstruct_next(entry);
357                 /*
358                  * we don't want to null out this jset_entry,
359                  * just the contents, so that later we can tell
360                  * we were _supposed_ to have a btree root
361                  */
362                 entry->u64s = 0;
363                 journal_entry_null_range(vstruct_next(entry), next);
364                 return 0;
365         }
366
367         return journal_validate_key(c, where, entry, 1, entry->btree_id, k,
368                                     "btree root", version, big_endian, write);
369 fsck_err:
370         return ret;
371 }
372
373 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
374                                              struct jset_entry *entry)
375 {
376         journal_entry_btree_keys_to_text(out, c, entry);
377 }
378
379 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
380                                             const char *where,
381                                             struct jset_entry *entry,
382                                             unsigned version, int big_endian, int write)
383 {
384         /* obsolete, don't care: */
385         return 0;
386 }
387
388 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
389                                             struct jset_entry *entry)
390 {
391 }
392
393 static int journal_entry_blacklist_validate(struct bch_fs *c,
394                                             const char *where,
395                                             struct jset_entry *entry,
396                                             unsigned version, int big_endian, int write)
397 {
398         int ret = 0;
399
400         if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1, c,
401                 "invalid journal seq blacklist entry: bad size")) {
402                 journal_entry_null_range(entry, vstruct_next(entry));
403         }
404 fsck_err:
405         return ret;
406 }
407
408 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
409                                             struct jset_entry *entry)
410 {
411         struct jset_entry_blacklist *bl =
412                 container_of(entry, struct jset_entry_blacklist, entry);
413
414         pr_buf(out, "seq=%llu", le64_to_cpu(bl->seq));
415 }
416
417 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
418                                                const char *where,
419                                                struct jset_entry *entry,
420                                                unsigned version, int big_endian, int write)
421 {
422         struct jset_entry_blacklist_v2 *bl_entry;
423         int ret = 0;
424
425         if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2, c,
426                 "invalid journal seq blacklist entry: bad size")) {
427                 journal_entry_null_range(entry, vstruct_next(entry));
428                 goto out;
429         }
430
431         bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
432
433         if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
434                                  le64_to_cpu(bl_entry->end), c,
435                 "invalid journal seq blacklist entry: start > end")) {
436                 journal_entry_null_range(entry, vstruct_next(entry));
437         }
438 out:
439 fsck_err:
440         return ret;
441 }
442
443 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
444                                                struct jset_entry *entry)
445 {
446         struct jset_entry_blacklist_v2 *bl =
447                 container_of(entry, struct jset_entry_blacklist_v2, entry);
448
449         pr_buf(out, "start=%llu end=%llu",
450                le64_to_cpu(bl->start),
451                le64_to_cpu(bl->end));
452 }
453
454 static int journal_entry_usage_validate(struct bch_fs *c,
455                                         const char *where,
456                                         struct jset_entry *entry,
457                                         unsigned version, int big_endian, int write)
458 {
459         struct jset_entry_usage *u =
460                 container_of(entry, struct jset_entry_usage, entry);
461         unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
462         int ret = 0;
463
464         if (journal_entry_err_on(bytes < sizeof(*u),
465                                  c,
466                                  "invalid journal entry usage: bad size")) {
467                 journal_entry_null_range(entry, vstruct_next(entry));
468                 return ret;
469         }
470
471 fsck_err:
472         return ret;
473 }
474
475 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
476                                         struct jset_entry *entry)
477 {
478         struct jset_entry_usage *u =
479                 container_of(entry, struct jset_entry_usage, entry);
480
481         pr_buf(out, "type=%s v=%llu",
482                bch2_fs_usage_types[u->entry.btree_id],
483                le64_to_cpu(u->v));
484 }
485
486 static int journal_entry_data_usage_validate(struct bch_fs *c,
487                                         const char *where,
488                                         struct jset_entry *entry,
489                                         unsigned version, int big_endian, int write)
490 {
491         struct jset_entry_data_usage *u =
492                 container_of(entry, struct jset_entry_data_usage, entry);
493         unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
494         int ret = 0;
495
496         if (journal_entry_err_on(bytes < sizeof(*u) ||
497                                  bytes < sizeof(*u) + u->r.nr_devs,
498                                  c,
499                                  "invalid journal entry usage: bad size")) {
500                 journal_entry_null_range(entry, vstruct_next(entry));
501                 return ret;
502         }
503
504 fsck_err:
505         return ret;
506 }
507
508 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
509                                              struct jset_entry *entry)
510 {
511         struct jset_entry_data_usage *u =
512                 container_of(entry, struct jset_entry_data_usage, entry);
513
514         bch2_replicas_entry_to_text(out, &u->r);
515         pr_buf(out, "=%llu", le64_to_cpu(u->v));
516 }
517
518 static int journal_entry_clock_validate(struct bch_fs *c,
519                                         const char *where,
520                                         struct jset_entry *entry,
521                                         unsigned version, int big_endian, int write)
522 {
523         struct jset_entry_clock *clock =
524                 container_of(entry, struct jset_entry_clock, entry);
525         unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
526         int ret = 0;
527
528         if (journal_entry_err_on(bytes != sizeof(*clock),
529                                  c, "invalid journal entry clock: bad size")) {
530                 journal_entry_null_range(entry, vstruct_next(entry));
531                 return ret;
532         }
533
534         if (journal_entry_err_on(clock->rw > 1,
535                                  c, "invalid journal entry clock: bad rw")) {
536                 journal_entry_null_range(entry, vstruct_next(entry));
537                 return ret;
538         }
539
540 fsck_err:
541         return ret;
542 }
543
544 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
545                                         struct jset_entry *entry)
546 {
547         struct jset_entry_clock *clock =
548                 container_of(entry, struct jset_entry_clock, entry);
549
550         pr_buf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
551 }
552
553 static int journal_entry_dev_usage_validate(struct bch_fs *c,
554                                             const char *where,
555                                             struct jset_entry *entry,
556                                             unsigned version, int big_endian, int write)
557 {
558         struct jset_entry_dev_usage *u =
559                 container_of(entry, struct jset_entry_dev_usage, entry);
560         unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
561         unsigned expected = sizeof(*u);
562         unsigned dev;
563         int ret = 0;
564
565         if (journal_entry_err_on(bytes < expected,
566                                  c, "invalid journal entry dev usage: bad size (%u < %u)",
567                                  bytes, expected)) {
568                 journal_entry_null_range(entry, vstruct_next(entry));
569                 return ret;
570         }
571
572         dev = le32_to_cpu(u->dev);
573
574         if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
575                                  c, "invalid journal entry dev usage: bad dev")) {
576                 journal_entry_null_range(entry, vstruct_next(entry));
577                 return ret;
578         }
579
580         if (journal_entry_err_on(u->pad,
581                                  c, "invalid journal entry dev usage: bad pad")) {
582                 journal_entry_null_range(entry, vstruct_next(entry));
583                 return ret;
584         }
585
586 fsck_err:
587         return ret;
588 }
589
590 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
591                                             struct jset_entry *entry)
592 {
593         struct jset_entry_dev_usage *u =
594                 container_of(entry, struct jset_entry_dev_usage, entry);
595         unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
596
597         pr_buf(out, "dev=%u", le32_to_cpu(u->dev));
598
599         for (i = 0; i < nr_types; i++) {
600                 if (i < BCH_DATA_NR)
601                         pr_buf(out, " %s", bch2_data_types[i]);
602                 else
603                         pr_buf(out, " (unknown data type %u)", i);
604                 pr_buf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
605                        le64_to_cpu(u->d[i].buckets),
606                        le64_to_cpu(u->d[i].sectors),
607                        le64_to_cpu(u->d[i].fragmented));
608         }
609
610         pr_buf(out, " buckets_ec: %llu", le64_to_cpu(u->buckets_ec));
611 }
612
613 static int journal_entry_log_validate(struct bch_fs *c,
614                                       const char *where,
615                                       struct jset_entry *entry,
616                                       unsigned version, int big_endian, int write)
617 {
618         return 0;
619 }
620
621 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
622                                       struct jset_entry *entry)
623 {
624         struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
625         unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
626
627         pr_buf(out, "%.*s", bytes, l->d);
628 }
629
630 struct jset_entry_ops {
631         int (*validate)(struct bch_fs *, const char *,
632                         struct jset_entry *, unsigned, int, int);
633         void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
634 };
635
636 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
637 #define x(f, nr)                                                \
638         [BCH_JSET_ENTRY_##f]    = (struct jset_entry_ops) {     \
639                 .validate       = journal_entry_##f##_validate, \
640                 .to_text        = journal_entry_##f##_to_text,  \
641         },
642         BCH_JSET_ENTRY_TYPES()
643 #undef x
644 };
645
646 int bch2_journal_entry_validate(struct bch_fs *c, const char *where,
647                                 struct jset_entry *entry,
648                                 unsigned version, int big_endian, int write)
649 {
650         return entry->type < BCH_JSET_ENTRY_NR
651                 ? bch2_jset_entry_ops[entry->type].validate(c, where, entry,
652                                 version, big_endian, write)
653                 : 0;
654 }
655
656 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
657                                 struct jset_entry *entry)
658 {
659         if (entry->type < BCH_JSET_ENTRY_NR) {
660                 pr_buf(out, "%s: ", bch2_jset_entry_types[entry->type]);
661                 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
662         } else {
663                 pr_buf(out, "(unknown type %u)", entry->type);
664         }
665 }
666
667 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
668                                  int write)
669 {
670         char buf[100];
671         struct jset_entry *entry;
672         int ret = 0;
673
674         vstruct_for_each(jset, entry) {
675                 scnprintf(buf, sizeof(buf), "jset %llu entry offset %zi/%u",
676                           le64_to_cpu(jset->seq),
677                           (u64 *) entry - jset->_data,
678                           le32_to_cpu(jset->u64s));
679
680                 if (journal_entry_err_on(vstruct_next(entry) >
681                                          vstruct_last(jset), c,
682                                 "journal entry extends past end of jset")) {
683                         jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
684                         break;
685                 }
686
687                 ret = bch2_journal_entry_validate(c, buf, entry,
688                                         le32_to_cpu(jset->version),
689                                         JSET_BIG_ENDIAN(jset), write);
690                 if (ret)
691                         break;
692         }
693 fsck_err:
694         return ret;
695 }
696
697 static int jset_validate(struct bch_fs *c,
698                          struct bch_dev *ca,
699                          struct jset *jset, u64 sector,
700                          unsigned bucket_sectors_left,
701                          unsigned sectors_read,
702                          int write)
703 {
704         size_t bytes = vstruct_bytes(jset);
705         struct bch_csum csum;
706         unsigned version;
707         int ret = 0;
708
709         if (le64_to_cpu(jset->magic) != jset_magic(c))
710                 return JOURNAL_ENTRY_NONE;
711
712         version = le32_to_cpu(jset->version);
713         if (journal_entry_err_on((version != BCH_JSET_VERSION_OLD &&
714                                   version < bcachefs_metadata_version_min) ||
715                                  version >= bcachefs_metadata_version_max, c,
716                         "%s sector %llu seq %llu: unknown journal entry version %u",
717                         ca ? ca->name : c->name,
718                         sector, le64_to_cpu(jset->seq),
719                         version)) {
720                 /* don't try to continue: */
721                 return EINVAL;
722         }
723
724         if (bytes > (sectors_read << 9) &&
725             sectors_read < bucket_sectors_left)
726                 return JOURNAL_ENTRY_REREAD;
727
728         if (journal_entry_err_on(bytes > bucket_sectors_left << 9, c,
729                         "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
730                         ca ? ca->name : c->name,
731                         sector, le64_to_cpu(jset->seq), bytes)) {
732                 ret = JOURNAL_ENTRY_BAD;
733                 le32_add_cpu(&jset->u64s,
734                              -((bytes - (bucket_sectors_left << 9)) / 8));
735         }
736
737         if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)), c,
738                         "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
739                         ca ? ca->name : c->name,
740                         sector, le64_to_cpu(jset->seq),
741                         JSET_CSUM_TYPE(jset))) {
742                 ret = JOURNAL_ENTRY_BAD;
743                 goto csum_done;
744         }
745
746         if (write)
747                 goto csum_done;
748
749         csum = csum_vstruct(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), jset);
750         if (journal_entry_err_on(bch2_crc_cmp(csum, jset->csum), c,
751                                  "%s sector %llu seq %llu: journal checksum bad",
752                                  ca ? ca->name : c->name,
753                                  sector, le64_to_cpu(jset->seq)))
754                 ret = JOURNAL_ENTRY_BAD;
755
756         ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
757                      jset->encrypted_start,
758                      vstruct_end(jset) - (void *) jset->encrypted_start);
759         bch2_fs_fatal_err_on(ret, c,
760                         "error decrypting journal entry: %i", ret);
761 csum_done:
762         /* last_seq is ignored when JSET_NO_FLUSH is true */
763         if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
764                                  le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq), c,
765                                  "invalid journal entry: last_seq > seq (%llu > %llu)",
766                                  le64_to_cpu(jset->last_seq),
767                                  le64_to_cpu(jset->seq))) {
768                 jset->last_seq = jset->seq;
769                 return JOURNAL_ENTRY_BAD;
770         }
771 fsck_err:
772         return ret;
773 }
774
775 static int jset_validate_for_write(struct bch_fs *c, struct jset *jset)
776 {
777         unsigned sectors = vstruct_sectors(jset, c->block_bits);
778
779         return jset_validate(c, NULL, jset, 0, sectors, sectors, WRITE) ?:
780                 jset_validate_entries(c, jset, WRITE);
781 }
782
783 struct journal_read_buf {
784         void            *data;
785         size_t          size;
786 };
787
788 static int journal_read_buf_realloc(struct journal_read_buf *b,
789                                     size_t new_size)
790 {
791         void *n;
792
793         /* the bios are sized for this many pages, max: */
794         if (new_size > JOURNAL_ENTRY_SIZE_MAX)
795                 return -ENOMEM;
796
797         new_size = roundup_pow_of_two(new_size);
798         n = kvpmalloc(new_size, GFP_KERNEL);
799         if (!n)
800                 return -ENOMEM;
801
802         kvpfree(b->data, b->size);
803         b->data = n;
804         b->size = new_size;
805         return 0;
806 }
807
808 static int journal_read_bucket(struct bch_dev *ca,
809                                struct journal_read_buf *buf,
810                                struct journal_list *jlist,
811                                unsigned bucket)
812 {
813         struct bch_fs *c = ca->fs;
814         struct journal_device *ja = &ca->journal;
815         struct jset *j = NULL;
816         unsigned sectors, sectors_read = 0;
817         u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
818             end = offset + ca->mi.bucket_size;
819         bool saw_bad = false;
820         int ret = 0;
821
822         pr_debug("reading %u", bucket);
823
824         while (offset < end) {
825                 if (!sectors_read) {
826                         struct bio *bio;
827 reread:
828                         sectors_read = min_t(unsigned,
829                                 end - offset, buf->size >> 9);
830
831                         bio = bio_kmalloc(GFP_KERNEL,
832                                           buf_pages(buf->data,
833                                                     sectors_read << 9));
834                         bio_set_dev(bio, ca->disk_sb.bdev);
835                         bio->bi_iter.bi_sector  = offset;
836                         bio_set_op_attrs(bio, REQ_OP_READ, 0);
837                         bch2_bio_map(bio, buf->data, sectors_read << 9);
838
839                         ret = submit_bio_wait(bio);
840                         bio_put(bio);
841
842                         if (bch2_dev_io_err_on(ret, ca,
843                                                "journal read error: sector %llu",
844                                                offset) ||
845                             bch2_meta_read_fault("journal")) {
846                                 /*
847                                  * We don't error out of the recovery process
848                                  * here, since the relevant journal entry may be
849                                  * found on a different device, and missing or
850                                  * no journal entries will be handled later
851                                  */
852                                 return 0;
853                         }
854
855                         j = buf->data;
856                 }
857
858                 ret = jset_validate(c, ca, j, offset,
859                                     end - offset, sectors_read,
860                                     READ);
861                 switch (ret) {
862                 case BCH_FSCK_OK:
863                         sectors = vstruct_sectors(j, c->block_bits);
864                         break;
865                 case JOURNAL_ENTRY_REREAD:
866                         if (vstruct_bytes(j) > buf->size) {
867                                 ret = journal_read_buf_realloc(buf,
868                                                         vstruct_bytes(j));
869                                 if (ret)
870                                         return ret;
871                         }
872                         goto reread;
873                 case JOURNAL_ENTRY_NONE:
874                         if (!saw_bad)
875                                 return 0;
876                         sectors = block_sectors(c);
877                         goto next_block;
878                 case JOURNAL_ENTRY_BAD:
879                         saw_bad = true;
880                         /*
881                          * On checksum error we don't really trust the size
882                          * field of the journal entry we read, so try reading
883                          * again at next block boundary:
884                          */
885                         sectors = block_sectors(c);
886                         break;
887                 default:
888                         return ret;
889                 }
890
891                 /*
892                  * This happens sometimes if we don't have discards on -
893                  * when we've partially overwritten a bucket with new
894                  * journal entries. We don't need the rest of the
895                  * bucket:
896                  */
897                 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
898                         return 0;
899
900                 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
901
902                 mutex_lock(&jlist->lock);
903                 ret = journal_entry_add(c, ca, (struct journal_ptr) {
904                                         .dev            = ca->dev_idx,
905                                         .bucket         = bucket,
906                                         .bucket_offset  = offset -
907                                                 bucket_to_sector(ca, ja->buckets[bucket]),
908                                         .sector         = offset,
909                                         }, jlist, j, ret != 0);
910                 mutex_unlock(&jlist->lock);
911
912                 switch (ret) {
913                 case JOURNAL_ENTRY_ADD_OK:
914                         break;
915                 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
916                         break;
917                 default:
918                         return ret;
919                 }
920 next_block:
921                 pr_debug("next");
922                 offset          += sectors;
923                 sectors_read    -= sectors;
924                 j = ((void *) j) + (sectors << 9);
925         }
926
927         return 0;
928 }
929
930 static void bch2_journal_read_device(struct closure *cl)
931 {
932         struct journal_device *ja =
933                 container_of(cl, struct journal_device, read);
934         struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
935         struct bch_fs *c = ca->fs;
936         struct journal_list *jlist =
937                 container_of(cl->parent, struct journal_list, cl);
938         struct journal_replay *r, **_r;
939         struct genradix_iter iter;
940         struct journal_read_buf buf = { NULL, 0 };
941         u64 min_seq = U64_MAX;
942         unsigned i;
943         int ret = 0;
944
945         if (!ja->nr)
946                 goto out;
947
948         ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
949         if (ret)
950                 goto err;
951
952         pr_debug("%u journal buckets", ja->nr);
953
954         for (i = 0; i < ja->nr; i++) {
955                 ret = journal_read_bucket(ca, &buf, jlist, i);
956                 if (ret)
957                         goto err;
958         }
959
960         /* Find the journal bucket with the highest sequence number: */
961         for (i = 0; i < ja->nr; i++) {
962                 if (ja->bucket_seq[i] > ja->bucket_seq[ja->cur_idx])
963                         ja->cur_idx = i;
964
965                 min_seq = min(ja->bucket_seq[i], min_seq);
966         }
967
968         /*
969          * If there's duplicate journal entries in multiple buckets (which
970          * definitely isn't supposed to happen, but...) - make sure to start
971          * cur_idx at the last of those buckets, so we don't deadlock trying to
972          * allocate
973          */
974         while (ja->bucket_seq[ja->cur_idx] > min_seq &&
975                ja->bucket_seq[ja->cur_idx] ==
976                ja->bucket_seq[(ja->cur_idx + 1) % ja->nr])
977                 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
978
979         ja->sectors_free = ca->mi.bucket_size;
980
981         mutex_lock(&jlist->lock);
982         genradix_for_each(&c->journal_entries, iter, _r) {
983                 r = *_r;
984
985                 if (!r)
986                         continue;
987
988                 for (i = 0; i < r->nr_ptrs; i++) {
989                         if (r->ptrs[i].dev == ca->dev_idx &&
990                             sector_to_bucket(ca, r->ptrs[i].sector) == ja->buckets[ja->cur_idx]) {
991                                 unsigned wrote = (r->ptrs[i].sector % ca->mi.bucket_size) +
992                                         vstruct_sectors(&r->j, c->block_bits);
993
994                                 ja->sectors_free = min(ja->sectors_free,
995                                                        ca->mi.bucket_size - wrote);
996                         }
997                 }
998         }
999         mutex_unlock(&jlist->lock);
1000
1001         if (ja->bucket_seq[ja->cur_idx] &&
1002             ja->sectors_free == ca->mi.bucket_size) {
1003                 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1004                 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1005                 for (i = 0; i < 3; i++) {
1006                         unsigned idx = ja->cur_idx - 1 + i;
1007                         bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1008                 }
1009                 ja->sectors_free = 0;
1010         }
1011
1012         /*
1013          * Set dirty_idx to indicate the entire journal is full and needs to be
1014          * reclaimed - journal reclaim will immediately reclaim whatever isn't
1015          * pinned when it first runs:
1016          */
1017         ja->discard_idx = ja->dirty_idx_ondisk =
1018                 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1019 out:
1020         bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1021         kvpfree(buf.data, buf.size);
1022         percpu_ref_put(&ca->io_ref);
1023         closure_return(cl);
1024         return;
1025 err:
1026         mutex_lock(&jlist->lock);
1027         jlist->ret = ret;
1028         mutex_unlock(&jlist->lock);
1029         goto out;
1030 }
1031
1032 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1033                                struct journal_replay *j)
1034 {
1035         unsigned i;
1036
1037         for (i = 0; i < j->nr_ptrs; i++) {
1038                 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1039                 u64 offset;
1040
1041                 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1042
1043                 if (i)
1044                         pr_buf(out, " ");
1045                 pr_buf(out, "%u:%u:%u (sector %llu)",
1046                        j->ptrs[i].dev,
1047                        j->ptrs[i].bucket,
1048                        j->ptrs[i].bucket_offset,
1049                        j->ptrs[i].sector);
1050         }
1051 }
1052
1053 int bch2_journal_read(struct bch_fs *c, u64 *blacklist_seq, u64 *start_seq)
1054 {
1055         struct journal_list jlist;
1056         struct journal_replay *i, **_i, *prev = NULL;
1057         struct genradix_iter radix_iter;
1058         struct bch_dev *ca;
1059         unsigned iter;
1060         struct printbuf buf = PRINTBUF;
1061         size_t keys = 0, entries = 0;
1062         bool degraded = false;
1063         u64 seq, last_seq = 0;
1064         int ret = 0;
1065
1066         closure_init_stack(&jlist.cl);
1067         mutex_init(&jlist.lock);
1068         jlist.ret = 0;
1069
1070         for_each_member_device(ca, c, iter) {
1071                 if (!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
1072                     !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1073                         continue;
1074
1075                 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1076                      ca->mi.state == BCH_MEMBER_STATE_ro) &&
1077                     percpu_ref_tryget(&ca->io_ref))
1078                         closure_call(&ca->journal.read,
1079                                      bch2_journal_read_device,
1080                                      system_unbound_wq,
1081                                      &jlist.cl);
1082                 else
1083                         degraded = true;
1084         }
1085
1086         closure_sync(&jlist.cl);
1087
1088         if (jlist.ret)
1089                 return jlist.ret;
1090
1091         *start_seq = 0;
1092
1093         /*
1094          * Find most recent flush entry, and ignore newer non flush entries -
1095          * those entries will be blacklisted:
1096          */
1097         genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1098                 i = *_i;
1099
1100                 if (!i || i->ignore)
1101                         continue;
1102
1103                 if (!*start_seq)
1104                         *start_seq = le64_to_cpu(i->j.seq) + 1;
1105
1106                 if (!JSET_NO_FLUSH(&i->j)) {
1107                         last_seq        = le64_to_cpu(i->j.last_seq);
1108                         *blacklist_seq  = le64_to_cpu(i->j.seq) + 1;
1109                         break;
1110                 }
1111
1112                 journal_replay_free(c, i);
1113         }
1114
1115         if (!*start_seq) {
1116                 bch_info(c, "journal read done, but no entries found");
1117                 return 0;
1118         }
1119
1120         if (!last_seq) {
1121                 fsck_err(c, "journal read done, but no entries found after dropping non-flushes");
1122                 ret = -1;
1123                 goto err;
1124         }
1125
1126         /* Drop blacklisted entries and entries older than last_seq: */
1127         genradix_for_each(&c->journal_entries, radix_iter, _i) {
1128                 i = *_i;
1129
1130                 if (!i || i->ignore)
1131                         continue;
1132
1133                 seq = le64_to_cpu(i->j.seq);
1134                 if (seq < last_seq) {
1135                         journal_replay_free(c, i);
1136                         continue;
1137                 }
1138
1139                 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1140                         fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1141                                     "found blacklisted journal entry %llu", seq);
1142
1143                         journal_replay_free(c, i);
1144                 }
1145         }
1146
1147         /* Check for missing entries: */
1148         seq = last_seq;
1149         genradix_for_each(&c->journal_entries, radix_iter, _i) {
1150                 i = *_i;
1151
1152                 if (!i || i->ignore)
1153                         continue;
1154
1155                 BUG_ON(seq > le64_to_cpu(i->j.seq));
1156
1157                 while (seq < le64_to_cpu(i->j.seq)) {
1158                         u64 missing_start, missing_end;
1159                         struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1160
1161                         while (seq < le64_to_cpu(i->j.seq) &&
1162                                bch2_journal_seq_is_blacklisted(c, seq, false))
1163                                 seq++;
1164
1165                         if (seq == le64_to_cpu(i->j.seq))
1166                                 break;
1167
1168                         missing_start = seq;
1169
1170                         while (seq < le64_to_cpu(i->j.seq) &&
1171                                !bch2_journal_seq_is_blacklisted(c, seq, false))
1172                                 seq++;
1173
1174                         if (prev) {
1175                                 bch2_journal_ptrs_to_text(&buf1, c, prev);
1176                                 pr_buf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1177                         } else
1178                                 pr_buf(&buf1, "(none)");
1179                         bch2_journal_ptrs_to_text(&buf2, c, i);
1180
1181                         missing_end = seq - 1;
1182                         fsck_err(c, "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1183                                  "  prev at %s\n"
1184                                  "  next at %s",
1185                                  missing_start, missing_end,
1186                                  last_seq, *blacklist_seq - 1,
1187                                  buf1.buf, buf2.buf);
1188
1189                         printbuf_exit(&buf1);
1190                         printbuf_exit(&buf2);
1191                 }
1192
1193                 prev = i;
1194                 seq++;
1195         }
1196
1197         genradix_for_each(&c->journal_entries, radix_iter, _i) {
1198                 struct jset_entry *entry;
1199                 struct bkey_i *k, *_n;
1200                 struct bch_replicas_padded replicas = {
1201                         .e.data_type = BCH_DATA_journal,
1202                         .e.nr_required = 1,
1203                 };
1204                 unsigned ptr;
1205
1206                 i = *_i;
1207                 if (!i || i->ignore)
1208                         continue;
1209
1210                 ret = jset_validate_entries(c, &i->j, READ);
1211                 if (ret)
1212                         goto err;
1213
1214                 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1215                         replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1216
1217                 bch2_replicas_entry_sort(&replicas.e);
1218
1219                 /*
1220                  * If we're mounting in degraded mode - if we didn't read all
1221                  * the devices - this is wrong:
1222                  */
1223
1224                 printbuf_reset(&buf);
1225                 bch2_replicas_entry_to_text(&buf, &replicas.e);
1226
1227                 if (!degraded &&
1228                     (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
1229                      fsck_err_on(!bch2_replicas_marked(c, &replicas.e), c,
1230                                  "superblock not marked as containing replicas %s",
1231                                  buf.buf))) {
1232                         ret = bch2_mark_replicas(c, &replicas.e);
1233                         if (ret)
1234                                 goto err;
1235                 }
1236
1237                 for_each_jset_key(k, _n, entry, &i->j)
1238                         keys++;
1239                 entries++;
1240         }
1241
1242         bch_info(c, "journal read done, %zu keys in %zu entries, seq %llu",
1243                  keys, entries, *start_seq);
1244
1245         if (*start_seq != *blacklist_seq)
1246                 bch_info(c, "dropped unflushed entries %llu-%llu",
1247                          *blacklist_seq, *start_seq - 1);
1248 err:
1249 fsck_err:
1250         printbuf_exit(&buf);
1251         return ret;
1252 }
1253
1254 /* journal write: */
1255
1256 static void __journal_write_alloc(struct journal *j,
1257                                   struct journal_buf *w,
1258                                   struct dev_alloc_list *devs_sorted,
1259                                   unsigned sectors,
1260                                   unsigned *replicas,
1261                                   unsigned replicas_want)
1262 {
1263         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1264         struct journal_device *ja;
1265         struct bch_dev *ca;
1266         unsigned i;
1267
1268         if (*replicas >= replicas_want)
1269                 return;
1270
1271         for (i = 0; i < devs_sorted->nr; i++) {
1272                 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1273                 if (!ca)
1274                         continue;
1275
1276                 ja = &ca->journal;
1277
1278                 /*
1279                  * Check that we can use this device, and aren't already using
1280                  * it:
1281                  */
1282                 if (!ca->mi.durability ||
1283                     ca->mi.state != BCH_MEMBER_STATE_rw ||
1284                     !ja->nr ||
1285                     bch2_bkey_has_device(bkey_i_to_s_c(&w->key),
1286                                          ca->dev_idx) ||
1287                     sectors > ja->sectors_free)
1288                         continue;
1289
1290                 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1291
1292                 bch2_bkey_append_ptr(&w->key,
1293                         (struct bch_extent_ptr) {
1294                                   .offset = bucket_to_sector(ca,
1295                                         ja->buckets[ja->cur_idx]) +
1296                                         ca->mi.bucket_size -
1297                                         ja->sectors_free,
1298                                   .dev = ca->dev_idx,
1299                 });
1300
1301                 ja->sectors_free -= sectors;
1302                 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1303
1304                 *replicas += ca->mi.durability;
1305
1306                 if (*replicas >= replicas_want)
1307                         break;
1308         }
1309 }
1310
1311 /**
1312  * journal_next_bucket - move on to the next journal bucket if possible
1313  */
1314 static int journal_write_alloc(struct journal *j, struct journal_buf *w,
1315                                unsigned sectors)
1316 {
1317         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1318         struct bch_devs_mask devs;
1319         struct journal_device *ja;
1320         struct bch_dev *ca;
1321         struct dev_alloc_list devs_sorted;
1322         unsigned target = c->opts.metadata_target ?:
1323                 c->opts.foreground_target;
1324         unsigned i, replicas = 0, replicas_want =
1325                 READ_ONCE(c->opts.metadata_replicas);
1326
1327         rcu_read_lock();
1328 retry:
1329         devs = target_rw_devs(c, BCH_DATA_journal, target);
1330
1331         devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1332
1333         __journal_write_alloc(j, w, &devs_sorted,
1334                               sectors, &replicas, replicas_want);
1335
1336         if (replicas >= replicas_want)
1337                 goto done;
1338
1339         for (i = 0; i < devs_sorted.nr; i++) {
1340                 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1341                 if (!ca)
1342                         continue;
1343
1344                 ja = &ca->journal;
1345
1346                 if (sectors > ja->sectors_free &&
1347                     sectors <= ca->mi.bucket_size &&
1348                     bch2_journal_dev_buckets_available(j, ja,
1349                                         journal_space_discarded)) {
1350                         ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1351                         ja->sectors_free = ca->mi.bucket_size;
1352
1353                         /*
1354                          * ja->bucket_seq[ja->cur_idx] must always have
1355                          * something sensible:
1356                          */
1357                         ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1358                 }
1359         }
1360
1361         __journal_write_alloc(j, w, &devs_sorted,
1362                               sectors, &replicas, replicas_want);
1363
1364         if (replicas < replicas_want && target) {
1365                 /* Retry from all devices: */
1366                 target = 0;
1367                 goto retry;
1368         }
1369 done:
1370         rcu_read_unlock();
1371
1372         BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1373
1374         return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1375 }
1376
1377 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1378 {
1379         /* we aren't holding j->lock: */
1380         unsigned new_size = READ_ONCE(j->buf_size_want);
1381         void *new_buf;
1382
1383         if (buf->buf_size >= new_size)
1384                 return;
1385
1386         new_buf = kvpmalloc(new_size, GFP_NOIO|__GFP_NOWARN);
1387         if (!new_buf)
1388                 return;
1389
1390         memcpy(new_buf, buf->data, buf->buf_size);
1391
1392         spin_lock(&j->lock);
1393         swap(buf->data,         new_buf);
1394         swap(buf->buf_size,     new_size);
1395         spin_unlock(&j->lock);
1396
1397         kvpfree(new_buf, new_size);
1398 }
1399
1400 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1401 {
1402         return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1403 }
1404
1405 static void journal_write_done(struct closure *cl)
1406 {
1407         struct journal *j = container_of(cl, struct journal, io);
1408         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1409         struct journal_buf *w = journal_last_unwritten_buf(j);
1410         struct bch_replicas_padded replicas;
1411         union journal_res_state old, new;
1412         u64 v, seq;
1413         int err = 0;
1414
1415         bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1416                                ? j->flush_write_time
1417                                : j->noflush_write_time, j->write_start_time);
1418
1419         if (!w->devs_written.nr) {
1420                 bch_err(c, "unable to write journal to sufficient devices");
1421                 err = -EIO;
1422         } else {
1423                 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1424                                          w->devs_written);
1425                 if (bch2_mark_replicas(c, &replicas.e))
1426                         err = -EIO;
1427         }
1428
1429         if (err)
1430                 bch2_fatal_error(c);
1431
1432         spin_lock(&j->lock);
1433         seq = le64_to_cpu(w->data->seq);
1434
1435         if (seq >= j->pin.front)
1436                 journal_seq_pin(j, seq)->devs = w->devs_written;
1437
1438         if (!err) {
1439                 if (!JSET_NO_FLUSH(w->data)) {
1440                         j->flushed_seq_ondisk = seq;
1441                         j->last_seq_ondisk = w->last_seq;
1442
1443                         bch2_do_discards(c);
1444                         closure_wake_up(&c->freelist_wait);
1445                 }
1446         } else if (!j->err_seq || seq < j->err_seq)
1447                 j->err_seq      = seq;
1448
1449         j->seq_ondisk           = seq;
1450
1451         /*
1452          * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1453          * more buckets:
1454          *
1455          * Must come before signaling write completion, for
1456          * bch2_fs_journal_stop():
1457          */
1458         journal_reclaim_kick(&c->journal);
1459
1460         /* also must come before signalling write completion: */
1461         closure_debug_destroy(cl);
1462
1463         v = atomic64_read(&j->reservations.counter);
1464         do {
1465                 old.v = new.v = v;
1466                 BUG_ON(journal_state_count(new, new.unwritten_idx));
1467
1468                 new.unwritten_idx++;
1469         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1470                                        old.v, new.v)) != old.v);
1471
1472         bch2_journal_space_available(j);
1473
1474         closure_wake_up(&w->wait);
1475         journal_wake(j);
1476
1477         if (!journal_state_count(new, new.unwritten_idx) &&
1478             journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1479                 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1480         } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1481                    new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1482                 struct journal_buf *buf = journal_cur_buf(j);
1483                 long delta = buf->expires - jiffies;
1484
1485                 /*
1486                  * We don't close a journal entry to write it while there's
1487                  * previous entries still in flight - the current journal entry
1488                  * might want to be written now:
1489                  */
1490
1491                 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1492         }
1493
1494         spin_unlock(&j->lock);
1495 }
1496
1497 static void journal_write_endio(struct bio *bio)
1498 {
1499         struct bch_dev *ca = bio->bi_private;
1500         struct journal *j = &ca->fs->journal;
1501         struct journal_buf *w = journal_last_unwritten_buf(j);
1502         unsigned long flags;
1503
1504         if (bch2_dev_io_err_on(bio->bi_status, ca, "error writing journal entry %llu: %s",
1505                                le64_to_cpu(w->data->seq),
1506                                bch2_blk_status_to_str(bio->bi_status)) ||
1507             bch2_meta_write_fault("journal")) {
1508                 spin_lock_irqsave(&j->err_lock, flags);
1509                 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1510                 spin_unlock_irqrestore(&j->err_lock, flags);
1511         }
1512
1513         closure_put(&j->io);
1514         percpu_ref_put(&ca->io_ref);
1515 }
1516
1517 static void do_journal_write(struct closure *cl)
1518 {
1519         struct journal *j = container_of(cl, struct journal, io);
1520         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1521         struct bch_dev *ca;
1522         struct journal_buf *w = journal_last_unwritten_buf(j);
1523         struct bch_extent_ptr *ptr;
1524         struct bio *bio;
1525         unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1526
1527         extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1528                 ca = bch_dev_bkey_exists(c, ptr->dev);
1529                 if (!percpu_ref_tryget(&ca->io_ref)) {
1530                         /* XXX: fix this */
1531                         bch_err(c, "missing device for journal write\n");
1532                         continue;
1533                 }
1534
1535                 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1536                              sectors);
1537
1538                 bio = ca->journal.bio;
1539                 bio_reset(bio);
1540                 bio_set_dev(bio, ca->disk_sb.bdev);
1541                 bio->bi_iter.bi_sector  = ptr->offset;
1542                 bio->bi_end_io          = journal_write_endio;
1543                 bio->bi_private         = ca;
1544                 bio->bi_opf             = REQ_OP_WRITE|REQ_SYNC|REQ_META;
1545
1546                 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1547                 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1548
1549                 if (!JSET_NO_FLUSH(w->data))
1550                         bio->bi_opf    |= REQ_FUA;
1551                 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1552                         bio->bi_opf    |= REQ_PREFLUSH;
1553
1554                 bch2_bio_map(bio, w->data, sectors << 9);
1555
1556                 trace_journal_write(bio);
1557                 closure_bio_submit(bio, cl);
1558
1559                 ca->journal.bucket_seq[ca->journal.cur_idx] =
1560                         le64_to_cpu(w->data->seq);
1561         }
1562
1563         continue_at(cl, journal_write_done, c->io_complete_wq);
1564         return;
1565 }
1566
1567 void bch2_journal_write(struct closure *cl)
1568 {
1569         struct journal *j = container_of(cl, struct journal, io);
1570         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1571         struct bch_dev *ca;
1572         struct journal_buf *w = journal_last_unwritten_buf(j);
1573         struct jset_entry *start, *end;
1574         struct jset *jset;
1575         struct bio *bio;
1576         struct printbuf journal_debug_buf = PRINTBUF;
1577         bool validate_before_checksum = false;
1578         unsigned i, sectors, bytes, u64s, nr_rw_members = 0;
1579         int ret;
1580
1581         BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1582
1583         journal_buf_realloc(j, w);
1584         jset = w->data;
1585
1586         j->write_start_time = local_clock();
1587
1588         spin_lock(&j->lock);
1589         if (bch2_journal_error(j) ||
1590             w->noflush ||
1591             (!w->must_flush &&
1592              (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1593              test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags))) {
1594                 w->noflush = true;
1595                 SET_JSET_NO_FLUSH(jset, true);
1596                 jset->last_seq  = 0;
1597                 w->last_seq     = 0;
1598
1599                 j->nr_noflush_writes++;
1600         } else {
1601                 j->last_flush_write = jiffies;
1602                 j->nr_flush_writes++;
1603         }
1604         spin_unlock(&j->lock);
1605
1606         /*
1607          * New btree roots are set by journalling them; when the journal entry
1608          * gets written we have to propagate them to c->btree_roots
1609          *
1610          * But, every journal entry we write has to contain all the btree roots
1611          * (at least for now); so after we copy btree roots to c->btree_roots we
1612          * have to get any missing btree roots and add them to this journal
1613          * entry:
1614          */
1615
1616         bch2_journal_entries_to_btree_roots(c, jset);
1617
1618         start = end = vstruct_last(jset);
1619
1620         end     = bch2_btree_roots_to_journal_entries(c, jset->start, end);
1621
1622         bch2_journal_super_entries_add_common(c, &end,
1623                                 le64_to_cpu(jset->seq));
1624         u64s    = (u64 *) end - (u64 *) start;
1625         BUG_ON(u64s > j->entry_u64s_reserved);
1626
1627         le32_add_cpu(&jset->u64s, u64s);
1628         BUG_ON(vstruct_sectors(jset, c->block_bits) > w->sectors);
1629
1630         jset->magic             = cpu_to_le64(jset_magic(c));
1631         jset->version           = c->sb.version < bcachefs_metadata_version_bkey_renumber
1632                 ? cpu_to_le32(BCH_JSET_VERSION_OLD)
1633                 : cpu_to_le32(c->sb.version);
1634
1635         SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1636         SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1637
1638         if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1639                 j->last_empty_seq = le64_to_cpu(jset->seq);
1640
1641         if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1642                 validate_before_checksum = true;
1643
1644         if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1645                 validate_before_checksum = true;
1646
1647         if (validate_before_checksum &&
1648             jset_validate_for_write(c, jset))
1649                 goto err;
1650
1651         ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1652                     jset->encrypted_start,
1653                     vstruct_end(jset) - (void *) jset->encrypted_start);
1654         if (bch2_fs_fatal_err_on(ret, c,
1655                         "error decrypting journal entry: %i", ret))
1656                 goto err;
1657
1658         jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1659                                   journal_nonce(jset), jset);
1660
1661         if (!validate_before_checksum &&
1662             jset_validate_for_write(c, jset))
1663                 goto err;
1664
1665         sectors = vstruct_sectors(jset, c->block_bits);
1666         BUG_ON(sectors > w->sectors);
1667
1668         bytes = vstruct_bytes(jset);
1669         memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1670
1671 retry_alloc:
1672         spin_lock(&j->lock);
1673         ret = journal_write_alloc(j, w, sectors);
1674
1675         if (ret && j->can_discard) {
1676                 spin_unlock(&j->lock);
1677                 bch2_journal_do_discards(j);
1678                 goto retry_alloc;
1679         }
1680
1681         if (ret)
1682                 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1683
1684         /*
1685          * write is allocated, no longer need to account for it in
1686          * bch2_journal_space_available():
1687          */
1688         w->sectors = 0;
1689
1690         /*
1691          * journal entry has been compacted and allocated, recalculate space
1692          * available:
1693          */
1694         bch2_journal_space_available(j);
1695         spin_unlock(&j->lock);
1696
1697         if (ret) {
1698                 bch_err(c, "Unable to allocate journal write:\n%s",
1699                         journal_debug_buf.buf);
1700                 printbuf_exit(&journal_debug_buf);
1701                 bch2_fatal_error(c);
1702                 continue_at(cl, journal_write_done, c->io_complete_wq);
1703                 return;
1704         }
1705
1706         w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1707
1708         if (c->opts.nochanges)
1709                 goto no_io;
1710
1711         for_each_rw_member(ca, c, i)
1712                 nr_rw_members++;
1713
1714         if (nr_rw_members > 1)
1715                 w->separate_flush = true;
1716
1717         if (!JSET_NO_FLUSH(jset) && w->separate_flush) {
1718                 for_each_rw_member(ca, c, i) {
1719                         percpu_ref_get(&ca->io_ref);
1720
1721                         bio = ca->journal.bio;
1722                         bio_reset(bio);
1723                         bio_set_dev(bio, ca->disk_sb.bdev);
1724                         bio->bi_opf             = REQ_OP_FLUSH;
1725                         bio->bi_end_io          = journal_write_endio;
1726                         bio->bi_private         = ca;
1727                         closure_bio_submit(bio, cl);
1728                 }
1729         }
1730
1731         continue_at(cl, do_journal_write, c->io_complete_wq);
1732         return;
1733 no_io:
1734         continue_at(cl, journal_write_done, c->io_complete_wq);
1735         return;
1736 err:
1737         bch2_fatal_error(c);
1738         continue_at(cl, journal_write_done, c->io_complete_wq);
1739 }