]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/migrate.c
Update bcachefs sources to 98a1bff393 bcachefs: Topology repair fixes
[bcachefs-tools-debian] / libbcachefs / migrate.c
index 01c8896078f2c027849cc46e69b044bcd89f1281..6defc33322b3b24bd5f9a076165accce6f204aa0 100644 (file)
@@ -1,9 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Code for moving data off a device.
  */
 
 #include "bcachefs.h"
+#include "bkey_buf.h"
 #include "btree_update.h"
+#include "btree_update_interior.h"
 #include "buckets.h"
 #include "extents.h"
 #include "io.h"
 #include "keylist.h"
 #include "migrate.h"
 #include "move.h"
+#include "replicas.h"
 #include "super-io.h"
 
-static bool migrate_pred(void *arg, struct bkey_s_c_extent e)
-{
-       struct bch_dev *ca = arg;
-
-       return bch2_extent_has_device(e, ca->dev_idx);
-}
-
-#define MAX_DATA_OFF_ITER      10
-
-static int bch2_dev_usrdata_migrate(struct bch_fs *c, struct bch_dev *ca,
-                                   int flags)
-{
-       struct btree_iter iter;
-       struct bkey_s_c k;
-       struct bch_move_stats stats;
-       unsigned pass = 0;
-       int ret = 0;
-
-       if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_USER)))
-               return 0;
-
-       /*
-        * XXX: we should be able to do this in one pass, but bch2_move_data()
-        * can spuriously fail to move an extent due to racing with other move
-        * operations
-        */
-       do {
-               ret = bch2_move_data(c, NULL,
-                                    SECTORS_IN_FLIGHT_PER_DEVICE,
-                                    NULL,
-                                    writepoint_hashed((unsigned long) current),
-                                    0,
-                                    ca->dev_idx,
-                                    migrate_pred, ca,
-                                    &stats);
-               if (ret) {
-                       bch_err(c, "error migrating data: %i", ret);
-                       return ret;
-               }
-       } while (atomic64_read(&stats.keys_moved) && pass++ < MAX_DATA_OFF_ITER);
-
-       if (atomic64_read(&stats.keys_moved)) {
-               bch_err(c, "unable to migrate all data in %d iterations",
-                       MAX_DATA_OFF_ITER);
-               return -1;
-       }
-
-       mutex_lock(&c->replicas_gc_lock);
-       bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
-
-       for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, BTREE_ITER_PREFETCH, k) {
-               ret = bch2_check_mark_super(c, BCH_DATA_USER, bch2_bkey_devs(k));
-               if (ret) {
-                       bch_err(c, "error migrating data %i from check_mark_super()", ret);
-                       break;
-               }
-       }
-
-       bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
-       return ret;
-}
-
-static int bch2_dev_metadata_migrate(struct bch_fs *c, struct bch_dev *ca,
-                                    int flags)
-{
-       struct btree_iter iter;
-       struct btree *b;
-       int ret = 0;
-       unsigned id;
-
-       if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_BTREE)))
-               return 0;
-
-       mutex_lock(&c->replicas_gc_lock);
-       bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
-
-       for (id = 0; id < BTREE_ID_NR; id++) {
-               for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
-                       struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
-
-                       if (!bch2_extent_has_device(e, ca->dev_idx))
-                               continue;
-
-                       ret = bch2_btree_node_rewrite(c, &iter, b->data->keys.seq, 0);
-                       if (ret) {
-                               bch2_btree_iter_unlock(&iter);
-                               goto err;
-                       }
-               }
-               ret = bch2_btree_iter_unlock(&iter);
-               if (ret)
-                       goto err;
-       }
-err:
-       bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
-       return ret;
-}
-
-int bch2_dev_data_migrate(struct bch_fs *c, struct bch_dev *ca, int flags)
-{
-       BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW &&
-              bch2_dev_is_online(ca));
-
-       return bch2_dev_usrdata_migrate(c, ca, flags) ?:
-               bch2_dev_metadata_migrate(c, ca, flags);
-}
-
-static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
+static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
                         unsigned dev_idx, int flags, bool metadata)
 {
        unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
@@ -130,9 +25,9 @@ static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
        unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
        unsigned nr_good;
 
-       bch2_extent_drop_device(e, dev_idx);
+       bch2_bkey_drop_device(k, dev_idx);
 
-       nr_good = bch2_extent_nr_good_ptrs(c, e.c);
+       nr_good = bch2_bkey_durability(c, k.s_c);
        if ((!nr_good && !(flags & lost)) ||
            (nr_good < replicas && !(flags & degraded)))
                return -EINVAL;
@@ -140,57 +35,58 @@ static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
        return 0;
 }
 
