]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/recovery.c
Update bcachefs sources to 84f132d569 bcachefs: fsck: Break walk_inode() up into...
[bcachefs-tools-debian] / libbcachefs / recovery.c
index 8df94ad5d505594f46b9e132e6bc874df65194e5..09c9d4058f829d4e7395712146074ebd7cab7f3c 100644 (file)
@@ -228,7 +228,7 @@ int bch2_journal_key_insert_take(struct bch_fs *c, enum btree_id id,
                if (!new_keys.d) {
                        bch_err(c, "%s: error allocating new key array (size %zu)",
                                __func__, new_keys.size);
-                       return -ENOMEM;
+                       return -BCH_ERR_ENOMEM_journal_key_insert;
                }
 
                /* Since @keys was full, there was no gap: */
@@ -266,7 +266,7 @@ int bch2_journal_key_insert(struct bch_fs *c, enum btree_id id,
 
        n = kmalloc(bkey_bytes(&k->k), GFP_KERNEL);
        if (!n)
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_journal_key_insert;
 
        bkey_copy(n, k);
        ret = bch2_journal_key_insert_take(c, id, level, n);
@@ -476,15 +476,34 @@ void bch2_journal_keys_free(struct journal_keys *keys)
        keys->nr = keys->gap = keys->size = 0;
 }
 
+static void __journal_keys_sort(struct journal_keys *keys)
+{
+       struct journal_key *src, *dst;
+
+       sort(keys->d, keys->nr, sizeof(keys->d[0]), journal_sort_key_cmp, NULL);
+
+       src = dst = keys->d;
+       while (src < keys->d + keys->nr) {
+               while (src + 1 < keys->d + keys->nr &&
+                      src[0].btree_id  == src[1].btree_id &&
+                      src[0].level     == src[1].level &&
+                      bpos_eq(src[0].k->k.p, src[1].k->k.p))
+                       src++;
+
+               *dst++ = *src++;
+       }
+
+       keys->nr = dst - keys->d;
+}
+
 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;
+       size_t nr_keys = 0, nr_read = 0;
 
        genradix_for_each(&c->journal_entries, iter, _i) {
                i = *_i;
@@ -492,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)
                        nr_keys++;
        }
 
@@ -502,8 +521,21 @@ static int journal_keys_sort(struct bch_fs *c)
        keys->size = roundup_pow_of_two(nr_keys);
 
        keys->d = kvmalloc_array(keys->size, sizeof(keys->d[0]), GFP_KERNEL);
-       if (!keys->d)
-               return -ENOMEM;
+       if (!keys->d) {
+               bch_err(c, "Failed to allocate buffer for sorted journal keys (%zu keys); trying slowpath",
+                       nr_keys);
+
+               do {
+                       keys->size >>= 1;
+                       keys->d = kvmalloc_array(keys->size, sizeof(keys->d[0]), GFP_KERNEL);
+               } while (!keys->d && keys->size > nr_keys / 8);
+
+               if (!keys->d) {
+                       bch_err(c, "Failed to allocate %zu size buffer for sorted journal keys; exiting",
+                               keys->size);
+                       return -BCH_ERR_ENOMEM_journal_keys_sort;
+               }
+       }
 
        genradix_for_each(&c->journal_entries, iter, _i) {
                i = *_i;
@@ -511,7 +543,19 @@ static int journal_keys_sort(struct bch_fs *c)
                if (!i || i->ignore)
                        continue;
 
-               for_each_jset_key(k, _n, entry, &i->j)
+               cond_resched();
+
+               for_each_jset_key(k, entry, &i->j) {
+                       if (keys->nr == keys->size) {
+                               __journal_keys_sort(keys);
+
+                               if (keys->nr > keys->size * 7 / 8) {
+                                       bch_err(c, "Too many journal keys for slowpath; have %zu compacted, buf size %zu, processed %zu/%zu",
+                                               keys->nr, keys->size, nr_read, nr_keys);
+                                       return -BCH_ERR_ENOMEM_journal_keys_sort;
+                               }
+                       }
+
                        keys->d[keys->nr++] = (struct journal_key) {
                                .btree_id       = entry->btree_id,
                                .level          = entry->level,
@@ -519,23 +563,15 @@ static int journal_keys_sort(struct bch_fs *c)
                                .journal_seq    = le64_to_cpu(i->j.seq),
                                .journal_offset = k->_data - i->j._data,
                        };
-       }
-
-       sort(keys->d, keys->nr, sizeof(keys->d[0]), journal_sort_key_cmp, NULL);
-
-       src = dst = keys->d;
-       while (src < keys->d + keys->nr) {
-               while (src + 1 < keys->d + keys->nr &&
-                      src[0].btree_id  == src[1].btree_id &&
-                      src[0].level     == src[1].level &&
-                      bpos_eq(src[0].k->k.p, src[1].k->k.p))
-                       src++;
 
-               *dst++ = *src++;
+                       nr_read++;
+               }
        }
 
