2 * Code for moving data off a device.
6 #include "btree_update.h"
16 static bool migrate_pred(void *arg, struct bkey_s_c_extent e)
18 struct bch_dev *ca = arg;
20 return bch2_extent_has_device(e, ca->dev_idx);
23 #define MAX_DATA_OFF_ITER 10
25 static int bch2_dev_usrdata_migrate(struct bch_fs *c, struct bch_dev *ca,
28 struct btree_iter iter;
30 struct bch_move_stats stats;
34 if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_USER)))
38 * XXX: we should be able to do this in one pass, but bch2_move_data()
39 * can spuriously fail to move an extent due to racing with other move
43 ret = bch2_move_data(c, NULL,
44 SECTORS_IN_FLIGHT_PER_DEVICE,
46 writepoint_hashed((unsigned long) current),
52 bch_err(c, "error migrating data: %i", ret);
55 } while (atomic64_read(&stats.keys_moved) && pass++ < MAX_DATA_OFF_ITER);
57 if (atomic64_read(&stats.keys_moved)) {
58 bch_err(c, "unable to migrate all data in %d iterations",
63 mutex_lock(&c->replicas_gc_lock);
64 bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
66 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, BTREE_ITER_PREFETCH, k) {
67 ret = bch2_check_mark_super(c, BCH_DATA_USER, bch2_bkey_devs(k));
69 bch_err(c, "error migrating data %i from check_mark_super()", ret);
74 bch2_replicas_gc_end(c, ret);
75 mutex_unlock(&c->replicas_gc_lock);
79 static int bch2_dev_metadata_migrate(struct bch_fs *c, struct bch_dev *ca,
82 struct btree_iter iter;
87 if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_BTREE)))
90 mutex_lock(&c->replicas_gc_lock);
91 bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
93 for (id = 0; id < BTREE_ID_NR; id++) {
94 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
95 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
97 if (!bch2_extent_has_device(e, ca->dev_idx))
100 ret = bch2_btree_node_rewrite(c, &iter, b->data->keys.seq, 0);
102 bch2_btree_iter_unlock(&iter);
106 ret = bch2_btree_iter_unlock(&iter);
111 bch2_replicas_gc_end(c, ret);
112 mutex_unlock(&c->replicas_gc_lock);
116 int bch2_dev_data_migrate(struct bch_fs *c, struct bch_dev *ca, int flags)
118 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW &&
119 bch2_dev_is_online(ca));
121 return bch2_dev_usrdata_migrate(c, ca, flags) ?:
122 bch2_dev_metadata_migrate(c, ca, flags);
125 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
126 unsigned dev_idx, int flags, bool metadata)
128 unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
129 unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
130 unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
133 bch2_extent_drop_device(e, dev_idx);
135 nr_good = bch2_extent_nr_good_ptrs(c, e.c);
136 if ((!nr_good && !(flags & lost)) ||
137 (nr_good < replicas && !(flags & degraded)))
143 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
146 struct bkey_s_extent e;
147 BKEY_PADDED(key) tmp;
148 struct btree_iter iter;
151 mutex_lock(&c->replicas_gc_lock);
152 bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
154 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
155 POS_MIN, BTREE_ITER_PREFETCH);
157 while ((k = bch2_btree_iter_peek(&iter)).k &&
158 !(ret = btree_iter_err(k))) {
159 if (!bkey_extent_is_data(k.k) ||
160 !bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx)) {
161 ret = bch2_check_mark_super(c, BCH_DATA_USER,
165 bch2_btree_iter_next(&iter);
169 bkey_reassemble(&tmp.key, k);
170 e = bkey_i_to_s_extent(&tmp.key);
172 ret = drop_dev_ptrs(c, e, dev_idx, flags, false);
177 * If the new extent no longer has any pointers, bch2_extent_normalize()
178 * will do the appropriate thing with it (turning it into a
179 * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
181 bch2_extent_normalize(c, e.s);
183 ret = bch2_check_mark_super(c, BCH_DATA_USER,
184 bch2_bkey_devs(bkey_i_to_s_c(&tmp.key)));
188 iter.pos = bkey_start_pos(&tmp.key.k);
190 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
193 BTREE_INSERT_ENTRY(&iter, &tmp.key));
196 * don't want to leave ret == -EINTR, since if we raced and
197 * something else overwrote the key we could spuriously return
206 bch2_btree_iter_unlock(&iter);
208 bch2_replicas_gc_end(c, ret);
209 mutex_unlock(&c->replicas_gc_lock);
214 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
216 struct btree_iter iter;
222 /* don't handle this yet: */
223 if (flags & BCH_FORCE_IF_METADATA_LOST)
226 closure_init_stack(&cl);
228 mutex_lock(&c->replicas_gc_lock);
229 bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
231 for (id = 0; id < BTREE_ID_NR; id++) {
232 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
233 __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
234 struct bkey_i_extent *new_key;
236 if (!bch2_extent_has_device(bkey_i_to_s_c_extent(&b->key),
238 bch2_btree_iter_set_locks_want(&iter, 0);
240 ret = bch2_check_mark_super(c, BCH_DATA_BTREE,
241 bch2_bkey_devs(bkey_i_to_s_c(&b->key)));
245 bkey_copy(&tmp.k, &b->key);
246 new_key = bkey_i_to_extent(&tmp.k);
248 ret = drop_dev_ptrs(c, extent_i_to_s(new_key),
249 dev_idx, flags, true);
253 if (!bch2_btree_iter_set_locks_want(&iter, U8_MAX)) {
254 b = bch2_btree_iter_peek_node(&iter);
258 ret = bch2_btree_node_update_key(c, &iter, b, new_key);
260 b = bch2_btree_iter_peek_node(&iter);
267 bch2_btree_iter_unlock(&iter);
272 bch2_replicas_gc_end(c, ret);
273 mutex_unlock(&c->replicas_gc_lock);
277 bch2_btree_iter_unlock(&iter);
281 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
283 return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
284 bch2_dev_metadata_drop(c, dev_idx, flags);