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 inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
22 return (seq - c->journal_entries_base_seq) & (~0U >> 1);
25 static void __journal_replay_free(struct bch_fs *c,
26 struct journal_replay *i)
28 struct journal_replay **p =
29 genradix_ptr(&c->journal_entries,
30 journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
34 kvpfree(i, offsetof(struct journal_replay, j) +
35 vstruct_bytes(&i->j));
38 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
42 if (!c->opts.read_entire_journal)
43 __journal_replay_free(c, i);
53 #define JOURNAL_ENTRY_ADD_OK 0
54 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
57 * Given a journal entry we just read, add it to the list of journal entries to
60 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
61 struct journal_ptr entry_ptr,
62 struct journal_list *jlist, struct jset *j,
65 struct genradix_iter iter;
66 struct journal_replay **_i, *i, *dup;
67 struct journal_ptr *ptr;
68 size_t bytes = vstruct_bytes(j);
69 u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
70 int ret = JOURNAL_ENTRY_ADD_OK;
72 /* Is this entry older than the range we need? */
73 if (!c->opts.read_entire_journal &&
74 le64_to_cpu(j->seq) < jlist->last_seq)
75 return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
78 * genradixes are indexed by a ulong, not a u64, so we can't index them
79 * by sequence number directly: Assume instead that they will all fall
80 * within the range of +-2billion of the filrst one we find.
82 if (!c->journal_entries_base_seq)
83 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
85 /* Drop entries we don't need anymore */
86 if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
87 genradix_for_each_from(&c->journal_entries, iter, _i,
88 journal_entry_radix_idx(c, jlist->last_seq)) {
94 if (le64_to_cpu(i->j.seq) >= last_seq)
96 journal_replay_free(c, i);
100 jlist->last_seq = max(jlist->last_seq, last_seq);
102 _i = genradix_ptr_alloc(&c->journal_entries,
103 journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
109 * Duplicate journal entries? If so we want the one that didn't have a
115 /* we'll replace @dup: */
120 fsck_err_on(bytes != vstruct_bytes(&dup->j) ||
121 memcmp(j, &dup->j, bytes), c,
122 "found duplicate but non identical journal entries (seq %llu)",
123 le64_to_cpu(j->seq));
129 i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
136 memcpy(&i->j, j, bytes);
139 i->nr_ptrs = dup->nr_ptrs;
140 memcpy(i->ptrs, dup->ptrs, sizeof(dup->ptrs));
141 __journal_replay_free(c, dup);
147 for (ptr = i->ptrs; ptr < i->ptrs + i->nr_ptrs; ptr++) {
148 if (ptr->dev == ca->dev_idx) {
149 bch_err(c, "duplicate journal entry %llu on same device",
150 le64_to_cpu(i->j.seq));
155 if (i->nr_ptrs >= ARRAY_SIZE(i->ptrs)) {
156 bch_err(c, "found too many copies of journal entry %llu",
157 le64_to_cpu(i->j.seq));
161 i->ptrs[i->nr_ptrs++] = entry_ptr;
167 static struct nonce journal_nonce(const struct jset *jset)
169 return (struct nonce) {{
171 [1] = ((__le32 *) &jset->seq)[0],
172 [2] = ((__le32 *) &jset->seq)[1],
173 [3] = BCH_NONCE_JOURNAL,
177 /* this fills in a range with empty jset_entries: */
178 static void journal_entry_null_range(void *start, void *end)
180 struct jset_entry *entry;
182 for (entry = start; entry != end; entry = vstruct_next(entry))
183 memset(entry, 0, sizeof(*entry));
186 #define JOURNAL_ENTRY_REREAD 5
187 #define JOURNAL_ENTRY_NONE 6
188 #define JOURNAL_ENTRY_BAD 7
190 #define journal_entry_err(c, msg, ...) \
194 mustfix_fsck_err(c, msg, ##__VA_ARGS__); \
197 bch_err(c, "corrupt metadata before write:\n" \
198 msg, ##__VA_ARGS__); \
199 if (bch2_fs_inconsistent(c)) { \
200 ret = BCH_FSCK_ERRORS_NOT_FIXED; \
208 #define journal_entry_err_on(cond, c, msg, ...) \
209 ((cond) ? journal_entry_err(c, msg, ##__VA_ARGS__) : false)
211 #define FSCK_DELETED_KEY 5
213 static int journal_validate_key(struct bch_fs *c, const char *where,
214 struct jset_entry *entry,
215 unsigned level, enum btree_id btree_id,
216 struct bkey_i *k, const char *type,
217 unsigned version, int big_endian, int write)
219 void *next = vstruct_next(entry);
220 struct printbuf buf = PRINTBUF;
223 if (journal_entry_err_on(!k->k.u64s, c,
224 "invalid %s in %s entry offset %zi/%u: k->u64s 0",
226 (u64 *) k - entry->_data,
227 le16_to_cpu(entry->u64s))) {
228 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
229 journal_entry_null_range(vstruct_next(entry), next);
230 return FSCK_DELETED_KEY;
233 if (journal_entry_err_on((void *) bkey_next(k) >
234 (void *) vstruct_next(entry), c,
235 "invalid %s in %s entry offset %zi/%u: extends past end of journal entry",
237 (u64 *) k - entry->_data,
238 le16_to_cpu(entry->u64s))) {
239 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
240 journal_entry_null_range(vstruct_next(entry), next);
241 return FSCK_DELETED_KEY;
244 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT, c,
245 "invalid %s in %s entry offset %zi/%u: bad format %u",
247 (u64 *) k - entry->_data,
248 le16_to_cpu(entry->u64s),
250 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
251 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
252 journal_entry_null_range(vstruct_next(entry), next);
253 return FSCK_DELETED_KEY;
257 bch2_bkey_compat(level, btree_id, version, big_endian,
258 write, NULL, bkey_to_packed(k));
260 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
261 __btree_node_type(level, btree_id), write, &buf)) {
262 printbuf_reset(&buf);
263 pr_buf(&buf, "invalid %s in %s entry offset %zi/%u:",
265 (u64 *) k - entry->_data,
266 le16_to_cpu(entry->u64s));
268 pr_indent_push(&buf, 2);
270 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
272 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
273 __btree_node_type(level, btree_id), write, &buf);
275 mustfix_fsck_err(c, "%s", buf.buf);
277 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
278 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
279 journal_entry_null_range(vstruct_next(entry), next);
282 return FSCK_DELETED_KEY;
286 bch2_bkey_compat(level, btree_id, version, big_endian,
287 write, NULL, bkey_to_packed(k));
293 static int journal_entry_btree_keys_validate(struct bch_fs *c,
295 struct jset_entry *entry,
296 unsigned version, int big_endian, int write)
298 struct bkey_i *k = entry->start;
300 while (k != vstruct_last(entry)) {
301 int ret = journal_validate_key(c, where, entry,
304 k, "key", version, big_endian, write);
305 if (ret == FSCK_DELETED_KEY)
314 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
315 struct jset_entry *entry)
320 vstruct_for_each(entry, k) {
323 pr_buf(out, "%s: ", bch2_jset_entry_types[entry->type]);
325 pr_buf(out, "btree=%s l=%u ", bch2_btree_ids[entry->btree_id], entry->level);
326 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
331 static int journal_entry_btree_root_validate(struct bch_fs *c,
333 struct jset_entry *entry,
334 unsigned version, int big_endian, int write)
336 struct bkey_i *k = entry->start;
339 if (journal_entry_err_on(!entry->u64s ||
340 le16_to_cpu(entry->u64s) != k->k.u64s, c,
341 "invalid btree root journal entry: wrong number of keys")) {
342 void *next = vstruct_next(entry);
344 * we don't want to null out this jset_entry,
345 * just the contents, so that later we can tell
346 * we were _supposed_ to have a btree root
349 journal_entry_null_range(vstruct_next(entry), next);
353 return journal_validate_key(c, where, entry, 1, entry->btree_id, k,
354 "btree root", version, big_endian, write);
359 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
360 struct jset_entry *entry)
362 journal_entry_btree_keys_to_text(out, c, entry);
365 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
367 struct jset_entry *entry,
368 unsigned version, int big_endian, int write)
370 /* obsolete, don't care: */
374 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
375 struct jset_entry *entry)
379 static int journal_entry_blacklist_validate(struct bch_fs *c,
381 struct jset_entry *entry,
382 unsigned version, int big_endian, int write)
386 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1, c,
387 "invalid journal seq blacklist entry: bad size")) {
388 journal_entry_null_range(entry, vstruct_next(entry));
394 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
395 struct jset_entry *entry)
397 struct jset_entry_blacklist *bl =
398 container_of(entry, struct jset_entry_blacklist, entry);
400 pr_buf(out, "seq=%llu", le64_to_cpu(bl->seq));
403 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
405 struct jset_entry *entry,
406 unsigned version, int big_endian, int write)
408 struct jset_entry_blacklist_v2 *bl_entry;
411 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2, c,
412 "invalid journal seq blacklist entry: bad size")) {
413 journal_entry_null_range(entry, vstruct_next(entry));
417 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
419 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
420 le64_to_cpu(bl_entry->end), c,
421 "invalid journal seq blacklist entry: start > end")) {
422 journal_entry_null_range(entry, vstruct_next(entry));
429 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
430 struct jset_entry *entry)
432 struct jset_entry_blacklist_v2 *bl =
433 container_of(entry, struct jset_entry_blacklist_v2, entry);
435 pr_buf(out, "start=%llu end=%llu",
436 le64_to_cpu(bl->start),
437 le64_to_cpu(bl->end));
440 static int journal_entry_usage_validate(struct bch_fs *c,
442 struct jset_entry *entry,
443 unsigned version, int big_endian, int write)
445 struct jset_entry_usage *u =
446 container_of(entry, struct jset_entry_usage, entry);
447 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
450 if (journal_entry_err_on(bytes < sizeof(*u),
452 "invalid journal entry usage: bad size")) {
453 journal_entry_null_range(entry, vstruct_next(entry));
461 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
462 struct jset_entry *entry)
464 struct jset_entry_usage *u =
465 container_of(entry, struct jset_entry_usage, entry);
467 pr_buf(out, "type=%s v=%llu",
468 bch2_fs_usage_types[u->entry.btree_id],
472 static int journal_entry_data_usage_validate(struct bch_fs *c,
474 struct jset_entry *entry,
475 unsigned version, int big_endian, int write)
477 struct jset_entry_data_usage *u =
478 container_of(entry, struct jset_entry_data_usage, entry);
479 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
482 if (journal_entry_err_on(bytes < sizeof(*u) ||
483 bytes < sizeof(*u) + u->r.nr_devs,
485 "invalid journal entry usage: bad size")) {
486 journal_entry_null_range(entry, vstruct_next(entry));
494 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
495 struct jset_entry *entry)
497 struct jset_entry_data_usage *u =
498 container_of(entry, struct jset_entry_data_usage, entry);
500 bch2_replicas_entry_to_text(out, &u->r);
501 pr_buf(out, "=%llu", le64_to_cpu(u->v));
504 static int journal_entry_clock_validate(struct bch_fs *c,
506 struct jset_entry *entry,
507 unsigned version, int big_endian, int write)
509 struct jset_entry_clock *clock =
510 container_of(entry, struct jset_entry_clock, entry);
511 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
514 if (journal_entry_err_on(bytes != sizeof(*clock),
515 c, "invalid journal entry clock: bad size")) {
516 journal_entry_null_range(entry, vstruct_next(entry));
520 if (journal_entry_err_on(clock->rw > 1,
521 c, "invalid journal entry clock: bad rw")) {
522 journal_entry_null_range(entry, vstruct_next(entry));
530 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
531 struct jset_entry *entry)
533 struct jset_entry_clock *clock =
534 container_of(entry, struct jset_entry_clock, entry);
536 pr_buf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
539 static int journal_entry_dev_usage_validate(struct bch_fs *c,
541 struct jset_entry *entry,
542 unsigned version, int big_endian, int write)
544 struct jset_entry_dev_usage *u =
545 container_of(entry, struct jset_entry_dev_usage, entry);
546 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
547 unsigned expected = sizeof(*u);
551 if (journal_entry_err_on(bytes < expected,
552 c, "invalid journal entry dev usage: bad size (%u < %u)",
554 journal_entry_null_range(entry, vstruct_next(entry));
558 dev = le32_to_cpu(u->dev);
560 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
561 c, "invalid journal entry dev usage: bad dev")) {
562 journal_entry_null_range(entry, vstruct_next(entry));
566 if (journal_entry_err_on(u->pad,
567 c, "invalid journal entry dev usage: bad pad")) {
568 journal_entry_null_range(entry, vstruct_next(entry));
576 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
577 struct jset_entry *entry)
579 struct jset_entry_dev_usage *u =
580 container_of(entry, struct jset_entry_dev_usage, entry);
581 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
583 pr_buf(out, "dev=%u", le32_to_cpu(u->dev));
585 for (i = 0; i < nr_types; i++) {
587 pr_buf(out, " %s", bch2_data_types[i]);
589 pr_buf(out, " (unknown data type %u)", i);
590 pr_buf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
591 le64_to_cpu(u->d[i].buckets),
592 le64_to_cpu(u->d[i].sectors),
593 le64_to_cpu(u->d[i].fragmented));
596 pr_buf(out, " buckets_ec: %llu", le64_to_cpu(u->buckets_ec));
599 static int journal_entry_log_validate(struct bch_fs *c,
601 struct jset_entry *entry,
602 unsigned version, int big_endian, int write)
607 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
608 struct jset_entry *entry)
610 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
611 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
613 pr_buf(out, "%.*s", bytes, l->d);
616 struct jset_entry_ops {
617 int (*validate)(struct bch_fs *, const char *,
618 struct jset_entry *, unsigned, int, int);
619 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
622 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
624 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
625 .validate = journal_entry_##f##_validate, \
626 .to_text = journal_entry_##f##_to_text, \
628 BCH_JSET_ENTRY_TYPES()
632 int bch2_journal_entry_validate(struct bch_fs *c, const char *where,
633 struct jset_entry *entry,
634 unsigned version, int big_endian, int write)
636 return entry->type < BCH_JSET_ENTRY_NR
637 ? bch2_jset_entry_ops[entry->type].validate(c, where, entry,
638 version, big_endian, write)
642 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
643 struct jset_entry *entry)
645 if (entry->type < BCH_JSET_ENTRY_NR) {
646 pr_buf(out, "%s: ", bch2_jset_entry_types[entry->type]);
647 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
649 pr_buf(out, "(unknown type %u)", entry->type);
653 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
657 struct jset_entry *entry;
660 vstruct_for_each(jset, entry) {
661 scnprintf(buf, sizeof(buf), "jset %llu entry offset %zi/%u",
662 le64_to_cpu(jset->seq),
663 (u64 *) entry - jset->_data,
664 le32_to_cpu(jset->u64s));
666 if (journal_entry_err_on(vstruct_next(entry) >
667 vstruct_last(jset), c,
668 "journal entry extends past end of jset")) {
669 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
673 ret = bch2_journal_entry_validate(c, buf, entry,
674 le32_to_cpu(jset->version),
675 JSET_BIG_ENDIAN(jset), write);
683 static int jset_validate(struct bch_fs *c,
685 struct jset *jset, u64 sector,
686 unsigned bucket_sectors_left,
687 unsigned sectors_read,
690 size_t bytes = vstruct_bytes(jset);
691 struct bch_csum csum;
695 if (le64_to_cpu(jset->magic) != jset_magic(c))
696 return JOURNAL_ENTRY_NONE;
698 version = le32_to_cpu(jset->version);
699 if (journal_entry_err_on((version != BCH_JSET_VERSION_OLD &&
700 version < bcachefs_metadata_version_min) ||
701 version >= bcachefs_metadata_version_max, c,
702 "%s sector %llu seq %llu: unknown journal entry version %u",
703 ca ? ca->name : c->name,
704 sector, le64_to_cpu(jset->seq),
706 /* don't try to continue: */
710 if (bytes > (sectors_read << 9) &&
711 sectors_read < bucket_sectors_left)
712 return JOURNAL_ENTRY_REREAD;
714 if (journal_entry_err_on(bytes > bucket_sectors_left << 9, c,
715 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
716 ca ? ca->name : c->name,
717 sector, le64_to_cpu(jset->seq), bytes)) {
718 ret = JOURNAL_ENTRY_BAD;
719 le32_add_cpu(&jset->u64s,
720 -((bytes - (bucket_sectors_left << 9)) / 8));
723 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)), c,
724 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
725 ca ? ca->name : c->name,
726 sector, le64_to_cpu(jset->seq),
727 JSET_CSUM_TYPE(jset))) {
728 ret = JOURNAL_ENTRY_BAD;
735 csum = csum_vstruct(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), jset);
736 if (journal_entry_err_on(bch2_crc_cmp(csum, jset->csum), c,
737 "%s sector %llu seq %llu: journal checksum bad",
738 ca ? ca->name : c->name,
739 sector, le64_to_cpu(jset->seq)))
740 ret = JOURNAL_ENTRY_BAD;
742 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
743 jset->encrypted_start,
744 vstruct_end(jset) - (void *) jset->encrypted_start);
745 bch2_fs_fatal_err_on(ret, c,
746 "error decrypting journal entry: %i", ret);
748 /* last_seq is ignored when JSET_NO_FLUSH is true */
749 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
750 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq), c,
751 "invalid journal entry: last_seq > seq (%llu > %llu)",
752 le64_to_cpu(jset->last_seq),
753 le64_to_cpu(jset->seq))) {
754 jset->last_seq = jset->seq;
755 return JOURNAL_ENTRY_BAD;
761 static int jset_validate_for_write(struct bch_fs *c, struct jset *jset)
763 unsigned sectors = vstruct_sectors(jset, c->block_bits);
765 return jset_validate(c, NULL, jset, 0, sectors, sectors, WRITE) ?:
766 jset_validate_entries(c, jset, WRITE);
769 struct journal_read_buf {
774 static int journal_read_buf_realloc(struct journal_read_buf *b,
779 /* the bios are sized for this many pages, max: */
780 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
783 new_size = roundup_pow_of_two(new_size);
784 n = kvpmalloc(new_size, GFP_KERNEL);
788 kvpfree(b->data, b->size);
794 static int journal_read_bucket(struct bch_dev *ca,
795 struct journal_read_buf *buf,
796 struct journal_list *jlist,
799 struct bch_fs *c = ca->fs;
800 struct journal_device *ja = &ca->journal;
801 struct jset *j = NULL;
802 unsigned sectors, sectors_read = 0;
803 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
804 end = offset + ca->mi.bucket_size;
805 bool saw_bad = false;
808 pr_debug("reading %u", bucket);
810 while (offset < end) {
814 sectors_read = min_t(unsigned,
815 end - offset, buf->size >> 9);
817 bio = bio_kmalloc(GFP_KERNEL,
820 bio_set_dev(bio, ca->disk_sb.bdev);
821 bio->bi_iter.bi_sector = offset;
822 bio_set_op_attrs(bio, REQ_OP_READ, 0);
823 bch2_bio_map(bio, buf->data, sectors_read << 9);
825 ret = submit_bio_wait(bio);
828 if (bch2_dev_io_err_on(ret, ca,
829 "journal read error: sector %llu",
831 bch2_meta_read_fault("journal")) {
833 * We don't error out of the recovery process
834 * here, since the relevant journal entry may be
835 * found on a different device, and missing or
836 * no journal entries will be handled later
844 ret = jset_validate(c, ca, j, offset,
845 end - offset, sectors_read,
849 sectors = vstruct_sectors(j, c->block_bits);
851 case JOURNAL_ENTRY_REREAD:
852 if (vstruct_bytes(j) > buf->size) {
853 ret = journal_read_buf_realloc(buf,
859 case JOURNAL_ENTRY_NONE:
862 sectors = block_sectors(c);
864 case JOURNAL_ENTRY_BAD:
867 * On checksum error we don't really trust the size
868 * field of the journal entry we read, so try reading
869 * again at next block boundary:
871 sectors = block_sectors(c);
878 * This happens sometimes if we don't have discards on -
879 * when we've partially overwritten a bucket with new
880 * journal entries. We don't need the rest of the
883 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
886 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
888 mutex_lock(&jlist->lock);
889 ret = journal_entry_add(c, ca, (struct journal_ptr) {
892 .bucket_offset = offset -
893 bucket_to_sector(ca, ja->buckets[bucket]),
895 }, jlist, j, ret != 0);
896 mutex_unlock(&jlist->lock);
899 case JOURNAL_ENTRY_ADD_OK:
901 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
909 sectors_read -= sectors;
910 j = ((void *) j) + (sectors << 9);
916 static void bch2_journal_read_device(struct closure *cl)
918 struct journal_device *ja =
919 container_of(cl, struct journal_device, read);
920 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
921 struct bch_fs *c = ca->fs;
922 struct journal_list *jlist =
923 container_of(cl->parent, struct journal_list, cl);
924 struct journal_replay *r, **_r;
925 struct genradix_iter iter;
926 struct journal_read_buf buf = { NULL, 0 };
927 u64 min_seq = U64_MAX;
934 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
938 pr_debug("%u journal buckets", ja->nr);
940 for (i = 0; i < ja->nr; i++) {
941 ret = journal_read_bucket(ca, &buf, jlist, i);
946 /* Find the journal bucket with the highest sequence number: */
947 for (i = 0; i < ja->nr; i++) {
948 if (ja->bucket_seq[i] > ja->bucket_seq[ja->cur_idx])
951 min_seq = min(ja->bucket_seq[i], min_seq);
955 * If there's duplicate journal entries in multiple buckets (which
956 * definitely isn't supposed to happen, but...) - make sure to start
957 * cur_idx at the last of those buckets, so we don't deadlock trying to
960 while (ja->bucket_seq[ja->cur_idx] > min_seq &&
961 ja->bucket_seq[ja->cur_idx] ==
962 ja->bucket_seq[(ja->cur_idx + 1) % ja->nr])
963 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
965 ja->sectors_free = ca->mi.bucket_size;
967 mutex_lock(&jlist->lock);
968 genradix_for_each(&c->journal_entries, iter, _r) {
974 for (i = 0; i < r->nr_ptrs; i++) {
975 if (r->ptrs[i].dev == ca->dev_idx &&
976 sector_to_bucket(ca, r->ptrs[i].sector) == ja->buckets[ja->cur_idx]) {
977 unsigned wrote = bucket_remainder(ca, r->ptrs[i].sector) +
978 vstruct_sectors(&r->j, c->block_bits);
980 ja->sectors_free = min(ja->sectors_free,
981 ca->mi.bucket_size - wrote);
985 mutex_unlock(&jlist->lock);
987 if (ja->bucket_seq[ja->cur_idx] &&
988 ja->sectors_free == ca->mi.bucket_size) {
989 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
990 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
991 for (i = 0; i < 3; i++) {
992 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
993 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
995 ja->sectors_free = 0;
999 * Set dirty_idx to indicate the entire journal is full and needs to be
1000 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1001 * pinned when it first runs:
1003 ja->discard_idx = ja->dirty_idx_ondisk =
1004 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1006 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1007 kvpfree(buf.data, buf.size);
1008 percpu_ref_put(&ca->io_ref);
1012 mutex_lock(&jlist->lock);
1014 mutex_unlock(&jlist->lock);
1018 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
1019 struct journal_replay *j)
1023 for (i = 0; i < j->nr_ptrs; i++) {
1024 struct bch_dev *ca = bch_dev_bkey_exists(c, j->ptrs[i].dev);
1027 div64_u64_rem(j->ptrs[i].sector, ca->mi.bucket_size, &offset);
1031 pr_buf(out, "%u:%u:%u (sector %llu)",
1034 j->ptrs[i].bucket_offset,
1039 int bch2_journal_read(struct bch_fs *c, u64 *blacklist_seq, u64 *start_seq)
1041 struct journal_list jlist;
1042 struct journal_replay *i, **_i, *prev = NULL;
1043 struct genradix_iter radix_iter;
1046 struct printbuf buf = PRINTBUF;
1047 size_t keys = 0, entries = 0;
1048 bool degraded = false;
1049 u64 seq, last_seq = 0;
1052 closure_init_stack(&jlist.cl);
1053 mutex_init(&jlist.lock);
1057 for_each_member_device(ca, c, iter) {
1058 if (!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
1059 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1062 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1063 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1064 percpu_ref_tryget(&ca->io_ref))
1065 closure_call(&ca->journal.read,
1066 bch2_journal_read_device,
1073 closure_sync(&jlist.cl);
1081 * Find most recent flush entry, and ignore newer non flush entries -
1082 * those entries will be blacklisted:
1084 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1087 if (!i || i->ignore)
1091 *start_seq = le64_to_cpu(i->j.seq) + 1;
1093 if (!JSET_NO_FLUSH(&i->j)) {
1094 last_seq = le64_to_cpu(i->j.last_seq);
1095 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1099 journal_replay_free(c, i);
1103 bch_info(c, "journal read done, but no entries found");
1108 fsck_err(c, "journal read done, but no entries found after dropping non-flushes");
1113 /* Drop blacklisted entries and entries older than last_seq: */
1114 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1117 if (!i || i->ignore)
1120 seq = le64_to_cpu(i->j.seq);
1121 if (seq < last_seq) {
1122 journal_replay_free(c, i);
1126 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1127 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1128 "found blacklisted journal entry %llu", seq);
1130 journal_replay_free(c, i);
1134 /* Check for missing entries: */
1136 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1139 if (!i || i->ignore)
1142 BUG_ON(seq > le64_to_cpu(i->j.seq));
1144 while (seq < le64_to_cpu(i->j.seq)) {
1145 u64 missing_start, missing_end;
1146 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1148 while (seq < le64_to_cpu(i->j.seq) &&
1149 bch2_journal_seq_is_blacklisted(c, seq, false))
1152 if (seq == le64_to_cpu(i->j.seq))
1155 missing_start = seq;
1157 while (seq < le64_to_cpu(i->j.seq) &&
1158 !bch2_journal_seq_is_blacklisted(c, seq, false))
1162 bch2_journal_ptrs_to_text(&buf1, c, prev);
1163 pr_buf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1165 pr_buf(&buf1, "(none)");
1166 bch2_journal_ptrs_to_text(&buf2, c, i);
1168 missing_end = seq - 1;
1169 fsck_err(c, "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1172 missing_start, missing_end,
1173 last_seq, *blacklist_seq - 1,
1174 buf1.buf, buf2.buf);
1176 printbuf_exit(&buf1);
1177 printbuf_exit(&buf2);
1184 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1185 struct jset_entry *entry;
1186 struct bkey_i *k, *_n;
1187 struct bch_replicas_padded replicas = {
1188 .e.data_type = BCH_DATA_journal,
1194 if (!i || i->ignore)
1197 ret = jset_validate_entries(c, &i->j, READ);
1201 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1202 replicas.e.devs[replicas.e.nr_devs++] = i->ptrs[ptr].dev;
1204 bch2_replicas_entry_sort(&replicas.e);
1207 * If we're mounting in degraded mode - if we didn't read all
1208 * the devices - this is wrong:
1211 printbuf_reset(&buf);
1212 bch2_replicas_entry_to_text(&buf, &replicas.e);
1215 (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
1216 fsck_err_on(!bch2_replicas_marked(c, &replicas.e), c,
1217 "superblock not marked as containing replicas %s",
1219 ret = bch2_mark_replicas(c, &replicas.e);
1224 for_each_jset_key(k, _n, entry, &i->j)
1229 bch_info(c, "journal read done, %zu keys in %zu entries, seq %llu",
1230 keys, entries, *start_seq);
1232 if (*start_seq != *blacklist_seq)
1233 bch_info(c, "dropped unflushed entries %llu-%llu",
1234 *blacklist_seq, *start_seq - 1);
1237 printbuf_exit(&buf);
1241 /* journal write: */
1243 static void __journal_write_alloc(struct journal *j,
1244 struct journal_buf *w,
1245 struct dev_alloc_list *devs_sorted,
1248 unsigned replicas_want)
1250 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1251 struct journal_device *ja;
1255 if (*replicas >= replicas_want)
1258 for (i = 0; i < devs_sorted->nr; i++) {
1259 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1266 * Check that we can use this device, and aren't already using
1269 if (!ca->mi.durability ||
1270 ca->mi.state != BCH_MEMBER_STATE_rw ||
1272 bch2_bkey_has_device(bkey_i_to_s_c(&w->key),
1274 sectors > ja->sectors_free)
1277 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1279 bch2_bkey_append_ptr(&w->key,
1280 (struct bch_extent_ptr) {
1281 .offset = bucket_to_sector(ca,
1282 ja->buckets[ja->cur_idx]) +
1283 ca->mi.bucket_size -
1288 ja->sectors_free -= sectors;
1289 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1291 *replicas += ca->mi.durability;
1293 if (*replicas >= replicas_want)
1299 * journal_next_bucket - move on to the next journal bucket if possible
1301 static int journal_write_alloc(struct journal *j, struct journal_buf *w,
1304 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1305 struct bch_devs_mask devs;
1306 struct journal_device *ja;
1308 struct dev_alloc_list devs_sorted;
1309 unsigned target = c->opts.metadata_target ?:
1310 c->opts.foreground_target;
1311 unsigned i, replicas = 0, replicas_want =
1312 READ_ONCE(c->opts.metadata_replicas);
1316 devs = target_rw_devs(c, BCH_DATA_journal, target);
1318 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1320 __journal_write_alloc(j, w, &devs_sorted,
1321 sectors, &replicas, replicas_want);
1323 if (replicas >= replicas_want)
1326 for (i = 0; i < devs_sorted.nr; i++) {
1327 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1333 if (sectors > ja->sectors_free &&
1334 sectors <= ca->mi.bucket_size &&
1335 bch2_journal_dev_buckets_available(j, ja,
1336 journal_space_discarded)) {
1337 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1338 ja->sectors_free = ca->mi.bucket_size;
1341 * ja->bucket_seq[ja->cur_idx] must always have
1342 * something sensible:
1344 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1348 __journal_write_alloc(j, w, &devs_sorted,
1349 sectors, &replicas, replicas_want);
1351 if (replicas < replicas_want && target) {
1352 /* Retry from all devices: */
1359 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1361 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1364 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1366 /* we aren't holding j->lock: */
1367 unsigned new_size = READ_ONCE(j->buf_size_want);
1370 if (buf->buf_size >= new_size)
1373 new_buf = kvpmalloc(new_size, GFP_NOIO|__GFP_NOWARN);
1377 memcpy(new_buf, buf->data, buf->buf_size);
1379 spin_lock(&j->lock);
1380 swap(buf->data, new_buf);
1381 swap(buf->buf_size, new_size);
1382 spin_unlock(&j->lock);
1384 kvpfree(new_buf, new_size);
1387 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1389 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1392 static void journal_write_done(struct closure *cl)
1394 struct journal *j = container_of(cl, struct journal, io);
1395 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1396 struct journal_buf *w = journal_last_unwritten_buf(j);
1397 struct bch_replicas_padded replicas;
1398 union journal_res_state old, new;
1402 bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1403 ? j->flush_write_time
1404 : j->noflush_write_time, j->write_start_time);
1406 if (!w->devs_written.nr) {
1407 bch_err(c, "unable to write journal to sufficient devices");
1410 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1412 if (bch2_mark_replicas(c, &replicas.e))
1417 bch2_fatal_error(c);
1419 spin_lock(&j->lock);
1420 seq = le64_to_cpu(w->data->seq);
1422 if (seq >= j->pin.front)
1423 journal_seq_pin(j, seq)->devs = w->devs_written;
1426 if (!JSET_NO_FLUSH(w->data)) {
1427 j->flushed_seq_ondisk = seq;
1428 j->last_seq_ondisk = w->last_seq;
1430 bch2_do_discards(c);
1431 closure_wake_up(&c->freelist_wait);
1433 } else if (!j->err_seq || seq < j->err_seq)
1436 j->seq_ondisk = seq;
1439 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1442 * Must come before signaling write completion, for
1443 * bch2_fs_journal_stop():
1445 journal_reclaim_kick(&c->journal);
1447 /* also must come before signalling write completion: */
1448 closure_debug_destroy(cl);
1450 v = atomic64_read(&j->reservations.counter);
1453 BUG_ON(journal_state_count(new, new.unwritten_idx));
1455 new.unwritten_idx++;
1456 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1457 old.v, new.v)) != old.v);
1459 bch2_journal_space_available(j);
1461 closure_wake_up(&w->wait);
1464 if (!journal_state_count(new, new.unwritten_idx) &&
1465 journal_last_unwritten_seq(j) <= journal_cur_seq(j)) {
1466 closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
1467 } else if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1468 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1469 struct journal_buf *buf = journal_cur_buf(j);
1470 long delta = buf->expires - jiffies;
1473 * We don't close a journal entry to write it while there's
1474 * previous entries still in flight - the current journal entry
1475 * might want to be written now:
1478 mod_delayed_work(c->io_complete_wq, &j->write_work, max(0L, delta));
1481 spin_unlock(&j->lock);
1484 static void journal_write_endio(struct bio *bio)
1486 struct bch_dev *ca = bio->bi_private;
1487 struct journal *j = &ca->fs->journal;
1488 struct journal_buf *w = journal_last_unwritten_buf(j);
1489 unsigned long flags;
1491 if (bch2_dev_io_err_on(bio->bi_status, ca, "error writing journal entry %llu: %s",
1492 le64_to_cpu(w->data->seq),
1493 bch2_blk_status_to_str(bio->bi_status)) ||
1494 bch2_meta_write_fault("journal")) {
1495 spin_lock_irqsave(&j->err_lock, flags);
1496 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1497 spin_unlock_irqrestore(&j->err_lock, flags);
1500 closure_put(&j->io);
1501 percpu_ref_put(&ca->io_ref);
1504 static void do_journal_write(struct closure *cl)
1506 struct journal *j = container_of(cl, struct journal, io);
1507 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1509 struct journal_buf *w = journal_last_unwritten_buf(j);
1510 struct bch_extent_ptr *ptr;
1512 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1514 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1515 ca = bch_dev_bkey_exists(c, ptr->dev);
1516 if (!percpu_ref_tryget(&ca->io_ref)) {
1518 bch_err(c, "missing device for journal write\n");
1522 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1525 bio = ca->journal.bio;
1527 bio_set_dev(bio, ca->disk_sb.bdev);
1528 bio->bi_iter.bi_sector = ptr->offset;
1529 bio->bi_end_io = journal_write_endio;
1530 bio->bi_private = ca;
1531 bio->bi_opf = REQ_OP_WRITE|REQ_SYNC|REQ_META;
1533 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1534 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1536 if (!JSET_NO_FLUSH(w->data))
1537 bio->bi_opf |= REQ_FUA;
1538 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1539 bio->bi_opf |= REQ_PREFLUSH;
1541 bch2_bio_map(bio, w->data, sectors << 9);
1543 trace_journal_write(bio);
1544 closure_bio_submit(bio, cl);
1546 ca->journal.bucket_seq[ca->journal.cur_idx] =
1547 le64_to_cpu(w->data->seq);
1550 continue_at(cl, journal_write_done, c->io_complete_wq);
1554 void bch2_journal_write(struct closure *cl)
1556 struct journal *j = container_of(cl, struct journal, io);
1557 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1559 struct journal_buf *w = journal_last_unwritten_buf(j);
1560 struct jset_entry *start, *end;
1563 struct printbuf journal_debug_buf = PRINTBUF;
1564 bool validate_before_checksum = false;
1565 unsigned i, sectors, bytes, u64s, nr_rw_members = 0;
1568 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1570 journal_buf_realloc(j, w);
1573 j->write_start_time = local_clock();
1575 spin_lock(&j->lock);
1576 if (bch2_journal_error(j) ||
1579 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1580 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags))) {
1582 SET_JSET_NO_FLUSH(jset, true);
1586 j->nr_noflush_writes++;
1588 j->last_flush_write = jiffies;
1589 j->nr_flush_writes++;
1591 spin_unlock(&j->lock);
1594 * New btree roots are set by journalling them; when the journal entry
1595 * gets written we have to propagate them to c->btree_roots
1597 * But, every journal entry we write has to contain all the btree roots
1598 * (at least for now); so after we copy btree roots to c->btree_roots we
1599 * have to get any missing btree roots and add them to this journal
1603 bch2_journal_entries_to_btree_roots(c, jset);
1605 start = end = vstruct_last(jset);
1607 end = bch2_btree_roots_to_journal_entries(c, jset->start, end);
1609 bch2_journal_super_entries_add_common(c, &end,
1610 le64_to_cpu(jset->seq));
1611 u64s = (u64 *) end - (u64 *) start;
1612 BUG_ON(u64s > j->entry_u64s_reserved);
1614 le32_add_cpu(&jset->u64s, u64s);
1615 BUG_ON(vstruct_sectors(jset, c->block_bits) > w->sectors);
1617 jset->magic = cpu_to_le64(jset_magic(c));
1618 jset->version = c->sb.version < bcachefs_metadata_version_bkey_renumber
1619 ? cpu_to_le32(BCH_JSET_VERSION_OLD)
1620 : cpu_to_le32(c->sb.version);
1622 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1623 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1625 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1626 j->last_empty_seq = le64_to_cpu(jset->seq);
1628 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1629 validate_before_checksum = true;
1631 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1632 validate_before_checksum = true;
1634 if (validate_before_checksum &&
1635 jset_validate_for_write(c, jset))
1638 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1639 jset->encrypted_start,
1640 vstruct_end(jset) - (void *) jset->encrypted_start);
1641 if (bch2_fs_fatal_err_on(ret, c,
1642 "error decrypting journal entry: %i", ret))
1645 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1646 journal_nonce(jset), jset);
1648 if (!validate_before_checksum &&
1649 jset_validate_for_write(c, jset))
1652 sectors = vstruct_sectors(jset, c->block_bits);
1653 BUG_ON(sectors > w->sectors);
1655 bytes = vstruct_bytes(jset);
1656 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1659 spin_lock(&j->lock);
1660 ret = journal_write_alloc(j, w, sectors);
1662 if (ret && j->can_discard) {
1663 spin_unlock(&j->lock);
1664 bch2_journal_do_discards(j);
1669 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1672 * write is allocated, no longer need to account for it in
1673 * bch2_journal_space_available():
1678 * journal entry has been compacted and allocated, recalculate space
1681 bch2_journal_space_available(j);
1682 spin_unlock(&j->lock);
1685 bch_err(c, "Unable to allocate journal write:\n%s",
1686 journal_debug_buf.buf);
1687 printbuf_exit(&journal_debug_buf);
1688 bch2_fatal_error(c);
1689 continue_at(cl, journal_write_done, c->io_complete_wq);
1693 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1695 if (c->opts.nochanges)
1698 for_each_rw_member(ca, c, i)
1701 if (nr_rw_members > 1)
1702 w->separate_flush = true;
1704 if (!JSET_NO_FLUSH(jset) && w->separate_flush) {
1705 for_each_rw_member(ca, c, i) {
1706 percpu_ref_get(&ca->io_ref);
1708 bio = ca->journal.bio;
1710 bio_set_dev(bio, ca->disk_sb.bdev);
1711 bio->bi_opf = REQ_OP_FLUSH;
1712 bio->bi_end_io = journal_write_endio;
1713 bio->bi_private = ca;
1714 closure_bio_submit(bio, cl);
1718 continue_at(cl, do_journal_write, c->io_complete_wq);
1721 continue_at(cl, journal_write_done, c->io_complete_wq);
1724 bch2_fatal_error(c);
1725 continue_at(cl, journal_write_done, c->io_complete_wq);