-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 {
- memset(&stats, 0, sizeof(stats));
-
- ret = bch2_move_data(c, NULL,
- SECTORS_IN_FLIGHT_PER_DEVICE,
- NULL,
- writepoint_hashed((unsigned long) current),
- 0,
- ca->dev_idx,
- POS_MIN, POS_MAX,
- 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);
-}
-