]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/migrate.c
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / libbcachefs / migrate.c
index c0dfe1c641e22d89eb98d6451eedaf8bcede01c6..5623cee3ef8693413ee51d7dd521c496e90f206c 100644 (file)
@@ -1,12 +1,16 @@
+// 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 "errcode.h"
 #include "extents.h"
-#include "io.h"
+#include "io_write.h"
 #include "journal.h"
 #include "keylist.h"
 #include "migrate.h"
@@ -14,7 +18,7 @@
 #include "replicas.h"
 #include "super-io.h"
 
-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;
@@ -22,9 +26,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_durability(c, e.c);
+       nr_good = bch2_bkey_durability(c, k.s_c);
        if ((!nr_good && !(flags & lost)) ||
            (nr_good < replicas && !(flags & degraded)))
                return -EINVAL;
@@ -32,81 +36,76 @@ 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_key(struct btree_trans *trans,
+                                    struct btree_iter *iter,
+                                    struct bkey_s_c k,
+                                    unsigned dev_idx,
+                                    int flags)
 {
-       struct bkey_s_c k;
-       struct bkey_s_extent e;
-       BKEY_PADDED(key) tmp;
-       struct btree_iter iter;
-       int ret = 0;
+       struct bch_fs *c = trans->c;
+       struct bkey_i *n;
+       int ret;
 
-       mutex_lock(&c->replicas_gc_lock);
-       bch2_replicas_gc_start(c, (1 << BCH_DATA_USER)|(1 << BCH_DATA_CACHED));
+       if (!bch2_bkey_has_device_c(k, dev_idx))
+               return 0;
+
+       n = bch2_bkey_make_mut(trans, iter, &k, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
+       ret = PTR_ERR_OR_ZERO(n);
+       if (ret)
+               return ret;
+
+       ret = drop_dev_ptrs(c, bkey_i_to_s(n), dev_idx, flags, false);
+       if (ret)
+               return ret;
+
+       /*
+        * 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)
+        */
+       bch2_extent_normalize(c, bkey_i_to_s(n));
+
+       /*
+        * 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(&n->k))
+               n->k.size = 0;
+       return 0;
+}
 
-       bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
-                            POS_MIN, BTREE_ITER_PREFETCH);
+static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
+{
+       struct btree_trans *trans = bch2_trans_get(c);
+       enum btree_id id;
+       int ret = 0;
 
-       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_mark_bkey_replicas(c, BKEY_TYPE_EXTENTS, k);
-                       if (ret)
-                               break;
-                       bch2_btree_iter_next(&iter);
+       for (id = 0; id < BTREE_ID_NR; id++) {
+               if (!btree_type_has_ptrs(id))
                        continue;
-               }
 
-               bkey_reassemble(&tmp.key, k);
-               e = bkey_i_to_s_extent(&tmp.key);
-
-               ret = drop_dev_ptrs(c, e, 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)
-                */
-               bch2_extent_normalize(c, e.s);
-
-               ret = bch2_mark_bkey_replicas(c, BKEY_TYPE_EXTENTS,
-                                             bkey_i_to_s_c(&tmp.key));
-               if (ret)
-                       break;
-
-               iter.pos = bkey_start_pos(&tmp.key.k);
-
-               ret = bch2_btree_insert_at(c, NULL, NULL,
-                                          BTREE_INSERT_ATOMIC|
-                                          BTREE_INSERT_NOFAIL,
-                                          BTREE_INSERT_ENTRY(&iter, &tmp.key));
-
-               /*
-                * don't want to leave ret == -EINTR, since if we raced and
-                * something else overwrote the key we could spuriously return
-                * -EINTR below:
-                */
-               if (ret == -EINTR)
-                       ret = 0;
+               ret = for_each_btree_key_commit(trans, iter, id, POS_MIN,
+                               BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
+                               NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
+                       bch2_dev_usrdata_drop_key(trans, &iter, k, dev_idx, flags));
                if (ret)
                        break;
        }
 
-       bch2_btree_iter_unlock(&iter);
-
-       bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
+       bch2_trans_put(trans);
 
        return ret;
 }
 
 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;
 
@@ -114,60 +113,60 @@ static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
        if (flags & BCH_FORCE_IF_METADATA_LOST)
                return -EINVAL;
 
+       trans = bch2_trans_get(c);
+       bch2_bkey_buf_init(&k);
        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)) {
-                               /*
-                                * we might have found a btree node key we
-                                * needed to update, and then tried to update it
-                                * but got -EINTR after upgrading the iter, but
-                                * then raced and the node is now gone:
-                                */
-                               bch2_btree_iter_downgrade(&iter);
-
-                               ret = bch2_mark_bkey_replicas(c, BKEY_TYPE_BTREE,
-                                                             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;
-
-                               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_c(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, 0, false);
+                       if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
+                               ret = 0;
+                               continue;
                        }
+
+                       bch_err_msg(c, ret, "updating btree node key");
+                       if (ret)
+                               break;
+next:
+                       bch2_btree_iter_next_node(&iter);
                }
-               bch2_btree_iter_unlock(&iter);
+               if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
+                       goto retry;
+
+               bch2_trans_iter_exit(trans, &iter);
+
+               if (ret)
+                       goto err;
        }
 
+       bch2_btree_interior_updates_flush(c);
        ret = 0;
-out:
-       ret = bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
+err:
+       bch2_bkey_buf_exit(&k, c);
+       bch2_trans_put(trans);
+
+       BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
 
        return ret;
-err:
-       bch2_btree_iter_unlock(&iter);
-       goto out;
 }
 
 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)