2 * Code for moving data off a device.
6 #include "btree_update.h"
17 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
18 unsigned dev_idx, int flags, bool metadata)
20 unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
21 unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
22 unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
25 bch2_bkey_drop_device(k, dev_idx);
27 nr_good = bch2_bkey_durability(c, k.s_c);
28 if ((!nr_good && !(flags & lost)) ||
29 (nr_good < replicas && !(flags & degraded)))
35 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
39 struct btree_iter iter;
42 mutex_lock(&c->replicas_gc_lock);
43 bch2_replicas_gc_start(c, (1 << BCH_DATA_USER)|(1 << BCH_DATA_CACHED));
45 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
46 POS_MIN, BTREE_ITER_PREFETCH);
48 while ((k = bch2_btree_iter_peek(&iter)).k &&
49 !(ret = btree_iter_err(k))) {
50 if (!bkey_extent_is_data(k.k) ||
51 !bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx)) {
52 ret = bch2_mark_bkey_replicas(c, k);
55 bch2_btree_iter_next(&iter);
59 bkey_reassemble(&tmp.key, k);
61 ret = drop_dev_ptrs(c, bkey_i_to_s(&tmp.key),
62 dev_idx, flags, false);
67 * If the new extent no longer has any pointers, bch2_extent_normalize()
68 * will do the appropriate thing with it (turning it into a
69 * KEY_TYPE_error key, or just a discard if it was a cached extent)
71 bch2_extent_normalize(c, bkey_i_to_s(&tmp.key));
73 iter.pos = bkey_start_pos(&tmp.key.k);
75 ret = bch2_btree_insert_at(c, NULL, NULL,
78 BTREE_INSERT_ENTRY(&iter, &tmp.key));
81 * don't want to leave ret == -EINTR, since if we raced and
82 * something else overwrote the key we could spuriously return
91 bch2_btree_iter_unlock(&iter);
93 bch2_replicas_gc_end(c, ret);
94 mutex_unlock(&c->replicas_gc_lock);
99 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
101 struct btree_iter iter;
107 /* don't handle this yet: */
108 if (flags & BCH_FORCE_IF_METADATA_LOST)
111 closure_init_stack(&cl);
113 mutex_lock(&c->replicas_gc_lock);
114 bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
116 for (id = 0; id < BTREE_ID_NR; id++) {
117 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
118 __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
119 struct bkey_i_btree_ptr *new_key;
121 if (!bch2_bkey_has_device(bkey_i_to_s_c(&b->key),
124 * we might have found a btree node key we
125 * needed to update, and then tried to update it
126 * but got -EINTR after upgrading the iter, but
127 * then raced and the node is now gone:
129 bch2_btree_iter_downgrade(&iter);
131 ret = bch2_mark_bkey_replicas(c, bkey_i_to_s_c(&b->key));
135 bkey_copy(&tmp.k, &b->key);
136 new_key = bkey_i_to_btree_ptr(&tmp.k);
138 ret = drop_dev_ptrs(c, bkey_i_to_s(&new_key->k_i),
139 dev_idx, flags, true);
143 ret = bch2_btree_node_update_key(c, &iter, b, new_key);
145 b = bch2_btree_iter_peek_node(&iter);
152 bch2_btree_iter_unlock(&iter);
157 ret = bch2_replicas_gc_end(c, ret);
158 mutex_unlock(&c->replicas_gc_lock);
162 bch2_btree_iter_unlock(&iter);
166 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
168 return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
169 bch2_dev_metadata_drop(c, dev_idx, flags);