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