1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
6 #include "btree_update.h"
7 #include "btree_update_interior.h"
13 #include "fs-common.h"
15 #include "journal_io.h"
16 #include "journal_reclaim.h"
17 #include "journal_seq_blacklist.h"
23 #include <linux/sort.h>
24 #include <linux/stat.h>
26 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
28 /* for -o reconstruct_alloc: */
29 static void drop_alloc_keys(struct journal_keys *keys)
33 for (src = 0, dst = 0; src < keys->nr; src++)
34 if (keys->d[src].btree_id != BTREE_ID_ALLOC)
35 keys->d[dst++] = keys->d[src];
40 /* iterate over keys read from the journal: */
42 static struct journal_key *journal_key_search(struct journal_keys *journal_keys,
43 enum btree_id id, unsigned level,
46 size_t l = 0, r = journal_keys->nr, m;
49 m = l + ((r - l) >> 1);
50 if ((cmp_int(id, journal_keys->d[m].btree_id) ?:
51 cmp_int(level, journal_keys->d[m].level) ?:
52 bkey_cmp(pos, journal_keys->d[m].k->k.p)) > 0)
58 BUG_ON(l < journal_keys->nr &&
59 (cmp_int(id, journal_keys->d[l].btree_id) ?:
60 cmp_int(level, journal_keys->d[l].level) ?:
61 bkey_cmp(pos, journal_keys->d[l].k->k.p)) > 0);
64 (cmp_int(id, journal_keys->d[l - 1].btree_id) ?:
65 cmp_int(level, journal_keys->d[l - 1].level) ?:
66 bkey_cmp(pos, journal_keys->d[l - 1].k->k.p)) <= 0);
68 return l < journal_keys->nr ? journal_keys->d + l : NULL;
71 static struct bkey_i *bch2_journal_iter_peek(struct journal_iter *iter)
74 iter->k < iter->keys->d + iter->keys->nr &&
75 iter->k->btree_id == iter->btree_id &&
76 iter->k->level == iter->level)
83 static void bch2_journal_iter_advance(struct journal_iter *iter)
89 static void bch2_journal_iter_init(struct journal_iter *iter,
90 struct journal_keys *journal_keys,
91 enum btree_id id, unsigned level,
96 iter->keys = journal_keys;
97 iter->k = journal_key_search(journal_keys, id, level, pos);
100 static struct bkey_s_c bch2_journal_iter_peek_btree(struct btree_and_journal_iter *iter)
103 ? bch2_btree_iter_peek(iter->btree)
104 : bch2_btree_node_iter_peek_unpack(&iter->node_iter,
105 iter->b, &iter->unpacked);
108 static void bch2_journal_iter_advance_btree(struct btree_and_journal_iter *iter)
111 bch2_btree_iter_next(iter->btree);
113 bch2_btree_node_iter_advance(&iter->node_iter, iter->b);
116 void bch2_btree_and_journal_iter_advance(struct btree_and_journal_iter *iter)
118 switch (iter->last) {
122 bch2_journal_iter_advance_btree(iter);
125 bch2_journal_iter_advance(&iter->journal);
132 struct bkey_s_c bch2_btree_and_journal_iter_peek(struct btree_and_journal_iter *iter)
137 struct bkey_s_c btree_k =
138 bch2_journal_iter_peek_btree(iter);
139 struct bkey_s_c journal_k =
140 bkey_i_to_s_c(bch2_journal_iter_peek(&iter->journal));
142 if (btree_k.k && journal_k.k) {
143 int cmp = bkey_cmp(btree_k.k->p, journal_k.k->p);
146 bch2_journal_iter_advance_btree(iter);
148 iter->last = cmp < 0 ? btree : journal;
149 } else if (btree_k.k) {
151 } else if (journal_k.k) {
152 iter->last = journal;
155 return bkey_s_c_null;
158 ret = iter->last == journal ? journal_k : btree_k;
161 bkey_cmp(ret.k->p, iter->b->data->max_key) > 0) {
162 iter->journal.k = NULL;
164 return bkey_s_c_null;
167 if (!bkey_deleted(ret.k))
170 bch2_btree_and_journal_iter_advance(iter);
176 struct bkey_s_c bch2_btree_and_journal_iter_next(struct btree_and_journal_iter *iter)
178 bch2_btree_and_journal_iter_advance(iter);
180 return bch2_btree_and_journal_iter_peek(iter);
183 void bch2_btree_and_journal_iter_init(struct btree_and_journal_iter *iter,
184 struct btree_trans *trans,
185 struct journal_keys *journal_keys,
186 enum btree_id id, struct bpos pos)
188 memset(iter, 0, sizeof(*iter));
190 iter->btree = bch2_trans_get_iter(trans, id, pos, BTREE_ITER_PREFETCH);
191 bch2_journal_iter_init(&iter->journal, journal_keys, id, 0, pos);
194 void bch2_btree_and_journal_iter_init_node_iter(struct btree_and_journal_iter *iter,
195 struct journal_keys *journal_keys,
198 memset(iter, 0, sizeof(*iter));
201 bch2_btree_node_iter_init_from_start(&iter->node_iter, iter->b);
202 bch2_journal_iter_init(&iter->journal, journal_keys,
203 b->c.btree_id, b->c.level, b->data->min_key);
206 /* Walk btree, overlaying keys from the journal: */
208 static int bch2_btree_and_journal_walk_recurse(struct bch_fs *c, struct btree *b,
209 struct journal_keys *journal_keys,
210 enum btree_id btree_id,
211 btree_walk_node_fn node_fn,
212 btree_walk_key_fn key_fn)
214 struct btree_and_journal_iter iter;
218 bch2_btree_and_journal_iter_init_node_iter(&iter, journal_keys, b);
220 while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
221 ret = key_fn(c, btree_id, b->c.level, k);
229 bkey_reassemble(&tmp.k, k);
230 k = bkey_i_to_s_c(&tmp.k);
232 bch2_btree_and_journal_iter_advance(&iter);
234 if (b->c.level > 0) {
235 child = bch2_btree_node_get_noiter(c, &tmp.k,
236 b->c.btree_id, b->c.level - 1);
237 ret = PTR_ERR_OR_ZERO(child);
241 ret = (node_fn ? node_fn(c, b) : 0) ?:
242 bch2_btree_and_journal_walk_recurse(c, child,
243 journal_keys, btree_id, node_fn, key_fn);
244 six_unlock_read(&child->c.lock);
250 bch2_btree_and_journal_iter_advance(&iter);
257 int bch2_btree_and_journal_walk(struct bch_fs *c, struct journal_keys *journal_keys,
258 enum btree_id btree_id,
259 btree_walk_node_fn node_fn,
260 btree_walk_key_fn key_fn)
262 struct btree *b = c->btree_roots[btree_id].b;
265 if (btree_node_fake(b))
268 six_lock_read(&b->c.lock, NULL, NULL);
269 ret = (node_fn ? node_fn(c, b) : 0) ?:
270 bch2_btree_and_journal_walk_recurse(c, b, journal_keys, btree_id,
272 key_fn(c, btree_id, b->c.level + 1, bkey_i_to_s_c(&b->key));
273 six_unlock_read(&b->c.lock);
278 /* sort and dedup all keys in the journal: */
280 void bch2_journal_entries_free(struct list_head *list)
283 while (!list_empty(list)) {
284 struct journal_replay *i =
285 list_first_entry(list, struct journal_replay, list);
287 kvpfree(i, offsetof(struct journal_replay, j) +
288 vstruct_bytes(&i->j));
293 * When keys compare equal, oldest compares first:
295 static int journal_sort_key_cmp(const void *_l, const void *_r)
297 const struct journal_key *l = _l;
298 const struct journal_key *r = _r;
300 return cmp_int(l->btree_id, r->btree_id) ?:
301 cmp_int(l->level, r->level) ?:
302 bkey_cmp(l->k->k.p, r->k->k.p) ?:
303 cmp_int(l->journal_seq, r->journal_seq) ?:
304 cmp_int(l->journal_offset, r->journal_offset);
307 void bch2_journal_keys_free(struct journal_keys *keys)
314 static struct journal_keys journal_keys_sort(struct list_head *journal_entries)
316 struct journal_replay *p;
317 struct jset_entry *entry;
318 struct bkey_i *k, *_n;
319 struct journal_keys keys = { NULL };
320 struct journal_key *src, *dst;
323 if (list_empty(journal_entries))
326 keys.journal_seq_base =
327 le64_to_cpu(list_last_entry(journal_entries,
328 struct journal_replay, list)->j.last_seq);
330 list_for_each_entry(p, journal_entries, list) {
331 if (le64_to_cpu(p->j.seq) < keys.journal_seq_base)
334 for_each_jset_key(k, _n, entry, &p->j)
339 keys.d = kvmalloc(sizeof(keys.d[0]) * nr_keys, GFP_KERNEL);
343 list_for_each_entry(p, journal_entries, list) {
344 if (le64_to_cpu(p->j.seq) < keys.journal_seq_base)
347 for_each_jset_key(k, _n, entry, &p->j)
348 keys.d[keys.nr++] = (struct journal_key) {
349 .btree_id = entry->btree_id,
350 .level = entry->level,
352 .journal_seq = le64_to_cpu(p->j.seq) -
353 keys.journal_seq_base,
354 .journal_offset = k->_data - p->j._data,
358 sort(keys.d, keys.nr, sizeof(keys.d[0]), journal_sort_key_cmp, NULL);
361 while (src < keys.d + keys.nr) {
362 while (src + 1 < keys.d + keys.nr &&
363 src[0].btree_id == src[1].btree_id &&
364 src[0].level == src[1].level &&
365 !bkey_cmp(src[0].k->k.p, src[1].k->k.p))
371 keys.nr = dst - keys.d;
376 /* journal replay: */
378 static void replay_now_at(struct journal *j, u64 seq)
380 BUG_ON(seq < j->replay_journal_seq);
381 BUG_ON(seq > j->replay_journal_seq_end);
383 while (j->replay_journal_seq < seq)
384 bch2_journal_pin_put(j, j->replay_journal_seq++);
387 static int bch2_extent_replay_key(struct bch_fs *c, enum btree_id btree_id,
390 struct btree_trans trans;
391 struct btree_iter *iter, *split_iter;
393 * We might cause compressed extents to be split, so we need to pass in
394 * a disk_reservation:
396 struct disk_reservation disk_res =
397 bch2_disk_reservation_init(c, 0);
398 struct bkey_i *split;
399 struct bpos atomic_end;
401 * Some extents aren't equivalent - w.r.t. what the triggers do
402 * - if they're split:
404 bool remark_if_split = bch2_bkey_sectors_compressed(bkey_i_to_s_c(k)) ||
405 k->k.type == KEY_TYPE_reflink_p;
409 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
411 bch2_trans_begin(&trans);
413 iter = bch2_trans_get_iter(&trans, btree_id,
414 bkey_start_pos(&k->k),
418 ret = bch2_btree_iter_traverse(iter);
422 atomic_end = bpos_min(k->k.p, iter->l[0].b->key.k.p);
424 split = bch2_trans_kmalloc(&trans, bkey_bytes(&k->k));
425 ret = PTR_ERR_OR_ZERO(split);
431 bkey_cmp(atomic_end, k->k.p) < 0) {
432 ret = bch2_disk_reservation_add(c, &disk_res,
434 bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(k)),
435 BCH_DISK_RESERVATION_NOFAIL);
442 bch2_cut_front(iter->pos, split);
443 bch2_cut_back(atomic_end, split);
445 split_iter = bch2_trans_copy_iter(&trans, iter);
448 * It's important that we don't go through the
449 * extent_handle_overwrites() and extent_update_to_keys() path
450 * here: journal replay is supposed to treat extents like
453 __bch2_btree_iter_set_pos(split_iter, split->k.p, false);
454 bch2_trans_update(&trans, split_iter, split,
455 BTREE_TRIGGER_NORUN);
456 bch2_trans_iter_put(&trans, split_iter);
458 bch2_btree_iter_set_pos(iter, split->k.p);
461 ret = bch2_trans_mark_key(&trans, bkey_i_to_s_c(split),
463 BTREE_TRIGGER_INSERT);
467 } while (bkey_cmp(iter->pos, k->k.p) < 0);
470 ret = bch2_trans_mark_key(&trans, bkey_i_to_s_c(k),
471 0, -((s64) k->k.size),
472 BTREE_TRIGGER_OVERWRITE);
477 ret = bch2_trans_commit(&trans, &disk_res, NULL,
479 BTREE_INSERT_LAZY_RW|
480 BTREE_INSERT_JOURNAL_REPLAY);
482 bch2_trans_iter_put(&trans, iter);
487 bch2_disk_reservation_put(c, &disk_res);
489 return bch2_trans_exit(&trans) ?: ret;
492 static int __bch2_journal_replay_key(struct btree_trans *trans,
493 enum btree_id id, unsigned level,
496 struct btree_iter *iter;
499 iter = bch2_trans_get_node_iter(trans, id, k->k.p,
500 BTREE_MAX_DEPTH, level,
504 * iter->flags & BTREE_ITER_IS_EXTENTS triggers the update path to run
505 * extent_handle_overwrites() and extent_update_to_keys() - but we don't
506 * want that here, journal replay is supposed to treat extents like
509 __bch2_btree_iter_set_pos(iter, k->k.p, false);
511 ret = bch2_btree_iter_traverse(iter) ?:
512 bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
513 bch2_trans_iter_put(trans, iter);
517 static int bch2_journal_replay_key(struct bch_fs *c, enum btree_id id,
518 unsigned level, struct bkey_i *k)
520 return bch2_trans_do(c, NULL, NULL,
522 BTREE_INSERT_LAZY_RW|
523 BTREE_INSERT_JOURNAL_REPLAY,
524 __bch2_journal_replay_key(&trans, id, level, k));
527 static int __bch2_alloc_replay_key(struct btree_trans *trans, struct bkey_i *k)
529 struct btree_iter *iter;
532 iter = bch2_trans_get_iter(trans, BTREE_ID_ALLOC, k->k.p,
534 BTREE_ITER_CACHED_NOFILL|
536 ret = bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
537 bch2_trans_iter_put(trans, iter);
541 static int bch2_alloc_replay_key(struct bch_fs *c, struct bkey_i *k)
543 return bch2_trans_do(c, NULL, NULL,
545 BTREE_INSERT_USE_RESERVE|
546 BTREE_INSERT_LAZY_RW|
547 BTREE_INSERT_JOURNAL_REPLAY,
548 __bch2_alloc_replay_key(&trans, k));
551 static int journal_sort_seq_cmp(const void *_l, const void *_r)
553 const struct journal_key *l = _l;
554 const struct journal_key *r = _r;
556 return cmp_int(r->level, l->level) ?:
557 cmp_int(l->journal_seq, r->journal_seq) ?:
558 cmp_int(l->btree_id, r->btree_id) ?:
559 bkey_cmp(l->k->k.p, r->k->k.p);
562 static int bch2_journal_replay(struct bch_fs *c,
563 struct journal_keys keys)
565 struct journal *j = &c->journal;
566 struct journal_key *i;
570 sort(keys.d, keys.nr, sizeof(keys.d[0]), journal_sort_seq_cmp, NULL);
573 replay_now_at(j, keys.journal_seq_base);
575 seq = j->replay_journal_seq;
578 * First replay updates to the alloc btree - these will only update the
581 for_each_journal_key(keys, i) {
584 if (!i->level && i->btree_id == BTREE_ID_ALLOC) {
585 j->replay_journal_seq = keys.journal_seq_base + i->journal_seq;
586 ret = bch2_alloc_replay_key(c, i->k);
593 * Next replay updates to interior btree nodes:
595 for_each_journal_key(keys, i) {
599 j->replay_journal_seq = keys.journal_seq_base + i->journal_seq;
600 ret = bch2_journal_replay_key(c, i->btree_id, i->level, i->k);
607 * Now that the btree is in a consistent state, we can start journal
608 * reclaim (which will be flushing entries from the btree key cache back
611 set_bit(BCH_FS_BTREE_INTERIOR_REPLAY_DONE, &c->flags);
612 set_bit(JOURNAL_RECLAIM_STARTED, &j->flags);
613 journal_reclaim_kick(j);
615 j->replay_journal_seq = seq;
618 * Now replay leaf node updates:
620 for_each_journal_key(keys, i) {
623 if (i->level || i->btree_id == BTREE_ID_ALLOC)
626 replay_now_at(j, keys.journal_seq_base + i->journal_seq);
629 ? bch2_extent_replay_key(c, i->btree_id, i->k)
630 : bch2_journal_replay_key(c, i->btree_id, i->level, i->k);
635 replay_now_at(j, j->replay_journal_seq_end);
636 j->replay_journal_seq = 0;
638 bch2_journal_set_replay_done(j);
639 bch2_journal_flush_all_pins(j);
640 return bch2_journal_error(j);
642 bch_err(c, "journal replay: error %d while replaying key", ret);
646 static bool journal_empty(struct list_head *journal)
648 return list_empty(journal) ||
649 journal_entry_empty(&list_last_entry(journal,
650 struct journal_replay, list)->j);
654 verify_journal_entries_not_blacklisted_or_missing(struct bch_fs *c,
655 struct list_head *journal)
657 struct journal_replay *i =
658 list_last_entry(journal, struct journal_replay, list);
659 u64 start_seq = le64_to_cpu(i->j.last_seq);
660 u64 end_seq = le64_to_cpu(i->j.seq);
664 list_for_each_entry(i, journal, list) {
665 if (le64_to_cpu(i->j.seq) < start_seq)
668 fsck_err_on(seq != le64_to_cpu(i->j.seq), c,
669 "journal entries %llu-%llu missing! (replaying %llu-%llu)",
670 seq, le64_to_cpu(i->j.seq) - 1,
673 seq = le64_to_cpu(i->j.seq);
675 fsck_err_on(bch2_journal_seq_is_blacklisted(c, seq, false), c,
676 "found blacklisted journal entry %llu", seq);
680 } while (bch2_journal_seq_is_blacklisted(c, seq, false));
686 /* journal replay early: */
688 static int journal_replay_entry_early(struct bch_fs *c,
689 struct jset_entry *entry)
693 switch (entry->type) {
694 case BCH_JSET_ENTRY_btree_root: {
695 struct btree_root *r;
697 if (entry->btree_id >= BTREE_ID_NR) {
698 bch_err(c, "filesystem has unknown btree type %u",
703 r = &c->btree_roots[entry->btree_id];
706 r->level = entry->level;
707 bkey_copy(&r->key, &entry->start[0]);
715 case BCH_JSET_ENTRY_usage: {
716 struct jset_entry_usage *u =
717 container_of(entry, struct jset_entry_usage, entry);
719 switch (entry->btree_id) {
720 case FS_USAGE_RESERVED:
721 if (entry->level < BCH_REPLICAS_MAX)
722 c->usage_base->persistent_reserved[entry->level] =
725 case FS_USAGE_INODES:
726 c->usage_base->nr_inodes = le64_to_cpu(u->v);
728 case FS_USAGE_KEY_VERSION:
729 atomic64_set(&c->key_version,
736 case BCH_JSET_ENTRY_data_usage: {
737 struct jset_entry_data_usage *u =
738 container_of(entry, struct jset_entry_data_usage, entry);
739 ret = bch2_replicas_set_usage(c, &u->r,
743 case BCH_JSET_ENTRY_blacklist: {
744 struct jset_entry_blacklist *bl_entry =
745 container_of(entry, struct jset_entry_blacklist, entry);
747 ret = bch2_journal_seq_blacklist_add(c,
748 le64_to_cpu(bl_entry->seq),
749 le64_to_cpu(bl_entry->seq) + 1);
752 case BCH_JSET_ENTRY_blacklist_v2: {
753 struct jset_entry_blacklist_v2 *bl_entry =
754 container_of(entry, struct jset_entry_blacklist_v2, entry);
756 ret = bch2_journal_seq_blacklist_add(c,
757 le64_to_cpu(bl_entry->start),
758 le64_to_cpu(bl_entry->end) + 1);
766 static int journal_replay_early(struct bch_fs *c,
767 struct bch_sb_field_clean *clean,
768 struct list_head *journal)
770 struct jset_entry *entry;
774 c->bucket_clock[READ].hand = le16_to_cpu(clean->read_clock);
775 c->bucket_clock[WRITE].hand = le16_to_cpu(clean->write_clock);
777 for (entry = clean->start;
778 entry != vstruct_end(&clean->field);
779 entry = vstruct_next(entry)) {
780 ret = journal_replay_entry_early(c, entry);
785 struct journal_replay *i =
786 list_last_entry(journal, struct journal_replay, list);
788 c->bucket_clock[READ].hand = le16_to_cpu(i->j.read_clock);
789 c->bucket_clock[WRITE].hand = le16_to_cpu(i->j.write_clock);
791 list_for_each_entry(i, journal, list)
792 vstruct_for_each(&i->j, entry) {
793 ret = journal_replay_entry_early(c, entry);
799 bch2_fs_usage_initialize(c);
804 /* sb clean section: */
806 static struct bkey_i *btree_root_find(struct bch_fs *c,
807 struct bch_sb_field_clean *clean,
809 enum btree_id id, unsigned *level)
812 struct jset_entry *entry, *start, *end;
815 start = clean->start;
816 end = vstruct_end(&clean->field);
819 end = vstruct_last(j);
822 for (entry = start; entry < end; entry = vstruct_next(entry))
823 if (entry->type == BCH_JSET_ENTRY_btree_root &&
824 entry->btree_id == id)
830 return ERR_PTR(-EINVAL);
833 *level = entry->level;
837 static int verify_superblock_clean(struct bch_fs *c,
838 struct bch_sb_field_clean **cleanp,
842 struct bch_sb_field_clean *clean = *cleanp;
845 if (!c->sb.clean || !j)
848 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
849 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
850 le64_to_cpu(clean->journal_seq),
851 le64_to_cpu(j->seq))) {
857 mustfix_fsck_err_on(j->read_clock != clean->read_clock, c,
858 "superblock read clock %u doesn't match journal %u after clean shutdown",
859 clean->read_clock, j->read_clock);
860 mustfix_fsck_err_on(j->write_clock != clean->write_clock, c,
861 "superblock write clock %u doesn't match journal %u after clean shutdown",
862 clean->write_clock, j->write_clock);
864 for (i = 0; i < BTREE_ID_NR; i++) {
865 char buf1[200], buf2[200];
866 struct bkey_i *k1, *k2;
867 unsigned l1 = 0, l2 = 0;
869 k1 = btree_root_find(c, clean, NULL, i, &l1);
870 k2 = btree_root_find(c, NULL, j, i, &l2);
875 mustfix_fsck_err_on(!k1 || !k2 ||
878 k1->k.u64s != k2->k.u64s ||
879 memcmp(k1, k2, bkey_bytes(k1)) ||
881 "superblock btree root %u doesn't match journal after clean shutdown\n"
883 "journal: l=%u %s\n", i,
884 l1, (bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(k1)), buf1),
885 l2, (bch2_bkey_val_to_text(&PBUF(buf2), c, bkey_i_to_s_c(k2)), buf2));
891 static struct bch_sb_field_clean *read_superblock_clean(struct bch_fs *c)
893 struct bch_sb_field_clean *clean, *sb_clean;
896 mutex_lock(&c->sb_lock);
897 sb_clean = bch2_sb_get_clean(c->disk_sb.sb);
899 if (fsck_err_on(!sb_clean, c,
900 "superblock marked clean but clean section not present")) {
901 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
903 mutex_unlock(&c->sb_lock);
907 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
910 mutex_unlock(&c->sb_lock);
911 return ERR_PTR(-ENOMEM);
914 if (le16_to_cpu(c->disk_sb.sb->version) <
915 bcachefs_metadata_version_bkey_renumber)
916 bch2_sb_clean_renumber(clean, READ);
918 mutex_unlock(&c->sb_lock);
922 mutex_unlock(&c->sb_lock);
926 static int read_btree_roots(struct bch_fs *c)
931 for (i = 0; i < BTREE_ID_NR; i++) {
932 struct btree_root *r = &c->btree_roots[i];
937 if (i == BTREE_ID_ALLOC &&
938 c->opts.reconstruct_alloc) {
939 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
944 __fsck_err(c, i == BTREE_ID_ALLOC
945 ? FSCK_CAN_IGNORE : 0,
946 "invalid btree root %s",
948 if (i == BTREE_ID_ALLOC)
949 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
952 ret = bch2_btree_root_read(c, i, &r->key, r->level);
954 __fsck_err(c, i == BTREE_ID_ALLOC
955 ? FSCK_CAN_IGNORE : 0,
956 "error reading btree root %s",
958 if (i == BTREE_ID_ALLOC)
959 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
963 for (i = 0; i < BTREE_ID_NR; i++)
964 if (!c->btree_roots[i].b)
965 bch2_btree_root_alloc(c, i);
970 int bch2_fs_recovery(struct bch_fs *c)
972 const char *err = "cannot allocate memory";
973 struct bch_sb_field_clean *clean = NULL;
975 bool write_sb = false, need_write_alloc = false;
979 clean = read_superblock_clean(c);
980 ret = PTR_ERR_OR_ZERO(clean);
985 bch_info(c, "recovering from clean shutdown, journal seq %llu",
986 le64_to_cpu(clean->journal_seq));
988 if (!c->replicas.entries ||
989 c->opts.rebuild_replicas) {
990 bch_info(c, "building replicas info");
991 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
994 if (!c->sb.clean || c->opts.fsck || c->opts.keep_journal) {
997 ret = bch2_journal_read(c, &c->journal_entries);
1001 if (mustfix_fsck_err_on(c->sb.clean && !journal_empty(&c->journal_entries), c,
1002 "filesystem marked clean but journal not empty")) {
1003 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
1004 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1005 c->sb.clean = false;
1008 if (!c->sb.clean && list_empty(&c->journal_entries)) {
1009 bch_err(c, "no journal entries found");
1010 ret = BCH_FSCK_REPAIR_IMPOSSIBLE;
1014 c->journal_keys = journal_keys_sort(&c->journal_entries);
1015 if (!c->journal_keys.d) {
1020 j = &list_last_entry(&c->journal_entries,
1021 struct journal_replay, list)->j;
1023 ret = verify_superblock_clean(c, &clean, j);
1027 journal_seq = le64_to_cpu(j->seq) + 1;
1029 journal_seq = le64_to_cpu(clean->journal_seq) + 1;
1033 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
1034 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
1039 if (c->opts.reconstruct_alloc) {
1040 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
1041 drop_alloc_keys(&c->journal_keys);
1044 ret = journal_replay_early(c, clean, &c->journal_entries);
1049 ret = bch2_journal_seq_blacklist_add(c,
1053 bch_err(c, "error creating new journal seq blacklist entry");
1060 * The superblock needs to be written before we do any btree
1061 * node writes: it will be in the read_write() path
1065 ret = bch2_blacklist_table_initialize(c);
1067 if (!list_empty(&c->journal_entries)) {
1068 ret = verify_journal_entries_not_blacklisted_or_missing(c,
1069 &c->journal_entries);
1074 ret = bch2_fs_journal_start(&c->journal, journal_seq,
1075 &c->journal_entries);
1079 ret = read_btree_roots(c);
1083 bch_verbose(c, "starting alloc read");
1084 err = "error reading allocation information";
1085 ret = bch2_alloc_read(c, &c->journal_keys);
1088 bch_verbose(c, "alloc read done");
1090 bch_verbose(c, "starting stripes_read");
1091 err = "error reading stripes";
1092 ret = bch2_stripes_read(c, &c->journal_keys);
1095 bch_verbose(c, "stripes_read done");
1097 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
1099 if ((c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) &&
1100 !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_METADATA))) {
1102 * interior btree node updates aren't consistent with the
1103 * journal; after an unclean shutdown we have to walk all
1104 * pointers to metadata:
1106 bch_info(c, "starting metadata mark and sweep");
1107 err = "error in mark and sweep";
1108 ret = bch2_gc(c, &c->journal_keys, true, true);
1112 need_write_alloc = true;
1113 bch_verbose(c, "mark and sweep done");
1117 !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) ||
1118 test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags)) {
1119 bch_info(c, "starting mark and sweep");
1120 err = "error in mark and sweep";
1121 ret = bch2_gc(c, &c->journal_keys, true, false);
1125 need_write_alloc = true;
1126 bch_verbose(c, "mark and sweep done");
1129 clear_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
1130 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
1133 * Skip past versions that might have possibly been used (as nonces),
1134 * but hadn't had their pointers written:
1136 if (c->sb.encryption_type && !c->sb.clean)
1137 atomic64_add(1 << 16, &c->key_version);
1139 if (c->opts.norecovery)
1142 bch_verbose(c, "starting journal replay");
1143 err = "journal replay failed";
1144 ret = bch2_journal_replay(c, c->journal_keys);
1147 bch_verbose(c, "journal replay done");
1149 if (need_write_alloc && !c->opts.nochanges) {
1151 * note that even when filesystem was clean there might be work
1152 * to do here, if we ran gc (because of fsck) which recalculated
1155 bch_verbose(c, "writing allocation info");
1156 err = "error writing out alloc info";
1157 ret = bch2_stripes_write(c, BTREE_INSERT_LAZY_RW) ?:
1158 bch2_alloc_write(c, BTREE_INSERT_LAZY_RW);
1160 bch_err(c, "error writing alloc info");
1163 bch_verbose(c, "alloc write done");
1165 set_bit(BCH_FS_ALLOC_WRITTEN, &c->flags);
1169 if (!(c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1170 bch_info(c, "checking inode link counts");
1171 err = "error in recovery";
1172 ret = bch2_fsck_inode_nlink(c);
1175 bch_verbose(c, "check inodes done");
1178 bch_verbose(c, "checking for deleted inodes");
1179 err = "error in recovery";
1180 ret = bch2_fsck_walk_inodes_only(c);
1183 bch_verbose(c, "check inodes done");
1188 bch_info(c, "starting fsck");
1189 err = "error in fsck";
1190 ret = bch2_fsck_full(c);
1193 bch_verbose(c, "fsck done");
1196 if (enabled_qtypes(c)) {
1197 bch_verbose(c, "reading quotas");
1198 ret = bch2_fs_quota_read(c);
1201 bch_verbose(c, "quotas done");
1204 mutex_lock(&c->sb_lock);
1205 if (c->opts.version_upgrade) {
1206 if (c->sb.version < bcachefs_metadata_version_new_versioning)
1207 c->disk_sb.sb->version_min =
1208 le16_to_cpu(bcachefs_metadata_version_min);
1209 c->disk_sb.sb->version = le16_to_cpu(bcachefs_metadata_version_current);
1210 c->disk_sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
1214 if (!test_bit(BCH_FS_ERROR, &c->flags)) {
1215 c->disk_sb.sb->compat[0] |= 1ULL << BCH_COMPAT_FEAT_ALLOC_INFO;
1220 !test_bit(BCH_FS_ERROR, &c->flags)) {
1221 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
1222 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
1227 bch2_write_super(c);
1228 mutex_unlock(&c->sb_lock);
1230 if (c->journal_seq_blacklist_table &&
1231 c->journal_seq_blacklist_table->nr > 128)
1232 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
1237 set_bit(BCH_FS_FSCK_DONE, &c->flags);
1238 bch2_flush_fsck_errs(c);
1240 if (!c->opts.keep_journal) {
1241 bch2_journal_keys_free(&c->journal_keys);
1242 bch2_journal_entries_free(&c->journal_entries);
1246 bch_err(c, "Error in recovery: %s (%i)", err, ret);
1248 bch_verbose(c, "ret %i", ret);
1252 int bch2_fs_initialize(struct bch_fs *c)
1254 struct bch_inode_unpacked root_inode, lostfound_inode;
1255 struct bkey_inode_buf packed_inode;
1256 struct qstr lostfound = QSTR("lost+found");
1257 const char *err = "cannot allocate memory";
1263 bch_notice(c, "initializing new filesystem");
1265 mutex_lock(&c->sb_lock);
1266 for_each_online_member(ca, c, i)
1267 bch2_mark_dev_superblock(c, ca, 0);
1268 mutex_unlock(&c->sb_lock);
1270 mutex_lock(&c->sb_lock);
1271 c->disk_sb.sb->version = c->disk_sb.sb->version_min =
1272 le16_to_cpu(bcachefs_metadata_version_current);
1273 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
1274 c->disk_sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
1276 bch2_write_super(c);
1277 mutex_unlock(&c->sb_lock);
1279 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
1280 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
1282 for (i = 0; i < BTREE_ID_NR; i++)
1283 bch2_btree_root_alloc(c, i);
1285 set_bit(BCH_FS_BTREE_INTERIOR_REPLAY_DONE, &c->flags);
1286 set_bit(JOURNAL_RECLAIM_STARTED, &c->journal.flags);
1288 err = "unable to allocate journal buckets";
1289 for_each_online_member(ca, c, i) {
1290 ret = bch2_dev_journal_alloc(ca);
1292 percpu_ref_put(&ca->io_ref);
1298 * journal_res_get() will crash if called before this has
1299 * set up the journal.pin FIFO and journal.cur pointer:
1301 bch2_fs_journal_start(&c->journal, 1, &journal);
1302 bch2_journal_set_replay_done(&c->journal);
1304 err = "error going read-write";
1305 ret = bch2_fs_read_write_early(c);
1310 * Write out the superblock and journal buckets, now that we can do
1313 err = "error writing alloc info";
1314 ret = bch2_alloc_write(c, 0);
1318 bch2_inode_init(c, &root_inode, 0, 0,
1319 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
1320 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1321 bch2_inode_pack(c, &packed_inode, &root_inode);
1323 err = "error creating root directory";
1324 ret = bch2_btree_insert(c, BTREE_ID_INODES,
1325 &packed_inode.inode.k_i,
1330 bch2_inode_init_early(c, &lostfound_inode);
1332 err = "error creating lost+found";
1333 ret = bch2_trans_do(c, NULL, NULL, 0,
1334 bch2_create_trans(&trans, BCACHEFS_ROOT_INO,
1335 &root_inode, &lostfound_inode,
1337 0, 0, S_IFDIR|0700, 0,
1342 if (enabled_qtypes(c)) {
1343 ret = bch2_fs_quota_read(c);
1348 err = "error writing first journal entry";
1349 ret = bch2_journal_meta(&c->journal);
1353 mutex_lock(&c->sb_lock);
1354 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1355 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1357 bch2_write_super(c);
1358 mutex_unlock(&c->sb_lock);
1362 pr_err("Error initializing new filesystem: %s (%i)", err, ret);