1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
6 #include "btree_update_interior.h"
7 #include "btree_write_buffer.h"
10 #include "disk_groups.h"
13 #include "journal_io.h"
14 #include "journal_reclaim.h"
15 #include "journal_seq_blacklist.h"
20 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
21 struct journal_replay *j)
23 darray_for_each(j->ptrs, i) {
24 struct bch_dev *ca = bch_dev_bkey_exists(c, i->dev);
27 div64_u64_rem(i->sector, ca->mi.bucket_size, &offset);
29 if (i != j->ptrs.data)
31 prt_printf(out, "%u:%u:%u (sector %llu)",
32 i->dev, i->bucket, i->bucket_offset, i->sector);
36 static void bch2_journal_replay_to_text(struct printbuf *out, struct bch_fs *c,
37 struct journal_replay *j)
39 prt_printf(out, "seq %llu ", le64_to_cpu(j->j.seq));
41 bch2_journal_ptrs_to_text(out, c, j);
43 struct jset_entry *entry;
44 for_each_jset_entry_type(entry, &j->j, BCH_JSET_ENTRY_datetime) {
45 struct jset_entry_datetime *datetime =
46 container_of(entry, struct jset_entry_datetime, entry);
47 bch2_prt_datetime(out, le64_to_cpu(datetime->seconds));
52 static struct nonce journal_nonce(const struct jset *jset)
54 return (struct nonce) {{
56 [1] = ((__le32 *) &jset->seq)[0],
57 [2] = ((__le32 *) &jset->seq)[1],
58 [3] = BCH_NONCE_JOURNAL,
62 static bool jset_csum_good(struct bch_fs *c, struct jset *j, struct bch_csum *csum)
64 if (!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j))) {
65 *csum = (struct bch_csum) {};
69 *csum = csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j);
70 return !bch2_crc_cmp(j->csum, *csum);
73 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
75 return (seq - c->journal_entries_base_seq) & (~0U >> 1);
78 static void __journal_replay_free(struct bch_fs *c,
79 struct journal_replay *i)
81 struct journal_replay **p =
82 genradix_ptr(&c->journal_entries,
83 journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
90 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
94 if (!c->opts.read_entire_journal)
95 __journal_replay_free(c, i);
105 #define JOURNAL_ENTRY_ADD_OK 0
106 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
109 * Given a journal entry we just read, add it to the list of journal entries to
112 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
113 struct journal_ptr entry_ptr,
114 struct journal_list *jlist, struct jset *j)
116 struct genradix_iter iter;
117 struct journal_replay **_i, *i, *dup;
118 size_t bytes = vstruct_bytes(j);
119 u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
120 struct printbuf buf = PRINTBUF;
121 int ret = JOURNAL_ENTRY_ADD_OK;
123 /* Is this entry older than the range we need? */
124 if (!c->opts.read_entire_journal &&
125 le64_to_cpu(j->seq) < jlist->last_seq)
126 return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
129 * genradixes are indexed by a ulong, not a u64, so we can't index them
130 * by sequence number directly: Assume instead that they will all fall
131 * within the range of +-2billion of the filrst one we find.
133 if (!c->journal_entries_base_seq)
134 c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
136 /* Drop entries we don't need anymore */
137 if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
138 genradix_for_each_from(&c->journal_entries, iter, _i,
139 journal_entry_radix_idx(c, jlist->last_seq)) {
145 if (le64_to_cpu(i->j.seq) >= last_seq)
147 journal_replay_free(c, i);
151 jlist->last_seq = max(jlist->last_seq, last_seq);
153 _i = genradix_ptr_alloc(&c->journal_entries,
154 journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
157 return -BCH_ERR_ENOMEM_journal_entry_add;
160 * Duplicate journal entries? If so we want the one that didn't have a
165 bool identical = bytes == vstruct_bytes(&dup->j) &&
166 !memcmp(j, &dup->j, bytes);
167 bool not_identical = !identical &&
168 entry_ptr.csum_good &&
171 bool same_device = false;
172 darray_for_each(dup->ptrs, ptr)
173 if (ptr->dev == ca->dev_idx)
176 ret = darray_push(&dup->ptrs, entry_ptr);
180 bch2_journal_replay_to_text(&buf, c, dup);
182 fsck_err_on(same_device,
183 c, journal_entry_dup_same_device,
184 "duplicate journal entry on same device\n %s",
187 fsck_err_on(not_identical,
188 c, journal_entry_replicas_data_mismatch,
189 "found duplicate but non identical journal entries\n %s",
192 if (entry_ptr.csum_good && !identical)
198 i = kvmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
200 return -BCH_ERR_ENOMEM_journal_entry_add;
202 darray_init(&i->ptrs);
203 i->csum_good = entry_ptr.csum_good;
205 unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct");
208 /* The first ptr should represent the jset we kept: */
209 darray_for_each(dup->ptrs, ptr)
210 darray_push(&i->ptrs, *ptr);
211 __journal_replay_free(c, dup);
213 darray_push(&i->ptrs, entry_ptr);
223 /* this fills in a range with empty jset_entries: */
224 static void journal_entry_null_range(void *start, void *end)
226 struct jset_entry *entry;
228 for (entry = start; entry != end; entry = vstruct_next(entry))
229 memset(entry, 0, sizeof(*entry));
232 #define JOURNAL_ENTRY_REREAD 5
233 #define JOURNAL_ENTRY_NONE 6
234 #define JOURNAL_ENTRY_BAD 7
236 static void journal_entry_err_msg(struct printbuf *out,
239 struct jset_entry *entry)
241 prt_str(out, "invalid journal entry, version=");
242 bch2_version_to_text(out, version);
245 prt_str(out, " type=");
246 prt_str(out, bch2_jset_entry_types[entry->type]);
250 prt_printf(out, " in superblock");
253 prt_printf(out, " seq=%llu", le64_to_cpu(jset->seq));
256 prt_printf(out, " offset=%zi/%u",
257 (u64 *) entry - jset->_data,
258 le32_to_cpu(jset->u64s));
264 #define journal_entry_err(c, version, jset, entry, _err, msg, ...) \
266 struct printbuf _buf = PRINTBUF; \
268 journal_entry_err_msg(&_buf, version, jset, entry); \
269 prt_printf(&_buf, msg, ##__VA_ARGS__); \
271 switch (flags & BKEY_INVALID_WRITE) { \
273 mustfix_fsck_err(c, _err, "%s", _buf.buf); \
276 bch2_sb_error_count(c, BCH_FSCK_ERR_##_err); \
277 bch_err(c, "corrupt metadata before write: %s\n", _buf.buf);\
278 if (bch2_fs_inconsistent(c)) { \
279 ret = -BCH_ERR_fsck_errors_not_fixed; \
285 printbuf_exit(&_buf); \
289 #define journal_entry_err_on(cond, ...) \
290 ((cond) ? journal_entry_err(__VA_ARGS__) : false)
292 #define FSCK_DELETED_KEY 5
294 static int journal_validate_key(struct bch_fs *c,
296 struct jset_entry *entry,
297 unsigned level, enum btree_id btree_id,
299 unsigned version, int big_endian,
300 enum bkey_invalid_flags flags)
302 int write = flags & BKEY_INVALID_WRITE;
303 void *next = vstruct_next(entry);
304 struct printbuf buf = PRINTBUF;
307 if (journal_entry_err_on(!k->k.u64s,
308 c, version, jset, entry,
309 journal_entry_bkey_u64s_0,
311 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
312 journal_entry_null_range(vstruct_next(entry), next);
313 return FSCK_DELETED_KEY;
316 if (journal_entry_err_on((void *) bkey_next(k) >
317 (void *) vstruct_next(entry),
318 c, version, jset, entry,
319 journal_entry_bkey_past_end,
320 "extends past end of journal entry")) {
321 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
322 journal_entry_null_range(vstruct_next(entry), next);
323 return FSCK_DELETED_KEY;
326 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT,
327 c, version, jset, entry,
328 journal_entry_bkey_bad_format,
329 "bad format %u", k->k.format)) {
330 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
331 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
332 journal_entry_null_range(vstruct_next(entry), next);
333 return FSCK_DELETED_KEY;
337 bch2_bkey_compat(level, btree_id, version, big_endian,
338 write, NULL, bkey_to_packed(k));
340 if (bch2_bkey_invalid(c, bkey_i_to_s_c(k),
341 __btree_node_type(level, btree_id), write, &buf)) {
342 printbuf_reset(&buf);
343 journal_entry_err_msg(&buf, version, jset, entry);
345 printbuf_indent_add(&buf, 2);
347 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
349 bch2_bkey_invalid(c, bkey_i_to_s_c(k),
350 __btree_node_type(level, btree_id), write, &buf);
352 mustfix_fsck_err(c, journal_entry_bkey_invalid,
355 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
356 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
357 journal_entry_null_range(vstruct_next(entry), next);
360 return FSCK_DELETED_KEY;
364 bch2_bkey_compat(level, btree_id, version, big_endian,
365 write, NULL, bkey_to_packed(k));
371 static int journal_entry_btree_keys_validate(struct bch_fs *c,
373 struct jset_entry *entry,
374 unsigned version, int big_endian,
375 enum bkey_invalid_flags flags)
377 struct bkey_i *k = entry->start;
379 while (k != vstruct_last(entry)) {
380 int ret = journal_validate_key(c, jset, entry,
383 k, version, big_endian,
384 flags|BKEY_INVALID_JOURNAL);
385 if (ret == FSCK_DELETED_KEY)
394 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
395 struct jset_entry *entry)
400 jset_entry_for_each_key(entry, k) {
403 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
405 prt_printf(out, "btree=%s l=%u ", bch2_btree_id_str(entry->btree_id), entry->level);
406 bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
411 static int journal_entry_btree_root_validate(struct bch_fs *c,
413 struct jset_entry *entry,
414 unsigned version, int big_endian,
415 enum bkey_invalid_flags flags)
417 struct bkey_i *k = entry->start;
420 if (journal_entry_err_on(!entry->u64s ||
421 le16_to_cpu(entry->u64s) != k->k.u64s,
422 c, version, jset, entry,
423 journal_entry_btree_root_bad_size,
424 "invalid btree root journal entry: wrong number of keys")) {
425 void *next = vstruct_next(entry);
427 * we don't want to null out this jset_entry,
428 * just the contents, so that later we can tell
429 * we were _supposed_ to have a btree root
432 journal_entry_null_range(vstruct_next(entry), next);
436 ret = journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
437 version, big_endian, flags);
438 if (ret == FSCK_DELETED_KEY)
444 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
445 struct jset_entry *entry)
447 journal_entry_btree_keys_to_text(out, c, entry);
450 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
452 struct jset_entry *entry,
453 unsigned version, int big_endian,
454 enum bkey_invalid_flags flags)
456 /* obsolete, don't care: */
460 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
461 struct jset_entry *entry)
465 static int journal_entry_blacklist_validate(struct bch_fs *c,
467 struct jset_entry *entry,
468 unsigned version, int big_endian,
469 enum bkey_invalid_flags flags)
473 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
474 c, version, jset, entry,
475 journal_entry_blacklist_bad_size,
476 "invalid journal seq blacklist entry: bad size")) {
477 journal_entry_null_range(entry, vstruct_next(entry));
483 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
484 struct jset_entry *entry)
486 struct jset_entry_blacklist *bl =
487 container_of(entry, struct jset_entry_blacklist, entry);
489 prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
492 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
494 struct jset_entry *entry,
495 unsigned version, int big_endian,
496 enum bkey_invalid_flags flags)
498 struct jset_entry_blacklist_v2 *bl_entry;
501 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
502 c, version, jset, entry,
503 journal_entry_blacklist_v2_bad_size,
504 "invalid journal seq blacklist entry: bad size")) {
505 journal_entry_null_range(entry, vstruct_next(entry));
509 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
511 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
512 le64_to_cpu(bl_entry->end),
513 c, version, jset, entry,
514 journal_entry_blacklist_v2_start_past_end,
515 "invalid journal seq blacklist entry: start > end")) {
516 journal_entry_null_range(entry, vstruct_next(entry));
523 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
524 struct jset_entry *entry)
526 struct jset_entry_blacklist_v2 *bl =
527 container_of(entry, struct jset_entry_blacklist_v2, entry);
529 prt_printf(out, "start=%llu end=%llu",
530 le64_to_cpu(bl->start),
531 le64_to_cpu(bl->end));
534 static int journal_entry_usage_validate(struct bch_fs *c,
536 struct jset_entry *entry,
537 unsigned version, int big_endian,
538 enum bkey_invalid_flags flags)
540 struct jset_entry_usage *u =
541 container_of(entry, struct jset_entry_usage, entry);
542 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
545 if (journal_entry_err_on(bytes < sizeof(*u),
546 c, version, jset, entry,
547 journal_entry_usage_bad_size,
548 "invalid journal entry usage: bad size")) {
549 journal_entry_null_range(entry, vstruct_next(entry));
557 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
558 struct jset_entry *entry)
560 struct jset_entry_usage *u =
561 container_of(entry, struct jset_entry_usage, entry);
563 prt_printf(out, "type=%s v=%llu",
564 bch2_fs_usage_types[u->entry.btree_id],
568 static int journal_entry_data_usage_validate(struct bch_fs *c,
570 struct jset_entry *entry,
571 unsigned version, int big_endian,
572 enum bkey_invalid_flags flags)
574 struct jset_entry_data_usage *u =
575 container_of(entry, struct jset_entry_data_usage, entry);
576 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
577 struct printbuf err = PRINTBUF;
580 if (journal_entry_err_on(bytes < sizeof(*u) ||
581 bytes < sizeof(*u) + u->r.nr_devs,
582 c, version, jset, entry,
583 journal_entry_data_usage_bad_size,
584 "invalid journal entry usage: bad size")) {
585 journal_entry_null_range(entry, vstruct_next(entry));
589 if (journal_entry_err_on(bch2_replicas_entry_validate(&u->r, c->disk_sb.sb, &err),
590 c, version, jset, entry,
591 journal_entry_data_usage_bad_size,
592 "invalid journal entry usage: %s", err.buf)) {
593 journal_entry_null_range(entry, vstruct_next(entry));
602 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
603 struct jset_entry *entry)
605 struct jset_entry_data_usage *u =
606 container_of(entry, struct jset_entry_data_usage, entry);
608 bch2_replicas_entry_to_text(out, &u->r);
609 prt_printf(out, "=%llu", le64_to_cpu(u->v));
612 static int journal_entry_clock_validate(struct bch_fs *c,
614 struct jset_entry *entry,
615 unsigned version, int big_endian,
616 enum bkey_invalid_flags flags)
618 struct jset_entry_clock *clock =
619 container_of(entry, struct jset_entry_clock, entry);
620 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
623 if (journal_entry_err_on(bytes != sizeof(*clock),
624 c, version, jset, entry,
625 journal_entry_clock_bad_size,
627 journal_entry_null_range(entry, vstruct_next(entry));
631 if (journal_entry_err_on(clock->rw > 1,
632 c, version, jset, entry,
633 journal_entry_clock_bad_rw,
635 journal_entry_null_range(entry, vstruct_next(entry));
643 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
644 struct jset_entry *entry)
646 struct jset_entry_clock *clock =
647 container_of(entry, struct jset_entry_clock, entry);
649 prt_printf(out, "%s=%llu", clock->rw ? "write" : "read", le64_to_cpu(clock->time));
652 static int journal_entry_dev_usage_validate(struct bch_fs *c,
654 struct jset_entry *entry,
655 unsigned version, int big_endian,
656 enum bkey_invalid_flags flags)
658 struct jset_entry_dev_usage *u =
659 container_of(entry, struct jset_entry_dev_usage, entry);
660 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
661 unsigned expected = sizeof(*u);
665 if (journal_entry_err_on(bytes < expected,
666 c, version, jset, entry,
667 journal_entry_dev_usage_bad_size,
668 "bad size (%u < %u)",
670 journal_entry_null_range(entry, vstruct_next(entry));
674 dev = le32_to_cpu(u->dev);
676 if (journal_entry_err_on(!bch2_dev_exists2(c, dev),
677 c, version, jset, entry,
678 journal_entry_dev_usage_bad_dev,
680 journal_entry_null_range(entry, vstruct_next(entry));
684 if (journal_entry_err_on(u->pad,
685 c, version, jset, entry,
686 journal_entry_dev_usage_bad_pad,
688 journal_entry_null_range(entry, vstruct_next(entry));
696 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
697 struct jset_entry *entry)
699 struct jset_entry_dev_usage *u =
700 container_of(entry, struct jset_entry_dev_usage, entry);
701 unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
703 prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
705 for (i = 0; i < nr_types; i++) {
706 bch2_prt_data_type(out, i);
707 prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
708 le64_to_cpu(u->d[i].buckets),
709 le64_to_cpu(u->d[i].sectors),
710 le64_to_cpu(u->d[i].fragmented));
714 static int journal_entry_log_validate(struct bch_fs *c,
716 struct jset_entry *entry,
717 unsigned version, int big_endian,
718 enum bkey_invalid_flags flags)
723 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
724 struct jset_entry *entry)
726 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
727 unsigned bytes = vstruct_bytes(entry) - offsetof(struct jset_entry_log, d);
729 prt_printf(out, "%.*s", bytes, l->d);
732 static int journal_entry_overwrite_validate(struct bch_fs *c,
734 struct jset_entry *entry,
735 unsigned version, int big_endian,
736 enum bkey_invalid_flags flags)
738 return journal_entry_btree_keys_validate(c, jset, entry,
739 version, big_endian, READ);
742 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
743 struct jset_entry *entry)
745 journal_entry_btree_keys_to_text(out, c, entry);
748 static int journal_entry_write_buffer_keys_validate(struct bch_fs *c,
750 struct jset_entry *entry,
751 unsigned version, int big_endian,
752 enum bkey_invalid_flags flags)
754 return journal_entry_btree_keys_validate(c, jset, entry,
755 version, big_endian, READ);
758 static void journal_entry_write_buffer_keys_to_text(struct printbuf *out, struct bch_fs *c,
759 struct jset_entry *entry)
761 journal_entry_btree_keys_to_text(out, c, entry);
764 static int journal_entry_datetime_validate(struct bch_fs *c,
766 struct jset_entry *entry,
767 unsigned version, int big_endian,
768 enum bkey_invalid_flags flags)
770 unsigned bytes = vstruct_bytes(entry);
771 unsigned expected = 16;
774 if (journal_entry_err_on(vstruct_bytes(entry) < expected,
775 c, version, jset, entry,
776 journal_entry_dev_usage_bad_size,
777 "bad size (%u < %u)",
779 journal_entry_null_range(entry, vstruct_next(entry));
786 static void journal_entry_datetime_to_text(struct printbuf *out, struct bch_fs *c,
787 struct jset_entry *entry)
789 struct jset_entry_datetime *datetime =
790 container_of(entry, struct jset_entry_datetime, entry);
792 bch2_prt_datetime(out, le64_to_cpu(datetime->seconds));
795 struct jset_entry_ops {
796 int (*validate)(struct bch_fs *, struct jset *,
797 struct jset_entry *, unsigned, int,
798 enum bkey_invalid_flags);
799 void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
802 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
804 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
805 .validate = journal_entry_##f##_validate, \
806 .to_text = journal_entry_##f##_to_text, \
808 BCH_JSET_ENTRY_TYPES()
812 int bch2_journal_entry_validate(struct bch_fs *c,
814 struct jset_entry *entry,
815 unsigned version, int big_endian,
816 enum bkey_invalid_flags flags)
818 return entry->type < BCH_JSET_ENTRY_NR
819 ? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
820 version, big_endian, flags)
824 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
825 struct jset_entry *entry)
827 if (entry->type < BCH_JSET_ENTRY_NR) {
828 prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
829 bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
831 prt_printf(out, "(unknown type %u)", entry->type);
835 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
836 enum bkey_invalid_flags flags)
838 unsigned version = le32_to_cpu(jset->version);
841 vstruct_for_each(jset, entry) {
842 if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset),
843 c, version, jset, entry,
844 journal_entry_past_jset_end,
845 "journal entry extends past end of jset")) {
846 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
850 ret = bch2_journal_entry_validate(c, jset, entry,
851 version, JSET_BIG_ENDIAN(jset), flags);
859 static int jset_validate(struct bch_fs *c,
861 struct jset *jset, u64 sector,
862 enum bkey_invalid_flags flags)
867 if (le64_to_cpu(jset->magic) != jset_magic(c))
868 return JOURNAL_ENTRY_NONE;
870 version = le32_to_cpu(jset->version);
871 if (journal_entry_err_on(!bch2_version_compatible(version),
872 c, version, jset, NULL,
873 jset_unsupported_version,
874 "%s sector %llu seq %llu: incompatible journal entry version %u.%u",
875 ca ? ca->name : c->name,
876 sector, le64_to_cpu(jset->seq),
877 BCH_VERSION_MAJOR(version),
878 BCH_VERSION_MINOR(version))) {
879 /* don't try to continue: */
883 if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
884 c, version, jset, NULL,
886 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
887 ca ? ca->name : c->name,
888 sector, le64_to_cpu(jset->seq),
889 JSET_CSUM_TYPE(jset)))
890 ret = JOURNAL_ENTRY_BAD;
892 /* last_seq is ignored when JSET_NO_FLUSH is true */
893 if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
894 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
895 c, version, jset, NULL,
896 jset_last_seq_newer_than_seq,
897 "invalid journal entry: last_seq > seq (%llu > %llu)",
898 le64_to_cpu(jset->last_seq),
899 le64_to_cpu(jset->seq))) {
900 jset->last_seq = jset->seq;
901 return JOURNAL_ENTRY_BAD;
904 ret = jset_validate_entries(c, jset, flags);
909 static int jset_validate_early(struct bch_fs *c,
911 struct jset *jset, u64 sector,
912 unsigned bucket_sectors_left,
913 unsigned sectors_read)
915 size_t bytes = vstruct_bytes(jset);
917 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
920 if (le64_to_cpu(jset->magic) != jset_magic(c))
921 return JOURNAL_ENTRY_NONE;
923 version = le32_to_cpu(jset->version);
924 if (journal_entry_err_on(!bch2_version_compatible(version),
925 c, version, jset, NULL,
926 jset_unsupported_version,
927 "%s sector %llu seq %llu: unknown journal entry version %u.%u",
928 ca ? ca->name : c->name,
929 sector, le64_to_cpu(jset->seq),
930 BCH_VERSION_MAJOR(version),
931 BCH_VERSION_MINOR(version))) {
932 /* don't try to continue: */
936 if (bytes > (sectors_read << 9) &&
937 sectors_read < bucket_sectors_left)
938 return JOURNAL_ENTRY_REREAD;
940 if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
941 c, version, jset, NULL,
942 jset_past_bucket_end,
943 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
944 ca ? ca->name : c->name,
945 sector, le64_to_cpu(jset->seq), bytes))
946 le32_add_cpu(&jset->u64s,
947 -((bytes - (bucket_sectors_left << 9)) / 8));
952 struct journal_read_buf {
957 static int journal_read_buf_realloc(struct journal_read_buf *b,
962 /* the bios are sized for this many pages, max: */
963 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
964 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
966 new_size = roundup_pow_of_two(new_size);
967 n = kvmalloc(new_size, GFP_KERNEL);
969 return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
977 static int journal_read_bucket(struct bch_dev *ca,
978 struct journal_read_buf *buf,
979 struct journal_list *jlist,
982 struct bch_fs *c = ca->fs;
983 struct journal_device *ja = &ca->journal;
984 struct jset *j = NULL;
985 unsigned sectors, sectors_read = 0;
986 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
987 end = offset + ca->mi.bucket_size;
988 bool saw_bad = false, csum_good;
989 struct printbuf err = PRINTBUF;
992 pr_debug("reading %u", bucket);
994 while (offset < end) {
999 sectors_read = min_t(unsigned,
1000 end - offset, buf->size >> 9);
1001 nr_bvecs = buf_pages(buf->data, sectors_read << 9);
1003 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
1004 bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
1006 bio->bi_iter.bi_sector = offset;
1007 bch2_bio_map(bio, buf->data, sectors_read << 9);
1009 ret = submit_bio_wait(bio);
1012 if (bch2_dev_io_err_on(ret, ca, BCH_MEMBER_ERROR_read,
1013 "journal read error: sector %llu",
1015 bch2_meta_read_fault("journal")) {
1017 * We don't error out of the recovery process
1018 * here, since the relevant journal entry may be
1019 * found on a different device, and missing or
1020 * no journal entries will be handled later
1028 ret = jset_validate_early(c, ca, j, offset,
1029 end - offset, sectors_read);
1032 sectors = vstruct_sectors(j, c->block_bits);
1034 case JOURNAL_ENTRY_REREAD:
1035 if (vstruct_bytes(j) > buf->size) {
1036 ret = journal_read_buf_realloc(buf,
1042 case JOURNAL_ENTRY_NONE:
1046 * On checksum error we don't really trust the size
1047 * field of the journal entry we read, so try reading
1048 * again at next block boundary:
1050 sectors = block_sectors(c);
1057 * This happens sometimes if we don't have discards on -
1058 * when we've partially overwritten a bucket with new
1059 * journal entries. We don't need the rest of the
1062 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
1065 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
1067 enum bch_csum_type csum_type = JSET_CSUM_TYPE(j);
1068 struct bch_csum csum;
1069 csum_good = jset_csum_good(c, j, &csum);
1071 if (bch2_dev_io_err_on(!csum_good, ca, BCH_MEMBER_ERROR_checksum,
1073 (printbuf_reset(&err),
1074 prt_str(&err, "journal "),
1075 bch2_csum_err_msg(&err, csum_type, j->csum, csum),
1079 ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
1081 vstruct_end(j) - (void *) j->encrypted_start);
1082 bch2_fs_fatal_err_on(ret, c,
1083 "error decrypting journal entry: %s",
1086 mutex_lock(&jlist->lock);
1087 ret = journal_entry_add(c, ca, (struct journal_ptr) {
1088 .csum_good = csum_good,
1091 .bucket_offset = offset -
1092 bucket_to_sector(ca, ja->buckets[bucket]),
1095 mutex_unlock(&jlist->lock);
1098 case JOURNAL_ENTRY_ADD_OK:
1100 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
1108 sectors_read -= sectors;
1109 j = ((void *) j) + (sectors << 9);
1115 printbuf_exit(&err);
1119 static CLOSURE_CALLBACK(bch2_journal_read_device)
1121 closure_type(ja, struct journal_device, read);
1122 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
1123 struct bch_fs *c = ca->fs;
1124 struct journal_list *jlist =
1125 container_of(cl->parent, struct journal_list, cl);
1126 struct journal_replay *r, **_r;
1127 struct genradix_iter iter;
1128 struct journal_read_buf buf = { NULL, 0 };
1135 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1139 pr_debug("%u journal buckets", ja->nr);
1141 for (i = 0; i < ja->nr; i++) {
1142 ret = journal_read_bucket(ca, &buf, jlist, i);
1147 ja->sectors_free = ca->mi.bucket_size;
1149 mutex_lock(&jlist->lock);
1150 genradix_for_each_reverse(&c->journal_entries, iter, _r) {
1156 darray_for_each(r->ptrs, i)
1157 if (i->dev == ca->dev_idx) {
1158 unsigned wrote = bucket_remainder(ca, i->sector) +
1159 vstruct_sectors(&r->j, c->block_bits);
1161 ja->cur_idx = i->bucket;
1162 ja->sectors_free = ca->mi.bucket_size - wrote;
1167 mutex_unlock(&jlist->lock);
1169 if (ja->bucket_seq[ja->cur_idx] &&
1170 ja->sectors_free == ca->mi.bucket_size) {
1173 * Debug code for ZNS support, where we (probably) want to be
1174 * correlated where we stopped in the journal to the zone write
1177 bch_err(c, "ja->sectors_free == ca->mi.bucket_size");
1178 bch_err(c, "cur_idx %u/%u", ja->cur_idx, ja->nr);
1179 for (i = 0; i < 3; i++) {
1180 unsigned idx = (ja->cur_idx + ja->nr - 1 + i) % ja->nr;
1182 bch_err(c, "bucket_seq[%u] = %llu", idx, ja->bucket_seq[idx]);
1185 ja->sectors_free = 0;
1189 * Set dirty_idx to indicate the entire journal is full and needs to be
1190 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1191 * pinned when it first runs:
1193 ja->discard_idx = ja->dirty_idx_ondisk =
1194 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1196 bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1198 percpu_ref_put(&ca->io_ref);
1202 mutex_lock(&jlist->lock);
1204 mutex_unlock(&jlist->lock);
1208 int bch2_journal_read(struct bch_fs *c,
1213 struct journal_list jlist;
1214 struct journal_replay *i, **_i, *prev = NULL;
1215 struct genradix_iter radix_iter;
1216 struct printbuf buf = PRINTBUF;
1217 bool degraded = false, last_write_torn = false;
1221 closure_init_stack(&jlist.cl);
1222 mutex_init(&jlist.lock);
1226 for_each_member_device(c, ca) {
1227 if (!c->opts.fsck &&
1228 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1231 if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1232 ca->mi.state == BCH_MEMBER_STATE_ro) &&
1233 percpu_ref_tryget(&ca->io_ref))
1234 closure_call(&ca->journal.read,
1235 bch2_journal_read_device,
1242 closure_sync(&jlist.cl);
1252 * Find most recent flush entry, and ignore newer non flush entries -
1253 * those entries will be blacklisted:
1255 genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1256 enum bkey_invalid_flags flags = BKEY_INVALID_JOURNAL;
1260 if (!i || i->ignore)
1264 *blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1266 if (JSET_NO_FLUSH(&i->j)) {
1271 if (!last_write_torn && !i->csum_good) {
1272 last_write_torn = true;
1277 if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1278 c, le32_to_cpu(i->j.version), &i->j, NULL,
1279 jset_last_seq_newer_than_seq,
1280 "invalid journal entry: last_seq > seq (%llu > %llu)",
1281 le64_to_cpu(i->j.last_seq),
1282 le64_to_cpu(i->j.seq)))
1283 i->j.last_seq = i->j.seq;
1285 *last_seq = le64_to_cpu(i->j.last_seq);
1286 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
1291 bch_info(c, "journal read done, but no entries found");
1296 fsck_err(c, dirty_but_no_journal_entries_post_drop_nonflushes,
1297 "journal read done, but no entries found after dropping non-flushes");
1301 bch_info(c, "journal read done, replaying entries %llu-%llu",
1302 *last_seq, *blacklist_seq - 1);
1304 if (*start_seq != *blacklist_seq)
1305 bch_info(c, "dropped unflushed entries %llu-%llu",
1306 *blacklist_seq, *start_seq - 1);
1308 /* Drop blacklisted entries and entries older than last_seq: */
1309 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1312 if (!i || i->ignore)
1315 seq = le64_to_cpu(i->j.seq);
1316 if (seq < *last_seq) {
1317 journal_replay_free(c, i);
1321 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1322 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1323 jset_seq_blacklisted,
1324 "found blacklisted journal entry %llu", seq);
1329 /* Check for missing entries: */
1331 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1334 if (!i || i->ignore)
1337 BUG_ON(seq > le64_to_cpu(i->j.seq));
1339 while (seq < le64_to_cpu(i->j.seq)) {
1340 u64 missing_start, missing_end;
1341 struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1343 while (seq < le64_to_cpu(i->j.seq) &&
1344 bch2_journal_seq_is_blacklisted(c, seq, false))
1347 if (seq == le64_to_cpu(i->j.seq))
1350 missing_start = seq;
1352 while (seq < le64_to_cpu(i->j.seq) &&
1353 !bch2_journal_seq_is_blacklisted(c, seq, false))
1357 bch2_journal_ptrs_to_text(&buf1, c, prev);
1358 prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1360 prt_printf(&buf1, "(none)");
1361 bch2_journal_ptrs_to_text(&buf2, c, i);
1363 missing_end = seq - 1;
1364 fsck_err(c, journal_entries_missing,
1365 "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1368 missing_start, missing_end,
1369 *last_seq, *blacklist_seq - 1,
1370 buf1.buf, buf2.buf);
1372 printbuf_exit(&buf1);
1373 printbuf_exit(&buf2);
1380 genradix_for_each(&c->journal_entries, radix_iter, _i) {
1381 struct bch_replicas_padded replicas = {
1382 .e.data_type = BCH_DATA_journal,
1387 if (!i || i->ignore)
1390 darray_for_each(i->ptrs, ptr) {
1391 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
1393 if (!ptr->csum_good)
1394 bch_err_dev_offset(ca, ptr->sector,
1395 "invalid journal checksum, seq %llu%s",
1396 le64_to_cpu(i->j.seq),
1397 i->csum_good ? " (had good copy on another device)" : "");
1400 ret = jset_validate(c,
1401 bch_dev_bkey_exists(c, i->ptrs.data[0].dev),
1403 i->ptrs.data[0].sector,
1408 darray_for_each(i->ptrs, ptr)
1409 replicas.e.devs[replicas.e.nr_devs++] = ptr->dev;
1411 bch2_replicas_entry_sort(&replicas.e);
1413 printbuf_reset(&buf);
1414 bch2_replicas_entry_to_text(&buf, &replicas.e);
1417 !bch2_replicas_marked(c, &replicas.e) &&
1418 (le64_to_cpu(i->j.seq) == *last_seq ||
1419 fsck_err(c, journal_entry_replicas_not_marked,
1420 "superblock not marked as containing replicas for journal entry %llu\n %s",
1421 le64_to_cpu(i->j.seq), buf.buf))) {
1422 ret = bch2_mark_replicas(c, &replicas.e);
1429 printbuf_exit(&buf);
1433 /* journal write: */
1435 static void __journal_write_alloc(struct journal *j,
1436 struct journal_buf *w,
1437 struct dev_alloc_list *devs_sorted,
1440 unsigned replicas_want)
1442 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1443 struct journal_device *ja;
1447 if (*replicas >= replicas_want)
1450 for (i = 0; i < devs_sorted->nr; i++) {
1451 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
1458 * Check that we can use this device, and aren't already using
1461 if (!ca->mi.durability ||
1462 ca->mi.state != BCH_MEMBER_STATE_rw ||
1464 bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) ||
1465 sectors > ja->sectors_free)
1468 bch2_dev_stripe_increment(ca, &j->wp.stripe);
1470 bch2_bkey_append_ptr(&w->key,
1471 (struct bch_extent_ptr) {
1472 .offset = bucket_to_sector(ca,
1473 ja->buckets[ja->cur_idx]) +
1474 ca->mi.bucket_size -
1479 ja->sectors_free -= sectors;
1480 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1482 *replicas += ca->mi.durability;
1484 if (*replicas >= replicas_want)
1490 * journal_write_alloc - decide where to write next journal entry
1492 * @j: journal object
1493 * @w: journal buf (entry to be written)
1495 * Returns: 0 on success, or -EROFS on failure
1497 static int journal_write_alloc(struct journal *j, struct journal_buf *w)
1499 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1500 struct bch_devs_mask devs;
1501 struct journal_device *ja;
1503 struct dev_alloc_list devs_sorted;
1504 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1505 unsigned target = c->opts.metadata_target ?:
1506 c->opts.foreground_target;
1507 unsigned i, replicas = 0, replicas_want =
1508 READ_ONCE(c->opts.metadata_replicas);
1512 devs = target_rw_devs(c, BCH_DATA_journal, target);
1514 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1516 __journal_write_alloc(j, w, &devs_sorted,
1517 sectors, &replicas, replicas_want);
1519 if (replicas >= replicas_want)
1522 for (i = 0; i < devs_sorted.nr; i++) {
1523 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
1529 if (sectors > ja->sectors_free &&
1530 sectors <= ca->mi.bucket_size &&
1531 bch2_journal_dev_buckets_available(j, ja,
1532 journal_space_discarded)) {
1533 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1534 ja->sectors_free = ca->mi.bucket_size;
1537 * ja->bucket_seq[ja->cur_idx] must always have
1538 * something sensible:
1540 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1544 __journal_write_alloc(j, w, &devs_sorted,
1545 sectors, &replicas, replicas_want);
1547 if (replicas < replicas_want && target) {
1548 /* Retry from all devices: */
1555 BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1557 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1560 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1562 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1564 /* we aren't holding j->lock: */
1565 unsigned new_size = READ_ONCE(j->buf_size_want);
1568 if (buf->buf_size >= new_size)
1571 size_t btree_write_buffer_size = new_size / 64;
1573 if (bch2_btree_write_buffer_resize(c, btree_write_buffer_size))
1576 new_buf = kvmalloc(new_size, GFP_NOFS|__GFP_NOWARN);
1580 memcpy(new_buf, buf->data, buf->buf_size);
1582 spin_lock(&j->lock);
1583 swap(buf->data, new_buf);
1584 swap(buf->buf_size, new_size);
1585 spin_unlock(&j->lock);
1590 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1592 return j->buf + (journal_last_unwritten_seq(j) & JOURNAL_BUF_MASK);
1595 static CLOSURE_CALLBACK(journal_write_done)
1597 closure_type(w, struct journal_buf, io);
1598 struct journal *j = container_of(w, struct journal, buf[w->idx]);
1599 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1600 struct bch_replicas_padded replicas;
1601 union journal_res_state old, new;
1602 u64 v, seq = le64_to_cpu(w->data->seq);
1605 time_stats_update(!JSET_NO_FLUSH(w->data)
1606 ? j->flush_write_time
1607 : j->noflush_write_time, j->write_start_time);
1609 if (!w->devs_written.nr) {
1610 bch_err(c, "unable to write journal to sufficient devices");
1613 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1615 if (bch2_mark_replicas(c, &replicas.e))
1620 bch2_fatal_error(c);
1622 closure_debug_destroy(cl);
1624 spin_lock(&j->lock);
1625 if (seq >= j->pin.front)
1626 journal_seq_pin(j, seq)->devs = w->devs_written;
1627 if (err && (!j->err_seq || seq < j->err_seq))
1629 w->write_done = true;
1631 bool completed = false;
1633 for (seq = journal_last_unwritten_seq(j);
1634 seq <= journal_cur_seq(j);
1636 w = j->buf + (seq & JOURNAL_BUF_MASK);
1640 if (!j->err_seq && !JSET_NO_FLUSH(w->data)) {
1641 j->flushed_seq_ondisk = seq;
1642 j->last_seq_ondisk = w->last_seq;
1644 bch2_do_discards(c);
1645 closure_wake_up(&c->freelist_wait);
1646 bch2_reset_alloc_cursors(c);
1649 j->seq_ondisk = seq;
1652 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1655 * Must come before signaling write completion, for
1656 * bch2_fs_journal_stop():
1658 if (j->watermark != BCH_WATERMARK_stripe)
1659 journal_reclaim_kick(&c->journal);
1661 v = atomic64_read(&j->reservations.counter);
1664 BUG_ON(journal_state_count(new, new.unwritten_idx));
1665 BUG_ON(new.unwritten_idx != (seq & JOURNAL_BUF_MASK));
1667 new.unwritten_idx++;
1668 } while ((v = atomic64_cmpxchg(&j->reservations.counter, old.v, new.v)) != old.v);
1674 bch2_journal_reclaim_fast(j);
1675 bch2_journal_space_available(j);
1677 track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight], false);
1679 closure_wake_up(&w->wait);
1683 if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1684 new.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1685 struct journal_buf *buf = journal_cur_buf(j);
1686 long delta = buf->expires - jiffies;
1689 * We don't close a journal entry to write it while there's
1690 * previous entries still in flight - the current journal entry
1691 * might want to be written now:
1693 mod_delayed_work(j->wq, &j->write_work, max(0L, delta));
1696 spin_unlock(&j->lock);
1699 static void journal_write_endio(struct bio *bio)
1701 struct journal_bio *jbio = container_of(bio, struct journal_bio, bio);
1702 struct bch_dev *ca = jbio->ca;
1703 struct journal *j = &ca->fs->journal;
1704 struct journal_buf *w = j->buf + jbio->buf_idx;
1706 if (bch2_dev_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_write,
1707 "error writing journal entry %llu: %s",
1708 le64_to_cpu(w->data->seq),
1709 bch2_blk_status_to_str(bio->bi_status)) ||
1710 bch2_meta_write_fault("journal")) {
1711 unsigned long flags;
1713 spin_lock_irqsave(&j->err_lock, flags);
1714 bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1715 spin_unlock_irqrestore(&j->err_lock, flags);
1718 closure_put(&w->io);
1719 percpu_ref_put(&ca->io_ref);
1722 static CLOSURE_CALLBACK(do_journal_write)
1724 closure_type(w, struct journal_buf, io);
1725 struct journal *j = container_of(w, struct journal, buf[w->idx]);
1726 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1727 unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1729 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1730 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
1731 struct journal_device *ja = &ca->journal;
1733 if (!percpu_ref_tryget(&ca->io_ref)) {
1735 bch_err(c, "missing device for journal write\n");
1739 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1742 struct bio *bio = &ja->bio[w->idx]->bio;
1743 bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1744 bio->bi_iter.bi_sector = ptr->offset;
1745 bio->bi_end_io = journal_write_endio;
1746 bio->bi_private = ca;
1748 BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1749 ca->prev_journal_sector = bio->bi_iter.bi_sector;
1751 if (!JSET_NO_FLUSH(w->data))
1752 bio->bi_opf |= REQ_FUA;
1753 if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1754 bio->bi_opf |= REQ_PREFLUSH;
1756 bch2_bio_map(bio, w->data, sectors << 9);
1758 trace_and_count(c, journal_write, bio);
1759 closure_bio_submit(bio, cl);
1761 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1764 continue_at(cl, journal_write_done, j->wq);
1767 static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w)
1769 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1770 struct jset_entry *start, *end;
1771 struct jset *jset = w->data;
1772 struct journal_keys_to_wb wb = { NULL };
1773 unsigned sectors, bytes, u64s;
1774 unsigned long btree_roots_have = 0;
1775 bool validate_before_checksum = false;
1776 u64 seq = le64_to_cpu(jset->seq);
1780 * Simple compaction, dropping empty jset_entries (from journal
1781 * reservations that weren't fully used) and merging jset_entries that
1784 * If we wanted to be really fancy here, we could sort all the keys in
1785 * the jset and drop keys that were overwritten - probably not worth it:
1787 vstruct_for_each(jset, i) {
1788 unsigned u64s = le16_to_cpu(i->u64s);
1795 * New btree roots are set by journalling them; when the journal
1796 * entry gets written we have to propagate them to
1799 * But, every journal entry we write has to contain all the
1800 * btree roots (at least for now); so after we copy btree roots
1801 * to c->btree_roots we have to get any missing btree roots and
1802 * add them to this journal entry:
1805 case BCH_JSET_ENTRY_btree_root:
1806 bch2_journal_entry_to_btree_root(c, i);
1807 __set_bit(i->btree_id, &btree_roots_have);
1809 case BCH_JSET_ENTRY_write_buffer_keys:
1810 EBUG_ON(!w->need_flush_to_write_buffer);
1813 bch2_journal_keys_to_write_buffer_start(c, &wb, seq);
1816 jset_entry_for_each_key(i, k) {
1817 ret = bch2_journal_key_to_wb(c, &wb, i->btree_id, k);
1819 bch2_fs_fatal_error(c, "-ENOMEM flushing journal keys to btree write buffer");
1820 bch2_journal_keys_to_write_buffer_end(c, &wb);
1824 i->type = BCH_JSET_ENTRY_btree_keys;
1830 bch2_journal_keys_to_write_buffer_end(c, &wb);
1831 w->need_flush_to_write_buffer = false;
1833 start = end = vstruct_last(jset);
1835 end = bch2_btree_roots_to_journal_entries(c, end, btree_roots_have);
1837 struct jset_entry_datetime *d =
1838 container_of(jset_entry_init(&end, sizeof(*d)), struct jset_entry_datetime, entry);
1839 d->entry.type = BCH_JSET_ENTRY_datetime;
1840 d->seconds = cpu_to_le64(ktime_get_real_seconds());
1842 bch2_journal_super_entries_add_common(c, &end, seq);
1843 u64s = (u64 *) end - (u64 *) start;
1844 BUG_ON(u64s > j->entry_u64s_reserved);
1846 le32_add_cpu(&jset->u64s, u64s);
1848 sectors = vstruct_sectors(jset, c->block_bits);
1849 bytes = vstruct_bytes(jset);
1851 if (sectors > w->sectors) {
1852 bch2_fs_fatal_error(c, "aieeee! journal write overran available space, %zu > %u (extra %u reserved %u/%u)",
1853 vstruct_bytes(jset), w->sectors << 9,
1854 u64s, w->u64s_reserved, j->entry_u64s_reserved);
1858 jset->magic = cpu_to_le64(jset_magic(c));
1859 jset->version = cpu_to_le32(c->sb.version);
1861 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1862 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1864 if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1865 j->last_empty_seq = seq;
1867 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1868 validate_before_checksum = true;
1870 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1871 validate_before_checksum = true;
1873 if (validate_before_checksum &&
1874 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1877 ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1878 jset->encrypted_start,
1879 vstruct_end(jset) - (void *) jset->encrypted_start);
1880 if (bch2_fs_fatal_err_on(ret, c,
1881 "error decrypting journal entry: %i", ret))
1884 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1885 journal_nonce(jset), jset);
1887 if (!validate_before_checksum &&
1888 (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1891 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1895 static int bch2_journal_write_pick_flush(struct journal *j, struct journal_buf *w)
1897 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1898 int error = bch2_journal_error(j);
1901 * If the journal is in an error state - we did an emergency shutdown -
1902 * we prefer to continue doing journal writes. We just mark them as
1903 * noflush so they'll never be used, but they'll still be visible by the
1904 * list_journal tool - this helps in debugging.
1906 * There's a caveat: the first journal write after marking the
1907 * superblock dirty must always be a flush write, because on startup
1908 * from a clean shutdown we didn't necessarily read the journal and the
1909 * new journal write might overwrite whatever was in the journal
1910 * previously - we can't leave the journal without any flush writes in
1913 * So if we're in an error state, and we're still starting up, we don't
1914 * write anything at all.
1916 if (error && test_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags))
1922 (jiffies - j->last_flush_write) < msecs_to_jiffies(c->opts.journal_flush_delay) &&
1923 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags))) {
1925 SET_JSET_NO_FLUSH(w->data, true);
1926 w->data->last_seq = 0;
1929 j->nr_noflush_writes++;
1931 j->last_flush_write = jiffies;
1932 j->nr_flush_writes++;
1933 clear_bit(JOURNAL_NEED_FLUSH_WRITE, &j->flags);
1939 CLOSURE_CALLBACK(bch2_journal_write)
1941 closure_type(w, struct journal_buf, io);
1942 struct journal *j = container_of(w, struct journal, buf[w->idx]);
1943 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1944 struct bch_replicas_padded replicas;
1945 struct printbuf journal_debug_buf = PRINTBUF;
1946 unsigned nr_rw_members = 0;
1949 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1950 BUG_ON(w->write_allocated);
1952 j->write_start_time = local_clock();
1954 spin_lock(&j->lock);
1955 ret = bch2_journal_write_pick_flush(j, w);
1956 spin_unlock(&j->lock);
1960 mutex_lock(&j->buf_lock);
1961 journal_buf_realloc(j, w);
1963 ret = bch2_journal_write_prep(j, w);
1964 mutex_unlock(&j->buf_lock);
1968 j->entry_bytes_written += vstruct_bytes(w->data);
1971 spin_lock(&j->lock);
1972 ret = journal_write_alloc(j, w);
1973 if (!ret || !j->can_discard)
1976 spin_unlock(&j->lock);
1977 bch2_journal_do_discards(j);
1981 __bch2_journal_debug_to_text(&journal_debug_buf, j);
1982 spin_unlock(&j->lock);
1983 bch_err(c, "Unable to allocate journal write:\n%s",
1984 journal_debug_buf.buf);
1985 printbuf_exit(&journal_debug_buf);
1990 * write is allocated, no longer need to account for it in
1991 * bch2_journal_space_available():
1994 w->write_allocated = true;
1997 * journal entry has been compacted and allocated, recalculate space
2000 bch2_journal_space_available(j);
2001 bch2_journal_do_writes(j);
2002 spin_unlock(&j->lock);
2004 w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
2006 if (c->opts.nochanges)
2009 for_each_rw_member(c, ca)
2012 if (nr_rw_members > 1)
2013 w->separate_flush = true;
2016 * Mark journal replicas before we submit the write to guarantee
2017 * recovery will find the journal entries after a crash.
2019 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
2021 ret = bch2_mark_replicas(c, &replicas.e);
2025 if (!JSET_NO_FLUSH(w->data))
2026 closure_wait_event(&j->async_wait, j->seq_ondisk + 1 == le64_to_cpu(w->data->seq));
2028 if (!JSET_NO_FLUSH(w->data) && w->separate_flush) {
2029 for_each_rw_member(c, ca) {
2030 percpu_ref_get(&ca->io_ref);
2032 struct journal_device *ja = &ca->journal;
2033 struct bio *bio = &ja->bio[w->idx]->bio;
2034 bio_reset(bio, ca->disk_sb.bdev,
2035 REQ_OP_WRITE|REQ_SYNC|REQ_META|REQ_PREFLUSH);
2036 bio->bi_end_io = journal_write_endio;
2037 bio->bi_private = ca;
2038 closure_bio_submit(bio, cl);
2042 continue_at(cl, do_journal_write, j->wq);
2045 continue_at(cl, journal_write_done, j->wq);
2048 bch2_fatal_error(c);
2049 continue_at(cl, journal_write_done, j->wq);