2017-03-17 06:18:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Code for moving data off a device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
2020-12-17 20:08:58 +00:00
|
|
|
#include "bkey_buf.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "btree_update.h"
|
2019-01-21 20:32:13 +00:00
|
|
|
#include "btree_update_interior.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "buckets.h"
|
|
|
|
#include "extents.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "journal.h"
|
|
|
|
#include "keylist.h"
|
|
|
|
#include "migrate.h"
|
|
|
|
#include "move.h"
|
|
|
|
#include "replicas.h"
|
|
|
|
#include "super-io.h"
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned dev_idx, int flags, bool metadata)
|
|
|
|
{
|
|
|
|
unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
|
|
|
|
unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
|
|
|
|
unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
|
|
|
|
unsigned nr_good;
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
bch2_bkey_drop_device(k, dev_idx);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
nr_good = bch2_bkey_durability(c, k.s_c);
|
2017-03-17 06:18:50 +00:00
|
|
|
if ((!nr_good && !(flags & lost)) ||
|
|
|
|
(nr_good < replicas && !(flags & degraded)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-16 13:59:56 +00:00
|
|
|
static int __bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags,
|
|
|
|
enum btree_id btree_id)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2019-03-14 00:49:16 +00:00
|
|
|
struct btree_trans trans;
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_iter iter;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bkey_s_c k;
|
2020-12-17 20:08:58 +00:00
|
|
|
struct bkey_buf sk;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_init(&sk);
|
2019-05-15 14:54:43 +00:00
|
|
|
bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
|
2019-03-14 00:49:16 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
bch2_trans_iter_init(&trans, &iter, btree_id, POS_MIN,
|
2021-08-05 04:41:41 +00:00
|
|
|
BTREE_ITER_PREFETCH|
|
|
|
|
BTREE_ITER_ALL_SNAPSHOTS);
|
2019-03-14 00:49:16 +00:00
|
|
|
|
2021-10-07 18:59:00 +00:00
|
|
|
while ((bch2_trans_begin(&trans),
|
|
|
|
(k = bch2_btree_iter_peek(&iter)).k) &&
|
2019-03-28 02:03:30 +00:00
|
|
|
!(ret = bkey_err(k))) {
|
2019-07-25 17:52:14 +00:00
|
|
|
if (!bch2_bkey_has_device(k, dev_idx)) {
|
2021-08-30 19:18:31 +00:00
|
|
|
bch2_btree_iter_advance(&iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_reassemble(&sk, c, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-11-09 21:01:15 +00:00
|
|
|
ret = drop_dev_ptrs(c, bkey_i_to_s(sk.k),
|
2018-11-01 19:10:01 +00:00
|
|
|
dev_idx, flags, false);
|
2017-03-17 06:18:50 +00:00
|
|
|
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
|
2018-11-01 19:10:01 +00:00
|
|
|
* KEY_TYPE_error key, or just a discard if it was a cached extent)
|
2017-03-17 06:18:50 +00:00
|
|
|
*/
|
2019-11-09 21:01:15 +00:00
|
|
|
bch2_extent_normalize(c, bkey_i_to_s(sk.k));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-10-12 18:25:13 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
ret = bch2_btree_iter_traverse(&iter) ?:
|
2021-08-05 04:41:41 +00:00
|
|
|
bch2_trans_update(&trans, &iter, sk.k,
|
|
|
|
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
|
2021-06-02 04:15:07 +00:00
|
|
|
bch2_trans_commit(&trans, NULL, NULL,
|
2019-03-14 00:49:16 +00:00
|
|
|
BTREE_INSERT_NOFAIL);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
2021-08-30 19:18:31 +00:00
|
|
|
bch2_trans_iter_exit(&trans, &iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-10-19 19:08:00 +00:00
|
|
|
bch2_trans_exit(&trans);
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_exit(&sk, c);
|
2019-03-17 20:49:59 +00:00
|
|
|
|
2019-04-30 21:15:39 +00:00
|
|
|
BUG_ON(ret == -EINTR);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-16 13:59:56 +00:00
|
|
|
static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
|
|
|
|
{
|
2021-02-21 00:27:37 +00:00
|
|
|
return __bch2_dev_usrdata_drop(c, dev_idx, flags, BTREE_ID_extents) ?:
|
|
|
|
__bch2_dev_usrdata_drop(c, dev_idx, flags, BTREE_ID_reflink);
|
2019-08-16 13:59:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
|
|
|
|
{
|
2019-03-25 19:10:15 +00:00
|
|
|
struct btree_trans trans;
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_iter iter;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct closure cl;
|
|
|
|
struct btree *b;
|
2020-12-17 20:08:58 +00:00
|
|
|
struct bkey_buf k;
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* don't handle this yet: */
|
|
|
|
if (flags & BCH_FORCE_IF_METADATA_LOST)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_init(&k);
|
2019-05-15 14:54:43 +00:00
|
|
|
bch2_trans_init(&trans, c, 0, 0);
|
2017-03-17 06:18:50 +00:00
|
|
|
closure_init_stack(&cl);
|
|
|
|
|
|
|
|
for (id = 0; id < BTREE_ID_NR; id++) {
|
2021-10-07 18:59:00 +00:00
|
|
|
bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
|
|
|
|
BTREE_ITER_PREFETCH);
|
2021-10-19 18:20:50 +00:00
|
|
|
retry:
|
2021-10-19 19:11:45 +00:00
|
|
|
ret = 0;
|
2021-10-07 18:59:00 +00:00
|
|
|
while (bch2_trans_begin(&trans),
|
2021-10-19 18:20:50 +00:00
|
|
|
(b = bch2_btree_iter_peek_node(&iter)) &&
|
|
|
|
!(ret = PTR_ERR_OR_ZERO(b))) {
|
2018-11-01 19:10:01 +00:00
|
|
|
if (!bch2_bkey_has_device(bkey_i_to_s_c(&b->key),
|
2020-01-04 03:38:14 +00:00
|
|
|
dev_idx))
|
2021-10-07 18:59:00 +00:00
|
|
|
goto next;
|
2020-01-04 03:38:14 +00:00
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_copy(&k, c, &b->key);
|
2020-01-04 03:38:14 +00:00
|
|
|
|
2020-12-17 20:08:58 +00:00
|
|
|
ret = drop_dev_ptrs(c, bkey_i_to_s(k.k),
|
2020-01-04 03:38:14 +00:00
|
|
|
dev_idx, flags, true);
|
|
|
|
if (ret) {
|
|
|
|
bch_err(c, "Cannot drop device without losing data");
|
2021-03-20 00:29:11 +00:00
|
|
|
break;
|
2020-01-04 03:38:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
ret = bch2_btree_node_update_key(&trans, &iter, b, k.k, false);
|
2020-01-04 03:38:14 +00:00
|
|
|
if (ret == -EINTR) {
|
2021-03-20 00:29:11 +00:00
|
|
|
ret = 0;
|
2021-10-07 18:59:00 +00:00
|
|
|
continue;
|
2020-01-04 03:38:14 +00:00
|
|
|
}
|
2021-10-07 18:59:00 +00:00
|
|
|
|
2020-01-04 03:38:14 +00:00
|
|
|
if (ret) {
|
|
|
|
bch_err(c, "Error updating btree node key: %i", ret);
|
2021-03-20 00:29:11 +00:00
|
|
|
break;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
2021-10-07 18:59:00 +00:00
|
|
|
next:
|
|
|
|
bch2_btree_iter_next_node(&iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
2021-10-19 18:20:50 +00:00
|
|
|
if (ret == -EINTR)
|
|
|
|
goto retry;
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
bch2_trans_iter_exit(&trans, &iter);
|
2021-03-20 00:29:11 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
/* flush relevant btree updates */
|
2020-05-25 18:57:06 +00:00
|
|
|
closure_wait_event(&c->btree_interior_update_wait,
|
|
|
|
!bch2_btree_interior_updates_nr_pending(c));
|
2019-01-21 20:32:13 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
ret = 0;
|
2019-03-25 19:10:15 +00:00
|
|
|
err:
|
2021-10-19 19:08:00 +00:00
|
|
|
bch2_trans_exit(&trans);
|
2020-12-17 20:08:58 +00:00
|
|
|
bch2_bkey_buf_exit(&k, c);
|
2019-03-25 19:10:15 +00:00
|
|
|
|
2019-04-30 21:15:39 +00:00
|
|
|
BUG_ON(ret == -EINTR);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
|
|
|
|
{
|
|
|
|
return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
|
2020-01-04 03:38:14 +00:00
|
|
|
bch2_dev_metadata_drop(c, dev_idx, flags);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|