1 // SPDX-License-Identifier: GPL-2.0
3 * Code for moving data off a device.
8 #include "btree_update.h"
9 #include "btree_update_interior.h"
21 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
22 unsigned dev_idx, int flags, bool metadata)
24 unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
25 unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
26 unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
29 bch2_bkey_drop_device(k, dev_idx);
31 nr_good = bch2_bkey_durability(c, k.s_c);
32 if ((!nr_good && !(flags & lost)) ||
33 (nr_good < replicas && !(flags & degraded)))
39 static int bch2_dev_usrdata_drop_key(struct btree_trans *trans,
40 struct btree_iter *iter,
45 struct bch_fs *c = trans->c;
49 if (!bch2_bkey_has_device_c(k, dev_idx))
52 n = bch2_bkey_make_mut(trans, k);
53 ret = PTR_ERR_OR_ZERO(n);
57 ret = drop_dev_ptrs(c, bkey_i_to_s(n), dev_idx, flags, false);
62 * If the new extent no longer has any pointers, bch2_extent_normalize()
63 * will do the appropriate thing with it (turning it into a
64 * KEY_TYPE_error key, or just a discard if it was a cached extent)
66 bch2_extent_normalize(c, bkey_i_to_s(n));
69 * Since we're not inserting through an extent iterator
70 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
71 * we aren't using the extent overwrite path to delete, we're
72 * just using the normal key deletion path:
74 if (bkey_deleted(&n->k))
77 return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
80 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
82 struct btree_trans trans;
83 struct btree_iter iter;
88 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
90 for (id = 0; id < BTREE_ID_NR; id++) {
91 if (!btree_type_has_ptrs(id))
94 ret = for_each_btree_key_commit(&trans, iter, id, POS_MIN,
95 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
96 NULL, NULL, BTREE_INSERT_NOFAIL,
97 bch2_dev_usrdata_drop_key(&trans, &iter, k, dev_idx, flags));
102 bch2_trans_exit(&trans);
107 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
109 struct btree_trans trans;
110 struct btree_iter iter;
117 /* don't handle this yet: */
118 if (flags & BCH_FORCE_IF_METADATA_LOST)
121 bch2_bkey_buf_init(&k);
122 bch2_trans_init(&trans, c, 0, 0);
123 closure_init_stack(&cl);
125 for (id = 0; id < BTREE_ID_NR; id++) {
126 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
127 BTREE_ITER_PREFETCH);
130 while (bch2_trans_begin(&trans),
131 (b = bch2_btree_iter_peek_node(&iter)) &&
132 !(ret = PTR_ERR_OR_ZERO(b))) {
133 if (!bch2_bkey_has_device_c(bkey_i_to_s_c(&b->key), dev_idx))
136 bch2_bkey_buf_copy(&k, c, &b->key);
138 ret = drop_dev_ptrs(c, bkey_i_to_s(k.k),
139 dev_idx, flags, true);
141 bch_err(c, "Cannot drop device without losing data");
145 ret = bch2_btree_node_update_key(&trans, &iter, b, k.k, false);
146 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
152 bch_err(c, "Error updating btree node key: %s",
157 bch2_btree_iter_next_node(&iter);
159 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
162 bch2_trans_iter_exit(&trans, &iter);
168 bch2_btree_interior_updates_flush(c);
171 bch2_trans_exit(&trans);
172 bch2_bkey_buf_exit(&k, c);
174 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
179 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
181 return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
182 bch2_dev_metadata_drop(c, dev_idx, flags);