1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
5 #include "backpointers.h"
8 #include "btree_update.h"
9 #include "btree_update_interior.h"
10 #include "disk_groups.h"
16 #include "journal_reclaim.h"
22 #include <linux/ioprio.h>
23 #include <linux/kthread.h>
25 #include <trace/events/bcachefs.h>
27 static void progress_list_add(struct bch_fs *c, struct bch_move_stats *stats)
29 mutex_lock(&c->data_progress_lock);
30 list_add(&stats->list, &c->data_progress_list);
31 mutex_unlock(&c->data_progress_lock);
34 static void progress_list_del(struct bch_fs *c, struct bch_move_stats *stats)
36 mutex_lock(&c->data_progress_lock);
37 list_del(&stats->list);
38 mutex_unlock(&c->data_progress_lock);
42 struct list_head list;
46 unsigned read_sectors;
47 unsigned write_sectors;
49 struct bch_read_bio rbio;
51 struct data_update write;
52 /* Must be last since it is variable size */
53 struct bio_vec bi_inline_vecs[0];
56 static void move_free(struct moving_io *io)
58 struct moving_context *ctxt = io->write.ctxt;
59 struct bch_fs *c = ctxt->c;
61 bch2_data_update_exit(&io->write);
63 percpu_ref_put(&c->writes);
67 static void move_write_done(struct bch_write_op *op)
69 struct moving_io *io = container_of(op, struct moving_io, write.op);
70 struct moving_context *ctxt = io->write.ctxt;
72 if (io->write.op.error)
73 ctxt->write_error = true;
75 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
77 closure_put(&ctxt->cl);
80 static void move_write(struct moving_io *io)
82 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
87 closure_get(&io->write.ctxt->cl);
88 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
90 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
93 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
95 struct moving_io *io =
96 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
98 return io && io->read_completed ? io : NULL;
101 static void move_read_endio(struct bio *bio)
103 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
104 struct moving_context *ctxt = io->write.ctxt;
106 atomic_sub(io->read_sectors, &ctxt->read_sectors);
107 io->read_completed = true;
109 wake_up(&ctxt->wait);
110 closure_put(&ctxt->cl);
113 static void do_pending_writes(struct moving_context *ctxt, struct btree_trans *trans)
115 struct moving_io *io;
118 bch2_trans_unlock(trans);
120 while ((io = next_pending_write(ctxt))) {
126 #define move_ctxt_wait_event(_ctxt, _trans, _cond) \
128 do_pending_writes(_ctxt, _trans); \
132 __wait_event((_ctxt)->wait, \
133 next_pending_write(_ctxt) || (_cond)); \
136 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
137 struct btree_trans *trans)
139 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
141 move_ctxt_wait_event(ctxt, trans,
142 !atomic_read(&ctxt->write_sectors) ||
143 atomic_read(&ctxt->write_sectors) != sectors_pending);
146 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
148 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
149 closure_sync(&ctxt->cl);
150 EBUG_ON(atomic_read(&ctxt->write_sectors));
153 progress_list_del(ctxt->c, ctxt->stats);
155 trace_move_data(ctxt->c,
156 atomic64_read(&ctxt->stats->sectors_moved),
157 atomic64_read(&ctxt->stats->keys_moved));
161 void bch2_moving_ctxt_init(struct moving_context *ctxt,
163 struct bch_ratelimit *rate,
164 struct bch_move_stats *stats,
165 struct write_point_specifier wp,
168 memset(ctxt, 0, sizeof(*ctxt));
174 ctxt->wait_on_copygc = wait_on_copygc;
176 closure_init_stack(&ctxt->cl);
177 INIT_LIST_HEAD(&ctxt->reads);
178 init_waitqueue_head(&ctxt->wait);
181 progress_list_add(c, stats);
182 stats->data_type = BCH_DATA_user;
186 void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
188 memset(stats, 0, sizeof(*stats));
189 scnprintf(stats->name, sizeof(stats->name), "%s", name);
192 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
193 struct btree_iter *iter,
195 struct data_update_opts data_opts)
197 struct bch_fs *c = trans->c;
201 n = bch2_bkey_make_mut(trans, k);
202 ret = PTR_ERR_OR_ZERO(n);
206 while (data_opts.kill_ptrs) {
207 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
208 struct bch_extent_ptr *ptr;
210 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
211 data_opts.kill_ptrs ^= 1U << drop;
215 * If the new extent no longer has any pointers, bch2_extent_normalize()
216 * will do the appropriate thing with it (turning it into a
217 * KEY_TYPE_error key, or just a discard if it was a cached extent)
219 bch2_extent_normalize(c, bkey_i_to_s(n));
222 * Since we're not inserting through an extent iterator
223 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
224 * we aren't using the extent overwrite path to delete, we're
225 * just using the normal key deletion path:
227 if (bkey_deleted(&n->k))
230 return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
231 bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
234 static int bch2_move_extent(struct btree_trans *trans,
235 struct btree_iter *iter,
236 struct moving_context *ctxt,
237 struct bch_io_opts io_opts,
238 enum btree_id btree_id,
240 struct data_update_opts data_opts)
242 struct bch_fs *c = trans->c;
243 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
244 struct moving_io *io;
245 const union bch_extent_entry *entry;
246 struct extent_ptr_decoded p;
247 unsigned sectors = k.k->size, pages;
250 bch2_data_update_opts_normalize(k, &data_opts);
252 if (!data_opts.rewrite_ptrs &&
253 !data_opts.extra_replicas) {
254 if (data_opts.kill_ptrs)
255 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
259 if (!percpu_ref_tryget_live(&c->writes))
263 * Before memory allocations & taking nocow locks in
264 * bch2_data_update_init():
266 bch2_trans_unlock(trans);
268 /* write path might have to decompress data: */
269 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
270 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
272 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
273 io = kzalloc(sizeof(struct moving_io) +
274 sizeof(struct bio_vec) * pages, GFP_KERNEL);
278 io->write.ctxt = ctxt;
279 io->read_sectors = k.k->size;
280 io->write_sectors = k.k->size;
282 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
283 bio_set_prio(&io->write.op.wbio.bio,
284 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
286 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
291 io->rbio.opts = io_opts;
292 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
293 io->rbio.bio.bi_vcnt = pages;
294 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
295 io->rbio.bio.bi_iter.bi_size = sectors << 9;
297 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
298 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
299 io->rbio.bio.bi_end_io = move_read_endio;
301 ret = bch2_data_update_init(c, &io->write, ctxt->wp, io_opts,
302 data_opts, btree_id, k);
303 if (ret && ret != -BCH_ERR_unwritten_extent_update)
306 io->write.ctxt = ctxt;
307 io->write.op.end_io = move_write_done;
309 atomic64_inc(&ctxt->stats->keys_moved);
310 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
312 if (ret == -BCH_ERR_unwritten_extent_update) {
313 bch2_update_unwritten_extent(trans, &io->write);
320 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
321 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
322 trace_move_extent_read(k.k);
324 atomic_add(io->read_sectors, &ctxt->read_sectors);
325 list_add_tail(&io->list, &ctxt->reads);
328 * dropped by move_read_endio() - guards against use after free of
329 * ctxt when doing wakeup
331 closure_get(&ctxt->cl);
332 bch2_read_extent(trans, &io->rbio,
336 BCH_READ_LAST_FRAGMENT);
339 bio_free_pages(&io->write.op.wbio.bio);
343 percpu_ref_put(&c->writes);
344 trace_and_count(c, move_extent_alloc_mem_fail, k.k);
348 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
349 struct bch_inode_unpacked *inode)
351 struct btree_iter iter;
355 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
356 BTREE_ITER_ALL_SNAPSHOTS);
357 k = bch2_btree_iter_peek(&iter);
362 if (!k.k || !bkey_eq(k.k->p, pos)) {
367 ret = bkey_is_inode(k.k) ? 0 : -EIO;
371 ret = bch2_inode_unpack(k, inode);
375 bch2_trans_iter_exit(trans, &iter);
379 static int move_ratelimit(struct btree_trans *trans,
380 struct moving_context *ctxt)
382 struct bch_fs *c = trans->c;
385 if (ctxt->wait_on_copygc) {
386 bch2_trans_unlock(trans);
387 wait_event_killable(c->copygc_running_wq,
388 !c->copygc_running ||
389 kthread_should_stop());
393 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
396 bch2_trans_unlock(trans);
397 set_current_state(TASK_INTERRUPTIBLE);
400 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
401 __set_current_state(TASK_RUNNING);
406 schedule_timeout(delay);
408 if (unlikely(freezing(current))) {
409 move_ctxt_wait_event(ctxt, trans, list_empty(&ctxt->reads));
414 move_ctxt_wait_event(ctxt, trans,
415 atomic_read(&ctxt->write_sectors) <
416 c->opts.move_bytes_in_flight >> 9);
418 move_ctxt_wait_event(ctxt, trans,
419 atomic_read(&ctxt->read_sectors) <
420 c->opts.move_bytes_in_flight >> 9);
425 static int move_get_io_opts(struct btree_trans *trans,
426 struct bch_io_opts *io_opts,
427 struct bkey_s_c k, u64 *cur_inum)
429 struct bch_inode_unpacked inode;
432 if (*cur_inum == k.k->p.inode)
435 ret = lookup_inode(trans,
436 SPOS(0, k.k->p.inode, k.k->p.snapshot),
438 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
442 bch2_inode_opts_get(io_opts, trans->c, &inode);
444 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
445 *cur_inum = k.k->p.inode;
449 static int __bch2_move_data(struct moving_context *ctxt,
452 move_pred_fn pred, void *arg,
453 enum btree_id btree_id)
455 struct bch_fs *c = ctxt->c;
456 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
458 struct btree_trans trans;
459 struct btree_iter iter;
461 struct data_update_opts data_opts;
462 u64 cur_inum = U64_MAX;
465 bch2_bkey_buf_init(&sk);
466 bch2_trans_init(&trans, c, 0, 0);
468 ctxt->stats->data_type = BCH_DATA_user;
469 ctxt->stats->btree_id = btree_id;
470 ctxt->stats->pos = start;
472 bch2_trans_iter_init(&trans, &iter, btree_id, start,
474 BTREE_ITER_ALL_SNAPSHOTS);
477 bch2_ratelimit_reset(ctxt->rate);
479 while (!move_ratelimit(&trans, ctxt)) {
480 bch2_trans_begin(&trans);
482 k = bch2_btree_iter_peek(&iter);
487 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
492 if (bkey_ge(bkey_start_pos(k.k), end))
495 ctxt->stats->pos = iter.pos;
497 if (!bkey_extent_is_direct_data(k.k))
500 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
504 memset(&data_opts, 0, sizeof(data_opts));
505 if (!pred(c, arg, k, &io_opts, &data_opts))
509 * The iterator gets unlocked by __bch2_read_extent - need to
510 * save a copy of @k elsewhere:
512 bch2_bkey_buf_reassemble(&sk, c, k);
513 k = bkey_i_to_s_c(sk.k);
514 bch2_trans_unlock(&trans);
516 ret2 = bch2_move_extent(&trans, &iter, ctxt, io_opts,
517 btree_id, k, data_opts);
519 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
522 if (ret2 == -ENOMEM) {
523 /* memory allocation failure, wait for some IO to finish */
524 bch2_move_ctxt_wait_for_io(ctxt, &trans);
528 /* XXX signal failure */
533 bch2_ratelimit_increment(ctxt->rate, k.k->size);
535 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
537 bch2_btree_iter_advance(&iter);
540 bch2_trans_iter_exit(&trans, &iter);
541 bch2_trans_exit(&trans);
542 bch2_bkey_buf_exit(&sk, c);
547 int bch2_move_data(struct bch_fs *c,
548 enum btree_id start_btree_id, struct bpos start_pos,
549 enum btree_id end_btree_id, struct bpos end_pos,
550 struct bch_ratelimit *rate,
551 struct bch_move_stats *stats,
552 struct write_point_specifier wp,
554 move_pred_fn pred, void *arg)
556 struct moving_context ctxt;
560 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
562 for (id = start_btree_id;
563 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
565 stats->btree_id = id;
567 if (id != BTREE_ID_extents &&
568 id != BTREE_ID_reflink)
571 ret = __bch2_move_data(&ctxt,
572 id == start_btree_id ? start_pos : POS_MIN,
573 id == end_btree_id ? end_pos : POS_MAX,
579 bch2_moving_ctxt_exit(&ctxt);
584 static int verify_bucket_evacuated(struct btree_trans *trans, struct bpos bucket, int gen)
586 struct bch_fs *c = trans->c;
587 struct btree_iter iter;
589 struct printbuf buf = PRINTBUF;
590 struct bch_backpointer bp;
594 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
595 bucket, BTREE_ITER_CACHED);
597 k = bch2_btree_iter_peek_slot(&iter);
600 if (!ret && k.k->type == KEY_TYPE_alloc_v4) {
601 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
603 if (a.v->gen == gen &&
604 a.v->dirty_sectors) {
605 if (a.v->data_type == BCH_DATA_btree) {
606 bch2_trans_unlock(trans);
607 if (bch2_btree_interior_updates_flush(c))
609 goto failed_to_evacuate;
614 bch2_trans_iter_exit(trans, &iter);
617 bch2_trans_iter_exit(trans, &iter);
619 prt_printf(&buf, bch2_log_msg(c, "failed to evacuate bucket "));
620 bch2_bkey_val_to_text(&buf, c, k);
623 bch2_trans_begin(trans);
625 ret = bch2_get_next_backpointer(trans, bucket, gen,
628 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
632 if (bp_offset == U64_MAX)
635 k = bch2_backpointer_get_key(trans, &iter,
636 bucket, bp_offset, bp);
638 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
645 bch2_bkey_val_to_text(&buf, c, k);
646 bch2_trans_iter_exit(trans, &iter);
649 bch2_print_string_as_lines(KERN_ERR, buf.buf);
654 int __bch2_evacuate_bucket(struct moving_context *ctxt,
655 struct bpos bucket, int gen,
656 struct data_update_opts _data_opts)
658 struct bch_fs *c = ctxt->c;
659 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
660 struct btree_trans trans;
661 struct btree_iter iter;
663 struct bch_backpointer bp;
664 struct data_update_opts data_opts;
665 u64 bp_offset = 0, cur_inum = U64_MAX;
668 bch2_bkey_buf_init(&sk);
669 bch2_trans_init(&trans, c, 0, 0);
671 while (!(ret = move_ratelimit(&trans, ctxt))) {
672 bch2_trans_begin(&trans);
674 ret = bch2_get_next_backpointer(&trans, bucket, gen,
677 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
681 if (bp_offset == U64_MAX)
685 const struct bch_extent_ptr *ptr;
689 k = bch2_backpointer_get_key(&trans, &iter,
690 bucket, bp_offset, bp);
692 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
699 bch2_bkey_buf_reassemble(&sk, c, k);
700 k = bkey_i_to_s_c(sk.k);
702 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
704 bch2_trans_iter_exit(&trans, &iter);
708 data_opts = _data_opts;
709 data_opts.target = io_opts.background_target;
710 data_opts.rewrite_ptrs = 0;
712 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
713 if (ptr->dev == bucket.inode)
714 data_opts.rewrite_ptrs |= 1U << i;
718 ret = bch2_move_extent(&trans, &iter, ctxt, io_opts,
719 bp.btree_id, k, data_opts);
720 bch2_trans_iter_exit(&trans, &iter);
722 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
724 if (ret == -ENOMEM) {
725 /* memory allocation failure, wait for some IO to finish */
726 bch2_move_ctxt_wait_for_io(ctxt, &trans);
733 bch2_ratelimit_increment(ctxt->rate, k.k->size);
734 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
738 b = bch2_backpointer_get_node(&trans, &iter,
739 bucket, bp_offset, bp);
740 ret = PTR_ERR_OR_ZERO(b);
741 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
743 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
750 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0);
751 bch2_trans_iter_exit(&trans, &iter);
753 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
759 bch2_ratelimit_increment(ctxt->rate,
760 c->opts.btree_node_size >> 9);
761 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
762 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
768 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && gen >= 0) {
769 bch2_trans_unlock(&trans);
770 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
771 closure_sync(&ctxt->cl);
772 if (!ctxt->write_error)
773 lockrestart_do(&trans, verify_bucket_evacuated(&trans, bucket, gen));
776 bch2_trans_exit(&trans);
777 bch2_bkey_buf_exit(&sk, c);
781 int bch2_evacuate_bucket(struct bch_fs *c,
782 struct bpos bucket, int gen,
783 struct data_update_opts data_opts,
784 struct bch_ratelimit *rate,
785 struct bch_move_stats *stats,
786 struct write_point_specifier wp,
789 struct moving_context ctxt;
792 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
793 ret = __bch2_evacuate_bucket(&ctxt, bucket, gen, data_opts);
794 bch2_moving_ctxt_exit(&ctxt);
799 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
800 struct btree *, struct bch_io_opts *,
801 struct data_update_opts *);
803 static int bch2_move_btree(struct bch_fs *c,
804 enum btree_id start_btree_id, struct bpos start_pos,
805 enum btree_id end_btree_id, struct bpos end_pos,
806 move_btree_pred pred, void *arg,
807 struct bch_move_stats *stats)
809 bool kthread = (current->flags & PF_KTHREAD) != 0;
810 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
811 struct btree_trans trans;
812 struct btree_iter iter;
815 struct data_update_opts data_opts;
818 bch2_trans_init(&trans, c, 0, 0);
819 progress_list_add(c, stats);
821 stats->data_type = BCH_DATA_btree;
823 for (id = start_btree_id;
824 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
826 stats->btree_id = id;
828 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
829 BTREE_ITER_PREFETCH);
832 while (bch2_trans_begin(&trans),
833 (b = bch2_btree_iter_peek_node(&iter)) &&
834 !(ret = PTR_ERR_OR_ZERO(b))) {
835 if (kthread && kthread_should_stop())
838 if ((cmp_int(id, end_btree_id) ?:
839 bpos_cmp(b->key.k.p, end_pos)) > 0)
842 stats->pos = iter.pos;
844 if (!pred(c, arg, b, &io_opts, &data_opts))
847 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
848 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
853 bch2_btree_iter_next_node(&iter);
855 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
858 bch2_trans_iter_exit(&trans, &iter);
860 if (kthread && kthread_should_stop())
864 bch2_trans_exit(&trans);
867 bch_err(c, "error in %s(): %s", __func__, bch2_err_str(ret));
869 bch2_btree_interior_updates_flush(c);
871 progress_list_del(c, stats);
875 static bool rereplicate_pred(struct bch_fs *c, void *arg,
877 struct bch_io_opts *io_opts,
878 struct data_update_opts *data_opts)
880 unsigned nr_good = bch2_bkey_durability(c, k);
881 unsigned replicas = bkey_is_btree_ptr(k.k)
882 ? c->opts.metadata_replicas
883 : io_opts->data_replicas;
885 if (!nr_good || nr_good >= replicas)
888 data_opts->target = 0;
889 data_opts->extra_replicas = replicas - nr_good;
890 data_opts->btree_insert_flags = 0;
894 static bool migrate_pred(struct bch_fs *c, void *arg,
896 struct bch_io_opts *io_opts,
897 struct data_update_opts *data_opts)
899 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
900 const struct bch_extent_ptr *ptr;
901 struct bch_ioctl_data *op = arg;
904 data_opts->rewrite_ptrs = 0;
905 data_opts->target = 0;
906 data_opts->extra_replicas = 0;
907 data_opts->btree_insert_flags = 0;
909 bkey_for_each_ptr(ptrs, ptr) {
910 if (ptr->dev == op->migrate.dev)
911 data_opts->rewrite_ptrs |= 1U << i;
915 return data_opts->rewrite_ptrs != 0;
918 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
920 struct bch_io_opts *io_opts,
921 struct data_update_opts *data_opts)
923 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
926 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
928 struct bch_io_opts *io_opts,
929 struct data_update_opts *data_opts)
931 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
934 static bool bformat_needs_redo(struct bkey_format *f)
938 for (i = 0; i < f->nr_fields; i++) {
939 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
940 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
941 u64 field_offset = le64_to_cpu(f->field_offset[i]);
943 if (f->bits_per_field[i] > unpacked_bits)
946 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
949 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
958 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
960 struct bch_io_opts *io_opts,
961 struct data_update_opts *data_opts)
963 if (b->version_ondisk != c->sb.version ||
964 btree_node_need_rewrite(b) ||
965 bformat_needs_redo(&b->format)) {
966 data_opts->target = 0;
967 data_opts->extra_replicas = 0;
968 data_opts->btree_insert_flags = 0;
975 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
979 ret = bch2_move_btree(c,
981 BTREE_ID_NR, SPOS_MAX,
982 rewrite_old_nodes_pred, c, stats);
984 mutex_lock(&c->sb_lock);
985 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
986 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
987 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
989 mutex_unlock(&c->sb_lock);
995 int bch2_data_job(struct bch_fs *c,
996 struct bch_move_stats *stats,
997 struct bch_ioctl_data op)
1002 case BCH_DATA_OP_REREPLICATE:
1003 bch2_move_stats_init(stats, "rereplicate");
1004 stats->data_type = BCH_DATA_journal;
1005 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1007 ret = bch2_move_btree(c,
1008 op.start_btree, op.start_pos,
1009 op.end_btree, op.end_pos,
1010 rereplicate_btree_pred, c, stats) ?: ret;
1011 ret = bch2_replicas_gc2(c) ?: ret;
1013 ret = bch2_move_data(c,
1014 op.start_btree, op.start_pos,
1015 op.end_btree, op.end_pos,
1018 writepoint_hashed((unsigned long) current),
1020 rereplicate_pred, c) ?: ret;
1021 ret = bch2_replicas_gc2(c) ?: ret;
1023 case BCH_DATA_OP_MIGRATE:
1024 if (op.migrate.dev >= c->sb.nr_devices)
1027 bch2_move_stats_init(stats, "migrate");
1028 stats->data_type = BCH_DATA_journal;
1029 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1031 ret = bch2_move_btree(c,
1032 op.start_btree, op.start_pos,
1033 op.end_btree, op.end_pos,
1034 migrate_btree_pred, &op, stats) ?: ret;
1035 ret = bch2_replicas_gc2(c) ?: ret;
1037 ret = bch2_move_data(c,
1038 op.start_btree, op.start_pos,
1039 op.end_btree, op.end_pos,
1042 writepoint_hashed((unsigned long) current),
1044 migrate_pred, &op) ?: ret;
1045 ret = bch2_replicas_gc2(c) ?: ret;
1047 case BCH_DATA_OP_REWRITE_OLD_NODES:
1048 bch2_move_stats_init(stats, "rewrite_old_nodes");
1049 ret = bch2_scan_old_btree_nodes(c, stats);