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