]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/migrate.c
Update bcachefs sources to f4b290345a bcachefs: device resize
[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, 1 << BCH_DATA_BTREE);
200
201         for (i = 0; i < BTREE_ID_NR; i++) {
202                 ret = bch2_move_btree_off(c, ca, i);
203                 if (ret)
204                         goto err;
205         }
206 err:
207         bch2_replicas_gc_end(c, ret);
208         mutex_unlock(&c->replicas_gc_lock);
209         return ret;
210 }
211
212 int bch2_dev_data_migrate(struct bch_fs *c, struct bch_dev *ca, int flags)
213 {
214         return bch2_dev_usrdata_migrate(c, ca, flags) ?:
215                 bch2_dev_metadata_migrate(c, ca, flags);
216 }
217
218 static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s_extent e,
219                          unsigned dev_idx, int flags, bool metadata)
220 {
221         unsigned replicas = metadata ? c->opts.metadata_replicas : c->opts.data_replicas;
222         unsigned lost = metadata ? BCH_FORCE_IF_METADATA_LOST : BCH_FORCE_IF_DATA_LOST;
223         unsigned degraded = metadata ? BCH_FORCE_IF_METADATA_DEGRADED : BCH_FORCE_IF_DATA_DEGRADED;
224         unsigned nr_good;
225
226         bch2_extent_drop_device(e, dev_idx);
227
228         nr_good = bch2_extent_nr_good_ptrs(c, e.c);
229         if ((!nr_good && !(flags & lost)) ||
230             (nr_good < replicas && !(flags & degraded)))
231                 return -EINVAL;
232
233         return 0;
234 }
235
236 /*
237  * This doesn't actually move any data -- it marks the keys as bad
238  * if they contain a pointer to a device that is forcibly removed
239  * and don't have other valid pointers.  If there are valid pointers,
240  * the necessary pointers to the removed device are replaced with
241  * bad pointers instead.
242  *
243  * This is only called if bch_move_data_off_device above failed, meaning
244  * that we've already tried to move the data MAX_DATA_OFF_ITER times and
245  * are not likely to succeed if we try again.
246  */
247 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
248 {
249         struct bkey_s_c k;
250         struct bkey_s_extent e;
251         BKEY_PADDED(key) tmp;
252         struct btree_iter iter;
253         int ret = 0;
254
255         mutex_lock(&c->replicas_gc_lock);
256         bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
257
258         bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
259                              POS_MIN, BTREE_ITER_PREFETCH);
260
261         while ((k = bch2_btree_iter_peek(&iter)).k &&
262                !(ret = btree_iter_err(k))) {
263                 if (!bkey_extent_is_data(k.k))
264                         goto advance;
265
266                 if (!bch2_extent_has_device(bkey_s_c_to_extent(k), dev_idx))
267                         goto advance;
268
269                 bkey_reassemble(&tmp.key, k);
270                 e = bkey_i_to_s_extent(&tmp.key);
271
272                 ret = drop_dev_ptrs(c, e, dev_idx, flags, false);
273                 if (ret)
274                         break;
275
276                 /*
277                  * If the new extent no longer has any pointers, bch2_extent_normalize()
278                  * will do the appropriate thing with it (turning it into a
279                  * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
280                  */
281                 bch2_extent_normalize(c, e.s);
282
283                 if (bkey_extent_is_data(e.k) &&
284                     (ret = bch2_check_mark_super(c, e.c, BCH_DATA_USER)))
285                         break;
286
287                 iter.pos = bkey_start_pos(&tmp.key.k);
288
289                 ret = bch2_btree_insert_at(c, NULL, NULL, NULL,
290                                            BTREE_INSERT_ATOMIC|
291                                            BTREE_INSERT_NOFAIL,
292                                            BTREE_INSERT_ENTRY(&iter, &tmp.key));
293
294                 /*
295                  * don't want to leave ret == -EINTR, since if we raced and
296                  * something else overwrote the key we could spuriously return
297                  * -EINTR below:
298                  */
299                 if (ret == -EINTR)
300                         ret = 0;
301                 if (ret)
302                         break;
303
304                 continue;
305 advance:
306                 if (bkey_extent_is_data(k.k)) {
307                         ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
308                                                     BCH_DATA_USER);
309                         if (ret)
310                                 break;
311                 }
312                 bch2_btree_iter_advance_pos(&iter);
313         }
314
315         bch2_btree_iter_unlock(&iter);
316
317         bch2_replicas_gc_end(c, ret);
318         mutex_unlock(&c->replicas_gc_lock);
319
320         return ret;
321 }
322
323 static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
324 {
325         struct btree_iter iter;
326         struct closure cl;
327         struct btree *b;
328         unsigned id;
329         int ret;
330
331         /* don't handle this yet: */
332         if (flags & BCH_FORCE_IF_METADATA_LOST)
333                 return -EINVAL;
334
335         closure_init_stack(&cl);
336
337         mutex_lock(&c->replicas_gc_lock);
338         bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
339
340         for (id = 0; id < BTREE_ID_NR; id++) {
341                 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
342                         __BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX) tmp;
343                         struct bkey_i_extent *new_key;
344 retry:
345                         if (!bch2_extent_has_device(bkey_i_to_s_c_extent(&b->key),
346                                                     dev_idx)) {
347                                 bch2_btree_iter_set_locks_want(&iter, 0);
348
349                                 ret = bch2_check_mark_super(c, bkey_i_to_s_c_extent(&b->key),
350                                                             BCH_DATA_BTREE);
351                                 if (ret)
352                                         goto err;
353                         } else {
354                                 bkey_copy(&tmp.k, &b->key);
355                                 new_key = bkey_i_to_extent(&tmp.k);
356
357                                 ret = drop_dev_ptrs(c, extent_i_to_s(new_key),
358                                                     dev_idx, flags, true);
359                                 if (ret)
360                                         goto err;
361
362                                 if (!bch2_btree_iter_set_locks_want(&iter, U8_MAX)) {
363                                         b = bch2_btree_iter_peek_node(&iter);
364                                         goto retry;
365                                 }
366
367                                 ret = bch2_btree_node_update_key(c, &iter, b, new_key);
368                                 if (ret == -EINTR) {
369                                         b = bch2_btree_iter_peek_node(&iter);
370                                         goto retry;
371                                 }
372                                 if (ret)
373                                         goto err;
374                         }
375                 }
376                 bch2_btree_iter_unlock(&iter);
377
378                 /* btree root */
379                 mutex_lock(&c->btree_root_lock);
380                 mutex_unlock(&c->btree_root_lock);
381         }
382
383         ret = 0;
384 out:
385         bch2_replicas_gc_end(c, ret);
386         mutex_unlock(&c->replicas_gc_lock);
387
388         return ret;
389 err:
390         bch2_btree_iter_unlock(&iter);
391         goto out;
392 }
393
394 int bch2_dev_data_drop(struct bch_fs *c, unsigned dev_idx, int flags)
395 {
396         return bch2_dev_usrdata_drop(c, dev_idx, flags) ?:
397                 bch2_dev_metadata_drop(c, dev_idx, flags);
398 }