]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/migrate.c
Update bcachefs sources to f38382c574 bcachefs: Improve key marking interface
[bcachefs-tools-debian] / libbcachefs / migrate.c
1 /*
2  * Code for moving data off a device.
3  */
4
5 #include "bcachefs.h"
6 #include "btree_update.h"
7 #include "btree_update_interior.h"
8 #include "buckets.h"
9 #include "extents.h"
10 #include "io.h"
11 #include "journal.h"
12 #include "keylist.h"
13 #include "migrate.h"
14 #include "move.h"
15 #include "replicas.h"
16 #include "super-io.h"
17
18 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
19                          unsigned dev_idx, int flags, bool metadata)
20 {
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;
24         unsigned nr_good;
25
26         bch2_bkey_drop_device(k, dev_idx);
27
28         nr_good = bch2_bkey_durability(c, k.s_c);
29         if ((!nr_good && !(flags & lost)) ||
30             (nr_good < replicas && !(flags & degraded)))
31                 return -EINVAL;
32
33         return 0;
34 }
35
36 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
37 {
38         struct btree_trans trans;
39         struct btree_iter *iter;
40         struct bkey_s_c k;
41         BKEY_PADDED(key) tmp;
42         int ret = 0;
43
44         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
45
46         iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
47                                    POS_MIN, BTREE_ITER_PREFETCH);
48
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);
54                         if (ret)
55                                 break;
56                         bch2_btree_iter_next(iter);
57                         continue;
58                 }
59
60                 bkey_reassemble(&tmp.key, k);
61
62                 ret = drop_dev_ptrs(c, bkey_i_to_s(&tmp.key),
63                                     dev_idx, flags, false);
64                 if (ret)
65                         break;
66
67                 /*
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)
71                  */
72                 bch2_extent_normalize(c, bkey_i_to_s(&tmp.key));
73
74                 /* XXX not sketchy at all */
75                 iter->pos = bkey_start_pos(&tmp.key.k);
76
77                 bch2_trans_update(&trans, BTREE_INSERT_ENTRY(iter, &tmp.key));
78
79                 ret = bch2_trans_commit(&trans, NULL, NULL,
80                                         BTREE_INSERT_ATOMIC|
81                                         BTREE_INSERT_NOFAIL);
82
83                 /*
84                  * don't want to leave ret == -EINTR, since if we raced and
85                  * something else overwrote the key we could spuriously return
86                  * -EINTR below:
87                  */
88                 if (ret == -EINTR)
89                         ret = 0;
90                 if (ret)
91                         break;
92         }
93
94         ret = bch2_trans_exit(&trans) ?: ret;
95
96         BUG_ON(ret == -EINTR);
97
98         return ret;
99 }
100
101 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
102 {
103         struct btree_trans trans;
104         struct btree_iter *iter;
105         struct closure cl;
106         struct btree *b;
107         unsigned id;
108         int ret;
109
110         /* don't handle this yet: */
111         if (flags & BCH_FORCE_IF_METADATA_LOST)
112                 return -EINVAL;
113
114         bch2_trans_init(&trans, c, 0, 0);
115         closure_init_stack(&cl);
116
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;
122 retry:
123                         if (!bch2_bkey_has_device(bkey_i_to_s_c(&b->key),
124                                                   dev_idx)) {
125                                 /*
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:
130                                  */
131                                 bch2_btree_iter_downgrade(iter);
132
133                                 ret = bch2_mark_bkey_replicas(c, bkey_i_to_s_c(&b->key));
134                                 if (ret)
135                                         goto err;
136                         } else {
137                                 bkey_copy(&tmp.k, &b->key);
138                                 new_key = bkey_i_to_btree_ptr(&tmp.k);
139
140                                 ret = drop_dev_ptrs(c, bkey_i_to_s(&new_key->k_i),
141                                                     dev_idx, flags, true);
142                                 if (ret)
143                                         goto err;
144
145                                 ret = bch2_btree_node_update_key(c, iter, b, new_key);
146                                 if (ret == -EINTR) {
147                                         b = bch2_btree_iter_peek_node(iter);
148                                         goto retry;
149                                 }
150                                 if (ret)
151                                         goto err;
152                         }
153                 }
154                 bch2_trans_iter_free(&trans, iter);
155         }
156
157         /* flush relevant btree updates */
158         while (1) {
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))
163                         break;
164                 bch2_journal_meta(&c->journal);
165         }
166
167         ret = 0;
168 err:
169         ret = bch2_trans_exit(&trans) ?: ret;
170
171         BUG_ON(ret == -EINTR);
172
173         return ret;
174 }
175
176 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
177 {
178         return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
179                 bch2_dev_metadata_drop(c, dev_idx, flags) ?:
180                 bch2_replicas_gc2(c);
181 }