]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/recovery.c
Update bcachefs sources to 72405e7ff8 bcachefs: Fix bch2_check_extents_to_backpointers()
[bcachefs-tools-debian] / libbcachefs / recovery.c
index af2628f43582ee3de736bc264916ba86bafc0eb7..aafe4054d25def18426fa42126bc13c902d8ebd8 100644 (file)
@@ -129,12 +129,12 @@ search:
        if (!*idx)
                *idx = __bch2_journal_key_search(keys, btree_id, level, pos);
 
-       while (*idx < keys->nr &&
-              (k = idx_to_key(keys, *idx),
-               k->btree_id == btree_id &&
-               k->level == level &&
-               bpos_le(k->k->k.p, end_pos))) {
-               if (bpos_ge(k->k->k.p, pos) && !k->overwritten)
+       while ((k = *idx < keys->nr ? idx_to_key(keys, *idx) : NULL)) {
+               if (__journal_key_cmp(btree_id, level, end_pos, k) < 0)
+                       return NULL;
+
+               if (__journal_key_cmp(btree_id, level, pos, k) <= 0 &&
+                   !k->overwritten)
                        return k->k;
 
                (*idx)++;
@@ -481,7 +481,7 @@ static int journal_keys_sort(struct bch_fs *c)
        struct genradix_iter iter;
        struct journal_replay *i, **_i;
        struct jset_entry *entry;
-       struct bkey_i *k, *_n;
+       struct bkey_i *k;
        struct journal_keys *keys = &c->journal_keys;
        struct journal_key *src, *dst;
        size_t nr_keys = 0;
@@ -492,7 +492,7 @@ static int journal_keys_sort(struct bch_fs *c)
                if (!i || i->ignore)
                        continue;
 
-               for_each_jset_key(k, _n, entry, &i->j)
+               for_each_jset_key(k, entry, &i->j)
                        nr_keys++;
        }
 
@@ -511,7 +511,7 @@ static int journal_keys_sort(struct bch_fs *c)
                if (!i || i->ignore)
                        continue;
 
-               for_each_jset_key(k, _n, entry, &i->j)
+               for_each_jset_key(k, entry, &i->j)
                        keys->d[keys->nr++] = (struct journal_key) {
                                .btree_id       = entry->btree_id,
                                .level          = entry->level,
@@ -588,7 +588,7 @@ static int journal_sort_seq_cmp(const void *_l, const void *_r)
        return cmp_int(l->journal_seq, r->journal_seq);
 }
 
-static int bch2_journal_replay(struct bch_fs *c)
+static int bch2_journal_replay(struct bch_fs *c, u64 start_seq, u64 end_seq)
 {
        struct journal_keys *keys = &c->journal_keys;
        struct journal_key **keys_sorted, *k;
@@ -610,6 +610,13 @@ static int bch2_journal_replay(struct bch_fs *c)
             sizeof(keys_sorted[0]),
             journal_sort_seq_cmp, NULL);
 
+       if (keys->nr) {
+               ret = bch2_fs_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
+                                     keys->nr, start_seq, end_seq);
+               if (ret)
+                       goto err;
+       }
+
        for (i = 0; i < keys->nr; i++) {
                k = keys_sorted[i];
 
@@ -625,8 +632,8 @@ static int bch2_journal_replay(struct bch_fs *c)
                                     : 0),
                             bch2_journal_replay_key(&trans, k));
                if (ret) {
-                       bch_err(c, "journal replay: error %d while replaying key at btree %s level %u",
-                               ret, bch2_btree_ids[k->btree_id], k->level);
+                       bch_err(c, "journal replay: error while replaying key at btree %s level %u: %s",
+                               bch2_btree_ids[k->btree_id], k->level, bch2_err_str(ret));
                        goto err;
                }
        }
@@ -639,7 +646,7 @@ static int bch2_journal_replay(struct bch_fs *c)
        ret = bch2_journal_error(j);
 
        if (keys->nr && !ret)
-               bch2_journal_log_msg(&c->journal, "journal replay finished");
+               bch2_fs_log_msg(c, "journal replay finished");
 err:
        kvfree(keys_sorted);
        return ret;
