]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/migrate.c
78f6d3c12d1cfe32a88c2d3711d1b3f72094237e
[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 int issue_migration_move(struct bch_dev *ca,
17                                 struct moving_context *ctxt,
18                                 struct bkey_s_c k)
19 {
20         struct bch_fs *c = ca->fs;
21         struct disk_reservation res;
22         const struct bch_extent_ptr *ptr;
23         int ret;
24
25         if (bch2_disk_reservation_get(c, &res, k.k->size, 0))
26                 return -ENOSPC;
27
28         extent_for_each_ptr(bkey_s_c_to_extent(k), ptr)
29                 if (ptr->dev == ca->dev_idx)
30                         goto found;
31
32         BUG();
33 found:
34         /* XXX: we need to be doing something with the disk reservation */
35
36         ret = bch2_data_move(c, ctxt, &c->migration_write_point, k, ptr);
37         if (ret)
38                 bch2_disk_reservation_put(c, &res);
39         return ret;
40 }
41
42 #define MAX_DATA_OFF_ITER       10
43
44 /*
45  * This moves only the data off, leaving the meta-data (if any) in place.
46  * It walks the key space, and for any key with a valid pointer to the
47  * relevant device, it copies it elsewhere, updating the key to point to
48  * the copy.
49  * The meta-data is moved off by bch_move_meta_data_off_device.
50  *
51  * Note: If the number of data replicas desired is > 1, ideally, any
52  * new copies would not be made in the same device that already have a
53  * copy (if there are enough devices).
54  * This is _not_ currently implemented.  The multiple replicas can
55  * land in the same device even if there are others available.
56  */
57
58 int bch2_move_data_off_device(struct bch_dev *ca)
59 {
60         struct moving_context ctxt;
61         struct bch_fs *c = ca->fs;
62         unsigned pass = 0;
63         u64 seen_key_count;
64         int ret = 0;
65
66         BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
67
68         if (!(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_USER)))
69                 return 0;
70
71         mutex_lock(&c->replicas_gc_lock);
72         bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
73
74         bch2_move_ctxt_init(&ctxt, NULL, SECTORS_IN_FLIGHT_PER_DEVICE);
75         ctxt.avoid = ca;
76
77         /*
78          * In theory, only one pass should be necessary as we've
79          * quiesced all writes before calling this.
80          *
81          * However, in practice, more than one pass may be necessary:
82          * - Some move fails due to an error. We can can find this out
83          *   from the moving_context.
84          * - Some key swap failed because some of the pointers in the
85          *   key in the tree changed due to caching behavior, btree gc
86          *   pruning stale pointers, or tiering (if the device being
87          *   removed is in tier 0).  A smarter bkey_cmpxchg would
88          *   handle these cases.
89          *
90          * Thus this scans the tree one more time than strictly necessary,
91          * but that can be viewed as a verification pass.
92          */
93
94         do {
95                 struct btree_iter iter;
96                 struct bkey_s_c k;
97
98                 seen_key_count = 0;
99                 atomic_set(&ctxt.error_count, 0);
100                 atomic_set(&ctxt.error_flags, 0);
101
102                 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS, POS_MIN,
103                                      BTREE_ITER_PREFETCH);
104
105                 while (!bch2_move_ctxt_wait(&ctxt) &&
106                        (k = bch2_btree_iter_peek(&iter)).k &&
107                        !(ret = btree_iter_err(k))) {
108                         if (!bkey_extent_is_data(k.k) ||
109                             !bch2_extent_has_device(bkey_s_c_to_extent(k),
110                                                    ca->dev_idx))
111                                 goto next;
112
113                         ret = issue_migration_move(ca, &ctxt, k);
114                         if (ret == -ENOMEM) {
115                                 bch2_btree_iter_unlock(&iter);
116
117                                 /*
118                                  * memory allocation failure, wait for some IO
119                                  * to finish
120                                  */
121                                 bch2_move_ctxt_wait_for_io(&ctxt);
122                                 continue;
123                         }
124                         if (ret == -ENOSPC)
125                                 break;
126                         BUG_ON(ret);
127
128                         seen_key_count++;
129                         continue;
130 next:
131                         if (bkey_extent_is_data(k.k)) {
132                                 ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
133                                                             BCH_DATA_USER);
134                                 if (ret)
135                                         break;
136                         }
137                         bch2_btree_iter_advance_pos(&iter);
138                         bch2_btree_iter_cond_resched(&iter);
139
140                 }
141                 bch2_btree_iter_unlock(&iter);
142                 bch2_move_ctxt_exit(&ctxt);
143
144                 if (ret)
145                         goto err;
146         } while (seen_key_count && pass++ < MAX_DATA_OFF_ITER);
147
148         if (seen_key_count) {
149                 pr_err("Unable to migrate all data in %d iterations.",
150                        MAX_DATA_OFF_ITER);
151                 ret = -1;
152                 goto err;
153         }
154
155 err:
156         bch2_replicas_gc_end(c, ret);
157         mutex_unlock(&c->replicas_gc_lock);
158         return ret;
159 }
160
161 /*
162  * This walks the btree, and for any node on the relevant device it moves the
163  * node elsewhere.
164  */
165 static int bch2_move_btree_off(struct bch_fs *c, struct bch_dev *ca,
166                                enum btree_id id)
167 {
168         struct btree_iter iter;
169         struct closure cl;
170         struct btree *b;
171         int ret;
172
173         BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
174
175         closure_init_stack(&cl);
176
177         for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
178                 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
179
180                 if (!bch2_extent_has_device(e, ca->dev_idx))
181                         continue;
182
183                 ret = bch2_btree_node_rewrite(c, &iter, b->data->keys.seq, 0);
184                 if (ret) {
185                         bch2_btree_iter_unlock(&iter);
186                         return ret;
187                 }
188
189                 bch2_btree_iter_set_locks_want(&iter, 0);
190         }
191         ret = bch2_btree_iter_unlock(&iter);
192         if (ret)
193                 return ret; /* btree IO error */
194
195         if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
196                 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
197                         struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
198
199                         BUG_ON(bch2_extent_has_device(e, ca->dev_idx));
200                 }
201                 bch2_btree_iter_unlock(&iter);
202         }
203
204         return 0;
205 }
206
207 /*
208  * This moves only the meta-data off, leaving the data (if any) in place.
209  * The data is moved off by bch_move_data_off_device, if desired, and
210  * called first.
211  *
212  * Before calling this, allocation of buckets to the device must have
213  * been disabled, as else we'll continue to write meta-data to the device
214  * when new buckets are picked for meta-data writes.
215  * In addition, the copying gc and allocator threads for the device
216  * must have been stopped.  The allocator thread is the only thread
217  * that writes prio/gen information.
218  *
219  * Meta-data consists of:
220  * - Btree nodes
221  * - Prio/gen information
222  * - Journal entries
223  * - Superblock
224  *
225  * This has to move the btree nodes and the journal only:
226  * - prio/gen information is not written once the allocator thread is stopped.
227  *   also, as the prio/gen information is per-device it is not moved.
228  * - the superblock will be written by the caller once after everything
229  *   is stopped.
230  *
231  * Note that currently there is no way to stop btree node and journal
232  * meta-data writes to a device without moving the meta-data because
233  * once a bucket is open for a btree node, unless a replacement btree
234  * node is allocated (and the tree updated), the bucket will continue
235  * to be written with updates.  Similarly for the journal (it gets
236  * written until filled).
237  *
238  * This routine leaves the data (if any) in place.  Whether the data
239  * should be moved off is a decision independent of whether the meta
240  * data should be moved off and stopped:
241  *
242  * - For device removal, both data and meta-data are moved off, in
243  *   that order.
244  *
245  * - However, for turning a device read-only without removing it, only
246  *   meta-data is moved off since that's the only way to prevent it
247  *   from being written.  Data is left in the device, but no new data
248  *   is written.
249  */
250
251 int bch2_move_metadata_off_device(struct bch_dev *ca)
252 {
253         struct bch_fs *c = ca->fs;
254         unsigned i;
255         int ret = 0;
256
257         BUG_ON(ca->mi.state == BCH_MEMBER_STATE_RW);
258
259         if (!(bch2_dev_has_data(c, ca) &
260               ((1 << BCH_DATA_JOURNAL)|
261                (1 << BCH_DATA_BTREE))))
262                 return 0;
263
264         mutex_lock(&c->replicas_gc_lock);
265         bch2_replicas_gc_start(c,
266                                (1 << BCH_DATA_JOURNAL)|
267                                (1 << BCH_DATA_BTREE));
268
269         /* 1st, Move the btree nodes off the device */
270
271         for (i = 0; i < BTREE_ID_NR; i++) {
272                 ret = bch2_move_btree_off(c, ca, i);
273                 if (ret)
274                         goto err;
275         }
276
277         /* There are no prios/gens to move -- they are already in the device. */
278
279         /* 2nd. Move the journal off the device */
280
281         ret = bch2_journal_move(ca);
282         if (ret)
283                 goto err;
284
285 err:
286         bch2_replicas_gc_end(c, ret);
287         mutex_unlock(&c->replicas_gc_lock);
288         return ret;
289 }
290
291 /*
292  * Flagging data bad when forcibly removing a device after failing to
293  * migrate the data off the device.
294  */
295
296 static int bch2_flag_key_bad(struct btree_iter *iter,
297                             struct bch_dev *ca,
298                             struct bkey_s_c_extent orig)
299 {
300         BKEY_PADDED(key) tmp;
301         struct bkey_s_extent e;
302         struct bch_extent_ptr *ptr;
303         struct bch_fs *c = ca->fs;
304
305         bkey_reassemble(&tmp.key, orig.s_c);
306         e = bkey_i_to_s_extent(&tmp.key);
307
308         extent_for_each_ptr_backwards(e, ptr)
309                 if (ptr->dev == ca->dev_idx)
310                         bch2_extent_drop_ptr(e, ptr);
311
312         /*
313          * If the new extent no longer has any pointers, bch2_extent_normalize()
314          * will do the appropriate thing with it (turning it into a
315          * KEY_TYPE_ERROR key, or just a discard if it was a cached extent)
316          */
317         bch2_extent_normalize(c, e.s);
318
319         return bch2_btree_insert_at(c, NULL, NULL, NULL,
320                                    BTREE_INSERT_ATOMIC,
321                                    BTREE_INSERT_ENTRY(iter, &tmp.key));
322 }
323
324 /*
325  * This doesn't actually move any data -- it marks the keys as bad
326  * if they contain a pointer to a device that is forcibly removed
327  * and don't have other valid pointers.  If there are valid pointers,
328  * the necessary pointers to the removed device are replaced with
329  * bad pointers instead.
330  *
331  * This is only called if bch_move_data_off_device above failed, meaning
332  * that we've already tried to move the data MAX_DATA_OFF_ITER times and
333  * are not likely to succeed if we try again.
334  */
335 int bch2_flag_data_bad(struct bch_dev *ca)
336 {
337         struct bch_fs *c = ca->fs;
338         struct bkey_s_c k;
339         struct bkey_s_c_extent e;
340         struct btree_iter iter;
341         int ret = 0;
342
343         mutex_lock(&c->replicas_gc_lock);
344         bch2_replicas_gc_start(c, 1 << BCH_DATA_USER);
345
346         bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
347                              POS_MIN, BTREE_ITER_PREFETCH);
348
349         while ((k = bch2_btree_iter_peek(&iter)).k &&
350                !(ret = btree_iter_err(k))) {
351                 if (!bkey_extent_is_data(k.k))
352                         goto advance;
353
354                 e = bkey_s_c_to_extent(k);
355                 if (!bch2_extent_has_device(e, ca->dev_idx))
356                         goto advance;
357
358                 ret = bch2_flag_key_bad(&iter, ca, e);
359
360                 /*
361                  * don't want to leave ret == -EINTR, since if we raced and
362                  * something else overwrote the key we could spuriously return
363                  * -EINTR below:
364                  */
365                 if (ret == -EINTR)
366                         ret = 0;
367                 if (ret)
368                         break;
369
370                 /*
371                  * If the replica we're dropping was dirty and there is an
372                  * additional cached replica, the cached replica will now be
373                  * considered dirty - upon inserting the new version of the key,
374                  * the bucket accounting will be updated to reflect the fact
375                  * that the cached data is now dirty and everything works out as
376                  * if by magic without us having to do anything.
377                  *
378                  * The one thing we need to be concerned with here is there's a
379                  * race between when we drop any stale pointers from the key
380                  * we're about to insert, and when the key actually gets
381                  * inserted and the cached data is marked as dirty - we could
382                  * end up trying to insert a key with a pointer that should be
383                  * dirty, but points to stale data.
384                  *
385                  * If that happens the insert code just bails out and doesn't do
386                  * the insert - however, it doesn't return an error. Hence we
387                  * need to always recheck the current key before advancing to
388                  * the next:
389                  */
390                 continue;
391 advance:
392                 if (bkey_extent_is_data(k.k)) {
393                         ret = bch2_check_mark_super(c, bkey_s_c_to_extent(k),
394                                                     BCH_DATA_USER);
395                         if (ret)
396                                 break;
397                 }
398                 bch2_btree_iter_advance_pos(&iter);
399         }
400
401         bch2_btree_iter_unlock(&iter);
402
403         bch2_replicas_gc_end(c, ret);
404         mutex_unlock(&c->replicas_gc_lock);
405
406         return ret;
407 }