1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
6 #include "btree_update.h"
7 #include "btree_update_interior.h"
12 #include "journal_io.h"
17 #include <linux/stat.h>
19 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
21 struct bkey_i *btree_root_find(struct bch_fs *c,
22 struct bch_sb_field_clean *clean,
24 enum btree_id id, unsigned *level)
27 struct jset_entry *entry, *start, *end;
31 end = vstruct_end(&clean->field);
34 end = vstruct_last(j);
37 for (entry = start; entry < end; entry = vstruct_next(entry))
38 if (entry->type == BCH_JSET_ENTRY_btree_root &&
39 entry->btree_id == id)
45 return ERR_PTR(-EINVAL);
48 *level = entry->level;
52 static int verify_superblock_clean(struct bch_fs *c,
53 struct bch_sb_field_clean *clean,
62 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
63 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
64 le64_to_cpu(clean->journal_seq),
66 bch2_fs_mark_clean(c, false);
68 mustfix_fsck_err_on(j->read_clock != clean->read_clock, c,
69 "superblock read clock doesn't match journal after clean shutdown");
70 mustfix_fsck_err_on(j->write_clock != clean->write_clock, c,
71 "superblock read clock doesn't match journal after clean shutdown");
73 for (i = 0; i < BTREE_ID_NR; i++) {
74 struct bkey_i *k1, *k2;
75 unsigned l1 = 0, l2 = 0;
77 k1 = btree_root_find(c, clean, NULL, i, &l1);
78 k2 = btree_root_find(c, NULL, j, i, &l2);
83 mustfix_fsck_err_on(!k1 || !k2 ||
86 k1->k.u64s != k2->k.u64s ||
87 memcmp(k1, k2, bkey_bytes(k1)) ||
89 "superblock btree root doesn't match journal after clean shutdown");
95 static bool journal_empty(struct list_head *journal)
97 struct journal_replay *i;
98 struct jset_entry *entry;
100 if (list_empty(journal))
103 i = list_last_entry(journal, struct journal_replay, list);
105 if (i->j.last_seq != i->j.seq)
108 list_for_each_entry(i, journal, list) {
109 vstruct_for_each(&i->j, entry) {
110 if (entry->type == BCH_JSET_ENTRY_btree_root)
113 if (entry->type == BCH_JSET_ENTRY_btree_keys &&
123 int bch2_fs_recovery(struct bch_fs *c)
125 const char *err = "cannot allocate memory";
126 struct bch_sb_field_clean *clean = NULL, *sb_clean = NULL;
128 struct jset *j = NULL;
132 mutex_lock(&c->sb_lock);
133 if (!bch2_sb_get_replicas(c->disk_sb.sb)) {
134 bch_info(c, "building replicas info");
135 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
139 sb_clean = bch2_sb_get_clean(c->disk_sb.sb);
141 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
145 mutex_unlock(&c->sb_lock);
149 mutex_unlock(&c->sb_lock);
152 bch_info(c, "recovering from clean shutdown, journal seq %llu",
153 le64_to_cpu(clean->journal_seq));
155 if (!clean || c->opts.fsck) {
156 ret = bch2_journal_read(c, &journal);
160 j = &list_entry(journal.prev, struct journal_replay, list)->j;
162 ret = bch2_journal_set_seq(c,
163 le64_to_cpu(clean->journal_seq),
164 le64_to_cpu(clean->journal_seq));
168 ret = verify_superblock_clean(c, clean, j);
172 fsck_err_on(clean && !journal_empty(&journal), c,
173 "filesystem marked clean but journal not empty");
176 c->bucket_clock[READ].hand = le16_to_cpu(clean->read_clock);
177 c->bucket_clock[WRITE].hand = le16_to_cpu(clean->write_clock);
179 c->bucket_clock[READ].hand = le16_to_cpu(j->read_clock);
180 c->bucket_clock[WRITE].hand = le16_to_cpu(j->write_clock);
183 for (i = 0; i < BTREE_ID_NR; i++) {
187 k = btree_root_find(c, clean, j, i, &level);
191 err = "invalid btree root pointer";
195 err = "error reading btree root";
196 if (bch2_btree_root_read(c, i, k, level)) {
197 if (i != BTREE_ID_ALLOC)
200 mustfix_fsck_err(c, "error reading btree root");
204 for (i = 0; i < BTREE_ID_NR; i++)
205 if (!c->btree_roots[i].b)
206 bch2_btree_root_alloc(c, i);
208 err = "error reading allocation information";
209 ret = bch2_alloc_read(c, &journal);
213 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
215 bch_verbose(c, "starting mark and sweep:");
216 err = "error in recovery";
217 ret = bch2_initial_gc(c, &journal);
220 bch_verbose(c, "mark and sweep done");
222 clear_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
224 if (c->opts.noreplay)
228 * Mark dirty before journal replay, fsck:
229 * XXX: after a clean shutdown, this could be done lazily only when fsck
232 bch2_fs_mark_clean(c, false);
235 * bch2_fs_journal_start() can't happen sooner, or btree_gc_finish()
236 * will give spurious errors about oldest_gen > bucket_gen -
237 * this is a hack but oh well.
239 bch2_fs_journal_start(&c->journal);
241 err = "error starting allocator";
242 ret = bch2_fs_allocator_start(c);
246 bch_verbose(c, "starting journal replay:");
247 err = "journal replay failed";
248 ret = bch2_journal_replay(c, &journal);
251 bch_verbose(c, "journal replay done");
253 if (c->opts.norecovery)
256 err = "error in fsck";
261 if (!test_bit(BCH_FS_FSCK_UNFIXED_ERRORS, &c->flags)) {
262 mutex_lock(&c->sb_lock);
263 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_ATOMIC_NLINK;
264 mutex_unlock(&c->sb_lock);
267 if (enabled_qtypes(c)) {
268 bch_verbose(c, "reading quotas:");
269 ret = bch2_fs_quota_read(c);
272 bch_verbose(c, "quotas done");
276 bch2_journal_entries_free(&journal);
281 pr_err("Error in recovery: %s (%i)", err, ret);
285 int bch2_fs_initialize(struct bch_fs *c)
287 struct bch_inode_unpacked root_inode, lostfound_inode;
288 struct bkey_inode_buf packed_inode;
289 struct bch_hash_info root_hash_info;
290 struct qstr lostfound = QSTR("lost+found");
291 const char *err = "cannot allocate memory";
297 bch_notice(c, "initializing new filesystem");
299 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
301 ret = bch2_initial_gc(c, &journal);
305 err = "unable to allocate journal buckets";
306 for_each_online_member(ca, c, i)
307 if (bch2_dev_journal_alloc(ca)) {
308 percpu_ref_put(&ca->io_ref);
312 for (i = 0; i < BTREE_ID_NR; i++)
313 bch2_btree_root_alloc(c, i);
316 * journal_res_get() will crash if called before this has
317 * set up the journal.pin FIFO and journal.cur pointer:
319 bch2_fs_journal_start(&c->journal);
320 bch2_journal_set_replay_done(&c->journal);
322 err = "error starting allocator";
323 ret = bch2_fs_allocator_start(c);
327 bch2_inode_init(c, &root_inode, 0, 0,
328 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
329 root_inode.bi_inum = BCACHEFS_ROOT_INO;
330 root_inode.bi_nlink++; /* lost+found */
331 bch2_inode_pack(&packed_inode, &root_inode);
333 err = "error creating root directory";
334 ret = bch2_btree_insert(c, BTREE_ID_INODES,
335 &packed_inode.inode.k_i,
340 bch2_inode_init(c, &lostfound_inode, 0, 0,
341 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0,
343 lostfound_inode.bi_inum = BCACHEFS_ROOT_INO + 1;
344 bch2_inode_pack(&packed_inode, &lostfound_inode);
346 err = "error creating lost+found";
347 ret = bch2_btree_insert(c, BTREE_ID_INODES,
348 &packed_inode.inode.k_i,
353 root_hash_info = bch2_hash_info_init(c, &root_inode);
355 ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
356 &lostfound, lostfound_inode.bi_inum, NULL,
357 BTREE_INSERT_NOFAIL);
361 atomic_long_set(&c->nr_inodes, 2);
363 if (enabled_qtypes(c)) {
364 ret = bch2_fs_quota_read(c);
369 err = "error writing first journal entry";
370 ret = bch2_journal_meta(&c->journal);
374 mutex_lock(&c->sb_lock);
375 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
376 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
377 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_ATOMIC_NLINK;
380 mutex_unlock(&c->sb_lock);
384 pr_err("Error initializing new filesystem: %s (%i)", err, ret);