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);
59 static void trace_move_extent_alloc_mem_fail2(struct bch_fs *c, struct bkey_s_c k)
61 if (trace_move_extent_alloc_mem_fail_enabled()) {
62 struct printbuf buf = PRINTBUF;
64 bch2_bkey_val_to_text(&buf, c, k);
65 trace_move_extent_alloc_mem_fail(c, buf.buf);
71 struct list_head read_list;
72 struct list_head io_list;
73 struct move_bucket_in_flight *b;
77 unsigned read_sectors;
78 unsigned write_sectors;
80 struct bch_read_bio rbio;
82 struct data_update write;
83 /* Must be last since it is variable size */
84 struct bio_vec bi_inline_vecs[0];
87 static void move_free(struct moving_io *io)
89 struct moving_context *ctxt = io->write.ctxt;
92 atomic_dec(&io->b->count);
94 bch2_data_update_exit(&io->write);
96 mutex_lock(&ctxt->lock);
97 list_del(&io->io_list);
99 mutex_unlock(&ctxt->lock);
104 static void move_write_done(struct bch_write_op *op)
106 struct moving_io *io = container_of(op, struct moving_io, write.op);
107 struct moving_context *ctxt = io->write.ctxt;
109 if (io->write.op.error)
110 ctxt->write_error = true;
112 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
113 atomic_dec(&io->write.ctxt->write_ios);
115 closure_put(&ctxt->cl);
118 static void move_write(struct moving_io *io)
120 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
125 closure_get(&io->write.ctxt->cl);
126 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
127 atomic_inc(&io->write.ctxt->write_ios);
129 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
132 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
134 struct moving_io *io =
135 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
137 return io && io->read_completed ? io : NULL;
140 static void move_read_endio(struct bio *bio)
142 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
143 struct moving_context *ctxt = io->write.ctxt;
145 atomic_sub(io->read_sectors, &ctxt->read_sectors);
146 atomic_dec(&ctxt->read_ios);
147 io->read_completed = true;
149 wake_up(&ctxt->wait);
150 closure_put(&ctxt->cl);
153 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt)
155 struct moving_io *io;
157 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
158 bch2_trans_unlock_long(ctxt->trans);
159 list_del(&io->read_list);
164 void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
166 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
168 move_ctxt_wait_event(ctxt,
169 !atomic_read(&ctxt->write_sectors) ||
170 atomic_read(&ctxt->write_sectors) != sectors_pending);
173 static void bch2_moving_ctxt_flush_all(struct moving_context *ctxt)
175 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
176 bch2_trans_unlock_long(ctxt->trans);
177 closure_sync(&ctxt->cl);
180 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
182 struct bch_fs *c = ctxt->trans->c;
184 bch2_moving_ctxt_flush_all(ctxt);
186 EBUG_ON(atomic_read(&ctxt->write_sectors));
187 EBUG_ON(atomic_read(&ctxt->write_ios));
188 EBUG_ON(atomic_read(&ctxt->read_sectors));
189 EBUG_ON(atomic_read(&ctxt->read_ios));
191 mutex_lock(&c->moving_context_lock);
192 list_del(&ctxt->list);
193 mutex_unlock(&c->moving_context_lock);
195 bch2_trans_put(ctxt->trans);
196 memset(ctxt, 0, sizeof(*ctxt));
199 void bch2_moving_ctxt_init(struct moving_context *ctxt,
201 struct bch_ratelimit *rate,
202 struct bch_move_stats *stats,
203 struct write_point_specifier wp,
206 memset(ctxt, 0, sizeof(*ctxt));
208 ctxt->trans = bch2_trans_get(c);
209 ctxt->fn = (void *) _RET_IP_;
213 ctxt->wait_on_copygc = wait_on_copygc;
215 closure_init_stack(&ctxt->cl);
217 mutex_init(&ctxt->lock);
218 INIT_LIST_HEAD(&ctxt->reads);
219 INIT_LIST_HEAD(&ctxt->ios);
220 init_waitqueue_head(&ctxt->wait);
222 mutex_lock(&c->moving_context_lock);
223 list_add(&ctxt->list, &c->moving_context_list);
224 mutex_unlock(&c->moving_context_lock);
227 void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
229 trace_move_data(c, stats);
232 void bch2_move_stats_init(struct bch_move_stats *stats, const char *name)
234 memset(stats, 0, sizeof(*stats));
235 stats->data_type = BCH_DATA_user;
236 scnprintf(stats->name, sizeof(stats->name), "%s", name);
239 int bch2_move_extent(struct moving_context *ctxt,
240 struct move_bucket_in_flight *bucket_in_flight,
241 struct btree_iter *iter,
243 struct bch_io_opts io_opts,
244 struct data_update_opts data_opts)
246 struct btree_trans *trans = ctxt->trans;
247 struct bch_fs *c = trans->c;
248 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
249 struct moving_io *io;
250 const union bch_extent_entry *entry;
251 struct extent_ptr_decoded p;
252 unsigned sectors = k.k->size, pages;
256 ctxt->stats->pos = BBPOS(iter->btree_id, iter->pos);
257 trace_move_extent2(c, k);
259 bch2_data_update_opts_normalize(k, &data_opts);
261 if (!data_opts.rewrite_ptrs &&
262 !data_opts.extra_replicas) {
263 if (data_opts.kill_ptrs)
264 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
269 * Before memory allocations & taking nocow locks in
270 * bch2_data_update_init():
272 bch2_trans_unlock(trans);
274 /* write path might have to decompress data: */
275 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
276 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
278 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
279 io = kzalloc(sizeof(struct moving_io) +
280 sizeof(struct bio_vec) * pages, GFP_KERNEL);
284 INIT_LIST_HEAD(&io->io_list);
285 io->write.ctxt = ctxt;
286 io->read_sectors = k.k->size;
287 io->write_sectors = k.k->size;
289 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
290 bio_set_prio(&io->write.op.wbio.bio,
291 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
293 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
298 io->rbio.opts = io_opts;
299 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
300 io->rbio.bio.bi_vcnt = pages;
301 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
302 io->rbio.bio.bi_iter.bi_size = sectors << 9;
304 io->rbio.bio.bi_opf = REQ_OP_READ;
305 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
306 io->rbio.bio.bi_end_io = move_read_endio;
308 ret = bch2_data_update_init(trans, iter, ctxt, &io->write, ctxt->wp,
309 io_opts, data_opts, iter->btree_id, k);
313 io->write.op.end_io = move_write_done;
316 bch2_ratelimit_increment(ctxt->rate, k.k->size);
319 atomic64_inc(&ctxt->stats->keys_moved);
320 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
323 if (bucket_in_flight) {
324 io->b = bucket_in_flight;
325 atomic_inc(&io->b->count);
328 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
329 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
330 trace_move_extent_read2(c, k);
332 mutex_lock(&ctxt->lock);
333 atomic_add(io->read_sectors, &ctxt->read_sectors);
334 atomic_inc(&ctxt->read_ios);
336 list_add_tail(&io->read_list, &ctxt->reads);
337 list_add_tail(&io->io_list, &ctxt->ios);
338 mutex_unlock(&ctxt->lock);
341 * dropped by move_read_endio() - guards against use after free of
342 * ctxt when doing wakeup
344 closure_get(&ctxt->cl);
345 bch2_read_extent(trans, &io->rbio,
347 iter->btree_id, k, 0,
349 BCH_READ_LAST_FRAGMENT);
352 bio_free_pages(&io->write.op.wbio.bio);
356 if (ret == -BCH_ERR_data_update_done)
359 this_cpu_inc(c->counters[BCH_COUNTER_move_extent_alloc_mem_fail]);
360 trace_move_extent_alloc_mem_fail2(c, k);
364 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
365 struct per_snapshot_io_opts *io_opts,
366 struct bkey_s_c extent_k)
368 struct bch_fs *c = trans->c;
369 u32 restart_count = trans->restart_count;
372 if (io_opts->cur_inum != extent_k.k->p.inode) {
373 struct btree_iter iter;
378 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
379 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
380 if (k.k->p.offset != extent_k.k->p.inode)
383 if (!bkey_is_inode(k.k))
386 struct bch_inode_unpacked inode;
387 BUG_ON(bch2_inode_unpack(k, &inode));
389 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
390 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
392 ret = darray_push(&io_opts->d, e);
396 bch2_trans_iter_exit(trans, &iter);
397 io_opts->cur_inum = extent_k.k->p.inode;
400 ret = ret ?: trans_was_restarted(trans, restart_count);
404 if (extent_k.k->p.snapshot) {
405 struct snapshot_io_opts_entry *i;
406 darray_for_each(io_opts->d, i)
407 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
411 return &io_opts->fs_io_opts;
414 int bch2_move_get_io_opts_one(struct btree_trans *trans,
415 struct bch_io_opts *io_opts,
416 struct bkey_s_c extent_k)
418 struct btree_iter iter;
423 if (!extent_k.k->p.inode) {
424 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
428 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
429 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
432 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
435 if (!ret && bkey_is_inode(k.k)) {
436 struct bch_inode_unpacked inode;
437 bch2_inode_unpack(k, &inode);
438 bch2_inode_opts_get(io_opts, trans->c, &inode);
440 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
443 bch2_trans_iter_exit(trans, &iter);
447 int bch2_move_ratelimit(struct moving_context *ctxt)
449 struct bch_fs *c = ctxt->trans->c;
452 if (ctxt->wait_on_copygc && c->copygc_running) {
453 bch2_moving_ctxt_flush_all(ctxt);
454 wait_event_killable(c->copygc_running_wq,
455 !c->copygc_running ||
456 kthread_should_stop());
460 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
462 if (kthread_should_stop())
466 move_ctxt_wait_event_timeout(ctxt,
467 freezing(current) || kthread_should_stop(),
470 if (unlikely(freezing(current))) {
471 bch2_moving_ctxt_flush_all(ctxt);
477 * XXX: these limits really ought to be per device, SSDs and hard drives
478 * will want different limits
480 move_ctxt_wait_event(ctxt,
481 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
482 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
483 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
484 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
489 static int bch2_move_data_btree(struct moving_context *ctxt,
492 move_pred_fn pred, void *arg,
493 enum btree_id btree_id)
495 struct btree_trans *trans = ctxt->trans;
496 struct bch_fs *c = trans->c;
497 struct per_snapshot_io_opts snapshot_io_opts;
498 struct bch_io_opts *io_opts;
500 struct btree_iter iter;
502 struct data_update_opts data_opts;
505 per_snapshot_io_opts_init(&snapshot_io_opts, c);
506 bch2_bkey_buf_init(&sk);
509 ctxt->stats->data_type = BCH_DATA_user;
510 ctxt->stats->pos = BBPOS(btree_id, start);
513 bch2_trans_iter_init(trans, &iter, btree_id, start,
515 BTREE_ITER_ALL_SNAPSHOTS);
518 bch2_ratelimit_reset(ctxt->rate);
520 while (!bch2_move_ratelimit(ctxt)) {
521 bch2_trans_begin(trans);
523 k = bch2_btree_iter_peek(&iter);
528 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
533 if (bkey_ge(bkey_start_pos(k.k), end))
537 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
539 if (!bkey_extent_is_direct_data(k.k))
542 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
543 ret = PTR_ERR_OR_ZERO(io_opts);
547 memset(&data_opts, 0, sizeof(data_opts));
548 if (!pred(c, arg, k, io_opts, &data_opts))
552 * The iterator gets unlocked by __bch2_read_extent - need to
553 * save a copy of @k elsewhere:
555 bch2_bkey_buf_reassemble(&sk, c, k);
556 k = bkey_i_to_s_c(sk.k);
558 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
560 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
563 if (ret2 == -ENOMEM) {
564 /* memory allocation failure, wait for some IO to finish */
565 bch2_move_ctxt_wait_for_io(ctxt);
569 /* XXX signal failure */
574 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
576 bch2_btree_iter_advance(&iter);
579 bch2_trans_iter_exit(trans, &iter);
580 bch2_bkey_buf_exit(&sk, c);
581 per_snapshot_io_opts_exit(&snapshot_io_opts);
586 int __bch2_move_data(struct moving_context *ctxt,
589 move_pred_fn pred, void *arg)
591 struct bch_fs *c = ctxt->trans->c;
595 for (id = start.btree;
596 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
598 ctxt->stats->pos = BBPOS(id, POS_MIN);
600 if (!btree_type_has_ptrs(id) ||
601 !bch2_btree_id_root(c, id)->b)
604 ret = bch2_move_data_btree(ctxt,
605 id == start.btree ? start.pos : POS_MIN,
606 id == end.btree ? end.pos : POS_MAX,
615 int bch2_move_data(struct bch_fs *c,
618 struct bch_ratelimit *rate,
619 struct bch_move_stats *stats,
620 struct write_point_specifier wp,
622 move_pred_fn pred, void *arg)
625 struct moving_context ctxt;
628 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
629 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
630 bch2_moving_ctxt_exit(&ctxt);
635 int __bch2_evacuate_bucket(struct moving_context *ctxt,
636 struct move_bucket_in_flight *bucket_in_flight,
637 struct bpos bucket, int gen,
638 struct data_update_opts _data_opts)
640 struct btree_trans *trans = ctxt->trans;
641 struct bch_fs *c = trans->c;
642 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
643 struct btree_iter iter;
645 struct bch_backpointer bp;
646 struct bch_alloc_v4 a_convert;
647 const struct bch_alloc_v4 *a;
649 struct data_update_opts data_opts;
650 unsigned dirty_sectors, bucket_size;
652 struct bpos bp_pos = POS_MIN;
655 trace_bucket_evacuate(c, &bucket);
657 bch2_bkey_buf_init(&sk);
660 * We're not run in a context that handles transaction restarts:
662 bch2_trans_begin(trans);
664 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
665 bucket, BTREE_ITER_CACHED);
666 ret = lockrestart_do(trans,
667 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
668 bch2_trans_iter_exit(trans, &iter);
671 bch_err_msg(c, ret, "looking up alloc key");
675 a = bch2_alloc_to_v4(k, &a_convert);
676 dirty_sectors = bch2_bucket_sectors_dirty(*a);
677 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
678 fragmentation = a->fragmentation_lru;
680 ret = bch2_btree_write_buffer_tryflush(trans);
681 bch_err_msg(c, ret, "flushing btree write buffer");
685 while (!(ret = bch2_move_ratelimit(ctxt))) {
686 bch2_trans_begin(trans);
688 ret = bch2_get_next_backpointer(trans, bucket, gen,
691 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
695 if (bkey_eq(bp_pos, POS_MAX))
699 const struct bch_extent_ptr *ptr;
702 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
704 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
711 bch2_bkey_buf_reassemble(&sk, c, k);
712 k = bkey_i_to_s_c(sk.k);
714 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
716 bch2_trans_iter_exit(trans, &iter);
720 data_opts = _data_opts;
721 data_opts.target = io_opts.background_target;
722 data_opts.rewrite_ptrs = 0;
724 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
725 if (ptr->dev == bucket.inode) {
726 data_opts.rewrite_ptrs |= 1U << i;
728 bch2_trans_iter_exit(trans, &iter);
735 ret = bch2_move_extent(ctxt, bucket_in_flight,
736 &iter, k, io_opts, data_opts);
737 bch2_trans_iter_exit(trans, &iter);
739 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
741 if (ret == -ENOMEM) {
742 /* memory allocation failure, wait for some IO to finish */
743 bch2_move_ctxt_wait_for_io(ctxt);
750 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
754 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
755 ret = PTR_ERR_OR_ZERO(b);
756 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
758 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
765 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
766 bch2_trans_iter_exit(trans, &iter);
768 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
774 bch2_ratelimit_increment(ctxt->rate,
775 c->opts.btree_node_size >> 9);
777 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
778 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
782 bp_pos = bpos_nosnap_successor(bp_pos);
785 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
787 bch2_bkey_buf_exit(&sk, c);
791 int bch2_evacuate_bucket(struct bch_fs *c,
792 struct bpos bucket, int gen,
793 struct data_update_opts data_opts,
794 struct bch_ratelimit *rate,
795 struct bch_move_stats *stats,
796 struct write_point_specifier wp,
799 struct moving_context ctxt;
802 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
803 ret = __bch2_evacuate_bucket(&ctxt, NULL, bucket, gen, data_opts);
804 bch2_moving_ctxt_exit(&ctxt);
809 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
810 struct btree *, struct bch_io_opts *,
811 struct data_update_opts *);
813 static int bch2_move_btree(struct bch_fs *c,
816 move_btree_pred pred, void *arg,
817 struct bch_move_stats *stats)
819 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
820 struct moving_context ctxt;
821 struct btree_trans *trans;
822 struct btree_iter iter;
825 struct data_update_opts data_opts;
828 bch2_moving_ctxt_init(&ctxt, c, NULL, stats,
829 writepoint_ptr(&c->btree_write_point),
833 stats->data_type = BCH_DATA_btree;
835 for (btree = start.btree;
836 btree <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
838 stats->pos = BBPOS(btree, POS_MIN);
840 if (!bch2_btree_id_root(c, btree)->b)
843 bch2_trans_node_iter_init(trans, &iter, btree, POS_MIN, 0, 0,
844 BTREE_ITER_PREFETCH);
847 while (bch2_trans_begin(trans),
848 (b = bch2_btree_iter_peek_node(&iter)) &&
849 !(ret = PTR_ERR_OR_ZERO(b))) {
850 if (kthread_should_stop())
853 if ((cmp_int(btree, end.btree) ?:
854 bpos_cmp(b->key.k.p, end.pos)) > 0)
857 stats->pos = BBPOS(iter.btree_id, iter.pos);
859 if (!pred(c, arg, b, &io_opts, &data_opts))
862 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
863 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
868 bch2_btree_iter_next_node(&iter);
870 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
873 bch2_trans_iter_exit(trans, &iter);
875 if (kthread_should_stop())
880 bch2_moving_ctxt_exit(&ctxt);
881 bch2_btree_interior_updates_flush(c);
886 static bool rereplicate_pred(struct bch_fs *c, void *arg,
888 struct bch_io_opts *io_opts,
889 struct data_update_opts *data_opts)
891 unsigned nr_good = bch2_bkey_durability(c, k);
892 unsigned replicas = bkey_is_btree_ptr(k.k)
893 ? c->opts.metadata_replicas
894 : io_opts->data_replicas;
896 if (!nr_good || nr_good >= replicas)
899 data_opts->target = 0;
900 data_opts->extra_replicas = replicas - nr_good;
901 data_opts->btree_insert_flags = 0;
905 static bool migrate_pred(struct bch_fs *c, void *arg,
907 struct bch_io_opts *io_opts,
908 struct data_update_opts *data_opts)
910 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
911 const struct bch_extent_ptr *ptr;
912 struct bch_ioctl_data *op = arg;
915 data_opts->rewrite_ptrs = 0;
916 data_opts->target = 0;
917 data_opts->extra_replicas = 0;
918 data_opts->btree_insert_flags = 0;
920 bkey_for_each_ptr(ptrs, ptr) {
921 if (ptr->dev == op->migrate.dev)
922 data_opts->rewrite_ptrs |= 1U << i;
926 return data_opts->rewrite_ptrs != 0;
929 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
931 struct bch_io_opts *io_opts,
932 struct data_update_opts *data_opts)
934 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
937 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
939 struct bch_io_opts *io_opts,
940 struct data_update_opts *data_opts)
942 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
945 static bool bformat_needs_redo(struct bkey_format *f)
949 for (i = 0; i < f->nr_fields; i++) {
950 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
951 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
952 u64 field_offset = le64_to_cpu(f->field_offset[i]);
954 if (f->bits_per_field[i] > unpacked_bits)
957 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
960 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
969 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
971 struct bch_io_opts *io_opts,
972 struct data_update_opts *data_opts)
974 if (b->version_ondisk != c->sb.version ||
975 btree_node_need_rewrite(b) ||
976 bformat_needs_redo(&b->format)) {
977 data_opts->target = 0;
978 data_opts->extra_replicas = 0;
979 data_opts->btree_insert_flags = 0;
986 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
990 ret = bch2_move_btree(c,
993 rewrite_old_nodes_pred, c, stats);
995 mutex_lock(&c->sb_lock);
996 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
997 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
998 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1000 mutex_unlock(&c->sb_lock);
1007 static bool drop_extra_replicas_pred(struct bch_fs *c, void *arg,
1009 struct bch_io_opts *io_opts,
1010 struct data_update_opts *data_opts)
1012 unsigned durability = bch2_bkey_durability(c, k);
1013 unsigned replicas = bkey_is_btree_ptr(k.k)
1014 ? c->opts.metadata_replicas
1015 : io_opts->data_replicas;
1016 const union bch_extent_entry *entry;
1017 struct extent_ptr_decoded p;
1020 bkey_for_each_ptr_decode(k.k, bch2_bkey_ptrs_c(k), p, entry) {
1021 unsigned d = bch2_extent_ptr_durability(c, &p);
1023 if (d && durability - d >= replicas) {
1024 data_opts->kill_ptrs |= BIT(i);
1031 return data_opts->kill_ptrs != 0;
1034 static bool drop_extra_replicas_btree_pred(struct bch_fs *c, void *arg,
1036 struct bch_io_opts *io_opts,
1037 struct data_update_opts *data_opts)
1039 return drop_extra_replicas_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1042 int bch2_data_job(struct bch_fs *c,
1043 struct bch_move_stats *stats,
1044 struct bch_ioctl_data op)
1046 struct bbpos start = BBPOS(op.start_btree, op.start_pos);
1047 struct bbpos end = BBPOS(op.end_btree, op.end_pos);
1050 if (op.op >= BCH_DATA_OP_NR)
1053 bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
1056 case BCH_DATA_OP_rereplicate:
1057 stats->data_type = BCH_DATA_journal;
1058 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1059 ret = bch2_move_btree(c, start, end,
1060 rereplicate_btree_pred, c, stats) ?: ret;
1061 ret = bch2_move_data(c, start, end,
1064 writepoint_hashed((unsigned long) current),
1066 rereplicate_pred, c) ?: ret;
1067 ret = bch2_replicas_gc2(c) ?: ret;
1069 case BCH_DATA_OP_migrate:
1070 if (op.migrate.dev >= c->sb.nr_devices)
1073 stats->data_type = BCH_DATA_journal;
1074 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1075 ret = bch2_move_btree(c, start, end,
1076 migrate_btree_pred, &op, stats) ?: ret;
1077 ret = bch2_move_data(c, start, end,
1080 writepoint_hashed((unsigned long) current),
1082 migrate_pred, &op) ?: ret;
1083 ret = bch2_replicas_gc2(c) ?: ret;
1085 case BCH_DATA_OP_rewrite_old_nodes:
1086 ret = bch2_scan_old_btree_nodes(c, stats);
1088 case BCH_DATA_OP_drop_extra_replicas:
1089 ret = bch2_move_btree(c, start, end,
1090 drop_extra_replicas_btree_pred, c, stats) ?: ret;
1091 ret = bch2_move_data(c, start, end, NULL, stats,
1092 writepoint_hashed((unsigned long) current),
1094 drop_extra_replicas_pred, c) ?: ret;
1095 ret = bch2_replicas_gc2(c) ?: ret;
1101 bch2_move_stats_exit(stats, c);
1105 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1107 prt_printf(out, "%s: data type=%s pos=",
1109 bch2_data_types[stats->data_type]);
1110 bch2_bbpos_to_text(out, stats->pos);
1112 printbuf_indent_add(out, 2);
1114 prt_str(out, "keys moved: ");
1115 prt_u64(out, atomic64_read(&stats->keys_moved));
1118 prt_str(out, "keys raced: ");
1119 prt_u64(out, atomic64_read(&stats->keys_raced));
1122 prt_str(out, "bytes seen: ");
1123 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1126 prt_str(out, "bytes moved: ");
1127 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1130 prt_str(out, "bytes raced: ");
1131 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1134 printbuf_indent_sub(out, 2);
1137 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1139 struct moving_io *io;
1141 bch2_move_stats_to_text(out, ctxt->stats);
1142 printbuf_indent_add(out, 2);
1144 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1145 atomic_read(&ctxt->read_ios),
1146 c->opts.move_ios_in_flight,
1147 atomic_read(&ctxt->read_sectors),
1148 c->opts.move_bytes_in_flight >> 9);
1151 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1152 atomic_read(&ctxt->write_ios),
1153 c->opts.move_ios_in_flight,
1154 atomic_read(&ctxt->write_sectors),
1155 c->opts.move_bytes_in_flight >> 9);
1158 printbuf_indent_add(out, 2);
1160 mutex_lock(&ctxt->lock);
1161 list_for_each_entry(io, &ctxt->ios, io_list)
1162 bch2_write_op_to_text(out, &io->write.op);
1163 mutex_unlock(&ctxt->lock);
1165 printbuf_indent_sub(out, 4);
1168 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1170 struct moving_context *ctxt;
1172 mutex_lock(&c->moving_context_lock);
1173 list_for_each_entry(ctxt, &c->moving_context_list, list)
1174 bch2_moving_ctxt_to_text(out, c, ctxt);
1175 mutex_unlock(&c->moving_context_lock);
1178 void bch2_fs_move_init(struct bch_fs *c)
1180 INIT_LIST_HEAD(&c->moving_context_list);
1181 mutex_init(&c->moving_context_lock);