1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_foreground.h"
5 #include "btree_update_interior.h"
11 #include "journal_io.h"
12 #include "journal_reclaim.h"
13 #include "journal_seq_blacklist.h"
16 #include <trace/events/bcachefs.h>
18 static void __journal_replay_free(struct journal_replay *i)
21 kvpfree(i, offsetof(struct journal_replay, j) +
22 vstruct_bytes(&i->j));
26 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i)
30 if (!c->opts.read_entire_journal)
31 __journal_replay_free(i);
37 struct list_head *head;
41 #define JOURNAL_ENTRY_ADD_OK 0
42 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE 5
45 * Given a journal entry we just read, add it to the list of journal entries to
48 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
49 struct journal_list *jlist, struct jset *j,
52 struct journal_replay *i, *pos;
53 struct bch_devs_list devs = { .nr = 0 };
54 struct list_head *where;
55 size_t bytes = vstruct_bytes(j);
59 list_for_each_entry_reverse(i, jlist->head, list) {
60 if (!JSET_NO_FLUSH(&i->j)) {
61 last_seq = le64_to_cpu(i->j.last_seq);
66 /* Is this entry older than the range we need? */
67 if (!c->opts.read_entire_journal &&
68 le64_to_cpu(j->seq) < last_seq) {
69 ret = JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
73 /* Drop entries we don't need anymore */
74 if (!JSET_NO_FLUSH(j)) {
75 list_for_each_entry_safe(i, pos, jlist->head, list) {
76 if (le64_to_cpu(i->j.seq) >= le64_to_cpu(j->last_seq))
78 journal_replay_free(c, i);
82 list_for_each_entry_reverse(i, jlist->head, list) {
83 if (le64_to_cpu(j->seq) > le64_to_cpu(i->j.seq)) {
91 i = where->next != jlist->head
92 ? container_of(where->next, struct journal_replay, list)
96 * Duplicate journal entries? If so we want the one that didn't have a
99 if (i && le64_to_cpu(j->seq) == le64_to_cpu(i->j.seq)) {
102 __journal_replay_free(i);
106 fsck_err_on(bytes != vstruct_bytes(&i->j) ||
107 memcmp(j, &i->j, bytes), c,
108 "found duplicate but non identical journal entries (seq %llu)",
109 le64_to_cpu(j->seq));
115 i = kvpmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
121 list_add(&i->list, where);
125 memcpy(&i->j, j, bytes);
127 if (!bch2_dev_list_has_dev(i->devs, ca->dev_idx))
128 bch2_dev_list_add_dev(&i->devs, ca->dev_idx);
130 fsck_err_on(1, c, "duplicate journal entries on same device");
131 ret = JOURNAL_ENTRY_ADD_OK;
137 static struct nonce journal_nonce(const struct jset *jset)
139 return (struct nonce) {{
141 [1] = ((__le32 *) &jset->seq)[0],
142 [2] = ((__le32 *) &jset->seq)[1],
143 [3] = BCH_NONCE_JOURNAL,
147 /* this fills in a range with empty jset_entries: */
148 static void journal_entry_null_range(void *start, void *end)
150 struct jset_entry *entry;
152 for (entry = start; entry != end; entry = vstruct_next(entry))
153 memset(entry, 0, sizeof(*entry));
156 #define JOURNAL_ENTRY_REREAD 5
157 #define JOURNAL_ENTRY_NONE 6
158 #define JOURNAL_ENTRY_BAD 7
160 #define journal_entry_err(c, msg, ...) \
164 mustfix_fsck_err(c, msg, ##__VA_ARGS__); \
167 bch_err(c, "corrupt metadata before write:\n" \
168 msg, ##__VA_ARGS__); \
169 if (bch2_fs_inconsistent(c)) { \
170 ret = BCH_FSCK_ERRORS_NOT_FIXED; \
178 #define journal_entry_err_on(cond, c, msg, ...) \
179 ((cond) ? journal_entry_err(c, msg, ##__VA_ARGS__) : false)
181 #define FSCK_DELETED_KEY 5
183 static int journal_validate_key(struct bch_fs *c, struct jset *jset,
184 struct jset_entry *entry,
185 unsigned level, enum btree_id btree_id,
187 const char *type, int write)
189 void *next = vstruct_next(entry);
191 unsigned version = le32_to_cpu(jset->version);
194 if (journal_entry_err_on(!k->k.u64s, c,
195 "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: k->u64s 0",
196 type, le64_to_cpu(jset->seq),
197 (u64 *) entry - jset->_data,
198 le32_to_cpu(jset->u64s),
199 (u64 *) k - entry->_data,
200 le16_to_cpu(entry->u64s))) {
201 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
202 journal_entry_null_range(vstruct_next(entry), next);
203 return FSCK_DELETED_KEY;
206 if (journal_entry_err_on((void *) bkey_next(k) >
207 (void *) vstruct_next(entry), c,
208 "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: extends past end of journal entry",
209 type, le64_to_cpu(jset->seq),
210 (u64 *) entry - jset->_data,
211 le32_to_cpu(jset->u64s),
212 (u64 *) k - entry->_data,
213 le16_to_cpu(entry->u64s))) {
214 entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
215 journal_entry_null_range(vstruct_next(entry), next);
216 return FSCK_DELETED_KEY;
219 if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT, c,
220 "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: bad format %u",
221 type, le64_to_cpu(jset->seq),
222 (u64 *) entry - jset->_data,
223 le32_to_cpu(jset->u64s),
224 (u64 *) k - entry->_data,
225 le16_to_cpu(entry->u64s),
227 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
228 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
229 journal_entry_null_range(vstruct_next(entry), next);
230 return FSCK_DELETED_KEY;
234 bch2_bkey_compat(level, btree_id, version,
235 JSET_BIG_ENDIAN(jset), write,
236 NULL, bkey_to_packed(k));
238 invalid = bch2_bkey_invalid(c, bkey_i_to_s_c(k),
239 __btree_node_type(level, btree_id));
243 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(k));
244 mustfix_fsck_err(c, "invalid %s in jset %llu offset %zi/%u entry offset %zi/%u: %s\n%s",
245 type, le64_to_cpu(jset->seq),
246 (u64 *) entry - jset->_data,
247 le32_to_cpu(jset->u64s),
248 (u64 *) k - entry->_data,
249 le16_to_cpu(entry->u64s),
252 le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
253 memmove(k, bkey_next(k), next - (void *) bkey_next(k));
254 journal_entry_null_range(vstruct_next(entry), next);
255 return FSCK_DELETED_KEY;
259 bch2_bkey_compat(level, btree_id, version,
260 JSET_BIG_ENDIAN(jset), write,
261 NULL, bkey_to_packed(k));
266 static int journal_entry_validate_btree_keys(struct bch_fs *c,
268 struct jset_entry *entry,
271 struct bkey_i *k = entry->start;
273 while (k != vstruct_last(entry)) {
274 int ret = journal_validate_key(c, jset, entry,
278 if (ret == FSCK_DELETED_KEY)
287 static int journal_entry_validate_btree_root(struct bch_fs *c,
289 struct jset_entry *entry,
292 struct bkey_i *k = entry->start;
295 if (journal_entry_err_on(!entry->u64s ||
296 le16_to_cpu(entry->u64s) != k->k.u64s, c,
297 "invalid btree root journal entry: wrong number of keys")) {
298 void *next = vstruct_next(entry);
300 * we don't want to null out this jset_entry,
301 * just the contents, so that later we can tell
302 * we were _supposed_ to have a btree root
305 journal_entry_null_range(vstruct_next(entry), next);
309 return journal_validate_key(c, jset, entry, 1, entry->btree_id, k,
310 "btree root", write);
315 static int journal_entry_validate_prio_ptrs(struct bch_fs *c,
317 struct jset_entry *entry,
320 /* obsolete, don't care: */
324 static int journal_entry_validate_blacklist(struct bch_fs *c,
326 struct jset_entry *entry,
331 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1, c,
332 "invalid journal seq blacklist entry: bad size")) {
333 journal_entry_null_range(entry, vstruct_next(entry));
339 static int journal_entry_validate_blacklist_v2(struct bch_fs *c,
341 struct jset_entry *entry,
344 struct jset_entry_blacklist_v2 *bl_entry;
347 if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2, c,
348 "invalid journal seq blacklist entry: bad size")) {
349 journal_entry_null_range(entry, vstruct_next(entry));
353 bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
355 if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
356 le64_to_cpu(bl_entry->end), c,
357 "invalid journal seq blacklist entry: start > end")) {
358 journal_entry_null_range(entry, vstruct_next(entry));
365 static int journal_entry_validate_usage(struct bch_fs *c,
367 struct jset_entry *entry,
370 struct jset_entry_usage *u =
371 container_of(entry, struct jset_entry_usage, entry);
372 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
375 if (journal_entry_err_on(bytes < sizeof(*u),
377 "invalid journal entry usage: bad size")) {
378 journal_entry_null_range(entry, vstruct_next(entry));
386 static int journal_entry_validate_data_usage(struct bch_fs *c,
388 struct jset_entry *entry,
391 struct jset_entry_data_usage *u =
392 container_of(entry, struct jset_entry_data_usage, entry);
393 unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
396 if (journal_entry_err_on(bytes < sizeof(*u) ||
397 bytes < sizeof(*u) + u->r.nr_devs,
399 "invalid journal entry usage: bad size")) {
400 journal_entry_null_range(entry, vstruct_next(entry));
408 struct jset_entry_ops {
409 int (*validate)(struct bch_fs *, struct jset *,
410 struct jset_entry *, int);
413 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
415 [BCH_JSET_ENTRY_##f] = (struct jset_entry_ops) { \
416 .validate = journal_entry_validate_##f, \
418 BCH_JSET_ENTRY_TYPES()
422 static int journal_entry_validate(struct bch_fs *c, struct jset *jset,
423 struct jset_entry *entry, int write)
425 return entry->type < BCH_JSET_ENTRY_NR
426 ? bch2_jset_entry_ops[entry->type].validate(c, jset,
431 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
434 struct jset_entry *entry;
437 vstruct_for_each(jset, entry) {
438 if (journal_entry_err_on(vstruct_next(entry) >
439 vstruct_last(jset), c,
440 "journal entry extends past end of jset")) {
441 jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
445 ret = journal_entry_validate(c, jset, entry, write);
453 static int jset_validate(struct bch_fs *c,
455 struct jset *jset, u64 sector,
456 unsigned bucket_sectors_left,
457 unsigned sectors_read,
460 size_t bytes = vstruct_bytes(jset);
461 struct bch_csum csum;
465 if (le64_to_cpu(jset->magic) != jset_magic(c))
466 return JOURNAL_ENTRY_NONE;
468 version = le32_to_cpu(jset->version);
469 if (journal_entry_err_on((version != BCH_JSET_VERSION_OLD &&
470 version < bcachefs_metadata_version_min) ||
471 version >= bcachefs_metadata_version_max, c,
472 "%s sector %llu seq %llu: unknown journal entry version %u",
473 ca->name, sector, le64_to_cpu(jset->seq),
475 /* don't try to continue: */
479 if (bytes > (sectors_read << 9) &&
480 sectors_read < bucket_sectors_left)
481 return JOURNAL_ENTRY_REREAD;
483 if (journal_entry_err_on(bytes > bucket_sectors_left << 9, c,
484 "%s sector %llu seq %llu: journal entry too big (%zu bytes)",
485 ca->name, sector, le64_to_cpu(jset->seq), bytes)) {
486 ret = JOURNAL_ENTRY_BAD;
487 le32_add_cpu(&jset->u64s,
488 -((bytes - (bucket_sectors_left << 9)) / 8));
491 if (fsck_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)), c,
492 "%s sector %llu seq %llu: journal entry with unknown csum type %llu",
493 ca->name, sector, le64_to_cpu(jset->seq),
494 JSET_CSUM_TYPE(jset))) {
495 ret = JOURNAL_ENTRY_BAD;
499 csum = csum_vstruct(c, JSET_CSUM_TYPE(jset), journal_nonce(jset), jset);
500 if (journal_entry_err_on(bch2_crc_cmp(csum, jset->csum), c,
501 "%s sector %llu seq %llu: journal checksum bad",
502 ca->name, sector, le64_to_cpu(jset->seq)))
503 ret = JOURNAL_ENTRY_BAD;
505 bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
506 jset->encrypted_start,
507 vstruct_end(jset) - (void *) jset->encrypted_start);
509 if (journal_entry_err_on(le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq), c,
510 "invalid journal entry: last_seq > seq")) {
511 jset->last_seq = jset->seq;
512 return JOURNAL_ENTRY_BAD;
518 struct journal_read_buf {
523 static int journal_read_buf_realloc(struct journal_read_buf *b,
528 /* the bios are sized for this many pages, max: */
529 if (new_size > JOURNAL_ENTRY_SIZE_MAX)
532 new_size = roundup_pow_of_two(new_size);
533 n = kvpmalloc(new_size, GFP_KERNEL);
537 kvpfree(b->data, b->size);
543 static int journal_read_bucket(struct bch_dev *ca,
544 struct journal_read_buf *buf,
545 struct journal_list *jlist,
548 struct bch_fs *c = ca->fs;
549 struct journal_device *ja = &ca->journal;
550 struct jset *j = NULL;
551 unsigned sectors, sectors_read = 0;
552 u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
553 end = offset + ca->mi.bucket_size;
554 bool saw_bad = false;
557 pr_debug("reading %u", bucket);
559 while (offset < end) {
563 sectors_read = min_t(unsigned,
564 end - offset, buf->size >> 9);
566 bio = bio_kmalloc(GFP_KERNEL,
569 bio_set_dev(bio, ca->disk_sb.bdev);
570 bio->bi_iter.bi_sector = offset;
571 bio_set_op_attrs(bio, REQ_OP_READ, 0);
572 bch2_bio_map(bio, buf->data, sectors_read << 9);
574 ret = submit_bio_wait(bio);
577 if (bch2_dev_io_err_on(ret, ca,
578 "journal read error: sector %llu",
580 bch2_meta_read_fault("journal"))
586 ret = jset_validate(c, ca, j, offset,
587 end - offset, sectors_read,
591 sectors = vstruct_sectors(j, c->block_bits);
593 case JOURNAL_ENTRY_REREAD:
594 if (vstruct_bytes(j) > buf->size) {
595 ret = journal_read_buf_realloc(buf,
601 case JOURNAL_ENTRY_NONE:
604 sectors = c->opts.block_size;
606 case JOURNAL_ENTRY_BAD:
609 * On checksum error we don't really trust the size
610 * field of the journal entry we read, so try reading
611 * again at next block boundary:
613 sectors = c->opts.block_size;
620 * This happens sometimes if we don't have discards on -
621 * when we've partially overwritten a bucket with new
622 * journal entries. We don't need the rest of the
625 if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
628 ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
630 mutex_lock(&jlist->lock);
631 ret = journal_entry_add(c, ca, jlist, j, ret != 0);
632 mutex_unlock(&jlist->lock);
635 case JOURNAL_ENTRY_ADD_OK:
637 case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
645 sectors_read -= sectors;
646 j = ((void *) j) + (sectors << 9);
652 static void bch2_journal_read_device(struct closure *cl)
654 struct journal_device *ja =
655 container_of(cl, struct journal_device, read);
656 struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
657 struct journal_list *jlist =
658 container_of(cl->parent, struct journal_list, cl);
659 struct journal_read_buf buf = { NULL, 0 };
660 u64 min_seq = U64_MAX;
667 ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
671 pr_debug("%u journal buckets", ja->nr);
673 for (i = 0; i < ja->nr; i++) {
674 ret = journal_read_bucket(ca, &buf, jlist, i);
679 /* Find the journal bucket with the highest sequence number: */
680 for (i = 0; i < ja->nr; i++) {
681 if (ja->bucket_seq[i] > ja->bucket_seq[ja->cur_idx])
684 min_seq = min(ja->bucket_seq[i], min_seq);
688 * If there's duplicate journal entries in multiple buckets (which
689 * definitely isn't supposed to happen, but...) - make sure to start
690 * cur_idx at the last of those buckets, so we don't deadlock trying to
693 while (ja->bucket_seq[ja->cur_idx] > min_seq &&
694 ja->bucket_seq[ja->cur_idx] >
695 ja->bucket_seq[(ja->cur_idx + 1) % ja->nr])
696 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
698 ja->sectors_free = 0;
701 * Set dirty_idx to indicate the entire journal is full and needs to be
702 * reclaimed - journal reclaim will immediately reclaim whatever isn't
703 * pinned when it first runs:
705 ja->discard_idx = ja->dirty_idx_ondisk =
706 ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
708 kvpfree(buf.data, buf.size);
709 percpu_ref_put(&ca->io_ref);
713 mutex_lock(&jlist->lock);
715 mutex_unlock(&jlist->lock);
719 int bch2_journal_read(struct bch_fs *c, struct list_head *list,
720 u64 *blacklist_seq, u64 *start_seq)
722 struct journal_list jlist;
723 struct journal_replay *i, *t;
726 size_t keys = 0, entries = 0;
727 bool degraded = false;
728 u64 seq, last_seq = 0;
731 closure_init_stack(&jlist.cl);
732 mutex_init(&jlist.lock);
736 for_each_member_device(ca, c, iter) {
737 if (!test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) &&
738 !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
741 if ((ca->mi.state == BCH_MEMBER_STATE_RW ||
742 ca->mi.state == BCH_MEMBER_STATE_RO) &&
743 percpu_ref_tryget(&ca->io_ref))
744 closure_call(&ca->journal.read,
745 bch2_journal_read_device,
752 closure_sync(&jlist.cl);
757 if (list_empty(list)) {
758 bch_info(c, "journal read done, but no entries found");
762 i = list_last_entry(list, struct journal_replay, list);
763 *start_seq = le64_to_cpu(i->j.seq) + 1;
766 * Find most recent flush entry, and ignore newer non flush entries -
767 * those entries will be blacklisted:
769 list_for_each_entry_safe_reverse(i, t, list, list) {
773 if (!JSET_NO_FLUSH(&i->j)) {
774 last_seq = le64_to_cpu(i->j.last_seq);
775 *blacklist_seq = le64_to_cpu(i->j.seq) + 1;
779 journal_replay_free(c, i);
783 fsck_err(c, "journal read done, but no entries found after dropping non-flushes");
787 /* Drop blacklisted entries and entries older than last_seq: */
788 list_for_each_entry_safe(i, t, list, list) {
792 seq = le64_to_cpu(i->j.seq);
793 if (seq < last_seq) {
794 journal_replay_free(c, i);
798 if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
799 fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
800 "found blacklisted journal entry %llu", seq);
802 journal_replay_free(c, i);
806 /* Check for missing entries: */
808 list_for_each_entry(i, list, list) {
812 BUG_ON(seq > le64_to_cpu(i->j.seq));
814 while (seq < le64_to_cpu(i->j.seq)) {
815 u64 missing_start, missing_end;
817 while (seq < le64_to_cpu(i->j.seq) &&
818 bch2_journal_seq_is_blacklisted(c, seq, false))
821 if (seq == le64_to_cpu(i->j.seq))
826 while (seq < le64_to_cpu(i->j.seq) &&
827 !bch2_journal_seq_is_blacklisted(c, seq, false))
830 missing_end = seq - 1;
831 fsck_err(c, "journal entries %llu-%llu missing! (replaying %llu-%llu)",
832 missing_start, missing_end,
833 last_seq, *blacklist_seq - 1);
839 list_for_each_entry(i, list, list) {
840 struct jset_entry *entry;
841 struct bkey_i *k, *_n;
842 struct bch_replicas_padded replicas;
848 ret = jset_validate_entries(c, &i->j, READ);
853 * If we're mounting in degraded mode - if we didn't read all
854 * the devices - this is wrong:
857 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal, i->devs);
860 (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
861 fsck_err_on(!bch2_replicas_marked(c, &replicas.e), c,
862 "superblock not marked as containing replicas %s",
863 (bch2_replicas_entry_to_text(&PBUF(buf),
864 &replicas.e), buf)))) {
865 ret = bch2_mark_replicas(c, &replicas.e);
870 for_each_jset_key(k, _n, entry, &i->j)
875 bch_info(c, "journal read done, %zu keys in %zu entries, seq %llu",
876 keys, entries, *start_seq);
878 if (*start_seq != *blacklist_seq)
879 bch_info(c, "dropped unflushed entries %llu-%llu",
880 *blacklist_seq, *start_seq - 1);
887 static void __journal_write_alloc(struct journal *j,
888 struct journal_buf *w,
889 struct dev_alloc_list *devs_sorted,
892 unsigned replicas_want)
894 struct bch_fs *c = container_of(j, struct bch_fs, journal);
895 struct journal_device *ja;
899 if (*replicas >= replicas_want)
902 for (i = 0; i < devs_sorted->nr; i++) {
903 ca = rcu_dereference(c->devs[devs_sorted->devs[i]]);
910 * Check that we can use this device, and aren't already using
913 if (!ca->mi.durability ||
914 ca->mi.state != BCH_MEMBER_STATE_RW ||
916 bch2_bkey_has_device(bkey_i_to_s_c(&w->key),
918 sectors > ja->sectors_free)
921 bch2_dev_stripe_increment(ca, &j->wp.stripe);
923 bch2_bkey_append_ptr(&w->key,
924 (struct bch_extent_ptr) {
925 .offset = bucket_to_sector(ca,
926 ja->buckets[ja->cur_idx]) +
932 ja->sectors_free -= sectors;
933 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
935 *replicas += ca->mi.durability;
937 if (*replicas >= replicas_want)
943 * journal_next_bucket - move on to the next journal bucket if possible
945 static int journal_write_alloc(struct journal *j, struct journal_buf *w,
948 struct bch_fs *c = container_of(j, struct bch_fs, journal);
949 struct journal_device *ja;
951 struct dev_alloc_list devs_sorted;
952 unsigned i, replicas = 0, replicas_want =
953 READ_ONCE(c->opts.metadata_replicas);
957 devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe,
958 &c->rw_devs[BCH_DATA_journal]);
960 __journal_write_alloc(j, w, &devs_sorted,
961 sectors, &replicas, replicas_want);
963 if (replicas >= replicas_want)
966 for (i = 0; i < devs_sorted.nr; i++) {
967 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
973 if (sectors > ja->sectors_free &&
974 sectors <= ca->mi.bucket_size &&
975 bch2_journal_dev_buckets_available(j, ja,
976 journal_space_discarded)) {
977 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
978 ja->sectors_free = ca->mi.bucket_size;
981 * ja->bucket_seq[ja->cur_idx] must always have
982 * something sensible:
984 ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
988 __journal_write_alloc(j, w, &devs_sorted,
989 sectors, &replicas, replicas_want);
993 return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
996 static void journal_write_compact(struct jset *jset)
998 struct jset_entry *i, *next, *prev = NULL;
1001 * Simple compaction, dropping empty jset_entries (from journal
1002 * reservations that weren't fully used) and merging jset_entries that
1005 * If we wanted to be really fancy here, we could sort all the keys in
1006 * the jset and drop keys that were overwritten - probably not worth it:
1008 vstruct_for_each_safe(jset, i, next) {
1009 unsigned u64s = le16_to_cpu(i->u64s);
1015 /* Can we merge with previous entry? */
1017 i->btree_id == prev->btree_id &&
1018 i->level == prev->level &&
1019 i->type == prev->type &&
1020 i->type == BCH_JSET_ENTRY_btree_keys &&
1021 le16_to_cpu(prev->u64s) + u64s <= U16_MAX) {
1022 memmove_u64s_down(vstruct_next(prev),
1025 le16_add_cpu(&prev->u64s, u64s);
1029 /* Couldn't merge, move i into new position (after prev): */
1030 prev = prev ? vstruct_next(prev) : jset->start;
1032 memmove_u64s_down(prev, i, jset_u64s(u64s));
1035 prev = prev ? vstruct_next(prev) : jset->start;
1036 jset->u64s = cpu_to_le32((u64 *) prev - jset->_data);
1039 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1041 /* we aren't holding j->lock: */
1042 unsigned new_size = READ_ONCE(j->buf_size_want);
1045 if (buf->buf_size >= new_size)
1048 new_buf = kvpmalloc(new_size, GFP_NOIO|__GFP_NOWARN);
1052 memcpy(new_buf, buf->data, buf->buf_size);
1053 kvpfree(buf->data, buf->buf_size);
1054 buf->data = new_buf;
1055 buf->buf_size = new_size;
1058 static inline struct journal_buf *journal_last_unwritten_buf(struct journal *j)
1060 return j->buf + j->reservations.unwritten_idx;
1063 static void journal_write_done(struct closure *cl)
1065 struct journal *j = container_of(cl, struct journal, io);
1066 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1067 struct journal_buf *w = journal_last_unwritten_buf(j);
1068 struct bch_devs_list devs =
1069 bch2_bkey_devs(bkey_i_to_s_c(&w->key));
1070 struct bch_replicas_padded replicas;
1071 union journal_res_state old, new;
1072 u64 seq = le64_to_cpu(w->data->seq);
1073 u64 last_seq = le64_to_cpu(w->data->last_seq);
1077 bch2_time_stats_update(j->write_time, j->write_start_time);
1080 bch_err(c, "unable to write journal to sufficient devices");
1083 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal, devs);
1084 if (bch2_mark_replicas(c, &replicas.e))
1089 bch2_fatal_error(c);
1091 spin_lock(&j->lock);
1092 if (seq >= j->pin.front)
1093 journal_seq_pin(j, seq)->devs = devs;
1095 j->seq_ondisk = seq;
1096 if (err && (!j->err_seq || seq < j->err_seq))
1100 j->flushed_seq_ondisk = seq;
1101 j->last_seq_ondisk = last_seq;
1105 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1108 * Must come before signaling write completion, for
1109 * bch2_fs_journal_stop():
1111 journal_reclaim_kick(&c->journal);
1113 /* also must come before signalling write completion: */
1114 closure_debug_destroy(cl);
1116 v = atomic64_read(&j->reservations.counter);
1119 BUG_ON(new.idx == new.unwritten_idx);
1121 new.unwritten_idx++;
1122 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
1123 old.v, new.v)) != old.v);
1125 bch2_journal_space_available(j);
1127 closure_wake_up(&w->wait);
1130 if (test_bit(JOURNAL_NEED_WRITE, &j->flags))
1131 mod_delayed_work(system_freezable_wq, &j->write_work, 0);
1132 spin_unlock(&j->lock);
1134 if (new.unwritten_idx != new.idx &&
1135 !journal_state_count(new, new.unwritten_idx))
1136 closure_call(&j->io, bch2_journal_write, system_highpri_wq, NULL);
1139 static void journal_write_endio(struct bio *bio)
1141 struct bch_dev *ca = bio->bi_private;
1142 struct journal *j = &ca->fs->journal;
1144 if (bch2_dev_io_err_on(bio->bi_status, ca, "journal write error: %s",
1145 bch2_blk_status_to_str(bio->bi_status)) ||
1146 bch2_meta_write_fault("journal")) {
1147 struct journal_buf *w = journal_last_unwritten_buf(j);
1148 unsigned long flags;
1150 spin_lock_irqsave(&j->err_lock, flags);
1151 bch2_bkey_drop_device(bkey_i_to_s(&w->key), ca->dev_idx);
1152 spin_unlock_irqrestore(&j->err_lock, flags);
1155 closure_put(&j->io);
1156 percpu_ref_put(&ca->io_ref);
1159 void bch2_journal_write(struct closure *cl)
1161 struct journal *j = container_of(cl, struct journal, io);
1162 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1164 struct journal_buf *w = journal_last_unwritten_buf(j);
1165 struct jset_entry *start, *end;
1168 struct bch_extent_ptr *ptr;
1169 bool validate_before_checksum = false;
1170 unsigned i, sectors, bytes, u64s;
1173 BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
1175 journal_buf_realloc(j, w);
1178 j->write_start_time = local_clock();
1180 spin_lock(&j->lock);
1181 if (c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush) &&
1183 (jiffies - j->last_flush_write) < msecs_to_jiffies(j->write_delay_ms) &&
1184 test_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags)) {
1186 SET_JSET_NO_FLUSH(jset, true);
1187 jset->last_seq = cpu_to_le64(j->last_seq_ondisk);
1189 j->nr_noflush_writes++;
1191 j->last_flush_write = jiffies;
1192 j->nr_flush_writes++;
1194 spin_unlock(&j->lock);
1197 * New btree roots are set by journalling them; when the journal entry
1198 * gets written we have to propagate them to c->btree_roots
1200 * But, every journal entry we write has to contain all the btree roots
1201 * (at least for now); so after we copy btree roots to c->btree_roots we
1202 * have to get any missing btree roots and add them to this journal
1206 bch2_journal_entries_to_btree_roots(c, jset);
1208 start = end = vstruct_last(jset);
1210 end = bch2_btree_roots_to_journal_entries(c, jset->start, end);
1212 end = bch2_journal_super_entries_add_common(c, end,
1213 le64_to_cpu(jset->seq));
1214 u64s = (u64 *) end - (u64 *) start;
1215 BUG_ON(u64s > j->entry_u64s_reserved);
1217 le32_add_cpu(&jset->u64s, u64s);
1218 BUG_ON(vstruct_sectors(jset, c->block_bits) > w->sectors);
1220 journal_write_compact(jset);
1222 jset->read_clock = cpu_to_le16(c->bucket_clock[READ].hand);
1223 jset->write_clock = cpu_to_le16(c->bucket_clock[WRITE].hand);
1224 jset->magic = cpu_to_le64(jset_magic(c));
1226 jset->version = c->sb.version < bcachefs_metadata_version_new_versioning
1227 ? cpu_to_le32(BCH_JSET_VERSION_OLD)
1228 : cpu_to_le32(c->sb.version);
1230 SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1231 SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1233 if (journal_entry_empty(jset))
1234 j->last_empty_seq = le64_to_cpu(jset->seq);
1236 if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1237 validate_before_checksum = true;
1239 if (le32_to_cpu(jset->version) < bcachefs_metadata_version_max)
1240 validate_before_checksum = true;
1242 if (validate_before_checksum &&
1243 jset_validate_entries(c, jset, WRITE))
1246 bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1247 jset->encrypted_start,
1248 vstruct_end(jset) - (void *) jset->encrypted_start);
1250 jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1251 journal_nonce(jset), jset);
1253 if (!validate_before_checksum &&
1254 jset_validate_entries(c, jset, WRITE))
1257 sectors = vstruct_sectors(jset, c->block_bits);
1258 BUG_ON(sectors > w->sectors);
1260 bytes = vstruct_bytes(jset);
1261 memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1264 spin_lock(&j->lock);
1265 ret = journal_write_alloc(j, w, sectors);
1267 if (ret && j->can_discard) {
1268 spin_unlock(&j->lock);
1269 bch2_journal_do_discards(j);
1274 * write is allocated, no longer need to account for it in
1275 * bch2_journal_space_available():
1280 * journal entry has been compacted and allocated, recalculate space
1283 bch2_journal_space_available(j);
1284 spin_unlock(&j->lock);
1287 bch_err(c, "Unable to allocate journal write");
1288 bch2_fatal_error(c);
1289 continue_at(cl, journal_write_done, system_highpri_wq);
1294 * XXX: we really should just disable the entire journal in nochanges
1297 if (c->opts.nochanges)
1300 extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1301 ca = bch_dev_bkey_exists(c, ptr->dev);
1302 if (!percpu_ref_tryget(&ca->io_ref)) {
1304 bch_err(c, "missing device for journal write\n");
1308 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1311 bio = ca->journal.bio;
1313 bio_set_dev(bio, ca->disk_sb.bdev);
1314 bio->bi_iter.bi_sector = ptr->offset;
1315 bio->bi_end_io = journal_write_endio;
1316 bio->bi_private = ca;
1317 bio->bi_opf = REQ_OP_WRITE|REQ_SYNC|REQ_META;
1318 if (!JSET_NO_FLUSH(jset))
1319 bio->bi_opf |= REQ_PREFLUSH|REQ_FUA;
1320 bch2_bio_map(bio, jset, sectors << 9);
1322 trace_journal_write(bio);
1323 closure_bio_submit(bio, cl);
1325 ca->journal.bucket_seq[ca->journal.cur_idx] = le64_to_cpu(jset->seq);
1328 if (!JSET_NO_FLUSH(jset)) {
1329 for_each_rw_member(ca, c, i)
1330 if (journal_flushes_device(ca) &&
1331 !bch2_bkey_has_device(bkey_i_to_s_c(&w->key), i)) {
1332 percpu_ref_get(&ca->io_ref);
1334 bio = ca->journal.bio;
1336 bio_set_dev(bio, ca->disk_sb.bdev);
1337 bio->bi_opf = REQ_OP_FLUSH;
1338 bio->bi_end_io = journal_write_endio;
1339 bio->bi_private = ca;
1340 closure_bio_submit(bio, cl);
1344 bch2_bucket_seq_cleanup(c);
1346 continue_at(cl, journal_write_done, system_highpri_wq);
1349 bch2_inconsistent_error(c);
1350 continue_at(cl, journal_write_done, system_highpri_wq);