2 * Code for moving data off a device.
6 #include "btree_update.h"
7 #include "btree_update_interior.h"
18 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
19 unsigned dev_idx, int flags, bool metadata)
21 unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
22 unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
23 unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
26 bch2_bkey_drop_device(k, dev_idx);
28 nr_good = bch2_bkey_durability(c, k.s_c);
29 if ((!nr_good && !(flags & lost)) ||
30 (nr_good < replicas && !(flags & degraded)))
36 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
38 struct btree_trans trans;
39 struct btree_iter *iter;
44 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
46 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
47 POS_MIN, BTREE_ITER_PREFETCH);
49 while ((k = bch2_btree_iter_peek(iter)).k &&
50 !(ret = bkey_err(k))) {
51 if (!bkey_extent_is_data(k.k) ||
52 !bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx)) {
53 ret = bch2_mark_bkey_replicas(c, k);
56 bch2_btree_iter_next(iter);
60 bkey_reassemble(&tmp.key, k);
62 ret = drop_dev_ptrs(c, bkey_i_to_s(&tmp.key),
63 dev_idx, flags, false);
68 * If the new extent no longer has any pointers, bch2_extent_normalize()
69 * will do the appropriate thing with it (turning it into a
70 * KEY_TYPE_error key, or just a discard if it was a cached extent)
72 bch2_extent_normalize(c, bkey_i_to_s(&tmp.key));
74 /* XXX not sketchy at all */
75 iter->pos = bkey_start_pos(&tmp.key.k);
77 bch2_trans_update(&trans, BTREE_INSERT_ENTRY(iter, &tmp.key));
79 ret = bch2_trans_commit(&trans, NULL, NULL,
84 * don't want to leave ret == -EINTR, since if we raced and
85 * something else overwrote the key we could spuriously return
94 ret = bch2_trans_exit(&trans) ?: ret;
96 BUG_ON(ret == -EINTR);
101 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
103 struct btree_trans trans;
104 struct btree_iter *iter;
110 /* don't handle this yet: */
111 if (flags & BCH_FORCE_IF_METADATA_LOST)
114 bch2_trans_init(&trans, c, 0, 0);
115 closure_init_stack(&cl);
117 for (id = 0; id < BTREE_ID_NR; id++) {
118 for_each_btree_node(&trans, iter, id, POS_MIN,
119 BTREE_ITER_PREFETCH, b) {
120 __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
121 struct bkey_i_btree_ptr *new_key;
123 if (!bch2_bkey_has_device(bkey_i_to_s_c(&b->key),
126 * we might have found a btree node key we
127 * needed to update, and then tried to update it
128 * but got -EINTR after upgrading the iter, but
129 * then raced and the node is now gone:
131 bch2_btree_iter_downgrade(iter);
133 ret = bch2_mark_bkey_replicas(c, bkey_i_to_s_c(&b->key));
137 bkey_copy(&tmp.k, &b->key);
138 new_key = bkey_i_to_btree_ptr(&tmp.k);
140 ret = drop_dev_ptrs(c, bkey_i_to_s(&new_key->k_i),
141 dev_idx, flags, true);
145 ret = bch2_btree_node_update_key(c, iter, b, new_key);
147 b = bch2_btree_iter_peek_node(iter);
154 bch2_trans_iter_free(&trans, iter);
157 /* flush relevant btree updates */
159 closure_wait_event(&c->btree_interior_update_wait,
160 !bch2_btree_interior_updates_nr_pending(c) ||
161 c->btree_roots_dirty);
162 if (!bch2_btree_interior_updates_nr_pending(c))
164 bch2_journal_meta(&c->journal);
169 ret = bch2_trans_exit(&trans) ?: ret;
171 BUG_ON(ret == -EINTR);
176 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
178 return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
179 bch2_dev_metadata_drop(c, dev_idx, flags) ?:
180 bch2_replicas_gc2(c);