@@ -864,7 +871,7 @@ static int verify_superblock_clean(struct bch_fs *c,
                                    IS_ERR(k1) ||
                                    IS_ERR(k2) ||
                                    k1->k.u64s != k2->k.u64s ||
-                                   memcmp(k1, k2, bkey_bytes(k1)) ||
+                                   memcmp(k1, k2, bkey_bytes(&k1->k)) ||
                                    l1 != l2, c,
                        "superblock btree root %u doesn't match journal after clean shutdown\n"
                        "sb:      l=%u %s\n"
@@ -922,6 +929,7 @@ static bool btree_id_is_alloc(enum btree_id id)
        case BTREE_ID_backpointers:
        case BTREE_ID_need_discard:
        case BTREE_ID_freespace:
+       case BTREE_ID_bucket_gens:
                return true;
        default:
                return false;
@@ -961,14 +969,20 @@ static int read_btree_roots(struct bch_fs *c)
                                   ? FSCK_CAN_IGNORE : 0,
                                   "error reading btree root %s",
                                   bch2_btree_ids[i]);
-                       if (i == BTREE_ID_alloc)
+                       if (btree_id_is_alloc(i))
                                c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info);
                }
        }
 
-       for (i = 0; i < BTREE_ID_NR; i++)
-               if (!c->btree_roots[i].b)
+       for (i = 0; i < BTREE_ID_NR; i++) {
+               struct btree_root *r = &c->btree_roots[i];
+
+               if (!r->b) {
+                       r->alive = false;
+                       r->level = 0;
                        bch2_btree_root_alloc(c, i);
+               }
+       }
 fsck_err:
        return ret;
 }
@@ -1044,7 +1058,7 @@ int bch2_fs_recovery(struct bch_fs *c)
        const char *err = "cannot allocate memory";
        struct bch_sb_field_clean *clean = NULL;
        struct jset *last_journal_entry = NULL;
-       u64 blacklist_seq, journal_seq;
+       u64 last_seq, blacklist_seq, journal_seq;
        bool write_sb = false;
        int ret = 0;
 
