]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/move.c
ab20e981145b578f5f71f0243001d6ca3b0b3297
[bcachefs-tools-debian] / libbcachefs / move.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "alloc_foreground.h"
5 #include "btree_gc.h"
6 #include "btree_update.h"
7 #include "btree_update_interior.h"
8 #include "buckets.h"
9 #include "disk_groups.h"
10 #include "inode.h"
11 #include "io.h"
12 #include "journal_reclaim.h"
13 #include "move.h"
14 #include "replicas.h"
15 #include "super-io.h"
16 #include "keylist.h"
17
18 #include <linux/ioprio.h>
19 #include <linux/kthread.h>
20
21 #include <trace/events/bcachefs.h>
22
23 #define SECTORS_IN_FLIGHT_PER_DEVICE    2048
24
25 struct moving_io {
26         struct list_head        list;
27         struct closure          cl;
28         bool                    read_completed;
29
30         unsigned                read_sectors;
31         unsigned                write_sectors;
32
33         struct bch_read_bio     rbio;
34
35         struct migrate_write    write;
36         /* Must be last since it is variable size */
37         struct bio_vec          bi_inline_vecs[0];
38 };
39
40 struct moving_context {
41         /* Closure for waiting on all reads and writes to complete */
42         struct closure          cl;
43
44         struct bch_move_stats   *stats;
45
46         struct list_head        reads;
47
48         /* in flight sectors: */
49         atomic_t                read_sectors;
50         atomic_t                write_sectors;
51
52         wait_queue_head_t       wait;
53 };
54
55 static int bch2_migrate_index_update(struct bch_write_op *op)
56 {
57         struct bch_fs *c = op->c;
58         struct btree_trans trans;
59         struct btree_iter *iter;
60         struct migrate_write *m =
61                 container_of(op, struct migrate_write, op);
62         struct keylist *keys = &op->insert_keys;
63         int ret = 0;
64
65         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
66
67         iter = bch2_trans_get_iter(&trans, m->btree_id,
68                                    bkey_start_pos(&bch2_keylist_front(keys)->k),
69                                    BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
70
71         while (1) {
72                 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
73                 struct bkey_i *insert;
74                 struct bkey_i_extent *new =
75                         bkey_i_to_extent(bch2_keylist_front(keys));
76                 BKEY_PADDED(k) _new, _insert;
77                 const union bch_extent_entry *entry;
78                 struct extent_ptr_decoded p;
79                 bool did_work = false;
80                 int nr;
81
82                 ret = bkey_err(k);
83                 if (ret)
84                         break;
85
86                 if (bversion_cmp(k.k->version, new->k.version) ||
87                     !bch2_bkey_matches_ptr(c, k, m->ptr, m->offset))
88                         goto nomatch;
89
90                 if (m->data_cmd == DATA_REWRITE &&
91                     !bch2_bkey_has_device(k, m->data_opts.rewrite_dev))
92                         goto nomatch;
93
94                 bkey_reassemble(&_insert.k, k);
95                 insert = &_insert.k;
96
97                 bkey_copy(&_new.k, bch2_keylist_front(keys));
98                 new = bkey_i_to_extent(&_new.k);
99
100                 bch2_cut_front(iter->pos, insert);
101                 bch2_cut_back(new->k.p, &insert->k);
102                 bch2_cut_back(insert->k.p, &new->k);
103
104                 if (m->data_cmd == DATA_REWRITE)
105                         bch2_bkey_drop_device(bkey_i_to_s(insert),
106                                               m->data_opts.rewrite_dev);
107
108                 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
109                         if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
110                                 /*
111                                  * raced with another move op? extent already
112                                  * has a pointer to the device we just wrote
113                                  * data to
114                                  */
115                                 continue;
116                         }
117
118                         bch2_extent_ptr_decoded_append(insert, &p);
119                         did_work = true;
120                 }
121
122                 if (!did_work)
123                         goto nomatch;
124
125                 bch2_bkey_narrow_crcs(insert,
126                                 (struct bch_extent_crc_unpacked) { 0 });
127                 bch2_extent_normalize(c, bkey_i_to_s(insert));
128                 bch2_bkey_mark_replicas_cached(c, bkey_i_to_s(insert),
129                                                op->opts.background_target,
130                                                op->opts.data_replicas);
131
132                 /*
133                  * If we're not fully overwriting @k, and it's compressed, we
134                  * need a reservation for all the pointers in @insert
135                  */
136                 nr = bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(insert)) -
137                          m->nr_ptrs_reserved;
138
139                 if (insert->k.size < k.k->size &&
140                     bch2_extent_is_compressed(k) &&
141                     nr > 0) {
142                         ret = bch2_disk_reservation_add(c, &op->res,
143                                         keylist_sectors(keys) * nr, 0);
144                         if (ret)
145                                 goto out;
146
147                         m->nr_ptrs_reserved += nr;
148                         goto next;
149                 }
150
151                 bch2_trans_update(&trans, iter, insert);
152
153                 ret = bch2_trans_commit(&trans, &op->res,
154                                 op_journal_seq(op),
155                                 BTREE_INSERT_ATOMIC|
156                                 BTREE_INSERT_NOFAIL|
157                                 BTREE_INSERT_USE_RESERVE|
158                                 m->data_opts.btree_insert_flags);
159                 if (!ret)
160                         atomic_long_inc(&c->extent_migrate_done);
161                 if (ret == -EINTR)
162                         ret = 0;
163                 if (ret)
164                         break;
165 next:
166                 while (bkey_cmp(iter->pos, bch2_keylist_front(keys)->k.p) >= 0) {
167                         bch2_keylist_pop_front(keys);
168                         if (bch2_keylist_empty(keys))
169                                 goto out;
170                 }
171
172                 bch2_cut_front(iter->pos, bch2_keylist_front(keys));
173                 continue;
174 nomatch:
175                 if (m->ctxt)
176                         atomic64_add(k.k->p.offset - iter->pos.offset,
177                                      &m->ctxt->stats->sectors_raced);
178                 atomic_long_inc(&c->extent_migrate_raced);
179                 trace_move_race(&new->k);
180                 bch2_btree_iter_next_slot(iter);
181                 goto next;
182         }
183 out:
184         bch2_trans_exit(&trans);
185         BUG_ON(ret == -EINTR);
186         return ret;
187 }
188
189 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
190 {
191         /* write bio must own pages: */
192         BUG_ON(!m->op.wbio.bio.bi_vcnt);
193
194         m->ptr          = rbio->pick.ptr;
195         m->offset       = rbio->pos.offset - rbio->pick.crc.offset;
196         m->op.devs_have = rbio->devs_have;
197         m->op.pos       = rbio->pos;
198         m->op.version   = rbio->version;
199         m->op.crc       = rbio->pick.crc;
200         m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
201
202         if (bch2_csum_type_is_encryption(m->op.crc.csum_type)) {
203                 m->op.nonce     = m->op.crc.nonce + m->op.crc.offset;
204                 m->op.csum_type = m->op.crc.csum_type;
205         }
206
207         if (m->data_cmd == DATA_REWRITE)
208                 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
209 }
210
211 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
212                             struct write_point_specifier wp,
213                             struct bch_io_opts io_opts,
214                             enum data_cmd data_cmd,
215                             struct data_opts data_opts,
216                             enum btree_id btree_id,
217                             struct bkey_s_c k)
218 {
219         int ret;
220
221         m->btree_id     = btree_id;
222         m->data_cmd     = data_cmd;
223         m->data_opts    = data_opts;
224         m->nr_ptrs_reserved = 0;
225
226         bch2_write_op_init(&m->op, c, io_opts);
227         m->op.compression_type =
228                 bch2_compression_opt_to_type[io_opts.background_compression ?:
229                                              io_opts.compression];
230         m->op.target    = data_opts.target,
231         m->op.write_point = wp;
232
233         if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
234                 m->op.alloc_reserve = RESERVE_MOVINGGC;
235
236         m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS|
237                 BCH_WRITE_PAGES_STABLE|
238                 BCH_WRITE_PAGES_OWNED|
239                 BCH_WRITE_DATA_ENCODED;
240
241         m->op.nr_replicas       = 1;
242         m->op.nr_replicas_required = 1;
243         m->op.index_update_fn   = bch2_migrate_index_update;
244
245         switch (data_cmd) {
246         case DATA_ADD_REPLICAS: {
247                 /*
248                  * DATA_ADD_REPLICAS is used for moving data to a different
249                  * device in the background, and due to compression the new copy
250                  * might take up more space than the old copy:
251                  */
252 #if 0
253                 int nr = (int) io_opts.data_replicas -
254                         bch2_bkey_nr_dirty_ptrs(k);
255 #endif
256                 int nr = (int) io_opts.data_replicas;
257
258                 if (nr > 0) {
259                         m->op.nr_replicas = m->nr_ptrs_reserved = nr;
260
261                         ret = bch2_disk_reservation_get(c, &m->op.res,
262                                         k.k->size, m->op.nr_replicas, 0);
263                         if (ret)
264                                 return ret;
265                 }
266                 break;
267         }
268         case DATA_REWRITE: {
269                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
270                 const union bch_extent_entry *entry;
271                 struct extent_ptr_decoded p;
272                 unsigned compressed_sectors = 0;
273
274                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
275                         if (!p.ptr.cached &&
276                             p.crc.compression_type != BCH_COMPRESSION_NONE &&
277                             bch2_dev_in_target(c, p.ptr.dev, data_opts.target))
278                                 compressed_sectors += p.crc.compressed_size;
279
280                 if (compressed_sectors) {
281                         ret = bch2_disk_reservation_add(c, &m->op.res,
282                                         compressed_sectors,
283                                         BCH_DISK_RESERVATION_NOFAIL);
284                         if (ret)
285                                 return ret;
286                 }
287                 break;
288         }
289         case DATA_PROMOTE:
290                 m->op.flags     |= BCH_WRITE_ALLOC_NOWAIT;
291                 m->op.flags     |= BCH_WRITE_CACHED;
292                 break;
293         default:
294                 BUG();
295         }
296
297         return 0;
298 }
299
300 static void move_free(struct closure *cl)
301 {
302         struct moving_io *io = container_of(cl, struct moving_io, cl);
303         struct moving_context *ctxt = io->write.ctxt;
304         struct bvec_iter_all iter;
305         struct bio_vec *bv;
306
307         bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
308
309         bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter)
310                 if (bv->bv_page)
311                         __free_page(bv->bv_page);
312
313         wake_up(&ctxt->wait);
314
315         kfree(io);
316 }
317
318 static void move_write_done(struct closure *cl)
319 {
320         struct moving_io *io = container_of(cl, struct moving_io, cl);
321
322         atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
323         closure_return_with_destructor(cl, move_free);
324 }
325
326 static void move_write(struct closure *cl)
327 {
328         struct moving_io *io = container_of(cl, struct moving_io, cl);
329
330         if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
331                 closure_return_with_destructor(cl, move_free);
332                 return;
333         }
334
335         bch2_migrate_read_done(&io->write, &io->rbio);
336
337         atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
338         closure_call(&io->write.op.cl, bch2_write, NULL, cl);
339         continue_at(cl, move_write_done, NULL);
340 }
341
342 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
343 {
344         struct moving_io *io =
345                 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
346
347         return io && io->read_completed ? io : NULL;
348 }
349
350 static void move_read_endio(struct bio *bio)
351 {
352         struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
353         struct moving_context *ctxt = io->write.ctxt;
354
355         atomic_sub(io->read_sectors, &ctxt->read_sectors);
356         io->read_completed = true;
357
358         if (next_pending_write(ctxt))
359                 wake_up(&ctxt->wait);
360
361         closure_put(&ctxt->cl);
362 }
363
364 static void do_pending_writes(struct moving_context *ctxt)
365 {
366         struct moving_io *io;
367
368         while ((io = next_pending_write(ctxt))) {
369                 list_del(&io->list);
370                 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
371         }
372 }
373
374 #define move_ctxt_wait_event(_ctxt, _cond)                      \
375 do {                                                            \
376         do_pending_writes(_ctxt);                               \
377                                                                 \
378         if (_cond)                                              \
379                 break;                                          \
380         __wait_event((_ctxt)->wait,                             \
381                      next_pending_write(_ctxt) || (_cond));     \
382 } while (1)
383
384 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
385 {
386         unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
387
388         move_ctxt_wait_event(ctxt,
389                 !atomic_read(&ctxt->write_sectors) ||
390                 atomic_read(&ctxt->write_sectors) != sectors_pending);
391 }
392
393 static int bch2_move_extent(struct bch_fs *c,
394                             struct moving_context *ctxt,
395                             struct write_point_specifier wp,
396                             struct bch_io_opts io_opts,
397                             enum btree_id btree_id,
398                             struct bkey_s_c k,
399                             enum data_cmd data_cmd,
400                             struct data_opts data_opts)
401 {
402         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
403         struct moving_io *io;
404         const union bch_extent_entry *entry;
405         struct extent_ptr_decoded p;
406         unsigned sectors = k.k->size, pages;
407         int ret = -ENOMEM;
408
409         move_ctxt_wait_event(ctxt,
410                 atomic_read(&ctxt->write_sectors) <
411                 SECTORS_IN_FLIGHT_PER_DEVICE);
412
413         move_ctxt_wait_event(ctxt,
414                 atomic_read(&ctxt->read_sectors) <
415                 SECTORS_IN_FLIGHT_PER_DEVICE);
416
417         /* write path might have to decompress data: */
418         bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
419                 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
420
421         pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
422         io = kzalloc(sizeof(struct moving_io) +
423                      sizeof(struct bio_vec) * pages, GFP_KERNEL);
424         if (!io)
425                 goto err;
426
427         io->write.ctxt          = ctxt;
428         io->read_sectors        = k.k->size;
429         io->write_sectors       = k.k->size;
430
431         bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
432         bio_set_prio(&io->write.op.wbio.bio,
433                      IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
434
435         if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
436                                  GFP_KERNEL))
437                 goto err_free;
438
439         io->rbio.c              = c;
440         io->rbio.opts           = io_opts;
441         bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
442         io->rbio.bio.bi_vcnt = pages;
443         bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
444         io->rbio.bio.bi_iter.bi_size = sectors << 9;
445
446         bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
447         io->rbio.bio.bi_iter.bi_sector  = bkey_start_offset(k.k);
448         io->rbio.bio.bi_end_io          = move_read_endio;
449
450         ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
451                                       data_cmd, data_opts, btree_id, k);
452         if (ret)
453                 goto err_free_pages;
454
455         atomic64_inc(&ctxt->stats->keys_moved);
456         atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
457
458         trace_move_extent(k.k);
459
460         atomic_add(io->read_sectors, &ctxt->read_sectors);
461         list_add_tail(&io->list, &ctxt->reads);
462
463         /*
464          * dropped by move_read_endio() - guards against use after free of
465          * ctxt when doing wakeup
466          */
467         closure_get(&ctxt->cl);
468         bch2_read_extent(c, &io->rbio, k, 0,
469                          BCH_READ_NODECODE|
470                          BCH_READ_LAST_FRAGMENT);
471         return 0;
472 err_free_pages:
473         bio_free_pages(&io->write.op.wbio.bio);
474 err_free:
475         kfree(io);
476 err:
477         trace_move_alloc_fail(k.k);
478         return ret;
479 }
480
481 static int __bch2_move_data(struct bch_fs *c,
482                 struct moving_context *ctxt,
483                 struct bch_ratelimit *rate,
484                 struct write_point_specifier wp,
485                 struct bpos start,
486                 struct bpos end,
487                 move_pred_fn pred, void *arg,
488                 struct bch_move_stats *stats,
489                 enum btree_id btree_id)
490 {
491         bool kthread = (current->flags & PF_KTHREAD) != 0;
492         struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
493         BKEY_PADDED(k) tmp;
494         struct btree_trans trans;
495         struct btree_iter *iter;
496         struct bkey_s_c k;
497         struct data_opts data_opts;
498         enum data_cmd data_cmd;
499         u64 delay, cur_inum = U64_MAX;
500         int ret = 0, ret2;
501
502         bch2_trans_init(&trans, c, 0, 0);
503
504         stats->data_type = BCH_DATA_USER;
505         stats->btree_id = btree_id;
506         stats->pos      = POS_MIN;
507
508         iter = bch2_trans_get_iter(&trans, btree_id, start,
509                                    BTREE_ITER_PREFETCH);
510
511         if (rate)
512                 bch2_ratelimit_reset(rate);
513
514         while (1) {
515                 do {
516                         delay = rate ? bch2_ratelimit_delay(rate) : 0;
517
518                         if (delay) {
519                                 bch2_trans_unlock(&trans);
520                                 set_current_state(TASK_INTERRUPTIBLE);
521                         }
522
523                         if (kthread && (ret = kthread_should_stop())) {
524                                 __set_current_state(TASK_RUNNING);
525                                 goto out;
526                         }
527
528                         if (delay)
529                                 schedule_timeout(delay);
530
531                         if (unlikely(freezing(current))) {
532                                 bch2_trans_unlock(&trans);
533                                 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
534                                 try_to_freeze();
535                         }
536                 } while (delay);
537 peek:
538                 k = bch2_btree_iter_peek(iter);
539
540                 stats->pos = iter->pos;
541
542                 if (!k.k)
543                         break;
544                 ret = bkey_err(k);
545                 if (ret)
546                         break;
547                 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
548                         break;
549
550                 if (!bkey_extent_is_direct_data(k.k))
551                         goto next_nondata;
552
553                 if (btree_id == BTREE_ID_EXTENTS &&
554                     cur_inum != k.k->p.inode) {
555                         struct bch_inode_unpacked inode;
556
557                         /* don't hold btree locks while looking up inode: */
558                         bch2_trans_unlock(&trans);
559
560                         io_opts = bch2_opts_to_inode_opts(c->opts);
561                         if (!bch2_inode_find_by_inum(c, k.k->p.inode, &inode))
562                                 bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
563                         cur_inum = k.k->p.inode;
564                         goto peek;
565                 }
566
567                 switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) {
568                 case DATA_SKIP:
569                         goto next;
570                 case DATA_SCRUB:
571                         BUG();
572                 case DATA_ADD_REPLICAS:
573                 case DATA_REWRITE:
574                 case DATA_PROMOTE:
575                         break;
576                 default:
577                         BUG();
578                 }
579
580                 /* unlock before doing IO: */
581                 bkey_reassemble(&tmp.k, k);
582                 k = bkey_i_to_s_c(&tmp.k);
583                 bch2_trans_unlock(&trans);
584
585                 ret2 = bch2_move_extent(c, ctxt, wp, io_opts, btree_id, k,
586                                         data_cmd, data_opts);
587                 if (ret2) {
588                         if (ret2 == -ENOMEM) {
589                                 /* memory allocation failure, wait for some IO to finish */
590                                 bch2_move_ctxt_wait_for_io(ctxt);
591                                 continue;
592                         }
593
594                         /* XXX signal failure */
595                         goto next;
596                 }
597
598                 if (rate)
599                         bch2_ratelimit_increment(rate, k.k->size);
600 next:
601                 atomic64_add(k.k->size * bch2_bkey_nr_dirty_ptrs(k),
602                              &stats->sectors_seen);
603 next_nondata:
604                 bch2_btree_iter_next(iter);
605                 bch2_trans_cond_resched(&trans);
606         }
607 out:
608         ret = bch2_trans_exit(&trans) ?: ret;
609
610         return ret;
611 }
612
613 int bch2_move_data(struct bch_fs *c,
614                    struct bch_ratelimit *rate,
615                    struct write_point_specifier wp,
616                    struct bpos start,
617                    struct bpos end,
618                    move_pred_fn pred, void *arg,
619                    struct bch_move_stats *stats)
620 {
621         struct moving_context ctxt = { .stats = stats };
622         int ret;
623
624         closure_init_stack(&ctxt.cl);
625         INIT_LIST_HEAD(&ctxt.reads);
626         init_waitqueue_head(&ctxt.wait);
627
628         stats->data_type = BCH_DATA_USER;
629
630         ret =   __bch2_move_data(c, &ctxt, rate, wp, start, end,
631                                  pred, arg, stats, BTREE_ID_EXTENTS) ?:
632                 __bch2_move_data(c, &ctxt, rate, wp, start, end,
633                                  pred, arg, stats, BTREE_ID_REFLINK);
634
635         move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
636         closure_sync(&ctxt.cl);
637
638         EBUG_ON(atomic_read(&ctxt.write_sectors));
639
640         trace_move_data(c,
641                         atomic64_read(&stats->sectors_moved),
642                         atomic64_read(&stats->keys_moved));
643
644         return ret;
645 }
646
647 static int bch2_move_btree(struct bch_fs *c,
648                            move_pred_fn pred,
649                            void *arg,
650                            struct bch_move_stats *stats)
651 {
652         struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
653         struct btree_trans trans;
654         struct btree_iter *iter;
655         struct btree *b;
656         unsigned id;
657         struct data_opts data_opts;
658         enum data_cmd cmd;
659         int ret = 0;
660
661         bch2_trans_init(&trans, c, 0, 0);
662
663         stats->data_type = BCH_DATA_BTREE;
664
665         for (id = 0; id < BTREE_ID_NR; id++) {
666                 stats->btree_id = id;
667
668                 for_each_btree_node(&trans, iter, id, POS_MIN,
669                                     BTREE_ITER_PREFETCH, b) {
670                         stats->pos = iter->pos;
671
672                         switch ((cmd = pred(c, arg,
673                                             bkey_i_to_s_c(&b->key),
674                                             &io_opts, &data_opts))) {
675                         case DATA_SKIP:
676                                 goto next;
677                         case DATA_SCRUB:
678                                 BUG();
679                         case DATA_ADD_REPLICAS:
680                         case DATA_REWRITE:
681                                 break;
682                         default:
683                                 BUG();
684                         }
685
686                         ret = bch2_btree_node_rewrite(c, iter,
687                                         b->data->keys.seq, 0) ?: ret;
688 next:
689                         bch2_trans_cond_resched(&trans);
690                 }
691
692                 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
693         }
694
695         bch2_trans_exit(&trans);
696
697         return ret;
698 }
699
700 #if 0
701 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
702                                 struct bkey_s_c k,
703                                 struct bch_io_opts *io_opts,
704                                 struct data_opts *data_opts)
705 {
706         return DATA_SCRUB;
707 }
708 #endif
709
710 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
711                                       struct bkey_s_c k,
712                                       struct bch_io_opts *io_opts,
713                                       struct data_opts *data_opts)
714 {
715         unsigned nr_good = bch2_bkey_durability(c, k);
716         unsigned replicas = 0;
717
718         switch (k.k->type) {
719         case KEY_TYPE_btree_ptr:
720                 replicas = c->opts.metadata_replicas;
721                 break;
722         case KEY_TYPE_extent:
723                 replicas = io_opts->data_replicas;
724                 break;
725         }
726
727         if (!nr_good || nr_good >= replicas)
728                 return DATA_SKIP;
729
730         data_opts->target               = 0;
731         data_opts->btree_insert_flags   = 0;
732         return DATA_ADD_REPLICAS;
733 }
734
735 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
736                                   struct bkey_s_c k,
737                                   struct bch_io_opts *io_opts,
738                                   struct data_opts *data_opts)
739 {
740         struct bch_ioctl_data *op = arg;
741
742         if (!bch2_bkey_has_device(k, op->migrate.dev))
743                 return DATA_SKIP;
744
745         data_opts->target               = 0;
746         data_opts->btree_insert_flags   = 0;
747         data_opts->rewrite_dev          = op->migrate.dev;
748         return DATA_REWRITE;
749 }
750
751 int bch2_data_job(struct bch_fs *c,
752                   struct bch_move_stats *stats,
753                   struct bch_ioctl_data op)
754 {
755         int ret = 0;
756
757         switch (op.op) {
758         case BCH_DATA_OP_REREPLICATE:
759                 stats->data_type = BCH_DATA_JOURNAL;
760                 ret = bch2_journal_flush_device_pins(&c->journal, -1);
761
762                 ret = bch2_move_btree(c, rereplicate_pred, c, stats) ?: ret;
763
764                 while (1) {
765                         closure_wait_event(&c->btree_interior_update_wait,
766                                            !bch2_btree_interior_updates_nr_pending(c) ||
767                                            c->btree_roots_dirty);
768                         if (!bch2_btree_interior_updates_nr_pending(c))
769                                 break;
770                         bch2_journal_meta(&c->journal);
771                 }
772
773                 ret = bch2_replicas_gc2(c) ?: ret;
774
775                 ret = bch2_move_data(c, NULL,
776                                      writepoint_hashed((unsigned long) current),
777                                      op.start,
778                                      op.end,
779                                      rereplicate_pred, c, stats) ?: ret;
780                 ret = bch2_replicas_gc2(c) ?: ret;
781                 break;
782         case BCH_DATA_OP_MIGRATE:
783                 if (op.migrate.dev >= c->sb.nr_devices)
784                         return -EINVAL;
785
786                 stats->data_type = BCH_DATA_JOURNAL;
787                 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
788
789                 ret = bch2_move_btree(c, migrate_pred, &op, stats) ?: ret;
790                 ret = bch2_replicas_gc2(c) ?: ret;
791
792                 ret = bch2_move_data(c, NULL,
793                                      writepoint_hashed((unsigned long) current),
794                                      op.start,
795                                      op.end,
796                                      migrate_pred, &op, stats) ?: ret;
797                 ret = bch2_replicas_gc2(c) ?: ret;
798                 break;
799         default:
800                 ret = -EINVAL;
801         }
802
803         return ret;
804 }