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