-       keys->nr = dst - keys->d;
+       __journal_keys_sort(keys);
        keys->gap = keys->nr;
+
+       bch_verbose(c, "Journal keys: %zu read, %zu after sorting and compacting", nr_keys, keys->nr);
        return 0;
 }
 
@@ -601,7 +637,7 @@ static int bch2_journal_replay(struct bch_fs *c, u64 start_seq, u64 end_seq)
 
        keys_sorted = kvmalloc_array(sizeof(*keys_sorted), keys->nr, GFP_KERNEL);
        if (!keys_sorted)
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_journal_replay;
 
        for (i = 0; i < keys->nr; i++)
                keys_sorted[i] = &keys->d[i];
@@ -611,8 +647,8 @@ static int bch2_journal_replay(struct bch_fs *c, u64 start_seq, u64 end_seq)
             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);
+               ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
+                                          keys->nr, start_seq, end_seq);
                if (ret)
                        goto err;
        }
@@ -646,9 +682,12 @@ static int bch2_journal_replay(struct bch_fs *c, u64 start_seq, u64 end_seq)
        ret = bch2_journal_error(j);
 
        if (keys->nr && !ret)
-               bch2_fs_log_msg(c, "journal replay finished");
+               bch2_journal_log_msg(c, "journal replay finished");
 err:
        kvfree(keys_sorted);
+
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -871,7 +910,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"
@@ -905,7 +944,7 @@ static struct bch_sb_field_clean *read_superblock_clean(struct bch_fs *c)
                        GFP_KERNEL);
        if (!clean) {
                mutex_unlock(&c->sb_lock);
-               return ERR_PTR(-ENOMEM);
+               return ERR_PTR(-BCH_ERR_ENOMEM_read_superblock_clean);
        }
 
        ret = bch2_sb_clean_validate_late(c, clean, READ);
@@ -969,70 +1008,80 @@ 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;
 }
 
 static int bch2_fs_initialize_subvolumes(struct bch_fs *c)
 {
-       struct bkey_i_snapshot  root_snapshot;
-       struct bkey_i_subvolume root_volume;
+       struct bkey_i_snapshot_tree     root_tree;
+       struct bkey_i_snapshot          root_snapshot;
+       struct bkey_i_subvolume         root_volume;
        int ret;
 
+       bkey_snapshot_tree_init(&root_tree.k_i);
+       root_tree.k.p.offset            = 1;
+       root_tree.v.master_subvol       = cpu_to_le32(1);
+       root_tree.v.root_snapshot       = cpu_to_le32(U32_MAX);
+
        bkey_snapshot_init(&root_snapshot.k_i);
        root_snapshot.k.p.offset = U32_MAX;
        root_snapshot.v.flags   = 0;
        root_snapshot.v.parent  = 0;
        root_snapshot.v.subvol  = BCACHEFS_ROOT_SUBVOL;
-       root_snapshot.v.pad     = 0;
+       root_snapshot.v.tree    = cpu_to_le32(1);
        SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
 
-       ret = bch2_btree_insert(c, BTREE_ID_snapshots,
-                               &root_snapshot.k_i,
-                               NULL, NULL, 0);
-       if (ret)
-               return ret;
-
        bkey_subvolume_init(&root_volume.k_i);
        root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
        root_volume.v.flags     = 0;
        root_volume.v.snapshot  = cpu_to_le32(U32_MAX);
        root_volume.v.inode     = cpu_to_le64(BCACHEFS_ROOT_INO);
 
-       ret = bch2_btree_insert(c, BTREE_ID_subvolumes,
-                               &root_volume.k_i,
-                               NULL, NULL, 0);
+       ret =   bch2_btree_insert(c, BTREE_ID_snapshot_trees,
+                                 &root_tree.k_i,
+                                 NULL, NULL, 0) ?:
+               bch2_btree_insert(c, BTREE_ID_snapshots,
+                                 &root_snapshot.k_i,
+                                 NULL, NULL, 0) ?:
+               bch2_btree_insert(c, BTREE_ID_subvolumes,
+                                 &root_volume.k_i,
+                                 NULL, NULL, 0);
        if (ret)
-               return ret;
-
-       return 0;
+               bch_err_fn(c, ret);
+       return ret;
 }
 
