]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/super-io.c
Update bcachefs sources to fb39031ade bcachefs: bch2_sb_maybe_downgrade(), bch2_sb_up...
[bcachefs-tools-debian] / libbcachefs / super-io.c
index 472f5b216c654c184922aafba5f786145ca44170..1ac36060ed2d839215b751f8fd1f02d4d2ab0d61 100644 (file)
 #include <linux/backing-dev.h>
 #include <linux/sort.h>
 
-static const char * const bch2_metadata_versions[] = {
-#define x(t, n) [n] = #t,
+struct bch2_metadata_version_str {
+       u16             version;
+       const char      *name;
+};
+
+static const struct bch2_metadata_version_str bch2_metadata_versions[] = {
+#define x(n, v) { .version = v, .name = #n },
        BCH_METADATA_VERSIONS()
 #undef x
 };
 
 void bch2_version_to_text(struct printbuf *out, unsigned v)
 {
-       const char *str = v < ARRAY_SIZE(bch2_metadata_versions)
-               ? bch2_metadata_versions[v]
-               : "(unknown version)";
+       const char *str = "(unknown version)";
+
+       for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
+               if (bch2_metadata_versions[i].version == v) {
+                       str = bch2_metadata_versions[i].name;
+                       break;
+               }
 
-       prt_printf(out, "%u: %s", v, str);
+       prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
+}
+
+unsigned bch2_latest_compatible_version(unsigned v)
+{
+       if (!BCH_VERSION_MAJOR(v))
+               return v;
+
+       for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
+               if (bch2_metadata_versions[i].version > v &&
+                   BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
+                   BCH_VERSION_MAJOR(v))
+                       v = bch2_metadata_versions[i].version;
+
+       return v;
 }
 
 const char * const bch2_sb_fields[] = {
@@ -445,6 +468,7 @@ static void bch2_sb_update(struct bch_fs *c)
        c->sb.user_uuid         = src->user_uuid;
        c->sb.version           = le16_to_cpu(src->version);
        c->sb.version_min       = le16_to_cpu(src->version_min);
+       c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src) ?: c->sb.version;
        c->sb.nr_devices        = src->nr_devices;
        c->sb.clean             = BCH_SB_CLEAN(src);
        c->sb.encryption_type   = BCH_SB_ENCRYPTION_TYPE(src);
@@ -611,8 +635,6 @@ int bch2_read_super(const char *path, struct bch_opts *opts,
        __le64 *i;
        int ret;
 
-       pr_verbose_init(*opts, "");
-
        memset(sb, 0, sizeof(*sb));
        sb->mode        = FMODE_READ;
        sb->have_bio    = true;
@@ -719,7 +741,6 @@ got_super:
                goto err_no_print;
        }
 out:
-       pr_verbose_init(*opts, "ret %i", ret);
        printbuf_exit(&err);
        return ret;
 err:
@@ -811,10 +832,9 @@ int bch2_write_super(struct bch_fs *c)
        closure_init_stack(cl);
        memset(&sb_written, 0, sizeof(sb_written));
 
-       if (c->opts.version_upgrade) {
-               c->disk_sb.sb->magic = BCHFS_MAGIC;
-               c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
-       }
+       /* Make sure we're using the new magic numbers: */
+       c->disk_sb.sb->magic = BCHFS_MAGIC;
+       c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
 
        le64_add_cpu(&c->disk_sb.sb->seq, 1);
 
@@ -1177,6 +1197,32 @@ int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *cle
        return 0;
 }
 
+/* Downgrade if superblock is at a higher version than currently supported: */
+void bch2_sb_maybe_downgrade(struct bch_fs *c)
+{
+       lockdep_assert_held(&c->sb_lock);
+
+       /*
+        * Downgrade, if superblock is at a higher version than currently
+        * supported:
+        */
+       if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
+               SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
+       if (c->sb.version > bcachefs_metadata_version_current)
+               c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
+       if (c->sb.version_min > bcachefs_metadata_version_current)
+               c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
+       c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
+}
+
+void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
+{
+       lockdep_assert_held(&c->sb_lock);
+
+       c->disk_sb.sb->version = cpu_to_le16(new_version);
+       c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
+}
+
 int bch2_fs_mark_dirty(struct bch_fs *c)
 {
        int ret;
@@ -1188,8 +1234,10 @@ int bch2_fs_mark_dirty(struct bch_fs *c)
 
        mutex_lock(&c->sb_lock);
        SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
+
+       bch2_sb_maybe_downgrade(c);
        c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
-       c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
+
        ret = bch2_write_super(c);
        mutex_unlock(&c->sb_lock);
 
@@ -1532,6 +1580,11 @@ void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
        bch2_version_to_text(out, le16_to_cpu(sb->version));
        prt_newline(out);
 
+       prt_str(out, "Version upgrade complete:");
+       prt_tab(out);
+       bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
+       prt_newline(out);
+
        prt_printf(out, "Oldest version on disk:");
        prt_tab(out);
        bch2_version_to_text(out, le16_to_cpu(sb->version_min));