1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
5 #include "alloc_foreground.h"
6 #include "backpointers.h"
9 #include "btree_update.h"
10 #include "btree_update_interior.h"
11 #include "btree_write_buffer.h"
12 #include "disk_groups.h"
19 #include "journal_reclaim.h"
27 #include <linux/ioprio.h>
28 #include <linux/kthread.h>
30 const char * const bch2_data_ops_strs[] = {
31 #define x(t, n, ...) [n] = #t,
37 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k)
39 if (trace_move_extent_enabled()) {
40 struct printbuf buf = PRINTBUF;
42 bch2_bkey_val_to_text(&buf, c, k);
43 trace_move_extent(c, buf.buf);
48 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
50 if (trace_move_extent_read_enabled()) {
51 struct printbuf buf = PRINTBUF;
53 bch2_bkey_val_to_text(&buf, c, k);
54 trace_move_extent_read(c, buf.buf);
60 struct list_head read_list;
61 struct list_head io_list;
62 struct move_bucket_in_flight *b;
66 unsigned read_sectors;
67 unsigned write_sectors;
69 struct bch_read_bio rbio;
71 struct data_update write;
72 /* Must be last since it is variable size */
73 struct bio_vec bi_inline_vecs[0];
76 static void move_free(struct moving_io *io)
78 struct moving_context *ctxt = io->write.ctxt;
81 atomic_dec(&io->b->count);
83 bch2_data_update_exit(&io->write);
85 mutex_lock(&ctxt->lock);
86 list_del(&io->io_list);
88 mutex_unlock(&ctxt->lock);
93 static void move_write_done(struct bch_write_op *op)
95 struct moving_io *io = container_of(op, struct moving_io, write.op);
96 struct moving_context *ctxt = io->write.ctxt;
98 if (io->write.op.error)
99 ctxt->write_error = true;
101 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
102 atomic_dec(&io->write.ctxt->write_ios);
104 closure_put(&ctxt->cl);
107 static void move_write(struct moving_io *io)
109 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
114 closure_get(&io->write.ctxt->cl);
115 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
116 atomic_inc(&io->write.ctxt->write_ios);
118 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
121 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
123 struct moving_io *io =
124 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
126 return io && io->read_completed ? io : NULL;
129 static void move_read_endio(struct bio *bio)
131 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
132 struct moving_context *ctxt = io->write.ctxt;
134 atomic_sub(io->read_sectors, &ctxt->read_sectors);
135 atomic_dec(&ctxt->read_ios);
136 io->read_completed = true;
138 wake_up(&ctxt->wait);
139 closure_put(&ctxt->cl);
142 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt)
144 struct moving_io *io;
146 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
147 bch2_trans_unlock_long(ctxt->trans);
148 list_del(&io->read_list);
153 void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
155 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
157 move_ctxt_wait_event(ctxt,
158 !atomic_read(&ctxt->write_sectors) ||
159 atomic_read(&ctxt->write_sectors) != sectors_pending);
162 static void bch2_moving_ctxt_flush_all(struct moving_context *ctxt)
164 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
165 bch2_trans_unlock_long(ctxt->trans);
166 closure_sync(&ctxt->cl);
169 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
171 struct bch_fs *c = ctxt->trans->c;
173 bch2_moving_ctxt_flush_all(ctxt);
175 EBUG_ON(atomic_read(&ctxt->write_sectors));
176 EBUG_ON(atomic_read(&ctxt->write_ios));
177 EBUG_ON(atomic_read(&ctxt->read_sectors));
178 EBUG_ON(atomic_read(&ctxt->read_ios));
180 mutex_lock(&c->moving_context_lock);
181 list_del(&ctxt->list);
182 mutex_unlock(&c->moving_context_lock);
184 bch2_trans_put(ctxt->trans);
185 memset(ctxt, 0, sizeof(*ctxt));
188 void bch2_moving_ctxt_init(struct moving_context *ctxt,
190 struct bch_ratelimit *rate,
191 struct bch_move_stats *stats,
192 struct write_point_specifier wp,
195 memset(ctxt, 0, sizeof(*ctxt));
197 ctxt->trans = bch2_trans_get(c);
198 ctxt->fn = (void *) _RET_IP_;
202 ctxt->wait_on_copygc = wait_on_copygc;
204 closure_init_stack(&ctxt->cl);
206 mutex_init(&ctxt->lock);
207 INIT_LIST_HEAD(&ctxt->reads);
208 INIT_LIST_HEAD(&ctxt->ios);
209 init_waitqueue_head(&ctxt->wait);
211 mutex_lock(&c->moving_context_lock);
212 list_add(&ctxt->list, &c->moving_context_list);
213 mutex_unlock(&c->moving_context_lock);
216 void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
218 trace_move_data(c, stats);
221 void bch2_move_stats_init(struct bch_move_stats *stats, const char *name)
223 memset(stats, 0, sizeof(*stats));
224 stats->data_type = BCH_DATA_user;
225 scnprintf(stats->name, sizeof(stats->name), "%s", name);
228 int bch2_move_extent(struct moving_context *ctxt,
229 struct move_bucket_in_flight *bucket_in_flight,
230 struct btree_iter *iter,
232 struct bch_io_opts io_opts,
233 struct data_update_opts data_opts)
235 struct btree_trans *trans = ctxt->trans;
236 struct bch_fs *c = trans->c;
237 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
238 struct moving_io *io;
239 const union bch_extent_entry *entry;
240 struct extent_ptr_decoded p;
241 unsigned sectors = k.k->size, pages;
245 ctxt->stats->pos = BBPOS(iter->btree_id, iter->pos);
246 trace_move_extent2(c, k);
248 bch2_data_update_opts_normalize(k, &data_opts);
250 if (!data_opts.rewrite_ptrs &&
251 !data_opts.extra_replicas) {
252 if (data_opts.kill_ptrs)
253 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
258 * Before memory allocations & taking nocow locks in
259 * bch2_data_update_init():
261 bch2_trans_unlock(trans);
263 /* write path might have to decompress data: */
264 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
265 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
267 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
268 io = kzalloc(sizeof(struct moving_io) +
269 sizeof(struct bio_vec) * pages, GFP_KERNEL);
273 INIT_LIST_HEAD(&io->io_list);
274 io->write.ctxt = ctxt;
275 io->read_sectors = k.k->size;
276 io->write_sectors = k.k->size;
278 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
279 bio_set_prio(&io->write.op.wbio.bio,
280 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
282 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
287 io->rbio.opts = io_opts;
288 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
289 io->rbio.bio.bi_vcnt = pages;
290 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
291 io->rbio.bio.bi_iter.bi_size = sectors << 9;
293 io->rbio.bio.bi_opf = REQ_OP_READ;
294 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
295 io->rbio.bio.bi_end_io = move_read_endio;
297 ret = bch2_data_update_init(trans, iter, ctxt, &io->write, ctxt->wp,
298 io_opts, data_opts, iter->btree_id, k);
302 io->write.op.end_io = move_write_done;
305 bch2_ratelimit_increment(ctxt->rate, k.k->size);
308 atomic64_inc(&ctxt->stats->keys_moved);
309 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
312 if (bucket_in_flight) {
313 io->b = bucket_in_flight;
314 atomic_inc(&io->b->count);
317 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
318 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
319 trace_move_extent_read2(c, k);
321 mutex_lock(&ctxt->lock);
322 atomic_add(io->read_sectors, &ctxt->read_sectors);
323 atomic_inc(&ctxt->read_ios);
325 list_add_tail(&io->read_list, &ctxt->reads);
326 list_add_tail(&io->io_list, &ctxt->ios);
327 mutex_unlock(&ctxt->lock);
330 * dropped by move_read_endio() - guards against use after free of
331 * ctxt when doing wakeup
333 closure_get(&ctxt->cl);
334 bch2_read_extent(trans, &io->rbio,
336 iter->btree_id, k, 0,
338 BCH_READ_LAST_FRAGMENT);
341 bio_free_pages(&io->write.op.wbio.bio);
345 if (ret == -BCH_ERR_data_update_done)
348 this_cpu_inc(c->counters[BCH_COUNTER_move_extent_start_fail]);
349 if (trace_move_extent_start_fail_enabled()) {
350 struct printbuf buf = PRINTBUF;
352 bch2_bkey_val_to_text(&buf, c, k);
354 prt_str(&buf, bch2_err_str(ret));
355 trace_move_extent_start_fail(c, buf.buf);
361 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
362 struct per_snapshot_io_opts *io_opts,
363 struct bkey_s_c extent_k)
365 struct bch_fs *c = trans->c;
366 u32 restart_count = trans->restart_count;
369 if (io_opts->cur_inum != extent_k.k->p.inode) {
370 struct btree_iter iter;
375 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
376 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
377 if (k.k->p.offset != extent_k.k->p.inode)
380 if (!bkey_is_inode(k.k))
383 struct bch_inode_unpacked inode;
384 BUG_ON(bch2_inode_unpack(k, &inode));
386 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
387 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
389 ret = darray_push(&io_opts->d, e);
393 bch2_trans_iter_exit(trans, &iter);
394 io_opts->cur_inum = extent_k.k->p.inode;
397 ret = ret ?: trans_was_restarted(trans, restart_count);
401 if (extent_k.k->p.snapshot) {
402 struct snapshot_io_opts_entry *i;
403 darray_for_each(io_opts->d, i)
404 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
408 return &io_opts->fs_io_opts;
411 int bch2_move_get_io_opts_one(struct btree_trans *trans,
412 struct bch_io_opts *io_opts,
413 struct bkey_s_c extent_k)
415 struct btree_iter iter;
420 if (!extent_k.k->p.inode) {
421 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
425 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
426 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
429 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
432 if (!ret && bkey_is_inode(k.k)) {
433 struct bch_inode_unpacked inode;
434 bch2_inode_unpack(k, &inode);
435 bch2_inode_opts_get(io_opts, trans->c, &inode);
437 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
440 bch2_trans_iter_exit(trans, &iter);
444 int bch2_move_ratelimit(struct moving_context *ctxt)
446 struct bch_fs *c = ctxt->trans->c;
449 if (ctxt->wait_on_copygc && c->copygc_running) {
450 bch2_moving_ctxt_flush_all(ctxt);
451 wait_event_killable(c->copygc_running_wq,
452 !c->copygc_running ||
453 kthread_should_stop());
457 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
459 if (kthread_should_stop())
463 move_ctxt_wait_event_timeout(ctxt,
464 freezing(current) || kthread_should_stop(),
467 if (unlikely(freezing(current))) {
468 bch2_moving_ctxt_flush_all(ctxt);
474 * XXX: these limits really ought to be per device, SSDs and hard drives
475 * will want different limits
477 move_ctxt_wait_event(ctxt,
478 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
479 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
480 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
481 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
486 static int bch2_move_data_btree(struct moving_context *ctxt,
489 move_pred_fn pred, void *arg,
490 enum btree_id btree_id)
492 struct btree_trans *trans = ctxt->trans;
493 struct bch_fs *c = trans->c;
494 struct per_snapshot_io_opts snapshot_io_opts;
495 struct bch_io_opts *io_opts;
497 struct btree_iter iter;
499 struct data_update_opts data_opts;
502 per_snapshot_io_opts_init(&snapshot_io_opts, c);
503 bch2_bkey_buf_init(&sk);
506 ctxt->stats->data_type = BCH_DATA_user;
507 ctxt->stats->pos = BBPOS(btree_id, start);
510 bch2_trans_iter_init(trans, &iter, btree_id, start,
512 BTREE_ITER_ALL_SNAPSHOTS);
515 bch2_ratelimit_reset(ctxt->rate);
517 while (!bch2_move_ratelimit(ctxt)) {
518 bch2_trans_begin(trans);
520 k = bch2_btree_iter_peek(&iter);
525 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
530 if (bkey_ge(bkey_start_pos(k.k), end))
534 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
536 if (!bkey_extent_is_direct_data(k.k))
539 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
540 ret = PTR_ERR_OR_ZERO(io_opts);
544 memset(&data_opts, 0, sizeof(data_opts));
545 if (!pred(c, arg, k, io_opts, &data_opts))
549 * The iterator gets unlocked by __bch2_read_extent - need to
550 * save a copy of @k elsewhere:
552 bch2_bkey_buf_reassemble(&sk, c, k);
553 k = bkey_i_to_s_c(sk.k);
555 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
557 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
560 if (ret2 == -ENOMEM) {
561 /* memory allocation failure, wait for some IO to finish */
562 bch2_move_ctxt_wait_for_io(ctxt);
566 /* XXX signal failure */
571 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
573 bch2_btree_iter_advance(&iter);
576 bch2_trans_iter_exit(trans, &iter);
577 bch2_bkey_buf_exit(&sk, c);
578 per_snapshot_io_opts_exit(&snapshot_io_opts);
583 int __bch2_move_data(struct moving_context *ctxt,
586 move_pred_fn pred, void *arg)
588 struct bch_fs *c = ctxt->trans->c;
592 for (id = start.btree;
593 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
595 ctxt->stats->pos = BBPOS(id, POS_MIN);
597 if (!btree_type_has_ptrs(id) ||
598 !bch2_btree_id_root(c, id)->b)
601 ret = bch2_move_data_btree(ctxt,
602 id == start.btree ? start.pos : POS_MIN,
603 id == end.btree ? end.pos : POS_MAX,
612 int bch2_move_data(struct bch_fs *c,
615 struct bch_ratelimit *rate,
616 struct bch_move_stats *stats,
617 struct write_point_specifier wp,
619 move_pred_fn pred, void *arg)
622 struct moving_context ctxt;
625 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
626 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
627 bch2_moving_ctxt_exit(&ctxt);
632 int __bch2_evacuate_bucket(struct moving_context *ctxt,
633 struct move_bucket_in_flight *bucket_in_flight,
634 struct bpos bucket, int gen,
635 struct data_update_opts _data_opts)
637 struct btree_trans *trans = ctxt->trans;
638 struct bch_fs *c = trans->c;
639 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
640 struct btree_iter iter;
642 struct bch_backpointer bp;
643 struct bch_alloc_v4 a_convert;
644 const struct bch_alloc_v4 *a;
646 struct data_update_opts data_opts;
647 unsigned dirty_sectors, bucket_size;
649 struct bpos bp_pos = POS_MIN;
652 trace_bucket_evacuate(c, &bucket);
654 bch2_bkey_buf_init(&sk);
657 * We're not run in a context that handles transaction restarts:
659 bch2_trans_begin(trans);
661 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
662 bucket, BTREE_ITER_CACHED);
663 ret = lockrestart_do(trans,
664 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
665 bch2_trans_iter_exit(trans, &iter);
668 bch_err_msg(c, ret, "looking up alloc key");
672 a = bch2_alloc_to_v4(k, &a_convert);
673 dirty_sectors = bch2_bucket_sectors_dirty(*a);
674 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
675 fragmentation = a->fragmentation_lru;
677 ret = bch2_btree_write_buffer_tryflush(trans);
678 bch_err_msg(c, ret, "flushing btree write buffer");
682 while (!(ret = bch2_move_ratelimit(ctxt))) {
683 bch2_trans_begin(trans);
685 ret = bch2_get_next_backpointer(trans, bucket, gen,
688 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
692 if (bkey_eq(bp_pos, POS_MAX))
696 const struct bch_extent_ptr *ptr;
699 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
701 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
708 bch2_bkey_buf_reassemble(&sk, c, k);
709 k = bkey_i_to_s_c(sk.k);
711 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
713 bch2_trans_iter_exit(trans, &iter);
717 data_opts = _data_opts;
718 data_opts.target = io_opts.background_target;
719 data_opts.rewrite_ptrs = 0;
721 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
722 if (ptr->dev == bucket.inode) {
723 data_opts.rewrite_ptrs |= 1U << i;
725 bch2_trans_iter_exit(trans, &iter);
732 ret = bch2_move_extent(ctxt, bucket_in_flight,
733 &iter, k, io_opts, data_opts);
734 bch2_trans_iter_exit(trans, &iter);
736 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
738 if (ret == -ENOMEM) {
739 /* memory allocation failure, wait for some IO to finish */
740 bch2_move_ctxt_wait_for_io(ctxt);
747 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
751 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
752 ret = PTR_ERR_OR_ZERO(b);
753 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
755 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
762 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
763 bch2_trans_iter_exit(trans, &iter);
765 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
771 bch2_ratelimit_increment(ctxt->rate,
772 c->opts.btree_node_size >> 9);
774 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
775 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
779 bp_pos = bpos_nosnap_successor(bp_pos);
782 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
784 bch2_bkey_buf_exit(&sk, c);
788 int bch2_evacuate_bucket(struct bch_fs *c,
789 struct bpos bucket, int gen,
790 struct data_update_opts data_opts,
791 struct bch_ratelimit *rate,
792 struct bch_move_stats *stats,
793 struct write_point_specifier wp,
796 struct moving_context ctxt;
799 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
800 ret = __bch2_evacuate_bucket(&ctxt, NULL, bucket, gen, data_opts);
801 bch2_moving_ctxt_exit(&ctxt);
806 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
807 struct btree *, struct bch_io_opts *,
808 struct data_update_opts *);
810 static int bch2_move_btree(struct bch_fs *c,
813 move_btree_pred pred, void *arg,
814 struct bch_move_stats *stats)
816 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
817 struct moving_context ctxt;
818 struct btree_trans *trans;
819 struct btree_iter iter;
822 struct data_update_opts data_opts;
825 bch2_moving_ctxt_init(&ctxt, c, NULL, stats,
826 writepoint_ptr(&c->btree_write_point),
830 stats->data_type = BCH_DATA_btree;
832 for (btree = start.btree;
833 btree <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
835 stats->pos = BBPOS(btree, POS_MIN);
837 if (!bch2_btree_id_root(c, btree)->b)
840 bch2_trans_node_iter_init(trans, &iter, btree, POS_MIN, 0, 0,
841 BTREE_ITER_PREFETCH);
844 while (bch2_trans_begin(trans),
845 (b = bch2_btree_iter_peek_node(&iter)) &&
846 !(ret = PTR_ERR_OR_ZERO(b))) {
847 if (kthread_should_stop())
850 if ((cmp_int(btree, end.btree) ?:
851 bpos_cmp(b->key.k.p, end.pos)) > 0)
854 stats->pos = BBPOS(iter.btree_id, iter.pos);
856 if (!pred(c, arg, b, &io_opts, &data_opts))
859 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
860 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
865 bch2_btree_iter_next_node(&iter);
867 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
870 bch2_trans_iter_exit(trans, &iter);
872 if (kthread_should_stop())
877 bch2_moving_ctxt_exit(&ctxt);
878 bch2_btree_interior_updates_flush(c);
883 static bool rereplicate_pred(struct bch_fs *c, void *arg,
885 struct bch_io_opts *io_opts,
886 struct data_update_opts *data_opts)
888 unsigned nr_good = bch2_bkey_durability(c, k);
889 unsigned replicas = bkey_is_btree_ptr(k.k)
890 ? c->opts.metadata_replicas
891 : io_opts->data_replicas;
893 if (!nr_good || nr_good >= replicas)
896 data_opts->target = 0;
897 data_opts->extra_replicas = replicas - nr_good;
898 data_opts->btree_insert_flags = 0;
902 static bool migrate_pred(struct bch_fs *c, void *arg,
904 struct bch_io_opts *io_opts,
905 struct data_update_opts *data_opts)
907 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
908 const struct bch_extent_ptr *ptr;
909 struct bch_ioctl_data *op = arg;
912 data_opts->rewrite_ptrs = 0;
913 data_opts->target = 0;
914 data_opts->extra_replicas = 0;
915 data_opts->btree_insert_flags = 0;
917 bkey_for_each_ptr(ptrs, ptr) {
918 if (ptr->dev == op->migrate.dev)
919 data_opts->rewrite_ptrs |= 1U << i;
923 return data_opts->rewrite_ptrs != 0;
926 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
928 struct bch_io_opts *io_opts,
929 struct data_update_opts *data_opts)
931 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
934 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
936 struct bch_io_opts *io_opts,
937 struct data_update_opts *data_opts)
939 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
942 static bool bformat_needs_redo(struct bkey_format *f)
946 for (i = 0; i < f->nr_fields; i++) {
947 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
948 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
949 u64 field_offset = le64_to_cpu(f->field_offset[i]);
951 if (f->bits_per_field[i] > unpacked_bits)
954 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
957 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
966 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
968 struct bch_io_opts *io_opts,
969 struct data_update_opts *data_opts)
971 if (b->version_ondisk != c->sb.version ||
972 btree_node_need_rewrite(b) ||
973 bformat_needs_redo(&b->format)) {
974 data_opts->target = 0;
975 data_opts->extra_replicas = 0;
976 data_opts->btree_insert_flags = 0;
983 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
987 ret = bch2_move_btree(c,
990 rewrite_old_nodes_pred, c, stats);
992 mutex_lock(&c->sb_lock);
993 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
994 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
995 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
997 mutex_unlock(&c->sb_lock);
1004 static bool drop_extra_replicas_pred(struct bch_fs *c, void *arg,
1006 struct bch_io_opts *io_opts,
1007 struct data_update_opts *data_opts)
1009 unsigned durability = bch2_bkey_durability(c, k);
1010 unsigned replicas = bkey_is_btree_ptr(k.k)
1011 ? c->opts.metadata_replicas
1012 : io_opts->data_replicas;
1013 const union bch_extent_entry *entry;
1014 struct extent_ptr_decoded p;
1017 bkey_for_each_ptr_decode(k.k, bch2_bkey_ptrs_c(k), p, entry) {
1018 unsigned d = bch2_extent_ptr_durability(c, &p);
1020 if (d && durability - d >= replicas) {
1021 data_opts->kill_ptrs |= BIT(i);
1028 return data_opts->kill_ptrs != 0;
1031 static bool drop_extra_replicas_btree_pred(struct bch_fs *c, void *arg,
1033 struct bch_io_opts *io_opts,
1034 struct data_update_opts *data_opts)
1036 return drop_extra_replicas_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1039 int bch2_data_job(struct bch_fs *c,
1040 struct bch_move_stats *stats,
1041 struct bch_ioctl_data op)
1043 struct bbpos start = BBPOS(op.start_btree, op.start_pos);
1044 struct bbpos end = BBPOS(op.end_btree, op.end_pos);
1047 if (op.op >= BCH_DATA_OP_NR)
1050 bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
1053 case BCH_DATA_OP_rereplicate:
1054 stats->data_type = BCH_DATA_journal;
1055 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1056 ret = bch2_move_btree(c, start, end,
1057 rereplicate_btree_pred, c, stats) ?: ret;
1058 ret = bch2_move_data(c, start, end,
1061 writepoint_hashed((unsigned long) current),
1063 rereplicate_pred, c) ?: ret;
1064 ret = bch2_replicas_gc2(c) ?: ret;
1066 case BCH_DATA_OP_migrate:
1067 if (op.migrate.dev >= c->sb.nr_devices)
1070 stats->data_type = BCH_DATA_journal;
1071 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1072 ret = bch2_move_btree(c, start, end,
1073 migrate_btree_pred, &op, stats) ?: ret;
1074 ret = bch2_move_data(c, start, end,
1077 writepoint_hashed((unsigned long) current),
1079 migrate_pred, &op) ?: ret;
1080 ret = bch2_replicas_gc2(c) ?: ret;
1082 case BCH_DATA_OP_rewrite_old_nodes:
1083 ret = bch2_scan_old_btree_nodes(c, stats);
1085 case BCH_DATA_OP_drop_extra_replicas:
1086 ret = bch2_move_btree(c, start, end,
1087 drop_extra_replicas_btree_pred, c, stats) ?: ret;
1088 ret = bch2_move_data(c, start, end, NULL, stats,
1089 writepoint_hashed((unsigned long) current),
1091 drop_extra_replicas_pred, c) ?: ret;
1092 ret = bch2_replicas_gc2(c) ?: ret;
1098 bch2_move_stats_exit(stats, c);
1102 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1104 prt_printf(out, "%s: data type=%s pos=",
1106 bch2_data_types[stats->data_type]);
1107 bch2_bbpos_to_text(out, stats->pos);
1109 printbuf_indent_add(out, 2);
1111 prt_str(out, "keys moved: ");
1112 prt_u64(out, atomic64_read(&stats->keys_moved));
1115 prt_str(out, "keys raced: ");
1116 prt_u64(out, atomic64_read(&stats->keys_raced));
1119 prt_str(out, "bytes seen: ");
1120 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1123 prt_str(out, "bytes moved: ");
1124 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1127 prt_str(out, "bytes raced: ");
1128 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1131 printbuf_indent_sub(out, 2);
1134 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1136 struct moving_io *io;
1138 bch2_move_stats_to_text(out, ctxt->stats);
1139 printbuf_indent_add(out, 2);
1141 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1142 atomic_read(&ctxt->read_ios),
1143 c->opts.move_ios_in_flight,
1144 atomic_read(&ctxt->read_sectors),
1145 c->opts.move_bytes_in_flight >> 9);
1148 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1149 atomic_read(&ctxt->write_ios),
1150 c->opts.move_ios_in_flight,
1151 atomic_read(&ctxt->write_sectors),
1152 c->opts.move_bytes_in_flight >> 9);
1155 printbuf_indent_add(out, 2);
1157 mutex_lock(&ctxt->lock);
1158 list_for_each_entry(io, &ctxt->ios, io_list)
1159 bch2_write_op_to_text(out, &io->write.op);
1160 mutex_unlock(&ctxt->lock);
1162 printbuf_indent_sub(out, 4);
1165 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1167 struct moving_context *ctxt;
1169 mutex_lock(&c->moving_context_lock);
1170 list_for_each_entry(ctxt, &c->moving_context_list, list)
1171 bch2_moving_ctxt_to_text(out, c, ctxt);
1172 mutex_unlock(&c->moving_context_lock);
1175 void bch2_fs_move_init(struct bch_fs *c)
1177 INIT_LIST_HEAD(&c->moving_context_list);
1178 mutex_init(&c->moving_context_lock);