1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
6 #include "btree_update_interior.h"
7 #include "btree_write_buffer.h"
10 #include "disk_groups.h"
13 #include "journal_io.h"
14 #include "journal_reclaim.h"
15 #include "journal_seq_blacklist.h"
20 static struct nonce journal_nonce(const struct jset *jset)
22 return (struct nonce) {{
24 [1] = ((__le32 *) &jset->seq)[0],
25 [2] = ((__le32 *) &jset->seq)[1],
26 [3] = BCH_NONCE_JOURNAL,
30 static bool jset_csum_good(struct bch_fs *c, struct jset *j)
32 return bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j)) &&
33 !bch2_crc_cmp(j->csum,
34 csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j));
37 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
39 return (seq - c->journal_entries_base_seq) & (~0U >> 1);
42 static void __journal_replay_free(struct bch_fs *c,
43 struct journal_replay *i)
45 struct journal_replay **p =
46 genradix_ptr(&c->journal_entries,
47 journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
51 kvpfree(i, offsetof(struct journal_replay, j) +
52 vstruct_bytes(&i->j));
55 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
59 if (!c->opts.read_entire_journal)
60 __journal_replay_free(c, i);
70 #define JOURNAL_ENTRY_ADD_OK 0
71 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
74 * Given a journal entry we just read, add it to the list of journal entries to
77 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
78 struct journal_ptr entry_ptr,
79 struct journal_list *jlist, struct jset *j)
81 struct genradix_iter iter;
82 struct journal_replay **_i, *i, *dup;
83 struct journal_ptr *ptr;
84 size_t bytes = vstruct_bytes(j);
85 u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
86 int ret = JOURNAL_ENTRY_ADD_OK;
88 /* Is this entry older than the range we need? */
89 if (!c->opts.read_entire_journal &&
90 le64_to_cpu(j->seq) < jlist->last_seq)
91 return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
94 * genradixes are indexed by a ulong, not a u64, so we can't index them
95 * by sequence number directly: Assume instead that they will all fall
96 * within the range of +-2billion of the filrst one we find.
98 if (!c->journal_entries_base_seq)
99 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
101 /* Drop entries we don't need anymore */
102 if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
103 genradix_for_each_from(&c->journal_entries, iter, _i,
104 journal_entry_radix_idx(c, jlist->last_seq)) {
110 if (le64_to_cpu(i->j.seq) >= last_seq)
112 journal_replay_free(c, i);
116 jlist->last_seq = max(jlist->last_seq, last_seq);
118 _i = genradix_ptr_alloc(&c->journal_entries,
119 journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
122 return -BCH_ERR_ENOMEM_journal_entry_add;
125 * Duplicate journal entries? If so we want the one that didn't have a
130 if (bytes == vstruct_bytes(&dup->j) &&
131 !memcmp(j, &dup->j, bytes)) {
136 if (!entry_ptr.csum_good) {
144 fsck_err(c, journal_entry_replicas_data_mismatch,
145 "found duplicate but non identical journal entries (seq %llu)",
146 le64_to_cpu(j->seq));
151 i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
153 return -BCH_ERR_ENOMEM_journal_entry_add;
156 i->csum_good = entry_ptr.csum_good;
158 unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct");
159 i->ptrs[i->nr_ptrs++] = entry_ptr;
162 if (dup->nr_ptrs >= ARRAY_SIZE(dup->ptrs)) {
163 bch_err(c, "found too many copies of journal entry %llu",
164 le64_to_cpu(i->j.seq));
165 dup->nr_ptrs = ARRAY_SIZE(dup->ptrs) - 1;
168 /* The first ptr should represent the jset we kept: */
169 memcpy(i->ptrs + i->nr_ptrs,
171 sizeof(dup->ptrs[0]) * dup->nr_ptrs);
172 i->nr_ptrs += dup->nr_ptrs;
173 __journal_replay_free(c, dup);
179 for (ptr = i->ptrs; ptr < i->ptrs + i->nr_ptrs; ptr++) {
180 if (ptr->dev == ca->dev_idx) {
181 bch_err(c, "duplicate journal entry %llu on same device",
182 le64_to_cpu(i->j.seq));
187 if (i->nr_ptrs >= ARRAY_SIZE(i->ptrs)) {
188 bch_err(c, "found too many copies of journal entry %llu",
189 le64_to_cpu(i->j.seq));
193 i->ptrs[i->nr_ptrs++] = entry_ptr;
199 /* this fills in a range with empty jset_entries: */
200 static void journal_entry_null_range(void *start, void *end)
202 struct jset_entry *entry;
204 for (entry = start; entry != end; entry = vstruct_next(entry))
205 memset(entry, 0, sizeof(*entry));
208 #define JOURNAL_ENTRY_REREAD 5
209 #define JOURNAL_ENTRY_NONE 6
210 #define JOURNAL_ENTRY_BAD 7
212 static void journal_entry_err_msg(struct printbuf *out,
215 struct jset_entry *entry)
217 prt_str(out, "invalid journal entry, version=");
218 bch2_version_to_text(out, version);
221 prt_str(out, " type=");
222 prt_str(out, bch2_jset_entry_types[entry->type]);
226 prt_printf(out, " in superblock");
229 prt_printf(out, " seq=%llu", le64_to_cpu(jset->seq));
232 prt_printf(out, " offset=%zi/%u",
233 (u64 *) entry - jset->_data,
234 le32_to_cpu(jset->u64s));
240 #define journal_entry_err(c, version, jset, entry, _err, msg, ...) \
242 struct printbuf _buf = PRINTBUF; \
244 journal_entry_err_msg(&_buf, version, jset, entry); \
245 prt_printf(&_buf, msg, ##__VA_ARGS__); \
247 switch (flags & BKEY_INVALID_WRITE) { \
249 mustfix_fsck_err(c, _err, "%s", _buf.buf); \
252 bch2_sb_error_count(c, BCH_FSCK_ERR_##_err); \
253 bch_err(c, "corrupt metadata before write: %s\n", _buf.buf);\
254 if (bch2_fs_inconsistent(c)) { \
255 ret = -BCH_ERR_fsck_errors_not_fixed; \
261 printbuf_exit(&_buf); \
265 #define journal_entry_err_on(cond, ...) \
266 ((cond) ? journal_entry_err(__VA_ARGS__) : false)
268 #define FSCK_DELETED_KEY 5
270 static int journal_validate_key(struct bch_fs *c,
272 struct jset_entry *entry,
273 unsigned level, enum btree_id btree_id,
275 unsigned version, int big_endian,
276 enum bkey_invalid_flags flags)
278 int write = flags & BKEY_INVALID_WRITE;
279 void *next = vstruct_next(entry);
280 struct printbuf buf = PRINTBUF;
283 if (journal_entry_err_on(!k->k.u64s,
284 c, version, jset, entry,
285 journal_entry_bkey_u64s_0,
287 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
288 journal_entry_null_range(vstruct_next(entry), next);
289 return FSCK_DELETED_KEY;
292 if (journal_entry_err_on((void *) bkey_next(k) >
293 (void *) vstruct_next(entry),
294 c, version, jset, entry,
295 journal_entry_bkey_past_end,
296 "extends past end of journal entry")) {
297 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
298 journal_entry_null_range(vstruct_next(entry), next);
299 return FSCK_DELETED_KEY;
302 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT,
303 c, version, jset, entry,
304 journal_entry_bkey_bad_format,
305 "bad format %u", k->k.format)) {
306 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
307 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
308 journal_entry_null_range(vstruct_next(entry), next);
309 return FSCK_DELETED_KEY;
313 bch2_bkey_compat(level, btree_id, version, big_endian,
314 write, NULL, bkey_to_packed(k));
316 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
317 __btree_node_type(level, btree_id), write, &buf)) {
318 printbuf_reset(&buf);
319 journal_entry_err_msg(&buf, version, jset, entry);
321 printbuf_indent_add(&buf, 2);
323 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
325 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
326 __btree_node_type(level, btree_id), write, &buf);
328 mustfix_fsck_err(c, journal_entry_bkey_invalid,
331 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
332 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
333 journal_entry_null_range(vstruct_next(entry), next);
336 return FSCK_DELETED_KEY;
340 bch2_bkey_compat(level, btree_id, version, big_endian,
341 write, NULL, bkey_to_packed(k));
347 static int journal_entry_btree_keys_validate(struct bch_fs *c,
349 struct jset_entry *entry,
350 unsigned version, int big_endian,
351 enum bkey_invalid_flags flags)
353 struct bkey_i *k = entry->start;
355 while (k != vstruct_last(entry)) {
356 int ret = journal_validate_key(c, jset, entry,
359 k, version, big_endian,
360 flags|BKEY_INVALID_JOURNAL);
361 if (ret == FSCK_DELETED_KEY)
370 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
371 struct jset_entry *entry)
376 jset_entry_for_each_key(entry, k) {
379 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
381 prt_printf(out, "btree=%s l=%u ", bch2_btree_id_str(entry->btree_id), entry->level);
382 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
387 static int journal_entry_btree_root_validate(struct bch_fs *c,
389 struct jset_entry *entry,
390 unsigned version, int big_endian,
391 enum bkey_invalid_flags flags)
393 struct bkey_i *k = entry->start;
396 if (journal_entry_err_on(!entry->u64s ||
397 le16_to_cpu(entry->u64s) != k->k.u64s,
398 c, version, jset, entry,
399 journal_entry_btree_root_bad_size,
400 "invalid btree root journal entry: wrong number of keys")) {
401 void *next = vstruct_next(entry);
403 * we don't want to null out this jset_entry,
404 * just the contents, so that later we can tell
405 * we were _supposed_ to have a btree root
408 journal_entry_null_range(vstruct_next(entry), next);
412 return journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
413 version, big_endian, flags);
418 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
419 struct jset_entry *entry)
421 journal_entry_btree_keys_to_text(out, c, entry);
424 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
426 struct jset_entry *entry,
427 unsigned version, int big_endian,
428 enum bkey_invalid_flags flags)
430 /* obsolete, don't care: */
434 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
435 struct jset_entry *entry)
439 static int journal_entry_blacklist_validate(struct bch_fs *c,
441 struct jset_entry *entry,
442 unsigned version, int big_endian,
443 enum bkey_invalid_flags flags)
447 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
448 c, version, jset, entry,
449 journal_entry_blacklist_bad_size,
450 "invalid journal seq blacklist entry: bad size")) {
451 journal_entry_null_range(entry, vstruct_next(entry));
457 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
458 struct jset_entry *entry)
460 struct jset_entry_blacklist *bl =
461 container_of(entry, struct jset_entry_blacklist, entry);
463 prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
466 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
468 struct jset_entry *entry,
469 unsigned version, int big_endian,
470 enum bkey_invalid_flags flags)
472 struct jset_entry_blacklist_v2 *bl_entry;
475 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
476 c, version, jset, entry,
477 journal_entry_blacklist_v2_bad_size,
478 "invalid journal seq blacklist entry: bad size")) {
479 journal_entry_null_range(entry, vstruct_next(entry));
483 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
485 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
486 le64_to_cpu(bl_entry->end),
487 c, version, jset, entry,
488 journal_entry_blacklist_v2_start_past_end,
489 "invalid journal seq blacklist entry: start > end")) {
490 journal_entry_null_range(entry, vstruct_next(entry));
497 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
498 struct jset_entry *entry)
500 struct jset_entry_blacklist_v2 *bl =
501 container_of(entry, struct jset_entry_blacklist_v2, entry);
503 prt_printf(out, "start=%llu end=%llu",
504 le64_to_cpu(bl->start),
505 le64_to_cpu(bl->end));
508 static int journal_entry_usage_validate(struct bch_fs *c,
510 struct jset_entry *entry,
511 unsigned version, int big_endian,
512 enum bkey_invalid_flags flags)
514 struct jset_entry_usage *u =
515 container_of(entry, struct jset_entry_usage, entry);
516 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
519 if (journal_entry_err_on(bytes < sizeof(*u),
520 c, version, jset, entry,
521 journal_entry_usage_bad_size,
522 "invalid journal entry usage: bad size")) {
523 journal_entry_null_range(entry, vstruct_next(entry));
531 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
532 struct jset_entry *entry)
534 struct jset_entry_usage *u =
535 container_of(entry, struct jset_entry_usage, entry);
537 prt_printf(out, "type=%s v=%llu",
538 bch2_fs_usage_types[u->entry.btree_id],
542 static int journal_entry_data_usage_validate(struct bch_fs *c,
544 struct jset_entry *entry,
545 unsigned version, int big_endian,
546 enum bkey_invalid_flags flags)
548 struct jset_entry_data_usage *u =
549 container_of(entry, struct jset_entry_data_usage, entry);
550 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
551 struct printbuf err = PRINTBUF;
554 if (journal_entry_err_on(bytes < sizeof(*u) ||
555 bytes < sizeof(*u) + u->r.nr_devs,
556 c, version, jset, entry,
557 journal_entry_data_usage_bad_size,
558 "invalid journal entry usage: bad size")) {
559 journal_entry_null_range(entry, vstruct_next(entry));
563 if (journal_entry_err_on(bch2_replicas_entry_validate(&u->r, c->disk_sb.sb, &err),
564 c, version, jset, entry,
565 journal_entry_data_usage_bad_size,
566 "invalid journal entry usage: %s", err.buf)) {
567 journal_entry_null_range(entry, vstruct_next(entry));
576 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
577 struct jset_entry *entry)
579 struct jset_entry_data_usage *u =
580 container_of(entry, struct jset_entry_data_usage, entry);
582 bch2_replicas_entry_to_text(out, &u->r);
583 prt_printf(out, "=%llu", le64_to_cpu(u->v));
586 static int journal_entry_clock_validate(struct bch_fs *c,
588 struct jset_entry *entry,
589 unsigned version, int big_endian,
590 enum bkey_invalid_flags flags)
592 struct jset_entry_clock *clock =
593 container_of(entry, struct jset_entry_clock, entry);
594 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
597 if (journal_entry_err_on(bytes != sizeof(*clock),
598 c, version, jset, entry,
599 journal_entry_clock_bad_size,
601 journal_entry_null_range(entry, vstruct_next(entry));
605 if (journal_entry_err_on(clock->rw > 1,
606 c, version, jset, entry,
607 journal_entry_clock_bad_rw,
609 journal_entry_null_range(entry, vstruct_next(entry));
617 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
618 struct jset_entry *entry)
620 struct jset_entry_clock *clock =
621 container_of(entry, struct jset_entry_clock, entry);
623 prt_printf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
626 static int journal_entry_dev_usage_validate(struct bch_fs *c,
628 struct jset_entry *entry,
629 unsigned version, int big_endian,
630 enum bkey_invalid_flags flags)
632 struct jset_entry_dev_usage *u =
633 container_of(entry, struct jset_entry_dev_usage, entry);
634 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
635 unsigned expected = sizeof(*u);
639 if (journal_entry_err_on(bytes < expected,
640 c, version, jset, entry,
641 journal_entry_dev_usage_bad_size,
642 "bad size (%u < %u)",
644 journal_entry_null_range(entry, vstruct_next(entry));
648 dev = le32_to_cpu(u->dev);
650 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
651 c, version, jset, entry,
652 journal_entry_dev_usage_bad_dev,
654 journal_entry_null_range(entry, vstruct_next(entry));
658 if (journal_entry_err_on(u->pad,
659 c, version, jset, entry,
660 journal_entry_dev_usage_bad_pad,
662 journal_entry_null_range(entry, vstruct_next(entry));
670 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
671 struct jset_entry *entry)
673 struct jset_entry_dev_usage *u =
674 container_of(entry, struct jset_entry_dev_usage, entry);
675 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
677 prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
679 for (i = 0; i < nr_types; i++) {
681 prt_printf(out, " %s", bch2_data_types[i]);
683 prt_printf(out, " (unknown data type %u)", i);
684 prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
685 le64_to_cpu(u->d[i].buckets),
686 le64_to_cpu(u->d[i].sectors),
687 le64_to_cpu(u->d[i].fragmented));
691 static int journal_entry_log_validate(struct bch_fs *c,
693 struct jset_entry *entry,
694 unsigned version, int big_endian,
695 enum bkey_invalid_flags flags)
700 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
701 struct jset_entry *entry)
703 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
704 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
706 prt_printf(out, "%.*s", bytes, l->d);
709 static int journal_entry_overwrite_validate(struct bch_fs *c,
711 struct jset_entry *entry,
712 unsigned version, int big_endian,
713 enum bkey_invalid_flags flags)
715 return journal_entry_btree_keys_validate(c, jset, entry,
716 version, big_endian, READ);
719 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
720 struct jset_entry *entry)
722 journal_entry_btree_keys_to_text(out, c, entry);
725 static int journal_entry_write_buffer_keys_validate(struct bch_fs *c,
727 struct jset_entry *entry,
728 unsigned version, int big_endian,
729 enum bkey_invalid_flags flags)
731 return journal_entry_btree_keys_validate(c, jset, entry,
732 version, big_endian, READ);
735 static void journal_entry_write_buffer_keys_to_text(struct printbuf *out, struct bch_fs *c,
736 struct jset_entry *entry)
738 journal_entry_btree_keys_to_text(out, c, entry);
741 struct jset_entry_ops {
742 int (*validate)(struct bch_fs *, struct jset *,
743 struct jset_entry *, unsigned, int,
744 enum bkey_invalid_flags);
745 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
748 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
750 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
751 .validate = journal_entry_##f##_validate, \
752 .to_text = journal_entry_##f##_to_text, \
754 BCH_JSET_ENTRY_TYPES()
758 int bch2_journal_entry_validate(struct bch_fs *c,
760 struct jset_entry *entry,
761 unsigned version, int big_endian,
762 enum bkey_invalid_flags flags)
764 return entry->type < BCH_JSET_ENTRY_NR
765 ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
766 version, big_endian, flags)
770 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
771 struct jset_entry *entry)
773 if (entry->type < BCH_JSET_ENTRY_NR) {
774 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
775 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
777 prt_printf(out, "(unknown type %u)", entry->type);
781 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
782 enum bkey_invalid_flags flags)
784 struct jset_entry *entry;
785 unsigned version = le32_to_cpu(jset->version);
788 vstruct_for_each(jset, entry) {
789 if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset),
790 c, version, jset, entry,
791 journal_entry_past_jset_end,
792 "journal entry extends past end of jset")) {
793 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
797 ret = bch2_journal_entry_validate(c, jset, entry,
798 version, JSET_BIG_ENDIAN(jset), flags);
806 static int jset_validate(struct bch_fs *c,
808 struct jset *jset, u64 sector,
809 enum bkey_invalid_flags flags)
814 if (le64_to_cpu(jset->magic) != jset_magic(c))
815 return JOURNAL_ENTRY_NONE;
817 version = le32_to_cpu(jset->version);
818 if (journal_entry_err_on(!bch2_version_compatible(version),
819 c, version, jset, NULL,
820 jset_unsupported_version,
821 "%s sector %llu seq %llu: incompatible journal entry version %u.%u",
822 ca ? ca->name : c->name,
823 sector, le64_to_cpu(jset->seq),
824 BCH_VERSION_MAJOR(version),
825 BCH_VERSION_MINOR(version))) {
826 /* don't try to continue: */
830 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
831 c, version, jset, NULL,
833 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
834 ca ? ca->name : c->name,
835 sector, le64_to_cpu(jset->seq),
836 JSET_CSUM_TYPE(jset)))
837 ret = JOURNAL_ENTRY_BAD;
839 /* last_seq is ignored when JSET_NO_FLUSH is true */
840 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
841 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
842 c, version, jset, NULL,
843 jset_last_seq_newer_than_seq,
844 "invalid journal entry: last_seq > seq (%llu > %llu)",
845 le64_to_cpu(jset->last_seq),
846 le64_to_cpu(jset->seq))) {
847 jset->last_seq = jset->seq;
848 return JOURNAL_ENTRY_BAD;
851 ret = jset_validate_entries(c, jset, flags);
856 static int jset_validate_early(struct bch_fs *c,
858 struct jset *jset, u64 sector,
859 unsigned bucket_sectors_left,
860 unsigned sectors_read)
862 size_t bytes = vstruct_bytes(jset);
864 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
867 if (le64_to_cpu(jset->magic) != jset_magic(c))
868 return JOURNAL_ENTRY_NONE;
870 version = le32_to_cpu(jset->version);
871 if (journal_entry_err_on(!bch2_version_compatible(version),
872 c, version, jset, NULL,
873 jset_unsupported_version,
874 "%s sector %llu seq %llu: unknown journal entry version %u.%u",
875 ca ? ca->name : c->name,
876 sector, le64_to_cpu(jset->seq),
877 BCH_VERSION_MAJOR(version),
878 BCH_VERSION_MINOR(version))) {
879 /* don't try to continue: */
883 if (bytes > (sectors_read << 9) &&
884 sectors_read < bucket_sectors_left)
885 return JOURNAL_ENTRY_REREAD;
887 if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
888 c, version, jset, NULL,
889 jset_past_bucket_end,
890 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
891 ca ? ca->name : c->name,
892 sector, le64_to_cpu(jset->seq), bytes))
893 le32_add_cpu(&jset->u64s,
894 -((bytes - (bucket_sectors_left << 9)) / 8));
899 struct journal_read_buf {
904 static int journal_read_buf_realloc(struct journal_read_buf *b,
909 /* the bios are sized for this many pages, max: */
910 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
911 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
913 new_size = roundup_pow_of_two(new_size);
914 n = kvpmalloc(new_size, GFP_KERNEL);
916 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
918 kvpfree(b->data, b->size);
924 static int journal_read_bucket(struct bch_dev *ca,
925 struct journal_read_buf *buf,
926 struct journal_list *jlist,
929 struct bch_fs *c = ca->fs;
930 struct journal_device *ja = &ca->journal;
931 struct jset *j = NULL;
932 unsigned sectors, sectors_read = 0;
933 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
934 end = offset + ca->mi.bucket_size;
935 bool saw_bad = false, csum_good;
938 pr_debug("reading %u", bucket);
940 while (offset < end) {
945 sectors_read = min_t(unsigned,
946 end - offset, buf->size >> 9);
947 nr_bvecs = buf_pages(buf->data, sectors_read << 9);
949 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
950 bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
952 bio->bi_iter.bi_sector = offset;
953 bch2_bio_map(bio, buf->data, sectors_read << 9);
955 ret = submit_bio_wait(bio);
958 if (bch2_dev_io_err_on(ret, ca, BCH_MEMBER_ERROR_read,
959 "journal read error: sector %llu",
961 bch2_meta_read_fault("journal")) {
963 * We don't error out of the recovery process
964 * here, since the relevant journal entry may be
965 * found on a different device, and missing or
966 * no journal entries will be handled later
974 ret = jset_validate_early(c, ca, j, offset,
975 end - offset, sectors_read);
978 sectors = vstruct_sectors(j, c->block_bits);
980 case JOURNAL_ENTRY_REREAD:
981 if (vstruct_bytes(j) > buf->size) {
982 ret = journal_read_buf_realloc(buf,
988 case JOURNAL_ENTRY_NONE:
992 * On checksum error we don't really trust the size
993 * field of the journal entry we read, so try reading
994 * again at next block boundary:
996 sectors = block_sectors(c);
1003 * This happens sometimes if we don't have discards on -
1004 * when we've partially overwritten a bucket with new
1005 * journal entries. We don't need the rest of the
1008 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
1011 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
1013 csum_good = jset_csum_good(c, j);
1014 if (bch2_dev_io_err_on(!csum_good, ca, BCH_MEMBER_ERROR_checksum,
1015 "journal checksum error"))
1018 ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
1020 vstruct_end(j) - (void *) j->encrypted_start);
1021 bch2_fs_fatal_err_on(ret, c,
1022 "error decrypting journal entry: %i", ret);
1024 mutex_lock(&jlist->lock);
1025 ret = journal_entry_add(c, ca, (struct journal_ptr) {
1026 .csum_good = csum_good,
1029 .bucket_offset = offset -
1030 bucket_to_sector(ca, ja->buckets[bucket]),
1033 mutex_unlock(&jlist->lock);
1036 case JOURNAL_ENTRY_ADD_OK:
1038 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
1046 sectors_read -= sectors;
1047 j = ((void *) j) + (sectors << 9);
1053 static CLOSURE_CALLBACK(bch2_journal_read_device)
1055 closure_type(ja, struct journal_device, read);
1056 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
1057 struct bch_fs *c = ca->fs;
1058 struct journal_list *jlist =
1059 container_of(cl->parent, struct journal_list, cl);
1060 struct journal_replay *r, **_r;
1061 struct genradix_iter iter;
1062 struct journal_read_buf buf = { NULL, 0 };
1069 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1073 pr_debug("%u journal buckets", ja->nr);
1075 for (i = 0; i < ja->nr; i++) {
1076 ret = journal_read_bucket(ca, &buf, jlist, i);
1081 ja->sectors_free = ca->mi.bucket_size;
1083 mutex_lock(&jlist->lock);
1084 genradix_for_each_reverse(&c->journal_entries, iter, _r) {
1090 for (i = 0; i < r->nr_ptrs; i++) {
1091 if (r->ptrs[i].dev == ca->dev_idx) {
1092 unsigned wrote = bucket_remainder(ca, r->ptrs[i].sector) +
1093 vstruct_sectors(&r->j, c->block_bits);
1095 ja->cur_idx = r->ptrs[i].bucket;
1096 ja->sectors_free = ca->mi.bucket_size - wrote;
1102 mutex_unlock(&jlist->lock);
1104 if (ja->bucket_seq[ja->cur_idx] &&
1105 ja->sectors_free == ca->mi.bucket_size) {
1108 * Debug code for ZNS support, where we (probably) want to be
1109 * correlated where we stopped in the journal to the zone write
1112 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1113 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1114 for (i = 0; i < 3; i++) {
1115 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
1117 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1120 ja->sectors_free = 0;
1124 * Set dirty_idx to indicate the entire journal is full and needs to be
1125 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1126 * pinned when it first runs:
1128 ja->discard_idx = ja->dirty_idx_ondisk =
1129 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1131 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1132 kvpfree(buf.data, buf.size);
1133 percpu_ref_put(&ca->io_ref);
1137 mutex_lock(&jlist->lock);
1139 mutex_unlock(&jlist->lock);
1143 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1144 struct journal_replay *j)
1148 for (i = 0; i < j->nr_ptrs; i++) {
1149 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1152 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1155 prt_printf(out, " ");
1156 prt_printf(out, "%u:%u:%u (sector %llu)",
1159 j->ptrs[i].bucket_offset,
1164 int bch2_journal_read(struct bch_fs *c,
1169 struct journal_list jlist;
1170 struct journal_replay *i, **_i, *prev = NULL;
1171 struct genradix_iter radix_iter;
1174 struct printbuf buf = PRINTBUF;
1175 bool degraded = false, last_write_torn = false;
1179 closure_init_stack(&jlist.cl);
1180 mutex_init(&jlist.lock);
1184 for_each_member_device(ca, c, iter) {
1185 if (!c->opts.fsck &&
1186 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1189 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1190 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1191 percpu_ref_tryget(&ca->io_ref))
1192 closure_call(&ca->journal.read,
1193 bch2_journal_read_device,
1200 closure_sync(&jlist.cl);
1210 * Find most recent flush entry, and ignore newer non flush entries -
1211 * those entries will be blacklisted:
1213 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1214 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
1218 if (!i || i->ignore)
1222 *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1224 if (JSET_NO_FLUSH(&i->j)) {
1229 if (!last_write_torn && !i->csum_good) {
1230 last_write_torn = true;
1235 if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1236 c, le32_to_cpu(i->j.version), &i->j, NULL,
1237 jset_last_seq_newer_than_seq,
1238 "invalid journal entry: last_seq > seq (%llu > %llu)",
1239 le64_to_cpu(i->j.last_seq),
1240 le64_to_cpu(i->j.seq)))
1241 i->j.last_seq = i->j.seq;
1243 *last_seq = le64_to_cpu(i->j.last_seq);
1244 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1249 bch_info(c, "journal read done, but no entries found");
1254 fsck_err(c, dirty_but_no_journal_entries_post_drop_nonflushes,
1255 "journal read done, but no entries found after dropping non-flushes");
1259 bch_info(c, "journal read done, replaying entries %llu-%llu",
1260 *last_seq, *blacklist_seq - 1);
1262 if (*start_seq != *blacklist_seq)
1263 bch_info(c, "dropped unflushed entries %llu-%llu",
1264 *blacklist_seq, *start_seq - 1);
1266 /* Drop blacklisted entries and entries older than last_seq: */
1267 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1270 if (!i || i->ignore)
1273 seq = le64_to_cpu(i->j.seq);
1274 if (seq < *last_seq) {
1275 journal_replay_free(c, i);
1279 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1280 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1281 jset_seq_blacklisted,
1282 "found blacklisted journal entry %llu", seq);
1287 /* Check for missing entries: */
1289 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1292 if (!i || i->ignore)
1295 BUG_ON(seq > le64_to_cpu(i->j.seq));
1297 while (seq < le64_to_cpu(i->j.seq)) {
1298 u64 missing_start, missing_end;
1299 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1301 while (seq < le64_to_cpu(i->j.seq) &&
1302 bch2_journal_seq_is_blacklisted(c, seq, false))
1305 if (seq == le64_to_cpu(i->j.seq))
1308 missing_start = seq;
1310 while (seq < le64_to_cpu(i->j.seq) &&
1311 !bch2_journal_seq_is_blacklisted(c, seq, false))
1315 bch2_journal_ptrs_to_text(&buf1, c, prev);
1316 prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1318 prt_printf(&buf1, "(none)");
1319 bch2_journal_ptrs_to_text(&buf2, c, i);
1321 missing_end = seq - 1;
1322 fsck_err(c, journal_entries_missing,
1323 "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1326 missing_start, missing_end,
1327 *last_seq, *blacklist_seq - 1,
1328 buf1.buf, buf2.buf);
1330 printbuf_exit(&buf1);
1331 printbuf_exit(&buf2);
1338 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1339 struct bch_replicas_padded replicas = {
1340 .e.data_type = BCH_DATA_journal,
1346 if (!i || i->ignore)
1349 for (ptr = 0; ptr < i->nr_ptrs; ptr++) {
1350 ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
1352 if (!i->ptrs[ptr].csum_good)
1353 bch_err_dev_offset(ca, i->ptrs[ptr].sector,
1354 "invalid journal checksum, seq %llu%s",
1355 le64_to_cpu(i->j.seq),
1356 i->csum_good ? " (had good copy on another device)" : "");
1359 ret = jset_validate(c,
1360 bch_dev_bkey_exists(c, i->ptrs[0].dev),
1367 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1368 replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1370 bch2_replicas_entry_sort(&replicas.e);
1372 printbuf_reset(&buf);
1373 bch2_replicas_entry_to_text(&buf, &replicas.e);
1376 !bch2_replicas_marked(c, &replicas.e) &&
1377 (le64_to_cpu(i->j.seq) == *last_seq ||
1378 fsck_err(c, journal_entry_replicas_not_marked,
1379 "superblock not marked as containing replicas for journal entry %llu\n %s",
1380 le64_to_cpu(i->j.seq), buf.buf))) {
1381 ret = bch2_mark_replicas(c, &replicas.e);
1388 printbuf_exit(&buf);
1392 /* journal write: */
1394 static void __journal_write_alloc(struct journal *j,
1395 struct journal_buf *w,
1396 struct dev_alloc_list *devs_sorted,
1399 unsigned replicas_want)
1401 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1402 struct journal_device *ja;
1406 if (*replicas >= replicas_want)
1409 for (i = 0; i < devs_sorted->nr; i++) {
1410 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1417 * Check that we can use this device, and aren't already using
1420 if (!ca->mi.durability ||
1421 ca->mi.state != BCH_MEMBER_STATE_rw ||
1423 bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) ||
1424 sectors > ja->sectors_free)
1427 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1429 bch2_bkey_append_ptr(&w->key,
1430 (struct bch_extent_ptr) {
1431 .offset = bucket_to_sector(ca,
1432 ja->buckets[ja->cur_idx]) +
1433 ca->mi.bucket_size -
1438 ja->sectors_free -= sectors;
1439 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1441 *replicas += ca->mi.durability;
1443 if (*replicas >= replicas_want)
1449 * journal_write_alloc - decide where to write next journal entry
1451 * @j: journal object
1452 * @w: journal buf (entry to be written)
1454 * Returns: 0 on success, or -EROFS on failure
1456 static int journal_write_alloc(struct journal *j, struct journal_buf *w)
1458 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1459 struct bch_devs_mask devs;
1460 struct journal_device *ja;
1462 struct dev_alloc_list devs_sorted;
1463 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1464 unsigned target = c->opts.metadata_target ?:
1465 c->opts.foreground_target;
1466 unsigned i, replicas = 0, replicas_want =
1467 READ_ONCE(c->opts.metadata_replicas);
1471 devs = target_rw_devs(c, BCH_DATA_journal, target);
1473 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1475 __journal_write_alloc(j, w, &devs_sorted,
1476 sectors, &replicas, replicas_want);
1478 if (replicas >= replicas_want)
1481 for (i = 0; i < devs_sorted.nr; i++) {
1482 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1488 if (sectors > ja->sectors_free &&
1489 sectors <= ca->mi.bucket_size &&
1490 bch2_journal_dev_buckets_available(j, ja,
1491 journal_space_discarded)) {
1492 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1493 ja->sectors_free = ca->mi.bucket_size;
1496 * ja->bucket_seq[ja->cur_idx] must always have
1497 * something sensible:
1499 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1503 __journal_write_alloc(j, w, &devs_sorted,
1504 sectors, &replicas, replicas_want);
1506 if (replicas < replicas_want && target) {
1507 /* Retry from all devices: */
1514 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1516 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1519 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1521 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1523 /* we aren't holding j->lock: */
1524 unsigned new_size = READ_ONCE(j->buf_size_want);
1527 if (buf->buf_size >= new_size)
1530 size_t btree_write_buffer_size = new_size / 64;
1532 if (bch2_btree_write_buffer_resize(c, btree_write_buffer_size))
1535 new_buf = kvpmalloc(new_size, GFP_NOFS|__GFP_NOWARN);
1539 memcpy(new_buf, buf->data, buf->buf_size);
1541 spin_lock(&j->lock);
1542 swap(buf->data, new_buf);
1543 swap(buf->buf_size, new_size);
1544 spin_unlock(&j->lock);
1546 kvpfree(new_buf, new_size);
1549 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1551 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1554 static CLOSURE_CALLBACK(journal_write_done)
1556 closure_type(j, struct journal, io);
1557 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1558 struct journal_buf *w = journal_last_unwritten_buf(j);
1559 struct bch_replicas_padded replicas;
1560 union journal_res_state old, new;
1564 bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1565 ? j->flush_write_time
1566 : j->noflush_write_time, j->write_start_time);
1568 if (!w->devs_written.nr) {
1569 bch_err(c, "unable to write journal to sufficient devices");
1572 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1574 if (bch2_mark_replicas(c, &replicas.e))
1579 bch2_fatal_error(c);
1581 spin_lock(&j->lock);
1582 seq = le64_to_cpu(w->data->seq);
1584 if (seq >= j->pin.front)
1585 journal_seq_pin(j, seq)->devs = w->devs_written;
1588 if (!JSET_NO_FLUSH(w->data)) {
1589 j->flushed_seq_ondisk = seq;
1590 j->last_seq_ondisk = w->last_seq;
1592 bch2_do_discards(c);
1593 closure_wake_up(&c->freelist_wait);
1595 bch2_reset_alloc_cursors(c);
1597 } else if (!j->err_seq || seq < j->err_seq)
1600 j->seq_ondisk = seq;
1603 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1606 * Must come before signaling write completion, for
1607 * bch2_fs_journal_stop():
1609 if (j->watermark != BCH_WATERMARK_stripe)
1610 journal_reclaim_kick(&c->journal);
1612 /* also must come before signalling write completion: */
1613 closure_debug_destroy(cl);
1615 v = atomic64_read(&j->reservations.counter);
1618 BUG_ON(journal_state_count(new, new.unwritten_idx));
1620 new.unwritten_idx++;
1621 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1622 old.v, new.v)) != old.v);
1624 bch2_journal_reclaim_fast(j);
1625 bch2_journal_space_available(j);
1627 track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight],
1628 &j->max_in_flight_start, false);
1630 closure_wake_up(&w->wait);
1633 if (!journal_state_count(new, new.unwritten_idx) &&
1634 journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1635 spin_unlock(&j->lock);
1636 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1637 } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1638 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1639 struct journal_buf *buf = journal_cur_buf(j);
1640 long delta = buf->expires - jiffies;
1643 * We don't close a journal entry to write it while there's
1644 * previous entries still in flight - the current journal entry
1645 * might want to be written now:
1648 spin_unlock(&j->lock);
1649 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1651 spin_unlock(&j->lock);
1655 static void journal_write_endio(struct bio *bio)
1657 struct bch_dev *ca = bio->bi_private;
1658 struct journal *j = &ca->fs->journal;
1659 struct journal_buf *w = journal_last_unwritten_buf(j);
1660 unsigned long flags;
1662 if (bch2_dev_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_write,
1663 "error writing journal entry %llu: %s",
1664 le64_to_cpu(w->data->seq),
1665 bch2_blk_status_to_str(bio->bi_status)) ||
1666 bch2_meta_write_fault("journal")) {
1667 spin_lock_irqsave(&j->err_lock, flags);
1668 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1669 spin_unlock_irqrestore(&j->err_lock, flags);
1672 closure_put(&j->io);
1673 percpu_ref_put(&ca->io_ref);
1676 static CLOSURE_CALLBACK(do_journal_write)
1678 closure_type(j, struct journal, io);
1679 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1681 struct journal_buf *w = journal_last_unwritten_buf(j);
1682 struct bch_extent_ptr *ptr;
1684 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1686 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1687 ca = bch_dev_bkey_exists(c, ptr->dev);
1688 if (!percpu_ref_tryget(&ca->io_ref)) {
1690 bch_err(c, "missing device for journal write\n");
1694 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1697 bio = ca->journal.bio;
1698 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1699 bio->bi_iter.bi_sector = ptr->offset;
1700 bio->bi_end_io = journal_write_endio;
1701 bio->bi_private = ca;
1703 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1704 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1706 if (!JSET_NO_FLUSH(w->data))
1707 bio->bi_opf |= REQ_FUA;
1708 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1709 bio->bi_opf |= REQ_PREFLUSH;
1711 bch2_bio_map(bio, w->data, sectors << 9);
1713 trace_and_count(c, journal_write, bio);
1714 closure_bio_submit(bio, cl);
1716 ca->journal.bucket_seq[ca->journal.cur_idx] =
1717 le64_to_cpu(w->data->seq);
1720 continue_at(cl, journal_write_done, c->io_complete_wq);
1723 static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w)
1725 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1726 struct jset_entry *start, *end, *i;
1727 struct jset *jset = w->data;
1728 struct journal_keys_to_wb wb = { NULL };
1729 unsigned sectors, bytes, u64s;
1730 unsigned long btree_roots_have = 0;
1731 bool validate_before_checksum = false;
1732 u64 seq = le64_to_cpu(jset->seq);
1736 * Simple compaction, dropping empty jset_entries (from journal
1737 * reservations that weren't fully used) and merging jset_entries that
1740 * If we wanted to be really fancy here, we could sort all the keys in
1741 * the jset and drop keys that were overwritten - probably not worth it:
1743 vstruct_for_each(jset, i) {
1744 unsigned u64s = le16_to_cpu(i->u64s);
1751 * New btree roots are set by journalling them; when the journal
1752 * entry gets written we have to propagate them to
1755 * But, every journal entry we write has to contain all the
1756 * btree roots (at least for now); so after we copy btree roots
1757 * to c->btree_roots we have to get any missing btree roots and
1758 * add them to this journal entry:
1761 case BCH_JSET_ENTRY_btree_root:
1762 bch2_journal_entry_to_btree_root(c, i);
1763 __set_bit(i->btree_id, &btree_roots_have);
1765 case BCH_JSET_ENTRY_write_buffer_keys:
1766 EBUG_ON(!w->need_flush_to_write_buffer);
1769 bch2_journal_keys_to_write_buffer_start(c, &wb, seq);
1772 jset_entry_for_each_key(i, k) {
1773 ret = bch2_journal_key_to_wb(c, &wb, i->btree_id, k);
1775 bch2_fs_fatal_error(c, "-ENOMEM flushing journal keys to btree write buffer");
1776 bch2_journal_keys_to_write_buffer_end(c, &wb);
1780 i->type = BCH_JSET_ENTRY_btree_keys;
1786 bch2_journal_keys_to_write_buffer_end(c, &wb);
1787 w->need_flush_to_write_buffer = false;
1789 start = end = vstruct_last(jset);
1791 end = bch2_btree_roots_to_journal_entries(c, end, btree_roots_have);
1793 bch2_journal_super_entries_add_common(c, &end, seq);
1794 u64s = (u64 *) end - (u64 *) start;
1795 BUG_ON(u64s > j->entry_u64s_reserved);
1797 le32_add_cpu(&jset->u64s, u64s);
1799 sectors = vstruct_sectors(jset, c->block_bits);
1800 bytes = vstruct_bytes(jset);
1802 if (sectors > w->sectors) {
1803 bch2_fs_fatal_error(c, "aieeee! journal write overran available space, %zu > %u (extra %u reserved %u/%u)",
1804 vstruct_bytes(jset), w->sectors << 9,
1805 u64s, w->u64s_reserved, j->entry_u64s_reserved);
1809 jset->magic = cpu_to_le64(jset_magic(c));
1810 jset->version = cpu_to_le32(c->sb.version);
1812 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1813 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1815 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1816 j->last_empty_seq = seq;
1818 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1819 validate_before_checksum = true;
1821 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1822 validate_before_checksum = true;
1824 if (validate_before_checksum &&
1825 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1828 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1829 jset->encrypted_start,
1830 vstruct_end(jset) - (void *) jset->encrypted_start);
1831 if (bch2_fs_fatal_err_on(ret, c,
1832 "error decrypting journal entry: %i", ret))
1835 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1836 journal_nonce(jset), jset);
1838 if (!validate_before_checksum &&
1839 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1842 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1846 static int bch2_journal_write_pick_flush(struct journal *j, struct journal_buf *w)
1848 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1849 int error = bch2_journal_error(j);
1852 * If the journal is in an error state - we did an emergency shutdown -
1853 * we prefer to continue doing journal writes. We just mark them as
1854 * noflush so they'll never be used, but they'll still be visible by the
1855 * list_journal tool - this helps in debugging.
1857 * There's a caveat: the first journal write after marking the
1858 * superblock dirty must always be a flush write, because on startup
1859 * from a clean shutdown we didn't necessarily read the journal and the
1860 * new journal write might overwrite whatever was in the journal
1861 * previously - we can't leave the journal without any flush writes in
1864 * So if we're in an error state, and we're still starting up, we don't
1865 * write anything at all.
1867 if (error && test_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags))
1873 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1874 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags))) {
1876 SET_JSET_NO_FLUSH(w->data, true);
1877 w->data->last_seq = 0;
1880 j->nr_noflush_writes++;
1882 j->last_flush_write = jiffies;
1883 j->nr_flush_writes++;
1884 clear_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags);
1890 CLOSURE_CALLBACK(bch2_journal_write)
1892 closure_type(j, struct journal, io);
1893 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1895 struct journal_buf *w = journal_last_unwritten_buf(j);
1896 struct bch_replicas_padded replicas;
1898 struct printbuf journal_debug_buf = PRINTBUF;
1899 unsigned i, nr_rw_members = 0;
1902 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1904 j->write_start_time = local_clock();
1906 spin_lock(&j->lock);
1907 ret = bch2_journal_write_pick_flush(j, w);
1908 spin_unlock(&j->lock);
1912 mutex_lock(&j->buf_lock);
1913 journal_buf_realloc(j, w);
1915 ret = bch2_journal_write_prep(j, w);
1916 mutex_unlock(&j->buf_lock);
1920 j->entry_bytes_written += vstruct_bytes(w->data);
1923 spin_lock(&j->lock);
1924 ret = journal_write_alloc(j, w);
1925 if (!ret || !j->can_discard)
1928 spin_unlock(&j->lock);
1929 bch2_journal_do_discards(j);
1933 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1934 spin_unlock(&j->lock);
1935 bch_err(c, "Unable to allocate journal write:\n%s",
1936 journal_debug_buf.buf);
1937 printbuf_exit(&journal_debug_buf);
1942 * write is allocated, no longer need to account for it in
1943 * bch2_journal_space_available():
1948 * journal entry has been compacted and allocated, recalculate space
1951 bch2_journal_space_available(j);
1952 spin_unlock(&j->lock);
1954 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1956 if (c->opts.nochanges)
1959 for_each_rw_member(ca, c, i)
1962 if (nr_rw_members > 1)
1963 w->separate_flush = true;
1966 * Mark journal replicas before we submit the write to guarantee
1967 * recovery will find the journal entries after a crash.
1969 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1971 ret = bch2_mark_replicas(c, &replicas.e);
1975 if (!JSET_NO_FLUSH(w->data) && w->separate_flush) {
1976 for_each_rw_member(ca, c, i) {
1977 percpu_ref_get(&ca->io_ref);
1979 bio = ca->journal.bio;
1980 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_FLUSH);
1981 bio->bi_end_io = journal_write_endio;
1982 bio->bi_private = ca;
1983 closure_bio_submit(bio, cl);
1987 continue_at(cl, do_journal_write, c->io_complete_wq);
1990 continue_at(cl, journal_write_done, c->io_complete_wq);
1993 bch2_fatal_error(c);
1994 continue_at(cl, journal_write_done, c->io_complete_wq);