1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
5 #include "alloc_foreground.h"
6 #include "backpointers.h"
10 #include "btree_update.h"
11 #include "btree_update_interior.h"
12 #include "btree_write_buffer.h"
14 #include "disk_groups.h"
21 #include "journal_reclaim.h"
29 #include <linux/ioprio.h>
30 #include <linux/kthread.h>
32 const char * const bch2_data_ops_strs[] = {
33 #define x(t, n, ...) [n] = #t,
39 static void bch2_data_update_opts_to_text(struct printbuf *out, struct bch_fs *c,
40 struct bch_io_opts *io_opts,
41 struct data_update_opts *data_opts)
43 printbuf_tabstop_push(out, 20);
44 prt_str(out, "rewrite ptrs:\t");
45 bch2_prt_u64_base2(out, data_opts->rewrite_ptrs);
48 prt_str(out, "kill ptrs:\t");
49 bch2_prt_u64_base2(out, data_opts->kill_ptrs);
52 prt_str(out, "target:\t");
53 bch2_target_to_text(out, c, data_opts->target);
56 prt_str(out, "compression:\t");
57 bch2_compression_opt_to_text(out, background_compression(*io_opts));
60 prt_str(out, "extra replicas:\t");
61 prt_u64(out, data_opts->extra_replicas);
64 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k,
65 struct bch_io_opts *io_opts,
66 struct data_update_opts *data_opts)
68 if (trace_move_extent_enabled()) {
69 struct printbuf buf = PRINTBUF;
71 bch2_bkey_val_to_text(&buf, c, k);
73 bch2_data_update_opts_to_text(&buf, c, io_opts, data_opts);
74 trace_move_extent(c, buf.buf);
79 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
81 if (trace_move_extent_read_enabled()) {
82 struct printbuf buf = PRINTBUF;
84 bch2_bkey_val_to_text(&buf, c, k);
85 trace_move_extent_read(c, buf.buf);
91 struct list_head read_list;
92 struct list_head io_list;
93 struct move_bucket_in_flight *b;
97 unsigned read_sectors;
98 unsigned write_sectors;
100 struct bch_read_bio rbio;
102 struct data_update write;
103 /* Must be last since it is variable size */
104 struct bio_vec bi_inline_vecs[];
107 static void move_free(struct moving_io *io)
109 struct moving_context *ctxt = io->write.ctxt;
112 atomic_dec(&io->b->count);
114 bch2_data_update_exit(&io->write);
116 mutex_lock(&ctxt->lock);
117 list_del(&io->io_list);
118 wake_up(&ctxt->wait);
119 mutex_unlock(&ctxt->lock);
124 static void move_write_done(struct bch_write_op *op)
126 struct moving_io *io = container_of(op, struct moving_io, write.op);
127 struct moving_context *ctxt = io->write.ctxt;
129 if (io->write.op.error)
130 ctxt->write_error = true;
132 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
133 atomic_dec(&io->write.ctxt->write_ios);
135 closure_put(&ctxt->cl);
138 static void move_write(struct moving_io *io)
140 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
145 if (trace_move_extent_write_enabled()) {
146 struct bch_fs *c = io->write.op.c;
147 struct printbuf buf = PRINTBUF;
149 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(io->write.k.k));
150 trace_move_extent_write(c, buf.buf);
154 closure_get(&io->write.ctxt->cl);
155 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
156 atomic_inc(&io->write.ctxt->write_ios);
158 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
161 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
163 struct moving_io *io =
164 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
166 return io && io->read_completed ? io : NULL;
169 static void move_read_endio(struct bio *bio)
171 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
172 struct moving_context *ctxt = io->write.ctxt;
174 atomic_sub(io->read_sectors, &ctxt->read_sectors);
175 atomic_dec(&ctxt->read_ios);
176 io->read_completed = true;
178 wake_up(&ctxt->wait);
179 closure_put(&ctxt->cl);
182 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt)
184 struct moving_io *io;
186 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
187 bch2_trans_unlock_long(ctxt->trans);
188 list_del(&io->read_list);
193 void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
195 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
197 move_ctxt_wait_event(ctxt,
198 !atomic_read(&ctxt->write_sectors) ||
199 atomic_read(&ctxt->write_sectors) != sectors_pending);
202 void bch2_moving_ctxt_flush_all(struct moving_context *ctxt)
204 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
205 bch2_trans_unlock_long(ctxt->trans);
206 closure_sync(&ctxt->cl);
209 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
211 struct bch_fs *c = ctxt->trans->c;
213 bch2_moving_ctxt_flush_all(ctxt);
215 EBUG_ON(atomic_read(&ctxt->write_sectors));
216 EBUG_ON(atomic_read(&ctxt->write_ios));
217 EBUG_ON(atomic_read(&ctxt->read_sectors));
218 EBUG_ON(atomic_read(&ctxt->read_ios));
220 mutex_lock(&c->moving_context_lock);
221 list_del(&ctxt->list);
222 mutex_unlock(&c->moving_context_lock);
224 bch2_trans_put(ctxt->trans);
225 memset(ctxt, 0, sizeof(*ctxt));
228 void bch2_moving_ctxt_init(struct moving_context *ctxt,
230 struct bch_ratelimit *rate,
231 struct bch_move_stats *stats,
232 struct write_point_specifier wp,
235 memset(ctxt, 0, sizeof(*ctxt));
237 ctxt->trans = bch2_trans_get(c);
238 ctxt->fn = (void *) _RET_IP_;
242 ctxt->wait_on_copygc = wait_on_copygc;
244 closure_init_stack(&ctxt->cl);
246 mutex_init(&ctxt->lock);
247 INIT_LIST_HEAD(&ctxt->reads);
248 INIT_LIST_HEAD(&ctxt->ios);
249 init_waitqueue_head(&ctxt->wait);
251 mutex_lock(&c->moving_context_lock);
252 list_add(&ctxt->list, &c->moving_context_list);
253 mutex_unlock(&c->moving_context_lock);
256 void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
258 trace_move_data(c, stats);
261 void bch2_move_stats_init(struct bch_move_stats *stats, const char *name)
263 memset(stats, 0, sizeof(*stats));
264 stats->data_type = BCH_DATA_user;
265 scnprintf(stats->name, sizeof(stats->name), "%s", name);
268 int bch2_move_extent(struct moving_context *ctxt,
269 struct move_bucket_in_flight *bucket_in_flight,
270 struct btree_iter *iter,
272 struct bch_io_opts io_opts,
273 struct data_update_opts data_opts)
275 struct btree_trans *trans = ctxt->trans;
276 struct bch_fs *c = trans->c;
277 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
278 struct moving_io *io;
279 const union bch_extent_entry *entry;
280 struct extent_ptr_decoded p;
281 unsigned sectors = k.k->size, pages;
284 trace_move_extent2(c, k, &io_opts, &data_opts);
287 ctxt->stats->pos = BBPOS(iter->btree_id, iter->pos);
289 bch2_data_update_opts_normalize(k, &data_opts);
291 if (!data_opts.rewrite_ptrs &&
292 !data_opts.extra_replicas) {
293 if (data_opts.kill_ptrs)
294 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
299 * Before memory allocations & taking nocow locks in
300 * bch2_data_update_init():
302 bch2_trans_unlock(trans);
304 /* write path might have to decompress data: */
305 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
306 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
308 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
309 io = kzalloc(sizeof(struct moving_io) +
310 sizeof(struct bio_vec) * pages, GFP_KERNEL);
314 INIT_LIST_HEAD(&io->io_list);
315 io->write.ctxt = ctxt;
316 io->read_sectors = k.k->size;
317 io->write_sectors = k.k->size;
319 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
320 bio_set_prio(&io->write.op.wbio.bio,
321 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
323 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
328 io->rbio.opts = io_opts;
329 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
330 io->rbio.bio.bi_vcnt = pages;
331 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
332 io->rbio.bio.bi_iter.bi_size = sectors << 9;
334 io->rbio.bio.bi_opf = REQ_OP_READ;
335 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
336 io->rbio.bio.bi_end_io = move_read_endio;
338 ret = bch2_data_update_init(trans, iter, ctxt, &io->write, ctxt->wp,
339 io_opts, data_opts, iter->btree_id, k);
343 io->write.op.end_io = move_write_done;
346 bch2_ratelimit_increment(ctxt->rate, k.k->size);
349 atomic64_inc(&ctxt->stats->keys_moved);
350 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
353 if (bucket_in_flight) {
354 io->b = bucket_in_flight;
355 atomic_inc(&io->b->count);
358 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
359 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
360 trace_move_extent_read2(c, k);
362 mutex_lock(&ctxt->lock);
363 atomic_add(io->read_sectors, &ctxt->read_sectors);
364 atomic_inc(&ctxt->read_ios);
366 list_add_tail(&io->read_list, &ctxt->reads);
367 list_add_tail(&io->io_list, &ctxt->ios);
368 mutex_unlock(&ctxt->lock);
371 * dropped by move_read_endio() - guards against use after free of
372 * ctxt when doing wakeup
374 closure_get(&ctxt->cl);
375 bch2_read_extent(trans, &io->rbio,
377 iter->btree_id, k, 0,
379 BCH_READ_LAST_FRAGMENT);
382 bio_free_pages(&io->write.op.wbio.bio);
386 if (ret == -BCH_ERR_data_update_done)
389 if (bch2_err_matches(ret, EROFS) ||
390 bch2_err_matches(ret, BCH_ERR_transaction_restart))
393 count_event(c, move_extent_start_fail);
395 if (trace_move_extent_start_fail_enabled()) {
396 struct printbuf buf = PRINTBUF;
398 bch2_bkey_val_to_text(&buf, c, k);
400 prt_str(&buf, bch2_err_str(ret));
401 trace_move_extent_start_fail(c, buf.buf);
407 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
408 struct per_snapshot_io_opts *io_opts,
409 struct bkey_s_c extent_k)
411 struct bch_fs *c = trans->c;
412 u32 restart_count = trans->restart_count;
415 if (io_opts->cur_inum != extent_k.k->p.inode) {
418 ret = for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
419 BTREE_ITER_all_snapshots, k, ({
420 if (k.k->p.offset != extent_k.k->p.inode)
423 if (!bkey_is_inode(k.k))
426 struct bch_inode_unpacked inode;
427 BUG_ON(bch2_inode_unpack(k, &inode));
429 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
430 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
432 darray_push(&io_opts->d, e);
434 io_opts->cur_inum = extent_k.k->p.inode;
437 ret = ret ?: trans_was_restarted(trans, restart_count);
441 if (extent_k.k->p.snapshot)
442 darray_for_each(io_opts->d, i)
443 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
446 return &io_opts->fs_io_opts;
449 int bch2_move_get_io_opts_one(struct btree_trans *trans,
450 struct bch_io_opts *io_opts,
451 struct bkey_s_c extent_k)
453 struct btree_iter iter;
458 if (!extent_k.k->p.inode) {
459 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
463 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
464 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
467 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
470 if (!ret && bkey_is_inode(k.k)) {
471 struct bch_inode_unpacked inode;
472 bch2_inode_unpack(k, &inode);
473 bch2_inode_opts_get(io_opts, trans->c, &inode);
475 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
478 bch2_trans_iter_exit(trans, &iter);
482 int bch2_move_ratelimit(struct moving_context *ctxt)
484 struct bch_fs *c = ctxt->trans->c;
485 bool is_kthread = current->flags & PF_KTHREAD;
488 if (ctxt->wait_on_copygc && c->copygc_running) {
489 bch2_moving_ctxt_flush_all(ctxt);
490 wait_event_killable(c->copygc_running_wq,
491 !c->copygc_running ||
492 (is_kthread && kthread_should_stop()));
496 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
498 if (is_kthread && kthread_should_stop())
502 move_ctxt_wait_event_timeout(ctxt,
504 (is_kthread && kthread_should_stop()),
507 if (unlikely(freezing(current))) {
508 bch2_moving_ctxt_flush_all(ctxt);
514 * XXX: these limits really ought to be per device, SSDs and hard drives
515 * will want different limits
517 move_ctxt_wait_event(ctxt,
518 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
519 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
520 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
521 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
526 static int bch2_move_data_btree(struct moving_context *ctxt,
529 move_pred_fn pred, void *arg,
530 enum btree_id btree_id)
532 struct btree_trans *trans = ctxt->trans;
533 struct bch_fs *c = trans->c;
534 struct per_snapshot_io_opts snapshot_io_opts;
535 struct bch_io_opts *io_opts;
537 struct btree_iter iter;
539 struct data_update_opts data_opts;
542 per_snapshot_io_opts_init(&snapshot_io_opts, c);
543 bch2_bkey_buf_init(&sk);
546 ctxt->stats->data_type = BCH_DATA_user;
547 ctxt->stats->pos = BBPOS(btree_id, start);
550 bch2_trans_iter_init(trans, &iter, btree_id, start,
552 BTREE_ITER_all_snapshots);
555 bch2_ratelimit_reset(ctxt->rate);
557 while (!bch2_move_ratelimit(ctxt)) {
558 bch2_trans_begin(trans);
560 k = bch2_btree_iter_peek(&iter);
565 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
570 if (bkey_ge(bkey_start_pos(k.k), end))
574 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
576 if (!bkey_extent_is_direct_data(k.k))
579 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
580 ret = PTR_ERR_OR_ZERO(io_opts);
584 memset(&data_opts, 0, sizeof(data_opts));
585 if (!pred(c, arg, k, io_opts, &data_opts))
589 * The iterator gets unlocked by __bch2_read_extent - need to
590 * save a copy of @k elsewhere:
592 bch2_bkey_buf_reassemble(&sk, c, k);
593 k = bkey_i_to_s_c(sk.k);
595 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
597 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
600 if (ret2 == -ENOMEM) {
601 /* memory allocation failure, wait for some IO to finish */
602 bch2_move_ctxt_wait_for_io(ctxt);
606 /* XXX signal failure */
611 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
613 bch2_btree_iter_advance(&iter);
616 bch2_trans_iter_exit(trans, &iter);
617 bch2_bkey_buf_exit(&sk, c);
618 per_snapshot_io_opts_exit(&snapshot_io_opts);
623 int __bch2_move_data(struct moving_context *ctxt,
626 move_pred_fn pred, void *arg)
628 struct bch_fs *c = ctxt->trans->c;
632 for (id = start.btree;
633 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
635 ctxt->stats->pos = BBPOS(id, POS_MIN);
637 if (!btree_type_has_ptrs(id) ||
638 !bch2_btree_id_root(c, id)->b)
641 ret = bch2_move_data_btree(ctxt,
642 id == start.btree ? start.pos : POS_MIN,
643 id == end.btree ? end.pos : POS_MAX,
652 int bch2_move_data(struct bch_fs *c,
655 struct bch_ratelimit *rate,
656 struct bch_move_stats *stats,
657 struct write_point_specifier wp,
659 move_pred_fn pred, void *arg)
662 struct moving_context ctxt;
665 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
666 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
667 bch2_moving_ctxt_exit(&ctxt);
672 int bch2_evacuate_bucket(struct moving_context *ctxt,
673 struct move_bucket_in_flight *bucket_in_flight,
674 struct bpos bucket, int gen,
675 struct data_update_opts _data_opts)
677 struct btree_trans *trans = ctxt->trans;
678 struct bch_fs *c = trans->c;
679 bool is_kthread = current->flags & PF_KTHREAD;
680 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
681 struct btree_iter iter;
683 struct bch_backpointer bp;
684 struct bch_alloc_v4 a_convert;
685 const struct bch_alloc_v4 *a;
687 struct data_update_opts data_opts;
688 unsigned dirty_sectors, bucket_size;
690 struct bpos bp_pos = POS_MIN;
693 struct bch_dev *ca = bch2_dev_tryget(c, bucket.inode);
697 trace_bucket_evacuate(c, &bucket);
699 bch2_bkey_buf_init(&sk);
702 * We're not run in a context that handles transaction restarts:
704 bch2_trans_begin(trans);
706 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
707 bucket, BTREE_ITER_cached);
708 ret = lockrestart_do(trans,
709 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
710 bch2_trans_iter_exit(trans, &iter);
712 bch_err_msg(c, ret, "looking up alloc key");
716 a = bch2_alloc_to_v4(k, &a_convert);
717 dirty_sectors = bch2_bucket_sectors_dirty(*a);
718 bucket_size = ca->mi.bucket_size;
719 fragmentation = a->fragmentation_lru;
721 ret = bch2_btree_write_buffer_tryflush(trans);
722 bch_err_msg(c, ret, "flushing btree write buffer");
726 while (!(ret = bch2_move_ratelimit(ctxt))) {
727 if (is_kthread && kthread_should_stop())
730 bch2_trans_begin(trans);
732 ret = bch2_get_next_backpointer(trans, ca, bucket, gen,
735 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
739 if (bkey_eq(bp_pos, POS_MAX))
743 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
745 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
752 bch2_bkey_buf_reassemble(&sk, c, k);
753 k = bkey_i_to_s_c(sk.k);
755 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
757 bch2_trans_iter_exit(trans, &iter);
761 data_opts = _data_opts;
762 data_opts.target = io_opts.background_target;
763 data_opts.rewrite_ptrs = 0;
766 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
767 if (ptr->dev == bucket.inode) {
768 data_opts.rewrite_ptrs |= 1U << i;
770 bch2_trans_iter_exit(trans, &iter);
777 ret = bch2_move_extent(ctxt, bucket_in_flight,
778 &iter, k, io_opts, data_opts);
779 bch2_trans_iter_exit(trans, &iter);
781 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
783 if (ret == -ENOMEM) {
784 /* memory allocation failure, wait for some IO to finish */
785 bch2_move_ctxt_wait_for_io(ctxt);
792 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
796 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
797 ret = PTR_ERR_OR_ZERO(b);
798 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
800 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
807 unsigned sectors = btree_ptr_sectors_written(&b->key);
809 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
810 bch2_trans_iter_exit(trans, &iter);
812 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
818 bch2_ratelimit_increment(ctxt->rate, sectors);
820 atomic64_add(sectors, &ctxt->stats->sectors_seen);
821 atomic64_add(sectors, &ctxt->stats->sectors_moved);
825 bp_pos = bpos_nosnap_successor(bp_pos);
828 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
831 bch2_bkey_buf_exit(&sk, c);
835 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
836 struct btree *, struct bch_io_opts *,
837 struct data_update_opts *);
839 static int bch2_move_btree(struct bch_fs *c,
842 move_btree_pred pred, void *arg,
843 struct bch_move_stats *stats)
845 bool kthread = (current->flags & PF_KTHREAD) != 0;
846 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
847 struct moving_context ctxt;
848 struct btree_trans *trans;
849 struct btree_iter iter;
852 struct data_update_opts data_opts;
855 bch2_moving_ctxt_init(&ctxt, c, NULL, stats,
856 writepoint_ptr(&c->btree_write_point),
860 stats->data_type = BCH_DATA_btree;
862 for (btree = start.btree;
863 btree <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
865 stats->pos = BBPOS(btree, POS_MIN);
867 if (!bch2_btree_id_root(c, btree)->b)
870 bch2_trans_node_iter_init(trans, &iter, btree, POS_MIN, 0, 0,
871 BTREE_ITER_prefetch);
874 while (bch2_trans_begin(trans),
875 (b = bch2_btree_iter_peek_node(&iter)) &&
876 !(ret = PTR_ERR_OR_ZERO(b))) {
877 if (kthread && kthread_should_stop())
880 if ((cmp_int(btree, end.btree) ?:
881 bpos_cmp(b->key.k.p, end.pos)) > 0)
884 stats->pos = BBPOS(iter.btree_id, iter.pos);
886 if (!pred(c, arg, b, &io_opts, &data_opts))
889 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
890 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
895 bch2_btree_iter_next_node(&iter);
897 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
900 bch2_trans_iter_exit(trans, &iter);
902 if (kthread && kthread_should_stop())
907 bch2_moving_ctxt_exit(&ctxt);
908 bch2_btree_interior_updates_flush(c);
913 static bool rereplicate_pred(struct bch_fs *c, void *arg,
915 struct bch_io_opts *io_opts,
916 struct data_update_opts *data_opts)
918 unsigned nr_good = bch2_bkey_durability(c, k);
919 unsigned replicas = bkey_is_btree_ptr(k.k)
920 ? c->opts.metadata_replicas
921 : io_opts->data_replicas;
923 if (!nr_good || nr_good >= replicas)
926 data_opts->target = 0;
927 data_opts->extra_replicas = replicas - nr_good;
928 data_opts->btree_insert_flags = 0;
932 static bool migrate_pred(struct bch_fs *c, void *arg,
934 struct bch_io_opts *io_opts,
935 struct data_update_opts *data_opts)
937 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
938 struct bch_ioctl_data *op = arg;
941 data_opts->rewrite_ptrs = 0;
942 data_opts->target = 0;
943 data_opts->extra_replicas = 0;
944 data_opts->btree_insert_flags = 0;
946 bkey_for_each_ptr(ptrs, ptr) {
947 if (ptr->dev == op->migrate.dev)
948 data_opts->rewrite_ptrs |= 1U << i;
952 return data_opts->rewrite_ptrs != 0;
955 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
957 struct bch_io_opts *io_opts,
958 struct data_update_opts *data_opts)
960 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
963 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
965 struct bch_io_opts *io_opts,
966 struct data_update_opts *data_opts)
968 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
972 * Ancient versions of bcachefs produced packed formats which could represent
973 * keys that the in memory format cannot represent; this checks for those
974 * formats so we can get rid of them.
976 static bool bformat_needs_redo(struct bkey_format *f)
978 for (unsigned i = 0; i < f->nr_fields; i++)
979 if (bch2_bkey_format_field_overflows(f, i))
985 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
987 struct bch_io_opts *io_opts,
988 struct data_update_opts *data_opts)
990 if (b->version_ondisk != c->sb.version ||
991 btree_node_need_rewrite(b) ||
992 bformat_needs_redo(&b->format)) {
993 data_opts->target = 0;
994 data_opts->extra_replicas = 0;
995 data_opts->btree_insert_flags = 0;
1002 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1006 ret = bch2_move_btree(c,
1009 rewrite_old_nodes_pred, c, stats);
1011 mutex_lock(&c->sb_lock);
1012 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1013 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1014 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1015 bch2_write_super(c);
1016 mutex_unlock(&c->sb_lock);
1023 static bool drop_extra_replicas_pred(struct bch_fs *c, void *arg,
1025 struct bch_io_opts *io_opts,
1026 struct data_update_opts *data_opts)
1028 unsigned durability = bch2_bkey_durability(c, k);
1029 unsigned replicas = bkey_is_btree_ptr(k.k)
1030 ? c->opts.metadata_replicas
1031 : io_opts->data_replicas;
1032 const union bch_extent_entry *entry;
1033 struct extent_ptr_decoded p;
1037 bkey_for_each_ptr_decode(k.k, bch2_bkey_ptrs_c(k), p, entry) {
1038 unsigned d = bch2_extent_ptr_durability(c, &p);
1040 if (d && durability - d >= replicas) {
1041 data_opts->kill_ptrs |= BIT(i);
1049 return data_opts->kill_ptrs != 0;
1052 static bool drop_extra_replicas_btree_pred(struct bch_fs *c, void *arg,
1054 struct bch_io_opts *io_opts,
1055 struct data_update_opts *data_opts)
1057 return drop_extra_replicas_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1060 int bch2_data_job(struct bch_fs *c,
1061 struct bch_move_stats *stats,
1062 struct bch_ioctl_data op)
1064 struct bbpos start = BBPOS(op.start_btree, op.start_pos);
1065 struct bbpos end = BBPOS(op.end_btree, op.end_pos);
1068 if (op.op >= BCH_DATA_OP_NR)
1071 bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
1074 case BCH_DATA_OP_rereplicate:
1075 stats->data_type = BCH_DATA_journal;
1076 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1077 ret = bch2_move_btree(c, start, end,
1078 rereplicate_btree_pred, c, stats) ?: ret;
1079 ret = bch2_move_data(c, start, end,
1082 writepoint_hashed((unsigned long) current),
1084 rereplicate_pred, c) ?: ret;
1085 ret = bch2_replicas_gc2(c) ?: ret;
1087 case BCH_DATA_OP_migrate:
1088 if (op.migrate.dev >= c->sb.nr_devices)
1091 stats->data_type = BCH_DATA_journal;
1092 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1093 ret = bch2_move_btree(c, start, end,
1094 migrate_btree_pred, &op, stats) ?: ret;
1095 ret = bch2_move_data(c, start, end,
1098 writepoint_hashed((unsigned long) current),
1100 migrate_pred, &op) ?: ret;
1101 ret = bch2_replicas_gc2(c) ?: ret;
1103 case BCH_DATA_OP_rewrite_old_nodes:
1104 ret = bch2_scan_old_btree_nodes(c, stats);
1106 case BCH_DATA_OP_drop_extra_replicas:
1107 ret = bch2_move_btree(c, start, end,
1108 drop_extra_replicas_btree_pred, c, stats) ?: ret;
1109 ret = bch2_move_data(c, start, end, NULL, stats,
1110 writepoint_hashed((unsigned long) current),
1112 drop_extra_replicas_pred, c) ?: ret;
1113 ret = bch2_replicas_gc2(c) ?: ret;
1119 bch2_move_stats_exit(stats, c);
1123 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1125 prt_printf(out, "%s: data type==", stats->name);
1126 bch2_prt_data_type(out, stats->data_type);
1127 prt_str(out, " pos=");
1128 bch2_bbpos_to_text(out, stats->pos);
1130 printbuf_indent_add(out, 2);
1132 prt_printf(out, "keys moved: %llu\n", atomic64_read(&stats->keys_moved));
1133 prt_printf(out, "keys raced: %llu\n", atomic64_read(&stats->keys_raced));
1134 prt_printf(out, "bytes seen: ");
1135 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1138 prt_printf(out, "bytes moved: ");
1139 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1142 prt_printf(out, "bytes raced: ");
1143 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1146 printbuf_indent_sub(out, 2);
1149 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1151 struct moving_io *io;
1153 bch2_move_stats_to_text(out, ctxt->stats);
1154 printbuf_indent_add(out, 2);
1156 prt_printf(out, "reads: ios %u/%u sectors %u/%u\n",
1157 atomic_read(&ctxt->read_ios),
1158 c->opts.move_ios_in_flight,
1159 atomic_read(&ctxt->read_sectors),
1160 c->opts.move_bytes_in_flight >> 9);
1162 prt_printf(out, "writes: ios %u/%u sectors %u/%u\n",
1163 atomic_read(&ctxt->write_ios),
1164 c->opts.move_ios_in_flight,
1165 atomic_read(&ctxt->write_sectors),
1166 c->opts.move_bytes_in_flight >> 9);
1168 printbuf_indent_add(out, 2);
1170 mutex_lock(&ctxt->lock);
1171 list_for_each_entry(io, &ctxt->ios, io_list)
1172 bch2_write_op_to_text(out, &io->write.op);
1173 mutex_unlock(&ctxt->lock);
1175 printbuf_indent_sub(out, 4);
1178 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1180 struct moving_context *ctxt;
1182 mutex_lock(&c->moving_context_lock);
1183 list_for_each_entry(ctxt, &c->moving_context_list, list)
1184 bch2_moving_ctxt_to_text(out, c, ctxt);
1185 mutex_unlock(&c->moving_context_lock);
1188 void bch2_fs_move_init(struct bch_fs *c)
1190 INIT_LIST_HEAD(&c->moving_context_list);
1191 mutex_init(&c->moving_context_lock);