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"
18 #include <trace/events/bcachefs.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)),
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);
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,
213 struct jset_entry *entry)
215 prt_str(out, "invalid journal entry ");
217 prt_printf(out, "%s ", bch2_jset_entry_types[entry->type]);
220 prt_printf(out, "in superblock");
222 prt_printf(out, "at seq %llu", le64_to_cpu(jset->seq));
224 prt_printf(out, "at offset %zi/%u seq %llu",
225 (u64 *) entry - jset->_data,
226 le32_to_cpu(jset->u64s),
227 le64_to_cpu(jset->seq));
231 #define journal_entry_err(c, jset, entry, msg, ...) \
233 struct printbuf buf = PRINTBUF; \
235 journal_entry_err_msg(&buf, jset, entry); \
236 prt_printf(&buf, msg, ##__VA_ARGS__); \
240 mustfix_fsck_err(c, "%s", buf.buf); \
243 bch_err(c, "corrupt metadata before write: %s\n", buf.buf);\
244 if (bch2_fs_inconsistent(c)) { \
245 ret = -BCH_ERR_fsck_errors_not_fixed; \
251 printbuf_exit(&buf); \
255 #define journal_entry_err_on(cond, c, jset, entry, msg, ...) \
256 ((cond) ? journal_entry_err(c, jset, entry, msg, ##__VA_ARGS__) : false)
258 #define FSCK_DELETED_KEY 5
260 static int journal_validate_key(struct bch_fs *c,
262 struct jset_entry *entry,
263 unsigned level, enum btree_id btree_id,
265 unsigned version, int big_endian, int write)
267 void *next = vstruct_next(entry);
268 struct printbuf buf = PRINTBUF;
271 if (journal_entry_err_on(!k->k.u64s, c, jset, entry, "k->u64s 0")) {
272 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
273 journal_entry_null_range(vstruct_next(entry), next);
274 return FSCK_DELETED_KEY;
277 if (journal_entry_err_on((void *) bkey_next(k) >
278 (void *) vstruct_next(entry),
280 "extends past end of journal entry")) {
281 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
282 journal_entry_null_range(vstruct_next(entry), next);
283 return FSCK_DELETED_KEY;
286 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT,
288 "bad format %u", k->k.format)) {
289 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
290 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
291 journal_entry_null_range(vstruct_next(entry), next);
292 return FSCK_DELETED_KEY;
296 bch2_bkey_compat(level, btree_id, version, big_endian,
297 write, NULL, bkey_to_packed(k));
299 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
300 __btree_node_type(level, btree_id), write, &buf)) {
301 printbuf_reset(&buf);
302 prt_printf(&buf, "invalid journal entry %s at offset %zi/%u seq %llu:",
303 bch2_jset_entry_types[entry->type],
304 (u64 *) entry - jset->_data,
305 le32_to_cpu(jset->u64s),
306 le64_to_cpu(jset->seq));
308 printbuf_indent_add(&buf, 2);
310 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
312 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
313 __btree_node_type(level, btree_id), write, &buf);
315 mustfix_fsck_err(c, "%s", buf.buf);
317 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
318 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
319 journal_entry_null_range(vstruct_next(entry), next);
322 return FSCK_DELETED_KEY;
326 bch2_bkey_compat(level, btree_id, version, big_endian,
327 write, NULL, bkey_to_packed(k));
333 static int journal_entry_btree_keys_validate(struct bch_fs *c,
335 struct jset_entry *entry,
336 unsigned version, int big_endian, int write)
338 struct bkey_i *k = entry->start;
340 while (k != vstruct_last(entry)) {
341 int ret = journal_validate_key(c, jset, entry,
344 k, version, big_endian, write|BKEY_INVALID_FROM_JOURNAL);
345 if (ret == FSCK_DELETED_KEY)
354 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
355 struct jset_entry *entry)
360 vstruct_for_each(entry, k) {
363 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
365 prt_printf(out, "btree=%s l=%u ", bch2_btree_ids[entry->btree_id], entry->level);
366 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
371 static int journal_entry_btree_root_validate(struct bch_fs *c,
373 struct jset_entry *entry,
374 unsigned version, int big_endian, int write)
376 struct bkey_i *k = entry->start;
379 if (journal_entry_err_on(!entry->u64s ||
380 le16_to_cpu(entry->u64s) != k->k.u64s,
382 "invalid btree root journal entry: wrong number of keys")) {
383 void *next = vstruct_next(entry);
385 * we don't want to null out this jset_entry,
386 * just the contents, so that later we can tell
387 * we were _supposed_ to have a btree root
390 journal_entry_null_range(vstruct_next(entry), next);
394 return journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
395 version, big_endian, write);
400 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
401 struct jset_entry *entry)
403 journal_entry_btree_keys_to_text(out, c, entry);
406 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
408 struct jset_entry *entry,
409 unsigned version, int big_endian, int write)
411 /* obsolete, don't care: */
415 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
416 struct jset_entry *entry)
420 static int journal_entry_blacklist_validate(struct bch_fs *c,
422 struct jset_entry *entry,
423 unsigned version, int big_endian, int write)
427 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
429 "invalid journal seq blacklist entry: bad size")) {
430 journal_entry_null_range(entry, vstruct_next(entry));
436 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
437 struct jset_entry *entry)
439 struct jset_entry_blacklist *bl =
440 container_of(entry, struct jset_entry_blacklist, entry);
442 prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
445 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
447 struct jset_entry *entry,
448 unsigned version, int big_endian, int write)
450 struct jset_entry_blacklist_v2 *bl_entry;
453 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
455 "invalid journal seq blacklist entry: bad size")) {
456 journal_entry_null_range(entry, vstruct_next(entry));
460 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
462 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
463 le64_to_cpu(bl_entry->end),
465 "invalid journal seq blacklist entry: start > end")) {
466 journal_entry_null_range(entry, vstruct_next(entry));
473 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
474 struct jset_entry *entry)
476 struct jset_entry_blacklist_v2 *bl =
477 container_of(entry, struct jset_entry_blacklist_v2, entry);
479 prt_printf(out, "start=%llu end=%llu",
480 le64_to_cpu(bl->start),
481 le64_to_cpu(bl->end));
484 static int journal_entry_usage_validate(struct bch_fs *c,
486 struct jset_entry *entry,
487 unsigned version, int big_endian, int write)
489 struct jset_entry_usage *u =
490 container_of(entry, struct jset_entry_usage, entry);
491 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
494 if (journal_entry_err_on(bytes < sizeof(*u),
496 "invalid journal entry usage: bad size")) {
497 journal_entry_null_range(entry, vstruct_next(entry));
505 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
506 struct jset_entry *entry)
508 struct jset_entry_usage *u =
509 container_of(entry, struct jset_entry_usage, entry);
511 prt_printf(out, "type=%s v=%llu",
512 bch2_fs_usage_types[u->entry.btree_id],
516 static int journal_entry_data_usage_validate(struct bch_fs *c,
518 struct jset_entry *entry,
519 unsigned version, int big_endian, int write)
521 struct jset_entry_data_usage *u =
522 container_of(entry, struct jset_entry_data_usage, entry);
523 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
526 if (journal_entry_err_on(bytes < sizeof(*u) ||
527 bytes < sizeof(*u) + u->r.nr_devs,
529 "invalid journal entry usage: bad size")) {
530 journal_entry_null_range(entry, vstruct_next(entry));
538 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
539 struct jset_entry *entry)
541 struct jset_entry_data_usage *u =
542 container_of(entry, struct jset_entry_data_usage, entry);
544 bch2_replicas_entry_to_text(out, &u->r);
545 prt_printf(out, "=%llu", le64_to_cpu(u->v));
548 static int journal_entry_clock_validate(struct bch_fs *c,
550 struct jset_entry *entry,
551 unsigned version, int big_endian, int write)
553 struct jset_entry_clock *clock =
554 container_of(entry, struct jset_entry_clock, entry);
555 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
558 if (journal_entry_err_on(bytes != sizeof(*clock),
559 c, jset, entry, "bad size")) {
560 journal_entry_null_range(entry, vstruct_next(entry));
564 if (journal_entry_err_on(clock->rw > 1,
565 c, jset, entry, "bad rw")) {
566 journal_entry_null_range(entry, vstruct_next(entry));
574 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
575 struct jset_entry *entry)
577 struct jset_entry_clock *clock =
578 container_of(entry, struct jset_entry_clock, entry);
580 prt_printf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
583 static int journal_entry_dev_usage_validate(struct bch_fs *c,
585 struct jset_entry *entry,
586 unsigned version, int big_endian, int write)
588 struct jset_entry_dev_usage *u =
589 container_of(entry, struct jset_entry_dev_usage, entry);
590 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
591 unsigned expected = sizeof(*u);
595 if (journal_entry_err_on(bytes < expected,
596 c, jset, entry, "bad size (%u < %u)",
598 journal_entry_null_range(entry, vstruct_next(entry));
602 dev = le32_to_cpu(u->dev);
604 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
605 c, jset, entry, "bad dev")) {
606 journal_entry_null_range(entry, vstruct_next(entry));
610 if (journal_entry_err_on(u->pad,
611 c, jset, entry, "bad pad")) {
612 journal_entry_null_range(entry, vstruct_next(entry));
620 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
621 struct jset_entry *entry)
623 struct jset_entry_dev_usage *u =
624 container_of(entry, struct jset_entry_dev_usage, entry);
625 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
627 prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
629 for (i = 0; i < nr_types; i++) {
631 prt_printf(out, " %s", bch2_data_types[i]);
633 prt_printf(out, " (unknown data type %u)", i);
634 prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
635 le64_to_cpu(u->d[i].buckets),
636 le64_to_cpu(u->d[i].sectors),
637 le64_to_cpu(u->d[i].fragmented));
640 prt_printf(out, " buckets_ec: %llu", le64_to_cpu(u->buckets_ec));
643 static int journal_entry_log_validate(struct bch_fs *c,
645 struct jset_entry *entry,
646 unsigned version, int big_endian, int write)
651 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
652 struct jset_entry *entry)
654 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
655 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
657 prt_printf(out, "%.*s", bytes, l->d);
660 static int journal_entry_overwrite_validate(struct bch_fs *c,
662 struct jset_entry *entry,
663 unsigned version, int big_endian, int write)
665 return journal_entry_btree_keys_validate(c, jset, entry,
666 version, big_endian, READ);
669 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
670 struct jset_entry *entry)
672 journal_entry_btree_keys_to_text(out, c, entry);
675 struct jset_entry_ops {
676 int (*validate)(struct bch_fs *, struct jset *,
677 struct jset_entry *, unsigned, int, int);
678 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
681 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
683 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
684 .validate = journal_entry_##f##_validate, \
685 .to_text = journal_entry_##f##_to_text, \
687 BCH_JSET_ENTRY_TYPES()
691 int bch2_journal_entry_validate(struct bch_fs *c,
693 struct jset_entry *entry,
694 unsigned version, int big_endian, int write)
696 return entry->type < BCH_JSET_ENTRY_NR
697 ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
698 version, big_endian, write)
702 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
703 struct jset_entry *entry)
705 if (entry->type < BCH_JSET_ENTRY_NR) {
706 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
707 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
709 prt_printf(out, "(unknown type %u)", entry->type);
713 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
716 struct jset_entry *entry;
719 vstruct_for_each(jset, entry) {
720 if (journal_entry_err_on(vstruct_next(entry) >
721 vstruct_last(jset), c, jset, entry,
722 "journal entry extends past end of jset")) {
723 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
727 ret = bch2_journal_entry_validate(c, jset, entry,
728 le32_to_cpu(jset->version),
729 JSET_BIG_ENDIAN(jset), write);
737 static int jset_validate(struct bch_fs *c,
739 struct jset *jset, u64 sector,
745 if (le64_to_cpu(jset->magic) != jset_magic(c))
746 return JOURNAL_ENTRY_NONE;
748 version = le32_to_cpu(jset->version);
749 if (journal_entry_err_on((version != BCH_JSET_VERSION_OLD &&
750 version < bcachefs_metadata_version_min) ||
751 version >= bcachefs_metadata_version_max,
753 "%s sector %llu seq %llu: unknown journal entry version %u",
754 ca ? ca->name : c->name,
755 sector, le64_to_cpu(jset->seq),
757 /* don't try to continue: */
761 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
763 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
764 ca ? ca->name : c->name,
765 sector, le64_to_cpu(jset->seq),
766 JSET_CSUM_TYPE(jset)))
767 ret = JOURNAL_ENTRY_BAD;
769 /* last_seq is ignored when JSET_NO_FLUSH is true */
770 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
771 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
773 "invalid journal entry: last_seq > seq (%llu > %llu)",
774 le64_to_cpu(jset->last_seq),
775 le64_to_cpu(jset->seq))) {
776 jset->last_seq = jset->seq;
777 return JOURNAL_ENTRY_BAD;
780 ret = jset_validate_entries(c, jset, write);
785 static int jset_validate_early(struct bch_fs *c,
787 struct jset *jset, u64 sector,
788 unsigned bucket_sectors_left,
789 unsigned sectors_read)
791 size_t bytes = vstruct_bytes(jset);
796 if (le64_to_cpu(jset->magic) != jset_magic(c))
797 return JOURNAL_ENTRY_NONE;
799 version = le32_to_cpu(jset->version);
800 if (journal_entry_err_on((version != BCH_JSET_VERSION_OLD &&
801 version < bcachefs_metadata_version_min) ||
802 version >= bcachefs_metadata_version_max,
804 "%s sector %llu seq %llu: unknown journal entry version %u",
805 ca ? ca->name : c->name,
806 sector, le64_to_cpu(jset->seq),
808 /* don't try to continue: */
812 if (bytes > (sectors_read << 9) &&
813 sectors_read < bucket_sectors_left)
814 return JOURNAL_ENTRY_REREAD;
816 if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
818 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
819 ca ? ca->name : c->name,
820 sector, le64_to_cpu(jset->seq), bytes))
821 le32_add_cpu(&jset->u64s,
822 -((bytes - (bucket_sectors_left << 9)) / 8));
827 struct journal_read_buf {
832 static int journal_read_buf_realloc(struct journal_read_buf *b,
837 /* the bios are sized for this many pages, max: */
838 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
841 new_size = roundup_pow_of_two(new_size);
842 n = kvpmalloc(new_size, GFP_KERNEL);
846 kvpfree(b->data, b->size);
852 static int journal_read_bucket(struct bch_dev *ca,
853 struct journal_read_buf *buf,
854 struct journal_list *jlist,
857 struct bch_fs *c = ca->fs;
858 struct journal_device *ja = &ca->journal;
859 struct jset *j = NULL;
860 unsigned sectors, sectors_read = 0;
861 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
862 end = offset + ca->mi.bucket_size;
863 bool saw_bad = false, csum_good;
866 pr_debug("reading %u", bucket);
868 while (offset < end) {
873 sectors_read = min_t(unsigned,
874 end - offset, buf->size >> 9);
875 nr_bvecs = buf_pages(buf->data, sectors_read << 9);
877 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
878 bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
880 bio->bi_iter.bi_sector = offset;
881 bch2_bio_map(bio, buf->data, sectors_read << 9);
883 ret = submit_bio_wait(bio);
886 if (bch2_dev_io_err_on(ret, ca,
887 "journal read error: sector %llu",
889 bch2_meta_read_fault("journal")) {
891 * We don't error out of the recovery process
892 * here, since the relevant journal entry may be
893 * found on a different device, and missing or
894 * no journal entries will be handled later
902 ret = jset_validate_early(c, ca, j, offset,
903 end - offset, sectors_read);
906 sectors = vstruct_sectors(j, c->block_bits);
908 case JOURNAL_ENTRY_REREAD:
909 if (vstruct_bytes(j) > buf->size) {
910 ret = journal_read_buf_realloc(buf,
916 case JOURNAL_ENTRY_NONE:
920 * On checksum error we don't really trust the size
921 * field of the journal entry we read, so try reading
922 * again at next block boundary:
924 sectors = block_sectors(c);
931 * This happens sometimes if we don't have discards on -
932 * when we've partially overwritten a bucket with new
933 * journal entries. We don't need the rest of the
936 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
939 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
941 csum_good = jset_csum_good(c, j);
945 ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
947 vstruct_end(j) - (void *) j->encrypted_start);
948 bch2_fs_fatal_err_on(ret, c,
949 "error decrypting journal entry: %i", ret);
951 mutex_lock(&jlist->lock);
952 ret = journal_entry_add(c, ca, (struct journal_ptr) {
953 .csum_good = csum_good,
956 .bucket_offset = offset -
957 bucket_to_sector(ca, ja->buckets[bucket]),
960 mutex_unlock(&jlist->lock);
963 case JOURNAL_ENTRY_ADD_OK:
965 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
973 sectors_read -= sectors;
974 j = ((void *) j) + (sectors << 9);
980 static void bch2_journal_read_device(struct closure *cl)
982 struct journal_device *ja =
983 container_of(cl, struct journal_device, read);
984 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
985 struct bch_fs *c = ca->fs;
986 struct journal_list *jlist =
987 container_of(cl->parent, struct journal_list, cl);
988 struct journal_replay *r, **_r;
989 struct genradix_iter iter;
990 struct journal_read_buf buf = { NULL, 0 };
997 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1001 pr_debug("%u journal buckets", ja->nr);
1003 for (i = 0; i < ja->nr; i++) {
1004 ret = journal_read_bucket(ca, &buf, jlist, i);
1009 ja->sectors_free = ca->mi.bucket_size;
1011 mutex_lock(&jlist->lock);
1012 genradix_for_each_reverse(&c->journal_entries, iter, _r) {
1018 for (i = 0; i < r->nr_ptrs; i++) {
1019 if (r->ptrs[i].dev == ca->dev_idx) {
1020 unsigned wrote = bucket_remainder(ca, r->ptrs[i].sector) +
1021 vstruct_sectors(&r->j, c->block_bits);
1023 ja->cur_idx = r->ptrs[i].bucket;
1024 ja->sectors_free = ca->mi.bucket_size - wrote;
1030 mutex_unlock(&jlist->lock);
1032 if (ja->bucket_seq[ja->cur_idx] &&
1033 ja->sectors_free == ca->mi.bucket_size) {
1034 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1035 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1036 for (i = 0; i < 3; i++) {
1037 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
1038 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1040 ja->sectors_free = 0;
1044 * Set dirty_idx to indicate the entire journal is full and needs to be
1045 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1046 * pinned when it first runs:
1048 ja->discard_idx = ja->dirty_idx_ondisk =
1049 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1051 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1052 kvpfree(buf.data, buf.size);
1053 percpu_ref_put(&ca->io_ref);
1057 mutex_lock(&jlist->lock);
1059 mutex_unlock(&jlist->lock);
1063 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1064 struct journal_replay *j)
1068 for (i = 0; i < j->nr_ptrs; i++) {
1069 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1072 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1075 prt_printf(out, " ");
1076 prt_printf(out, "%u:%u:%u (sector %llu)",
1079 j->ptrs[i].bucket_offset,
1084 int bch2_journal_read(struct bch_fs *c,
1089 struct journal_list jlist;
1090 struct journal_replay *i, **_i, *prev = NULL;
1091 struct genradix_iter radix_iter;
1094 struct printbuf buf = PRINTBUF;
1095 bool degraded = false, last_write_torn = false;
1099 closure_init_stack(&jlist.cl);
1100 mutex_init(&jlist.lock);
1104 for_each_member_device(ca, c, iter) {
1105 if (!c->opts.fsck &&
1106 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1109 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1110 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1111 percpu_ref_tryget(&ca->io_ref))
1112 closure_call(&ca->journal.read,
1113 bch2_journal_read_device,
1120 closure_sync(&jlist.cl);
1130 * Find most recent flush entry, and ignore newer non flush entries -
1131 * those entries will be blacklisted:
1133 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1138 if (!i || i->ignore)
1142 *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1144 if (JSET_NO_FLUSH(&i->j)) {
1149 if (!last_write_torn && !i->csum_good) {
1150 last_write_torn = true;
1155 if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1157 "invalid journal entry: last_seq > seq (%llu > %llu)",
1158 le64_to_cpu(i->j.last_seq),
1159 le64_to_cpu(i->j.seq)))
1160 i->j.last_seq = i->j.seq;
1162 *last_seq = le64_to_cpu(i->j.last_seq);
1163 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1168 bch_info(c, "journal read done, but no entries found");
1173 fsck_err(c, "journal read done, but no entries found after dropping non-flushes");
1177 bch_info(c, "journal read done, replaying entries %llu-%llu",
1178 *last_seq, *blacklist_seq - 1);
1180 if (*start_seq != *blacklist_seq)
1181 bch_info(c, "dropped unflushed entries %llu-%llu",
1182 *blacklist_seq, *start_seq - 1);
1184 /* Drop blacklisted entries and entries older than last_seq: */
1185 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1188 if (!i || i->ignore)
1191 seq = le64_to_cpu(i->j.seq);
1192 if (seq < *last_seq) {
1193 journal_replay_free(c, i);
1197 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1198 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1199 "found blacklisted journal entry %llu", seq);
1204 /* Check for missing entries: */
1206 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1209 if (!i || i->ignore)
1212 BUG_ON(seq > le64_to_cpu(i->j.seq));
1214 while (seq < le64_to_cpu(i->j.seq)) {
1215 u64 missing_start, missing_end;
1216 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1218 while (seq < le64_to_cpu(i->j.seq) &&
1219 bch2_journal_seq_is_blacklisted(c, seq, false))
1222 if (seq == le64_to_cpu(i->j.seq))
1225 missing_start = seq;
1227 while (seq < le64_to_cpu(i->j.seq) &&
1228 !bch2_journal_seq_is_blacklisted(c, seq, false))
1232 bch2_journal_ptrs_to_text(&buf1, c, prev);
1233 prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1235 prt_printf(&buf1, "(none)");
1236 bch2_journal_ptrs_to_text(&buf2, c, i);
1238 missing_end = seq - 1;
1239 fsck_err(c, "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1242 missing_start, missing_end,
1243 *last_seq, *blacklist_seq - 1,
1244 buf1.buf, buf2.buf);
1246 printbuf_exit(&buf1);
1247 printbuf_exit(&buf2);
1254 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1255 struct bch_replicas_padded replicas = {
1256 .e.data_type = BCH_DATA_journal,
1262 if (!i || i->ignore)
1265 for (ptr = 0; ptr < i->nr_ptrs; ptr++) {
1266 struct bch_dev *ca = bch_dev_bkey_exists(c, i->ptrs[ptr].dev);
1268 if (!i->ptrs[ptr].csum_good)
1269 bch_err_dev_offset(ca, i->ptrs[ptr].sector,
1270 "invalid journal checksum, seq %llu%s",
1271 le64_to_cpu(i->j.seq),
1272 i->csum_good ? " (had good copy on another device)" : "");
1275 ret = jset_validate(c,
1276 bch_dev_bkey_exists(c, i->ptrs[0].dev),
1283 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1284 replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1286 bch2_replicas_entry_sort(&replicas.e);
1289 * If we're mounting in degraded mode - if we didn't read all
1290 * the devices - this is wrong:
1293 printbuf_reset(&buf);
1294 bch2_replicas_entry_to_text(&buf, &replicas.e);
1297 fsck_err_on(!bch2_replicas_marked(c, &replicas.e), c,
1298 "superblock not marked as containing replicas %s",
1300 ret = bch2_mark_replicas(c, &replicas.e);
1307 printbuf_exit(&buf);
1311 /* journal write: */
1313 static void __journal_write_alloc(struct journal *j,
1314 struct journal_buf *w,
1315 struct dev_alloc_list *devs_sorted,
1318 unsigned replicas_want)
1320 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1321 struct journal_device *ja;
1325 if (*replicas >= replicas_want)
1328 for (i = 0; i < devs_sorted->nr; i++) {
1329 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1336 * Check that we can use this device, and aren't already using
1339 if (!ca->mi.durability ||
1340 ca->mi.state != BCH_MEMBER_STATE_rw ||
1342 bch2_bkey_has_device(bkey_i_to_s_c(&w->key),
1344 sectors > ja->sectors_free)
1347 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1349 bch2_bkey_append_ptr(&w->key,
1350 (struct bch_extent_ptr) {
1351 .offset = bucket_to_sector(ca,
1352 ja->buckets[ja->cur_idx]) +
1353 ca->mi.bucket_size -
1358 ja->sectors_free -= sectors;
1359 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1361 *replicas += ca->mi.durability;
1363 if (*replicas >= replicas_want)
1369 * journal_next_bucket - move on to the next journal bucket if possible
1371 static int journal_write_alloc(struct journal *j, struct journal_buf *w,
1374 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1375 struct bch_devs_mask devs;
1376 struct journal_device *ja;
1378 struct dev_alloc_list devs_sorted;
1379 unsigned target = c->opts.metadata_target ?:
1380 c->opts.foreground_target;
1381 unsigned i, replicas = 0, replicas_want =
1382 READ_ONCE(c->opts.metadata_replicas);
1386 devs = target_rw_devs(c, BCH_DATA_journal, target);
1388 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1390 __journal_write_alloc(j, w, &devs_sorted,
1391 sectors, &replicas, replicas_want);
1393 if (replicas >= replicas_want)
1396 for (i = 0; i < devs_sorted.nr; i++) {
1397 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1403 if (sectors > ja->sectors_free &&
1404 sectors <= ca->mi.bucket_size &&
1405 bch2_journal_dev_buckets_available(j, ja,
1406 journal_space_discarded)) {
1407 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1408 ja->sectors_free = ca->mi.bucket_size;
1411 * ja->bucket_seq[ja->cur_idx] must always have
1412 * something sensible:
1414 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1418 __journal_write_alloc(j, w, &devs_sorted,
1419 sectors, &replicas, replicas_want);
1421 if (replicas < replicas_want && target) {
1422 /* Retry from all devices: */
1429 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1431 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1434 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1436 /* we aren't holding j->lock: */
1437 unsigned new_size = READ_ONCE(j->buf_size_want);
1440 if (buf->buf_size >= new_size)
1443 new_buf = kvpmalloc(new_size, GFP_NOIO|__GFP_NOWARN);
1447 memcpy(new_buf, buf->data, buf->buf_size);
1449 spin_lock(&j->lock);
1450 swap(buf->data, new_buf);
1451 swap(buf->buf_size, new_size);
1452 spin_unlock(&j->lock);
1454 kvpfree(new_buf, new_size);
1457 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1459 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1462 static void journal_write_done(struct closure *cl)
1464 struct journal *j = container_of(cl, struct journal, io);
1465 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1466 struct journal_buf *w = journal_last_unwritten_buf(j);
1467 struct bch_replicas_padded replicas;
1468 union journal_res_state old, new;
1472 bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1473 ? j->flush_write_time
1474 : j->noflush_write_time, j->write_start_time);
1476 if (!w->devs_written.nr) {
1477 bch_err(c, "unable to write journal to sufficient devices");
1480 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1482 if (bch2_mark_replicas(c, &replicas.e))
1487 bch2_fatal_error(c);
1489 spin_lock(&j->lock);
1490 seq = le64_to_cpu(w->data->seq);
1492 if (seq >= j->pin.front)
1493 journal_seq_pin(j, seq)->devs = w->devs_written;
1496 if (!JSET_NO_FLUSH(w->data)) {
1497 j->flushed_seq_ondisk = seq;
1498 j->last_seq_ondisk = w->last_seq;
1500 bch2_do_discards(c);
1501 closure_wake_up(&c->freelist_wait);
1503 bch2_reset_alloc_cursors(c);
1505 } else if (!j->err_seq || seq < j->err_seq)
1508 j->seq_ondisk = seq;
1511 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1514 * Must come before signaling write completion, for
1515 * bch2_fs_journal_stop():
1518 journal_reclaim_kick(&c->journal);
1520 /* also must come before signalling write completion: */
1521 closure_debug_destroy(cl);
1523 v = atomic64_read(&j->reservations.counter);
1526 BUG_ON(journal_state_count(new, new.unwritten_idx));
1528 new.unwritten_idx++;
1529 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1530 old.v, new.v)) != old.v);
1532 bch2_journal_space_available(j);
1534 closure_wake_up(&w->wait);
1537 if (!journal_state_count(new, new.unwritten_idx) &&
1538 journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1539 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1540 } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1541 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1542 struct journal_buf *buf = journal_cur_buf(j);
1543 long delta = buf->expires - jiffies;
1546 * We don't close a journal entry to write it while there's
1547 * previous entries still in flight - the current journal entry
1548 * might want to be written now:
1551 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1554 spin_unlock(&j->lock);
1557 static void journal_write_endio(struct bio *bio)
1559 struct bch_dev *ca = bio->bi_private;
1560 struct journal *j = &ca->fs->journal;
1561 struct journal_buf *w = journal_last_unwritten_buf(j);
1562 unsigned long flags;
1564 if (bch2_dev_io_err_on(bio->bi_status, ca, "error writing journal entry %llu: %s",
1565 le64_to_cpu(w->data->seq),
1566 bch2_blk_status_to_str(bio->bi_status)) ||
1567 bch2_meta_write_fault("journal")) {
1568 spin_lock_irqsave(&j->err_lock, flags);
1569 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1570 spin_unlock_irqrestore(&j->err_lock, flags);
1573 closure_put(&j->io);
1574 percpu_ref_put(&ca->io_ref);
1577 static void do_journal_write(struct closure *cl)
1579 struct journal *j = container_of(cl, struct journal, io);
1580 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1582 struct journal_buf *w = journal_last_unwritten_buf(j);
1583 struct bch_extent_ptr *ptr;
1585 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1587 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1588 ca = bch_dev_bkey_exists(c, ptr->dev);
1589 if (!percpu_ref_tryget(&ca->io_ref)) {
1591 bch_err(c, "missing device for journal write\n");
1595 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1598 bio = ca->journal.bio;
1599 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1600 bio->bi_iter.bi_sector = ptr->offset;
1601 bio->bi_end_io = journal_write_endio;
1602 bio->bi_private = ca;
1604 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1605 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1607 if (!JSET_NO_FLUSH(w->data))
1608 bio->bi_opf |= REQ_FUA;
1609 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1610 bio->bi_opf |= REQ_PREFLUSH;
1612 bch2_bio_map(bio, w->data, sectors << 9);
1614 trace_and_count(c, journal_write, bio);
1615 closure_bio_submit(bio, cl);
1617 ca->journal.bucket_seq[ca->journal.cur_idx] =
1618 le64_to_cpu(w->data->seq);
1621 continue_at(cl, journal_write_done, c->io_complete_wq);
1625 static void bch2_journal_entries_postprocess(struct bch_fs *c, struct jset *jset)
1627 struct jset_entry *i, *next, *prev = NULL;
1630 * Simple compaction, dropping empty jset_entries (from journal
1631 * reservations that weren't fully used) and merging jset_entries that
1634 * If we wanted to be really fancy here, we could sort all the keys in
1635 * the jset and drop keys that were overwritten - probably not worth it:
1637 vstruct_for_each_safe(jset, i, next) {
1638 unsigned u64s = le16_to_cpu(i->u64s);
1644 if (i->type == BCH_JSET_ENTRY_btree_root)
1645 bch2_journal_entry_to_btree_root(c, i);
1647 /* Can we merge with previous entry? */
1649 i->btree_id == prev->btree_id &&
1650 i->level == prev->level &&
1651 i->type == prev->type &&
1652 i->type == BCH_JSET_ENTRY_btree_keys &&
1653 le16_to_cpu(prev->u64s) + u64s <= U16_MAX) {
1654 memmove_u64s_down(vstruct_next(prev),
1657 le16_add_cpu(&prev->u64s, u64s);
1661 /* Couldn't merge, move i into new position (after prev): */
1662 prev = prev ? vstruct_next(prev) : jset->start;
1664 memmove_u64s_down(prev, i, jset_u64s(u64s));
1667 prev = prev ? vstruct_next(prev) : jset->start;
1668 jset->u64s = cpu_to_le32((u64 *) prev - jset->_data);
1671 void bch2_journal_write(struct closure *cl)
1673 struct journal *j = container_of(cl, struct journal, io);
1674 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1676 struct journal_buf *w = journal_last_unwritten_buf(j);
1677 struct jset_entry *start, *end;
1680 struct printbuf journal_debug_buf = PRINTBUF;
1681 bool validate_before_checksum = false;
1682 unsigned i, sectors, bytes, u64s, nr_rw_members = 0;
1685 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1687 journal_buf_realloc(j, w);
1690 j->write_start_time = local_clock();
1692 spin_lock(&j->lock);
1695 * If the journal is in an error state - we did an emergency shutdown -
1696 * we prefer to continue doing journal writes. We just mark them as
1697 * noflush so they'll never be used, but they'll still be visible by the
1698 * list_journal tool - this helps in debugging.
1700 * There's a caveat: the first journal write after marking the
1701 * superblock dirty must always be a flush write, because on startup
1702 * from a clean shutdown we didn't necessarily read the journal and the
1703 * new journal write might overwrite whatever was in the journal
1704 * previously - we can't leave the journal without any flush writes in
1707 * So if we're in an error state, and we're still starting up, we don't
1708 * write anything at all.
1710 if (!test_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags) &&
1711 (bch2_journal_error(j) ||
1714 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1715 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags)))) {
1717 SET_JSET_NO_FLUSH(jset, true);
1721 j->nr_noflush_writes++;
1722 } else if (!bch2_journal_error(j)) {
1723 j->last_flush_write = jiffies;
1724 j->nr_flush_writes++;
1725 clear_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags);
1727 spin_unlock(&j->lock);
1730 spin_unlock(&j->lock);
1733 * New btree roots are set by journalling them; when the journal entry
1734 * gets written we have to propagate them to c->btree_roots
1736 * But, every journal entry we write has to contain all the btree roots
1737 * (at least for now); so after we copy btree roots to c->btree_roots we
1738 * have to get any missing btree roots and add them to this journal
1742 bch2_journal_entries_postprocess(c, jset);
1744 start = end = vstruct_last(jset);
1746 end = bch2_btree_roots_to_journal_entries(c, jset->start, end);
1748 bch2_journal_super_entries_add_common(c, &end,
1749 le64_to_cpu(jset->seq));
1750 u64s = (u64 *) end - (u64 *) start;
1751 BUG_ON(u64s > j->entry_u64s_reserved);
1753 le32_add_cpu(&jset->u64s, u64s);
1754 BUG_ON(vstruct_sectors(jset, c->block_bits) > w->sectors);
1756 jset->magic = cpu_to_le64(jset_magic(c));
1757 jset->version = c->sb.version < bcachefs_metadata_version_bkey_renumber
1758 ? cpu_to_le32(BCH_JSET_VERSION_OLD)
1759 : cpu_to_le32(c->sb.version);
1761 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1762 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1764 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1765 j->last_empty_seq = le64_to_cpu(jset->seq);
1767 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1768 validate_before_checksum = true;
1770 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1771 validate_before_checksum = true;
1773 if (validate_before_checksum &&
1774 jset_validate(c, NULL, jset, 0, WRITE))
1777 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1778 jset->encrypted_start,
1779 vstruct_end(jset) - (void *) jset->encrypted_start);
1780 if (bch2_fs_fatal_err_on(ret, c,
1781 "error decrypting journal entry: %i", ret))
1784 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1785 journal_nonce(jset), jset);
1787 if (!validate_before_checksum &&
1788 jset_validate(c, NULL, jset, 0, WRITE))
1791 sectors = vstruct_sectors(jset, c->block_bits);
1792 BUG_ON(sectors > w->sectors);
1794 bytes = vstruct_bytes(jset);
1795 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1798 spin_lock(&j->lock);
1799 ret = journal_write_alloc(j, w, sectors);
1801 if (ret && j->can_discard) {
1802 spin_unlock(&j->lock);
1803 bch2_journal_do_discards(j);
1808 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1811 * write is allocated, no longer need to account for it in
1812 * bch2_journal_space_available():
1817 * journal entry has been compacted and allocated, recalculate space
1820 bch2_journal_space_available(j);
1821 spin_unlock(&j->lock);
1824 bch_err(c, "Unable to allocate journal write:\n%s",
1825 journal_debug_buf.buf);
1826 printbuf_exit(&journal_debug_buf);
1827 bch2_fatal_error(c);
1828 continue_at(cl, journal_write_done, c->io_complete_wq);
1832 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1834 if (c->opts.nochanges)
1837 for_each_rw_member(ca, c, i)
1840 if (nr_rw_members > 1)
1841 w->separate_flush = true;
1843 if (!JSET_NO_FLUSH(jset) && w->separate_flush) {
1844 for_each_rw_member(ca, c, i) {
1845 percpu_ref_get(&ca->io_ref);
1847 bio = ca->journal.bio;
1848 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_FLUSH);
1849 bio->bi_end_io = journal_write_endio;
1850 bio->bi_private = ca;
1851 closure_bio_submit(bio, cl);
1855 continue_at(cl, do_journal_write, c->io_complete_wq);
1858 continue_at(cl, journal_write_done, c->io_complete_wq);
1861 bch2_fatal_error(c);
1862 continue_at(cl, journal_write_done, c->io_complete_wq);