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:");
46 bch2_prt_u64_base2(out, data_opts->rewrite_ptrs);
49 prt_str(out, "kill ptrs: ");
51 bch2_prt_u64_base2(out, data_opts->kill_ptrs);
54 prt_str(out, "target: ");
56 bch2_target_to_text(out, c, data_opts->target);
59 prt_str(out, "compression: ");
61 bch2_compression_opt_to_text(out, background_compression(*io_opts));
64 prt_str(out, "extra replicas: ");
66 prt_u64(out, data_opts->extra_replicas);
69 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k,
70 struct bch_io_opts *io_opts,
71 struct data_update_opts *data_opts)
73 if (trace_move_extent_enabled()) {
74 struct printbuf buf = PRINTBUF;
76 bch2_bkey_val_to_text(&buf, c, k);
78 bch2_data_update_opts_to_text(&buf, c, io_opts, data_opts);
79 trace_move_extent(c, buf.buf);
84 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
86 if (trace_move_extent_read_enabled()) {
87 struct printbuf buf = PRINTBUF;
89 bch2_bkey_val_to_text(&buf, c, k);
90 trace_move_extent_read(c, buf.buf);
96 struct list_head read_list;
97 struct list_head io_list;
98 struct move_bucket_in_flight *b;
102 unsigned read_sectors;
103 unsigned write_sectors;
105 struct bch_read_bio rbio;
107 struct data_update write;
108 /* Must be last since it is variable size */
109 struct bio_vec bi_inline_vecs[];
112 static void move_free(struct moving_io *io)
114 struct moving_context *ctxt = io->write.ctxt;
117 atomic_dec(&io->b->count);
119 bch2_data_update_exit(&io->write);
121 mutex_lock(&ctxt->lock);
122 list_del(&io->io_list);
123 wake_up(&ctxt->wait);
124 mutex_unlock(&ctxt->lock);
129 static void move_write_done(struct bch_write_op *op)
131 struct moving_io *io = container_of(op, struct moving_io, write.op);
132 struct moving_context *ctxt = io->write.ctxt;
134 if (io->write.op.error)
135 ctxt->write_error = true;
137 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
138 atomic_dec(&io->write.ctxt->write_ios);
140 closure_put(&ctxt->cl);
143 static void move_write(struct moving_io *io)
145 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
150 if (trace_move_extent_write_enabled()) {
151 struct bch_fs *c = io->write.op.c;
152 struct printbuf buf = PRINTBUF;
154 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(io->write.k.k));
155 trace_move_extent_write(c, buf.buf);
159 closure_get(&io->write.ctxt->cl);
160 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
161 atomic_inc(&io->write.ctxt->write_ios);
163 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
166 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
168 struct moving_io *io =
169 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
171 return io && io->read_completed ? io : NULL;
174 static void move_read_endio(struct bio *bio)
176 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
177 struct moving_context *ctxt = io->write.ctxt;
179 atomic_sub(io->read_sectors, &ctxt->read_sectors);
180 atomic_dec(&ctxt->read_ios);
181 io->read_completed = true;
183 wake_up(&ctxt->wait);
184 closure_put(&ctxt->cl);
187 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt)
189 struct moving_io *io;
191 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
192 bch2_trans_unlock_long(ctxt->trans);
193 list_del(&io->read_list);
198 void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
200 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
202 move_ctxt_wait_event(ctxt,
203 !atomic_read(&ctxt->write_sectors) ||
204 atomic_read(&ctxt->write_sectors) != sectors_pending);
207 void bch2_moving_ctxt_flush_all(struct moving_context *ctxt)
209 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
210 bch2_trans_unlock_long(ctxt->trans);
211 closure_sync(&ctxt->cl);
214 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
216 struct bch_fs *c = ctxt->trans->c;
218 bch2_moving_ctxt_flush_all(ctxt);
220 EBUG_ON(atomic_read(&ctxt->write_sectors));
221 EBUG_ON(atomic_read(&ctxt->write_ios));
222 EBUG_ON(atomic_read(&ctxt->read_sectors));
223 EBUG_ON(atomic_read(&ctxt->read_ios));
225 mutex_lock(&c->moving_context_lock);
226 list_del(&ctxt->list);
227 mutex_unlock(&c->moving_context_lock);
229 bch2_trans_put(ctxt->trans);
230 memset(ctxt, 0, sizeof(*ctxt));
233 void bch2_moving_ctxt_init(struct moving_context *ctxt,
235 struct bch_ratelimit *rate,
236 struct bch_move_stats *stats,
237 struct write_point_specifier wp,
240 memset(ctxt, 0, sizeof(*ctxt));
242 ctxt->trans = bch2_trans_get(c);
243 ctxt->fn = (void *) _RET_IP_;
247 ctxt->wait_on_copygc = wait_on_copygc;
249 closure_init_stack(&ctxt->cl);
251 mutex_init(&ctxt->lock);
252 INIT_LIST_HEAD(&ctxt->reads);
253 INIT_LIST_HEAD(&ctxt->ios);
254 init_waitqueue_head(&ctxt->wait);
256 mutex_lock(&c->moving_context_lock);
257 list_add(&ctxt->list, &c->moving_context_list);
258 mutex_unlock(&c->moving_context_lock);
261 void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
263 trace_move_data(c, stats);
266 void bch2_move_stats_init(struct bch_move_stats *stats, const char *name)
268 memset(stats, 0, sizeof(*stats));
269 stats->data_type = BCH_DATA_user;
270 scnprintf(stats->name, sizeof(stats->name), "%s", name);
273 int bch2_move_extent(struct moving_context *ctxt,
274 struct move_bucket_in_flight *bucket_in_flight,
275 struct btree_iter *iter,
277 struct bch_io_opts io_opts,
278 struct data_update_opts data_opts)
280 struct btree_trans *trans = ctxt->trans;
281 struct bch_fs *c = trans->c;
282 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
283 struct moving_io *io;
284 const union bch_extent_entry *entry;
285 struct extent_ptr_decoded p;
286 unsigned sectors = k.k->size, pages;
289 trace_move_extent2(c, k, &io_opts, &data_opts);
292 ctxt->stats->pos = BBPOS(iter->btree_id, iter->pos);
294 bch2_data_update_opts_normalize(k, &data_opts);
296 if (!data_opts.rewrite_ptrs &&
297 !data_opts.extra_replicas) {
298 if (data_opts.kill_ptrs)
299 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
304 * Before memory allocations & taking nocow locks in
305 * bch2_data_update_init():
307 bch2_trans_unlock(trans);
309 /* write path might have to decompress data: */
310 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
311 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
313 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
314 io = kzalloc(sizeof(struct moving_io) +
315 sizeof(struct bio_vec) * pages, GFP_KERNEL);
319 INIT_LIST_HEAD(&io->io_list);
320 io->write.ctxt = ctxt;
321 io->read_sectors = k.k->size;
322 io->write_sectors = k.k->size;
324 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
325 bio_set_prio(&io->write.op.wbio.bio,
326 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
328 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
333 io->rbio.opts = io_opts;
334 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
335 io->rbio.bio.bi_vcnt = pages;
336 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
337 io->rbio.bio.bi_iter.bi_size = sectors << 9;
339 io->rbio.bio.bi_opf = REQ_OP_READ;
340 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
341 io->rbio.bio.bi_end_io = move_read_endio;
343 ret = bch2_data_update_init(trans, iter, ctxt, &io->write, ctxt->wp,
344 io_opts, data_opts, iter->btree_id, k);
348 io->write.op.end_io = move_write_done;
351 bch2_ratelimit_increment(ctxt->rate, k.k->size);
354 atomic64_inc(&ctxt->stats->keys_moved);
355 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
358 if (bucket_in_flight) {
359 io->b = bucket_in_flight;
360 atomic_inc(&io->b->count);
363 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
364 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
365 trace_move_extent_read2(c, k);
367 mutex_lock(&ctxt->lock);
368 atomic_add(io->read_sectors, &ctxt->read_sectors);
369 atomic_inc(&ctxt->read_ios);
371 list_add_tail(&io->read_list, &ctxt->reads);
372 list_add_tail(&io->io_list, &ctxt->ios);
373 mutex_unlock(&ctxt->lock);
376 * dropped by move_read_endio() - guards against use after free of
377 * ctxt when doing wakeup
379 closure_get(&ctxt->cl);
380 bch2_read_extent(trans, &io->rbio,
382 iter->btree_id, k, 0,
384 BCH_READ_LAST_FRAGMENT);
387 bio_free_pages(&io->write.op.wbio.bio);
391 if (ret == -BCH_ERR_data_update_done)
394 if (bch2_err_matches(ret, EROFS) ||
395 bch2_err_matches(ret, BCH_ERR_transaction_restart))
398 count_event(c, move_extent_start_fail);
400 if (trace_move_extent_start_fail_enabled()) {
401 struct printbuf buf = PRINTBUF;
403 bch2_bkey_val_to_text(&buf, c, k);
405 prt_str(&buf, bch2_err_str(ret));
406 trace_move_extent_start_fail(c, buf.buf);
412 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
413 struct per_snapshot_io_opts *io_opts,
414 struct bkey_s_c extent_k)
416 struct bch_fs *c = trans->c;
417 u32 restart_count = trans->restart_count;
420 if (io_opts->cur_inum != extent_k.k->p.inode) {
423 ret = for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
424 BTREE_ITER_ALL_SNAPSHOTS, k, ({
425 if (k.k->p.offset != extent_k.k->p.inode)
428 if (!bkey_is_inode(k.k))
431 struct bch_inode_unpacked inode;
432 BUG_ON(bch2_inode_unpack(k, &inode));
434 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
435 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
437 darray_push(&io_opts->d, e);
439 io_opts->cur_inum = extent_k.k->p.inode;
442 ret = ret ?: trans_was_restarted(trans, restart_count);
446 if (extent_k.k->p.snapshot)
447 darray_for_each(io_opts->d, i)
448 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
451 return &io_opts->fs_io_opts;
454 int bch2_move_get_io_opts_one(struct btree_trans *trans,
455 struct bch_io_opts *io_opts,
456 struct bkey_s_c extent_k)
458 struct btree_iter iter;
463 if (!extent_k.k->p.inode) {
464 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
468 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
469 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
472 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
475 if (!ret && bkey_is_inode(k.k)) {
476 struct bch_inode_unpacked inode;
477 bch2_inode_unpack(k, &inode);
478 bch2_inode_opts_get(io_opts, trans->c, &inode);
480 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
483 bch2_trans_iter_exit(trans, &iter);
487 int bch2_move_ratelimit(struct moving_context *ctxt)
489 struct bch_fs *c = ctxt->trans->c;
490 bool is_kthread = current->flags & PF_KTHREAD;
493 if (ctxt->wait_on_copygc && c->copygc_running) {
494 bch2_moving_ctxt_flush_all(ctxt);
495 wait_event_killable(c->copygc_running_wq,
496 !c->copygc_running ||
497 (is_kthread && kthread_should_stop()));
501 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
503 if (is_kthread && kthread_should_stop())
507 move_ctxt_wait_event_timeout(ctxt,
509 (is_kthread && kthread_should_stop()),
512 if (unlikely(freezing(current))) {
513 bch2_moving_ctxt_flush_all(ctxt);
519 * XXX: these limits really ought to be per device, SSDs and hard drives
520 * will want different limits
522 move_ctxt_wait_event(ctxt,
523 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
524 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
525 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
526 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
531 static int bch2_move_data_btree(struct moving_context *ctxt,
534 move_pred_fn pred, void *arg,
535 enum btree_id btree_id)
537 struct btree_trans *trans = ctxt->trans;
538 struct bch_fs *c = trans->c;
539 struct per_snapshot_io_opts snapshot_io_opts;
540 struct bch_io_opts *io_opts;
542 struct btree_iter iter;
544 struct data_update_opts data_opts;
547 per_snapshot_io_opts_init(&snapshot_io_opts, c);
548 bch2_bkey_buf_init(&sk);
551 ctxt->stats->data_type = BCH_DATA_user;
552 ctxt->stats->pos = BBPOS(btree_id, start);
555 bch2_trans_iter_init(trans, &iter, btree_id, start,
557 BTREE_ITER_ALL_SNAPSHOTS);
560 bch2_ratelimit_reset(ctxt->rate);
562 while (!bch2_move_ratelimit(ctxt)) {
563 bch2_trans_begin(trans);
565 k = bch2_btree_iter_peek(&iter);
570 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
575 if (bkey_ge(bkey_start_pos(k.k), end))
579 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
581 if (!bkey_extent_is_direct_data(k.k))
584 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
585 ret = PTR_ERR_OR_ZERO(io_opts);
589 memset(&data_opts, 0, sizeof(data_opts));
590 if (!pred(c, arg, k, io_opts, &data_opts))
594 * The iterator gets unlocked by __bch2_read_extent - need to
595 * save a copy of @k elsewhere:
597 bch2_bkey_buf_reassemble(&sk, c, k);
598 k = bkey_i_to_s_c(sk.k);
600 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
602 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
605 if (ret2 == -ENOMEM) {
606 /* memory allocation failure, wait for some IO to finish */
607 bch2_move_ctxt_wait_for_io(ctxt);
611 /* XXX signal failure */
616 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
618 bch2_btree_iter_advance(&iter);
621 bch2_trans_iter_exit(trans, &iter);
622 bch2_bkey_buf_exit(&sk, c);
623 per_snapshot_io_opts_exit(&snapshot_io_opts);
628 int __bch2_move_data(struct moving_context *ctxt,
631 move_pred_fn pred, void *arg)
633 struct bch_fs *c = ctxt->trans->c;
637 for (id = start.btree;
638 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
640 ctxt->stats->pos = BBPOS(id, POS_MIN);
642 if (!btree_type_has_ptrs(id) ||
643 !bch2_btree_id_root(c, id)->b)
646 ret = bch2_move_data_btree(ctxt,
647 id == start.btree ? start.pos : POS_MIN,
648 id == end.btree ? end.pos : POS_MAX,
657 int bch2_move_data(struct bch_fs *c,
660 struct bch_ratelimit *rate,
661 struct bch_move_stats *stats,
662 struct write_point_specifier wp,
664 move_pred_fn pred, void *arg)
667 struct moving_context ctxt;
670 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
671 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
672 bch2_moving_ctxt_exit(&ctxt);
677 int bch2_evacuate_bucket(struct moving_context *ctxt,
678 struct move_bucket_in_flight *bucket_in_flight,
679 struct bpos bucket, int gen,
680 struct data_update_opts _data_opts)
682 struct btree_trans *trans = ctxt->trans;
683 struct bch_fs *c = trans->c;
684 bool is_kthread = current->flags & PF_KTHREAD;
685 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
686 struct btree_iter iter;
688 struct bch_backpointer bp;
689 struct bch_alloc_v4 a_convert;
690 const struct bch_alloc_v4 *a;
692 struct data_update_opts data_opts;
693 unsigned dirty_sectors, bucket_size;
695 struct bpos bp_pos = POS_MIN;
698 trace_bucket_evacuate(c, &bucket);
700 bch2_bkey_buf_init(&sk);
703 * We're not run in a context that handles transaction restarts:
705 bch2_trans_begin(trans);
707 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
708 bucket, BTREE_ITER_CACHED);
709 ret = lockrestart_do(trans,
710 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
711 bch2_trans_iter_exit(trans, &iter);
713 bch_err_msg(c, ret, "looking up alloc key");
717 a = bch2_alloc_to_v4(k, &a_convert);
718 dirty_sectors = bch2_bucket_sectors_dirty(*a);
719 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
720 fragmentation = a->fragmentation_lru;
722 ret = bch2_btree_write_buffer_tryflush(trans);
723 bch_err_msg(c, ret, "flushing btree write buffer");
727 while (!(ret = bch2_move_ratelimit(ctxt))) {
728 if (is_kthread && kthread_should_stop())
731 bch2_trans_begin(trans);
733 ret = bch2_get_next_backpointer(trans, bucket, gen,
736 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
740 if (bkey_eq(bp_pos, POS_MAX))
744 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
746 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
753 bch2_bkey_buf_reassemble(&sk, c, k);
754 k = bkey_i_to_s_c(sk.k);
756 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
758 bch2_trans_iter_exit(trans, &iter);
762 data_opts = _data_opts;
763 data_opts.target = io_opts.background_target;
764 data_opts.rewrite_ptrs = 0;
767 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
768 if (ptr->dev == bucket.inode) {
769 data_opts.rewrite_ptrs |= 1U << i;
771 bch2_trans_iter_exit(trans, &iter);
778 ret = bch2_move_extent(ctxt, bucket_in_flight,
779 &iter, k, io_opts, data_opts);
780 bch2_trans_iter_exit(trans, &iter);
782 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
784 if (ret == -ENOMEM) {
785 /* memory allocation failure, wait for some IO to finish */
786 bch2_move_ctxt_wait_for_io(ctxt);
793 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
797 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
798 ret = PTR_ERR_OR_ZERO(b);
799 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
801 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
808 unsigned sectors = btree_ptr_sectors_written(&b->key);
810 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
811 bch2_trans_iter_exit(trans, &iter);
813 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
819 bch2_ratelimit_increment(ctxt->rate, sectors);
821 atomic64_add(sectors, &ctxt->stats->sectors_seen);
822 atomic64_add(sectors, &ctxt->stats->sectors_moved);
826 bp_pos = bpos_nosnap_successor(bp_pos);
829 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 unsigned f_bits = f->bits_per_field[i];
980 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
981 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
982 u64 field_offset = le64_to_cpu(f->field_offset[i]);
984 if (f_bits > unpacked_bits)
987 if ((f_bits == unpacked_bits) && field_offset)
991 ? ~((~0ULL << (f_bits - 1)) << 1)
994 if (((field_offset + f_mask) & unpacked_mask) < field_offset)
1001 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1003 struct bch_io_opts *io_opts,
1004 struct data_update_opts *data_opts)
1006 if (b->version_ondisk != c->sb.version ||
1007 btree_node_need_rewrite(b) ||
1008 bformat_needs_redo(&b->format)) {
1009 data_opts->target = 0;
1010 data_opts->extra_replicas = 0;
1011 data_opts->btree_insert_flags = 0;
1018 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1022 ret = bch2_move_btree(c,
1025 rewrite_old_nodes_pred, c, stats);
1027 mutex_lock(&c->sb_lock);
1028 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1029 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1030 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1031 bch2_write_super(c);
1032 mutex_unlock(&c->sb_lock);
1039 static bool drop_extra_replicas_pred(struct bch_fs *c, void *arg,
1041 struct bch_io_opts *io_opts,
1042 struct data_update_opts *data_opts)
1044 unsigned durability = bch2_bkey_durability(c, k);
1045 unsigned replicas = bkey_is_btree_ptr(k.k)
1046 ? c->opts.metadata_replicas
1047 : io_opts->data_replicas;
1048 const union bch_extent_entry *entry;
1049 struct extent_ptr_decoded p;
1052 bkey_for_each_ptr_decode(k.k, bch2_bkey_ptrs_c(k), p, entry) {
1053 unsigned d = bch2_extent_ptr_durability(c, &p);
1055 if (d && durability - d >= replicas) {
1056 data_opts->kill_ptrs |= BIT(i);
1063 return data_opts->kill_ptrs != 0;
1066 static bool drop_extra_replicas_btree_pred(struct bch_fs *c, void *arg,
1068 struct bch_io_opts *io_opts,
1069 struct data_update_opts *data_opts)
1071 return drop_extra_replicas_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1074 int bch2_data_job(struct bch_fs *c,
1075 struct bch_move_stats *stats,
1076 struct bch_ioctl_data op)
1078 struct bbpos start = BBPOS(op.start_btree, op.start_pos);
1079 struct bbpos end = BBPOS(op.end_btree, op.end_pos);
1082 if (op.op >= BCH_DATA_OP_NR)
1085 bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
1088 case BCH_DATA_OP_rereplicate:
1089 stats->data_type = BCH_DATA_journal;
1090 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1091 ret = bch2_move_btree(c, start, end,
1092 rereplicate_btree_pred, c, stats) ?: ret;
1093 ret = bch2_move_data(c, start, end,
1096 writepoint_hashed((unsigned long) current),
1098 rereplicate_pred, c) ?: ret;
1099 ret = bch2_replicas_gc2(c) ?: ret;
1101 case BCH_DATA_OP_migrate:
1102 if (op.migrate.dev >= c->sb.nr_devices)
1105 stats->data_type = BCH_DATA_journal;
1106 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1107 ret = bch2_move_btree(c, start, end,
1108 migrate_btree_pred, &op, stats) ?: ret;
1109 ret = bch2_move_data(c, start, end,
1112 writepoint_hashed((unsigned long) current),
1114 migrate_pred, &op) ?: ret;
1115 ret = bch2_replicas_gc2(c) ?: ret;
1117 case BCH_DATA_OP_rewrite_old_nodes:
1118 ret = bch2_scan_old_btree_nodes(c, stats);
1120 case BCH_DATA_OP_drop_extra_replicas:
1121 ret = bch2_move_btree(c, start, end,
1122 drop_extra_replicas_btree_pred, c, stats) ?: ret;
1123 ret = bch2_move_data(c, start, end, NULL, stats,
1124 writepoint_hashed((unsigned long) current),
1126 drop_extra_replicas_pred, c) ?: ret;
1127 ret = bch2_replicas_gc2(c) ?: ret;
1133 bch2_move_stats_exit(stats, c);
1137 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1139 prt_printf(out, "%s: data type==", stats->name);
1140 bch2_prt_data_type(out, stats->data_type);
1141 prt_str(out, " pos=");
1142 bch2_bbpos_to_text(out, stats->pos);
1144 printbuf_indent_add(out, 2);
1146 prt_str(out, "keys moved: ");
1147 prt_u64(out, atomic64_read(&stats->keys_moved));
1150 prt_str(out, "keys raced: ");
1151 prt_u64(out, atomic64_read(&stats->keys_raced));
1154 prt_str(out, "bytes seen: ");
1155 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1158 prt_str(out, "bytes moved: ");
1159 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1162 prt_str(out, "bytes raced: ");
1163 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1166 printbuf_indent_sub(out, 2);
1169 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1171 struct moving_io *io;
1173 bch2_move_stats_to_text(out, ctxt->stats);
1174 printbuf_indent_add(out, 2);
1176 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1177 atomic_read(&ctxt->read_ios),
1178 c->opts.move_ios_in_flight,
1179 atomic_read(&ctxt->read_sectors),
1180 c->opts.move_bytes_in_flight >> 9);
1183 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1184 atomic_read(&ctxt->write_ios),
1185 c->opts.move_ios_in_flight,
1186 atomic_read(&ctxt->write_sectors),
1187 c->opts.move_bytes_in_flight >> 9);
1190 printbuf_indent_add(out, 2);
1192 mutex_lock(&ctxt->lock);
1193 list_for_each_entry(io, &ctxt->ios, io_list)
1194 bch2_write_op_to_text(out, &io->write.op);
1195 mutex_unlock(&ctxt->lock);
1197 printbuf_indent_sub(out, 4);
1200 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1202 struct moving_context *ctxt;
1204 mutex_lock(&c->moving_context_lock);
1205 list_for_each_entry(ctxt, &c->moving_context_list, list)
1206 bch2_moving_ctxt_to_text(out, c, ctxt);
1207 mutex_unlock(&c->moving_context_lock);
1210 void bch2_fs_move_init(struct bch_fs *c)
1212 INIT_LIST_HEAD(&c->moving_context_list);
1213 mutex_init(&c->moving_context_lock);