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