5 #include "btree_update.h"
6 #include "btree_update_interior.h"
11 #include "journal_io.h"
16 #include <linux/stat.h>
18 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
20 struct bkey_i *btree_root_find(struct bch_fs *c,
21 struct bch_sb_field_clean *clean,
23 enum btree_id id, unsigned *level)
26 struct jset_entry *entry, *start, *end;
30 end = vstruct_end(&clean->field);
33 end = vstruct_last(j);
36 for (entry = start; entry < end; entry = vstruct_next(entry))
37 if (entry->type == BCH_JSET_ENTRY_btree_root &&
38 entry->btree_id == id)
44 return ERR_PTR(-EINVAL);
47 *level = entry->level;
51 static int verify_superblock_clean(struct bch_fs *c,
52 struct bch_sb_field_clean *clean,
61 if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
62 "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
63 le64_to_cpu(clean->journal_seq),
65 bch2_fs_mark_clean(c, false);
67 mustfix_fsck_err_on(j->read_clock != clean->read_clock, c,
68 "superblock read clock doesn't match journal after clean shutdown");
69 mustfix_fsck_err_on(j->write_clock != clean->write_clock, c,
70 "superblock read clock doesn't match journal after clean shutdown");
72 for (i = 0; i < BTREE_ID_NR; i++) {
73 struct bkey_i *k1, *k2;
74 unsigned l1 = 0, l2 = 0;
76 k1 = btree_root_find(c, clean, NULL, i, &l1);
77 k2 = btree_root_find(c, NULL, j, i, &l2);
82 mustfix_fsck_err_on(!k1 || !k2 ||
85 k1->k.u64s != k2->k.u64s ||
86 memcmp(k1, k2, bkey_bytes(k1)) ||
88 "superblock btree root doesn't match journal after clean shutdown");
94 static bool journal_empty(struct list_head *journal)
96 struct journal_replay *i;
97 struct jset_entry *entry;
99 if (list_empty(journal))
102 i = list_last_entry(journal, struct journal_replay, list);
104 if (i->j.last_seq != i->j.seq)
107 list_for_each_entry(i, journal, list) {
108 vstruct_for_each(&i->j, entry) {
109 if (entry->type == BCH_JSET_ENTRY_btree_root)
112 if (entry->type == BCH_JSET_ENTRY_btree_keys &&
122 int bch2_fs_recovery(struct bch_fs *c)
124 const char *err = "cannot allocate memory";
125 struct bch_sb_field_clean *clean = NULL, *sb_clean = NULL;
127 struct jset *j = NULL;
131 mutex_lock(&c->sb_lock);
132 if (!bch2_sb_get_replicas(c->disk_sb.sb)) {
133 bch_info(c, "building replicas info");
134 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
138 sb_clean = bch2_sb_get_clean(c->disk_sb.sb);
140 clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
144 mutex_unlock(&c->sb_lock);
148 mutex_unlock(&c->sb_lock);
151 bch_info(c, "recovering from clean shutdown, journal seq %llu",
152 le64_to_cpu(clean->journal_seq));
154 if (!clean || !c->opts.nofsck) {
155 ret = bch2_journal_read(c, &journal);
159 j = &list_entry(journal.prev, struct journal_replay, list)->j;
161 ret = bch2_journal_set_seq(c,
162 le64_to_cpu(clean->journal_seq),
163 le64_to_cpu(clean->journal_seq));
167 ret = verify_superblock_clean(c, clean, j);
171 fsck_err_on(clean && !journal_empty(&journal), c,
172 "filesystem marked clean but journal not empty");
175 c->bucket_clock[READ].hand = le16_to_cpu(clean->read_clock);
176 c->bucket_clock[WRITE].hand = le16_to_cpu(clean->write_clock);
178 c->bucket_clock[READ].hand = le16_to_cpu(j->read_clock);
179 c->bucket_clock[WRITE].hand = le16_to_cpu(j->write_clock);
182 for (i = 0; i < BTREE_ID_NR; i++) {
186 k = btree_root_find(c, clean, j, i, &level);
190 err = "invalid btree root pointer";
194 err = "error reading btree root";
195 if (bch2_btree_root_read(c, i, k, level)) {
196 if (i != BTREE_ID_ALLOC)
199 mustfix_fsck_err(c, "error reading btree root");
203 for (i = 0; i < BTREE_ID_NR; i++)
204 if (!c->btree_roots[i].b)
205 bch2_btree_root_alloc(c, i);
207 err = "error reading allocation information";
208 ret = bch2_alloc_read(c, &journal);
212 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
214 bch_verbose(c, "starting mark and sweep:");
215 err = "error in recovery";
216 ret = bch2_initial_gc(c, &journal);
219 bch_verbose(c, "mark and sweep done");
221 if (c->opts.noreplay)
225 * Mark dirty before journal replay, fsck:
226 * XXX: after a clean shutdown, this could be done lazily only when fsck
229 bch2_fs_mark_clean(c, false);
232 * bch2_fs_journal_start() can't happen sooner, or btree_gc_finish()
233 * will give spurious errors about oldest_gen > bucket_gen -
234 * this is a hack but oh well.
236 bch2_fs_journal_start(&c->journal);
238 err = "error starting allocator";
239 ret = bch2_fs_allocator_start(c);
243 bch_verbose(c, "starting journal replay:");
244 err = "journal replay failed";
245 ret = bch2_journal_replay(c, &journal);
248 bch_verbose(c, "journal replay done");
250 if (c->opts.norecovery)
253 err = "error in fsck";
258 if (!test_bit(BCH_FS_FSCK_UNFIXED_ERRORS, &c->flags)) {
259 mutex_lock(&c->sb_lock);
260 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_ATOMIC_NLINK;
261 mutex_unlock(&c->sb_lock);
264 if (enabled_qtypes(c)) {
265 bch_verbose(c, "reading quotas:");
266 ret = bch2_fs_quota_read(c);
269 bch_verbose(c, "quotas done");
273 bch2_journal_entries_free(&journal);
282 int bch2_fs_initialize(struct bch_fs *c)
284 struct bch_inode_unpacked root_inode, lostfound_inode;
285 struct bkey_inode_buf packed_inode;
286 struct bch_hash_info root_hash_info;
287 struct qstr lostfound = QSTR("lost+found");
288 const char *err = "cannot allocate memory";
294 bch_notice(c, "initializing new filesystem");
296 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
298 ret = bch2_initial_gc(c, &journal);
302 err = "unable to allocate journal buckets";
303 for_each_online_member(ca, c, i)
304 if (bch2_dev_journal_alloc(ca)) {
305 percpu_ref_put(&ca->io_ref);
309 for (i = 0; i < BTREE_ID_NR; i++)
310 bch2_btree_root_alloc(c, i);
313 * journal_res_get() will crash if called before this has
314 * set up the journal.pin FIFO and journal.cur pointer:
316 bch2_fs_journal_start(&c->journal);
317 bch2_journal_set_replay_done(&c->journal);
319 err = "error starting allocator";
320 ret = bch2_fs_allocator_start(c);
324 bch2_inode_init(c, &root_inode, 0, 0,
325 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
326 root_inode.bi_inum = BCACHEFS_ROOT_INO;
327 root_inode.bi_nlink++; /* lost+found */
328 bch2_inode_pack(&packed_inode, &root_inode);
330 err = "error creating root directory";
331 ret = bch2_btree_insert(c, BTREE_ID_INODES,
332 &packed_inode.inode.k_i,
337 bch2_inode_init(c, &lostfound_inode, 0, 0,
338 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0,
340 lostfound_inode.bi_inum = BCACHEFS_ROOT_INO + 1;
341 bch2_inode_pack(&packed_inode, &lostfound_inode);
343 err = "error creating lost+found";
344 ret = bch2_btree_insert(c, BTREE_ID_INODES,
345 &packed_inode.inode.k_i,
350 root_hash_info = bch2_hash_info_init(c, &root_inode);
352 ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
353 &lostfound, lostfound_inode.bi_inum, NULL,
354 BTREE_INSERT_NOFAIL);
358 atomic_long_set(&c->nr_inodes, 2);
360 if (enabled_qtypes(c)) {
361 ret = bch2_fs_quota_read(c);
366 err = "error writing first journal entry";
367 ret = bch2_journal_meta(&c->journal);
371 mutex_lock(&c->sb_lock);
372 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
373 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
374 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_ATOMIC_NLINK;
377 mutex_unlock(&c->sb_lock);