-static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
+static int __bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags,
+                                  enum btree_id btree_id)
 {
-       struct bkey_s_c k;
-       struct bkey_s_extent e;
-       BKEY_PADDED(key) tmp;
+       struct btree_trans trans;
        struct btree_iter iter;
+       struct bkey_s_c k;
+       struct bkey_buf sk;
        int ret = 0;
 
-       mutex_lock(&c->replicas_gc_lock);
-       bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
+       bch2_bkey_buf_init(&sk);
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
-       bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
-                            POS_MIN, BTREE_ITER_PREFETCH);
+       bch2_trans_iter_init(&trans, &iter, btree_id, POS_MIN,
+                            BTREE_ITER_PREFETCH|
+                            BTREE_ITER_ALL_SNAPSHOTS);
 
-       while ((k = bch2_btree_iter_peek(&iter)).k &&
-              !(ret = btree_iter_err(k))) {
-               if (!bkey_extent_is_data(k.k) ||
-                   !bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx)) {
-                       ret = bch2_check_mark_super(c, BCH_DATA_USER,
-                                                   bch2_bkey_devs(k));
-                       if (ret)
-                               break;
-                       bch2_btree_iter_next(&iter);
+       while ((bch2_trans_begin(&trans),
+               (k = bch2_btree_iter_peek(&iter)).k) &&
+              !(ret = bkey_err(k))) {
+               if (!bch2_bkey_has_device(k, dev_idx)) {
+                       bch2_btree_iter_advance(&iter);
                        continue;
                }
 
-               bkey_reassemble(&tmp.key, k);
-               e = bkey_i_to_s_extent(&tmp.key);
+               bch2_bkey_buf_reassemble(&sk, c, k);
 
-               ret = drop_dev_ptrs(c, e, dev_idx, flags, false);
+               ret = drop_dev_ptrs(c, bkey_i_to_s(sk.k),
+                                   dev_idx, flags, false);
                if (ret)
                        break;
 
                /*
                 * If the new extent no longer has any pointers, bch2_extent_normalize()
                 * will do the appropriate thing with it (turning it into a
-                * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
+                * KEY_TYPE_error key, or just a discard if it was a cached extent)
                 */
-               bch2_extent_normalize(c, e.s);
-
-               ret = bch2_check_mark_super(c, BCH_DATA_USER,
-                               bch2_bkey_devs(bkey_i_to_s_c(&tmp.key)));
-               if (ret)
-                       break;
+               bch2_extent_normalize(c, bkey_i_to_s(sk.k));
 
-               iter.pos = bkey_start_pos(&tmp.key.k);
+               /*
+                * Since we're not inserting through an extent iterator
+                * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
+                * we aren't using the extent overwrite path to delete, we're
+                * just using the normal key deletion path:
+                */
+               if (bkey_deleted(&sk.k->k))
+                       sk.k->k.size = 0;
 
-               ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
-                                          BTREE_INSERT_ATOMIC|
-                                          BTREE_INSERT_NOFAIL,
-                                          BTREE_INSERT_ENTRY(&iter, &tmp.key));
+               ret   = bch2_btree_iter_traverse(&iter) ?:
+                       bch2_trans_update(&trans, &iter, sk.k,
+                                         BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
+                       bch2_trans_commit(&trans, NULL, NULL,
+                                       BTREE_INSERT_NOFAIL);
 
                /*
                 * don't want to leave ret == -EINTR, since if we raced and
@@ -202,20 +98,29 @@ static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
                if (ret)
                        break;
        }
+       bch2_trans_iter_exit(&trans, &iter);
 
-       bch2_btree_iter_unlock(&iter);
+       bch2_trans_exit(&trans);
+       bch2_bkey_buf_exit(&sk, c);
 
-       bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
+       BUG_ON(ret == -EINTR);
 
        return ret;
 }
 
+static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
+{
+       return  __bch2_dev_usrdata_drop(c, dev_idx, flags, BTREE_ID_extents) ?:
+               __bch2_dev_usrdata_drop(c, dev_idx, flags, BTREE_ID_reflink);
+}
+
 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
 {
+       struct btree_trans trans;
        struct btree_iter iter;
        struct closure cl;
        struct btree *b;
+       struct bkey_buf k;
        unsigned id;
        int ret;
 
@@ -223,59 +128,65 @@ static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
        if (flags & BCH_FORCE_IF_METADATA_LOST)
                return -EINVAL;
 
+       bch2_bkey_buf_init(&k);
+       bch2_trans_init(&trans, c, 0, 0);
        closure_init_stack(&cl);
 
-       mutex_lock(&c->replicas_gc_lock);
-       bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
-
        for (id = 0; id < BTREE_ID_NR; id++) {
-               for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
-                       __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
-                       struct bkey_i_extent *new_key;
+               bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
+                                         BTREE_ITER_PREFETCH);
 retry:
-                       if (!bch2_extent_has_device(bkey_i_to_s_c_extent(&b->key),
-                                                   dev_idx)) {
-                               bch2_btree_iter_set_locks_want(&iter, 0);
-
-                               ret = bch2_check_mark_super(c, BCH_DATA_BTREE,
-                                               bch2_bkey_devs(bkey_i_to_s_c(&b->key)));
-                               if (ret)
-                                       goto err;
-                       } else {
-                               bkey_copy(&tmp.k, &b->key);
-                               new_key = bkey_i_to_extent(&tmp.k);
-
-                               ret = drop_dev_ptrs(c, extent_i_to_s(new_key),
-                                                   dev_idx, flags, true);
-                               if (ret)
-                                       goto err;
-
-                               if (!bch2_btree_iter_set_locks_want(&iter, U8_MAX)) {
-                                       b = bch2_btree_iter_peek_node(&iter);
-                                       goto retry;
-                               }
-
-                               ret = bch2_btree_node_update_key(c, &iter, b, new_key);
-                               if (ret == -EINTR) {
-                                       b = bch2_btree_iter_peek_node(&iter);
-                                       goto retry;
-                               }
-                               if (ret)
-                                       goto err;
+               ret = 0;
+               while (bch2_trans_begin(&trans),
+                      (b = bch2_btree_iter_peek_node(&iter)) &&
+                      !(ret = PTR_ERR_OR_ZERO(b))) {
+                       if (!bch2_bkey_has_device(bkey_i_to_s_c(&b->key),
+                                                 dev_idx))
+                               goto next;
+
+                       bch2_bkey_buf_copy(&k, c, &b->key);
+
+                       ret = drop_dev_ptrs(c, bkey_i_to_s(k.k),
+                                           dev_idx, flags, true);
+                       if (ret) {
+                               bch_err(c, "Cannot drop device without losing data");
+                               break;
+                       }
+
+                       ret = bch2_btree_node_update_key(&trans, &iter, b, k.k, false);
+                       if (ret == -EINTR) {
+                               ret = 0;
+                               continue;
+                       }
+
+                       if (ret) {
+                               bch_err(c, "Error updating btree node key: %i", ret);
+                               break;
                        }
+next:
+                       bch2_btree_iter_next_node(&iter);
                }
-               bch2_btree_iter_unlock(&iter);
+               if (ret == -EINTR)
+                       goto retry;
+
+               bch2_trans_iter_exit(&trans, &iter);
+
+               if (ret)
+                       goto err;
        }
 
+       /* flush relevant btree updates */
+       closure_wait_event(&c->btree_interior_update_wait,
+                          !bch2_btree_interior_updates_nr_pending(c));
+
        ret = 0;
-out:
-       bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
+err:
+       bch2_trans_exit(&trans);
+       bch2_bkey_buf_exit(&k, c);
+
+       BUG_ON(ret == -EINTR);
 
        return ret;
-err:
-       bch2_btree_iter_unlock(&iter);
-       goto out;
 }
 
 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)