bcachefs: bch2_dev_data_drop_by_backpointers()

Currently, device removal has to scan all metadata for pointers to the
device being removed.

Add a new method, with the same interface as bch2_dev_data_drop(), that
scans by backpointers instead - this will drastically speed up device
removal.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2025-05-03 21:36:23 -04:00
parent b3f80d0923
commit 09fa6c3039
2 changed files with 105 additions and 15 deletions

View File

@ -4,10 +4,13 @@
*/
#include "bcachefs.h"
#include "backpointers.h"
#include "bkey_buf.h"
#include "btree_update.h"
#include "btree_update_interior.h"
#include "btree_write_buffer.h"
#include "buckets.h"
#include "ec.h"
#include "errcode.h"
#include "extents.h"
#include "io_write.h"
@ -20,7 +23,7 @@
#include "super-io.h"
static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
unsigned dev_idx, int flags, bool metadata)
unsigned dev_idx, unsigned 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;
@ -37,11 +40,28 @@ static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
return 0;
}
static int drop_btree_ptrs(struct btree_trans *trans, struct btree_iter *iter,
struct btree *b, unsigned dev_idx, unsigned flags)
{
struct bch_fs *c = trans->c;
struct bkey_buf k;
bch2_bkey_buf_init(&k);
bch2_bkey_buf_copy(&k, c, &b->key);
int ret = drop_dev_ptrs(c, bkey_i_to_s(k.k), dev_idx, flags, true) ?:
bch2_btree_node_update_key(trans, iter, b, k.k, 0, false);
bch_err_fn(c, ret);
bch2_bkey_buf_exit(&k, c);
return ret;
}
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)
unsigned flags)
{
struct bch_fs *c = trans->c;
struct bkey_i *n;
@ -77,9 +97,27 @@ static int bch2_dev_usrdata_drop_key(struct btree_trans *trans,
return 0;
}
static int bch2_dev_btree_drop_key(struct btree_trans *trans,
struct bkey_s_c_backpointer bp,
unsigned dev_idx,
struct bkey_buf *last_flushed,
unsigned flags)
{
struct btree_iter iter;
struct btree *b = bch2_backpointer_get_node(trans, bp, &iter, last_flushed);
int ret = PTR_ERR_OR_ZERO(b);
if (ret)
return ret == -BCH_ERR_backpointer_to_overwritten_btree_node ? 0 : ret;
ret = drop_btree_ptrs(trans, &iter, b, dev_idx, flags);
bch2_trans_iter_exit(trans, &iter);
return ret;
}
static int bch2_dev_usrdata_drop(struct bch_fs *c,
struct progress_indicator_state *progress,
unsigned dev_idx, int flags)
unsigned dev_idx, unsigned flags)
{
struct btree_trans *trans = bch2_trans_get(c);
enum btree_id id;
@ -106,7 +144,7 @@ static int bch2_dev_usrdata_drop(struct bch_fs *c,
static int bch2_dev_metadata_drop(struct bch_fs *c,
struct progress_indicator_state *progress,
unsigned dev_idx, int flags)
unsigned dev_idx, unsigned flags)
{
struct btree_trans *trans;
struct btree_iter iter;
@ -137,20 +175,12 @@ retry:
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)
break;
ret = bch2_btree_node_update_key(trans, &iter, b, k.k, 0, false);
ret = drop_btree_ptrs(trans, &iter, b, dev_idx, flags);
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:
@ -176,7 +206,66 @@ err:
return ret;
}
int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
static int data_drop_bp(struct btree_trans *trans, unsigned dev_idx,
struct bkey_s_c_backpointer bp, struct bkey_buf *last_flushed,
unsigned flags)
{
struct btree_iter iter;
struct bkey_s_c k = bch2_backpointer_get_key(trans, bp, &iter, BTREE_ITER_intent,
last_flushed);
int ret = bkey_err(k);
if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
return 0;
if (ret)
return ret;
if (!k.k || !bch2_bkey_has_device_c(k, dev_idx))
goto out;
/*
* XXX: pass flags arg to invalidate_stripe_to_dev and handle it
* properly
*/
if (bkey_is_btree_ptr(k.k))
ret = bch2_dev_btree_drop_key(trans, bp, dev_idx, last_flushed, flags);
else if (k.k->type == KEY_TYPE_stripe)
ret = bch2_invalidate_stripe_to_dev(trans, &iter, k, dev_idx, flags);
else
ret = bch2_dev_usrdata_drop_key(trans, &iter, k, dev_idx, flags);
out:
bch2_trans_iter_exit(trans, &iter);
return ret;
}
int bch2_dev_data_drop_by_backpointers(struct bch_fs *c, unsigned dev_idx, unsigned flags)
{
struct btree_trans *trans = bch2_trans_get(c);
struct bkey_buf last_flushed;
bch2_bkey_buf_init(&last_flushed);
bkey_init(&last_flushed.k->k);
int ret = bch2_btree_write_buffer_flush_sync(trans) ?:
for_each_btree_key_max_commit(trans, iter, BTREE_ID_backpointers,
POS(dev_idx, 0),
POS(dev_idx, U64_MAX), 0, k,
NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
if (k.k->type != KEY_TYPE_backpointer)
continue;
data_drop_bp(trans, dev_idx, bkey_s_c_to_backpointer(k),
&last_flushed, flags);
}));
bch2_bkey_buf_exit(&last_flushed, trans->c);
bch2_trans_put(trans);
bch_err_fn(c, ret);
return ret;
}
int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, unsigned flags)
{
struct progress_indicator_state progress;
bch2_progress_init(&progress, c,

View File

@ -2,6 +2,7 @@
#ifndef _BCACHEFS_MIGRATE_H
#define _BCACHEFS_MIGRATE_H
int bch2_dev_data_drop(struct bch_fs *, unsigned, int);
int bch2_dev_data_drop_by_backpointers(struct bch_fs *, unsigned, unsigned);
int bch2_dev_data_drop(struct bch_fs *, unsigned, unsigned);
#endif /* _BCACHEFS_MIGRATE_H */