-static int bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
+static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
 {
        struct btree_iter iter;
        struct bkey_s_c k;
        struct bch_inode_unpacked inode;
        int ret;
 
-       bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
-                            SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
-       k = bch2_btree_iter_peek_slot(&iter);
+       k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
+                              SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
        ret = bkey_err(k);
        if (ret)
-               goto err;
+               return ret;
 
        if (!bkey_is_inode(k.k)) {
                bch_err(trans->c, "root inode not found");
-               ret = -ENOENT;
+               ret = -BCH_ERR_ENOENT_inode;
                goto err;
        }
 
@@ -1047,9 +1096,19 @@ err:
        return ret;
 }
 
+/* set bi_subvol on root inode */
+noinline_for_stack
+static int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
+{
+       int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
+                               __bch2_fs_upgrade_for_subvolumes(&trans));
+       if (ret)
+               bch_err_fn(c, ret);
+       return ret;
+}
+
 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 last_seq, blacklist_seq, journal_seq;
@@ -1087,15 +1146,13 @@ int bch2_fs_recovery(struct bch_fs *c)
                goto err;
        }
 
-       if (!(c->sb.features & (1ULL << BCH_FEATURE_alloc_v2))) {
-               bch_info(c, "alloc_v2 feature bit not set, fsck required");
-               c->opts.fsck = true;
-               c->opts.fix_errors = FSCK_OPT_YES;
-       }
-
        if (!c->opts.nochanges) {
-               if (c->sb.version < bcachefs_metadata_version_lru_v2) {
-                       bch_info(c, "version prior to backpointers, upgrade and fsck required");
+               if (c->sb.version < bcachefs_metadata_required_upgrade_below) {
+                       bch_info(c, "version %s (%u) prior to %s (%u), upgrade and fsck required",
+                                bch2_metadata_versions[c->sb.version],
+                                c->sb.version,
+                                bch2_metadata_versions[bcachefs_metadata_required_upgrade_below],
+                                bcachefs_metadata_required_upgrade_below);
                        c->opts.version_upgrade = true;
                        c->opts.fsck            = true;
                        c->opts.fix_errors      = FSCK_OPT_YES;
@@ -1201,8 +1258,8 @@ use_clean:
                journal_seq += 8;
 
        if (blacklist_seq != journal_seq) {
-               ret =   bch2_fs_log_msg(c, "blacklisting entries %llu-%llu",
-                                       blacklist_seq, journal_seq) ?:
+               ret =   bch2_journal_log_msg(c, "blacklisting entries %llu-%llu",
+                                            blacklist_seq, journal_seq) ?:
                        bch2_journal_seq_blacklist_add(c,
                                        blacklist_seq, journal_seq);
                if (ret) {
@@ -1211,12 +1268,15 @@ use_clean:
                }
        }
 
-       ret =   bch2_fs_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
-                               journal_seq, last_seq, blacklist_seq - 1) ?:
+       ret =   bch2_journal_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_journal_log_msg(c, "dropping alloc info");
+
        /*
         * Skip past versions that might have possibly been used (as nonces),
         * but hadn't had their pointers written:
@@ -1229,32 +1289,37 @@ use_clean:
                goto err;
 
        bch_verbose(c, "starting alloc read");
-       err = "error reading allocation information";
-
        down_read(&c->gc_lock);
        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)
                goto err;
        bch_verbose(c, "alloc read done");
 
        bch_verbose(c, "starting stripes_read");
-       err = "error reading stripes";
        ret = bch2_stripes_read(c);
        if (ret)
                goto err;
        bch_verbose(c, "stripes_read done");
 
-       bch2_stripes_heap_start(c);
+       if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
+               ret = bch2_fs_initialize_subvolumes(c);
+               if (ret)
+                       goto err;
+       }
+
+       bch_verbose(c, "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;
 
                bch_info(c, "checking allocations");
-               err = "error checking allocations";
                ret = bch2_gc(c, true, metadata_only);
                if (ret)
                        goto err;
@@ -1262,24 +1327,9 @@ use_clean:
 
                set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
 
-               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, last_seq, blacklist_seq - 1);
                if (ret)
                        goto err;
@@ -1287,7 +1337,6 @@ use_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;
@@ -1296,7 +1345,6 @@ use_clean:
                set_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags);
 
                bch_info(c, "checking lrus");
-               err = "error checking lrus";
                ret = bch2_check_lrus(c);
                if (ret)
                        goto err;
@@ -1304,21 +1352,18 @@ use_clean:
                set_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags);
 
                bch_info(c, "checking backpointers to alloc keys");
-               err = "error checking backpointers to alloc keys";
                ret = bch2_check_btree_backpointers(c);
                if (ret)
                        goto err;
                bch_verbose(c, "done checking backpointers to alloc keys");
 
                bch_info(c, "checking backpointers to extents");
-               err = "error checking backpointers to extents";
                ret = bch2_check_backpointers_to_extents(c);
                if (ret)
                        goto err;
                bch_verbose(c, "done checking backpointers to extents");
 
                bch_info(c, "checking extents to backpointers");
-               err = "error checking extents to backpointers";
                ret = bch2_check_extents_to_backpointers(c);
                if (ret)
                        goto err;
@@ -1326,7 +1371,6 @@ use_clean:
                set_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags);
 
                bch_info(c, "checking alloc to lru refs");
-               err = "error checking alloc to lru refs";
                ret = bch2_check_alloc_to_lru_refs(c);
                if (ret)
                        goto err;
@@ -1343,24 +1387,9 @@ 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, last_seq, blacklist_seq - 1);
                if (ret)
                        goto err;
@@ -1368,7 +1397,6 @@ use_clean:
                        bch_info(c, "journal replay done");
        }
 
-       err = "error initializing freespace";
        ret = bch2_fs_freespace_init(c);
        if (ret)
                goto err;
@@ -1376,7 +1404,6 @@ use_clean:
        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;
@@ -1384,24 +1411,18 @@ use_clean:
        }
 
        if (c->sb.version < bcachefs_metadata_version_snapshot_2) {
-               /* set bi_subvol on root inode */
-               err = "error upgrade root inode for subvolumes";
-               ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
-                                   bch2_fs_upgrade_for_subvolumes(&trans));
+               ret = bch2_fs_upgrade_for_subvolumes(c);
                if (ret)
                        goto err;
        }
 
        if (c->opts.fsck) {
-               bch_info(c, "starting fsck");
-               err = "error in fsck";
                ret = bch2_fsck_full(c);
                if (ret)
                        goto err;
                bch_verbose(c, "fsck done");
        } else if (!c->sb.clean) {
                bch_verbose(c, "checking for deleted inodes");
-               err = "error in recovery";
                ret = bch2_fsck_walk_inodes_only(c);
                if (ret)
                        goto err;
@@ -1448,11 +1469,8 @@ use_clean:
                bch2_move_stats_init(&stats, "recovery");
 
                bch_info(c, "scanning for old btree nodes");
-               ret = bch2_fs_read_write(c);
-               if (ret)
-                       goto err;
-
-               ret = bch2_scan_old_btree_nodes(c, &stats);
+               ret =   bch2_fs_read_write(c) ?:
+                       bch2_scan_old_btree_nodes(c, &stats);
                if (ret)
                        goto err;
                bch_info(c, "scanning for old btree nodes done");
@@ -1480,7 +1498,7 @@ out:
        }
 
        if (ret)
