]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/migrate.c
Update bcachefs sources to 14ce2a2031 bcachefs: fixes for building in userspace
[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 "buckets.h"
8 #include "extents.h"
9 #include "io.h"
10 #include "journal.h"
11 #include "keylist.h"
12 #include "migrate.h"
13 #include "move.h"
14 #include "super-io.h"
15
16 static bool migrate_pred(void *arg, struct bkey_s_c_extent e)
17 {
18         struct bch_dev *ca = arg;
19         const struct bch_extent_ptr *ptr;
20
21         extent_for_each_ptr(e, ptr)
22                 if (ptr->dev == ca->dev_idx)
23                         return true;
24
25         return false;
26 }
27
28 #define MAX_DATA_OFF_ITER       10
29
30 static int bch2_dev_usrdata_migrate(struct bch_fs *c, struct bch_dev *ca,
31                                     int flags)
32 {
33         struct btree_iter iter;
34         struct bkey_s_c k;
35         u64 keys_moved, sectors_moved;
36         unsigned pass = 0;
37         int ret = 0;
38
39         BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
40
41         if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_USER)))
42                 return 0;
43
44         /*
45          * In theory, only one pass should be necessary as we've
46          * quiesced all writes before calling this.
47          *
48          * However, in practice, more than one pass may be necessary:
49          * - Some move fails due to an error. We can can find this out
50          *   from the moving_context.
51          * - Some key swap failed because some of the pointers in the
52          *   key in the tree changed due to caching behavior, btree gc
53          *   pruning stale pointers, or tiering (if the device being
54          *   removed is in tier 0).  A smarter bkey_cmpxchg would
55          *   handle these cases.
56          *
57          * Thus this scans the tree one more time than strictly necessary,
58          * but that can be viewed as a verification pass.
59          */
60         do {
61                 ret = bch2_move_data(c, NULL,
62                                      SECTORS_IN_FLIGHT_PER_DEVICE,
63                                      NULL,
64                                      writepoint_hashed((unsigned long) current),
65                                      0,
66                                      ca->dev_idx,
67                                      migrate_pred, ca,
68                                      &keys_moved,
69                                      &sectors_moved);
70                 if (ret) {
71                         bch_err(c, "error migrating data: %i", ret);
72                         return ret;
73                 }
74         } while (keys_moved && pass++ < MAX_DATA_OFF_ITER);
75
76         if (keys_moved) {
77                 bch_err(c, "unable to migrate all data in %d iterations",
78                         MAX_DATA_OFF_ITER);
79                 return -1;
80         }
81
82         mutex_lock(&c->replicas_gc_lock);
83         bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
84
85         for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, BTREE_ITER_PREFETCH, k) {
86                 if (!bkey_extent_is_data(k.k))
87                         continue;
88
89                 ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
90                                             BCH_DATA_USER);
91                 if (ret) {
92                         bch_err(c, "error migrating data %i from check_mark_super()", ret);
93                         break;
94                 }
95         }
96
97         bch2_replicas_gc_end(c, ret);
98         mutex_unlock(&c->replicas_gc_lock);
99         return ret;
100 }
101
102 static int bch2_move_btree_off(struct bch_fs *c, struct bch_dev *ca,
103                                enum btree_id id)
104 {
105         struct btree_iter iter;
106         struct btree *b;
107         int ret;
108
109         BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
110
111         for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
112                 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
113
114                 if (!bch2_extent_has_device(e, ca->dev_idx))
115                         continue;
116
117                 ret = bch2_btree_node_rewrite(c, &iter, b->data->keys.seq, 0);
118                 if (ret) {
119                         bch2_btree_iter_unlock(&iter);
120                         return ret;
121                 }
122
123                 bch2_btree_iter_set_locks_want(&iter, 0);
124         }
125         ret = bch2_btree_iter_unlock(&iter);
126         if (ret)
127                 return ret; /* btree IO error */
128
129         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
130                 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
131                         struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
132
133                         BUG_ON(bch2_extent_has_device(e, ca->dev_idx));
134                 }
135                 bch2_btree_iter_unlock(&iter);
136         }
137
138         return 0;
139 }
140
141 /*
142  * This moves only the meta-data off, leaving the data (if any) in place.
143  * The data is moved off by bch_move_data_off_device, if desired, and
144  * called first.
145  *
146  * Before calling this, allocation of buckets to the device must have
147  * been disabled, as else we'll continue to write meta-data to the device
148  * when new buckets are picked for meta-data writes.
149  * In addition, the copying gc and allocator threads for the device
150  * must have been stopped.  The allocator thread is the only thread
151  * that writes prio/gen information.
152  *
153  * Meta-data consists of:
154  * - Btree nodes
155  * - Prio/gen information
156  * - Journal entries
157  * - Superblock
158  *
159  * This has to move the btree nodes and the journal only:
160  * - prio/gen information is not written once the allocator thread is stopped.
161  *   also, as the prio/gen information is per-device it is not moved.
162  * - the superblock will be written by the caller once after everything
163  *   is stopped.
164  *
165  * Note that currently there is no way to stop btree node and journal
166  * meta-data writes to a device without moving the meta-data because
167  * once a bucket is open for a btree node, unless a replacement btree
168  * node is allocated (and the tree updated), the bucket will continue
169  * to be written with updates.  Similarly for the journal (it gets
170  * written until filled).
171  *
172  * This routine leaves the data (if any) in place.  Whether the data
173  * should be moved off is a decision independent of whether the meta
174  * data should be moved off and stopped:
175  *
176  * - For device removal, both data and meta-data are moved off, in
177  *   that order.
178  *
179  * - However, for turning a device read-only without removing it, only
180  *   meta-data is moved off since that's the only way to prevent it
181  *   from being written.  Data is left in the device, but no new data
182  *   is written.
183  */
184
185 static int bch2_dev_metadata_migrate(struct bch_fs *c, struct bch_dev *ca,
186                                      int flags)
187 {
188         unsigned i;
189         int ret = 0;
190
191         BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
192
193         if (!(bch2_dev_has_data(c, ca) &
194               ((1 << BCH_DATA_JOURNAL)|
195                (1 << BCH_DATA_BTREE))))
196                 return 0;
197
198         mutex_lock(&c->replicas_gc_lock);
199         bch2_replicas_gc_start(c,
200                                (1 << BCH_DATA_JOURNAL)|
201                                (1 << BCH_DATA_BTREE));
202
203         /* 1st, Move the btree nodes off the device */
204
205         for (i = 0; i < BTREE_ID_NR; i++) {
206                 ret = bch2_move_btree_off(c, ca, i);
207                 if (ret)
208                         goto err;
209         }
210
211         /* There are no prios/gens to move -- they are already in the device. */
212
213         /* 2nd. Move the journal off the device */
214
215         ret = bch2_journal_move(ca);
216         if (ret)
217                 goto err;
218
219 err:
220         bch2_replicas_gc_end(c, ret);
221         mutex_unlock(&c->replicas_gc_lock);
222         return ret;
223 }
224
225 int bch2_dev_data_migrate(struct bch_fs *c, struct bch_dev *ca, int flags)
226 {
227         return bch2_dev_usrdata_migrate(c, ca, flags) ?:
228                 bch2_dev_metadata_migrate(c, ca, flags);
229 }
230
231 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
232                          unsigned dev_idx, int flags, bool metadata)
233 {
234         struct bch_extent_ptr *ptr;
235         unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
236         unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
237         unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
238         unsigned nr_good;
239
240         extent_for_each_ptr_backwards(e, ptr)
241                 if (ptr->dev == dev_idx)
242                         bch2_extent_drop_ptr(e, ptr);
243
244         nr_good = bch2_extent_nr_good_ptrs(c, e.c);
245         if ((!nr_good && !(flags & lost)) ||
246             (nr_good < replicas && !(flags & degraded)))
247                 return -EINVAL;
248
249         return 0;
250 }
251
252 /*
253  * This doesn't actually move any data -- it marks the keys as bad
254  * if they contain a pointer to a device that is forcibly removed
255  * and don't have other valid pointers.  If there are valid pointers,
256  * the necessary pointers to the removed device are replaced with
257  * bad pointers instead.
258  *
259  * This is only called if bch_move_data_off_device above failed, meaning
260  * that we've already tried to move the data MAX_DATA_OFF_ITER times and
261  * are not likely to succeed if we try again.
262  */
263 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
264 {
265         struct bkey_s_c k;
266         struct bkey_s_extent e;
267         BKEY_PADDED(key) tmp;
268         struct btree_iter iter;
269         int ret = 0;
270
271         mutex_lock(&c->replicas_gc_lock);
272         bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
273
274         bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
275                              POS_MIN, BTREE_ITER_PREFETCH);
276
277         while ((k = bch2_btree_iter_peek(&iter)).k &&
278                !(ret = btree_iter_err(k))) {
279                 if (!bkey_extent_is_data(k.k))
280                         goto advance;
281
282                 if (!bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx))
283                         goto advance;
284
285                 bkey_reassemble(&tmp.key, k);
286                 e = bkey_i_to_s_extent(&tmp.key);
287
288                 ret = drop_dev_ptrs(c, e, dev_idx, flags, false);
289                 if (ret)
290                         break;
291
292                 /*
293                  * If the new extent no longer has any pointers, bch2_extent_normalize()
294                  * will do the appropriate thing with it (turning it into a
295                  * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
296                  */
297                 bch2_extent_normalize(c, e.s);
298
299                 if (bkey_extent_is_data(e.k) &&
300                     (ret = bch2_check_mark_super(c, e.c, BCH_DATA_USER)))
301                         break;
302
303                 iter.pos = bkey_start_pos(&tmp.key.k);
304
305                 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
306                                            BTREE_INSERT_ATOMIC|
307                                            BTREE_INSERT_NOFAIL,
308                                            BTREE_INSERT_ENTRY(&iter, &tmp.key));
309
310                 /*
311                  * don't want to leave ret == -EINTR, since if we raced and
312                  * something else overwrote the key we could spuriously return
313                  * -EINTR below:
314                  */
315                 if (ret == -EINTR)
316                         ret = 0;
317                 if (ret)
318                         break;
319
320                 continue;
321 advance:
322                 if (bkey_extent_is_data(k.k)) {
323                         ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
324                                                     BCH_DATA_USER);
325                         if (ret)
326                                 break;
327                 }
328                 bch2_btree_iter_advance_pos(&iter);
329         }
330
331         bch2_btree_iter_unlock(&iter);
332
333         bch2_replicas_gc_end(c, ret);
334         mutex_unlock(&c->replicas_gc_lock);
335
336         return ret;
337 }
338
339 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
340 {
341         struct btree_iter iter;
342         struct closure cl;
343         struct btree *b;
344         unsigned id;
345         int ret;
346
347         /* don't handle this yet: */
348         if (flags & BCH_FORCE_IF_METADATA_LOST)
349                 return -EINVAL;
350
351         closure_init_stack(&cl);
352
353         mutex_lock(&c->replicas_gc_lock);
354         bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
355
356         for (id = 0; id < BTREE_ID_NR; id++) {
357                 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
358                         __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
359                         struct bkey_i_extent *new_key;
360 retry:
361                         if (!bch2_extent_has_device(bkey_i_to_s_c_extent(&b->key),
362                                                     dev_idx)) {
363                                 bch2_btree_iter_set_locks_want(&iter, 0);
364
365                                 ret = bch2_check_mark_super(c, bkey_i_to_s_c_extent(&b->key),
366                                                             BCH_DATA_BTREE);
367                                 if (ret)
368                                         goto err;
369                         } else {
370                                 bkey_copy(&tmp.k, &b->key);
371                                 new_key = bkey_i_to_extent(&tmp.k);
372
373                                 ret = drop_dev_ptrs(c, extent_i_to_s(new_key),
374                                                     dev_idx, flags, true);
375                                 if (ret)
376                                         goto err;
377
378                                 if (!bch2_btree_iter_set_locks_want(&iter, U8_MAX)) {
379                                         b = bch2_btree_iter_peek_node(&iter);
380                                         goto retry;
381                                 }
382
383                                 ret = bch2_btree_node_update_key(c, &iter, b, new_key);
384                                 if (ret == -EINTR) {
385                                         b = bch2_btree_iter_peek_node(&iter);
386                                         goto retry;
387                                 }
388                                 if (ret)
389                                         goto err;
390                         }
391                 }
392                 bch2_btree_iter_unlock(&iter);
393
394                 /* btree root */
395                 mutex_lock(&c->btree_root_lock);
396                 mutex_unlock(&c->btree_root_lock);
397         }
398
399         ret = 0;
400 out:
401         bch2_replicas_gc_end(c, ret);
402         mutex_unlock(&c->replicas_gc_lock);
403
404         return ret;
405 err:
406         bch2_btree_iter_unlock(&iter);
407         goto out;
408 }
409
410 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
411 {
412         return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
413                 bch2_dev_metadata_drop(c, dev_idx, flags);
414 }