]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/move.c
Update bcachefs sources to 454bd4f82d bcachefs: Fix for the stripes mark path and gc
[bcachefs-tools-debian] / libbcachefs / move.c
1
2 #include "bcachefs.h"
3 #include "alloc_foreground.h"
4 #include "btree_gc.h"
5 #include "btree_update.h"
6 #include "btree_update_interior.h"
7 #include "buckets.h"
8 #include "disk_groups.h"
9 #include "inode.h"
10 #include "io.h"
11 #include "journal_reclaim.h"
12 #include "move.h"
13 #include "replicas.h"
14 #include "super-io.h"
15 #include "keylist.h"
16
17 #include <linux/ioprio.h>
18 #include <linux/kthread.h>
19
20 #include <trace/events/bcachefs.h>
21
22 #define SECTORS_IN_FLIGHT_PER_DEVICE    2048
23
24 struct moving_io {
25         struct list_head        list;
26         struct closure          cl;
27         bool                    read_completed;
28
29         unsigned                read_sectors;
30         unsigned                write_sectors;
31
32         struct bch_read_bio     rbio;
33
34         struct migrate_write    write;
35         /* Must be last since it is variable size */
36         struct bio_vec          bi_inline_vecs[0];
37 };
38
39 struct moving_context {
40         /* Closure for waiting on all reads and writes to complete */
41         struct closure          cl;
42
43         struct bch_move_stats   *stats;
44
45         struct list_head        reads;
46
47         /* in flight sectors: */
48         atomic_t                read_sectors;
49         atomic_t                write_sectors;
50
51         wait_queue_head_t       wait;
52 };
53
54 static int bch2_migrate_index_update(struct bch_write_op *op)
55 {
56         struct bch_fs *c = op->c;
57         struct btree_trans trans;
58         struct btree_iter *iter;
59         struct migrate_write *m =
60                 container_of(op, struct migrate_write, op);
61         struct keylist *keys = &op->insert_keys;
62         int ret = 0;
63
64         bch2_trans_init(&trans, c);
65         bch2_trans_preload_iters(&trans);
66
67         iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
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_extent *insert, *new =
74                         bkey_i_to_extent(bch2_keylist_front(keys));
75                 BKEY_PADDED(k) _new, _insert;
76                 const union bch_extent_entry *entry;
77                 struct extent_ptr_decoded p;
78                 bool did_work = false;
79                 int nr;
80
81                 ret = bkey_err(k);
82                 if (ret)
83                         break;
84
85                 if (bversion_cmp(k.k->version, new->k.version) ||
86                     !bkey_extent_is_data(k.k) ||
87                     !bch2_extent_matches_ptr(c, bkey_s_c_to_extent(k),
88                                              m->ptr, m->offset))
89                         goto nomatch;
90
91                 if (m->data_cmd == DATA_REWRITE &&
92                     !bch2_extent_has_device(bkey_s_c_to_extent(k),
93                                             m->data_opts.rewrite_dev))
94                         goto nomatch;
95
96                 bkey_reassemble(&_insert.k, k);
97                 insert = bkey_i_to_extent(&_insert.k);
98
99                 bkey_copy(&_new.k, bch2_keylist_front(keys));
100                 new = bkey_i_to_extent(&_new.k);
101
102                 bch2_cut_front(iter->pos, &insert->k_i);
103                 bch2_cut_back(new->k.p, &insert->k);
104                 bch2_cut_back(insert->k.p, &new->k);
105
106                 if (m->data_cmd == DATA_REWRITE)
107                         bch2_bkey_drop_device(extent_i_to_s(insert).s,
108                                               m->data_opts.rewrite_dev);
109
110                 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
111                         if (bch2_extent_has_device(extent_i_to_s_c(insert), p.ptr.dev)) {
112                                 /*
113                                  * raced with another move op? extent already
114                                  * has a pointer to the device we just wrote
115                                  * data to
116                                  */
117                                 continue;
118                         }
119
120                         bch2_extent_ptr_decoded_append(insert, &p);
121                         did_work = true;
122                 }
123
124                 if (!did_work)
125                         goto nomatch;
126
127                 bch2_extent_narrow_crcs(insert,
128                                 (struct bch_extent_crc_unpacked) { 0 });
129                 bch2_extent_normalize(c, extent_i_to_s(insert).s);
130                 bch2_extent_mark_replicas_cached(c, extent_i_to_s(insert),
131                                                  op->opts.background_target,
132                                                  op->opts.data_replicas);
133
134                 /*
135                  * If we're not fully overwriting @k, and it's compressed, we
136                  * need a reservation for all the pointers in @insert
137                  */
138                 nr = bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(&insert->k_i)) -
139                          m->nr_ptrs_reserved;
140
141                 if (insert->k.size < k.k->size &&
142                     bch2_extent_is_compressed(k) &&
143                     nr > 0) {
144                         ret = bch2_disk_reservation_add(c, &op->res,
145                                         keylist_sectors(keys) * nr, 0);
146                         if (ret)
147                                 goto out;
148
149                         m->nr_ptrs_reserved += nr;
150                         goto next;
151                 }
152
153                 bch2_trans_update(&trans,
154                                 BTREE_INSERT_ENTRY(iter, &insert->k_i));
155
156                 ret = bch2_trans_commit(&trans, &op->res,
157                                 op_journal_seq(op),
158                                 BTREE_INSERT_ATOMIC|
159                                 BTREE_INSERT_NOFAIL|
160                                 BTREE_INSERT_USE_RESERVE|
161                                 m->data_opts.btree_insert_flags);
162                 if (!ret)
163                         atomic_long_inc(&c->extent_migrate_done);
164                 if (ret == -EINTR)
165                         ret = 0;
166                 if (ret)
167                         break;
168 next:
169                 while (bkey_cmp(iter->pos, bch2_keylist_front(keys)->k.p) >= 0) {
170                         bch2_keylist_pop_front(keys);
171                         if (bch2_keylist_empty(keys))
172                                 goto out;
173                 }
174
175                 bch2_cut_front(iter->pos, bch2_keylist_front(keys));
176                 continue;
177 nomatch:
178                 if (m->ctxt)
179                         atomic64_add(k.k->p.offset - iter->pos.offset,
180                                      &m->ctxt->stats->sectors_raced);
181                 atomic_long_inc(&c->extent_migrate_raced);
182                 trace_move_race(&new->k);
183                 bch2_btree_iter_next_slot(iter);
184                 goto next;
185         }
186 out:
187         bch2_trans_exit(&trans);
188         BUG_ON(ret == -EINTR);
189         return ret;
190 }
191
192 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
193 {
194         /* write bio must own pages: */
195         BUG_ON(!m->op.wbio.bio.bi_vcnt);
196
197         m->ptr          = rbio->pick.ptr;
198         m->offset       = rbio->pos.offset - rbio->pick.crc.offset;
199         m->op.devs_have = rbio->devs_have;
200         m->op.pos       = rbio->pos;
201         m->op.version   = rbio->version;
202         m->op.crc       = rbio->pick.crc;
203         m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
204
205         if (bch2_csum_type_is_encryption(m->op.crc.csum_type)) {
206                 m->op.nonce     = m->op.crc.nonce + m->op.crc.offset;
207                 m->op.csum_type = m->op.crc.csum_type;
208         }
209
210         if (m->data_cmd == DATA_REWRITE)
211                 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
212 }
213
214 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
215                             struct write_point_specifier wp,
216                             struct bch_io_opts io_opts,
217                             enum data_cmd data_cmd,
218                             struct data_opts data_opts,
219                             struct bkey_s_c k)
220 {
221         int ret;
222
223         m->data_cmd     = data_cmd;
224         m->data_opts    = data_opts;
225         m->nr_ptrs_reserved = 0;
226
227         bch2_write_op_init(&m->op, c, io_opts);
228         m->op.compression_type =
229                 bch2_compression_opt_to_type[io_opts.background_compression ?:
230                                              io_opts.compression];
231         m->op.target    = data_opts.target,
232         m->op.write_point = wp;
233
234         if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
235                 m->op.alloc_reserve = RESERVE_MOVINGGC;
236
237         m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS|
238                 BCH_WRITE_PAGES_STABLE|
239                 BCH_WRITE_PAGES_OWNED|
240                 BCH_WRITE_DATA_ENCODED;
241
242         m->op.nr_replicas       = 1;
243         m->op.nr_replicas_required = 1;
244         m->op.index_update_fn   = bch2_migrate_index_update;
245
246         switch (data_cmd) {
247         case DATA_ADD_REPLICAS: {
248                 /*
249                  * DATA_ADD_REPLICAS is used for moving data to a different
250                  * device in the background, and due to compression the new copy
251                  * might take up more space than the old copy:
252                  */
253 #if 0
254                 int nr = (int) io_opts.data_replicas -
255                         bch2_bkey_nr_dirty_ptrs(k);
256 #endif
257                 int nr = (int) io_opts.data_replicas;
258
259                 if (nr > 0) {
260                         m->op.nr_replicas = m->nr_ptrs_reserved = nr;
261
262                         ret = bch2_disk_reservation_get(c, &m->op.res,
263                                         k.k->size, m->op.nr_replicas, 0);
264                         if (ret)
265                                 return ret;
266                 }
267                 break;
268         }
269         case DATA_REWRITE: {
270                 const union bch_extent_entry *entry;
271                 struct extent_ptr_decoded p;
272                 unsigned compressed_sectors = 0;
273
274                 extent_for_each_ptr_decode(bkey_s_c_to_extent(k), 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 bio_vec *bv;
305         int i;
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, i)
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                             struct bkey_s_c_extent e,
398                             enum data_cmd data_cmd,
399                             struct data_opts data_opts)
400 {
401         struct moving_io *io;
402         const union bch_extent_entry *entry;
403         struct extent_ptr_decoded p;
404         unsigned sectors = e.k->size, pages;
405         int ret = -ENOMEM;
406
407         move_ctxt_wait_event(ctxt,
408                 atomic_read(&ctxt->write_sectors) <
409                 SECTORS_IN_FLIGHT_PER_DEVICE);
410
411         move_ctxt_wait_event(ctxt,
412                 atomic_read(&ctxt->read_sectors) <
413                 SECTORS_IN_FLIGHT_PER_DEVICE);
414
415         /* write path might have to decompress data: */
416         extent_for_each_ptr_decode(e, p, entry)
417                 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
418
419         pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
420         io = kzalloc(sizeof(struct moving_io) +
421                      sizeof(struct bio_vec) * pages, GFP_KERNEL);
422         if (!io)
423                 goto err;
424
425         io->write.ctxt          = ctxt;
426         io->read_sectors        = e.k->size;
427         io->write_sectors       = e.k->size;
428
429         bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
430         bio_set_prio(&io->write.op.wbio.bio,
431                      IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
432         io->write.op.wbio.bio.bi_iter.bi_size = sectors << 9;
433
434         bch2_bio_map(&io->write.op.wbio.bio, NULL);
435         if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, GFP_KERNEL))
436                 goto err_free;
437
438         io->rbio.opts = io_opts;
439         bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
440         io->rbio.bio.bi_vcnt = pages;
441         bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
442         io->rbio.bio.bi_iter.bi_size = sectors << 9;
443
444         bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
445         io->rbio.bio.bi_iter.bi_sector  = bkey_start_offset(e.k);
446         io->rbio.bio.bi_end_io          = move_read_endio;
447
448         ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
449                                       data_cmd, data_opts, e.s_c);
450         if (ret)
451                 goto err_free_pages;
452
453         atomic64_inc(&ctxt->stats->keys_moved);
454         atomic64_add(e.k->size, &ctxt->stats->sectors_moved);
455
456         trace_move_extent(e.k);
457
458         atomic_add(io->read_sectors, &ctxt->read_sectors);
459         list_add_tail(&io->list, &ctxt->reads);
460
461         /*
462          * dropped by move_read_endio() - guards against use after free of
463          * ctxt when doing wakeup
464          */
465         closure_get(&ctxt->cl);
466         bch2_read_extent(c, &io->rbio, e.s_c,
467                          BCH_READ_NODECODE|
468                          BCH_READ_LAST_FRAGMENT);
469         return 0;
470 err_free_pages:
471         bio_free_pages(&io->write.op.wbio.bio);
472 err_free:
473         kfree(io);
474 err:
475         trace_move_alloc_fail(e.k);
476         return ret;
477 }
478
479 int bch2_move_data(struct bch_fs *c,
480                    struct bch_ratelimit *rate,
481                    struct write_point_specifier wp,
482                    struct bpos start,
483                    struct bpos end,
484                    move_pred_fn pred, void *arg,
485                    struct bch_move_stats *stats)
486 {
487         bool kthread = (current->flags & PF_KTHREAD) != 0;
488         struct moving_context ctxt = { .stats = stats };
489         struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
490         BKEY_PADDED(k) tmp;
491         struct btree_trans trans;
492         struct btree_iter *iter;
493         struct bkey_s_c k;
494         struct data_opts data_opts;
495         enum data_cmd data_cmd;
496         u64 delay, cur_inum = U64_MAX;
497         int ret = 0, ret2;
498
499         closure_init_stack(&ctxt.cl);
500         INIT_LIST_HEAD(&ctxt.reads);
501         init_waitqueue_head(&ctxt.wait);
502
503         bch2_trans_init(&trans, c);
504
505         stats->data_type = BCH_DATA_USER;
506         stats->btree_id = BTREE_ID_EXTENTS;
507         stats->pos      = POS_MIN;
508
509         iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, start,
510                                    BTREE_ITER_PREFETCH);
511
512         if (rate)
513                 bch2_ratelimit_reset(rate);
514
515         while (1) {
516                 do {
517                         delay = rate ? bch2_ratelimit_delay(rate) : 0;
518
519                         if (delay) {
520                                 bch2_trans_unlock(&trans);
521                                 set_current_state(TASK_INTERRUPTIBLE);
522                         }
523
524                         if (kthread && (ret = kthread_should_stop())) {
525                                 __set_current_state(TASK_RUNNING);
526                                 goto out;
527                         }
528
529                         if (delay)
530                                 schedule_timeout(delay);
531
532                         if (unlikely(freezing(current))) {
533                                 bch2_trans_unlock(&trans);
534                                 move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
535                                 try_to_freeze();
536                         }
537                 } while (delay);
538 peek:
539                 k = bch2_btree_iter_peek(iter);
540
541                 stats->pos = iter->pos;
542
543                 if (!k.k)
544                         break;
545                 ret = bkey_err(k);
546                 if (ret)
547                         break;
548                 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
549                         break;
550
551                 if (!bkey_extent_is_data(k.k))
552                         goto next_nondata;
553
554                 if (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,
586                                         bkey_s_c_to_extent(k),
587                                         data_cmd, data_opts);
588                 if (ret2) {
589                         if (ret2 == -ENOMEM) {
590                                 /* memory allocation failure, wait for some IO to finish */
591                                 bch2_move_ctxt_wait_for_io(&ctxt);
592                                 continue;
593                         }
594
595                         /* XXX signal failure */
596                         goto next;
597                 }
598
599                 if (rate)
600                         bch2_ratelimit_increment(rate, k.k->size);
601 next:
602                 atomic64_add(k.k->size * bch2_bkey_nr_dirty_ptrs(k),
603                              &stats->sectors_seen);
604 next_nondata:
605                 bch2_btree_iter_next(iter);
606                 bch2_trans_cond_resched(&trans);
607         }
608 out:
609         bch2_trans_exit(&trans);
610
611         move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
612         closure_sync(&ctxt.cl);
613
614         EBUG_ON(atomic_read(&ctxt.write_sectors));
615
616         trace_move_data(c,
617                         atomic64_read(&stats->sectors_moved),
618                         atomic64_read(&stats->keys_moved));
619
620         return ret;
621 }
622
623 static int bch2_move_btree(struct bch_fs *c,
624                            move_pred_fn pred,
625                            void *arg,
626                            struct bch_move_stats *stats)
627 {
628         struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
629         struct btree_trans trans;
630         struct btree_iter *iter;
631         struct btree *b;
632         unsigned id;
633         struct data_opts data_opts;
634         enum data_cmd cmd;
635         int ret = 0;
636
637         bch2_trans_init(&trans, c);
638
639         stats->data_type = BCH_DATA_BTREE;
640
641         for (id = 0; id < BTREE_ID_NR; id++) {
642                 stats->btree_id = id;
643
644                 for_each_btree_node(&trans, iter, id, POS_MIN,
645                                     BTREE_ITER_PREFETCH, b) {
646                         stats->pos = iter->pos;
647
648                         switch ((cmd = pred(c, arg,
649                                             bkey_i_to_s_c(&b->key),
650                                             &io_opts, &data_opts))) {
651                         case DATA_SKIP:
652                                 goto next;
653                         case DATA_SCRUB:
654                                 BUG();
655                         case DATA_ADD_REPLICAS:
656                         case DATA_REWRITE:
657                                 break;
658                         default:
659                                 BUG();
660                         }
661
662                         ret = bch2_btree_node_rewrite(c, iter,
663                                         b->data->keys.seq, 0) ?: ret;
664 next:
665                         bch2_trans_cond_resched(&trans);
666                 }
667
668                 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
669         }
670
671         bch2_trans_exit(&trans);
672
673         return ret;
674 }
675
676 #if 0
677 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
678                                 struct bkey_s_c k,
679                                 struct bch_io_opts *io_opts,
680                                 struct data_opts *data_opts)
681 {
682         return DATA_SCRUB;
683 }
684 #endif
685
686 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
687                                       struct bkey_s_c k,
688                                       struct bch_io_opts *io_opts,
689                                       struct data_opts *data_opts)
690 {
691         unsigned nr_good = bch2_bkey_durability(c, k);
692         unsigned replicas = 0;
693
694         switch (k.k->type) {
695         case KEY_TYPE_btree_ptr:
696                 replicas = c->opts.metadata_replicas;
697                 break;
698         case KEY_TYPE_extent:
699                 replicas = io_opts->data_replicas;
700                 break;
701         }
702
703         if (!nr_good || nr_good >= replicas)
704                 return DATA_SKIP;
705
706         data_opts->target               = 0;
707         data_opts->btree_insert_flags   = 0;
708         return DATA_ADD_REPLICAS;
709 }
710
711 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
712                                   struct bkey_s_c k,
713                                   struct bch_io_opts *io_opts,
714                                   struct data_opts *data_opts)
715 {
716         struct bch_ioctl_data *op = arg;
717
718         if (!bch2_bkey_has_device(k, op->migrate.dev))
719                 return DATA_SKIP;
720
721         data_opts->target               = 0;
722         data_opts->btree_insert_flags   = 0;
723         data_opts->rewrite_dev          = op->migrate.dev;
724         return DATA_REWRITE;
725 }
726
727 int bch2_data_job(struct bch_fs *c,
728                   struct bch_move_stats *stats,
729                   struct bch_ioctl_data op)
730 {
731         int ret = 0;
732
733         switch (op.op) {
734         case BCH_DATA_OP_REREPLICATE:
735                 stats->data_type = BCH_DATA_JOURNAL;
736                 ret = bch2_journal_flush_device_pins(&c->journal, -1);
737
738                 ret = bch2_move_btree(c, rereplicate_pred, c, stats) ?: ret;
739
740                 while (1) {
741                         closure_wait_event(&c->btree_interior_update_wait,
742                                            !bch2_btree_interior_updates_nr_pending(c) ||
743                                            c->btree_roots_dirty);
744                         if (!bch2_btree_interior_updates_nr_pending(c))
745                                 break;
746                         bch2_journal_meta(&c->journal);
747                 }
748
749                 ret = bch2_replicas_gc2(c) ?: ret;
750
751                 ret = bch2_move_data(c, NULL,
752                                      writepoint_hashed((unsigned long) current),
753                                      op.start,
754                                      op.end,
755                                      rereplicate_pred, c, stats) ?: ret;
756                 ret = bch2_replicas_gc2(c) ?: ret;
757                 break;
758         case BCH_DATA_OP_MIGRATE:
759                 if (op.migrate.dev >= c->sb.nr_devices)
760                         return -EINVAL;
761
762                 stats->data_type = BCH_DATA_JOURNAL;
763                 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
764
765                 ret = bch2_move_btree(c, migrate_pred, &op, stats) ?: ret;
766                 ret = bch2_replicas_gc2(c) ?: ret;
767
768                 ret = bch2_move_data(c, NULL,
769                                      writepoint_hashed((unsigned long) current),
770                                      op.start,
771                                      op.end,
772                                      migrate_pred, &op, stats) ?: ret;
773                 ret = bch2_replicas_gc2(c) ?: ret;
774                 break;
775         default:
776                 ret = -EINVAL;
777         }
778
779         return ret;
780 }