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 trace_move_extent2(struct bch_fs *c, struct bkey_s_c k,
40 struct bch_io_opts *io_opts,
41 struct data_update_opts *data_opts)
43 if (trace_move_extent_enabled()) {
44 struct printbuf buf = PRINTBUF;
46 bch2_bkey_val_to_text(&buf, c, k);
48 bch2_data_update_opts_to_text(&buf, c, io_opts, data_opts);
49 trace_move_extent(c, buf.buf);
54 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
56 if (trace_move_extent_read_enabled()) {
57 struct printbuf buf = PRINTBUF;
59 bch2_bkey_val_to_text(&buf, c, k);
60 trace_move_extent_read(c, buf.buf);
66 struct list_head read_list;
67 struct list_head io_list;
68 struct move_bucket_in_flight *b;
72 unsigned read_sectors;
73 unsigned write_sectors;
75 struct bch_read_bio rbio;
77 struct data_update write;
78 /* Must be last since it is variable size */
79 struct bio_vec bi_inline_vecs[];
82 static void move_free(struct moving_io *io)
84 struct moving_context *ctxt = io->write.ctxt;
87 atomic_dec(&io->b->count);
89 bch2_data_update_exit(&io->write);
91 mutex_lock(&ctxt->lock);
92 list_del(&io->io_list);
94 mutex_unlock(&ctxt->lock);
99 static void move_write_done(struct bch_write_op *op)
101 struct moving_io *io = container_of(op, struct moving_io, write.op);
102 struct moving_context *ctxt = io->write.ctxt;
104 if (io->write.op.error)
105 ctxt->write_error = true;
107 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
108 atomic_dec(&io->write.ctxt->write_ios);
110 closure_put(&ctxt->cl);
113 static void move_write(struct moving_io *io)
115 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
120 if (trace_move_extent_write_enabled()) {
121 struct bch_fs *c = io->write.op.c;
122 struct printbuf buf = PRINTBUF;
124 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(io->write.k.k));
125 trace_move_extent_write(c, buf.buf);
129 closure_get(&io->write.ctxt->cl);
130 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
131 atomic_inc(&io->write.ctxt->write_ios);
133 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
136 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
138 struct moving_io *io =
139 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
141 return io && io->read_completed ? io : NULL;
144 static void move_read_endio(struct bio *bio)
146 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
147 struct moving_context *ctxt = io->write.ctxt;
149 atomic_sub(io->read_sectors, &ctxt->read_sectors);
150 atomic_dec(&ctxt->read_ios);
151 io->read_completed = true;
153 wake_up(&ctxt->wait);
154 closure_put(&ctxt->cl);
157 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt)
159 struct moving_io *io;
161 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
162 bch2_trans_unlock_long(ctxt->trans);
163 list_del(&io->read_list);
168 void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
170 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
172 move_ctxt_wait_event(ctxt,
173 !atomic_read(&ctxt->write_sectors) ||
174 atomic_read(&ctxt->write_sectors) != sectors_pending);
177 void bch2_moving_ctxt_flush_all(struct moving_context *ctxt)
179 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
180 bch2_trans_unlock_long(ctxt->trans);
181 closure_sync(&ctxt->cl);
184 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
186 struct bch_fs *c = ctxt->trans->c;
188 bch2_moving_ctxt_flush_all(ctxt);
190 EBUG_ON(atomic_read(&ctxt->write_sectors));
191 EBUG_ON(atomic_read(&ctxt->write_ios));
192 EBUG_ON(atomic_read(&ctxt->read_sectors));
193 EBUG_ON(atomic_read(&ctxt->read_ios));
195 mutex_lock(&c->moving_context_lock);
196 list_del(&ctxt->list);
197 mutex_unlock(&c->moving_context_lock);
199 bch2_trans_put(ctxt->trans);
200 memset(ctxt, 0, sizeof(*ctxt));
203 void bch2_moving_ctxt_init(struct moving_context *ctxt,
205 struct bch_ratelimit *rate,
206 struct bch_move_stats *stats,
207 struct write_point_specifier wp,
210 memset(ctxt, 0, sizeof(*ctxt));
212 ctxt->trans = bch2_trans_get(c);
213 ctxt->fn = (void *) _RET_IP_;
217 ctxt->wait_on_copygc = wait_on_copygc;
219 closure_init_stack(&ctxt->cl);
221 mutex_init(&ctxt->lock);
222 INIT_LIST_HEAD(&ctxt->reads);
223 INIT_LIST_HEAD(&ctxt->ios);
224 init_waitqueue_head(&ctxt->wait);
226 mutex_lock(&c->moving_context_lock);
227 list_add(&ctxt->list, &c->moving_context_list);
228 mutex_unlock(&c->moving_context_lock);
231 void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
233 trace_move_data(c, stats);
236 void bch2_move_stats_init(struct bch_move_stats *stats, const char *name)
238 memset(stats, 0, sizeof(*stats));
239 stats->data_type = BCH_DATA_user;
240 scnprintf(stats->name, sizeof(stats->name), "%s", name);
243 int bch2_move_extent(struct moving_context *ctxt,
244 struct move_bucket_in_flight *bucket_in_flight,
245 struct btree_iter *iter,
247 struct bch_io_opts io_opts,
248 struct data_update_opts data_opts)
250 struct btree_trans *trans = ctxt->trans;
251 struct bch_fs *c = trans->c;
252 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
253 struct moving_io *io;
254 const union bch_extent_entry *entry;
255 struct extent_ptr_decoded p;
256 unsigned sectors = k.k->size, pages;
259 trace_move_extent2(c, k, &io_opts, &data_opts);
262 ctxt->stats->pos = BBPOS(iter->btree_id, iter->pos);
264 bch2_data_update_opts_normalize(k, &data_opts);
266 if (!data_opts.rewrite_ptrs &&
267 !data_opts.extra_replicas) {
268 if (data_opts.kill_ptrs)
269 return bch2_extent_drop_ptrs(trans, iter, k, &io_opts, &data_opts);
274 * Before memory allocations & taking nocow locks in
275 * bch2_data_update_init():
277 bch2_trans_unlock(trans);
279 /* write path might have to decompress data: */
280 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
281 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
283 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
284 io = kzalloc(sizeof(struct moving_io) +
285 sizeof(struct bio_vec) * pages, GFP_KERNEL);
289 INIT_LIST_HEAD(&io->io_list);
290 io->write.ctxt = ctxt;
291 io->read_sectors = k.k->size;
292 io->write_sectors = k.k->size;
294 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
295 bio_set_prio(&io->write.op.wbio.bio,
296 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
298 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
303 io->rbio.opts = io_opts;
304 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
305 io->rbio.bio.bi_vcnt = pages;
306 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
307 io->rbio.bio.bi_iter.bi_size = sectors << 9;
309 io->rbio.bio.bi_opf = REQ_OP_READ;
310 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
311 io->rbio.bio.bi_end_io = move_read_endio;
313 ret = bch2_data_update_init(trans, iter, ctxt, &io->write, ctxt->wp,
314 io_opts, data_opts, iter->btree_id, k);
318 io->write.op.end_io = move_write_done;
321 bch2_ratelimit_increment(ctxt->rate, k.k->size);
324 atomic64_inc(&ctxt->stats->keys_moved);
325 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
328 if (bucket_in_flight) {
329 io->b = bucket_in_flight;
330 atomic_inc(&io->b->count);
333 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
334 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
335 trace_move_extent_read2(c, k);
337 mutex_lock(&ctxt->lock);
338 atomic_add(io->read_sectors, &ctxt->read_sectors);
339 atomic_inc(&ctxt->read_ios);
341 list_add_tail(&io->read_list, &ctxt->reads);
342 list_add_tail(&io->io_list, &ctxt->ios);
343 mutex_unlock(&ctxt->lock);
346 * dropped by move_read_endio() - guards against use after free of
347 * ctxt when doing wakeup
349 closure_get(&ctxt->cl);
350 bch2_read_extent(trans, &io->rbio,
352 iter->btree_id, k, 0,
354 BCH_READ_LAST_FRAGMENT);
357 bio_free_pages(&io->write.op.wbio.bio);
361 if (ret == -BCH_ERR_data_update_done)
364 if (bch2_err_matches(ret, EROFS) ||
365 bch2_err_matches(ret, BCH_ERR_transaction_restart))
368 count_event(c, move_extent_start_fail);
370 if (trace_move_extent_start_fail_enabled()) {
371 struct printbuf buf = PRINTBUF;
373 bch2_bkey_val_to_text(&buf, c, k);
375 prt_str(&buf, bch2_err_str(ret));
376 trace_move_extent_start_fail(c, buf.buf);
382 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
383 struct per_snapshot_io_opts *io_opts,
384 struct bkey_s_c extent_k)
386 struct bch_fs *c = trans->c;
387 u32 restart_count = trans->restart_count;
390 if (io_opts->cur_inum != extent_k.k->p.inode) {
393 ret = for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
394 BTREE_ITER_all_snapshots, k, ({
395 if (k.k->p.offset != extent_k.k->p.inode)
398 if (!bkey_is_inode(k.k))
401 struct bch_inode_unpacked inode;
402 BUG_ON(bch2_inode_unpack(k, &inode));
404 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
405 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
407 darray_push(&io_opts->d, e);
409 io_opts->cur_inum = extent_k.k->p.inode;
412 ret = ret ?: trans_was_restarted(trans, restart_count);
416 if (extent_k.k->p.snapshot)
417 darray_for_each(io_opts->d, i)
418 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
421 return &io_opts->fs_io_opts;
424 int bch2_move_get_io_opts_one(struct btree_trans *trans,
425 struct bch_io_opts *io_opts,
426 struct bkey_s_c extent_k)
428 struct btree_iter iter;
433 if (!extent_k.k->p.inode) {
434 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
438 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
439 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
442 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
445 if (!ret && bkey_is_inode(k.k)) {
446 struct bch_inode_unpacked inode;
447 bch2_inode_unpack(k, &inode);
448 bch2_inode_opts_get(io_opts, trans->c, &inode);
450 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
453 bch2_trans_iter_exit(trans, &iter);
457 int bch2_move_ratelimit(struct moving_context *ctxt)
459 struct bch_fs *c = ctxt->trans->c;
460 bool is_kthread = current->flags & PF_KTHREAD;
463 if (ctxt->wait_on_copygc && c->copygc_running) {
464 bch2_moving_ctxt_flush_all(ctxt);
465 wait_event_killable(c->copygc_running_wq,
466 !c->copygc_running ||
467 (is_kthread && kthread_should_stop()));
471 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
473 if (is_kthread && kthread_should_stop())
477 move_ctxt_wait_event_timeout(ctxt,
479 (is_kthread && kthread_should_stop()),
482 if (unlikely(freezing(current))) {
483 bch2_moving_ctxt_flush_all(ctxt);
489 * XXX: these limits really ought to be per device, SSDs and hard drives
490 * will want different limits
492 move_ctxt_wait_event(ctxt,
493 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
494 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
495 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
496 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
501 static int bch2_move_data_btree(struct moving_context *ctxt,
504 move_pred_fn pred, void *arg,
505 enum btree_id btree_id)
507 struct btree_trans *trans = ctxt->trans;
508 struct bch_fs *c = trans->c;
509 struct per_snapshot_io_opts snapshot_io_opts;
510 struct bch_io_opts *io_opts;
512 struct btree_iter iter;
514 struct data_update_opts data_opts;
517 per_snapshot_io_opts_init(&snapshot_io_opts, c);
518 bch2_bkey_buf_init(&sk);
521 ctxt->stats->data_type = BCH_DATA_user;
522 ctxt->stats->pos = BBPOS(btree_id, start);
525 bch2_trans_begin(trans);
526 bch2_trans_iter_init(trans, &iter, btree_id, start,
528 BTREE_ITER_all_snapshots);
531 bch2_ratelimit_reset(ctxt->rate);
533 while (!bch2_move_ratelimit(ctxt)) {
534 bch2_trans_begin(trans);
536 k = bch2_btree_iter_peek(&iter);
541 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
546 if (bkey_ge(bkey_start_pos(k.k), end))
550 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
552 if (!bkey_extent_is_direct_data(k.k))
555 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
556 ret = PTR_ERR_OR_ZERO(io_opts);
560 memset(&data_opts, 0, sizeof(data_opts));
561 if (!pred(c, arg, k, io_opts, &data_opts))
565 * The iterator gets unlocked by __bch2_read_extent - need to
566 * save a copy of @k elsewhere:
568 bch2_bkey_buf_reassemble(&sk, c, k);
569 k = bkey_i_to_s_c(sk.k);
571 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
573 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
576 if (ret2 == -ENOMEM) {
577 /* memory allocation failure, wait for some IO to finish */
578 bch2_move_ctxt_wait_for_io(ctxt);
582 /* XXX signal failure */
587 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
589 bch2_btree_iter_advance(&iter);
592 bch2_trans_iter_exit(trans, &iter);
593 bch2_bkey_buf_exit(&sk, c);
594 per_snapshot_io_opts_exit(&snapshot_io_opts);
599 int __bch2_move_data(struct moving_context *ctxt,
602 move_pred_fn pred, void *arg)
604 struct bch_fs *c = ctxt->trans->c;
608 for (id = start.btree;
609 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
611 ctxt->stats->pos = BBPOS(id, POS_MIN);
613 if (!btree_type_has_ptrs(id) ||
614 !bch2_btree_id_root(c, id)->b)
617 ret = bch2_move_data_btree(ctxt,
618 id == start.btree ? start.pos : POS_MIN,
619 id == end.btree ? end.pos : POS_MAX,
628 int bch2_move_data(struct bch_fs *c,
631 struct bch_ratelimit *rate,
632 struct bch_move_stats *stats,
633 struct write_point_specifier wp,
635 move_pred_fn pred, void *arg)
638 struct moving_context ctxt;
641 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
642 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
643 bch2_moving_ctxt_exit(&ctxt);
648 int bch2_evacuate_bucket(struct moving_context *ctxt,
649 struct move_bucket_in_flight *bucket_in_flight,
650 struct bpos bucket, int gen,
651 struct data_update_opts _data_opts)
653 struct btree_trans *trans = ctxt->trans;
654 struct bch_fs *c = trans->c;
655 bool is_kthread = current->flags & PF_KTHREAD;
656 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
657 struct btree_iter iter;
659 struct bch_backpointer bp;
660 struct bch_alloc_v4 a_convert;
661 const struct bch_alloc_v4 *a;
663 struct data_update_opts data_opts;
664 unsigned dirty_sectors, bucket_size;
666 struct bpos bp_pos = POS_MIN;
669 struct bch_dev *ca = bch2_dev_tryget(c, bucket.inode);
673 trace_bucket_evacuate(c, &bucket);
675 bch2_bkey_buf_init(&sk);
678 * We're not run in a context that handles transaction restarts:
680 bch2_trans_begin(trans);
682 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
683 bucket, BTREE_ITER_cached);
684 ret = lockrestart_do(trans,
685 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
686 bch2_trans_iter_exit(trans, &iter);
688 bch_err_msg(c, ret, "looking up alloc key");
692 a = bch2_alloc_to_v4(k, &a_convert);
693 dirty_sectors = bch2_bucket_sectors_dirty(*a);
694 bucket_size = ca->mi.bucket_size;
695 fragmentation = alloc_lru_idx_fragmentation(*a, ca);
697 ret = bch2_btree_write_buffer_tryflush(trans);
698 bch_err_msg(c, ret, "flushing btree write buffer");
702 while (!(ret = bch2_move_ratelimit(ctxt))) {
703 if (is_kthread && kthread_should_stop())
706 bch2_trans_begin(trans);
708 ret = bch2_get_next_backpointer(trans, ca, bucket, gen,
711 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
715 if (bkey_eq(bp_pos, POS_MAX))
719 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
721 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
728 bch2_bkey_buf_reassemble(&sk, c, k);
729 k = bkey_i_to_s_c(sk.k);
731 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
733 bch2_trans_iter_exit(trans, &iter);
737 data_opts = _data_opts;
738 data_opts.target = io_opts.background_target;
739 data_opts.rewrite_ptrs = 0;
742 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
743 if (ptr->dev == bucket.inode) {
744 data_opts.rewrite_ptrs |= 1U << i;
746 bch2_trans_iter_exit(trans, &iter);
753 ret = bch2_move_extent(ctxt, bucket_in_flight,
754 &iter, k, io_opts, data_opts);
755 bch2_trans_iter_exit(trans, &iter);
757 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
759 if (ret == -ENOMEM) {
760 /* memory allocation failure, wait for some IO to finish */
761 bch2_move_ctxt_wait_for_io(ctxt);
768 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
772 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
773 ret = PTR_ERR_OR_ZERO(b);
774 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
776 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
783 unsigned sectors = btree_ptr_sectors_written(bkey_i_to_s_c(&b->key));
785 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
786 bch2_trans_iter_exit(trans, &iter);
788 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
794 bch2_ratelimit_increment(ctxt->rate, sectors);
796 atomic64_add(sectors, &ctxt->stats->sectors_seen);
797 atomic64_add(sectors, &ctxt->stats->sectors_moved);
801 bp_pos = bpos_nosnap_successor(bp_pos);
804 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
807 bch2_bkey_buf_exit(&sk, c);
811 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
812 struct btree *, struct bch_io_opts *,
813 struct data_update_opts *);
815 static int bch2_move_btree(struct bch_fs *c,
818 move_btree_pred pred, void *arg,
819 struct bch_move_stats *stats)
821 bool kthread = (current->flags & PF_KTHREAD) != 0;
822 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
823 struct moving_context ctxt;
824 struct btree_trans *trans;
825 struct btree_iter iter;
828 struct data_update_opts data_opts;
831 bch2_moving_ctxt_init(&ctxt, c, NULL, stats,
832 writepoint_ptr(&c->btree_write_point),
836 stats->data_type = BCH_DATA_btree;
838 for (btree = start.btree;
839 btree <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
841 stats->pos = BBPOS(btree, POS_MIN);
843 if (!bch2_btree_id_root(c, btree)->b)
846 bch2_trans_node_iter_init(trans, &iter, btree, POS_MIN, 0, 0,
847 BTREE_ITER_prefetch);
850 while (bch2_trans_begin(trans),
851 (b = bch2_btree_iter_peek_node(&iter)) &&
852 !(ret = PTR_ERR_OR_ZERO(b))) {
853 if (kthread && kthread_should_stop())
856 if ((cmp_int(btree, end.btree) ?:
857 bpos_cmp(b->key.k.p, end.pos)) > 0)
860 stats->pos = BBPOS(iter.btree_id, iter.pos);
862 if (!pred(c, arg, b, &io_opts, &data_opts))
865 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
866 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
871 bch2_btree_iter_next_node(&iter);
873 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
876 bch2_trans_iter_exit(trans, &iter);
878 if (kthread && kthread_should_stop())
883 bch2_moving_ctxt_exit(&ctxt);
884 bch2_btree_interior_updates_flush(c);
889 static bool rereplicate_pred(struct bch_fs *c, void *arg,
891 struct bch_io_opts *io_opts,
892 struct data_update_opts *data_opts)
894 unsigned nr_good = bch2_bkey_durability(c, k);
895 unsigned replicas = bkey_is_btree_ptr(k.k)
896 ? c->opts.metadata_replicas
897 : io_opts->data_replicas;
900 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
902 bkey_for_each_ptr(ptrs, ptr) {
903 struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev);
905 (!ca || !ca->mi.durability))
906 data_opts->kill_ptrs |= BIT(i);
911 if (!data_opts->kill_ptrs &&
912 (!nr_good || nr_good >= replicas))
915 data_opts->target = 0;
916 data_opts->extra_replicas = replicas - nr_good;
917 data_opts->btree_insert_flags = 0;
921 static bool migrate_pred(struct bch_fs *c, void *arg,
923 struct bch_io_opts *io_opts,
924 struct data_update_opts *data_opts)
926 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
927 struct bch_ioctl_data *op = arg;
930 data_opts->rewrite_ptrs = 0;
931 data_opts->target = 0;
932 data_opts->extra_replicas = 0;
933 data_opts->btree_insert_flags = 0;
935 bkey_for_each_ptr(ptrs, ptr) {
936 if (ptr->dev == op->migrate.dev)
937 data_opts->rewrite_ptrs |= 1U << i;
941 return data_opts->rewrite_ptrs != 0;
944 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
946 struct bch_io_opts *io_opts,
947 struct data_update_opts *data_opts)
949 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
952 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
954 struct bch_io_opts *io_opts,
955 struct data_update_opts *data_opts)
957 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
961 * Ancient versions of bcachefs produced packed formats which could represent
962 * keys that the in memory format cannot represent; this checks for those
963 * formats so we can get rid of them.
965 static bool bformat_needs_redo(struct bkey_format *f)
967 for (unsigned i = 0; i < f->nr_fields; i++)
968 if (bch2_bkey_format_field_overflows(f, i))
974 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
976 struct bch_io_opts *io_opts,
977 struct data_update_opts *data_opts)
979 if (b->version_ondisk != c->sb.version ||
980 btree_node_need_rewrite(b) ||
981 bformat_needs_redo(&b->format)) {
982 data_opts->target = 0;
983 data_opts->extra_replicas = 0;
984 data_opts->btree_insert_flags = 0;
991 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
995 ret = bch2_move_btree(c,
998 rewrite_old_nodes_pred, c, stats);
1000 mutex_lock(&c->sb_lock);
1001 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1002 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1003 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1004 bch2_write_super(c);
1005 mutex_unlock(&c->sb_lock);
1012 static bool drop_extra_replicas_pred(struct bch_fs *c, void *arg,
1014 struct bch_io_opts *io_opts,
1015 struct data_update_opts *data_opts)
1017 unsigned durability = bch2_bkey_durability(c, k);
1018 unsigned replicas = bkey_is_btree_ptr(k.k)
1019 ? c->opts.metadata_replicas
1020 : io_opts->data_replicas;
1021 const union bch_extent_entry *entry;
1022 struct extent_ptr_decoded p;
1026 bkey_for_each_ptr_decode(k.k, bch2_bkey_ptrs_c(k), p, entry) {
1027 unsigned d = bch2_extent_ptr_durability(c, &p);
1029 if (d && durability - d >= replicas) {
1030 data_opts->kill_ptrs |= BIT(i);
1038 return data_opts->kill_ptrs != 0;
1041 static bool drop_extra_replicas_btree_pred(struct bch_fs *c, void *arg,
1043 struct bch_io_opts *io_opts,
1044 struct data_update_opts *data_opts)
1046 return drop_extra_replicas_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1049 int bch2_data_job(struct bch_fs *c,
1050 struct bch_move_stats *stats,
1051 struct bch_ioctl_data op)
1053 struct bbpos start = BBPOS(op.start_btree, op.start_pos);
1054 struct bbpos end = BBPOS(op.end_btree, op.end_pos);
1057 if (op.op >= BCH_DATA_OP_NR)
1060 bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
1063 case BCH_DATA_OP_rereplicate:
1064 stats->data_type = BCH_DATA_journal;
1065 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1066 ret = bch2_move_btree(c, start, end,
1067 rereplicate_btree_pred, c, stats) ?: ret;
1068 ret = bch2_move_data(c, start, end,
1071 writepoint_hashed((unsigned long) current),
1073 rereplicate_pred, c) ?: ret;
1074 ret = bch2_replicas_gc2(c) ?: ret;
1076 case BCH_DATA_OP_migrate:
1077 if (op.migrate.dev >= c->sb.nr_devices)
1080 stats->data_type = BCH_DATA_journal;
1081 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1082 ret = bch2_move_btree(c, start, end,
1083 migrate_btree_pred, &op, stats) ?: ret;
1084 ret = bch2_move_data(c, start, end,
1087 writepoint_hashed((unsigned long) current),
1089 migrate_pred, &op) ?: ret;
1090 ret = bch2_replicas_gc2(c) ?: ret;
1092 case BCH_DATA_OP_rewrite_old_nodes:
1093 ret = bch2_scan_old_btree_nodes(c, stats);
1095 case BCH_DATA_OP_drop_extra_replicas:
1096 ret = bch2_move_btree(c, start, end,
1097 drop_extra_replicas_btree_pred, c, stats) ?: ret;
1098 ret = bch2_move_data(c, start, end, NULL, stats,
1099 writepoint_hashed((unsigned long) current),
1101 drop_extra_replicas_pred, c) ?: ret;
1102 ret = bch2_replicas_gc2(c) ?: ret;
1108 bch2_move_stats_exit(stats, c);
1112 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1114 prt_printf(out, "%s: data type==", stats->name);
1115 bch2_prt_data_type(out, stats->data_type);
1116 prt_str(out, " pos=");
1117 bch2_bbpos_to_text(out, stats->pos);
1119 printbuf_indent_add(out, 2);
1121 prt_printf(out, "keys moved: %llu\n", atomic64_read(&stats->keys_moved));
1122 prt_printf(out, "keys raced: %llu\n", atomic64_read(&stats->keys_raced));
1123 prt_printf(out, "bytes seen: ");
1124 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1127 prt_printf(out, "bytes moved: ");
1128 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1131 prt_printf(out, "bytes raced: ");
1132 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1135 printbuf_indent_sub(out, 2);
1138 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1140 struct moving_io *io;
1142 bch2_move_stats_to_text(out, ctxt->stats);
1143 printbuf_indent_add(out, 2);
1145 prt_printf(out, "reads: ios %u/%u sectors %u/%u\n",
1146 atomic_read(&ctxt->read_ios),
1147 c->opts.move_ios_in_flight,
1148 atomic_read(&ctxt->read_sectors),
1149 c->opts.move_bytes_in_flight >> 9);
1151 prt_printf(out, "writes: ios %u/%u sectors %u/%u\n",
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);
1157 printbuf_indent_add(out, 2);
1159 mutex_lock(&ctxt->lock);
1160 list_for_each_entry(io, &ctxt->ios, io_list)
1161 bch2_write_op_to_text(out, &io->write.op);
1162 mutex_unlock(&ctxt->lock);
1164 printbuf_indent_sub(out, 4);
1167 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1169 struct moving_context *ctxt;
1171 mutex_lock(&c->moving_context_lock);
1172 list_for_each_entry(ctxt, &c->moving_context_list, list)
1173 bch2_moving_ctxt_to_text(out, c, ctxt);
1174 mutex_unlock(&c->moving_context_lock);
1177 void bch2_fs_move_init(struct bch_fs *c)
1179 INIT_LIST_HEAD(&c->moving_context_list);
1180 mutex_init(&c->moving_context_lock);