2 * Code for moving data off a device.
6 #include "btree_update.h"
16 static int issue_migration_move(struct bch_dev *ca,
17 struct moving_context *ctxt,
20 struct bch_fs *c = ca->fs;
21 struct disk_reservation res;
22 const struct bch_extent_ptr *ptr;
25 if (bch2_disk_reservation_get(c, &res, k.k->size, 0))
28 extent_for_each_ptr(bkey_s_c_to_extent(k), ptr)
29 if (ptr->dev == ca->dev_idx)
34 /* XXX: we need to be doing something with the disk reservation */
36 ret = bch2_data_move(c, ctxt, &c->migration_write_point, k, ptr);
38 bch2_disk_reservation_put(c, &res);
42 #define MAX_DATA_OFF_ITER 10
45 * This moves only the data off, leaving the meta-data (if any) in place.
46 * It walks the key space, and for any key with a valid pointer to the
47 * relevant device, it copies it elsewhere, updating the key to point to
49 * The meta-data is moved off by bch_move_meta_data_off_device.
51 * Note: If the number of data replicas desired is > 1, ideally, any
52 * new copies would not be made in the same device that already have a
53 * copy (if there are enough devices).
54 * This is _not_ currently implemented. The multiple replicas can
55 * land in the same device even if there are others available.
58 int bch2_move_data_off_device(struct bch_dev *ca)
60 struct moving_context ctxt;
61 struct bch_fs *c = ca->fs;
66 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
68 if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_USER)))
71 mutex_lock(&c->replicas_gc_lock);
72 bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
74 bch2_move_ctxt_init(&ctxt, NULL, SECTORS_IN_FLIGHT_PER_DEVICE);
78 * In theory, only one pass should be necessary as we've
79 * quiesced all writes before calling this.
81 * However, in practice, more than one pass may be necessary:
82 * - Some move fails due to an error. We can can find this out
83 * from the moving_context.
84 * - Some key swap failed because some of the pointers in the
85 * key in the tree changed due to caching behavior, btree gc
86 * pruning stale pointers, or tiering (if the device being
87 * removed is in tier 0). A smarter bkey_cmpxchg would
90 * Thus this scans the tree one more time than strictly necessary,
91 * but that can be viewed as a verification pass.
95 struct btree_iter iter;
99 atomic_set(&ctxt.error_count, 0);
100 atomic_set(&ctxt.error_flags, 0);
102 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS, POS_MIN,
103 BTREE_ITER_PREFETCH);
105 while (!bch2_move_ctxt_wait(&ctxt) &&
106 (k = bch2_btree_iter_peek(&iter)).k &&
107 !(ret = btree_iter_err(k))) {
108 if (!bkey_extent_is_data(k.k) ||
109 !bch2_extent_has_device(bkey_s_c_to_extent(k),
113 ret = issue_migration_move(ca, &ctxt, k);
114 if (ret == -ENOMEM) {
115 bch2_btree_iter_unlock(&iter);
118 * memory allocation failure, wait for some IO
121 bch2_move_ctxt_wait_for_io(&ctxt);
131 if (bkey_extent_is_data(k.k)) {
132 ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
137 bch2_btree_iter_advance_pos(&iter);
138 bch2_btree_iter_cond_resched(&iter);
141 bch2_btree_iter_unlock(&iter);
142 bch2_move_ctxt_exit(&ctxt);
146 } while (seen_key_count && pass++ < MAX_DATA_OFF_ITER);
148 if (seen_key_count) {
149 pr_err("Unable to migrate all data in %d iterations.",
156 bch2_replicas_gc_end(c, ret);
157 mutex_unlock(&c->replicas_gc_lock);
162 * This walks the btree, and for any node on the relevant device it moves the
165 static int bch2_move_btree_off(struct bch_fs *c, struct bch_dev *ca,
168 struct btree_iter iter;
173 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
175 closure_init_stack(&cl);
177 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
178 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
180 if (!bch2_extent_has_device(e, ca->dev_idx))
183 ret = bch2_btree_node_rewrite(c, &iter, b->data->keys.seq, 0);
185 bch2_btree_iter_unlock(&iter);
189 bch2_btree_iter_set_locks_want(&iter, 0);
191 ret = bch2_btree_iter_unlock(&iter);
193 return ret; /* btree IO error */
195 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
196 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
197 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
199 BUG_ON(bch2_extent_has_device(e, ca->dev_idx));
201 bch2_btree_iter_unlock(&iter);
208 * This moves only the meta-data off, leaving the data (if any) in place.
209 * The data is moved off by bch_move_data_off_device, if desired, and
212 * Before calling this, allocation of buckets to the device must have
213 * been disabled, as else we'll continue to write meta-data to the device
214 * when new buckets are picked for meta-data writes.
215 * In addition, the copying gc and allocator threads for the device
216 * must have been stopped. The allocator thread is the only thread
217 * that writes prio/gen information.
219 * Meta-data consists of:
221 * - Prio/gen information
225 * This has to move the btree nodes and the journal only:
226 * - prio/gen information is not written once the allocator thread is stopped.
227 * also, as the prio/gen information is per-device it is not moved.
228 * - the superblock will be written by the caller once after everything
231 * Note that currently there is no way to stop btree node and journal
232 * meta-data writes to a device without moving the meta-data because
233 * once a bucket is open for a btree node, unless a replacement btree
234 * node is allocated (and the tree updated), the bucket will continue
235 * to be written with updates. Similarly for the journal (it gets
236 * written until filled).
238 * This routine leaves the data (if any) in place. Whether the data
239 * should be moved off is a decision independent of whether the meta
240 * data should be moved off and stopped:
242 * - For device removal, both data and meta-data are moved off, in
245 * - However, for turning a device read-only without removing it, only
246 * meta-data is moved off since that's the only way to prevent it
247 * from being written. Data is left in the device, but no new data
251 int bch2_move_metadata_off_device(struct bch_dev *ca)
253 struct bch_fs *c = ca->fs;
257 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
259 if (!(bch2_dev_has_data(c, ca) &
260 ((1 << BCH_DATA_JOURNAL)|
261 (1 << BCH_DATA_BTREE))))
264 mutex_lock(&c->replicas_gc_lock);
265 bch2_replicas_gc_start(c,
266 (1 << BCH_DATA_JOURNAL)|
267 (1 << BCH_DATA_BTREE));
269 /* 1st, Move the btree nodes off the device */
271 for (i = 0; i < BTREE_ID_NR; i++) {
272 ret = bch2_move_btree_off(c, ca, i);
277 /* There are no prios/gens to move -- they are already in the device. */
279 /* 2nd. Move the journal off the device */
281 ret = bch2_journal_move(ca);
286 bch2_replicas_gc_end(c, ret);
287 mutex_unlock(&c->replicas_gc_lock);
292 * Flagging data bad when forcibly removing a device after failing to
293 * migrate the data off the device.
296 static int bch2_flag_key_bad(struct btree_iter *iter,
298 struct bkey_s_c_extent orig)
300 BKEY_PADDED(key) tmp;
301 struct bkey_s_extent e;
302 struct bch_extent_ptr *ptr;
303 struct bch_fs *c = ca->fs;
305 bkey_reassemble(&tmp.key, orig.s_c);
306 e = bkey_i_to_s_extent(&tmp.key);
308 extent_for_each_ptr_backwards(e, ptr)
309 if (ptr->dev == ca->dev_idx)
310 bch2_extent_drop_ptr(e, ptr);
313 * If the new extent no longer has any pointers, bch2_extent_normalize()
314 * will do the appropriate thing with it (turning it into a
315 * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
317 bch2_extent_normalize(c, e.s);
319 return bch2_btree_insert_at(c, NULL, NULL, NULL,
321 BTREE_INSERT_ENTRY(iter, &tmp.key));
325 * This doesn't actually move any data -- it marks the keys as bad
326 * if they contain a pointer to a device that is forcibly removed
327 * and don't have other valid pointers. If there are valid pointers,
328 * the necessary pointers to the removed device are replaced with
329 * bad pointers instead.
331 * This is only called if bch_move_data_off_device above failed, meaning
332 * that we've already tried to move the data MAX_DATA_OFF_ITER times and
333 * are not likely to succeed if we try again.
335 int bch2_flag_data_bad(struct bch_dev *ca)
337 struct bch_fs *c = ca->fs;
339 struct bkey_s_c_extent e;
340 struct btree_iter iter;
343 mutex_lock(&c->replicas_gc_lock);
344 bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
346 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
347 POS_MIN, BTREE_ITER_PREFETCH);
349 while ((k = bch2_btree_iter_peek(&iter)).k &&
350 !(ret = btree_iter_err(k))) {
351 if (!bkey_extent_is_data(k.k))
354 e = bkey_s_c_to_extent(k);
355 if (!bch2_extent_has_device(e, ca->dev_idx))
358 ret = bch2_flag_key_bad(&iter, ca, e);
361 * don't want to leave ret == -EINTR, since if we raced and
362 * something else overwrote the key we could spuriously return
371 * If the replica we're dropping was dirty and there is an
372 * additional cached replica, the cached replica will now be
373 * considered dirty - upon inserting the new version of the key,
374 * the bucket accounting will be updated to reflect the fact
375 * that the cached data is now dirty and everything works out as
376 * if by magic without us having to do anything.
378 * The one thing we need to be concerned with here is there's a
379 * race between when we drop any stale pointers from the key
380 * we're about to insert, and when the key actually gets
381 * inserted and the cached data is marked as dirty - we could
382 * end up trying to insert a key with a pointer that should be
383 * dirty, but points to stale data.
385 * If that happens the insert code just bails out and doesn't do
386 * the insert - however, it doesn't return an error. Hence we
387 * need to always recheck the current key before advancing to
392 if (bkey_extent_is_data(k.k)) {
393 ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
398 bch2_btree_iter_advance_pos(&iter);
401 bch2_btree_iter_unlock(&iter);
403 bch2_replicas_gc_end(c, ret);
404 mutex_unlock(&c->replicas_gc_lock);