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"
13 #include "journal_io.h"
14 #include "journal_reclaim.h"
15 #include "journal_seq_blacklist.h"
20 static struct nonce journal_nonce(const struct jset *jset)
22 return (struct nonce) {{
24 [1] = ((__le32 *) &jset->seq)[0],
25 [2] = ((__le32 *) &jset->seq)[1],
26 [3] = BCH_NONCE_JOURNAL,
30 static bool jset_csum_good(struct bch_fs *c, struct jset *j)
32 return bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j)) &&
33 !bch2_crc_cmp(j->csum,
34 csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j));
37 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
39 return (seq - c->journal_entries_base_seq) & (~0U >> 1);
42 static void __journal_replay_free(struct bch_fs *c,
43 struct journal_replay *i)
45 struct journal_replay **p =
46 genradix_ptr(&c->journal_entries,
47 journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
51 kvpfree(i, offsetof(struct journal_replay, j) +
52 vstruct_bytes(&i->j));
55 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
59 if (!c->opts.read_entire_journal)
60 __journal_replay_free(c, i);
70 #define JOURNAL_ENTRY_ADD_OK 0
71 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
74 * Given a journal entry we just read, add it to the list of journal entries to
77 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
78 struct journal_ptr entry_ptr,
79 struct journal_list *jlist, struct jset *j)
81 struct genradix_iter iter;
82 struct journal_replay **_i, *i, *dup;
83 struct journal_ptr *ptr;
84 size_t bytes = vstruct_bytes(j);
85 u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
86 int ret = JOURNAL_ENTRY_ADD_OK;
88 /* Is this entry older than the range we need? */
89 if (!c->opts.read_entire_journal &&
90 le64_to_cpu(j->seq) < jlist->last_seq)
91 return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
94 * genradixes are indexed by a ulong, not a u64, so we can't index them
95 * by sequence number directly: Assume instead that they will all fall
96 * within the range of +-2billion of the filrst one we find.
98 if (!c->journal_entries_base_seq)
99 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
101 /* Drop entries we don't need anymore */
102 if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
103 genradix_for_each_from(&c->journal_entries, iter, _i,
104 journal_entry_radix_idx(c, jlist->last_seq)) {
110 if (le64_to_cpu(i->j.seq) >= last_seq)
112 journal_replay_free(c, i);
116 jlist->last_seq = max(jlist->last_seq, last_seq);
118 _i = genradix_ptr_alloc(&c->journal_entries,
119 journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
122 return -BCH_ERR_ENOMEM_journal_entry_add;
125 * Duplicate journal entries? If so we want the one that didn't have a
130 if (bytes == vstruct_bytes(&dup->j) &&
131 !memcmp(j, &dup->j, bytes)) {
136 if (!entry_ptr.csum_good) {
144 fsck_err(c, "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, 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, "%s", buf.buf); \
251 bch_err(c, "corrupt metadata before write: %s\n", buf.buf);\
252 if (bch2_fs_inconsistent(c)) { \
253 ret = -BCH_ERR_fsck_errors_not_fixed; \
259 printbuf_exit(&buf); \
263 #define journal_entry_err_on(cond, c, version, jset, entry, msg, ...) \
264 ((cond) ? journal_entry_err(c, version, jset, entry, msg, ##__VA_ARGS__) : false)
266 #define FSCK_DELETED_KEY 5
268 static int journal_validate_key(struct bch_fs *c,
270 struct jset_entry *entry,
271 unsigned level, enum btree_id btree_id,
273 unsigned version, int big_endian,
274 enum bkey_invalid_flags flags)
276 int write = flags & BKEY_INVALID_WRITE;
277 void *next = vstruct_next(entry);
278 struct printbuf buf = PRINTBUF;
281 if (journal_entry_err_on(!k->k.u64s, c, version, jset, entry, "k->u64s 0")) {
282 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
283 journal_entry_null_range(vstruct_next(entry), next);
284 return FSCK_DELETED_KEY;
287 if (journal_entry_err_on((void *) bkey_next(k) >
288 (void *) vstruct_next(entry),
289 c, version, jset, entry,
290 "extends past end of journal entry")) {
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(k->k.format != KEY_FORMAT_CURRENT,
297 c, version, jset, entry,
298 "bad format %u", k->k.format)) {
299 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
300 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
301 journal_entry_null_range(vstruct_next(entry), next);
302 return FSCK_DELETED_KEY;
306 bch2_bkey_compat(level, btree_id, version, big_endian,
307 write, NULL, bkey_to_packed(k));
309 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
310 __btree_node_type(level, btree_id), write, &buf)) {
311 printbuf_reset(&buf);
312 journal_entry_err_msg(&buf, version, jset, entry);
314 printbuf_indent_add(&buf, 2);
316 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
318 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
319 __btree_node_type(level, btree_id), write, &buf);
321 mustfix_fsck_err(c, "%s", buf.buf);
323 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
324 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
325 journal_entry_null_range(vstruct_next(entry), next);
328 return FSCK_DELETED_KEY;
332 bch2_bkey_compat(level, btree_id, version, big_endian,
333 write, NULL, bkey_to_packed(k));
339 static int journal_entry_btree_keys_validate(struct bch_fs *c,
341 struct jset_entry *entry,
342 unsigned version, int big_endian,
343 enum bkey_invalid_flags flags)
345 struct bkey_i *k = entry->start;
347 while (k != vstruct_last(entry)) {
348 int ret = journal_validate_key(c, jset, entry,
351 k, version, big_endian,
352 flags|BKEY_INVALID_JOURNAL);
353 if (ret == FSCK_DELETED_KEY)
362 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
363 struct jset_entry *entry)
368 jset_entry_for_each_key(entry, k) {
371 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
373 prt_printf(out, "btree=%s l=%u ", bch2_btree_ids[entry->btree_id], entry->level);
374 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
379 static int journal_entry_btree_root_validate(struct bch_fs *c,
381 struct jset_entry *entry,
382 unsigned version, int big_endian,
383 enum bkey_invalid_flags flags)
385 struct bkey_i *k = entry->start;
388 if (journal_entry_err_on(!entry->u64s ||
389 le16_to_cpu(entry->u64s) != k->k.u64s,
390 c, version, jset, entry,
391 "invalid btree root journal entry: wrong number of keys")) {
392 void *next = vstruct_next(entry);
394 * we don't want to null out this jset_entry,
395 * just the contents, so that later we can tell
396 * we were _supposed_ to have a btree root
399 journal_entry_null_range(vstruct_next(entry), next);
403 return journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
404 version, big_endian, flags);
409 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
410 struct jset_entry *entry)
412 journal_entry_btree_keys_to_text(out, c, entry);
415 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
417 struct jset_entry *entry,
418 unsigned version, int big_endian,
419 enum bkey_invalid_flags flags)
421 /* obsolete, don't care: */
425 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
426 struct jset_entry *entry)
430 static int journal_entry_blacklist_validate(struct bch_fs *c,
432 struct jset_entry *entry,
433 unsigned version, int big_endian,
434 enum bkey_invalid_flags flags)
438 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
439 c, version, jset, entry,
440 "invalid journal seq blacklist entry: bad size")) {
441 journal_entry_null_range(entry, vstruct_next(entry));
447 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
448 struct jset_entry *entry)
450 struct jset_entry_blacklist *bl =
451 container_of(entry, struct jset_entry_blacklist, entry);
453 prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
456 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
458 struct jset_entry *entry,
459 unsigned version, int big_endian,
460 enum bkey_invalid_flags flags)
462 struct jset_entry_blacklist_v2 *bl_entry;
465 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
466 c, version, jset, entry,
467 "invalid journal seq blacklist entry: bad size")) {
468 journal_entry_null_range(entry, vstruct_next(entry));
472 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
474 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
475 le64_to_cpu(bl_entry->end),
476 c, version, jset, entry,
477 "invalid journal seq blacklist entry: start > end")) {
478 journal_entry_null_range(entry, vstruct_next(entry));
485 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
486 struct jset_entry *entry)
488 struct jset_entry_blacklist_v2 *bl =
489 container_of(entry, struct jset_entry_blacklist_v2, entry);
491 prt_printf(out, "start=%llu end=%llu",
492 le64_to_cpu(bl->start),
493 le64_to_cpu(bl->end));
496 static int journal_entry_usage_validate(struct bch_fs *c,
498 struct jset_entry *entry,
499 unsigned version, int big_endian,
500 enum bkey_invalid_flags flags)
502 struct jset_entry_usage *u =
503 container_of(entry, struct jset_entry_usage, entry);
504 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
507 if (journal_entry_err_on(bytes < sizeof(*u),
508 c, version, jset, entry,
509 "invalid journal entry usage: bad size")) {
510 journal_entry_null_range(entry, vstruct_next(entry));
518 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
519 struct jset_entry *entry)
521 struct jset_entry_usage *u =
522 container_of(entry, struct jset_entry_usage, entry);
524 prt_printf(out, "type=%s v=%llu",
525 bch2_fs_usage_types[u->entry.btree_id],
529 static int journal_entry_data_usage_validate(struct bch_fs *c,
531 struct jset_entry *entry,
532 unsigned version, int big_endian,
533 enum bkey_invalid_flags flags)
535 struct jset_entry_data_usage *u =
536 container_of(entry, struct jset_entry_data_usage, entry);
537 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
540 if (journal_entry_err_on(bytes < sizeof(*u) ||
541 bytes < sizeof(*u) + u->r.nr_devs,
542 c, version, jset, entry,
543 "invalid journal entry usage: bad size")) {
544 journal_entry_null_range(entry, vstruct_next(entry));
552 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
553 struct jset_entry *entry)
555 struct jset_entry_data_usage *u =
556 container_of(entry, struct jset_entry_data_usage, entry);
558 bch2_replicas_entry_to_text(out, &u->r);
559 prt_printf(out, "=%llu", le64_to_cpu(u->v));
562 static int journal_entry_clock_validate(struct bch_fs *c,
564 struct jset_entry *entry,
565 unsigned version, int big_endian,
566 enum bkey_invalid_flags flags)
568 struct jset_entry_clock *clock =
569 container_of(entry, struct jset_entry_clock, entry);
570 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
573 if (journal_entry_err_on(bytes != sizeof(*clock),
574 c, version, jset, entry, "bad size")) {
575 journal_entry_null_range(entry, vstruct_next(entry));
579 if (journal_entry_err_on(clock->rw > 1,
580 c, version, jset, entry, "bad rw")) {
581 journal_entry_null_range(entry, vstruct_next(entry));
589 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
590 struct jset_entry *entry)
592 struct jset_entry_clock *clock =
593 container_of(entry, struct jset_entry_clock, entry);
595 prt_printf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
598 static int journal_entry_dev_usage_validate(struct bch_fs *c,
600 struct jset_entry *entry,
601 unsigned version, int big_endian,
602 enum bkey_invalid_flags flags)
604 struct jset_entry_dev_usage *u =
605 container_of(entry, struct jset_entry_dev_usage, entry);
606 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
607 unsigned expected = sizeof(*u);
611 if (journal_entry_err_on(bytes < expected,
612 c, version, jset, entry, "bad size (%u < %u)",
614 journal_entry_null_range(entry, vstruct_next(entry));
618 dev = le32_to_cpu(u->dev);
620 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
621 c, version, jset, entry, "bad dev")) {
622 journal_entry_null_range(entry, vstruct_next(entry));
626 if (journal_entry_err_on(u->pad,
627 c, version, jset, entry, "bad pad")) {
628 journal_entry_null_range(entry, vstruct_next(entry));
636 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
637 struct jset_entry *entry)
639 struct jset_entry_dev_usage *u =
640 container_of(entry, struct jset_entry_dev_usage, entry);
641 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
643 prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
645 for (i = 0; i < nr_types; i++) {
647 prt_printf(out, " %s", bch2_data_types[i]);
649 prt_printf(out, " (unknown data type %u)", i);
650 prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
651 le64_to_cpu(u->d[i].buckets),
652 le64_to_cpu(u->d[i].sectors),
653 le64_to_cpu(u->d[i].fragmented));
656 prt_printf(out, " buckets_ec: %llu", le64_to_cpu(u->buckets_ec));
659 static int journal_entry_log_validate(struct bch_fs *c,
661 struct jset_entry *entry,
662 unsigned version, int big_endian,
663 enum bkey_invalid_flags flags)
668 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
669 struct jset_entry *entry)
671 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
672 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
674 prt_printf(out, "%.*s", bytes, l->d);
677 static int journal_entry_overwrite_validate(struct bch_fs *c,
679 struct jset_entry *entry,
680 unsigned version, int big_endian,
681 enum bkey_invalid_flags flags)
683 return journal_entry_btree_keys_validate(c, jset, entry,
684 version, big_endian, READ);
687 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
688 struct jset_entry *entry)
690 journal_entry_btree_keys_to_text(out, c, entry);
693 struct jset_entry_ops {
694 int (*validate)(struct bch_fs *, struct jset *,
695 struct jset_entry *, unsigned, int,
696 enum bkey_invalid_flags);
697 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
700 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
702 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
703 .validate = journal_entry_##f##_validate, \
704 .to_text = journal_entry_##f##_to_text, \
706 BCH_JSET_ENTRY_TYPES()
710 int bch2_journal_entry_validate(struct bch_fs *c,
712 struct jset_entry *entry,
713 unsigned version, int big_endian,
714 enum bkey_invalid_flags flags)
716 return entry->type < BCH_JSET_ENTRY_NR
717 ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
718 version, big_endian, flags)
722 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
723 struct jset_entry *entry)
725 if (entry->type < BCH_JSET_ENTRY_NR) {
726 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
727 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
729 prt_printf(out, "(unknown type %u)", entry->type);
733 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
734 enum bkey_invalid_flags flags)
736 struct jset_entry *entry;
737 unsigned version = le32_to_cpu(jset->version);
740 vstruct_for_each(jset, entry) {
741 if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset),
742 c, version, jset, entry,
743 "journal entry extends past end of jset")) {
744 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
748 ret = bch2_journal_entry_validate(c, jset, entry,
749 version, JSET_BIG_ENDIAN(jset), flags);
757 static int jset_validate(struct bch_fs *c,
759 struct jset *jset, u64 sector,
760 enum bkey_invalid_flags flags)
765 if (le64_to_cpu(jset->magic) != jset_magic(c))
766 return JOURNAL_ENTRY_NONE;
768 version = le32_to_cpu(jset->version);
769 if (journal_entry_err_on(!bch2_version_compatible(version),
770 c, version, jset, NULL,
771 "%s sector %llu seq %llu: incompatible journal entry version %u.%u",
772 ca ? ca->name : c->name,
773 sector, le64_to_cpu(jset->seq),
774 BCH_VERSION_MAJOR(version),
775 BCH_VERSION_MINOR(version))) {
776 /* don't try to continue: */
780 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
781 c, version, jset, NULL,
782 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
783 ca ? ca->name : c->name,
784 sector, le64_to_cpu(jset->seq),
785 JSET_CSUM_TYPE(jset)))
786 ret = JOURNAL_ENTRY_BAD;
788 /* last_seq is ignored when JSET_NO_FLUSH is true */
789 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
790 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
791 c, version, jset, NULL,
792 "invalid journal entry: last_seq > seq (%llu > %llu)",
793 le64_to_cpu(jset->last_seq),
794 le64_to_cpu(jset->seq))) {
795 jset->last_seq = jset->seq;
796 return JOURNAL_ENTRY_BAD;
799 ret = jset_validate_entries(c, jset, flags);
804 static int jset_validate_early(struct bch_fs *c,
806 struct jset *jset, u64 sector,
807 unsigned bucket_sectors_left,
808 unsigned sectors_read)
810 size_t bytes = vstruct_bytes(jset);
812 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
815 if (le64_to_cpu(jset->magic) != jset_magic(c))
816 return JOURNAL_ENTRY_NONE;
818 version = le32_to_cpu(jset->version);
819 if (journal_entry_err_on(!bch2_version_compatible(version),
820 c, version, jset, NULL,
821 "%s sector %llu seq %llu: unknown journal entry version %u.%u",
822 ca ? ca->name : c->name,
823 sector, le64_to_cpu(jset->seq),
824 BCH_VERSION_MAJOR(version),
825 BCH_VERSION_MINOR(version))) {
826 /* don't try to continue: */
830 if (bytes > (sectors_read << 9) &&
831 sectors_read < bucket_sectors_left)
832 return JOURNAL_ENTRY_REREAD;
834 if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
835 c, version, jset, NULL,
836 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
837 ca ? ca->name : c->name,
838 sector, le64_to_cpu(jset->seq), bytes))
839 le32_add_cpu(&jset->u64s,
840 -((bytes - (bucket_sectors_left << 9)) / 8));
845 struct journal_read_buf {
850 static int journal_read_buf_realloc(struct journal_read_buf *b,
855 /* the bios are sized for this many pages, max: */
856 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
857 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
859 new_size = roundup_pow_of_two(new_size);
860 n = kvpmalloc(new_size, GFP_KERNEL);
862 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
864 kvpfree(b->data, b->size);
870 static int journal_read_bucket(struct bch_dev *ca,
871 struct journal_read_buf *buf,
872 struct journal_list *jlist,
875 struct bch_fs *c = ca->fs;
876 struct journal_device *ja = &ca->journal;
877 struct jset *j = NULL;
878 unsigned sectors, sectors_read = 0;
879 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
880 end = offset + ca->mi.bucket_size;
881 bool saw_bad = false, csum_good;
884 pr_debug("reading %u", bucket);
886 while (offset < end) {
891 sectors_read = min_t(unsigned,
892 end - offset, buf->size >> 9);
893 nr_bvecs = buf_pages(buf->data, sectors_read << 9);
895 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
896 bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
898 bio->bi_iter.bi_sector = offset;
899 bch2_bio_map(bio, buf->data, sectors_read << 9);
901 ret = submit_bio_wait(bio);
904 if (bch2_dev_io_err_on(ret, ca,
905 "journal read error: sector %llu",
907 bch2_meta_read_fault("journal")) {
909 * We don't error out of the recovery process
910 * here, since the relevant journal entry may be
911 * found on a different device, and missing or
912 * no journal entries will be handled later
920 ret = jset_validate_early(c, ca, j, offset,
921 end - offset, sectors_read);
924 sectors = vstruct_sectors(j, c->block_bits);
926 case JOURNAL_ENTRY_REREAD:
927 if (vstruct_bytes(j) > buf->size) {
928 ret = journal_read_buf_realloc(buf,
934 case JOURNAL_ENTRY_NONE:
938 * On checksum error we don't really trust the size
939 * field of the journal entry we read, so try reading
940 * again at next block boundary:
942 sectors = block_sectors(c);
949 * This happens sometimes if we don't have discards on -
950 * when we've partially overwritten a bucket with new
951 * journal entries. We don't need the rest of the
954 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
957 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
959 csum_good = jset_csum_good(c, j);
963 ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
965 vstruct_end(j) - (void *) j->encrypted_start);
966 bch2_fs_fatal_err_on(ret, c,
967 "error decrypting journal entry: %i", ret);
969 mutex_lock(&jlist->lock);
970 ret = journal_entry_add(c, ca, (struct journal_ptr) {
971 .csum_good = csum_good,
974 .bucket_offset = offset -
975 bucket_to_sector(ca, ja->buckets[bucket]),
978 mutex_unlock(&jlist->lock);
981 case JOURNAL_ENTRY_ADD_OK:
983 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
991 sectors_read -= sectors;
992 j = ((void *) j) + (sectors << 9);
998 static void bch2_journal_read_device(struct closure *cl)
1000 struct journal_device *ja =
1001 container_of(cl, struct journal_device, read);
1002 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
1003 struct bch_fs *c = ca->fs;
1004 struct journal_list *jlist =
1005 container_of(cl->parent, struct journal_list, cl);
1006 struct journal_replay *r, **_r;
1007 struct genradix_iter iter;
1008 struct journal_read_buf buf = { NULL, 0 };
1015 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1019 pr_debug("%u journal buckets", ja->nr);
1021 for (i = 0; i < ja->nr; i++) {
1022 ret = journal_read_bucket(ca, &buf, jlist, i);
1027 ja->sectors_free = ca->mi.bucket_size;
1029 mutex_lock(&jlist->lock);
1030 genradix_for_each_reverse(&c->journal_entries, iter, _r) {
1036 for (i = 0; i < r->nr_ptrs; i++) {
1037 if (r->ptrs[i].dev == ca->dev_idx) {
1038 unsigned wrote = bucket_remainder(ca, r->ptrs[i].sector) +
1039 vstruct_sectors(&r->j, c->block_bits);
1041 ja->cur_idx = r->ptrs[i].bucket;
1042 ja->sectors_free = ca->mi.bucket_size - wrote;
1048 mutex_unlock(&jlist->lock);
1050 if (ja->bucket_seq[ja->cur_idx] &&
1051 ja->sectors_free == ca->mi.bucket_size) {
1052 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1053 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1054 for (i = 0; i < 3; i++) {
1055 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
1057 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1059 ja->sectors_free = 0;
1063 * Set dirty_idx to indicate the entire journal is full and needs to be
1064 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1065 * pinned when it first runs:
1067 ja->discard_idx = ja->dirty_idx_ondisk =
1068 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1070 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1071 kvpfree(buf.data, buf.size);
1072 percpu_ref_put(&ca->io_ref);
1076 mutex_lock(&jlist->lock);
1078 mutex_unlock(&jlist->lock);
1082 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1083 struct journal_replay *j)
1087 for (i = 0; i < j->nr_ptrs; i++) {
1088 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1091 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1094 prt_printf(out, " ");
1095 prt_printf(out, "%u:%u:%u (sector %llu)",
1098 j->ptrs[i].bucket_offset,
1103 int bch2_journal_read(struct bch_fs *c,
1108 struct journal_list jlist;
1109 struct journal_replay *i, **_i, *prev = NULL;
1110 struct genradix_iter radix_iter;
1113 struct printbuf buf = PRINTBUF;
1114 bool degraded = false, last_write_torn = false;
1118 closure_init_stack(&jlist.cl);
1119 mutex_init(&jlist.lock);
1123 for_each_member_device(ca, c, iter) {
1124 if (!c->opts.fsck &&
1125 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1128 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1129 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1130 percpu_ref_tryget(&ca->io_ref))
1131 closure_call(&ca->journal.read,
1132 bch2_journal_read_device,
1139 closure_sync(&jlist.cl);
1149 * Find most recent flush entry, and ignore newer non flush entries -
1150 * those entries will be blacklisted:
1152 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1153 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
1157 if (!i || i->ignore)
1161 *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1163 if (JSET_NO_FLUSH(&i->j)) {
1168 if (!last_write_torn && !i->csum_good) {
1169 last_write_torn = true;
1174 if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1175 c, le32_to_cpu(i->j.version), &i->j, NULL,
1176 "invalid journal entry: last_seq > seq (%llu > %llu)",
1177 le64_to_cpu(i->j.last_seq),
1178 le64_to_cpu(i->j.seq)))
1179 i->j.last_seq = i->j.seq;
1181 *last_seq = le64_to_cpu(i->j.last_seq);
1182 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1187 bch_info(c, "journal read done, but no entries found");
1192 fsck_err(c, "journal read done, but no entries found after dropping non-flushes");
1196 bch_info(c, "journal read done, replaying entries %llu-%llu",
1197 *last_seq, *blacklist_seq - 1);
1199 if (*start_seq != *blacklist_seq)
1200 bch_info(c, "dropped unflushed entries %llu-%llu",
1201 *blacklist_seq, *start_seq - 1);
1203 /* Drop blacklisted entries and entries older than last_seq: */
1204 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1207 if (!i || i->ignore)
1210 seq = le64_to_cpu(i->j.seq);
1211 if (seq < *last_seq) {
1212 journal_replay_free(c, i);
1216 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1217 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1218 "found blacklisted journal entry %llu", seq);
1223 /* Check for missing entries: */
1225 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1228 if (!i || i->ignore)
1231 BUG_ON(seq > le64_to_cpu(i->j.seq));
1233 while (seq < le64_to_cpu(i->j.seq)) {
1234 u64 missing_start, missing_end;
1235 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1237 while (seq < le64_to_cpu(i->j.seq) &&
1238 bch2_journal_seq_is_blacklisted(c, seq, false))
1241 if (seq == le64_to_cpu(i->j.seq))
1244 missing_start = seq;
1246 while (seq < le64_to_cpu(i->j.seq) &&
1247 !bch2_journal_seq_is_blacklisted(c, seq, false))
1251 bch2_journal_ptrs_to_text(&buf1, c, prev);
1252 prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1254 prt_printf(&buf1, "(none)");
1255 bch2_journal_ptrs_to_text(&buf2, c, i);
1257 missing_end = seq - 1;
1258 fsck_err(c, "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1261 missing_start, missing_end,
1262 *last_seq, *blacklist_seq - 1,
1263 buf1.buf, buf2.buf);
1265 printbuf_exit(&buf1);
1266 printbuf_exit(&buf2);
1273 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1274 struct bch_replicas_padded replicas = {
1275 .e.data_type = BCH_DATA_journal,
1281 if (!i || i->ignore)
1284 for (ptr = 0; ptr < i->nr_ptrs; ptr++) {
1285 struct bch_dev *ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
1287 if (!i->ptrs[ptr].csum_good)
1288 bch_err_dev_offset(ca, i->ptrs[ptr].sector,
1289 "invalid journal checksum, seq %llu%s",
1290 le64_to_cpu(i->j.seq),
1291 i->csum_good ? " (had good copy on another device)" : "");
1294 ret = jset_validate(c,
1295 bch_dev_bkey_exists(c, i->ptrs[0].dev),
1302 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1303 replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1305 bch2_replicas_entry_sort(&replicas.e);
1307 printbuf_reset(&buf);
1308 bch2_replicas_entry_to_text(&buf, &replicas.e);
1311 !bch2_replicas_marked(c, &replicas.e) &&
1312 (le64_to_cpu(i->j.seq) == *last_seq ||
1313 fsck_err(c, "superblock not marked as containing replicas for journal entry %llu\n %s",
1314 le64_to_cpu(i->j.seq), buf.buf))) {
1315 ret = bch2_mark_replicas(c, &replicas.e);
1322 printbuf_exit(&buf);
1326 /* journal write: */
1328 static void __journal_write_alloc(struct journal *j,
1329 struct journal_buf *w,
1330 struct dev_alloc_list *devs_sorted,
1333 unsigned replicas_want)
1335 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1336 struct journal_device *ja;
1340 if (*replicas >= replicas_want)
1343 for (i = 0; i < devs_sorted->nr; i++) {
1344 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1351 * Check that we can use this device, and aren't already using
1354 if (!ca->mi.durability ||
1355 ca->mi.state != BCH_MEMBER_STATE_rw ||
1357 bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) ||
1358 sectors > ja->sectors_free)
1361 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1363 bch2_bkey_append_ptr(&w->key,
1364 (struct bch_extent_ptr) {
1365 .offset = bucket_to_sector(ca,
1366 ja->buckets[ja->cur_idx]) +
1367 ca->mi.bucket_size -
1372 ja->sectors_free -= sectors;
1373 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1375 *replicas += ca->mi.durability;
1377 if (*replicas >= replicas_want)
1383 * journal_next_bucket - move on to the next journal bucket if possible
1385 static int journal_write_alloc(struct journal *j, struct journal_buf *w,
1388 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1389 struct bch_devs_mask devs;
1390 struct journal_device *ja;
1392 struct dev_alloc_list devs_sorted;
1393 unsigned target = c->opts.metadata_target ?:
1394 c->opts.foreground_target;
1395 unsigned i, replicas = 0, replicas_want =
1396 READ_ONCE(c->opts.metadata_replicas);
1400 devs = target_rw_devs(c, BCH_DATA_journal, target);
1402 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1404 __journal_write_alloc(j, w, &devs_sorted,
1405 sectors, &replicas, replicas_want);
1407 if (replicas >= replicas_want)
1410 for (i = 0; i < devs_sorted.nr; i++) {
1411 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1417 if (sectors > ja->sectors_free &&
1418 sectors <= ca->mi.bucket_size &&
1419 bch2_journal_dev_buckets_available(j, ja,
1420 journal_space_discarded)) {
1421 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1422 ja->sectors_free = ca->mi.bucket_size;
1425 * ja->bucket_seq[ja->cur_idx] must always have
1426 * something sensible:
1428 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1432 __journal_write_alloc(j, w, &devs_sorted,
1433 sectors, &replicas, replicas_want);
1435 if (replicas < replicas_want && target) {
1436 /* Retry from all devices: */
1443 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1445 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1448 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1450 /* we aren't holding j->lock: */
1451 unsigned new_size = READ_ONCE(j->buf_size_want);
1454 if (buf->buf_size >= new_size)
1457 new_buf = kvpmalloc(new_size, GFP_NOFS|__GFP_NOWARN);
1461 memcpy(new_buf, buf->data, buf->buf_size);
1463 spin_lock(&j->lock);
1464 swap(buf->data, new_buf);
1465 swap(buf->buf_size, new_size);
1466 spin_unlock(&j->lock);
1468 kvpfree(new_buf, new_size);
1471 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1473 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1476 static void journal_write_done(struct closure *cl)
1478 struct journal *j = container_of(cl, struct journal, io);
1479 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1480 struct journal_buf *w = journal_last_unwritten_buf(j);
1481 struct bch_replicas_padded replicas;
1482 union journal_res_state old, new;
1486 bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1487 ? j->flush_write_time
1488 : j->noflush_write_time, j->write_start_time);
1490 if (!w->devs_written.nr) {
1491 bch_err(c, "unable to write journal to sufficient devices");
1494 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1496 if (bch2_mark_replicas(c, &replicas.e))
1501 bch2_fatal_error(c);
1503 spin_lock(&j->lock);
1504 seq = le64_to_cpu(w->data->seq);
1506 if (seq >= j->pin.front)
1507 journal_seq_pin(j, seq)->devs = w->devs_written;
1510 if (!JSET_NO_FLUSH(w->data)) {
1511 j->flushed_seq_ondisk = seq;
1512 j->last_seq_ondisk = w->last_seq;
1514 bch2_do_discards(c);
1515 closure_wake_up(&c->freelist_wait);
1517 bch2_reset_alloc_cursors(c);
1519 } else if (!j->err_seq || seq < j->err_seq)
1522 j->seq_ondisk = seq;
1525 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1528 * Must come before signaling write completion, for
1529 * bch2_fs_journal_stop():
1531 if (j->watermark != BCH_WATERMARK_stripe)
1532 journal_reclaim_kick(&c->journal);
1534 /* also must come before signalling write completion: */
1535 closure_debug_destroy(cl);
1537 v = atomic64_read(&j->reservations.counter);
1540 BUG_ON(journal_state_count(new, new.unwritten_idx));
1542 new.unwritten_idx++;
1543 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1544 old.v, new.v)) != old.v);
1546 bch2_journal_space_available(j);
1548 closure_wake_up(&w->wait);
1551 if (!journal_state_count(new, new.unwritten_idx) &&
1552 journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1553 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1554 } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1555 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1556 struct journal_buf *buf = journal_cur_buf(j);
1557 long delta = buf->expires - jiffies;
1560 * We don't close a journal entry to write it while there's
1561 * previous entries still in flight - the current journal entry
1562 * might want to be written now:
1565 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1568 spin_unlock(&j->lock);
1571 static void journal_write_endio(struct bio *bio)
1573 struct bch_dev *ca = bio->bi_private;
1574 struct journal *j = &ca->fs->journal;
1575 struct journal_buf *w = journal_last_unwritten_buf(j);
1576 unsigned long flags;
1578 if (bch2_dev_io_err_on(bio->bi_status, ca, "error writing journal entry %llu: %s",
1579 le64_to_cpu(w->data->seq),
1580 bch2_blk_status_to_str(bio->bi_status)) ||
1581 bch2_meta_write_fault("journal")) {
1582 spin_lock_irqsave(&j->err_lock, flags);
1583 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1584 spin_unlock_irqrestore(&j->err_lock, flags);
1587 closure_put(&j->io);
1588 percpu_ref_put(&ca->io_ref);
1591 static void do_journal_write(struct closure *cl)
1593 struct journal *j = container_of(cl, struct journal, io);
1594 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1596 struct journal_buf *w = journal_last_unwritten_buf(j);
1597 struct bch_extent_ptr *ptr;
1599 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1601 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1602 ca = bch_dev_bkey_exists(c, ptr->dev);
1603 if (!percpu_ref_tryget(&ca->io_ref)) {
1605 bch_err(c, "missing device for journal write\n");
1609 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1612 bio = ca->journal.bio;
1613 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1614 bio->bi_iter.bi_sector = ptr->offset;
1615 bio->bi_end_io = journal_write_endio;
1616 bio->bi_private = ca;
1618 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1619 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1621 if (!JSET_NO_FLUSH(w->data))
1622 bio->bi_opf |= REQ_FUA;
1623 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1624 bio->bi_opf |= REQ_PREFLUSH;
1626 bch2_bio_map(bio, w->data, sectors << 9);
1628 trace_and_count(c, journal_write, bio);
1629 closure_bio_submit(bio, cl);
1631 ca->journal.bucket_seq[ca->journal.cur_idx] =
1632 le64_to_cpu(w->data->seq);
1635 continue_at(cl, journal_write_done, c->io_complete_wq);
1638 static void bch2_journal_entries_postprocess(struct bch_fs *c, struct jset *jset)
1640 struct jset_entry *i, *next, *prev = NULL;
1643 * Simple compaction, dropping empty jset_entries (from journal
1644 * reservations that weren't fully used) and merging jset_entries that
1647 * If we wanted to be really fancy here, we could sort all the keys in
1648 * the jset and drop keys that were overwritten - probably not worth it:
1650 vstruct_for_each_safe(jset, i, next) {
1651 unsigned u64s = le16_to_cpu(i->u64s);
1657 if (i->type == BCH_JSET_ENTRY_btree_root)
1658 bch2_journal_entry_to_btree_root(c, i);
1660 /* Can we merge with previous entry? */
1662 i->btree_id == prev->btree_id &&
1663 i->level == prev->level &&
1664 i->type == prev->type &&
1665 i->type == BCH_JSET_ENTRY_btree_keys &&
1666 le16_to_cpu(prev->u64s) + u64s <= U16_MAX) {
1667 memmove_u64s_down(vstruct_next(prev),
1670 le16_add_cpu(&prev->u64s, u64s);
1674 /* Couldn't merge, move i into new position (after prev): */
1675 prev = prev ? vstruct_next(prev) : jset->start;
1677 memmove_u64s_down(prev, i, jset_u64s(u64s));
1680 prev = prev ? vstruct_next(prev) : jset->start;
1681 jset->u64s = cpu_to_le32((u64 *) prev - jset->_data);
1684 void bch2_journal_write(struct closure *cl)
1686 struct journal *j = container_of(cl, struct journal, io);
1687 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1689 struct journal_buf *w = journal_last_unwritten_buf(j);
1690 struct bch_replicas_padded replicas;
1691 struct jset_entry *start, *end;
1694 struct printbuf journal_debug_buf = PRINTBUF;
1695 bool validate_before_checksum = false;
1696 unsigned i, sectors, bytes, u64s, nr_rw_members = 0;
1699 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1701 journal_buf_realloc(j, w);
1704 j->write_start_time = local_clock();
1706 spin_lock(&j->lock);
1709 * If the journal is in an error state - we did an emergency shutdown -
1710 * we prefer to continue doing journal writes. We just mark them as
1711 * noflush so they'll never be used, but they'll still be visible by the
1712 * list_journal tool - this helps in debugging.
1714 * There's a caveat: the first journal write after marking the
1715 * superblock dirty must always be a flush write, because on startup
1716 * from a clean shutdown we didn't necessarily read the journal and the
1717 * new journal write might overwrite whatever was in the journal
1718 * previously - we can't leave the journal without any flush writes in
1721 * So if we're in an error state, and we're still starting up, we don't
1722 * write anything at all.
1724 if (!test_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags) &&
1725 (bch2_journal_error(j) ||
1728 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1729 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags)))) {
1731 SET_JSET_NO_FLUSH(jset, true);
1735 j->nr_noflush_writes++;
1736 } else if (!bch2_journal_error(j)) {
1737 j->last_flush_write = jiffies;
1738 j->nr_flush_writes++;
1739 clear_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags);
1741 spin_unlock(&j->lock);
1744 spin_unlock(&j->lock);
1747 * New btree roots are set by journalling them; when the journal entry
1748 * gets written we have to propagate them to c->btree_roots
1750 * But, every journal entry we write has to contain all the btree roots
1751 * (at least for now); so after we copy btree roots to c->btree_roots we
1752 * have to get any missing btree roots and add them to this journal
1756 bch2_journal_entries_postprocess(c, jset);
1758 start = end = vstruct_last(jset);
1760 end = bch2_btree_roots_to_journal_entries(c, jset->start, end);
1762 bch2_journal_super_entries_add_common(c, &end,
1763 le64_to_cpu(jset->seq));
1764 u64s = (u64 *) end - (u64 *) start;
1765 BUG_ON(u64s > j->entry_u64s_reserved);
1767 le32_add_cpu(&jset->u64s, u64s);
1769 sectors = vstruct_sectors(jset, c->block_bits);
1770 bytes = vstruct_bytes(jset);
1772 if (sectors > w->sectors) {
1773 bch2_fs_fatal_error(c, "aieeee! journal write overran available space, %zu > %u (extra %u reserved %u/%u)",
1774 vstruct_bytes(jset), w->sectors << 9,
1775 u64s, w->u64s_reserved, j->entry_u64s_reserved);
1779 jset->magic = cpu_to_le64(jset_magic(c));
1780 jset->version = cpu_to_le32(c->sb.version);
1782 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1783 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1785 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1786 j->last_empty_seq = le64_to_cpu(jset->seq);
1788 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1789 validate_before_checksum = true;
1791 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1792 validate_before_checksum = true;
1794 if (validate_before_checksum &&
1795 jset_validate(c, NULL, jset, 0, WRITE))
1798 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1799 jset->encrypted_start,
1800 vstruct_end(jset) - (void *) jset->encrypted_start);
1801 if (bch2_fs_fatal_err_on(ret, c,
1802 "error decrypting journal entry: %i", ret))
1805 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1806 journal_nonce(jset), jset);
1808 if (!validate_before_checksum &&
1809 jset_validate(c, NULL, jset, 0, WRITE))
1812 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1815 spin_lock(&j->lock);
1816 ret = journal_write_alloc(j, w, sectors);
1818 if (ret && j->can_discard) {
1819 spin_unlock(&j->lock);
1820 bch2_journal_do_discards(j);
1825 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1828 * write is allocated, no longer need to account for it in
1829 * bch2_journal_space_available():
1834 * journal entry has been compacted and allocated, recalculate space
1837 bch2_journal_space_available(j);
1838 spin_unlock(&j->lock);
1841 bch_err(c, "Unable to allocate journal write:\n%s",
1842 journal_debug_buf.buf);
1843 printbuf_exit(&journal_debug_buf);
1847 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1849 if (c->opts.nochanges)
1852 for_each_rw_member(ca, c, i)
1855 if (nr_rw_members > 1)
1856 w->separate_flush = true;
1859 * Mark journal replicas before we submit the write to guarantee
1860 * recovery will find the journal entries after a crash.
1862 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1864 ret = bch2_mark_replicas(c, &replicas.e);
1868 if (!JSET_NO_FLUSH(jset) && w->separate_flush) {
1869 for_each_rw_member(ca, c, i) {
1870 percpu_ref_get(&ca->io_ref);
1872 bio = ca->journal.bio;
1873 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_FLUSH);
1874 bio->bi_end_io = journal_write_endio;
1875 bio->bi_private = ca;
1876 closure_bio_submit(bio, cl);
1880 continue_at(cl, do_journal_write, c->io_complete_wq);
1883 continue_at(cl, journal_write_done, c->io_complete_wq);
1886 bch2_fatal_error(c);
1887 continue_at(cl, journal_write_done, c->io_complete_wq);