bcachefs: Scan for old btree nodes if necessary on mount
authorKent Overstreet <kent.overstreet@gmail.com>
Mon, 22 Mar 2021 22:39:16 +0000 (18:39 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:54 +0000 (17:08 -0400)
We dropped support for !BTREE_NODE_NEW_EXTENT_OVERWRITE but it turned
out there were people who still had filesystems with btree nodes in that
format in the wild. This adds a new compat feature that indicates we've
scanned for and rewritten nodes in the old format, and does that scan at
mount time if the option isn't set.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/bcachefs_format.h
fs/bcachefs/btree_gc.c
fs/bcachefs/move.c
fs/bcachefs/move.h
fs/bcachefs/rebalance.c
fs/bcachefs/recovery.c

index cf09290..e2df0f7 100644 (file)
@@ -1373,6 +1373,7 @@ enum bch_sb_feature {
 enum bch_sb_compat {
        BCH_COMPAT_FEAT_ALLOC_INFO      = 0,
        BCH_COMPAT_FEAT_ALLOC_METADATA  = 1,
+       BCH_COMPAT_FEAT_EXTENTS_ABOVE_BTREE_UPDATES_DONE = 2,
 };
 
 /* options: */
index 426c932..259a36f 100644 (file)
@@ -1618,7 +1618,8 @@ int bch2_gc_thread_start(struct bch_fs *c)
 {
        struct task_struct *p;
 
-       BUG_ON(c->gc_thread);
+       if (c->gc_thread)
+               return 0;
 
        p = kthread_create(bch2_gc_thread, c, "bch-gc/%s", c->name);
        if (IS_ERR(p)) {
index 72958b8..ed18abf 100644 (file)
@@ -845,6 +845,25 @@ static enum data_cmd rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
        return DATA_SKIP;
 }
 
+int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
+{
+       int ret;
+
+       ret = bch2_move_btree(c,
+                             0,                POS_MIN,
+                             BTREE_ID_NR,      POS_MAX,
+                             rewrite_old_nodes_pred, c, stats);
+       if (!ret) {
+               mutex_lock(&c->sb_lock);
+               c->disk_sb.sb->compat[0] |= 1ULL << BCH_COMPAT_FEAT_EXTENTS_ABOVE_BTREE_UPDATES_DONE;
+               c->disk_sb.sb->version_min = c->disk_sb.sb->version;
+               bch2_write_super(c);
+               mutex_unlock(&c->sb_lock);
+       }
+
+       return ret;
+}
+
 int bch2_data_job(struct bch_fs *c,
                  struct bch_move_stats *stats,
                  struct bch_ioctl_data op)
@@ -894,17 +913,7 @@ int bch2_data_job(struct bch_fs *c,
                ret = bch2_replicas_gc2(c) ?: ret;
                break;
        case BCH_DATA_OP_REWRITE_OLD_NODES:
-               ret = bch2_move_btree(c,
-                                     op.start_btree,   op.start_pos,
-                                     op.end_btree,     op.end_pos,
-                                     rewrite_old_nodes_pred, &op, stats) ?: ret;
-
-               if (!ret) {
-                       mutex_lock(&c->sb_lock);
-                       c->disk_sb.sb->version_min = c->disk_sb.sb->version;
-                       bch2_write_super(c);
-                       mutex_unlock(&c->sb_lock);
-               }
+               ret = bch2_scan_old_btree_nodes(c, stats);
                break;
        default:
                ret = -EINVAL;
index 403ca69..5076153 100644 (file)
@@ -52,6 +52,8 @@ typedef enum data_cmd (*move_pred_fn)(struct bch_fs *, void *,
                                struct bkey_s_c,
                                struct bch_io_opts *, struct data_opts *);
 
+int bch2_scan_old_btree_nodes(struct bch_fs *, struct bch_move_stats *);
+
 int bch2_move_data(struct bch_fs *,
                   enum btree_id, struct bpos,
                   enum btree_id, struct bpos,
index c83c12d..0e1f18d 100644 (file)
@@ -312,6 +312,9 @@ int bch2_rebalance_start(struct bch_fs *c)
 {
        struct task_struct *p;
 
+       if (c->rebalance.thread)
+               return 0;
+
        if (c->opts.nochanges)
                return 0;
 
index 0aeaaad..e322dc3 100644 (file)
@@ -16,6 +16,7 @@
 #include "journal_io.h"
 #include "journal_reclaim.h"
 #include "journal_seq_blacklist.h"
+#include "move.h"
 #include "quota.h"
 #include "recovery.h"
 #include "replicas.h"
@@ -1200,6 +1201,20 @@ use_clean:
                bch_verbose(c, "quotas done");
        }
 
+       if (!(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_EXTENTS_ABOVE_BTREE_UPDATES_DONE))) {
+               struct bch_move_stats stats = { 0 };
+
+               bch_verbose(c, "scanning for old btree nodes");
+               ret = bch2_fs_read_write(c);
+               if (ret)
+                       goto err;
+
+               ret = bch2_scan_old_btree_nodes(c, &stats);
+               if (ret)
+                       goto err;
+               bch_verbose(c, "scanning for old btree nodes done");
+       }
+
        mutex_lock(&c->sb_lock);
        if (c->opts.version_upgrade) {
                if (c->sb.version < bcachefs_metadata_version_new_versioning)
@@ -1271,6 +1286,7 @@ int bch2_fs_initialize(struct bch_fs *c)
                le16_to_cpu(bcachefs_metadata_version_current);
        c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
        c->disk_sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
+       c->disk_sb.sb->compat[0] |= 1ULL << BCH_COMPAT_FEAT_EXTENTS_ABOVE_BTREE_UPDATES_DONE;
 
        bch2_write_super(c);
        mutex_unlock(&c->sb_lock);