-               bch_err(c, "Error in recovery: %s (%s)", err, bch2_err_str(ret));
+               bch_err_fn(c, ret);
        else
                bch_verbose(c, "ret %s", bch2_err_str(ret));
        return ret;
@@ -1495,7 +1513,6 @@ int bch2_fs_initialize(struct bch_fs *c)
        struct bch_inode_unpacked root_inode, lostfound_inode;
        struct bkey_inode_buf packed_inode;
        struct qstr lostfound = QSTR("lost+found");
-       const char *err = "cannot allocate memory";
        struct bch_dev *ca;
        unsigned i;
        int ret;
@@ -1529,7 +1546,6 @@ int bch2_fs_initialize(struct bch_fs *c)
        for_each_online_member(ca, c, i)
                bch2_dev_usage_init(ca);
 
-       err = "unable to allocate journal buckets";
        for_each_online_member(ca, c, i) {
                ret = bch2_dev_journal_alloc(ca);
                if (ret) {
@@ -1545,7 +1561,6 @@ int bch2_fs_initialize(struct bch_fs *c)
        bch2_fs_journal_start(&c->journal, 1);
        bch2_journal_set_replay_done(&c->journal);
 
-       err = "error going read-write";
        ret = bch2_fs_read_write_early(c);
        if (ret)
                goto err;
@@ -1555,7 +1570,6 @@ int bch2_fs_initialize(struct bch_fs *c)
         * btree updates
         */
        bch_verbose(c, "marking superblocks");
-       err = "error marking superblock and journal";
        for_each_member_device(ca, c, i) {
                ret = bch2_trans_mark_dev_sb(c, ca);
                if (ret) {
@@ -1566,19 +1580,15 @@ int bch2_fs_initialize(struct bch_fs *c)
                ca->new_fs_bucket_idx = 0;
        }
 
-       bch_verbose(c, "initializing freespace");
-       err = "error initializing freespace";
        ret = bch2_fs_freespace_init(c);
        if (ret)
                goto err;
 
-       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;
@@ -1590,16 +1600,16 @@ int bch2_fs_initialize(struct bch_fs *c)
        bch2_inode_pack(&packed_inode, &root_inode);
        packed_inode.inode.k.p.snapshot = U32_MAX;
 
-       err = "error creating root directory";
        ret = bch2_btree_insert(c, BTREE_ID_inodes,
                                &packed_inode.inode.k_i,
                                NULL, NULL, 0);
-       if (ret)
+       if (ret) {
+               bch_err_msg(c, ret, "creating root directory");
                goto err;
+       }
 
        bch2_inode_init_early(c, &lostfound_inode);
 
-       err = "error creating lost+found";
        ret = bch2_trans_do(c, NULL, NULL, 0,
                bch2_create_trans(&trans,
                                  BCACHEFS_ROOT_SUBVOL_INUM,
@@ -1608,7 +1618,7 @@ int bch2_fs_initialize(struct bch_fs *c)
                                  0, 0, S_IFDIR|0700, 0,
                                  NULL, NULL, (subvol_inum) { 0 }, 0));
        if (ret) {
-               bch_err(c, "error creating lost+found");
+               bch_err_msg(c, ret, "creating lost+found");
                goto err;
        }
 
@@ -1618,10 +1628,11 @@ int bch2_fs_initialize(struct bch_fs *c)
                        goto err;
        }
 
-       err = "error writing first journal entry";
        ret = bch2_journal_flush(&c->journal);
-       if (ret)
+       if (ret) {
+               bch_err_msg(c, ret, "writing first journal entry");
                goto err;
+       }
 
        mutex_lock(&c->sb_lock);
        SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
@@ -1632,6 +1643,6 @@ int bch2_fs_initialize(struct bch_fs *c)
 
        return 0;
 err:
-       pr_err("Error initializing new filesystem: %s (%i)", err, ret);
+       bch_err_fn(ca, ret);
        return ret;
 }