]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/migrate.c
Update bcachefs sources to 1d669389f7 bcachefs: use a radix tree for inum bitmap...
[bcachefs-tools-debian] / libbcachefs / migrate.c
index 822b3fce09b4cd50687707a5db6b6f5c5a70a9d8..96c8690adc5bf51cfaecc8e8628a45109a07d7a1 100644 (file)
@@ -1,8 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Code for moving data off a device.
  */
 
 #include "bcachefs.h"
+#include "bkey_on_stack.h"
 #include "btree_update.h"
 #include "btree_update_interior.h"
 #include "buckets.h"
@@ -33,38 +35,31 @@ static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
        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 btree_trans trans;
        struct btree_iter *iter;
        struct bkey_s_c k;
-       BKEY_PADDED(key) tmp;
+       struct bkey_on_stack sk;
        int ret = 0;
 
-       bch2_trans_init(&trans, c);
-       bch2_trans_preload_iters(&trans);
-
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
-                                  POS_MIN, BTREE_ITER_PREFETCH);
-
-       mutex_lock(&c->replicas_gc_lock);
-       bch2_replicas_gc_start(c, (1 << BCH_DATA_USER)|(1 << BCH_DATA_CACHED));
+       bkey_on_stack_init(&sk);
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
 
+       iter = bch2_trans_get_iter(&trans, btree_id, POS_MIN,
+                                  BTREE_ITER_PREFETCH);
 
        while ((k = bch2_btree_iter_peek(iter)).k &&
               !(ret = bkey_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, k);
-                       if (ret)
-                               break;
+               if (!bch2_bkey_has_device(k, dev_idx)) {
                        bch2_btree_iter_next(iter);
                        continue;
                }
 
-               bkey_reassemble(&tmp.key, k);
+               bkey_on_stack_reassemble(&sk, c, k);
 
-               ret = drop_dev_ptrs(c, bkey_i_to_s(&tmp.key),
+               ret = drop_dev_ptrs(c, bkey_i_to_s(sk.k),
                                    dev_idx, flags, false);
                if (ret)
                        break;
@@ -74,15 +69,13 @@ static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
                 * 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(&tmp.key));
+               bch2_extent_normalize(c, bkey_i_to_s(sk.k));
 
-               /* XXX not sketchy at all */
-               iter->pos = bkey_start_pos(&tmp.key.k);
+               bch2_btree_iter_set_pos(iter, bkey_start_pos(&sk.k->k));
 
-               bch2_trans_update(&trans, BTREE_INSERT_ENTRY(iter, &tmp.key));
+               bch2_trans_update(&trans, iter, sk.k, 0);
 
                ret = bch2_trans_commit(&trans, NULL, NULL,
-                                       BTREE_INSERT_ATOMIC|
                                        BTREE_INSERT_NOFAIL);
 
                /*
@@ -96,16 +89,20 @@ static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
                        break;
        }
 
-       BUG_ON(ret == -EINTR);
-
-       bch2_trans_exit(&trans);
+       ret = bch2_trans_exit(&trans) ?: ret;
+       bkey_on_stack_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;
@@ -119,68 +116,49 @@ 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_trans_init(&trans, c);
+       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(&trans, iter, id, POS_MIN,
                                    BTREE_ITER_PREFETCH, b) {
                        __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
-                       struct bkey_i_btree_ptr *new_key;
 retry:
                        if (!bch2_bkey_has_device(bkey_i_to_s_c(&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_i_to_s_c(&b->key));
-                               if (ret)
-                                       goto err;
-                       } else {
-                               bkey_copy(&tmp.k, &b->key);
-                               new_key = bkey_i_to_btree_ptr(&tmp.k);
-
-                               ret = drop_dev_ptrs(c, bkey_i_to_s(&new_key->k_i),
-                                                   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;
+                                                 dev_idx))
+                               continue;
+
+                       bkey_copy(&tmp.k, &b->key);
+
+                       ret = drop_dev_ptrs(c, bkey_i_to_s(&tmp.k),
+                                           dev_idx, flags, true);
+                       if (ret) {
+                               bch_err(c, "Cannot drop device without losing data");
+                               goto err;
+                       }
+
+                       ret = bch2_btree_node_update_key(c, iter, b, &tmp.k);
+                       if (ret == -EINTR) {
+                               b = bch2_btree_iter_peek_node(iter);
+                               goto retry;
+                       }
+                       if (ret) {
+                               bch_err(c, "Error updating btree node key: %i", ret);
+                               goto err;
                        }
                }
                bch2_trans_iter_free(&trans, iter);
        }
 
        /* flush relevant btree updates */
-       while (1) {
-               closure_wait_event(&c->btree_interior_update_wait,
-                                  !bch2_btree_interior_updates_nr_pending(c) ||
-                                  c->btree_roots_dirty);
-               if (!bch2_btree_interior_updates_nr_pending(c))
-                       break;
-               bch2_journal_meta(&c->journal);
-       }
+       closure_wait_event(&c->btree_interior_update_wait,
+                          !bch2_btree_interior_updates_nr_pending(c));
 
        ret = 0;
 err:
-       bch2_trans_exit(&trans);
+       ret = bch2_trans_exit(&trans) ?: ret;
 
-       ret = bch2_replicas_gc_end(c, ret);
-       mutex_unlock(&c->replicas_gc_lock);
+       BUG_ON(ret == -EINTR);
 
        return ret;
 }