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[];
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 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 if (bch2_err_matches(ret, EROFS) ||
349 bch2_err_matches(ret, BCH_ERR_transaction_restart))
352 count_event(c, move_extent_start_fail);
354 if (trace_move_extent_start_fail_enabled()) {
355 struct printbuf buf = PRINTBUF;
357 bch2_bkey_val_to_text(&buf, c, k);
359 prt_str(&buf, bch2_err_str(ret));
360 trace_move_extent_start_fail(c, buf.buf);
366 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
367 struct per_snapshot_io_opts *io_opts,
368 struct bkey_s_c extent_k)
370 struct bch_fs *c = trans->c;
371 u32 restart_count = trans->restart_count;
374 if (io_opts->cur_inum != extent_k.k->p.inode) {
377 ret = for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
378 BTREE_ITER_ALL_SNAPSHOTS, k, ({
379 if (k.k->p.offset != extent_k.k->p.inode)
382 if (!bkey_is_inode(k.k))
385 struct bch_inode_unpacked inode;
386 BUG_ON(bch2_inode_unpack(k, &inode));
388 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
389 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
391 darray_push(&io_opts->d, e);
393 io_opts->cur_inum = extent_k.k->p.inode;
396 ret = ret ?: trans_was_restarted(trans, restart_count);
400 if (extent_k.k->p.snapshot)
401 darray_for_each(io_opts->d, i)
402 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
405 return &io_opts->fs_io_opts;
408 int bch2_move_get_io_opts_one(struct btree_trans *trans,
409 struct bch_io_opts *io_opts,
410 struct bkey_s_c extent_k)
412 struct btree_iter iter;
417 if (!extent_k.k->p.inode) {
418 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
422 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
423 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
426 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
429 if (!ret && bkey_is_inode(k.k)) {
430 struct bch_inode_unpacked inode;
431 bch2_inode_unpack(k, &inode);
432 bch2_inode_opts_get(io_opts, trans->c, &inode);
434 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
437 bch2_trans_iter_exit(trans, &iter);
441 int bch2_move_ratelimit(struct moving_context *ctxt)
443 struct bch_fs *c = ctxt->trans->c;
444 bool is_kthread = current->flags & PF_KTHREAD;
447 if (ctxt->wait_on_copygc && c->copygc_running) {
448 bch2_moving_ctxt_flush_all(ctxt);
449 wait_event_killable(c->copygc_running_wq,
450 !c->copygc_running ||
451 (is_kthread && kthread_should_stop()));
455 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
457 if (is_kthread && kthread_should_stop())
461 move_ctxt_wait_event_timeout(ctxt,
463 (is_kthread && kthread_should_stop()),
466 if (unlikely(freezing(current))) {
467 bch2_moving_ctxt_flush_all(ctxt);
473 * XXX: these limits really ought to be per device, SSDs and hard drives
474 * will want different limits
476 move_ctxt_wait_event(ctxt,
477 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
478 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
479 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
480 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
485 static int bch2_move_data_btree(struct moving_context *ctxt,
488 move_pred_fn pred, void *arg,
489 enum btree_id btree_id)
491 struct btree_trans *trans = ctxt->trans;
492 struct bch_fs *c = trans->c;
493 struct per_snapshot_io_opts snapshot_io_opts;
494 struct bch_io_opts *io_opts;
496 struct btree_iter iter;
498 struct data_update_opts data_opts;
501 per_snapshot_io_opts_init(&snapshot_io_opts, c);
502 bch2_bkey_buf_init(&sk);
505 ctxt->stats->data_type = BCH_DATA_user;
506 ctxt->stats->pos = BBPOS(btree_id, start);
509 bch2_trans_iter_init(trans, &iter, btree_id, start,
511 BTREE_ITER_ALL_SNAPSHOTS);
514 bch2_ratelimit_reset(ctxt->rate);
516 while (!bch2_move_ratelimit(ctxt)) {
517 bch2_trans_begin(trans);
519 k = bch2_btree_iter_peek(&iter);
524 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
529 if (bkey_ge(bkey_start_pos(k.k), end))
533 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
535 if (!bkey_extent_is_direct_data(k.k))
538 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
539 ret = PTR_ERR_OR_ZERO(io_opts);
543 memset(&data_opts, 0, sizeof(data_opts));
544 if (!pred(c, arg, k, io_opts, &data_opts))
548 * The iterator gets unlocked by __bch2_read_extent - need to
549 * save a copy of @k elsewhere:
551 bch2_bkey_buf_reassemble(&sk, c, k);
552 k = bkey_i_to_s_c(sk.k);
554 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
556 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
559 if (ret2 == -ENOMEM) {
560 /* memory allocation failure, wait for some IO to finish */
561 bch2_move_ctxt_wait_for_io(ctxt);
565 /* XXX signal failure */
570 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
572 bch2_btree_iter_advance(&iter);
575 bch2_trans_iter_exit(trans, &iter);
576 bch2_bkey_buf_exit(&sk, c);
577 per_snapshot_io_opts_exit(&snapshot_io_opts);
582 int __bch2_move_data(struct moving_context *ctxt,
585 move_pred_fn pred, void *arg)
587 struct bch_fs *c = ctxt->trans->c;
591 for (id = start.btree;
592 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
594 ctxt->stats->pos = BBPOS(id, POS_MIN);
596 if (!btree_type_has_ptrs(id) ||
597 !bch2_btree_id_root(c, id)->b)
600 ret = bch2_move_data_btree(ctxt,
601 id == start.btree ? start.pos : POS_MIN,
602 id == end.btree ? end.pos : POS_MAX,
611 int bch2_move_data(struct bch_fs *c,
614 struct bch_ratelimit *rate,
615 struct bch_move_stats *stats,
616 struct write_point_specifier wp,
618 move_pred_fn pred, void *arg)
621 struct moving_context ctxt;
624 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
625 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
626 bch2_moving_ctxt_exit(&ctxt);
631 int bch2_evacuate_bucket(struct moving_context *ctxt,
632 struct move_bucket_in_flight *bucket_in_flight,
633 struct bpos bucket, int gen,
634 struct data_update_opts _data_opts)
636 struct btree_trans *trans = ctxt->trans;
637 struct bch_fs *c = trans->c;
638 bool is_kthread = current->flags & PF_KTHREAD;
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);
667 bch_err_msg(c, ret, "looking up alloc key");
671 a = bch2_alloc_to_v4(k, &a_convert);
672 dirty_sectors = bch2_bucket_sectors_dirty(*a);
673 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
674 fragmentation = a->fragmentation_lru;
676 ret = bch2_btree_write_buffer_tryflush(trans);
677 bch_err_msg(c, ret, "flushing btree write buffer");
681 while (!(ret = bch2_move_ratelimit(ctxt))) {
682 if (is_kthread && kthread_should_stop())
685 bch2_trans_begin(trans);
687 ret = bch2_get_next_backpointer(trans, bucket, gen,
690 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
694 if (bkey_eq(bp_pos, POS_MAX))
698 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
700 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
707 bch2_bkey_buf_reassemble(&sk, c, k);
708 k = bkey_i_to_s_c(sk.k);
710 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
712 bch2_trans_iter_exit(trans, &iter);
716 data_opts = _data_opts;
717 data_opts.target = io_opts.background_target;
718 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 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
789 struct btree *, struct bch_io_opts *,
790 struct data_update_opts *);
792 static int bch2_move_btree(struct bch_fs *c,
795 move_btree_pred pred, void *arg,
796 struct bch_move_stats *stats)
798 bool kthread = (current->flags & PF_KTHREAD) != 0;
799 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
800 struct moving_context ctxt;
801 struct btree_trans *trans;
802 struct btree_iter iter;
805 struct data_update_opts data_opts;
808 bch2_moving_ctxt_init(&ctxt, c, NULL, stats,
809 writepoint_ptr(&c->btree_write_point),
813 stats->data_type = BCH_DATA_btree;
815 for (btree = start.btree;
816 btree <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
818 stats->pos = BBPOS(btree, POS_MIN);
820 if (!bch2_btree_id_root(c, btree)->b)
823 bch2_trans_node_iter_init(trans, &iter, btree, POS_MIN, 0, 0,
824 BTREE_ITER_PREFETCH);
827 while (bch2_trans_begin(trans),
828 (b = bch2_btree_iter_peek_node(&iter)) &&
829 !(ret = PTR_ERR_OR_ZERO(b))) {
830 if (kthread && kthread_should_stop())
833 if ((cmp_int(btree, end.btree) ?:
834 bpos_cmp(b->key.k.p, end.pos)) > 0)
837 stats->pos = BBPOS(iter.btree_id, iter.pos);
839 if (!pred(c, arg, b, &io_opts, &data_opts))
842 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
843 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
848 bch2_btree_iter_next_node(&iter);
850 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
853 bch2_trans_iter_exit(trans, &iter);
855 if (kthread && kthread_should_stop())
860 bch2_moving_ctxt_exit(&ctxt);
861 bch2_btree_interior_updates_flush(c);
866 static bool rereplicate_pred(struct bch_fs *c, void *arg,
868 struct bch_io_opts *io_opts,
869 struct data_update_opts *data_opts)
871 unsigned nr_good = bch2_bkey_durability(c, k);
872 unsigned replicas = bkey_is_btree_ptr(k.k)
873 ? c->opts.metadata_replicas
874 : io_opts->data_replicas;
876 if (!nr_good || nr_good >= replicas)
879 data_opts->target = 0;
880 data_opts->extra_replicas = replicas - nr_good;
881 data_opts->btree_insert_flags = 0;
885 static bool migrate_pred(struct bch_fs *c, void *arg,
887 struct bch_io_opts *io_opts,
888 struct data_update_opts *data_opts)
890 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
891 struct bch_ioctl_data *op = arg;
894 data_opts->rewrite_ptrs = 0;
895 data_opts->target = 0;
896 data_opts->extra_replicas = 0;
897 data_opts->btree_insert_flags = 0;
899 bkey_for_each_ptr(ptrs, ptr) {
900 if (ptr->dev == op->migrate.dev)
901 data_opts->rewrite_ptrs |= 1U << i;
905 return data_opts->rewrite_ptrs != 0;
908 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
910 struct bch_io_opts *io_opts,
911 struct data_update_opts *data_opts)
913 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
916 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
918 struct bch_io_opts *io_opts,
919 struct data_update_opts *data_opts)
921 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
924 static bool bformat_needs_redo(struct bkey_format *f)
928 for (i = 0; i < f->nr_fields; i++) {
929 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
930 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
931 u64 field_offset = le64_to_cpu(f->field_offset[i]);
933 if (f->bits_per_field[i] > unpacked_bits)
936 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
939 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
948 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
950 struct bch_io_opts *io_opts,
951 struct data_update_opts *data_opts)
953 if (b->version_ondisk != c->sb.version ||
954 btree_node_need_rewrite(b) ||
955 bformat_needs_redo(&b->format)) {
956 data_opts->target = 0;
957 data_opts->extra_replicas = 0;
958 data_opts->btree_insert_flags = 0;
965 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
969 ret = bch2_move_btree(c,
972 rewrite_old_nodes_pred, c, stats);
974 mutex_lock(&c->sb_lock);
975 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
976 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
977 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
979 mutex_unlock(&c->sb_lock);
986 static bool drop_extra_replicas_pred(struct bch_fs *c, void *arg,
988 struct bch_io_opts *io_opts,
989 struct data_update_opts *data_opts)
991 unsigned durability = bch2_bkey_durability(c, k);
992 unsigned replicas = bkey_is_btree_ptr(k.k)
993 ? c->opts.metadata_replicas
994 : io_opts->data_replicas;
995 const union bch_extent_entry *entry;
996 struct extent_ptr_decoded p;
999 bkey_for_each_ptr_decode(k.k, bch2_bkey_ptrs_c(k), p, entry) {
1000 unsigned d = bch2_extent_ptr_durability(c, &p);
1002 if (d && durability - d >= replicas) {
1003 data_opts->kill_ptrs |= BIT(i);
1010 return data_opts->kill_ptrs != 0;
1013 static bool drop_extra_replicas_btree_pred(struct bch_fs *c, void *arg,
1015 struct bch_io_opts *io_opts,
1016 struct data_update_opts *data_opts)
1018 return drop_extra_replicas_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1021 int bch2_data_job(struct bch_fs *c,
1022 struct bch_move_stats *stats,
1023 struct bch_ioctl_data op)
1025 struct bbpos start = BBPOS(op.start_btree, op.start_pos);
1026 struct bbpos end = BBPOS(op.end_btree, op.end_pos);
1029 if (op.op >= BCH_DATA_OP_NR)
1032 bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
1035 case BCH_DATA_OP_rereplicate:
1036 stats->data_type = BCH_DATA_journal;
1037 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1038 ret = bch2_move_btree(c, start, end,
1039 rereplicate_btree_pred, c, stats) ?: ret;
1040 ret = bch2_move_data(c, start, end,
1043 writepoint_hashed((unsigned long) current),
1045 rereplicate_pred, c) ?: ret;
1046 ret = bch2_replicas_gc2(c) ?: ret;
1048 case BCH_DATA_OP_migrate:
1049 if (op.migrate.dev >= c->sb.nr_devices)
1052 stats->data_type = BCH_DATA_journal;
1053 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1054 ret = bch2_move_btree(c, start, end,
1055 migrate_btree_pred, &op, stats) ?: ret;
1056 ret = bch2_move_data(c, start, end,
1059 writepoint_hashed((unsigned long) current),
1061 migrate_pred, &op) ?: ret;
1062 ret = bch2_replicas_gc2(c) ?: ret;
1064 case BCH_DATA_OP_rewrite_old_nodes:
1065 ret = bch2_scan_old_btree_nodes(c, stats);
1067 case BCH_DATA_OP_drop_extra_replicas:
1068 ret = bch2_move_btree(c, start, end,
1069 drop_extra_replicas_btree_pred, c, stats) ?: ret;
1070 ret = bch2_move_data(c, start, end, NULL, stats,
1071 writepoint_hashed((unsigned long) current),
1073 drop_extra_replicas_pred, c) ?: ret;
1074 ret = bch2_replicas_gc2(c) ?: ret;
1080 bch2_move_stats_exit(stats, c);
1084 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1086 prt_printf(out, "%s: data type=%s pos=",
1088 bch2_data_types[stats->data_type]);
1089 bch2_bbpos_to_text(out, stats->pos);
1091 printbuf_indent_add(out, 2);
1093 prt_str(out, "keys moved: ");
1094 prt_u64(out, atomic64_read(&stats->keys_moved));
1097 prt_str(out, "keys raced: ");
1098 prt_u64(out, atomic64_read(&stats->keys_raced));
1101 prt_str(out, "bytes seen: ");
1102 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1105 prt_str(out, "bytes moved: ");
1106 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1109 prt_str(out, "bytes raced: ");
1110 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1113 printbuf_indent_sub(out, 2);
1116 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1118 struct moving_io *io;
1120 bch2_move_stats_to_text(out, ctxt->stats);
1121 printbuf_indent_add(out, 2);
1123 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1124 atomic_read(&ctxt->read_ios),
1125 c->opts.move_ios_in_flight,
1126 atomic_read(&ctxt->read_sectors),
1127 c->opts.move_bytes_in_flight >> 9);
1130 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1131 atomic_read(&ctxt->write_ios),
1132 c->opts.move_ios_in_flight,
1133 atomic_read(&ctxt->write_sectors),
1134 c->opts.move_bytes_in_flight >> 9);
1137 printbuf_indent_add(out, 2);
1139 mutex_lock(&ctxt->lock);
1140 list_for_each_entry(io, &ctxt->ios, io_list)
1141 bch2_write_op_to_text(out, &io->write.op);
1142 mutex_unlock(&ctxt->lock);
1144 printbuf_indent_sub(out, 4);
1147 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1149 struct moving_context *ctxt;
1151 mutex_lock(&c->moving_context_lock);
1152 list_for_each_entry(ctxt, &c->moving_context_list, list)
1153 bch2_moving_ctxt_to_text(out, c, ctxt);
1154 mutex_unlock(&c->moving_context_lock);
1157 void bch2_fs_move_init(struct bch_fs *c)
1159 INIT_LIST_HEAD(&c->moving_context_list);
1160 mutex_init(&c->moving_context_lock);