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 /* iterate over keys read from the journal: */
30 static struct journal_key *journal_key_search(struct journal_keys *journal_keys,
31 enum btree_id id, unsigned level,
34 size_t l = 0, r = journal_keys->nr, m;
37 m = l + ((r - l) >> 1);
38 if ((cmp_int(id, journal_keys->d[m].btree_id) ?:
39 cmp_int(level, journal_keys->d[m].level) ?:
40 bkey_cmp(pos, journal_keys->d[m].k->k.p)) > 0)
46 BUG_ON(l < journal_keys->nr &&
47 (cmp_int(id, journal_keys->d[l].btree_id) ?:
48 cmp_int(level, journal_keys->d[l].level) ?:
49 bkey_cmp(pos, journal_keys->d[l].k->k.p)) > 0);
52 (cmp_int(id, journal_keys->d[l - 1].btree_id) ?:
53 cmp_int(level, journal_keys->d[l - 1].level) ?:
54 bkey_cmp(pos, journal_keys->d[l - 1].k->k.p)) <= 0);
56 return l < journal_keys->nr ? journal_keys->d + l : NULL;
59 static struct bkey_i *bch2_journal_iter_peek(struct journal_iter *iter)
62 iter->k < iter->keys->d + iter->keys->nr &&
63 iter->k->btree_id == iter->btree_id &&
64 iter->k->level == iter->level)
71 static void bch2_journal_iter_advance(struct journal_iter *iter)
77 static void bch2_journal_iter_init(struct journal_iter *iter,
78 struct journal_keys *journal_keys,
79 enum btree_id id, unsigned level,
84 iter->keys = journal_keys;
85 iter->k = journal_key_search(journal_keys, id, level, pos);
88 static struct bkey_s_c bch2_journal_iter_peek_btree(struct btree_and_journal_iter *iter)
91 ? bch2_btree_iter_peek(iter->btree)
92 : bch2_btree_node_iter_peek_unpack(&iter->node_iter,
93 iter->b, &iter->unpacked);
96 static void bch2_journal_iter_advance_btree(struct btree_and_journal_iter *iter)
99 bch2_btree_iter_next(iter->btree);
101 bch2_btree_node_iter_advance(&iter->node_iter, iter->b);
104 void bch2_btree_and_journal_iter_advance(struct btree_and_journal_iter *iter)
106 switch (iter->last) {
110 bch2_journal_iter_advance_btree(iter);
113 bch2_journal_iter_advance(&iter->journal);
120 struct bkey_s_c bch2_btree_and_journal_iter_peek(struct btree_and_journal_iter *iter)
125 struct bkey_s_c btree_k =
126 bch2_journal_iter_peek_btree(iter);
127 struct bkey_s_c journal_k =
128 bkey_i_to_s_c(bch2_journal_iter_peek(&iter->journal));
130 if (btree_k.k && journal_k.k) {
131 int cmp = bkey_cmp(btree_k.k->p, journal_k.k->p);
134 bch2_journal_iter_advance_btree(iter);
136 iter->last = cmp < 0 ? btree : journal;
137 } else if (btree_k.k) {
139 } else if (journal_k.k) {
140 iter->last = journal;
143 return bkey_s_c_null;
146 ret = iter->last == journal ? journal_k : btree_k;
149 bkey_cmp(ret.k->p, iter->b->data->max_key) > 0) {
150 iter->journal.k = NULL;
152 return bkey_s_c_null;
155 if (!bkey_deleted(ret.k))
158 bch2_btree_and_journal_iter_advance(iter);
164 struct bkey_s_c bch2_btree_and_journal_iter_next(struct btree_and_journal_iter *iter)
166 bch2_btree_and_journal_iter_advance(iter);
168 return bch2_btree_and_journal_iter_peek(iter);
171 void bch2_btree_and_journal_iter_init(struct btree_and_journal_iter *iter,
172 struct btree_trans *trans,
173 struct journal_keys *journal_keys,
174 enum btree_id id, struct bpos pos)
176 memset(iter, 0, sizeof(*iter));
178 iter->btree = bch2_trans_get_iter(trans, id, pos, 0);
179 bch2_journal_iter_init(&iter->journal, journal_keys, id, 0, pos);
182 void bch2_btree_and_journal_iter_init_node_iter(struct btree_and_journal_iter *iter,
183 struct journal_keys *journal_keys,
186 memset(iter, 0, sizeof(*iter));
189 bch2_btree_node_iter_init_from_start(&iter->node_iter, iter->b);
190 bch2_journal_iter_init(&iter->journal, journal_keys,
191 b->c.btree_id, b->c.level, b->data->min_key);
194 /* Walk btree, overlaying keys from the journal: */
196 static int bch2_btree_and_journal_walk_recurse(struct bch_fs *c, struct btree *b,
197 struct journal_keys *journal_keys,
198 enum btree_id btree_id,
199 btree_walk_node_fn node_fn,
200 btree_walk_key_fn key_fn)
202 struct btree_and_journal_iter iter;
206 bch2_btree_and_journal_iter_init_node_iter(&iter, journal_keys, b);
208 while ((k = bch2_btree_and_journal_iter_peek(&iter)).k) {
209 ret = key_fn(c, btree_id, b->c.level, k);
217 bkey_reassemble(&tmp.k, k);
218 k = bkey_i_to_s_c(&tmp.k);
220 bch2_btree_and_journal_iter_advance(&iter);
222 if (b->c.level > 0) {
223 child = bch2_btree_node_get_noiter(c, &tmp.k,
224 b->c.btree_id, b->c.level - 1);
225 ret = PTR_ERR_OR_ZERO(child);
229 ret = (node_fn ? node_fn(c, b) : 0) ?:
230 bch2_btree_and_journal_walk_recurse(c, child,
231 journal_keys, btree_id, node_fn, key_fn);
232 six_unlock_read(&child->c.lock);
238 bch2_btree_and_journal_iter_advance(&iter);
245 int bch2_btree_and_journal_walk(struct bch_fs *c, struct journal_keys *journal_keys,
246 enum btree_id btree_id,
247 btree_walk_node_fn node_fn,
248 btree_walk_key_fn key_fn)
250 struct btree *b = c->btree_roots[btree_id].b;
253 if (btree_node_fake(b))
256 six_lock_read(&b->c.lock, NULL, NULL);
257 ret = (node_fn ? node_fn(c, b) : 0) ?:
258 bch2_btree_and_journal_walk_recurse(c, b, journal_keys, btree_id,
260 key_fn(c, btree_id, b->c.level + 1, bkey_i_to_s_c(&b->key));
261 six_unlock_read(&b->c.lock);
266 /* sort and dedup all keys in the journal: */
268 void bch2_journal_entries_free(struct list_head *list)
271 while (!list_empty(list)) {
272 struct journal_replay *i =
273 list_first_entry(list, struct journal_replay, list);
275 kvpfree(i, offsetof(struct journal_replay, j) +
276 vstruct_bytes(&i->j));
281 * When keys compare equal, oldest compares first:
283 static int journal_sort_key_cmp(const void *_l, const void *_r)
285 const struct journal_key *l = _l;
286 const struct journal_key *r = _r;
288 return cmp_int(l->btree_id, r->btree_id) ?:
289 cmp_int(l->level, r->level) ?:
290 bkey_cmp(l->k->k.p, r->k->k.p) ?:
291 cmp_int(l->journal_seq, r->journal_seq) ?:
292 cmp_int(l->journal_offset, r->journal_offset);
295 void bch2_journal_keys_free(struct journal_keys *keys)
302 static struct journal_keys journal_keys_sort(struct list_head *journal_entries)
304 struct journal_replay *p;
305 struct jset_entry *entry;
306 struct bkey_i *k, *_n;
307 struct journal_keys keys = { NULL };
308 struct journal_key *src, *dst;
311 if (list_empty(journal_entries))
314 keys.journal_seq_base =
315 le64_to_cpu(list_last_entry(journal_entries,
316 struct journal_replay, list)->j.last_seq);
318 list_for_each_entry(p, journal_entries, list) {
319 if (le64_to_cpu(p->j.seq) < keys.journal_seq_base)
322 for_each_jset_key(k, _n, entry, &p->j)
327 keys.d = kvmalloc(sizeof(keys.d[0]) * nr_keys, GFP_KERNEL);
331 list_for_each_entry(p, journal_entries, list) {
332 if (le64_to_cpu(p->j.seq) < keys.journal_seq_base)
335 for_each_jset_key(k, _n, entry, &p->j)
336 keys.d[keys.nr++] = (struct journal_key) {
337 .btree_id = entry->btree_id,
338 .level = entry->level,
340 .journal_seq = le64_to_cpu(p->j.seq) -
341 keys.journal_seq_base,
342 .journal_offset = k->_data - p->j._data,
346 sort(keys.d, keys.nr, sizeof(keys.d[0]), journal_sort_key_cmp, NULL);
349 while (src < keys.d + keys.nr) {
350 while (src + 1 < keys.d + keys.nr &&
351 src[0].btree_id == src[1].btree_id &&
352 src[0].level == src[1].level &&
353 !bkey_cmp(src[0].k->k.p, src[1].k->k.p))
359 keys.nr = dst - keys.d;
364 /* journal replay: */
366 static void replay_now_at(struct journal *j, u64 seq)
368 BUG_ON(seq < j->replay_journal_seq);
369 BUG_ON(seq > j->replay_journal_seq_end);
371 while (j->replay_journal_seq < seq)
372 bch2_journal_pin_put(j, j->replay_journal_seq++);
375 static int bch2_extent_replay_key(struct bch_fs *c, enum btree_id btree_id,
378 struct btree_trans trans;
379 struct btree_iter *iter, *split_iter;
381 * We might cause compressed extents to be split, so we need to pass in
382 * a disk_reservation:
384 struct disk_reservation disk_res =
385 bch2_disk_reservation_init(c, 0);
386 struct bkey_i *split;
387 struct bpos atomic_end;
389 * Some extents aren't equivalent - w.r.t. what the triggers do
390 * - if they're split:
392 bool remark_if_split = bch2_bkey_sectors_compressed(bkey_i_to_s_c(k)) ||
393 k->k.type == KEY_TYPE_reflink_p;
397 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
399 bch2_trans_begin(&trans);
401 iter = bch2_trans_get_iter(&trans, btree_id,
402 bkey_start_pos(&k->k),
406 ret = bch2_btree_iter_traverse(iter);
410 atomic_end = bpos_min(k->k.p, iter->l[0].b->key.k.p);
412 split = bch2_trans_kmalloc(&trans, bkey_bytes(&k->k));
413 ret = PTR_ERR_OR_ZERO(split);
419 bkey_cmp(atomic_end, k->k.p) < 0) {
420 ret = bch2_disk_reservation_add(c, &disk_res,
422 bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(k)),
423 BCH_DISK_RESERVATION_NOFAIL);
430 bch2_cut_front(iter->pos, split);
431 bch2_cut_back(atomic_end, split);
433 split_iter = bch2_trans_copy_iter(&trans, iter);
434 ret = PTR_ERR_OR_ZERO(split_iter);
439 * It's important that we don't go through the
440 * extent_handle_overwrites() and extent_update_to_keys() path
441 * here: journal replay is supposed to treat extents like
444 __bch2_btree_iter_set_pos(split_iter, split->k.p, false);
445 bch2_trans_update(&trans, split_iter, split,
446 BTREE_TRIGGER_NORUN);
448 bch2_btree_iter_set_pos(iter, split->k.p);
451 ret = bch2_trans_mark_key(&trans, bkey_i_to_s_c(split),
453 BTREE_TRIGGER_INSERT);
457 } while (bkey_cmp(iter->pos, k->k.p) < 0);
460 ret = bch2_trans_mark_key(&trans, bkey_i_to_s_c(k),
461 0, -((s64) k->k.size),
462 BTREE_TRIGGER_OVERWRITE);
467 ret = bch2_trans_commit(&trans, &disk_res, NULL,
469 BTREE_INSERT_LAZY_RW|
470 BTREE_INSERT_JOURNAL_REPLAY);
475 bch2_disk_reservation_put(c, &disk_res);
477 return bch2_trans_exit(&trans) ?: ret;
480 static int __bch2_journal_replay_key(struct btree_trans *trans,
481 enum btree_id id, unsigned level,
484 struct btree_iter *iter;
487 iter = bch2_trans_get_node_iter(trans, id, k->k.p,
488 BTREE_MAX_DEPTH, level,
491 return PTR_ERR(iter);
494 * iter->flags & BTREE_ITER_IS_EXTENTS triggers the update path to run
495 * extent_handle_overwrites() and extent_update_to_keys() - but we don't
496 * want that here, journal replay is supposed to treat extents like
499 __bch2_btree_iter_set_pos(iter, k->k.p, false);
501 ret = bch2_btree_iter_traverse(iter) ?:
502 bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
503 bch2_trans_iter_put(trans, iter);
507 static int bch2_journal_replay_key(struct bch_fs *c, enum btree_id id,
508 unsigned level, struct bkey_i *k)
510 return bch2_trans_do(c, NULL, NULL,
512 BTREE_INSERT_LAZY_RW|
513 BTREE_INSERT_JOURNAL_REPLAY,
514 __bch2_journal_replay_key(&trans, id, level, k));
517 static int __bch2_alloc_replay_key(struct btree_trans *trans, struct bkey_i *k)
519 struct btree_iter *iter;
522 iter = bch2_trans_get_iter(trans, BTREE_ID_ALLOC, k->k.p,
524 BTREE_ITER_CACHED_NOFILL|
526 ret = PTR_ERR_OR_ZERO(iter) ?:
527 bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
528 bch2_trans_iter_put(trans, iter);
532 static int bch2_alloc_replay_key(struct bch_fs *c, struct bkey_i *k)
534 return bch2_trans_do(c, NULL, NULL,
536 BTREE_INSERT_USE_RESERVE|
537 BTREE_INSERT_LAZY_RW|
538 BTREE_INSERT_JOURNAL_REPLAY,
539 __bch2_alloc_replay_key(&trans, k));
542 static int journal_sort_seq_cmp(const void *_l, const void *_r)
544 const struct journal_key *l = _l;
545 const struct journal_key *r = _r;
547 return cmp_int(r->level, l->level) ?:
548 cmp_int(l->journal_seq, r->journal_seq) ?:
549 cmp_int(l->btree_id, r->btree_id) ?:
550 bkey_cmp(l->k->k.p, r->k->k.p);
553 static int bch2_journal_replay(struct bch_fs *c,
554 struct journal_keys keys)
556 struct journal *j = &c->journal;
557 struct journal_key *i;
561 sort(keys.d, keys.nr, sizeof(keys.d[0]), journal_sort_seq_cmp, NULL);
564 replay_now_at(j, keys.journal_seq_base);
566 seq = j->replay_journal_seq;
569 * First replay updates to the alloc btree - these will only update the
572 for_each_journal_key(keys, i) {
575 if (!i->level && i->btree_id == BTREE_ID_ALLOC) {
576 j->replay_journal_seq = keys.journal_seq_base + i->journal_seq;
577 ret = bch2_alloc_replay_key(c, i->k);
584 * Next replay updates to interior btree nodes:
586 for_each_journal_key(keys, i) {
590 j->replay_journal_seq = keys.journal_seq_base + i->journal_seq;
591 ret = bch2_journal_replay_key(c, i->btree_id, i->level, i->k);
598 * Now that the btree is in a consistent state, we can start journal
599 * reclaim (which will be flushing entries from the btree key cache back
602 set_bit(BCH_FS_BTREE_INTERIOR_REPLAY_DONE, &c->flags);
603 set_bit(JOURNAL_RECLAIM_STARTED, &j->flags);
605 j->replay_journal_seq = seq;
608 * Now replay leaf node updates:
610 for_each_journal_key(keys, i) {
613 if (i->level || i->btree_id == BTREE_ID_ALLOC)
616 replay_now_at(j, keys.journal_seq_base + i->journal_seq);
619 ? bch2_extent_replay_key(c, i->btree_id, i->k)
620 : bch2_journal_replay_key(c, i->btree_id, i->level, i->k);
625 replay_now_at(j, j->replay_journal_seq_end);
626 j->replay_journal_seq = 0;
628 bch2_journal_set_replay_done(j);
629 bch2_journal_flush_all_pins(j);
630 return bch2_journal_error(j);
632 bch_err(c, "journal replay: error %d while replaying key", ret);
636 static bool journal_empty(struct list_head *journal)
638 return list_empty(journal) ||
639 journal_entry_empty(&list_last_entry(journal,
640 struct journal_replay, list)->j);
644 verify_journal_entries_not_blacklisted_or_missing(struct bch_fs *c,
645 struct list_head *journal)
647 struct journal_replay *i =
648 list_last_entry(journal, struct journal_replay, list);
649 u64 start_seq = le64_to_cpu(i->j.last_seq);
650 u64 end_seq = le64_to_cpu(i->j.seq);
654 list_for_each_entry(i, journal, list) {
655 if (le64_to_cpu(i->j.seq) < start_seq)
658 fsck_err_on(seq != le64_to_cpu(i->j.seq), c,
659 "journal entries %llu-%llu missing! (replaying %llu-%llu)",
660 seq, le64_to_cpu(i->j.seq) - 1,
663 seq = le64_to_cpu(i->j.seq);
665 fsck_err_on(bch2_journal_seq_is_blacklisted(c, seq, false), c,
666 "found blacklisted journal entry %llu", seq);
670 } while (bch2_journal_seq_is_blacklisted(c, seq, false));
676 /* journal replay early: */
678 static int journal_replay_entry_early(struct bch_fs *c,
679 struct jset_entry *entry)
683 switch (entry->type) {
684 case BCH_JSET_ENTRY_btree_root: {
685 struct btree_root *r;
687 if (entry->btree_id >= BTREE_ID_NR) {
688 bch_err(c, "filesystem has unknown btree type %u",
693 r = &c->btree_roots[entry->btree_id];
696 r->level = entry->level;
697 bkey_copy(&r->key, &entry->start[0]);
705 case BCH_JSET_ENTRY_usage: {
706 struct jset_entry_usage *u =
707 container_of(entry, struct jset_entry_usage, entry);
709 switch (entry->btree_id) {
710 case FS_USAGE_RESERVED:
711 if (entry->level < BCH_REPLICAS_MAX)
712 c->usage_base->persistent_reserved[entry->level] =
715 case FS_USAGE_INODES:
716 c->usage_base->nr_inodes = le64_to_cpu(u->v);
718 case FS_USAGE_KEY_VERSION:
719 atomic64_set(&c->key_version,
726 case BCH_JSET_ENTRY_data_usage: {
727 struct jset_entry_data_usage *u =
728 container_of(entry, struct jset_entry_data_usage, entry);
729 ret = bch2_replicas_set_usage(c, &u->r,
733 case BCH_JSET_ENTRY_blacklist: {
734 struct jset_entry_blacklist *bl_entry =
735 container_of(entry, struct jset_entry_blacklist, entry);
737 ret = bch2_journal_seq_blacklist_add(c,
738 le64_to_cpu(bl_entry->seq),
739 le64_to_cpu(bl_entry->seq) + 1);
742 case BCH_JSET_ENTRY_blacklist_v2: {
743 struct jset_entry_blacklist_v2 *bl_entry =
744 container_of(entry, struct jset_entry_blacklist_v2, entry);
746 ret = bch2_journal_seq_blacklist_add(c,
747 le64_to_cpu(bl_entry->start),
748 le64_to_cpu(bl_entry->end) + 1);
756 static int journal_replay_early(struct bch_fs *c,
757 struct bch_sb_field_clean *clean,
758 struct list_head *journal)
760 struct jset_entry *entry;
764 c->bucket_clock[READ].hand = le16_to_cpu(clean->read_clock);
765 c->bucket_clock[WRITE].hand = le16_to_cpu(clean->write_clock);
767 for (entry = clean->start;
768 entry != vstruct_end(&clean->field);
769 entry = vstruct_next(entry)) {
770 ret = journal_replay_entry_early(c, entry);
775 struct journal_replay *i =
776 list_last_entry(journal, struct journal_replay, list);
778 c->bucket_clock[READ].hand = le16_to_cpu(i->j.read_clock);
779 c->bucket_clock[WRITE].hand = le16_to_cpu(i->j.write_clock);
781 list_for_each_entry(i, journal, list)
782 vstruct_for_each(&i->j, entry) {
783 ret = journal_replay_entry_early(c, entry);
789 bch2_fs_usage_initialize(c);
794 /* sb clean section: */
796 static struct bkey_i *btree_root_find(struct bch_fs *c,
797 struct bch_sb_field_clean *clean,
799 enum btree_id id, unsigned *level)
802 struct jset_entry *entry, *start, *end;
805 start = clean->start;
806 end = vstruct_end(&clean->field);
809 end = vstruct_last(j);
812 for (entry = start; entry < end; entry = vstruct_next(entry))
813 if (entry->type == BCH_JSET_ENTRY_btree_root &&
814 entry->btree_id == id)
820 return ERR_PTR(-EINVAL);
823 *level = entry->level;
827 static int verify_superblock_clean(struct bch_fs *c,
828 struct bch_sb_field_clean **cleanp,
832 struct bch_sb_field_clean *clean = *cleanp;
835 if (!c->sb.clean || !j)
838 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
839 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
840 le64_to_cpu(clean->journal_seq),
841 le64_to_cpu(j->seq))) {
847 mustfix_fsck_err_on(j->read_clock != clean->read_clock, c,
848 "superblock read clock %u doesn't match journal %u after clean shutdown",
849 clean->read_clock, j->read_clock);
850 mustfix_fsck_err_on(j->write_clock != clean->write_clock, c,
851 "superblock write clock %u doesn't match journal %u after clean shutdown",
852 clean->write_clock, j->write_clock);
854 for (i = 0; i < BTREE_ID_NR; i++) {
855 char buf1[200], buf2[200];
856 struct bkey_i *k1, *k2;
857 unsigned l1 = 0, l2 = 0;
859 k1 = btree_root_find(c, clean, NULL, i, &l1);
860 k2 = btree_root_find(c, NULL, j, i, &l2);
865 mustfix_fsck_err_on(!k1 || !k2 ||
868 k1->k.u64s != k2->k.u64s ||
869 memcmp(k1, k2, bkey_bytes(k1)) ||
871 "superblock btree root %u doesn't match journal after clean shutdown\n"
873 "journal: l=%u %s\n", i,
874 l1, (bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(k1)), buf1),
875 l2, (bch2_bkey_val_to_text(&PBUF(buf2), c, bkey_i_to_s_c(k2)), buf2));
881 static struct bch_sb_field_clean *read_superblock_clean(struct bch_fs *c)
883 struct bch_sb_field_clean *clean, *sb_clean;
886 mutex_lock(&c->sb_lock);
887 sb_clean = bch2_sb_get_clean(c->disk_sb.sb);
889 if (fsck_err_on(!sb_clean, c,
890 "superblock marked clean but clean section not present")) {
891 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
893 mutex_unlock(&c->sb_lock);
897 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
900 mutex_unlock(&c->sb_lock);
901 return ERR_PTR(-ENOMEM);
904 if (le16_to_cpu(c->disk_sb.sb->version) <
905 bcachefs_metadata_version_bkey_renumber)
906 bch2_sb_clean_renumber(clean, READ);
908 mutex_unlock(&c->sb_lock);
912 mutex_unlock(&c->sb_lock);
916 static int read_btree_roots(struct bch_fs *c)
921 for (i = 0; i < BTREE_ID_NR; i++) {
922 struct btree_root *r = &c->btree_roots[i];
927 if (i == BTREE_ID_ALLOC &&
928 c->opts.reconstruct_alloc) {
929 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
935 __fsck_err(c, i == BTREE_ID_ALLOC
936 ? FSCK_CAN_IGNORE : 0,
937 "invalid btree root %s",
939 if (i == BTREE_ID_ALLOC)
940 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
943 ret = bch2_btree_root_read(c, i, &r->key, r->level);
945 __fsck_err(c, i == BTREE_ID_ALLOC
946 ? FSCK_CAN_IGNORE : 0,
947 "error reading btree root %s",
949 if (i == BTREE_ID_ALLOC)
950 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
954 for (i = 0; i < BTREE_ID_NR; i++)
955 if (!c->btree_roots[i].b)
956 bch2_btree_root_alloc(c, i);
961 int bch2_fs_recovery(struct bch_fs *c)
963 const char *err = "cannot allocate memory";
964 struct bch_sb_field_clean *clean = NULL;
966 bool write_sb = false, need_write_alloc = false;
970 clean = read_superblock_clean(c);
971 ret = PTR_ERR_OR_ZERO(clean);
976 bch_info(c, "recovering from clean shutdown, journal seq %llu",
977 le64_to_cpu(clean->journal_seq));
979 if (!c->replicas.entries ||
980 c->opts.rebuild_replicas) {
981 bch_info(c, "building replicas info");
982 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
985 if (!c->sb.clean || c->opts.fsck || c->opts.keep_journal) {
988 ret = bch2_journal_read(c, &c->journal_entries);
992 if (mustfix_fsck_err_on(c->sb.clean && !journal_empty(&c->journal_entries), c,
993 "filesystem marked clean but journal not empty")) {
994 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
995 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
999 if (!c->sb.clean && list_empty(&c->journal_entries)) {
1000 bch_err(c, "no journal entries found");
1001 ret = BCH_FSCK_REPAIR_IMPOSSIBLE;
1005 c->journal_keys = journal_keys_sort(&c->journal_entries);
1006 if (!c->journal_keys.d) {
1011 j = &list_last_entry(&c->journal_entries,
1012 struct journal_replay, list)->j;
1014 ret = verify_superblock_clean(c, &clean, j);
1018 journal_seq = le64_to_cpu(j->seq) + 1;
1020 journal_seq = le64_to_cpu(clean->journal_seq) + 1;
1024 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) {
1025 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix");
1030 ret = journal_replay_early(c, clean, &c->journal_entries);
1035 ret = bch2_journal_seq_blacklist_add(c,
1039 bch_err(c, "error creating new journal seq blacklist entry");
1046 * The superblock needs to be written before we do any btree
1047 * node writes: it will be in the read_write() path
1051 ret = bch2_blacklist_table_initialize(c);
1053 if (!list_empty(&c->journal_entries)) {
1054 ret = verify_journal_entries_not_blacklisted_or_missing(c,
1055 &c->journal_entries);
1060 ret = bch2_fs_journal_start(&c->journal, journal_seq,
1061 &c->journal_entries);
1065 ret = read_btree_roots(c);
1069 bch_verbose(c, "starting alloc read");
1070 err = "error reading allocation information";
1071 ret = bch2_alloc_read(c, &c->journal_keys);
1074 bch_verbose(c, "alloc read done");
1076 bch_verbose(c, "starting stripes_read");
1077 err = "error reading stripes";
1078 ret = bch2_stripes_read(c, &c->journal_keys);
1081 bch_verbose(c, "stripes_read done");
1083 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
1085 if ((c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) &&
1086 !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_METADATA))) {
1088 * interior btree node updates aren't consistent with the
1089 * journal; after an unclean shutdown we have to walk all
1090 * pointers to metadata:
1092 bch_info(c, "starting metadata mark and sweep");
1093 err = "error in mark and sweep";
1094 ret = bch2_gc(c, &c->journal_keys, true, true);
1098 need_write_alloc = true;
1099 bch_verbose(c, "mark and sweep done");
1103 !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) ||
1104 test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags)) {
1105 bch_info(c, "starting mark and sweep");
1106 err = "error in mark and sweep";
1107 ret = bch2_gc(c, &c->journal_keys, true, false);
1111 need_write_alloc = true;
1112 bch_verbose(c, "mark and sweep done");
1115 clear_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
1116 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
1119 * Skip past versions that might have possibly been used (as nonces),
1120 * but hadn't had their pointers written:
1122 if (c->sb.encryption_type && !c->sb.clean)
1123 atomic64_add(1 << 16, &c->key_version);
1125 if (c->opts.norecovery)
1128 bch_verbose(c, "starting journal replay");
1129 err = "journal replay failed";
1130 ret = bch2_journal_replay(c, c->journal_keys);
1133 bch_verbose(c, "journal replay done");
1135 if (need_write_alloc && !c->opts.nochanges) {
1137 * note that even when filesystem was clean there might be work
1138 * to do here, if we ran gc (because of fsck) which recalculated
1141 bch_verbose(c, "writing allocation info");
1142 err = "error writing out alloc info";
1143 ret = bch2_stripes_write(c, BTREE_INSERT_LAZY_RW) ?:
1144 bch2_alloc_write(c, BTREE_INSERT_LAZY_RW);
1146 bch_err(c, "error writing alloc info");
1149 bch_verbose(c, "alloc write done");
1151 set_bit(BCH_FS_ALLOC_WRITTEN, &c->flags);
1155 if (!(c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1156 bch_info(c, "checking inode link counts");
1157 err = "error in recovery";
1158 ret = bch2_fsck_inode_nlink(c);
1161 bch_verbose(c, "check inodes done");
1164 bch_verbose(c, "checking for deleted inodes");
1165 err = "error in recovery";
1166 ret = bch2_fsck_walk_inodes_only(c);
1169 bch_verbose(c, "check inodes done");
1174 bch_info(c, "starting fsck");
1175 err = "error in fsck";
1176 ret = bch2_fsck_full(c);
1179 bch_verbose(c, "fsck done");
1182 if (enabled_qtypes(c)) {
1183 bch_verbose(c, "reading quotas");
1184 ret = bch2_fs_quota_read(c);
1187 bch_verbose(c, "quotas done");
1190 mutex_lock(&c->sb_lock);
1191 if (c->opts.version_upgrade) {
1192 if (c->sb.version < bcachefs_metadata_version_new_versioning)
1193 c->disk_sb.sb->version_min =
1194 le16_to_cpu(bcachefs_metadata_version_min);
1195 c->disk_sb.sb->version = le16_to_cpu(bcachefs_metadata_version_current);
1196 c->disk_sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
1200 if (!test_bit(BCH_FS_ERROR, &c->flags)) {
1201 c->disk_sb.sb->compat[0] |= 1ULL << BCH_COMPAT_FEAT_ALLOC_INFO;
1206 !test_bit(BCH_FS_ERROR, &c->flags)) {
1207 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
1208 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
1213 bch2_write_super(c);
1214 mutex_unlock(&c->sb_lock);
1216 if (c->journal_seq_blacklist_table &&
1217 c->journal_seq_blacklist_table->nr > 128)
1218 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
1223 set_bit(BCH_FS_FSCK_DONE, &c->flags);
1224 bch2_flush_fsck_errs(c);
1226 if (!c->opts.keep_journal) {
1227 bch2_journal_keys_free(&c->journal_keys);
1228 bch2_journal_entries_free(&c->journal_entries);
1232 bch_err(c, "Error in recovery: %s (%i)", err, ret);
1234 bch_verbose(c, "ret %i", ret);
1238 int bch2_fs_initialize(struct bch_fs *c)
1240 struct bch_inode_unpacked root_inode, lostfound_inode;
1241 struct bkey_inode_buf packed_inode;
1242 struct qstr lostfound = QSTR("lost+found");
1243 const char *err = "cannot allocate memory";
1249 bch_notice(c, "initializing new filesystem");
1251 mutex_lock(&c->sb_lock);
1252 for_each_online_member(ca, c, i)
1253 bch2_mark_dev_superblock(c, ca, 0);
1254 mutex_unlock(&c->sb_lock);
1256 mutex_lock(&c->sb_lock);
1257 c->disk_sb.sb->version = c->disk_sb.sb->version_min =
1258 le16_to_cpu(bcachefs_metadata_version_current);
1259 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
1260 c->disk_sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
1262 bch2_write_super(c);
1263 mutex_unlock(&c->sb_lock);
1265 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
1266 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
1268 for (i = 0; i < BTREE_ID_NR; i++)
1269 bch2_btree_root_alloc(c, i);
1271 set_bit(BCH_FS_BTREE_INTERIOR_REPLAY_DONE, &c->flags);
1272 set_bit(JOURNAL_RECLAIM_STARTED, &c->journal.flags);
1274 err = "unable to allocate journal buckets";
1275 for_each_online_member(ca, c, i) {
1276 ret = bch2_dev_journal_alloc(ca);
1278 percpu_ref_put(&ca->io_ref);
1284 * journal_res_get() will crash if called before this has
1285 * set up the journal.pin FIFO and journal.cur pointer:
1287 bch2_fs_journal_start(&c->journal, 1, &journal);
1288 bch2_journal_set_replay_done(&c->journal);
1290 err = "error going read-write";
1291 ret = bch2_fs_read_write_early(c);
1296 * Write out the superblock and journal buckets, now that we can do
1299 err = "error writing alloc info";
1300 ret = bch2_alloc_write(c, 0);
1304 bch2_inode_init(c, &root_inode, 0, 0,
1305 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
1306 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1307 bch2_inode_pack(&packed_inode, &root_inode);
1309 err = "error creating root directory";
1310 ret = bch2_btree_insert(c, BTREE_ID_INODES,
1311 &packed_inode.inode.k_i,
1316 bch2_inode_init_early(c, &lostfound_inode);
1318 err = "error creating lost+found";
1319 ret = bch2_trans_do(c, NULL, NULL, 0,
1320 bch2_create_trans(&trans, BCACHEFS_ROOT_INO,
1321 &root_inode, &lostfound_inode,
1323 0, 0, S_IFDIR|0700, 0,
1328 if (enabled_qtypes(c)) {
1329 ret = bch2_fs_quota_read(c);
1334 err = "error writing first journal entry";
1335 ret = bch2_journal_meta(&c->journal);
1339 mutex_lock(&c->sb_lock);
1340 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1341 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1343 bch2_write_super(c);
1344 mutex_unlock(&c->sb_lock);
1348 pr_err("Error initializing new filesystem: %s (%i)", err, ret);