1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
6 #include "btree_update_interior.h"
9 #include "disk_groups.h"
12 #include "journal_io.h"
13 #include "journal_reclaim.h"
14 #include "journal_seq_blacklist.h"
19 static struct nonce journal_nonce(const struct jset *jset)
21 return (struct nonce) {{
23 [1] = ((__le32 *) &jset->seq)[0],
24 [2] = ((__le32 *) &jset->seq)[1],
25 [3] = BCH_NONCE_JOURNAL,
29 static bool jset_csum_good(struct bch_fs *c, struct jset *j)
31 return bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j)) &&
32 !bch2_crc_cmp(j->csum,
33 csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j));
36 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
38 return (seq - c->journal_entries_base_seq) & (~0U >> 1);
41 static void __journal_replay_free(struct bch_fs *c,
42 struct journal_replay *i)
44 struct journal_replay **p =
45 genradix_ptr(&c->journal_entries,
46 journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
50 kvpfree(i, offsetof(struct journal_replay, j) +
51 vstruct_bytes(&i->j));
54 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
58 if (!c->opts.read_entire_journal)
59 __journal_replay_free(c, i);
69 #define JOURNAL_ENTRY_ADD_OK 0
70 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
73 * Given a journal entry we just read, add it to the list of journal entries to
76 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
77 struct journal_ptr entry_ptr,
78 struct journal_list *jlist, struct jset *j)
80 struct genradix_iter iter;
81 struct journal_replay **_i, *i, *dup;
82 struct journal_ptr *ptr;
83 size_t bytes = vstruct_bytes(j);
84 u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
85 int ret = JOURNAL_ENTRY_ADD_OK;
87 /* Is this entry older than the range we need? */
88 if (!c->opts.read_entire_journal &&
89 le64_to_cpu(j->seq) < jlist->last_seq)
90 return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
93 * genradixes are indexed by a ulong, not a u64, so we can't index them
94 * by sequence number directly: Assume instead that they will all fall
95 * within the range of +-2billion of the filrst one we find.
97 if (!c->journal_entries_base_seq)
98 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
100 /* Drop entries we don't need anymore */
101 if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
102 genradix_for_each_from(&c->journal_entries, iter, _i,
103 journal_entry_radix_idx(c, jlist->last_seq)) {
109 if (le64_to_cpu(i->j.seq) >= last_seq)
111 journal_replay_free(c, i);
115 jlist->last_seq = max(jlist->last_seq, last_seq);
117 _i = genradix_ptr_alloc(&c->journal_entries,
118 journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
121 return -BCH_ERR_ENOMEM_journal_entry_add;
124 * Duplicate journal entries? If so we want the one that didn't have a
129 if (bytes == vstruct_bytes(&dup->j) &&
130 !memcmp(j, &dup->j, bytes)) {
135 if (!entry_ptr.csum_good) {
143 fsck_err(c, journal_entry_replicas_data_mismatch,
144 "found duplicate but non identical journal entries (seq %llu)",
145 le64_to_cpu(j->seq));
150 i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
152 return -BCH_ERR_ENOMEM_journal_entry_add;
155 i->csum_good = entry_ptr.csum_good;
157 unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct");
158 i->ptrs[i->nr_ptrs++] = entry_ptr;
161 if (dup->nr_ptrs >= ARRAY_SIZE(dup->ptrs)) {
162 bch_err(c, "found too many copies of journal entry %llu",
163 le64_to_cpu(i->j.seq));
164 dup->nr_ptrs = ARRAY_SIZE(dup->ptrs) - 1;
167 /* The first ptr should represent the jset we kept: */
168 memcpy(i->ptrs + i->nr_ptrs,
170 sizeof(dup->ptrs[0]) * dup->nr_ptrs);
171 i->nr_ptrs += dup->nr_ptrs;
172 __journal_replay_free(c, dup);
178 for (ptr = i->ptrs; ptr < i->ptrs + i->nr_ptrs; ptr++) {
179 if (ptr->dev == ca->dev_idx) {
180 bch_err(c, "duplicate journal entry %llu on same device",
181 le64_to_cpu(i->j.seq));
186 if (i->nr_ptrs >= ARRAY_SIZE(i->ptrs)) {
187 bch_err(c, "found too many copies of journal entry %llu",
188 le64_to_cpu(i->j.seq));
192 i->ptrs[i->nr_ptrs++] = entry_ptr;
198 /* this fills in a range with empty jset_entries: */
199 static void journal_entry_null_range(void *start, void *end)
201 struct jset_entry *entry;
203 for (entry = start; entry != end; entry = vstruct_next(entry))
204 memset(entry, 0, sizeof(*entry));
207 #define JOURNAL_ENTRY_REREAD 5
208 #define JOURNAL_ENTRY_NONE 6
209 #define JOURNAL_ENTRY_BAD 7
211 static void journal_entry_err_msg(struct printbuf *out,
214 struct jset_entry *entry)
216 prt_str(out, "invalid journal entry, version=");
217 bch2_version_to_text(out, version);
220 prt_str(out, " type=");
221 prt_str(out, bch2_jset_entry_types[entry->type]);
225 prt_printf(out, " in superblock");
228 prt_printf(out, " seq=%llu", le64_to_cpu(jset->seq));
231 prt_printf(out, " offset=%zi/%u",
232 (u64 *) entry - jset->_data,
233 le32_to_cpu(jset->u64s));
239 #define journal_entry_err(c, version, jset, entry, _err, msg, ...) \
241 struct printbuf _buf = PRINTBUF; \
243 journal_entry_err_msg(&_buf, version, jset, entry); \
244 prt_printf(&_buf, msg, ##__VA_ARGS__); \
246 switch (flags & BKEY_INVALID_WRITE) { \
248 mustfix_fsck_err(c, _err, "%s", _buf.buf); \
251 bch2_sb_error_count(c, BCH_FSCK_ERR_##_err); \
252 bch_err(c, "corrupt metadata before write: %s\n", _buf.buf);\
253 if (bch2_fs_inconsistent(c)) { \
254 ret = -BCH_ERR_fsck_errors_not_fixed; \
260 printbuf_exit(&_buf); \
264 #define journal_entry_err_on(cond, ...) \
265 ((cond) ? journal_entry_err(__VA_ARGS__) : false)
267 #define FSCK_DELETED_KEY 5
269 static int journal_validate_key(struct bch_fs *c,
271 struct jset_entry *entry,
272 unsigned level, enum btree_id btree_id,
274 unsigned version, int big_endian,
275 enum bkey_invalid_flags flags)
277 int write = flags & BKEY_INVALID_WRITE;
278 void *next = vstruct_next(entry);
279 struct printbuf buf = PRINTBUF;
282 if (journal_entry_err_on(!k->k.u64s,
283 c, version, jset, entry,
284 journal_entry_bkey_u64s_0,
286 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
287 journal_entry_null_range(vstruct_next(entry), next);
288 return FSCK_DELETED_KEY;
291 if (journal_entry_err_on((void *) bkey_next(k) >
292 (void *) vstruct_next(entry),
293 c, version, jset, entry,
294 journal_entry_bkey_past_end,
295 "extends past end of journal entry")) {
296 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
297 journal_entry_null_range(vstruct_next(entry), next);
298 return FSCK_DELETED_KEY;
301 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT,
302 c, version, jset, entry,
303 journal_entry_bkey_bad_format,
304 "bad format %u", k->k.format)) {
305 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
306 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
307 journal_entry_null_range(vstruct_next(entry), next);
308 return FSCK_DELETED_KEY;
312 bch2_bkey_compat(level, btree_id, version, big_endian,
313 write, NULL, bkey_to_packed(k));
315 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
316 __btree_node_type(level, btree_id), write, &buf)) {
317 printbuf_reset(&buf);
318 journal_entry_err_msg(&buf, version, jset, entry);
320 printbuf_indent_add(&buf, 2);
322 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
324 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
325 __btree_node_type(level, btree_id), write, &buf);
327 mustfix_fsck_err(c, journal_entry_bkey_invalid,
330 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
331 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
332 journal_entry_null_range(vstruct_next(entry), next);
335 return FSCK_DELETED_KEY;
339 bch2_bkey_compat(level, btree_id, version, big_endian,
340 write, NULL, bkey_to_packed(k));
346 static int journal_entry_btree_keys_validate(struct bch_fs *c,
348 struct jset_entry *entry,
349 unsigned version, int big_endian,
350 enum bkey_invalid_flags flags)
352 struct bkey_i *k = entry->start;
354 while (k != vstruct_last(entry)) {
355 int ret = journal_validate_key(c, jset, entry,
358 k, version, big_endian,
359 flags|BKEY_INVALID_JOURNAL);
360 if (ret == FSCK_DELETED_KEY)
369 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
370 struct jset_entry *entry)
375 jset_entry_for_each_key(entry, k) {
378 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
380 prt_printf(out, "btree=%s l=%u ", bch2_btree_id_str(entry->btree_id), entry->level);
381 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
386 static int journal_entry_btree_root_validate(struct bch_fs *c,
388 struct jset_entry *entry,
389 unsigned version, int big_endian,
390 enum bkey_invalid_flags flags)
392 struct bkey_i *k = entry->start;
395 if (journal_entry_err_on(!entry->u64s ||
396 le16_to_cpu(entry->u64s) != k->k.u64s,
397 c, version, jset, entry,
398 journal_entry_btree_root_bad_size,
399 "invalid btree root journal entry: wrong number of keys")) {
400 void *next = vstruct_next(entry);
402 * we don't want to null out this jset_entry,
403 * just the contents, so that later we can tell
404 * we were _supposed_ to have a btree root
407 journal_entry_null_range(vstruct_next(entry), next);
411 return journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
412 version, big_endian, flags);
417 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
418 struct jset_entry *entry)
420 journal_entry_btree_keys_to_text(out, c, entry);
423 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
425 struct jset_entry *entry,
426 unsigned version, int big_endian,
427 enum bkey_invalid_flags flags)
429 /* obsolete, don't care: */
433 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
434 struct jset_entry *entry)
438 static int journal_entry_blacklist_validate(struct bch_fs *c,
440 struct jset_entry *entry,
441 unsigned version, int big_endian,
442 enum bkey_invalid_flags flags)
446 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
447 c, version, jset, entry,
448 journal_entry_blacklist_bad_size,
449 "invalid journal seq blacklist entry: bad size")) {
450 journal_entry_null_range(entry, vstruct_next(entry));
456 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
457 struct jset_entry *entry)
459 struct jset_entry_blacklist *bl =
460 container_of(entry, struct jset_entry_blacklist, entry);
462 prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
465 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
467 struct jset_entry *entry,
468 unsigned version, int big_endian,
469 enum bkey_invalid_flags flags)
471 struct jset_entry_blacklist_v2 *bl_entry;
474 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
475 c, version, jset, entry,
476 journal_entry_blacklist_v2_bad_size,
477 "invalid journal seq blacklist entry: bad size")) {
478 journal_entry_null_range(entry, vstruct_next(entry));
482 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
484 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
485 le64_to_cpu(bl_entry->end),
486 c, version, jset, entry,
487 journal_entry_blacklist_v2_start_past_end,
488 "invalid journal seq blacklist entry: start > end")) {
489 journal_entry_null_range(entry, vstruct_next(entry));
496 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
497 struct jset_entry *entry)
499 struct jset_entry_blacklist_v2 *bl =
500 container_of(entry, struct jset_entry_blacklist_v2, entry);
502 prt_printf(out, "start=%llu end=%llu",
503 le64_to_cpu(bl->start),
504 le64_to_cpu(bl->end));
507 static int journal_entry_usage_validate(struct bch_fs *c,
509 struct jset_entry *entry,
510 unsigned version, int big_endian,
511 enum bkey_invalid_flags flags)
513 struct jset_entry_usage *u =
514 container_of(entry, struct jset_entry_usage, entry);
515 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
518 if (journal_entry_err_on(bytes < sizeof(*u),
519 c, version, jset, entry,
520 journal_entry_usage_bad_size,
521 "invalid journal entry usage: bad size")) {
522 journal_entry_null_range(entry, vstruct_next(entry));
530 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
531 struct jset_entry *entry)
533 struct jset_entry_usage *u =
534 container_of(entry, struct jset_entry_usage, entry);
536 prt_printf(out, "type=%s v=%llu",
537 bch2_fs_usage_types[u->entry.btree_id],
541 static int journal_entry_data_usage_validate(struct bch_fs *c,
543 struct jset_entry *entry,
544 unsigned version, int big_endian,
545 enum bkey_invalid_flags flags)
547 struct jset_entry_data_usage *u =
548 container_of(entry, struct jset_entry_data_usage, entry);
549 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
552 if (journal_entry_err_on(bytes < sizeof(*u) ||
553 bytes < sizeof(*u) + u->r.nr_devs,
554 c, version, jset, entry,
555 journal_entry_data_usage_bad_size,
556 "invalid journal entry usage: bad size")) {
557 journal_entry_null_range(entry, vstruct_next(entry));
565 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
566 struct jset_entry *entry)
568 struct jset_entry_data_usage *u =
569 container_of(entry, struct jset_entry_data_usage, entry);
571 bch2_replicas_entry_to_text(out, &u->r);
572 prt_printf(out, "=%llu", le64_to_cpu(u->v));
575 static int journal_entry_clock_validate(struct bch_fs *c,
577 struct jset_entry *entry,
578 unsigned version, int big_endian,
579 enum bkey_invalid_flags flags)
581 struct jset_entry_clock *clock =
582 container_of(entry, struct jset_entry_clock, entry);
583 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
586 if (journal_entry_err_on(bytes != sizeof(*clock),
587 c, version, jset, entry,
588 journal_entry_clock_bad_size,
590 journal_entry_null_range(entry, vstruct_next(entry));
594 if (journal_entry_err_on(clock->rw > 1,
595 c, version, jset, entry,
596 journal_entry_clock_bad_rw,
598 journal_entry_null_range(entry, vstruct_next(entry));
606 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
607 struct jset_entry *entry)
609 struct jset_entry_clock *clock =
610 container_of(entry, struct jset_entry_clock, entry);
612 prt_printf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
615 static int journal_entry_dev_usage_validate(struct bch_fs *c,
617 struct jset_entry *entry,
618 unsigned version, int big_endian,
619 enum bkey_invalid_flags flags)
621 struct jset_entry_dev_usage *u =
622 container_of(entry, struct jset_entry_dev_usage, entry);
623 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
624 unsigned expected = sizeof(*u);
628 if (journal_entry_err_on(bytes < expected,
629 c, version, jset, entry,
630 journal_entry_dev_usage_bad_size,
631 "bad size (%u < %u)",
633 journal_entry_null_range(entry, vstruct_next(entry));
637 dev = le32_to_cpu(u->dev);
639 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
640 c, version, jset, entry,
641 journal_entry_dev_usage_bad_dev,
643 journal_entry_null_range(entry, vstruct_next(entry));
647 if (journal_entry_err_on(u->pad,
648 c, version, jset, entry,
649 journal_entry_dev_usage_bad_pad,
651 journal_entry_null_range(entry, vstruct_next(entry));
659 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
660 struct jset_entry *entry)
662 struct jset_entry_dev_usage *u =
663 container_of(entry, struct jset_entry_dev_usage, entry);
664 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
666 prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
668 for (i = 0; i < nr_types; i++) {
670 prt_printf(out, " %s", bch2_data_types[i]);
672 prt_printf(out, " (unknown data type %u)", i);
673 prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
674 le64_to_cpu(u->d[i].buckets),
675 le64_to_cpu(u->d[i].sectors),
676 le64_to_cpu(u->d[i].fragmented));
679 prt_printf(out, " buckets_ec: %llu", le64_to_cpu(u->buckets_ec));
682 static int journal_entry_log_validate(struct bch_fs *c,
684 struct jset_entry *entry,
685 unsigned version, int big_endian,
686 enum bkey_invalid_flags flags)
691 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
692 struct jset_entry *entry)
694 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
695 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
697 prt_printf(out, "%.*s", bytes, l->d);
700 static int journal_entry_overwrite_validate(struct bch_fs *c,
702 struct jset_entry *entry,
703 unsigned version, int big_endian,
704 enum bkey_invalid_flags flags)
706 return journal_entry_btree_keys_validate(c, jset, entry,
707 version, big_endian, READ);
710 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
711 struct jset_entry *entry)
713 journal_entry_btree_keys_to_text(out, c, entry);
716 struct jset_entry_ops {
717 int (*validate)(struct bch_fs *, struct jset *,
718 struct jset_entry *, unsigned, int,
719 enum bkey_invalid_flags);
720 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
723 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
725 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
726 .validate = journal_entry_##f##_validate, \
727 .to_text = journal_entry_##f##_to_text, \
729 BCH_JSET_ENTRY_TYPES()
733 int bch2_journal_entry_validate(struct bch_fs *c,
735 struct jset_entry *entry,
736 unsigned version, int big_endian,
737 enum bkey_invalid_flags flags)
739 return entry->type < BCH_JSET_ENTRY_NR
740 ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
741 version, big_endian, flags)
745 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
746 struct jset_entry *entry)
748 if (entry->type < BCH_JSET_ENTRY_NR) {
749 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
750 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
752 prt_printf(out, "(unknown type %u)", entry->type);
756 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
757 enum bkey_invalid_flags flags)
759 struct jset_entry *entry;
760 unsigned version = le32_to_cpu(jset->version);
763 vstruct_for_each(jset, entry) {
764 if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset),
765 c, version, jset, entry,
766 journal_entry_past_jset_end,
767 "journal entry extends past end of jset")) {
768 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
772 ret = bch2_journal_entry_validate(c, jset, entry,
773 version, JSET_BIG_ENDIAN(jset), flags);
781 static int jset_validate(struct bch_fs *c,
783 struct jset *jset, u64 sector,
784 enum bkey_invalid_flags flags)
789 if (le64_to_cpu(jset->magic) != jset_magic(c))
790 return JOURNAL_ENTRY_NONE;
792 version = le32_to_cpu(jset->version);
793 if (journal_entry_err_on(!bch2_version_compatible(version),
794 c, version, jset, NULL,
795 jset_unsupported_version,
796 "%s sector %llu seq %llu: incompatible journal entry version %u.%u",
797 ca ? ca->name : c->name,
798 sector, le64_to_cpu(jset->seq),
799 BCH_VERSION_MAJOR(version),
800 BCH_VERSION_MINOR(version))) {
801 /* don't try to continue: */
805 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
806 c, version, jset, NULL,
808 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
809 ca ? ca->name : c->name,
810 sector, le64_to_cpu(jset->seq),
811 JSET_CSUM_TYPE(jset)))
812 ret = JOURNAL_ENTRY_BAD;
814 /* last_seq is ignored when JSET_NO_FLUSH is true */
815 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
816 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
817 c, version, jset, NULL,
818 jset_last_seq_newer_than_seq,
819 "invalid journal entry: last_seq > seq (%llu > %llu)",
820 le64_to_cpu(jset->last_seq),
821 le64_to_cpu(jset->seq))) {
822 jset->last_seq = jset->seq;
823 return JOURNAL_ENTRY_BAD;
826 ret = jset_validate_entries(c, jset, flags);
831 static int jset_validate_early(struct bch_fs *c,
833 struct jset *jset, u64 sector,
834 unsigned bucket_sectors_left,
835 unsigned sectors_read)
837 size_t bytes = vstruct_bytes(jset);
839 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
842 if (le64_to_cpu(jset->magic) != jset_magic(c))
843 return JOURNAL_ENTRY_NONE;
845 version = le32_to_cpu(jset->version);
846 if (journal_entry_err_on(!bch2_version_compatible(version),
847 c, version, jset, NULL,
848 jset_unsupported_version,
849 "%s sector %llu seq %llu: unknown journal entry version %u.%u",
850 ca ? ca->name : c->name,
851 sector, le64_to_cpu(jset->seq),
852 BCH_VERSION_MAJOR(version),
853 BCH_VERSION_MINOR(version))) {
854 /* don't try to continue: */
858 if (bytes > (sectors_read << 9) &&
859 sectors_read < bucket_sectors_left)
860 return JOURNAL_ENTRY_REREAD;
862 if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
863 c, version, jset, NULL,
864 jset_past_bucket_end,
865 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
866 ca ? ca->name : c->name,
867 sector, le64_to_cpu(jset->seq), bytes))
868 le32_add_cpu(&jset->u64s,
869 -((bytes - (bucket_sectors_left << 9)) / 8));
874 struct journal_read_buf {
879 static int journal_read_buf_realloc(struct journal_read_buf *b,
884 /* the bios are sized for this many pages, max: */
885 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
886 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
888 new_size = roundup_pow_of_two(new_size);
889 n = kvpmalloc(new_size, GFP_KERNEL);
891 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
893 kvpfree(b->data, b->size);
899 static int journal_read_bucket(struct bch_dev *ca,
900 struct journal_read_buf *buf,
901 struct journal_list *jlist,
904 struct bch_fs *c = ca->fs;
905 struct journal_device *ja = &ca->journal;
906 struct jset *j = NULL;
907 unsigned sectors, sectors_read = 0;
908 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
909 end = offset + ca->mi.bucket_size;
910 bool saw_bad = false, csum_good;
913 pr_debug("reading %u", bucket);
915 while (offset < end) {
920 sectors_read = min_t(unsigned,
921 end - offset, buf->size >> 9);
922 nr_bvecs = buf_pages(buf->data, sectors_read << 9);
924 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
925 bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
927 bio->bi_iter.bi_sector = offset;
928 bch2_bio_map(bio, buf->data, sectors_read << 9);
930 ret = submit_bio_wait(bio);
933 if (bch2_dev_io_err_on(ret, ca, BCH_MEMBER_ERROR_read,
934 "journal read error: sector %llu",
936 bch2_meta_read_fault("journal")) {
938 * We don't error out of the recovery process
939 * here, since the relevant journal entry may be
940 * found on a different device, and missing or
941 * no journal entries will be handled later
949 ret = jset_validate_early(c, ca, j, offset,
950 end - offset, sectors_read);
953 sectors = vstruct_sectors(j, c->block_bits);
955 case JOURNAL_ENTRY_REREAD:
956 if (vstruct_bytes(j) > buf->size) {
957 ret = journal_read_buf_realloc(buf,
963 case JOURNAL_ENTRY_NONE:
967 * On checksum error we don't really trust the size
968 * field of the journal entry we read, so try reading
969 * again at next block boundary:
971 sectors = block_sectors(c);
978 * This happens sometimes if we don't have discards on -
979 * when we've partially overwritten a bucket with new
980 * journal entries. We don't need the rest of the
983 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
986 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
988 csum_good = jset_csum_good(c, j);
989 if (bch2_dev_io_err_on(!csum_good, ca, BCH_MEMBER_ERROR_checksum,
990 "journal checksum error"))
993 ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
995 vstruct_end(j) - (void *) j->encrypted_start);
996 bch2_fs_fatal_err_on(ret, c,
997 "error decrypting journal entry: %i", ret);
999 mutex_lock(&jlist->lock);
1000 ret = journal_entry_add(c, ca, (struct journal_ptr) {
1001 .csum_good = csum_good,
1004 .bucket_offset = offset -
1005 bucket_to_sector(ca, ja->buckets[bucket]),
1008 mutex_unlock(&jlist->lock);
1011 case JOURNAL_ENTRY_ADD_OK:
1013 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
1021 sectors_read -= sectors;
1022 j = ((void *) j) + (sectors << 9);
1028 static void bch2_journal_read_device(struct closure *cl)
1030 struct journal_device *ja =
1031 container_of(cl, struct journal_device, read);
1032 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
1033 struct bch_fs *c = ca->fs;
1034 struct journal_list *jlist =
1035 container_of(cl->parent, struct journal_list, cl);
1036 struct journal_replay *r, **_r;
1037 struct genradix_iter iter;
1038 struct journal_read_buf buf = { NULL, 0 };
1045 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1049 pr_debug("%u journal buckets", ja->nr);
1051 for (i = 0; i < ja->nr; i++) {
1052 ret = journal_read_bucket(ca, &buf, jlist, i);
1057 ja->sectors_free = ca->mi.bucket_size;
1059 mutex_lock(&jlist->lock);
1060 genradix_for_each_reverse(&c->journal_entries, iter, _r) {
1066 for (i = 0; i < r->nr_ptrs; i++) {
1067 if (r->ptrs[i].dev == ca->dev_idx) {
1068 unsigned wrote = bucket_remainder(ca, r->ptrs[i].sector) +
1069 vstruct_sectors(&r->j, c->block_bits);
1071 ja->cur_idx = r->ptrs[i].bucket;
1072 ja->sectors_free = ca->mi.bucket_size - wrote;
1078 mutex_unlock(&jlist->lock);
1080 if (ja->bucket_seq[ja->cur_idx] &&
1081 ja->sectors_free == ca->mi.bucket_size) {
1084 * Debug code for ZNS support, where we (probably) want to be
1085 * correlated where we stopped in the journal to the zone write
1088 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1089 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1090 for (i = 0; i < 3; i++) {
1091 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
1093 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1096 ja->sectors_free = 0;
1100 * Set dirty_idx to indicate the entire journal is full and needs to be
1101 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1102 * pinned when it first runs:
1104 ja->discard_idx = ja->dirty_idx_ondisk =
1105 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1107 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1108 kvpfree(buf.data, buf.size);
1109 percpu_ref_put(&ca->io_ref);
1113 mutex_lock(&jlist->lock);
1115 mutex_unlock(&jlist->lock);
1119 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1120 struct journal_replay *j)
1124 for (i = 0; i < j->nr_ptrs; i++) {
1125 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1128 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1131 prt_printf(out, " ");
1132 prt_printf(out, "%u:%u:%u (sector %llu)",
1135 j->ptrs[i].bucket_offset,
1140 int bch2_journal_read(struct bch_fs *c,
1145 struct journal_list jlist;
1146 struct journal_replay *i, **_i, *prev = NULL;
1147 struct genradix_iter radix_iter;
1150 struct printbuf buf = PRINTBUF;
1151 bool degraded = false, last_write_torn = false;
1155 closure_init_stack(&jlist.cl);
1156 mutex_init(&jlist.lock);
1160 for_each_member_device(ca, c, iter) {
1161 if (!c->opts.fsck &&
1162 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1165 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1166 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1167 percpu_ref_tryget(&ca->io_ref))
1168 closure_call(&ca->journal.read,
1169 bch2_journal_read_device,
1176 closure_sync(&jlist.cl);
1186 * Find most recent flush entry, and ignore newer non flush entries -
1187 * those entries will be blacklisted:
1189 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1190 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
1194 if (!i || i->ignore)
1198 *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1200 if (JSET_NO_FLUSH(&i->j)) {
1205 if (!last_write_torn && !i->csum_good) {
1206 last_write_torn = true;
1211 if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1212 c, le32_to_cpu(i->j.version), &i->j, NULL,
1213 jset_last_seq_newer_than_seq,
1214 "invalid journal entry: last_seq > seq (%llu > %llu)",
1215 le64_to_cpu(i->j.last_seq),
1216 le64_to_cpu(i->j.seq)))
1217 i->j.last_seq = i->j.seq;
1219 *last_seq = le64_to_cpu(i->j.last_seq);
1220 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1225 bch_info(c, "journal read done, but no entries found");
1230 fsck_err(c, dirty_but_no_journal_entries_post_drop_nonflushes,
1231 "journal read done, but no entries found after dropping non-flushes");
1235 bch_info(c, "journal read done, replaying entries %llu-%llu",
1236 *last_seq, *blacklist_seq - 1);
1238 if (*start_seq != *blacklist_seq)
1239 bch_info(c, "dropped unflushed entries %llu-%llu",
1240 *blacklist_seq, *start_seq - 1);
1242 /* Drop blacklisted entries and entries older than last_seq: */
1243 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1246 if (!i || i->ignore)
1249 seq = le64_to_cpu(i->j.seq);
1250 if (seq < *last_seq) {
1251 journal_replay_free(c, i);
1255 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1256 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1257 jset_seq_blacklisted,
1258 "found blacklisted journal entry %llu", seq);
1263 /* Check for missing entries: */
1265 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1268 if (!i || i->ignore)
1271 BUG_ON(seq > le64_to_cpu(i->j.seq));
1273 while (seq < le64_to_cpu(i->j.seq)) {
1274 u64 missing_start, missing_end;
1275 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1277 while (seq < le64_to_cpu(i->j.seq) &&
1278 bch2_journal_seq_is_blacklisted(c, seq, false))
1281 if (seq == le64_to_cpu(i->j.seq))
1284 missing_start = seq;
1286 while (seq < le64_to_cpu(i->j.seq) &&
1287 !bch2_journal_seq_is_blacklisted(c, seq, false))
1291 bch2_journal_ptrs_to_text(&buf1, c, prev);
1292 prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1294 prt_printf(&buf1, "(none)");
1295 bch2_journal_ptrs_to_text(&buf2, c, i);
1297 missing_end = seq - 1;
1298 fsck_err(c, journal_entries_missing,
1299 "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1302 missing_start, missing_end,
1303 *last_seq, *blacklist_seq - 1,
1304 buf1.buf, buf2.buf);
1306 printbuf_exit(&buf1);
1307 printbuf_exit(&buf2);
1314 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1315 struct bch_replicas_padded replicas = {
1316 .e.data_type = BCH_DATA_journal,
1322 if (!i || i->ignore)
1325 for (ptr = 0; ptr < i->nr_ptrs; ptr++) {
1326 ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
1328 if (!i->ptrs[ptr].csum_good)
1329 bch_err_dev_offset(ca, i->ptrs[ptr].sector,
1330 "invalid journal checksum, seq %llu%s",
1331 le64_to_cpu(i->j.seq),
1332 i->csum_good ? " (had good copy on another device)" : "");
1335 ret = jset_validate(c,
1336 bch_dev_bkey_exists(c, i->ptrs[0].dev),
1343 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1344 replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1346 bch2_replicas_entry_sort(&replicas.e);
1348 printbuf_reset(&buf);
1349 bch2_replicas_entry_to_text(&buf, &replicas.e);
1352 !bch2_replicas_marked(c, &replicas.e) &&
1353 (le64_to_cpu(i->j.seq) == *last_seq ||
1354 fsck_err(c, journal_entry_replicas_not_marked,
1355 "superblock not marked as containing replicas for journal entry %llu\n %s",
1356 le64_to_cpu(i->j.seq), buf.buf))) {
1357 ret = bch2_mark_replicas(c, &replicas.e);
1364 printbuf_exit(&buf);
1368 /* journal write: */
1370 static void __journal_write_alloc(struct journal *j,
1371 struct journal_buf *w,
1372 struct dev_alloc_list *devs_sorted,
1375 unsigned replicas_want)
1377 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1378 struct journal_device *ja;
1382 if (*replicas >= replicas_want)
1385 for (i = 0; i < devs_sorted->nr; i++) {
1386 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1393 * Check that we can use this device, and aren't already using
1396 if (!ca->mi.durability ||
1397 ca->mi.state != BCH_MEMBER_STATE_rw ||
1399 bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) ||
1400 sectors > ja->sectors_free)
1403 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1405 bch2_bkey_append_ptr(&w->key,
1406 (struct bch_extent_ptr) {
1407 .offset = bucket_to_sector(ca,
1408 ja->buckets[ja->cur_idx]) +
1409 ca->mi.bucket_size -
1414 ja->sectors_free -= sectors;
1415 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1417 *replicas += ca->mi.durability;
1419 if (*replicas >= replicas_want)
1425 * journal_write_alloc - decide where to write next journal entry
1427 * @j: journal object
1428 * @w: journal buf (entry to be written)
1430 * Returns: 0 on success, or -EROFS on failure
1432 static int journal_write_alloc(struct journal *j, struct journal_buf *w)
1434 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1435 struct bch_devs_mask devs;
1436 struct journal_device *ja;
1438 struct dev_alloc_list devs_sorted;
1439 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1440 unsigned target = c->opts.metadata_target ?:
1441 c->opts.foreground_target;
1442 unsigned i, replicas = 0, replicas_want =
1443 READ_ONCE(c->opts.metadata_replicas);
1447 devs = target_rw_devs(c, BCH_DATA_journal, target);
1449 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1451 __journal_write_alloc(j, w, &devs_sorted,
1452 sectors, &replicas, replicas_want);
1454 if (replicas >= replicas_want)
1457 for (i = 0; i < devs_sorted.nr; i++) {
1458 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1464 if (sectors > ja->sectors_free &&
1465 sectors <= ca->mi.bucket_size &&
1466 bch2_journal_dev_buckets_available(j, ja,
1467 journal_space_discarded)) {
1468 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1469 ja->sectors_free = ca->mi.bucket_size;
1472 * ja->bucket_seq[ja->cur_idx] must always have
1473 * something sensible:
1475 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1479 __journal_write_alloc(j, w, &devs_sorted,
1480 sectors, &replicas, replicas_want);
1482 if (replicas < replicas_want && target) {
1483 /* Retry from all devices: */
1490 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1492 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1495 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1497 /* we aren't holding j->lock: */
1498 unsigned new_size = READ_ONCE(j->buf_size_want);
1501 if (buf->buf_size >= new_size)
1504 new_buf = kvpmalloc(new_size, GFP_NOFS|__GFP_NOWARN);
1508 memcpy(new_buf, buf->data, buf->buf_size);
1510 spin_lock(&j->lock);
1511 swap(buf->data, new_buf);
1512 swap(buf->buf_size, new_size);
1513 spin_unlock(&j->lock);
1515 kvpfree(new_buf, new_size);
1518 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1520 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1523 static void journal_write_done(struct closure *cl)
1525 struct journal *j = container_of(cl, struct journal, io);
1526 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1527 struct journal_buf *w = journal_last_unwritten_buf(j);
1528 struct bch_replicas_padded replicas;
1529 union journal_res_state old, new;
1533 bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1534 ? j->flush_write_time
1535 : j->noflush_write_time, j->write_start_time);
1537 if (!w->devs_written.nr) {
1538 bch_err(c, "unable to write journal to sufficient devices");
1541 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1543 if (bch2_mark_replicas(c, &replicas.e))
1548 bch2_fatal_error(c);
1550 spin_lock(&j->lock);
1551 seq = le64_to_cpu(w->data->seq);
1553 if (seq >= j->pin.front)
1554 journal_seq_pin(j, seq)->devs = w->devs_written;
1557 if (!JSET_NO_FLUSH(w->data)) {
1558 j->flushed_seq_ondisk = seq;
1559 j->last_seq_ondisk = w->last_seq;
1561 bch2_do_discards(c);
1562 closure_wake_up(&c->freelist_wait);
1564 bch2_reset_alloc_cursors(c);
1566 } else if (!j->err_seq || seq < j->err_seq)
1569 j->seq_ondisk = seq;
1572 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1575 * Must come before signaling write completion, for
1576 * bch2_fs_journal_stop():
1578 if (j->watermark != BCH_WATERMARK_stripe)
1579 journal_reclaim_kick(&c->journal);
1581 /* also must come before signalling write completion: */
1582 closure_debug_destroy(cl);
1584 v = atomic64_read(&j->reservations.counter);
1587 BUG_ON(journal_state_count(new, new.unwritten_idx));
1589 new.unwritten_idx++;
1590 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1591 old.v, new.v)) != old.v);
1593 bch2_journal_space_available(j);
1595 track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight],
1596 &j->max_in_flight_start, false);
1598 closure_wake_up(&w->wait);
1601 if (!journal_state_count(new, new.unwritten_idx) &&
1602 journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1603 spin_unlock(&j->lock);
1604 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1605 } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1606 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1607 struct journal_buf *buf = journal_cur_buf(j);
1608 long delta = buf->expires - jiffies;
1611 * We don't close a journal entry to write it while there's
1612 * previous entries still in flight - the current journal entry
1613 * might want to be written now:
1616 spin_unlock(&j->lock);
1617 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1619 spin_unlock(&j->lock);
1623 static void journal_write_endio(struct bio *bio)
1625 struct bch_dev *ca = bio->bi_private;
1626 struct journal *j = &ca->fs->journal;
1627 struct journal_buf *w = journal_last_unwritten_buf(j);
1628 unsigned long flags;
1630 if (bch2_dev_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_write,
1631 "error writing journal entry %llu: %s",
1632 le64_to_cpu(w->data->seq),
1633 bch2_blk_status_to_str(bio->bi_status)) ||
1634 bch2_meta_write_fault("journal")) {
1635 spin_lock_irqsave(&j->err_lock, flags);
1636 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1637 spin_unlock_irqrestore(&j->err_lock, flags);
1640 closure_put(&j->io);
1641 percpu_ref_put(&ca->io_ref);
1644 static void do_journal_write(struct closure *cl)
1646 struct journal *j = container_of(cl, struct journal, io);
1647 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1649 struct journal_buf *w = journal_last_unwritten_buf(j);
1650 struct bch_extent_ptr *ptr;
1652 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1654 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1655 ca = bch_dev_bkey_exists(c, ptr->dev);
1656 if (!percpu_ref_tryget(&ca->io_ref)) {
1658 bch_err(c, "missing device for journal write\n");
1662 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1665 bio = ca->journal.bio;
1666 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1667 bio->bi_iter.bi_sector = ptr->offset;
1668 bio->bi_end_io = journal_write_endio;
1669 bio->bi_private = ca;
1671 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1672 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1674 if (!JSET_NO_FLUSH(w->data))
1675 bio->bi_opf |= REQ_FUA;
1676 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1677 bio->bi_opf |= REQ_PREFLUSH;
1679 bch2_bio_map(bio, w->data, sectors << 9);
1681 trace_and_count(c, journal_write, bio);
1682 closure_bio_submit(bio, cl);
1684 ca->journal.bucket_seq[ca->journal.cur_idx] =
1685 le64_to_cpu(w->data->seq);
1688 continue_at(cl, journal_write_done, c->io_complete_wq);
1691 static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w)
1693 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1694 struct jset_entry *start, *end, *i, *next, *prev = NULL;
1695 struct jset *jset = w->data;
1696 unsigned sectors, bytes, u64s;
1697 bool validate_before_checksum = false;
1698 unsigned long btree_roots_have = 0;
1702 * Simple compaction, dropping empty jset_entries (from journal
1703 * reservations that weren't fully used) and merging jset_entries that
1706 * If we wanted to be really fancy here, we could sort all the keys in
1707 * the jset and drop keys that were overwritten - probably not worth it:
1709 vstruct_for_each_safe(jset, i, next) {
1710 unsigned u64s = le16_to_cpu(i->u64s);
1717 * New btree roots are set by journalling them; when the journal
1718 * entry gets written we have to propagate them to
1721 * But, every journal entry we write has to contain all the
1722 * btree roots (at least for now); so after we copy btree roots
1723 * to c->btree_roots we have to get any missing btree roots and
1724 * add them to this journal entry:
1726 if (i->type == BCH_JSET_ENTRY_btree_root) {
1727 bch2_journal_entry_to_btree_root(c, i);
1728 __set_bit(i->btree_id, &btree_roots_have);
1731 /* Can we merge with previous entry? */
1733 i->btree_id == prev->btree_id &&
1734 i->level == prev->level &&
1735 i->type == prev->type &&
1736 i->type == BCH_JSET_ENTRY_btree_keys &&
1737 le16_to_cpu(prev->u64s) + u64s <= U16_MAX) {
1738 memmove_u64s_down(vstruct_next(prev),
1741 le16_add_cpu(&prev->u64s, u64s);
1745 /* Couldn't merge, move i into new position (after prev): */
1746 prev = prev ? vstruct_next(prev) : jset->start;
1748 memmove_u64s_down(prev, i, jset_u64s(u64s));
1751 prev = prev ? vstruct_next(prev) : jset->start;
1752 jset->u64s = cpu_to_le32((u64 *) prev - jset->_data);
1754 start = end = vstruct_last(jset);
1756 end = bch2_btree_roots_to_journal_entries(c, end, btree_roots_have);
1758 bch2_journal_super_entries_add_common(c, &end,
1759 le64_to_cpu(jset->seq));
1760 u64s = (u64 *) end - (u64 *) start;
1761 BUG_ON(u64s > j->entry_u64s_reserved);
1763 le32_add_cpu(&jset->u64s, u64s);
1765 sectors = vstruct_sectors(jset, c->block_bits);
1766 bytes = vstruct_bytes(jset);
1768 if (sectors > w->sectors) {
1769 bch2_fs_fatal_error(c, "aieeee! journal write overran available space, %zu > %u (extra %u reserved %u/%u)",
1770 vstruct_bytes(jset), w->sectors << 9,
1771 u64s, w->u64s_reserved, j->entry_u64s_reserved);
1775 jset->magic = cpu_to_le64(jset_magic(c));
1776 jset->version = cpu_to_le32(c->sb.version);
1778 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1779 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1781 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1782 j->last_empty_seq = le64_to_cpu(jset->seq);
1784 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1785 validate_before_checksum = true;
1787 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1788 validate_before_checksum = true;
1790 if (validate_before_checksum &&
1791 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1794 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1795 jset->encrypted_start,
1796 vstruct_end(jset) - (void *) jset->encrypted_start);
1797 if (bch2_fs_fatal_err_on(ret, c,
1798 "error decrypting journal entry: %i", ret))
1801 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1802 journal_nonce(jset), jset);
1804 if (!validate_before_checksum &&
1805 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1808 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1812 static int bch2_journal_write_pick_flush(struct journal *j, struct journal_buf *w)
1814 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1815 int error = bch2_journal_error(j);
1818 * If the journal is in an error state - we did an emergency shutdown -
1819 * we prefer to continue doing journal writes. We just mark them as
1820 * noflush so they'll never be used, but they'll still be visible by the
1821 * list_journal tool - this helps in debugging.
1823 * There's a caveat: the first journal write after marking the
1824 * superblock dirty must always be a flush write, because on startup
1825 * from a clean shutdown we didn't necessarily read the journal and the
1826 * new journal write might overwrite whatever was in the journal
1827 * previously - we can't leave the journal without any flush writes in
1830 * So if we're in an error state, and we're still starting up, we don't
1831 * write anything at all.
1833 if (error && test_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags))
1839 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1840 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags))) {
1842 SET_JSET_NO_FLUSH(w->data, true);
1843 w->data->last_seq = 0;
1846 j->nr_noflush_writes++;
1848 j->last_flush_write = jiffies;
1849 j->nr_flush_writes++;
1850 clear_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags);
1856 void bch2_journal_write(struct closure *cl)
1858 struct journal *j = container_of(cl, struct journal, io);
1859 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1861 struct journal_buf *w = journal_last_unwritten_buf(j);
1862 struct bch_replicas_padded replicas;
1864 struct printbuf journal_debug_buf = PRINTBUF;
1865 unsigned i, nr_rw_members = 0;
1868 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1870 j->write_start_time = local_clock();
1872 spin_lock(&j->lock);
1873 ret = bch2_journal_write_pick_flush(j, w);
1874 spin_unlock(&j->lock);
1878 journal_buf_realloc(j, w);
1880 ret = bch2_journal_write_prep(j, w);
1884 j->entry_bytes_written += vstruct_bytes(w->data);
1887 spin_lock(&j->lock);
1888 ret = journal_write_alloc(j, w);
1889 if (!ret || !j->can_discard)
1892 spin_unlock(&j->lock);
1893 bch2_journal_do_discards(j);
1897 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1898 spin_unlock(&j->lock);
1899 bch_err(c, "Unable to allocate journal write:\n%s",
1900 journal_debug_buf.buf);
1901 printbuf_exit(&journal_debug_buf);
1906 * write is allocated, no longer need to account for it in
1907 * bch2_journal_space_available():
1912 * journal entry has been compacted and allocated, recalculate space
1915 bch2_journal_space_available(j);
1916 spin_unlock(&j->lock);
1918 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1920 if (c->opts.nochanges)
1923 for_each_rw_member(ca, c, i)
1926 if (nr_rw_members > 1)
1927 w->separate_flush = true;
1930 * Mark journal replicas before we submit the write to guarantee
1931 * recovery will find the journal entries after a crash.
1933 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1935 ret = bch2_mark_replicas(c, &replicas.e);
1939 if (!JSET_NO_FLUSH(w->data) && w->separate_flush) {
1940 for_each_rw_member(ca, c, i) {
1941 percpu_ref_get(&ca->io_ref);
1943 bio = ca->journal.bio;
1944 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_FLUSH);
1945 bio->bi_end_io = journal_write_endio;
1946 bio->bi_private = ca;
1947 closure_bio_submit(bio, cl);
1951 continue_at(cl, do_journal_write, c->io_complete_wq);
1954 continue_at(cl, journal_write_done, c->io_complete_wq);
1957 bch2_fatal_error(c);
1958 continue_at(cl, journal_write_done, c->io_complete_wq);