@@ -1086,13 +1100,13 @@ int bch2_fs_recovery(struct bch_fs *c)
        }
 
        if (!c->opts.nochanges) {
-               if (c->sb.version < bcachefs_metadata_version_backpointers) {
+               if (c->sb.version < bcachefs_metadata_version_lru_v2) {
                        bch_info(c, "version prior to backpointers, upgrade and fsck required");
                        c->opts.version_upgrade = true;
                        c->opts.fsck            = true;
                        c->opts.fix_errors      = FSCK_OPT_YES;
-               } else if (c->sb.version < bcachefs_metadata_version_inode_v3) {
-                       bch_info(c, "version prior to inode_v3, upgrade required");
+               } else if (c->sb.version < bcachefs_metadata_version_fragmentation_lru) {
+                       bch_info(c, "version prior to backpointers, upgrade required");
                        c->opts.version_upgrade = true;
                }
        }
@@ -1114,7 +1128,7 @@ int bch2_fs_recovery(struct bch_fs *c)
                struct journal_replay **i;
 
                bch_verbose(c, "starting journal read");
-               ret = bch2_journal_read(c, &blacklist_seq, &journal_seq);
+               ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
                if (ret)
                        goto err;
 
@@ -1142,7 +1156,15 @@ int bch2_fs_recovery(struct bch_fs *c)
 
                if (!last_journal_entry) {
                        fsck_err_on(!c->sb.clean, c, "no journal entries found");
-                       goto use_clean;
+                       if (clean)
+                               goto use_clean;
+
+                       genradix_for_each_reverse(&c->journal_entries, iter, i)
+                               if (*i) {
+                                       last_journal_entry = &(*i)->j;
+                                       (*i)->ignore = false;
+                                       break;
+                               }
                }
 
                ret = journal_keys_sort(c);
@@ -1188,7 +1210,9 @@ use_clean:
                journal_seq += 8;
 
        if (blacklist_seq != journal_seq) {
-               ret = bch2_journal_seq_blacklist_add(c,
+               ret =   bch2_fs_log_msg(c, "blacklisting entries %llu-%llu",
+                                       blacklist_seq, journal_seq) ?:
+                       bch2_journal_seq_blacklist_add(c,
                                        blacklist_seq, journal_seq);
                if (ret) {
                        bch_err(c, "error creating new journal seq blacklist entry");
@@ -1196,10 +1220,15 @@ use_clean:
                }
        }
 
-       ret = bch2_fs_journal_start(&c->journal, journal_seq);
+       ret =   bch2_fs_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
+                               journal_seq, last_seq, blacklist_seq - 1) ?:
+               bch2_fs_journal_start(&c->journal, journal_seq);
        if (ret)
                goto err;
 
+       if (c->opts.reconstruct_alloc)
+               bch2_fs_log_msg(c, "dropping alloc info");
+
        /*
         * Skip past versions that might have possibly been used (as nonces),
         * but hadn't had their pointers written:
@@ -1215,7 +1244,9 @@ use_clean:
        err = "error reading allocation information";
 
        down_read(&c->gc_lock);
-       ret = bch2_alloc_read(c);
+       ret = c->sb.version < bcachefs_metadata_version_bucket_gens
+               ? bch2_alloc_read(c)
+               : bch2_bucket_gens_read(c);
        up_read(&c->gc_lock);
 
        if (ret)
@@ -1229,7 +1260,19 @@ use_clean:
                goto err;
        bch_verbose(c, "stripes_read done");
 
-       bch2_stripes_heap_start(c);
+       if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
+               err = "error creating root snapshot node";
+               ret = bch2_fs_initialize_subvolumes(c);
+               if (ret)
+                       goto err;
+       }
+
+       bch_verbose(c, "reading snapshots table");
+       err = "error reading snapshots table";
+       ret = bch2_fs_snapshots_start(c);
+       if (ret)
+               goto err;
+       bch_verbose(c, "reading snapshots done");
 
        if (c->opts.fsck) {
                bool metadata_only = c->opts.norecovery;
@@ -1243,37 +1286,25 @@ use_clean:
 
                set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
 
-               bch_info(c, "checking need_discard and freespace btrees");
-               err = "error checking need_discard and freespace btrees";
-               ret = bch2_check_alloc_info(c);
-               if (ret)
-                       goto err;
-               bch_verbose(c, "done checking need_discard and freespace btrees");
-
-               if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
-                       err = "error creating root snapshot node";
-                       ret = bch2_fs_initialize_subvolumes(c);
-                       if (ret)
-                               goto err;
-               }
-
-               bch_verbose(c, "reading snapshots table");
-               err = "error reading snapshots table";
-               ret = bch2_fs_snapshots_start(c);
-               if (ret)
-                       goto err;
-               bch_verbose(c, "reading snapshots done");
-
                set_bit(BCH_FS_MAY_GO_RW, &c->flags);
 
                bch_info(c, "starting journal replay, %zu keys", c->journal_keys.nr);
                err = "journal replay failed";
-               ret = bch2_journal_replay(c);
+               ret = bch2_journal_replay(c, last_seq, blacklist_seq - 1);
                if (ret)
                        goto err;
                if (c->opts.verbose || !c->sb.clean)
                        bch_info(c, "journal replay done");
 
+               bch_info(c, "checking need_discard and freespace btrees");
+               err = "error checking need_discard and freespace btrees";
+               ret = bch2_check_alloc_info(c);
+               if (ret)
+                       goto err;
+               bch_verbose(c, "done checking need_discard and freespace btrees");
+
+               set_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags);
+
                bch_info(c, "checking lrus");
                err = "error checking lrus";
                ret = bch2_check_lrus(c);
@@ -1313,6 +1344,7 @@ use_clean:
                set_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags);
        } else {
                set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
+               set_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags);
                set_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags);
                set_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags);
                set_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags);
@@ -1321,25 +1353,11 @@ use_clean:
                if (c->opts.norecovery)
                        goto out;
 
-               if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
-                       err = "error creating root snapshot node";
-                       ret = bch2_fs_initialize_subvolumes(c);
-                       if (ret)
-                               goto err;
-               }
-
-               bch_verbose(c, "reading snapshots table");
-               err = "error reading snapshots table";
-               ret = bch2_fs_snapshots_start(c);
-               if (ret)
-                       goto err;
-               bch_verbose(c, "reading snapshots done");
-
                set_bit(BCH_FS_MAY_GO_RW, &c->flags);
 
                bch_verbose(c, "starting journal replay, %zu keys", c->journal_keys.nr);
                err = "journal replay failed";
-               ret = bch2_journal_replay(c);
+               ret = bch2_journal_replay(c, last_seq, blacklist_seq - 1);
                if (ret)
                        goto err;
                if (c->opts.verbose || !c->sb.clean)
@@ -1351,6 +1369,16 @@ use_clean:
        if (ret)
                goto err;
 
+       if (c->sb.version < bcachefs_metadata_version_bucket_gens &&
+           c->opts.version_upgrade) {
+               bch_info(c, "initializing bucket_gens");
+               err = "error initializing bucket gens";
+               ret = bch2_bucket_gens_init(c);
+               if (ret)
+                       goto err;
+               bch_verbose(c, "bucket_gens init done");
+       }
+
        if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
                /* set bi_subvol on root inode */
                err = "error upgrade root inode for subvolumes";
@@ -1600,6 +1628,6 @@ int bch2_fs_initialize(struct bch_fs *c)
 
        return 0;
 err:
-       pr_err("Error initializing new filesystem: %s (%i)", err, ret);
+       pr_err("Error initializing new filesystem: %s (%s)", err, bch2_err_str(ret));
        return ret;
 }