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;
62 struct bch_sb_field_members *mi;
67 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
72 bch2_move_ctxt_init(&ctxt, NULL, SECTORS_IN_FLIGHT_PER_DEVICE);
76 * In theory, only one pass should be necessary as we've
77 * quiesced all writes before calling this.
79 * However, in practice, more than one pass may be necessary:
80 * - Some move fails due to an error. We can can find this out
81 * from the moving_context.
82 * - Some key swap failed because some of the pointers in the
83 * key in the tree changed due to caching behavior, btree gc
84 * pruning stale pointers, or tiering (if the device being
85 * removed is in tier 0). A smarter bkey_cmpxchg would
88 * Thus this scans the tree one more time than strictly necessary,
89 * but that can be viewed as a verification pass.
93 struct btree_iter iter;
97 atomic_set(&ctxt.error_count, 0);
98 atomic_set(&ctxt.error_flags, 0);
100 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS, POS_MIN,
101 BTREE_ITER_PREFETCH);
103 while (!bch2_move_ctxt_wait(&ctxt) &&
104 (k = bch2_btree_iter_peek(&iter)).k &&
105 !(ret = btree_iter_err(k))) {
106 if (!bkey_extent_is_data(k.k) ||
107 !bch2_extent_has_device(bkey_s_c_to_extent(k),
111 ret = issue_migration_move(ca, &ctxt, k);
112 if (ret == -ENOMEM) {
113 bch2_btree_iter_unlock(&iter);
116 * memory allocation failure, wait for some IO
119 bch2_move_ctxt_wait_for_io(&ctxt);
128 bch2_btree_iter_advance_pos(&iter);
129 bch2_btree_iter_cond_resched(&iter);
132 bch2_btree_iter_unlock(&iter);
133 bch2_move_ctxt_exit(&ctxt);
137 } while (seen_key_count && pass++ < MAX_DATA_OFF_ITER);
139 if (seen_key_count) {
140 pr_err("Unable to migrate all data in %d iterations.",
145 mutex_lock(&c->sb_lock);
146 mi = bch2_sb_get_members(c->disk_sb);
147 SET_BCH_MEMBER_HAS_DATA(&mi->members[ca->dev_idx], false);
150 mutex_unlock(&c->sb_lock);
156 * This walks the btree, and for any node on the relevant device it moves the
159 static int bch2_move_btree_off(struct bch_dev *ca, enum btree_id id)
161 struct bch_fs *c = ca->fs;
162 struct btree_iter iter;
167 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
169 closure_init_stack(&cl);
171 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
172 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
174 if (!bch2_extent_has_device(e, ca->dev_idx))
177 ret = bch2_btree_node_rewrite(&iter, b, &cl);
178 if (ret == -EINTR || ret == -ENOSPC) {
180 * Drop locks to upgrade locks or wait on
181 * reserve: after retaking, recheck in case we
184 bch2_btree_iter_unlock(&iter);
186 b = bch2_btree_iter_peek_node(&iter);
190 bch2_btree_iter_unlock(&iter);
194 bch2_btree_iter_set_locks_want(&iter, 0);
196 ret = bch2_btree_iter_unlock(&iter);
198 return ret; /* btree IO error */
200 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
201 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
202 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
204 BUG_ON(bch2_extent_has_device(e, ca->dev_idx));
206 bch2_btree_iter_unlock(&iter);
213 * This moves only the meta-data off, leaving the data (if any) in place.
214 * The data is moved off by bch_move_data_off_device, if desired, and
217 * Before calling this, allocation of buckets to the device must have
218 * been disabled, as else we'll continue to write meta-data to the device
219 * when new buckets are picked for meta-data writes.
220 * In addition, the copying gc and allocator threads for the device
221 * must have been stopped. The allocator thread is the only thread
222 * that writes prio/gen information.
224 * Meta-data consists of:
226 * - Prio/gen information
230 * This has to move the btree nodes and the journal only:
231 * - prio/gen information is not written once the allocator thread is stopped.
232 * also, as the prio/gen information is per-device it is not moved.
233 * - the superblock will be written by the caller once after everything
236 * Note that currently there is no way to stop btree node and journal
237 * meta-data writes to a device without moving the meta-data because
238 * once a bucket is open for a btree node, unless a replacement btree
239 * node is allocated (and the tree updated), the bucket will continue
240 * to be written with updates. Similarly for the journal (it gets
241 * written until filled).
243 * This routine leaves the data (if any) in place. Whether the data
244 * should be moved off is a decision independent of whether the meta
245 * data should be moved off and stopped:
247 * - For device removal, both data and meta-data are moved off, in
250 * - However, for turning a device read-only without removing it, only
251 * meta-data is moved off since that's the only way to prevent it
252 * from being written. Data is left in the device, but no new data
256 int bch2_move_metadata_off_device(struct bch_dev *ca)
258 struct bch_fs *c = ca->fs;
259 struct bch_sb_field_members *mi;
263 BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
265 if (!ca->mi.has_metadata)
268 /* 1st, Move the btree nodes off the device */
270 for (i = 0; i < BTREE_ID_NR; i++) {
271 ret = bch2_move_btree_off(ca, i);
276 /* There are no prios/gens to move -- they are already in the device. */
278 /* 2nd. Move the journal off the device */
280 ret = bch2_journal_move(ca);
284 mutex_lock(&c->sb_lock);
285 mi = bch2_sb_get_members(c->disk_sb);
286 SET_BCH_MEMBER_HAS_METADATA(&mi->members[ca->dev_idx], false);
289 mutex_unlock(&c->sb_lock);
295 * Flagging data bad when forcibly removing a device after failing to
296 * migrate the data off the device.
299 static int bch2_flag_key_bad(struct btree_iter *iter,
301 struct bkey_s_c_extent orig)
303 BKEY_PADDED(key) tmp;
304 struct bkey_s_extent e;
305 struct bch_extent_ptr *ptr;
306 struct bch_fs *c = ca->fs;
308 bkey_reassemble(&tmp.key, orig.s_c);
309 e = bkey_i_to_s_extent(&tmp.key);
311 extent_for_each_ptr_backwards(e, ptr)
312 if (ptr->dev == ca->dev_idx)
313 bch2_extent_drop_ptr(e, ptr);
316 * If the new extent no longer has any pointers, bch2_extent_normalize()
317 * will do the appropriate thing with it (turning it into a
318 * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
320 bch2_extent_normalize(c, e.s);
322 return bch2_btree_insert_at(c, NULL, NULL, NULL,
324 BTREE_INSERT_ENTRY(iter, &tmp.key));
328 * This doesn't actually move any data -- it marks the keys as bad
329 * if they contain a pointer to a device that is forcibly removed
330 * and don't have other valid pointers. If there are valid pointers,
331 * the necessary pointers to the removed device are replaced with
332 * bad pointers instead.
334 * This is only called if bch_move_data_off_device above failed, meaning
335 * that we've already tried to move the data MAX_DATA_OFF_ITER times and
336 * are not likely to succeed if we try again.
338 int bch2_flag_data_bad(struct bch_dev *ca)
342 struct bkey_s_c_extent e;
343 struct btree_iter iter;
345 bch2_btree_iter_init(&iter, ca->fs, BTREE_ID_EXTENTS,
346 POS_MIN, BTREE_ITER_PREFETCH);
348 while ((k = bch2_btree_iter_peek(&iter)).k &&
349 !(ret = btree_iter_err(k))) {
350 if (!bkey_extent_is_data(k.k))
353 e = bkey_s_c_to_extent(k);
354 if (!bch2_extent_has_device(e, ca->dev_idx))
357 ret = bch2_flag_key_bad(&iter, ca, e);
360 * don't want to leave ret == -EINTR, since if we raced and
361 * something else overwrote the key we could spuriously return
370 * If the replica we're dropping was dirty and there is an
371 * additional cached replica, the cached replica will now be
372 * considered dirty - upon inserting the new version of the key,
373 * the bucket accounting will be updated to reflect the fact
374 * that the cached data is now dirty and everything works out as
375 * if by magic without us having to do anything.
377 * The one thing we need to be concerned with here is there's a
378 * race between when we drop any stale pointers from the key
379 * we're about to insert, and when the key actually gets
380 * inserted and the cached data is marked as dirty - we could
381 * end up trying to insert a key with a pointer that should be
382 * dirty, but points to stale data.
384 * If that happens the insert code just bails out and doesn't do
385 * the insert - however, it doesn't return an error. Hence we
386 * need to always recheck the current key before advancing to
391 bch2_btree_iter_advance_pos(&iter);
394 bch2_btree_iter_unlock(&iter);