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