bcachefs: Ensure bch_sb_field_ext always exists
authorKent Overstreet <kent.overstreet@linux.dev>
Sat, 30 Mar 2024 22:57:53 +0000 (18:57 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Mon, 1 Apr 2024 00:36:12 +0000 (20:36 -0400)
This makes bch_sb_field_ext more consistent with the rest of -o
nochanges - we don't want to be varying other codepaths based on -o
nochanges, since it's used for testing in dry run mode; also fixes some
potential null ptr derefs.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/recovery.c
fs/bcachefs/super.c

index dbedb5f..e3b0643 100644 (file)
@@ -592,16 +592,9 @@ int bch2_fs_recovery(struct bch_fs *c)
 
        if (!c->opts.nochanges) {
                mutex_lock(&c->sb_lock);
+               struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
                bool write_sb = false;
 
-               struct bch_sb_field_ext *ext =
-                       bch2_sb_field_get_minsize(&c->disk_sb, ext, sizeof(*ext) / sizeof(u64));
-               if (!ext) {
-                       ret = -BCH_ERR_ENOSPC_sb;
-                       mutex_unlock(&c->sb_lock);
-                       goto err;
-               }
-
                if (BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)) {
                        ext->recovery_passes_required[0] |=
                                cpu_to_le64(bch2_recovery_passes_to_stable(BIT_ULL(BCH_RECOVERY_PASS_check_topology)));
@@ -832,6 +825,7 @@ use_clean:
        }
 
        mutex_lock(&c->sb_lock);
+       struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
        bool write_sb = false;
 
        if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != le16_to_cpu(c->disk_sb.sb->version)) {
@@ -845,15 +839,12 @@ use_clean:
                write_sb = true;
        }
 
-       if (!test_bit(BCH_FS_error, &c->flags)) {
-               struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext);
-               if (ext &&
-                   (!bch2_is_zero(ext->recovery_passes_required, sizeof(ext->recovery_passes_required)) ||
-                    !bch2_is_zero(ext->errors_silent, sizeof(ext->errors_silent)))) {
-                       memset(ext->recovery_passes_required, 0, sizeof(ext->recovery_passes_required));
-                       memset(ext->errors_silent, 0, sizeof(ext->errors_silent));
-                       write_sb = true;
-               }
+       if (!test_bit(BCH_FS_error, &c->flags) &&
+           (!bch2_is_zero(ext->recovery_passes_required, sizeof(ext->recovery_passes_required)) ||
+            !bch2_is_zero(ext->errors_silent, sizeof(ext->errors_silent)))) {
+               memset(ext->recovery_passes_required, 0, sizeof(ext->recovery_passes_required));
+               memset(ext->errors_silent, 0, sizeof(ext->errors_silent));
+               write_sb = true;
        }
 
        if (c->opts.fsck &&
index 1ad6e5c..89ee5d3 100644 (file)
@@ -1015,8 +1015,16 @@ int bch2_fs_start(struct bch_fs *c)
        for_each_online_member(c, ca)
                bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now);
 
+       struct bch_sb_field_ext *ext =
+               bch2_sb_field_get_minsize(&c->disk_sb, ext, sizeof(*ext) / sizeof(u64));
        mutex_unlock(&c->sb_lock);
 
+       if (!ext) {
+               bch_err(c, "insufficient space in superblock for sb_field_ext");
+               ret = -BCH_ERR_ENOSPC_sb;
+               goto err;
+       }
+
        for_each_rw_member(c, ca)
                bch2_dev_allocator_add(c, ca);
        bch2_recalc_capacity(c);