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 struct journal_iter bch2_journal_iter_init(struct journal_keys *keys,
33 return (struct journal_iter) {
40 struct bkey_s_c bch2_journal_iter_peek(struct journal_iter *iter)
43 if (iter->k == iter->keys->d + iter->keys->nr)
46 if (iter->k->btree_id == iter->btree_id)
47 return bkey_i_to_s_c(iter->k->k);
55 struct bkey_s_c bch2_journal_iter_next(struct journal_iter *iter)
57 if (iter->k == iter->keys->d + iter->keys->nr)
61 return bch2_journal_iter_peek(iter);
64 /* sort and dedup all keys in the journal: */
66 static void journal_entries_free(struct list_head *list)
69 while (!list_empty(list)) {
70 struct journal_replay *i =
71 list_first_entry(list, struct journal_replay, list);
73 kvpfree(i, offsetof(struct journal_replay, j) +
74 vstruct_bytes(&i->j));
78 static int journal_sort_key_cmp(const void *_l, const void *_r)
80 const struct journal_key *l = _l;
81 const struct journal_key *r = _r;
83 return cmp_int(l->btree_id, r->btree_id) ?:
84 bkey_cmp(l->pos, r->pos) ?:
85 cmp_int(l->journal_seq, r->journal_seq) ?:
86 cmp_int(l->journal_offset, r->journal_offset);
89 static int journal_sort_seq_cmp(const void *_l, const void *_r)
91 const struct journal_key *l = _l;
92 const struct journal_key *r = _r;
94 return cmp_int(l->journal_seq, r->journal_seq) ?:
95 cmp_int(l->btree_id, r->btree_id) ?:
96 bkey_cmp(l->pos, r->pos);
99 static void journal_keys_sift(struct journal_keys *keys, struct journal_key *i)
101 while (i + 1 < keys->d + keys->nr &&
102 journal_sort_key_cmp(i, i + 1) > 0) {
108 static void journal_keys_free(struct journal_keys *keys)
110 struct journal_key *i;
112 for_each_journal_key(*keys, i)
120 static struct journal_keys journal_keys_sort(struct list_head *journal_entries)
122 struct journal_replay *p;
123 struct jset_entry *entry;
124 struct bkey_i *k, *_n;
125 struct journal_keys keys = { NULL }, keys_deduped = { NULL };
126 struct journal_key *i;
129 list_for_each_entry(p, journal_entries, list)
130 for_each_jset_key(k, _n, entry, &p->j)
133 keys.journal_seq_base = keys_deduped.journal_seq_base =
134 le64_to_cpu(list_first_entry(journal_entries,
135 struct journal_replay,
138 keys.d = kvmalloc(sizeof(keys.d[0]) * nr_keys, GFP_KERNEL);
142 keys_deduped.d = kvmalloc(sizeof(keys.d[0]) * nr_keys * 2, GFP_KERNEL);
146 list_for_each_entry(p, journal_entries, list)
147 for_each_jset_key(k, _n, entry, &p->j)
148 keys.d[keys.nr++] = (struct journal_key) {
149 .btree_id = entry->btree_id,
150 .pos = bkey_start_pos(&k->k),
152 .journal_seq = le64_to_cpu(p->j.seq) -
153 keys.journal_seq_base,
154 .journal_offset = k->_data - p->j._data,
157 sort(keys.d, nr_keys, sizeof(keys.d[0]), journal_sort_key_cmp, NULL);
160 while (i < keys.d + keys.nr) {
161 if (i + 1 < keys.d + keys.nr &&
162 i[0].btree_id == i[1].btree_id &&
163 !bkey_cmp(i[0].pos, i[1].pos)) {
164 if (bkey_cmp(i[0].k->k.p, i[1].k->k.p) <= 0) {
167 bch2_cut_front(i[1].k->k.p, i[0].k);
168 i[0].pos = i[1].k->k.p;
169 journal_keys_sift(&keys, i);
174 if (i + 1 < keys.d + keys.nr &&
175 i[0].btree_id == i[1].btree_id &&
176 bkey_cmp(i[0].k->k.p, bkey_start_pos(&i[1].k->k)) > 0) {
177 if ((cmp_int(i[0].journal_seq, i[1].journal_seq) ?:
178 cmp_int(i[0].journal_offset, i[1].journal_offset)) < 0) {
179 if (bkey_cmp(i[0].k->k.p, i[1].k->k.p) <= 0) {
180 bch2_cut_back(bkey_start_pos(&i[1].k->k), i[0].k);
182 struct bkey_i *split =
183 kmalloc(bkey_bytes(i[0].k), GFP_KERNEL);
188 bkey_copy(split, i[0].k);
189 bch2_cut_back(bkey_start_pos(&i[1].k->k), split);
190 keys_deduped.d[keys_deduped.nr++] = (struct journal_key) {
191 .btree_id = i[0].btree_id,
193 .pos = bkey_start_pos(&split->k),
195 .journal_seq = i[0].journal_seq,
196 .journal_offset = i[0].journal_offset,
199 bch2_cut_front(i[1].k->k.p, i[0].k);
200 i[0].pos = i[1].k->k.p;
201 journal_keys_sift(&keys, i);
205 if (bkey_cmp(i[0].k->k.p, i[1].k->k.p) >= 0) {
210 bch2_cut_front(i[0].k->k.p, i[1].k);
211 i[1].pos = i[0].k->k.p;
212 journal_keys_sift(&keys, i + 1);
218 keys_deduped.d[keys_deduped.nr++] = *i++;
224 journal_keys_free(&keys_deduped);
226 return (struct journal_keys) { NULL };
229 /* journal replay: */
231 static void replay_now_at(struct journal *j, u64 seq)
233 BUG_ON(seq < j->replay_journal_seq);
234 BUG_ON(seq > j->replay_journal_seq_end);
236 while (j->replay_journal_seq < seq)
237 bch2_journal_pin_put(j, j->replay_journal_seq++);
240 static int bch2_extent_replay_key(struct bch_fs *c, enum btree_id btree_id,
243 struct btree_trans trans;
244 struct btree_iter *iter, *split_iter;
246 * We might cause compressed extents to be split, so we need to pass in
247 * a disk_reservation:
249 struct disk_reservation disk_res =
250 bch2_disk_reservation_init(c, 0);
251 struct bkey_i *split;
252 struct bpos atomic_end;
254 * Some extents aren't equivalent - w.r.t. what the triggers do
255 * - if they're split:
257 bool remark_if_split = bch2_bkey_sectors_compressed(bkey_i_to_s_c(k)) ||
258 k->k.type == KEY_TYPE_reflink_p;
262 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
264 bch2_trans_begin(&trans);
266 iter = bch2_trans_get_iter(&trans, btree_id,
267 bkey_start_pos(&k->k),
271 ret = bch2_btree_iter_traverse(iter);
275 atomic_end = bpos_min(k->k.p, iter->l[0].b->key.k.p);
277 split_iter = bch2_trans_copy_iter(&trans, iter);
278 ret = PTR_ERR_OR_ZERO(split_iter);
282 split = bch2_trans_kmalloc(&trans, bkey_bytes(&k->k));
283 ret = PTR_ERR_OR_ZERO(split);
289 bkey_cmp(atomic_end, k->k.p) < 0) {
290 ret = bch2_disk_reservation_add(c, &disk_res,
292 bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(k)),
293 BCH_DISK_RESERVATION_NOFAIL);
300 bch2_cut_front(split_iter->pos, split);
301 bch2_cut_back(atomic_end, split);
303 bch2_trans_update(&trans, split_iter, split);
304 bch2_btree_iter_set_pos(iter, split->k.p);
305 } while (bkey_cmp(iter->pos, k->k.p) < 0);
308 ret = bch2_trans_mark_key(&trans, bkey_i_to_s_c(k),
309 0, -((s64) k->k.size),
310 BCH_BUCKET_MARK_OVERWRITE) ?:
311 bch2_trans_commit(&trans, &disk_res, NULL,
314 BTREE_INSERT_LAZY_RW|
315 BTREE_INSERT_NOMARK_OVERWRITES|
316 BTREE_INSERT_NO_CLEAR_REPLICAS);
318 ret = bch2_trans_commit(&trans, &disk_res, NULL,
321 BTREE_INSERT_LAZY_RW|
322 BTREE_INSERT_JOURNAL_REPLAY|
323 BTREE_INSERT_NOMARK);
332 bch2_disk_reservation_put(c, &disk_res);
334 return bch2_trans_exit(&trans) ?: ret;
337 static int bch2_journal_replay(struct bch_fs *c,
338 struct journal_keys keys)
340 struct journal *j = &c->journal;
341 struct journal_key *i;
344 sort(keys.d, keys.nr, sizeof(keys.d[0]), journal_sort_seq_cmp, NULL);
346 for_each_journal_key(keys, i) {
347 replay_now_at(j, keys.journal_seq_base + i->journal_seq);
349 if (i->btree_id == BTREE_ID_ALLOC)
350 ret = bch2_alloc_replay_key(c, i->k);
351 else if (btree_node_type_is_extents(i->btree_id))
352 ret = bch2_extent_replay_key(c, i->btree_id, i->k);
354 ret = bch2_btree_insert(c, i->btree_id, i->k,
357 BTREE_INSERT_LAZY_RW|
358 BTREE_INSERT_JOURNAL_REPLAY|
359 BTREE_INSERT_NOMARK);
362 bch_err(c, "journal replay: error %d while replaying key",
370 replay_now_at(j, j->replay_journal_seq_end);
371 j->replay_journal_seq = 0;
373 bch2_journal_set_replay_done(j);
374 bch2_journal_flush_all_pins(j);
375 return bch2_journal_error(j);
378 static bool journal_empty(struct list_head *journal)
380 return list_empty(journal) ||
381 journal_entry_empty(&list_last_entry(journal,
382 struct journal_replay, list)->j);
386 verify_journal_entries_not_blacklisted_or_missing(struct bch_fs *c,
387 struct list_head *journal)
389 struct journal_replay *i =
390 list_last_entry(journal, struct journal_replay, list);
391 u64 start_seq = le64_to_cpu(i->j.last_seq);
392 u64 end_seq = le64_to_cpu(i->j.seq);
396 list_for_each_entry(i, journal, list) {
397 fsck_err_on(seq != le64_to_cpu(i->j.seq), c,
398 "journal entries %llu-%llu missing! (replaying %llu-%llu)",
399 seq, le64_to_cpu(i->j.seq) - 1,
402 seq = le64_to_cpu(i->j.seq);
404 fsck_err_on(bch2_journal_seq_is_blacklisted(c, seq, false), c,
405 "found blacklisted journal entry %llu", seq);
409 } while (bch2_journal_seq_is_blacklisted(c, seq, false));
415 /* journal replay early: */
417 static int journal_replay_entry_early(struct bch_fs *c,
418 struct jset_entry *entry)
422 switch (entry->type) {
423 case BCH_JSET_ENTRY_btree_root: {
424 struct btree_root *r;
426 if (entry->btree_id >= BTREE_ID_NR) {
427 bch_err(c, "filesystem has unknown btree type %u",
432 r = &c->btree_roots[entry->btree_id];
435 r->level = entry->level;
436 bkey_copy(&r->key, &entry->start[0]);
444 case BCH_JSET_ENTRY_usage: {
445 struct jset_entry_usage *u =
446 container_of(entry, struct jset_entry_usage, entry);
448 switch (entry->btree_id) {
449 case FS_USAGE_RESERVED:
450 if (entry->level < BCH_REPLICAS_MAX)
451 c->usage_base->persistent_reserved[entry->level] =
454 case FS_USAGE_INODES:
455 c->usage_base->nr_inodes = le64_to_cpu(u->v);
457 case FS_USAGE_KEY_VERSION:
458 atomic64_set(&c->key_version,
465 case BCH_JSET_ENTRY_data_usage: {
466 struct jset_entry_data_usage *u =
467 container_of(entry, struct jset_entry_data_usage, entry);
468 ret = bch2_replicas_set_usage(c, &u->r,
472 case BCH_JSET_ENTRY_blacklist: {
473 struct jset_entry_blacklist *bl_entry =
474 container_of(entry, struct jset_entry_blacklist, entry);
476 ret = bch2_journal_seq_blacklist_add(c,
477 le64_to_cpu(bl_entry->seq),
478 le64_to_cpu(bl_entry->seq) + 1);
481 case BCH_JSET_ENTRY_blacklist_v2: {
482 struct jset_entry_blacklist_v2 *bl_entry =
483 container_of(entry, struct jset_entry_blacklist_v2, entry);
485 ret = bch2_journal_seq_blacklist_add(c,
486 le64_to_cpu(bl_entry->start),
487 le64_to_cpu(bl_entry->end) + 1);
495 static int journal_replay_early(struct bch_fs *c,
496 struct bch_sb_field_clean *clean,
497 struct list_head *journal)
499 struct jset_entry *entry;
503 c->bucket_clock[READ].hand = le16_to_cpu(clean->read_clock);
504 c->bucket_clock[WRITE].hand = le16_to_cpu(clean->write_clock);
506 for (entry = clean->start;
507 entry != vstruct_end(&clean->field);
508 entry = vstruct_next(entry)) {
509 ret = journal_replay_entry_early(c, entry);
514 struct journal_replay *i =
515 list_last_entry(journal, struct journal_replay, list);
517 c->bucket_clock[READ].hand = le16_to_cpu(i->j.read_clock);
518 c->bucket_clock[WRITE].hand = le16_to_cpu(i->j.write_clock);
520 list_for_each_entry(i, journal, list)
521 vstruct_for_each(&i->j, entry) {
522 ret = journal_replay_entry_early(c, entry);
528 bch2_fs_usage_initialize(c);
533 /* sb clean section: */
535 static struct bkey_i *btree_root_find(struct bch_fs *c,
536 struct bch_sb_field_clean *clean,
538 enum btree_id id, unsigned *level)
541 struct jset_entry *entry, *start, *end;
544 start = clean->start;
545 end = vstruct_end(&clean->field);
548 end = vstruct_last(j);
551 for (entry = start; entry < end; entry = vstruct_next(entry))
552 if (entry->type == BCH_JSET_ENTRY_btree_root &&
553 entry->btree_id == id)
559 return ERR_PTR(-EINVAL);
562 *level = entry->level;
566 static int verify_superblock_clean(struct bch_fs *c,
567 struct bch_sb_field_clean **cleanp,
571 struct bch_sb_field_clean *clean = *cleanp;
574 if (!c->sb.clean || !j)
577 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
578 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
579 le64_to_cpu(clean->journal_seq),
580 le64_to_cpu(j->seq))) {
586 mustfix_fsck_err_on(j->read_clock != clean->read_clock, c,
587 "superblock read clock doesn't match journal after clean shutdown");
588 mustfix_fsck_err_on(j->write_clock != clean->write_clock, c,
589 "superblock read clock doesn't match journal after clean shutdown");
591 for (i = 0; i < BTREE_ID_NR; i++) {
592 struct bkey_i *k1, *k2;
593 unsigned l1 = 0, l2 = 0;
595 k1 = btree_root_find(c, clean, NULL, i, &l1);
596 k2 = btree_root_find(c, NULL, j, i, &l2);
601 mustfix_fsck_err_on(!k1 || !k2 ||
604 k1->k.u64s != k2->k.u64s ||
605 memcmp(k1, k2, bkey_bytes(k1)) ||
607 "superblock btree root doesn't match journal after clean shutdown");
613 static struct bch_sb_field_clean *read_superblock_clean(struct bch_fs *c)
615 struct bch_sb_field_clean *clean, *sb_clean;
618 mutex_lock(&c->sb_lock);
619 sb_clean = bch2_sb_get_clean(c->disk_sb.sb);
621 if (fsck_err_on(!sb_clean, c,
622 "superblock marked clean but clean section not present")) {
623 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
625 mutex_unlock(&c->sb_lock);
629 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
632 mutex_unlock(&c->sb_lock);
633 return ERR_PTR(-ENOMEM);
636 if (le16_to_cpu(c->disk_sb.sb->version) <
637 bcachefs_metadata_version_bkey_renumber)
638 bch2_sb_clean_renumber(clean, READ);
640 mutex_unlock(&c->sb_lock);
644 mutex_unlock(&c->sb_lock);
648 static int read_btree_roots(struct bch_fs *c)
653 for (i = 0; i < BTREE_ID_NR; i++) {
654 struct btree_root *r = &c->btree_roots[i];
659 if (i == BTREE_ID_ALLOC &&
660 c->opts.reconstruct_alloc) {
661 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
667 __fsck_err(c, i == BTREE_ID_ALLOC
668 ? FSCK_CAN_IGNORE : 0,
669 "invalid btree root %s",
671 if (i == BTREE_ID_ALLOC)
672 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
675 ret = bch2_btree_root_read(c, i, &r->key, r->level);
677 __fsck_err(c, i == BTREE_ID_ALLOC
678 ? FSCK_CAN_IGNORE : 0,
679 "error reading btree root %s",
681 if (i == BTREE_ID_ALLOC)
682 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
686 for (i = 0; i < BTREE_ID_NR; i++)
687 if (!c->btree_roots[i].b)
688 bch2_btree_root_alloc(c, i);
693 int bch2_fs_recovery(struct bch_fs *c)
695 const char *err = "cannot allocate memory";
696 struct bch_sb_field_clean *clean = NULL;
698 LIST_HEAD(journal_entries);
699 struct journal_keys journal_keys = { NULL };
700 bool wrote = false, write_sb = false;
704 clean = read_superblock_clean(c);
705 ret = PTR_ERR_OR_ZERO(clean);
710 bch_info(c, "recovering from clean shutdown, journal seq %llu",
711 le64_to_cpu(clean->journal_seq));
713 if (!c->replicas.entries) {
714 bch_info(c, "building replicas info");
715 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
718 if (!c->sb.clean || c->opts.fsck) {
721 ret = bch2_journal_read(c, &journal_entries);
725 if (mustfix_fsck_err_on(c->sb.clean && !journal_empty(&journal_entries), c,
726 "filesystem marked clean but journal not empty")) {
727 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
728 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
732 if (!c->sb.clean && list_empty(&journal_entries)) {
733 bch_err(c, "no journal entries found");
734 ret = BCH_FSCK_REPAIR_IMPOSSIBLE;
738 journal_keys = journal_keys_sort(&journal_entries);
739 if (!journal_keys.d) {
744 j = &list_last_entry(&journal_entries,
745 struct journal_replay, list)->j;
747 ret = verify_superblock_clean(c, &clean, j);
751 journal_seq = le64_to_cpu(j->seq) + 1;
753 journal_seq = le64_to_cpu(clean->journal_seq) + 1;
756 ret = journal_replay_early(c, clean, &journal_entries);
761 ret = bch2_journal_seq_blacklist_add(c,
765 bch_err(c, "error creating new journal seq blacklist entry");
772 ret = bch2_blacklist_table_initialize(c);
774 if (!list_empty(&journal_entries)) {
775 ret = verify_journal_entries_not_blacklisted_or_missing(c,
781 ret = bch2_fs_journal_start(&c->journal, journal_seq,
786 ret = read_btree_roots(c);
790 bch_verbose(c, "starting alloc read");
791 err = "error reading allocation information";
792 ret = bch2_alloc_read(c, &journal_keys);
795 bch_verbose(c, "alloc read done");
797 bch_verbose(c, "starting stripes_read");
798 err = "error reading stripes";
799 ret = bch2_stripes_read(c, &journal_keys);
802 bch_verbose(c, "stripes_read done");
804 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
806 if ((c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) &&
807 !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_METADATA))) {
809 * interior btree node updates aren't consistent with the
810 * journal; after an unclean shutdown we have to walk all
811 * pointers to metadata:
813 bch_info(c, "starting metadata mark and sweep");
814 err = "error in mark and sweep";
815 ret = bch2_gc(c, NULL, true, true);
818 bch_verbose(c, "mark and sweep done");
822 !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) ||
823 test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags)) {
824 bch_info(c, "starting mark and sweep");
825 err = "error in mark and sweep";
826 ret = bch2_gc(c, &journal_keys, true, false);
829 bch_verbose(c, "mark and sweep done");
832 clear_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
833 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
836 * Skip past versions that might have possibly been used (as nonces),
837 * but hadn't had their pointers written:
839 if (c->sb.encryption_type && !c->sb.clean)
840 atomic64_add(1 << 16, &c->key_version);
842 if (c->opts.norecovery)
845 bch_verbose(c, "starting journal replay");
846 err = "journal replay failed";
847 ret = bch2_journal_replay(c, journal_keys);
850 bch_verbose(c, "journal replay done");
852 if (!c->opts.nochanges) {
854 * note that even when filesystem was clean there might be work
855 * to do here, if we ran gc (because of fsck) which recalculated
858 bch_verbose(c, "writing allocation info");
859 err = "error writing out alloc info";
860 ret = bch2_stripes_write(c, BTREE_INSERT_LAZY_RW, &wrote) ?:
861 bch2_alloc_write(c, BTREE_INSERT_LAZY_RW, &wrote);
863 bch_err(c, "error writing alloc info");
866 bch_verbose(c, "alloc write done");
868 set_bit(BCH_FS_ALLOC_WRITTEN, &c->flags);
872 if (!(c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
873 bch_info(c, "checking inode link counts");
874 err = "error in recovery";
875 ret = bch2_fsck_inode_nlink(c);
878 bch_verbose(c, "check inodes done");
881 bch_verbose(c, "checking for deleted inodes");
882 err = "error in recovery";
883 ret = bch2_fsck_walk_inodes_only(c);
886 bch_verbose(c, "check inodes done");
891 bch_info(c, "starting fsck");
892 err = "error in fsck";
893 ret = bch2_fsck_full(c);
896 bch_verbose(c, "fsck done");
899 if (enabled_qtypes(c)) {
900 bch_verbose(c, "reading quotas");
901 ret = bch2_fs_quota_read(c);
904 bch_verbose(c, "quotas done");
907 mutex_lock(&c->sb_lock);
908 if (c->opts.version_upgrade) {
909 if (c->sb.version < bcachefs_metadata_version_new_versioning)
910 c->disk_sb.sb->version_min =
911 le16_to_cpu(bcachefs_metadata_version_min);
912 c->disk_sb.sb->version = le16_to_cpu(bcachefs_metadata_version_current);
916 if (!(c->sb.features & (1ULL << BCH_FEATURE_INLINE_DATA))) {
917 c->disk_sb.sb->features[0] |=
918 cpu_to_le64(1ULL << BCH_FEATURE_INLINE_DATA);
922 if (!test_bit(BCH_FS_ERROR, &c->flags)) {
923 c->disk_sb.sb->compat[0] |= 1ULL << BCH_COMPAT_FEAT_ALLOC_INFO;
928 !test_bit(BCH_FS_ERROR, &c->flags)) {
929 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_ATOMIC_NLINK;
930 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
936 mutex_unlock(&c->sb_lock);
938 if (c->journal_seq_blacklist_table &&
939 c->journal_seq_blacklist_table->nr > 128)
940 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
945 set_bit(BCH_FS_FSCK_DONE, &c->flags);
946 bch2_flush_fsck_errs(c);
948 journal_keys_free(&journal_keys);
949 journal_entries_free(&journal_entries);
952 bch_err(c, "Error in recovery: %s (%i)", err, ret);
954 bch_verbose(c, "ret %i", ret);
958 int bch2_fs_initialize(struct bch_fs *c)
960 struct bch_inode_unpacked root_inode, lostfound_inode;
961 struct bkey_inode_buf packed_inode;
962 struct qstr lostfound = QSTR("lost+found");
963 const char *err = "cannot allocate memory";
969 bch_notice(c, "initializing new filesystem");
971 mutex_lock(&c->sb_lock);
972 for_each_online_member(ca, c, i)
973 bch2_mark_dev_superblock(c, ca, 0);
974 mutex_unlock(&c->sb_lock);
976 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
977 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
979 for (i = 0; i < BTREE_ID_NR; i++)
980 bch2_btree_root_alloc(c, i);
982 err = "unable to allocate journal buckets";
983 for_each_online_member(ca, c, i) {
984 ret = bch2_dev_journal_alloc(ca);
986 percpu_ref_put(&ca->io_ref);
992 * journal_res_get() will crash if called before this has
993 * set up the journal.pin FIFO and journal.cur pointer:
995 bch2_fs_journal_start(&c->journal, 1, &journal);
996 bch2_journal_set_replay_done(&c->journal);
998 err = "error going read write";
999 ret = __bch2_fs_read_write(c, true);
1003 bch2_inode_init(c, &root_inode, 0, 0,
1004 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
1005 root_inode.bi_inum = BCACHEFS_ROOT_INO;
1006 bch2_inode_pack(&packed_inode, &root_inode);
1008 err = "error creating root directory";
1009 ret = bch2_btree_insert(c, BTREE_ID_INODES,
1010 &packed_inode.inode.k_i,
1015 bch2_inode_init_early(c, &lostfound_inode);
1017 err = "error creating lost+found";
1018 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
1019 bch2_create_trans(&trans, BCACHEFS_ROOT_INO,
1020 &root_inode, &lostfound_inode,
1022 0, 0, S_IFDIR|0700, 0,
1027 if (enabled_qtypes(c)) {
1028 ret = bch2_fs_quota_read(c);
1033 err = "error writing first journal entry";
1034 ret = bch2_journal_meta(&c->journal);
1038 mutex_lock(&c->sb_lock);
1039 c->disk_sb.sb->version = c->disk_sb.sb->version_min =
1040 le16_to_cpu(bcachefs_metadata_version_current);
1041 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_ATOMIC_NLINK;
1043 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1044 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1046 bch2_write_super(c);
1047 mutex_unlock(&c->sb_lock);
1051 pr_err("Error initializing new filesystem: %s (%i)", err, ret);