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, struct bch_csum *csum)
32 if (!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j))) {
33 *csum = (struct bch_csum) {};
37 *csum = csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j);
38 return !bch2_crc_cmp(j->csum, *csum);
41 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
43 return (seq - c->journal_entries_base_seq) & (~0U >> 1);
46 static void __journal_replay_free(struct bch_fs *c,
47 struct journal_replay *i)
49 struct journal_replay **p =
50 genradix_ptr(&c->journal_entries,
51 journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
55 kvpfree(i, offsetof(struct journal_replay, j) +
56 vstruct_bytes(&i->j));
59 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
63 if (!c->opts.read_entire_journal)
64 __journal_replay_free(c, i);
74 #define JOURNAL_ENTRY_ADD_OK 0
75 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
78 * Given a journal entry we just read, add it to the list of journal entries to
81 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
82 struct journal_ptr entry_ptr,
83 struct journal_list *jlist, struct jset *j)
85 struct genradix_iter iter;
86 struct journal_replay **_i, *i, *dup;
87 struct journal_ptr *ptr;
88 size_t bytes = vstruct_bytes(j);
89 u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
90 int ret = JOURNAL_ENTRY_ADD_OK;
92 /* Is this entry older than the range we need? */
93 if (!c->opts.read_entire_journal &&
94 le64_to_cpu(j->seq) < jlist->last_seq)
95 return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
98 * genradixes are indexed by a ulong, not a u64, so we can't index them
99 * by sequence number directly: Assume instead that they will all fall
100 * within the range of +-2billion of the filrst one we find.
102 if (!c->journal_entries_base_seq)
103 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
105 /* Drop entries we don't need anymore */
106 if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
107 genradix_for_each_from(&c->journal_entries, iter, _i,
108 journal_entry_radix_idx(c, jlist->last_seq)) {
114 if (le64_to_cpu(i->j.seq) >= last_seq)
116 journal_replay_free(c, i);
120 jlist->last_seq = max(jlist->last_seq, last_seq);
122 _i = genradix_ptr_alloc(&c->journal_entries,
123 journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
126 return -BCH_ERR_ENOMEM_journal_entry_add;
129 * Duplicate journal entries? If so we want the one that didn't have a
134 if (bytes == vstruct_bytes(&dup->j) &&
135 !memcmp(j, &dup->j, bytes)) {
140 if (!entry_ptr.csum_good) {
148 fsck_err(c, journal_entry_replicas_data_mismatch,
149 "found duplicate but non identical journal entries (seq %llu)",
150 le64_to_cpu(j->seq));
155 i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
157 return -BCH_ERR_ENOMEM_journal_entry_add;
160 i->csum_good = entry_ptr.csum_good;
162 unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct");
163 i->ptrs[i->nr_ptrs++] = entry_ptr;
166 if (dup->nr_ptrs >= ARRAY_SIZE(dup->ptrs)) {
167 bch_err(c, "found too many copies of journal entry %llu",
168 le64_to_cpu(i->j.seq));
169 dup->nr_ptrs = ARRAY_SIZE(dup->ptrs) - 1;
172 /* The first ptr should represent the jset we kept: */
173 memcpy(i->ptrs + i->nr_ptrs,
175 sizeof(dup->ptrs[0]) * dup->nr_ptrs);
176 i->nr_ptrs += dup->nr_ptrs;
177 __journal_replay_free(c, dup);
183 for (ptr = i->ptrs; ptr < i->ptrs + i->nr_ptrs; ptr++) {
184 if (ptr->dev == ca->dev_idx) {
185 bch_err(c, "duplicate journal entry %llu on same device",
186 le64_to_cpu(i->j.seq));
191 if (i->nr_ptrs >= ARRAY_SIZE(i->ptrs)) {
192 bch_err(c, "found too many copies of journal entry %llu",
193 le64_to_cpu(i->j.seq));
197 i->ptrs[i->nr_ptrs++] = entry_ptr;
203 /* this fills in a range with empty jset_entries: */
204 static void journal_entry_null_range(void *start, void *end)
206 struct jset_entry *entry;
208 for (entry = start; entry != end; entry = vstruct_next(entry))
209 memset(entry, 0, sizeof(*entry));
212 #define JOURNAL_ENTRY_REREAD 5
213 #define JOURNAL_ENTRY_NONE 6
214 #define JOURNAL_ENTRY_BAD 7
216 static void journal_entry_err_msg(struct printbuf *out,
219 struct jset_entry *entry)
221 prt_str(out, "invalid journal entry, version=");
222 bch2_version_to_text(out, version);
225 prt_str(out, " type=");
226 prt_str(out, bch2_jset_entry_types[entry->type]);
230 prt_printf(out, " in superblock");
233 prt_printf(out, " seq=%llu", le64_to_cpu(jset->seq));
236 prt_printf(out, " offset=%zi/%u",
237 (u64 *) entry - jset->_data,
238 le32_to_cpu(jset->u64s));
244 #define journal_entry_err(c, version, jset, entry, _err, msg, ...) \
246 struct printbuf _buf = PRINTBUF; \
248 journal_entry_err_msg(&_buf, version, jset, entry); \
249 prt_printf(&_buf, msg, ##__VA_ARGS__); \
251 switch (flags & BKEY_INVALID_WRITE) { \
253 mustfix_fsck_err(c, _err, "%s", _buf.buf); \
256 bch2_sb_error_count(c, BCH_FSCK_ERR_##_err); \
257 bch_err(c, "corrupt metadata before write: %s\n", _buf.buf);\
258 if (bch2_fs_inconsistent(c)) { \
259 ret = -BCH_ERR_fsck_errors_not_fixed; \
265 printbuf_exit(&_buf); \
269 #define journal_entry_err_on(cond, ...) \
270 ((cond) ? journal_entry_err(__VA_ARGS__) : false)
272 #define FSCK_DELETED_KEY 5
274 static int journal_validate_key(struct bch_fs *c,
276 struct jset_entry *entry,
277 unsigned level, enum btree_id btree_id,
279 unsigned version, int big_endian,
280 enum bkey_invalid_flags flags)
282 int write = flags & BKEY_INVALID_WRITE;
283 void *next = vstruct_next(entry);
284 struct printbuf buf = PRINTBUF;
287 if (journal_entry_err_on(!k->k.u64s,
288 c, version, jset, entry,
289 journal_entry_bkey_u64s_0,
291 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
292 journal_entry_null_range(vstruct_next(entry), next);
293 return FSCK_DELETED_KEY;
296 if (journal_entry_err_on((void *) bkey_next(k) >
297 (void *) vstruct_next(entry),
298 c, version, jset, entry,
299 journal_entry_bkey_past_end,
300 "extends past end of journal entry")) {
301 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
302 journal_entry_null_range(vstruct_next(entry), next);
303 return FSCK_DELETED_KEY;
306 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT,
307 c, version, jset, entry,
308 journal_entry_bkey_bad_format,
309 "bad format %u", k->k.format)) {
310 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
311 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
312 journal_entry_null_range(vstruct_next(entry), next);
313 return FSCK_DELETED_KEY;
317 bch2_bkey_compat(level, btree_id, version, big_endian,
318 write, NULL, bkey_to_packed(k));
320 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
321 __btree_node_type(level, btree_id), write, &buf)) {
322 printbuf_reset(&buf);
323 journal_entry_err_msg(&buf, version, jset, entry);
325 printbuf_indent_add(&buf, 2);
327 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
329 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
330 __btree_node_type(level, btree_id), write, &buf);
332 mustfix_fsck_err(c, journal_entry_bkey_invalid,
335 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
336 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
337 journal_entry_null_range(vstruct_next(entry), next);
340 return FSCK_DELETED_KEY;
344 bch2_bkey_compat(level, btree_id, version, big_endian,
345 write, NULL, bkey_to_packed(k));
351 static int journal_entry_btree_keys_validate(struct bch_fs *c,
353 struct jset_entry *entry,
354 unsigned version, int big_endian,
355 enum bkey_invalid_flags flags)
357 struct bkey_i *k = entry->start;
359 while (k != vstruct_last(entry)) {
360 int ret = journal_validate_key(c, jset, entry,
363 k, version, big_endian,
364 flags|BKEY_INVALID_JOURNAL);
365 if (ret == FSCK_DELETED_KEY)
374 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
375 struct jset_entry *entry)
380 jset_entry_for_each_key(entry, k) {
383 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
385 prt_printf(out, "btree=%s l=%u ", bch2_btree_id_str(entry->btree_id), entry->level);
386 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
391 static int journal_entry_btree_root_validate(struct bch_fs *c,
393 struct jset_entry *entry,
394 unsigned version, int big_endian,
395 enum bkey_invalid_flags flags)
397 struct bkey_i *k = entry->start;
400 if (journal_entry_err_on(!entry->u64s ||
401 le16_to_cpu(entry->u64s) != k->k.u64s,
402 c, version, jset, entry,
403 journal_entry_btree_root_bad_size,
404 "invalid btree root journal entry: wrong number of keys")) {
405 void *next = vstruct_next(entry);
407 * we don't want to null out this jset_entry,
408 * just the contents, so that later we can tell
409 * we were _supposed_ to have a btree root
412 journal_entry_null_range(vstruct_next(entry), next);
416 ret = journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
417 version, big_endian, flags);
418 if (ret == FSCK_DELETED_KEY)
424 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
425 struct jset_entry *entry)
427 journal_entry_btree_keys_to_text(out, c, entry);
430 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
432 struct jset_entry *entry,
433 unsigned version, int big_endian,
434 enum bkey_invalid_flags flags)
436 /* obsolete, don't care: */
440 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
441 struct jset_entry *entry)
445 static int journal_entry_blacklist_validate(struct bch_fs *c,
447 struct jset_entry *entry,
448 unsigned version, int big_endian,
449 enum bkey_invalid_flags flags)
453 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
454 c, version, jset, entry,
455 journal_entry_blacklist_bad_size,
456 "invalid journal seq blacklist entry: bad size")) {
457 journal_entry_null_range(entry, vstruct_next(entry));
463 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
464 struct jset_entry *entry)
466 struct jset_entry_blacklist *bl =
467 container_of(entry, struct jset_entry_blacklist, entry);
469 prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
472 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
474 struct jset_entry *entry,
475 unsigned version, int big_endian,
476 enum bkey_invalid_flags flags)
478 struct jset_entry_blacklist_v2 *bl_entry;
481 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
482 c, version, jset, entry,
483 journal_entry_blacklist_v2_bad_size,
484 "invalid journal seq blacklist entry: bad size")) {
485 journal_entry_null_range(entry, vstruct_next(entry));
489 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
491 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
492 le64_to_cpu(bl_entry->end),
493 c, version, jset, entry,
494 journal_entry_blacklist_v2_start_past_end,
495 "invalid journal seq blacklist entry: start > end")) {
496 journal_entry_null_range(entry, vstruct_next(entry));
503 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
504 struct jset_entry *entry)
506 struct jset_entry_blacklist_v2 *bl =
507 container_of(entry, struct jset_entry_blacklist_v2, entry);
509 prt_printf(out, "start=%llu end=%llu",
510 le64_to_cpu(bl->start),
511 le64_to_cpu(bl->end));
514 static int journal_entry_usage_validate(struct bch_fs *c,
516 struct jset_entry *entry,
517 unsigned version, int big_endian,
518 enum bkey_invalid_flags flags)
520 struct jset_entry_usage *u =
521 container_of(entry, struct jset_entry_usage, entry);
522 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
525 if (journal_entry_err_on(bytes < sizeof(*u),
526 c, version, jset, entry,
527 journal_entry_usage_bad_size,
528 "invalid journal entry usage: bad size")) {
529 journal_entry_null_range(entry, vstruct_next(entry));
537 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
538 struct jset_entry *entry)
540 struct jset_entry_usage *u =
541 container_of(entry, struct jset_entry_usage, entry);
543 prt_printf(out, "type=%s v=%llu",
544 bch2_fs_usage_types[u->entry.btree_id],
548 static int journal_entry_data_usage_validate(struct bch_fs *c,
550 struct jset_entry *entry,
551 unsigned version, int big_endian,
552 enum bkey_invalid_flags flags)
554 struct jset_entry_data_usage *u =
555 container_of(entry, struct jset_entry_data_usage, entry);
556 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
557 struct printbuf err = PRINTBUF;
560 if (journal_entry_err_on(bytes < sizeof(*u) ||
561 bytes < sizeof(*u) + u->r.nr_devs,
562 c, version, jset, entry,
563 journal_entry_data_usage_bad_size,
564 "invalid journal entry usage: bad size")) {
565 journal_entry_null_range(entry, vstruct_next(entry));
569 if (journal_entry_err_on(bch2_replicas_entry_validate(&u->r, c->disk_sb.sb, &err),
570 c, version, jset, entry,
571 journal_entry_data_usage_bad_size,
572 "invalid journal entry usage: %s", err.buf)) {
573 journal_entry_null_range(entry, vstruct_next(entry));
582 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
583 struct jset_entry *entry)
585 struct jset_entry_data_usage *u =
586 container_of(entry, struct jset_entry_data_usage, entry);
588 bch2_replicas_entry_to_text(out, &u->r);
589 prt_printf(out, "=%llu", le64_to_cpu(u->v));
592 static int journal_entry_clock_validate(struct bch_fs *c,
594 struct jset_entry *entry,
595 unsigned version, int big_endian,
596 enum bkey_invalid_flags flags)
598 struct jset_entry_clock *clock =
599 container_of(entry, struct jset_entry_clock, entry);
600 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
603 if (journal_entry_err_on(bytes != sizeof(*clock),
604 c, version, jset, entry,
605 journal_entry_clock_bad_size,
607 journal_entry_null_range(entry, vstruct_next(entry));
611 if (journal_entry_err_on(clock->rw > 1,
612 c, version, jset, entry,
613 journal_entry_clock_bad_rw,
615 journal_entry_null_range(entry, vstruct_next(entry));
623 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
624 struct jset_entry *entry)
626 struct jset_entry_clock *clock =
627 container_of(entry, struct jset_entry_clock, entry);
629 prt_printf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
632 static int journal_entry_dev_usage_validate(struct bch_fs *c,
634 struct jset_entry *entry,
635 unsigned version, int big_endian,
636 enum bkey_invalid_flags flags)
638 struct jset_entry_dev_usage *u =
639 container_of(entry, struct jset_entry_dev_usage, entry);
640 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
641 unsigned expected = sizeof(*u);
645 if (journal_entry_err_on(bytes < expected,
646 c, version, jset, entry,
647 journal_entry_dev_usage_bad_size,
648 "bad size (%u < %u)",
650 journal_entry_null_range(entry, vstruct_next(entry));
654 dev = le32_to_cpu(u->dev);
656 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
657 c, version, jset, entry,
658 journal_entry_dev_usage_bad_dev,
660 journal_entry_null_range(entry, vstruct_next(entry));
664 if (journal_entry_err_on(u->pad,
665 c, version, jset, entry,
666 journal_entry_dev_usage_bad_pad,
668 journal_entry_null_range(entry, vstruct_next(entry));
676 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
677 struct jset_entry *entry)
679 struct jset_entry_dev_usage *u =
680 container_of(entry, struct jset_entry_dev_usage, entry);
681 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
683 prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
685 for (i = 0; i < nr_types; i++) {
687 prt_printf(out, " %s", bch2_data_types[i]);
689 prt_printf(out, " (unknown data type %u)", i);
690 prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
691 le64_to_cpu(u->d[i].buckets),
692 le64_to_cpu(u->d[i].sectors),
693 le64_to_cpu(u->d[i].fragmented));
697 static int journal_entry_log_validate(struct bch_fs *c,
699 struct jset_entry *entry,
700 unsigned version, int big_endian,
701 enum bkey_invalid_flags flags)
706 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
707 struct jset_entry *entry)
709 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
710 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
712 prt_printf(out, "%.*s", bytes, l->d);
715 static int journal_entry_overwrite_validate(struct bch_fs *c,
717 struct jset_entry *entry,
718 unsigned version, int big_endian,
719 enum bkey_invalid_flags flags)
721 return journal_entry_btree_keys_validate(c, jset, entry,
722 version, big_endian, READ);
725 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
726 struct jset_entry *entry)
728 journal_entry_btree_keys_to_text(out, c, entry);
731 static int journal_entry_write_buffer_keys_validate(struct bch_fs *c,
733 struct jset_entry *entry,
734 unsigned version, int big_endian,
735 enum bkey_invalid_flags flags)
737 return journal_entry_btree_keys_validate(c, jset, entry,
738 version, big_endian, READ);
741 static void journal_entry_write_buffer_keys_to_text(struct printbuf *out, struct bch_fs *c,
742 struct jset_entry *entry)
744 journal_entry_btree_keys_to_text(out, c, entry);
747 struct jset_entry_ops {
748 int (*validate)(struct bch_fs *, struct jset *,
749 struct jset_entry *, unsigned, int,
750 enum bkey_invalid_flags);
751 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
754 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
756 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
757 .validate = journal_entry_##f##_validate, \
758 .to_text = journal_entry_##f##_to_text, \
760 BCH_JSET_ENTRY_TYPES()
764 int bch2_journal_entry_validate(struct bch_fs *c,
766 struct jset_entry *entry,
767 unsigned version, int big_endian,
768 enum bkey_invalid_flags flags)
770 return entry->type < BCH_JSET_ENTRY_NR
771 ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
772 version, big_endian, flags)
776 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
777 struct jset_entry *entry)
779 if (entry->type < BCH_JSET_ENTRY_NR) {
780 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
781 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
783 prt_printf(out, "(unknown type %u)", entry->type);
787 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
788 enum bkey_invalid_flags flags)
790 unsigned version = le32_to_cpu(jset->version);
793 vstruct_for_each(jset, entry) {
794 if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset),
795 c, version, jset, entry,
796 journal_entry_past_jset_end,
797 "journal entry extends past end of jset")) {
798 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
802 ret = bch2_journal_entry_validate(c, jset, entry,
803 version, JSET_BIG_ENDIAN(jset), flags);
811 static int jset_validate(struct bch_fs *c,
813 struct jset *jset, u64 sector,
814 enum bkey_invalid_flags flags)
819 if (le64_to_cpu(jset->magic) != jset_magic(c))
820 return JOURNAL_ENTRY_NONE;
822 version = le32_to_cpu(jset->version);
823 if (journal_entry_err_on(!bch2_version_compatible(version),
824 c, version, jset, NULL,
825 jset_unsupported_version,
826 "%s sector %llu seq %llu: incompatible journal entry version %u.%u",
827 ca ? ca->name : c->name,
828 sector, le64_to_cpu(jset->seq),
829 BCH_VERSION_MAJOR(version),
830 BCH_VERSION_MINOR(version))) {
831 /* don't try to continue: */
835 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
836 c, version, jset, NULL,
838 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
839 ca ? ca->name : c->name,
840 sector, le64_to_cpu(jset->seq),
841 JSET_CSUM_TYPE(jset)))
842 ret = JOURNAL_ENTRY_BAD;
844 /* last_seq is ignored when JSET_NO_FLUSH is true */
845 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
846 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
847 c, version, jset, NULL,
848 jset_last_seq_newer_than_seq,
849 "invalid journal entry: last_seq > seq (%llu > %llu)",
850 le64_to_cpu(jset->last_seq),
851 le64_to_cpu(jset->seq))) {
852 jset->last_seq = jset->seq;
853 return JOURNAL_ENTRY_BAD;
856 ret = jset_validate_entries(c, jset, flags);
861 static int jset_validate_early(struct bch_fs *c,
863 struct jset *jset, u64 sector,
864 unsigned bucket_sectors_left,
865 unsigned sectors_read)
867 size_t bytes = vstruct_bytes(jset);
869 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
872 if (le64_to_cpu(jset->magic) != jset_magic(c))
873 return JOURNAL_ENTRY_NONE;
875 version = le32_to_cpu(jset->version);
876 if (journal_entry_err_on(!bch2_version_compatible(version),
877 c, version, jset, NULL,
878 jset_unsupported_version,
879 "%s sector %llu seq %llu: unknown journal entry version %u.%u",
880 ca ? ca->name : c->name,
881 sector, le64_to_cpu(jset->seq),
882 BCH_VERSION_MAJOR(version),
883 BCH_VERSION_MINOR(version))) {
884 /* don't try to continue: */
888 if (bytes > (sectors_read << 9) &&
889 sectors_read < bucket_sectors_left)
890 return JOURNAL_ENTRY_REREAD;
892 if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
893 c, version, jset, NULL,
894 jset_past_bucket_end,
895 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
896 ca ? ca->name : c->name,
897 sector, le64_to_cpu(jset->seq), bytes))
898 le32_add_cpu(&jset->u64s,
899 -((bytes - (bucket_sectors_left << 9)) / 8));
904 struct journal_read_buf {
909 static int journal_read_buf_realloc(struct journal_read_buf *b,
914 /* the bios are sized for this many pages, max: */
915 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
916 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
918 new_size = roundup_pow_of_two(new_size);
919 n = kvpmalloc(new_size, GFP_KERNEL);
921 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
923 kvpfree(b->data, b->size);
929 static int journal_read_bucket(struct bch_dev *ca,
930 struct journal_read_buf *buf,
931 struct journal_list *jlist,
934 struct bch_fs *c = ca->fs;
935 struct journal_device *ja = &ca->journal;
936 struct jset *j = NULL;
937 unsigned sectors, sectors_read = 0;
938 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
939 end = offset + ca->mi.bucket_size;
940 bool saw_bad = false, csum_good;
941 struct printbuf err = PRINTBUF;
944 pr_debug("reading %u", bucket);
946 while (offset < end) {
951 sectors_read = min_t(unsigned,
952 end - offset, buf->size >> 9);
953 nr_bvecs = buf_pages(buf->data, sectors_read << 9);
955 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
956 bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
958 bio->bi_iter.bi_sector = offset;
959 bch2_bio_map(bio, buf->data, sectors_read << 9);
961 ret = submit_bio_wait(bio);
964 if (bch2_dev_io_err_on(ret, ca, BCH_MEMBER_ERROR_read,
965 "journal read error: sector %llu",
967 bch2_meta_read_fault("journal")) {
969 * We don't error out of the recovery process
970 * here, since the relevant journal entry may be
971 * found on a different device, and missing or
972 * no journal entries will be handled later
980 ret = jset_validate_early(c, ca, j, offset,
981 end - offset, sectors_read);
984 sectors = vstruct_sectors(j, c->block_bits);
986 case JOURNAL_ENTRY_REREAD:
987 if (vstruct_bytes(j) > buf->size) {
988 ret = journal_read_buf_realloc(buf,
994 case JOURNAL_ENTRY_NONE:
998 * On checksum error we don't really trust the size
999 * field of the journal entry we read, so try reading
1000 * again at next block boundary:
1002 sectors = block_sectors(c);
1009 * This happens sometimes if we don't have discards on -
1010 * when we've partially overwritten a bucket with new
1011 * journal entries. We don't need the rest of the
1014 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
1017 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
1019 enum bch_csum_type csum_type = JSET_CSUM_TYPE(j);
1020 struct bch_csum csum;
1021 csum_good = jset_csum_good(c, j, &csum);
1023 if (bch2_dev_io_err_on(!csum_good, ca, BCH_MEMBER_ERROR_checksum,
1025 (printbuf_reset(&err),
1026 prt_str(&err, "journal "),
1027 bch2_csum_err_msg(&err, csum_type, j->csum, csum),
1031 ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
1033 vstruct_end(j) - (void *) j->encrypted_start);
1034 bch2_fs_fatal_err_on(ret, c,
1035 "error decrypting journal entry: %s",
1038 mutex_lock(&jlist->lock);
1039 ret = journal_entry_add(c, ca, (struct journal_ptr) {
1040 .csum_good = csum_good,
1043 .bucket_offset = offset -
1044 bucket_to_sector(ca, ja->buckets[bucket]),
1047 mutex_unlock(&jlist->lock);
1050 case JOURNAL_ENTRY_ADD_OK:
1052 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
1060 sectors_read -= sectors;
1061 j = ((void *) j) + (sectors << 9);
1067 printbuf_exit(&err);
1071 static CLOSURE_CALLBACK(bch2_journal_read_device)
1073 closure_type(ja, struct journal_device, read);
1074 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
1075 struct bch_fs *c = ca->fs;
1076 struct journal_list *jlist =
1077 container_of(cl->parent, struct journal_list, cl);
1078 struct journal_replay *r, **_r;
1079 struct genradix_iter iter;
1080 struct journal_read_buf buf = { NULL, 0 };
1087 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1091 pr_debug("%u journal buckets", ja->nr);
1093 for (i = 0; i < ja->nr; i++) {
1094 ret = journal_read_bucket(ca, &buf, jlist, i);
1099 ja->sectors_free = ca->mi.bucket_size;
1101 mutex_lock(&jlist->lock);
1102 genradix_for_each_reverse(&c->journal_entries, iter, _r) {
1108 for (i = 0; i < r->nr_ptrs; i++) {
1109 if (r->ptrs[i].dev == ca->dev_idx) {
1110 unsigned wrote = bucket_remainder(ca, r->ptrs[i].sector) +
1111 vstruct_sectors(&r->j, c->block_bits);
1113 ja->cur_idx = r->ptrs[i].bucket;
1114 ja->sectors_free = ca->mi.bucket_size - wrote;
1120 mutex_unlock(&jlist->lock);
1122 if (ja->bucket_seq[ja->cur_idx] &&
1123 ja->sectors_free == ca->mi.bucket_size) {
1126 * Debug code for ZNS support, where we (probably) want to be
1127 * correlated where we stopped in the journal to the zone write
1130 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1131 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1132 for (i = 0; i < 3; i++) {
1133 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
1135 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1138 ja->sectors_free = 0;
1142 * Set dirty_idx to indicate the entire journal is full and needs to be
1143 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1144 * pinned when it first runs:
1146 ja->discard_idx = ja->dirty_idx_ondisk =
1147 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1149 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1150 kvpfree(buf.data, buf.size);
1151 percpu_ref_put(&ca->io_ref);
1155 mutex_lock(&jlist->lock);
1157 mutex_unlock(&jlist->lock);
1161 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1162 struct journal_replay *j)
1166 for (i = 0; i < j->nr_ptrs; i++) {
1167 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1170 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1173 prt_printf(out, " ");
1174 prt_printf(out, "%u:%u:%u (sector %llu)",
1177 j->ptrs[i].bucket_offset,
1182 int bch2_journal_read(struct bch_fs *c,
1187 struct journal_list jlist;
1188 struct journal_replay *i, **_i, *prev = NULL;
1189 struct genradix_iter radix_iter;
1190 struct printbuf buf = PRINTBUF;
1191 bool degraded = false, last_write_torn = false;
1195 closure_init_stack(&jlist.cl);
1196 mutex_init(&jlist.lock);
1200 for_each_member_device(c, ca) {
1201 if (!c->opts.fsck &&
1202 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1205 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1206 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1207 percpu_ref_tryget(&ca->io_ref))
1208 closure_call(&ca->journal.read,
1209 bch2_journal_read_device,
1216 closure_sync(&jlist.cl);
1226 * Find most recent flush entry, and ignore newer non flush entries -
1227 * those entries will be blacklisted:
1229 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1230 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
1234 if (!i || i->ignore)
1238 *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1240 if (JSET_NO_FLUSH(&i->j)) {
1245 if (!last_write_torn && !i->csum_good) {
1246 last_write_torn = true;
1251 if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1252 c, le32_to_cpu(i->j.version), &i->j, NULL,
1253 jset_last_seq_newer_than_seq,
1254 "invalid journal entry: last_seq > seq (%llu > %llu)",
1255 le64_to_cpu(i->j.last_seq),
1256 le64_to_cpu(i->j.seq)))
1257 i->j.last_seq = i->j.seq;
1259 *last_seq = le64_to_cpu(i->j.last_seq);
1260 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1265 bch_info(c, "journal read done, but no entries found");
1270 fsck_err(c, dirty_but_no_journal_entries_post_drop_nonflushes,
1271 "journal read done, but no entries found after dropping non-flushes");
1275 bch_info(c, "journal read done, replaying entries %llu-%llu",
1276 *last_seq, *blacklist_seq - 1);
1278 if (*start_seq != *blacklist_seq)
1279 bch_info(c, "dropped unflushed entries %llu-%llu",
1280 *blacklist_seq, *start_seq - 1);
1282 /* Drop blacklisted entries and entries older than last_seq: */
1283 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1286 if (!i || i->ignore)
1289 seq = le64_to_cpu(i->j.seq);
1290 if (seq < *last_seq) {
1291 journal_replay_free(c, i);
1295 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1296 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1297 jset_seq_blacklisted,
1298 "found blacklisted journal entry %llu", seq);
1303 /* Check for missing entries: */
1305 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1308 if (!i || i->ignore)
1311 BUG_ON(seq > le64_to_cpu(i->j.seq));
1313 while (seq < le64_to_cpu(i->j.seq)) {
1314 u64 missing_start, missing_end;
1315 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1317 while (seq < le64_to_cpu(i->j.seq) &&
1318 bch2_journal_seq_is_blacklisted(c, seq, false))
1321 if (seq == le64_to_cpu(i->j.seq))
1324 missing_start = seq;
1326 while (seq < le64_to_cpu(i->j.seq) &&
1327 !bch2_journal_seq_is_blacklisted(c, seq, false))
1331 bch2_journal_ptrs_to_text(&buf1, c, prev);
1332 prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1334 prt_printf(&buf1, "(none)");
1335 bch2_journal_ptrs_to_text(&buf2, c, i);
1337 missing_end = seq - 1;
1338 fsck_err(c, journal_entries_missing,
1339 "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1342 missing_start, missing_end,
1343 *last_seq, *blacklist_seq - 1,
1344 buf1.buf, buf2.buf);
1346 printbuf_exit(&buf1);
1347 printbuf_exit(&buf2);
1354 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1355 struct bch_replicas_padded replicas = {
1356 .e.data_type = BCH_DATA_journal,
1362 if (!i || i->ignore)
1365 for (ptr = 0; ptr < i->nr_ptrs; ptr++) {
1366 struct bch_dev *ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
1368 if (!i->ptrs[ptr].csum_good)
1369 bch_err_dev_offset(ca, i->ptrs[ptr].sector,
1370 "invalid journal checksum, seq %llu%s",
1371 le64_to_cpu(i->j.seq),
1372 i->csum_good ? " (had good copy on another device)" : "");
1375 ret = jset_validate(c,
1376 bch_dev_bkey_exists(c, i->ptrs[0].dev),
1383 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1384 replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1386 bch2_replicas_entry_sort(&replicas.e);
1388 printbuf_reset(&buf);
1389 bch2_replicas_entry_to_text(&buf, &replicas.e);
1392 !bch2_replicas_marked(c, &replicas.e) &&
1393 (le64_to_cpu(i->j.seq) == *last_seq ||
1394 fsck_err(c, journal_entry_replicas_not_marked,
1395 "superblock not marked as containing replicas for journal entry %llu\n %s",
1396 le64_to_cpu(i->j.seq), buf.buf))) {
1397 ret = bch2_mark_replicas(c, &replicas.e);
1404 printbuf_exit(&buf);
1408 /* journal write: */
1410 static void __journal_write_alloc(struct journal *j,
1411 struct journal_buf *w,
1412 struct dev_alloc_list *devs_sorted,
1415 unsigned replicas_want)
1417 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1418 struct journal_device *ja;
1422 if (*replicas >= replicas_want)
1425 for (i = 0; i < devs_sorted->nr; i++) {
1426 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1433 * Check that we can use this device, and aren't already using
1436 if (!ca->mi.durability ||
1437 ca->mi.state != BCH_MEMBER_STATE_rw ||
1439 bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) ||
1440 sectors > ja->sectors_free)
1443 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1445 bch2_bkey_append_ptr(&w->key,
1446 (struct bch_extent_ptr) {
1447 .offset = bucket_to_sector(ca,
1448 ja->buckets[ja->cur_idx]) +
1449 ca->mi.bucket_size -
1454 ja->sectors_free -= sectors;
1455 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1457 *replicas += ca->mi.durability;
1459 if (*replicas >= replicas_want)
1465 * journal_write_alloc - decide where to write next journal entry
1467 * @j: journal object
1468 * @w: journal buf (entry to be written)
1470 * Returns: 0 on success, or -EROFS on failure
1472 static int journal_write_alloc(struct journal *j, struct journal_buf *w)
1474 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1475 struct bch_devs_mask devs;
1476 struct journal_device *ja;
1478 struct dev_alloc_list devs_sorted;
1479 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1480 unsigned target = c->opts.metadata_target ?:
1481 c->opts.foreground_target;
1482 unsigned i, replicas = 0, replicas_want =
1483 READ_ONCE(c->opts.metadata_replicas);
1487 devs = target_rw_devs(c, BCH_DATA_journal, target);
1489 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1491 __journal_write_alloc(j, w, &devs_sorted,
1492 sectors, &replicas, replicas_want);
1494 if (replicas >= replicas_want)
1497 for (i = 0; i < devs_sorted.nr; i++) {
1498 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1504 if (sectors > ja->sectors_free &&
1505 sectors <= ca->mi.bucket_size &&
1506 bch2_journal_dev_buckets_available(j, ja,
1507 journal_space_discarded)) {
1508 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1509 ja->sectors_free = ca->mi.bucket_size;
1512 * ja->bucket_seq[ja->cur_idx] must always have
1513 * something sensible:
1515 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1519 __journal_write_alloc(j, w, &devs_sorted,
1520 sectors, &replicas, replicas_want);
1522 if (replicas < replicas_want && target) {
1523 /* Retry from all devices: */
1530 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1532 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1535 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1537 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1539 /* we aren't holding j->lock: */
1540 unsigned new_size = READ_ONCE(j->buf_size_want);
1543 if (buf->buf_size >= new_size)
1546 size_t btree_write_buffer_size = new_size / 64;
1548 if (bch2_btree_write_buffer_resize(c, btree_write_buffer_size))
1551 new_buf = kvpmalloc(new_size, GFP_NOFS|__GFP_NOWARN);
1555 memcpy(new_buf, buf->data, buf->buf_size);
1557 spin_lock(&j->lock);
1558 swap(buf->data, new_buf);
1559 swap(buf->buf_size, new_size);
1560 spin_unlock(&j->lock);
1562 kvpfree(new_buf, new_size);
1565 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1567 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1570 static CLOSURE_CALLBACK(journal_write_done)
1572 closure_type(j, struct journal, io);
1573 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1574 struct journal_buf *w = journal_last_unwritten_buf(j);
1575 struct bch_replicas_padded replicas;
1576 union journal_res_state old, new;
1580 bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1581 ? j->flush_write_time
1582 : j->noflush_write_time, j->write_start_time);
1584 if (!w->devs_written.nr) {
1585 bch_err(c, "unable to write journal to sufficient devices");
1588 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1590 if (bch2_mark_replicas(c, &replicas.e))
1595 bch2_fatal_error(c);
1597 spin_lock(&j->lock);
1598 seq = le64_to_cpu(w->data->seq);
1600 if (seq >= j->pin.front)
1601 journal_seq_pin(j, seq)->devs = w->devs_written;
1604 if (!JSET_NO_FLUSH(w->data)) {
1605 j->flushed_seq_ondisk = seq;
1606 j->last_seq_ondisk = w->last_seq;
1608 bch2_do_discards(c);
1609 closure_wake_up(&c->freelist_wait);
1611 bch2_reset_alloc_cursors(c);
1613 } else if (!j->err_seq || seq < j->err_seq)
1616 j->seq_ondisk = seq;
1619 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1622 * Must come before signaling write completion, for
1623 * bch2_fs_journal_stop():
1625 if (j->watermark != BCH_WATERMARK_stripe)
1626 journal_reclaim_kick(&c->journal);
1628 /* also must come before signalling write completion: */
1629 closure_debug_destroy(cl);
1631 v = atomic64_read(&j->reservations.counter);
1634 BUG_ON(journal_state_count(new, new.unwritten_idx));
1636 new.unwritten_idx++;
1637 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1638 old.v, new.v)) != old.v);
1640 bch2_journal_reclaim_fast(j);
1641 bch2_journal_space_available(j);
1643 track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight],
1644 &j->max_in_flight_start, false);
1646 closure_wake_up(&w->wait);
1649 if (!journal_state_count(new, new.unwritten_idx) &&
1650 journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1651 spin_unlock(&j->lock);
1652 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1653 } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1654 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1655 struct journal_buf *buf = journal_cur_buf(j);
1656 long delta = buf->expires - jiffies;
1659 * We don't close a journal entry to write it while there's
1660 * previous entries still in flight - the current journal entry
1661 * might want to be written now:
1664 spin_unlock(&j->lock);
1665 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1667 spin_unlock(&j->lock);
1671 static void journal_write_endio(struct bio *bio)
1673 struct bch_dev *ca = bio->bi_private;
1674 struct journal *j = &ca->fs->journal;
1675 struct journal_buf *w = journal_last_unwritten_buf(j);
1676 unsigned long flags;
1678 if (bch2_dev_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_write,
1679 "error writing journal entry %llu: %s",
1680 le64_to_cpu(w->data->seq),
1681 bch2_blk_status_to_str(bio->bi_status)) ||
1682 bch2_meta_write_fault("journal")) {
1683 spin_lock_irqsave(&j->err_lock, flags);
1684 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1685 spin_unlock_irqrestore(&j->err_lock, flags);
1688 closure_put(&j->io);
1689 percpu_ref_put(&ca->io_ref);
1692 static CLOSURE_CALLBACK(do_journal_write)
1694 closure_type(j, struct journal, io);
1695 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1697 struct journal_buf *w = journal_last_unwritten_buf(j);
1699 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1701 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1702 ca = bch_dev_bkey_exists(c, ptr->dev);
1703 if (!percpu_ref_tryget(&ca->io_ref)) {
1705 bch_err(c, "missing device for journal write\n");
1709 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1712 bio = ca->journal.bio;
1713 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1714 bio->bi_iter.bi_sector = ptr->offset;
1715 bio->bi_end_io = journal_write_endio;
1716 bio->bi_private = ca;
1718 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1719 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1721 if (!JSET_NO_FLUSH(w->data))
1722 bio->bi_opf |= REQ_FUA;
1723 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1724 bio->bi_opf |= REQ_PREFLUSH;
1726 bch2_bio_map(bio, w->data, sectors << 9);
1728 trace_and_count(c, journal_write, bio);
1729 closure_bio_submit(bio, cl);
1731 ca->journal.bucket_seq[ca->journal.cur_idx] =
1732 le64_to_cpu(w->data->seq);
1735 continue_at(cl, journal_write_done, c->io_complete_wq);
1738 static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w)
1740 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1741 struct jset_entry *start, *end;
1742 struct jset *jset = w->data;
1743 struct journal_keys_to_wb wb = { NULL };
1744 unsigned sectors, bytes, u64s;
1745 unsigned long btree_roots_have = 0;
1746 bool validate_before_checksum = false;
1747 u64 seq = le64_to_cpu(jset->seq);
1751 * Simple compaction, dropping empty jset_entries (from journal
1752 * reservations that weren't fully used) and merging jset_entries that
1755 * If we wanted to be really fancy here, we could sort all the keys in
1756 * the jset and drop keys that were overwritten - probably not worth it:
1758 vstruct_for_each(jset, i) {
1759 unsigned u64s = le16_to_cpu(i->u64s);
1766 * New btree roots are set by journalling them; when the journal
1767 * entry gets written we have to propagate them to
1770 * But, every journal entry we write has to contain all the
1771 * btree roots (at least for now); so after we copy btree roots
1772 * to c->btree_roots we have to get any missing btree roots and
1773 * add them to this journal entry:
1776 case BCH_JSET_ENTRY_btree_root:
1777 bch2_journal_entry_to_btree_root(c, i);
1778 __set_bit(i->btree_id, &btree_roots_have);
1780 case BCH_JSET_ENTRY_write_buffer_keys:
1781 EBUG_ON(!w->need_flush_to_write_buffer);
1784 bch2_journal_keys_to_write_buffer_start(c, &wb, seq);
1787 jset_entry_for_each_key(i, k) {
1788 ret = bch2_journal_key_to_wb(c, &wb, i->btree_id, k);
1790 bch2_fs_fatal_error(c, "-ENOMEM flushing journal keys to btree write buffer");
1791 bch2_journal_keys_to_write_buffer_end(c, &wb);
1795 i->type = BCH_JSET_ENTRY_btree_keys;
1801 bch2_journal_keys_to_write_buffer_end(c, &wb);
1802 w->need_flush_to_write_buffer = false;
1804 start = end = vstruct_last(jset);
1806 end = bch2_btree_roots_to_journal_entries(c, end, btree_roots_have);
1808 bch2_journal_super_entries_add_common(c, &end, seq);
1809 u64s = (u64 *) end - (u64 *) start;
1810 BUG_ON(u64s > j->entry_u64s_reserved);
1812 le32_add_cpu(&jset->u64s, u64s);
1814 sectors = vstruct_sectors(jset, c->block_bits);
1815 bytes = vstruct_bytes(jset);
1817 if (sectors > w->sectors) {
1818 bch2_fs_fatal_error(c, "aieeee! journal write overran available space, %zu > %u (extra %u reserved %u/%u)",
1819 vstruct_bytes(jset), w->sectors << 9,
1820 u64s, w->u64s_reserved, j->entry_u64s_reserved);
1824 jset->magic = cpu_to_le64(jset_magic(c));
1825 jset->version = cpu_to_le32(c->sb.version);
1827 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1828 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1830 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1831 j->last_empty_seq = seq;
1833 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1834 validate_before_checksum = true;
1836 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1837 validate_before_checksum = true;
1839 if (validate_before_checksum &&
1840 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1843 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1844 jset->encrypted_start,
1845 vstruct_end(jset) - (void *) jset->encrypted_start);
1846 if (bch2_fs_fatal_err_on(ret, c,
1847 "error decrypting journal entry: %i", ret))
1850 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1851 journal_nonce(jset), jset);
1853 if (!validate_before_checksum &&
1854 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1857 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1861 static int bch2_journal_write_pick_flush(struct journal *j, struct journal_buf *w)
1863 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1864 int error = bch2_journal_error(j);
1867 * If the journal is in an error state - we did an emergency shutdown -
1868 * we prefer to continue doing journal writes. We just mark them as
1869 * noflush so they'll never be used, but they'll still be visible by the
1870 * list_journal tool - this helps in debugging.
1872 * There's a caveat: the first journal write after marking the
1873 * superblock dirty must always be a flush write, because on startup
1874 * from a clean shutdown we didn't necessarily read the journal and the
1875 * new journal write might overwrite whatever was in the journal
1876 * previously - we can't leave the journal without any flush writes in
1879 * So if we're in an error state, and we're still starting up, we don't
1880 * write anything at all.
1882 if (error && test_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags))
1888 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1889 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags))) {
1891 SET_JSET_NO_FLUSH(w->data, true);
1892 w->data->last_seq = 0;
1895 j->nr_noflush_writes++;
1897 j->last_flush_write = jiffies;
1898 j->nr_flush_writes++;
1899 clear_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags);
1905 CLOSURE_CALLBACK(bch2_journal_write)
1907 closure_type(j, struct journal, io);
1908 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1909 struct journal_buf *w = journal_last_unwritten_buf(j);
1910 struct bch_replicas_padded replicas;
1912 struct printbuf journal_debug_buf = PRINTBUF;
1913 unsigned nr_rw_members = 0;
1916 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1918 j->write_start_time = local_clock();
1920 spin_lock(&j->lock);
1921 ret = bch2_journal_write_pick_flush(j, w);
1922 spin_unlock(&j->lock);
1926 mutex_lock(&j->buf_lock);
1927 journal_buf_realloc(j, w);
1929 ret = bch2_journal_write_prep(j, w);
1930 mutex_unlock(&j->buf_lock);
1934 j->entry_bytes_written += vstruct_bytes(w->data);
1937 spin_lock(&j->lock);
1938 ret = journal_write_alloc(j, w);
1939 if (!ret || !j->can_discard)
1942 spin_unlock(&j->lock);
1943 bch2_journal_do_discards(j);
1947 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1948 spin_unlock(&j->lock);
1949 bch_err(c, "Unable to allocate journal write:\n%s",
1950 journal_debug_buf.buf);
1951 printbuf_exit(&journal_debug_buf);
1956 * write is allocated, no longer need to account for it in
1957 * bch2_journal_space_available():
1962 * journal entry has been compacted and allocated, recalculate space
1965 bch2_journal_space_available(j);
1966 spin_unlock(&j->lock);
1968 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1970 if (c->opts.nochanges)
1973 for_each_rw_member(c, ca)
1976 if (nr_rw_members > 1)
1977 w->separate_flush = true;
1980 * Mark journal replicas before we submit the write to guarantee
1981 * recovery will find the journal entries after a crash.
1983 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1985 ret = bch2_mark_replicas(c, &replicas.e);
1989 if (!JSET_NO_FLUSH(w->data) && w->separate_flush) {
1990 for_each_rw_member(c, ca) {
1991 percpu_ref_get(&ca->io_ref);
1993 bio = ca->journal.bio;
1994 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_FLUSH);
1995 bio->bi_end_io = journal_write_endio;
1996 bio->bi_private = ca;
1997 closure_bio_submit(bio, cl);
2001 continue_at(cl, do_journal_write, c->io_complete_wq);
2004 continue_at(cl, journal_write_done, c->io_complete_wq);
2007 bch2_fatal_error(c);
2008 continue_at(cl, journal_write_done, c->io_complete_wq);