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 closure *cl)
58 struct moving_io *io = container_of(cl, struct moving_io, cl);
59 struct moving_context *ctxt = io->write.ctxt;
60 struct bch_fs *c = ctxt->c;
62 bch2_data_update_exit(&io->write);
64 percpu_ref_put(&c->writes);
68 static void move_write_done(struct closure *cl)
70 struct moving_io *io = container_of(cl, struct moving_io, cl);
71 struct moving_context *ctxt = io->write.ctxt;
73 if (io->write.op.error)
74 ctxt->write_error = true;
76 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
77 closure_return_with_destructor(cl, move_free);
80 static void move_write(struct closure *cl)
82 struct moving_io *io = container_of(cl, struct moving_io, cl);
84 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
85 closure_return_with_destructor(cl, move_free);
89 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
91 bch2_data_update_read_done(&io->write, io->rbio.pick.crc, cl);
92 continue_at(cl, move_write_done, NULL);
95 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
97 struct moving_io *io =
98 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
100 return io && io->read_completed ? io : NULL;
103 static void move_read_endio(struct bio *bio)
105 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
106 struct moving_context *ctxt = io->write.ctxt;
108 atomic_sub(io->read_sectors, &ctxt->read_sectors);
109 io->read_completed = true;
111 wake_up(&ctxt->wait);
112 closure_put(&ctxt->cl);
115 static void do_pending_writes(struct moving_context *ctxt, struct btree_trans *trans)
117 struct moving_io *io;
120 bch2_trans_unlock(trans);
122 while ((io = next_pending_write(ctxt))) {
124 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
128 #define move_ctxt_wait_event(_ctxt, _trans, _cond) \
130 do_pending_writes(_ctxt, _trans); \
134 __wait_event((_ctxt)->wait, \
135 next_pending_write(_ctxt) || (_cond)); \
138 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
139 struct btree_trans *trans)
141 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
143 move_ctxt_wait_event(ctxt, trans,
144 !atomic_read(&ctxt->write_sectors) ||
145 atomic_read(&ctxt->write_sectors) != sectors_pending);
148 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
150 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
151 closure_sync(&ctxt->cl);
152 EBUG_ON(atomic_read(&ctxt->write_sectors));
155 progress_list_del(ctxt->c, ctxt->stats);
157 trace_move_data(ctxt->c,
158 atomic64_read(&ctxt->stats->sectors_moved),
159 atomic64_read(&ctxt->stats->keys_moved));
163 void bch2_moving_ctxt_init(struct moving_context *ctxt,
165 struct bch_ratelimit *rate,
166 struct bch_move_stats *stats,
167 struct write_point_specifier wp,
170 memset(ctxt, 0, sizeof(*ctxt));
176 ctxt->wait_on_copygc = wait_on_copygc;
178 closure_init_stack(&ctxt->cl);
179 INIT_LIST_HEAD(&ctxt->reads);
180 init_waitqueue_head(&ctxt->wait);
183 progress_list_add(c, stats);
184 stats->data_type = BCH_DATA_user;
188 void bch_move_stats_init(struct bch_move_stats *stats, char *name)
190 memset(stats, 0, sizeof(*stats));
191 scnprintf(stats->name, sizeof(stats->name), "%s", name);
194 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
195 struct btree_iter *iter,
197 struct data_update_opts data_opts)
199 struct bch_fs *c = trans->c;
203 n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
204 ret = PTR_ERR_OR_ZERO(n);
208 bkey_reassemble(n, k);
210 while (data_opts.kill_ptrs) {
211 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
212 struct bch_extent_ptr *ptr;
214 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
215 data_opts.kill_ptrs ^= 1U << drop;
219 * If the new extent no longer has any pointers, bch2_extent_normalize()
220 * will do the appropriate thing with it (turning it into a
221 * KEY_TYPE_error key, or just a discard if it was a cached extent)
223 bch2_extent_normalize(c, bkey_i_to_s(n));
226 * Since we're not inserting through an extent iterator
227 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
228 * we aren't using the extent overwrite path to delete, we're
229 * just using the normal key deletion path:
231 if (bkey_deleted(&n->k))
234 return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
235 bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
238 static int bch2_move_extent(struct btree_trans *trans,
239 struct btree_iter *iter,
240 struct moving_context *ctxt,
241 struct bch_io_opts io_opts,
242 enum btree_id btree_id,
244 struct data_update_opts data_opts)
246 struct bch_fs *c = trans->c;
247 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
248 struct moving_io *io;
249 const union bch_extent_entry *entry;
250 struct extent_ptr_decoded p;
251 unsigned sectors = k.k->size, pages;
254 bch2_data_update_opts_normalize(k, &data_opts);
256 if (!data_opts.rewrite_ptrs &&
257 !data_opts.extra_replicas) {
258 if (data_opts.kill_ptrs)
259 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
263 if (!percpu_ref_tryget_live(&c->writes))
266 /* write path might have to decompress data: */
267 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
268 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
270 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
271 io = kzalloc(sizeof(struct moving_io) +
272 sizeof(struct bio_vec) * pages, GFP_KERNEL);
276 io->write.ctxt = ctxt;
277 io->read_sectors = k.k->size;
278 io->write_sectors = k.k->size;
280 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
281 bio_set_prio(&io->write.op.wbio.bio,
282 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
284 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
289 io->rbio.opts = io_opts;
290 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
291 io->rbio.bio.bi_vcnt = pages;
292 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
293 io->rbio.bio.bi_iter.bi_size = sectors << 9;
295 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
296 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
297 io->rbio.bio.bi_end_io = move_read_endio;
299 ret = bch2_data_update_init(c, &io->write, ctxt->wp, io_opts,
300 data_opts, btree_id, k);
304 io->write.ctxt = ctxt;
306 atomic64_inc(&ctxt->stats->keys_moved);
307 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
308 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
309 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
310 trace_move_extent_read(k.k);
312 atomic_add(io->read_sectors, &ctxt->read_sectors);
313 list_add_tail(&io->list, &ctxt->reads);
316 * dropped by move_read_endio() - guards against use after free of
317 * ctxt when doing wakeup
319 closure_get(&ctxt->cl);
320 bch2_read_extent(trans, &io->rbio,
324 BCH_READ_LAST_FRAGMENT);
327 bio_free_pages(&io->write.op.wbio.bio);
331 percpu_ref_put(&c->writes);
332 trace_and_count(c, move_extent_alloc_mem_fail, k.k);
336 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
337 struct bch_inode_unpacked *inode)
339 struct btree_iter iter;
343 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
344 BTREE_ITER_ALL_SNAPSHOTS);
345 k = bch2_btree_iter_peek(&iter);
350 if (!k.k || bkey_cmp(k.k->p, pos)) {
355 ret = bkey_is_inode(k.k) ? 0 : -EIO;
359 ret = bch2_inode_unpack(k, inode);
363 bch2_trans_iter_exit(trans, &iter);
367 static int move_ratelimit(struct btree_trans *trans,
368 struct moving_context *ctxt)
370 struct bch_fs *c = trans->c;
373 if (ctxt->wait_on_copygc) {
374 bch2_trans_unlock(trans);
375 wait_event_killable(c->copygc_running_wq,
376 !c->copygc_running ||
377 kthread_should_stop());
381 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
384 bch2_trans_unlock(trans);
385 set_current_state(TASK_INTERRUPTIBLE);
388 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
389 __set_current_state(TASK_RUNNING);
394 schedule_timeout(delay);
396 if (unlikely(freezing(current))) {
397 move_ctxt_wait_event(ctxt, trans, list_empty(&ctxt->reads));
402 move_ctxt_wait_event(ctxt, trans,
403 atomic_read(&ctxt->write_sectors) <
404 c->opts.move_bytes_in_flight >> 9);
406 move_ctxt_wait_event(ctxt, trans,
407 atomic_read(&ctxt->read_sectors) <
408 c->opts.move_bytes_in_flight >> 9);
413 static int move_get_io_opts(struct btree_trans *trans,
414 struct bch_io_opts *io_opts,
415 struct bkey_s_c k, u64 *cur_inum)
417 struct bch_inode_unpacked inode;
420 if (*cur_inum == k.k->p.inode)
423 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
425 ret = lookup_inode(trans,
426 SPOS(0, k.k->p.inode, k.k->p.snapshot),
428 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
432 bch2_io_opts_apply(io_opts, bch2_inode_opts_get(&inode));
434 *cur_inum = k.k->p.inode;
438 static int __bch2_move_data(struct moving_context *ctxt,
441 move_pred_fn pred, void *arg,
442 enum btree_id btree_id)
444 struct bch_fs *c = ctxt->c;
445 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
447 struct btree_trans trans;
448 struct btree_iter iter;
450 struct data_update_opts data_opts;
451 u64 cur_inum = U64_MAX;
454 bch2_bkey_buf_init(&sk);
455 bch2_trans_init(&trans, c, 0, 0);
457 ctxt->stats->data_type = BCH_DATA_user;
458 ctxt->stats->btree_id = btree_id;
459 ctxt->stats->pos = start;
461 bch2_trans_iter_init(&trans, &iter, btree_id, start,
463 BTREE_ITER_ALL_SNAPSHOTS);
466 bch2_ratelimit_reset(ctxt->rate);
468 while (!move_ratelimit(&trans, ctxt)) {
469 bch2_trans_begin(&trans);
471 k = bch2_btree_iter_peek(&iter);
476 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
481 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
484 ctxt->stats->pos = iter.pos;
486 if (!bkey_extent_is_direct_data(k.k))
489 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
493 memset(&data_opts, 0, sizeof(data_opts));
494 if (!pred(c, arg, k, &io_opts, &data_opts))
498 * The iterator gets unlocked by __bch2_read_extent - need to
499 * save a copy of @k elsewhere:
501 bch2_bkey_buf_reassemble(&sk, c, k);
502 k = bkey_i_to_s_c(sk.k);
504 ret2 = bch2_move_extent(&trans, &iter, ctxt, io_opts,
505 btree_id, k, data_opts);
507 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
510 if (ret2 == -ENOMEM) {
511 /* memory allocation failure, wait for some IO to finish */
512 bch2_move_ctxt_wait_for_io(ctxt, &trans);
516 /* XXX signal failure */
521 bch2_ratelimit_increment(ctxt->rate, k.k->size);
523 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
525 bch2_btree_iter_advance(&iter);
528 bch2_trans_iter_exit(&trans, &iter);
529 bch2_trans_exit(&trans);
530 bch2_bkey_buf_exit(&sk, c);
535 int bch2_move_data(struct bch_fs *c,
536 enum btree_id start_btree_id, struct bpos start_pos,
537 enum btree_id end_btree_id, struct bpos end_pos,
538 struct bch_ratelimit *rate,
539 struct bch_move_stats *stats,
540 struct write_point_specifier wp,
542 move_pred_fn pred, void *arg)
544 struct moving_context ctxt;
548 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
550 for (id = start_btree_id;
551 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
553 stats->btree_id = id;
555 if (id != BTREE_ID_extents &&
556 id != BTREE_ID_reflink)
559 ret = __bch2_move_data(&ctxt,
560 id == start_btree_id ? start_pos : POS_MIN,
561 id == end_btree_id ? end_pos : POS_MAX,
567 bch2_moving_ctxt_exit(&ctxt);
572 static int verify_bucket_evacuated(struct btree_trans *trans, struct bpos bucket, int gen)
574 struct bch_fs *c = trans->c;
575 struct btree_iter iter;
579 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
580 bucket, BTREE_ITER_CACHED);
582 k = bch2_btree_iter_peek_slot(&iter);
585 if (!ret && k.k->type == KEY_TYPE_alloc_v4) {
586 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
588 if (a.v->gen == gen &&
589 a.v->dirty_sectors) {
590 struct printbuf buf = PRINTBUF;
592 if (a.v->data_type == BCH_DATA_btree) {
593 bch2_trans_unlock(trans);
594 if (bch2_btree_interior_updates_flush(c))
598 prt_str(&buf, "failed to evacuate bucket ");
599 bch2_bkey_val_to_text(&buf, c, k);
601 bch_err(c, "%s", buf.buf);
606 bch2_trans_iter_exit(trans, &iter);
610 int __bch2_evacuate_bucket(struct moving_context *ctxt,
611 struct bpos bucket, int gen,
612 struct data_update_opts _data_opts)
614 struct bch_fs *c = ctxt->c;
615 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
616 struct btree_trans trans;
617 struct btree_iter iter;
619 struct bch_backpointer bp;
620 struct data_update_opts data_opts;
621 u64 bp_offset = 0, cur_inum = U64_MAX;
624 bch2_bkey_buf_init(&sk);
625 bch2_trans_init(&trans, c, 0, 0);
627 while (!(ret = move_ratelimit(&trans, ctxt))) {
628 bch2_trans_begin(&trans);
630 ret = bch2_get_next_backpointer(&trans, bucket, gen,
633 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
637 if (bp_offset == U64_MAX)
641 const struct bch_extent_ptr *ptr;
645 k = bch2_backpointer_get_key(&trans, &iter,
646 bucket, bp_offset, bp);
648 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
655 bch2_bkey_buf_reassemble(&sk, c, k);
656 k = bkey_i_to_s_c(sk.k);
658 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
660 bch2_trans_iter_exit(&trans, &iter);
664 data_opts = _data_opts;
665 data_opts.target = io_opts.background_target;
666 data_opts.rewrite_ptrs = 0;
668 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
669 if (ptr->dev == bucket.inode)
670 data_opts.rewrite_ptrs |= 1U << i;
674 ret = bch2_move_extent(&trans, &iter, ctxt, io_opts,
675 bp.btree_id, k, data_opts);
676 bch2_trans_iter_exit(&trans, &iter);
678 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
680 if (ret == -ENOMEM) {
681 /* memory allocation failure, wait for some IO to finish */
682 bch2_move_ctxt_wait_for_io(ctxt, &trans);
689 bch2_ratelimit_increment(ctxt->rate, k.k->size);
690 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
694 b = bch2_backpointer_get_node(&trans, &iter,
695 bucket, bp_offset, bp);
696 ret = PTR_ERR_OR_ZERO(b);
697 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
699 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
706 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0);
707 bch2_trans_iter_exit(&trans, &iter);
709 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
715 bch2_ratelimit_increment(ctxt->rate,
716 c->opts.btree_node_size >> 9);
717 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
718 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
724 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && gen >= 0) {
725 bch2_trans_unlock(&trans);
726 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
727 closure_sync(&ctxt->cl);
728 if (!ctxt->write_error)
729 lockrestart_do(&trans, verify_bucket_evacuated(&trans, bucket, gen));
732 bch2_trans_exit(&trans);
733 bch2_bkey_buf_exit(&sk, c);
737 int bch2_evacuate_bucket(struct bch_fs *c,
738 struct bpos bucket, int gen,
739 struct data_update_opts data_opts,
740 struct bch_ratelimit *rate,
741 struct bch_move_stats *stats,
742 struct write_point_specifier wp,
745 struct moving_context ctxt;
748 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
749 ret = __bch2_evacuate_bucket(&ctxt, bucket, gen, data_opts);
750 bch2_moving_ctxt_exit(&ctxt);
755 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
756 struct btree *, struct bch_io_opts *,
757 struct data_update_opts *);
759 static int bch2_move_btree(struct bch_fs *c,
760 enum btree_id start_btree_id, struct bpos start_pos,
761 enum btree_id end_btree_id, struct bpos end_pos,
762 move_btree_pred pred, void *arg,
763 struct bch_move_stats *stats)
765 bool kthread = (current->flags & PF_KTHREAD) != 0;
766 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
767 struct btree_trans trans;
768 struct btree_iter iter;
771 struct data_update_opts data_opts;
774 bch2_trans_init(&trans, c, 0, 0);
775 progress_list_add(c, stats);
777 stats->data_type = BCH_DATA_btree;
779 for (id = start_btree_id;
780 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
782 stats->btree_id = id;
784 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
785 BTREE_ITER_PREFETCH);
788 while (bch2_trans_begin(&trans),
789 (b = bch2_btree_iter_peek_node(&iter)) &&
790 !(ret = PTR_ERR_OR_ZERO(b))) {
791 if (kthread && kthread_should_stop())
794 if ((cmp_int(id, end_btree_id) ?:
795 bpos_cmp(b->key.k.p, end_pos)) > 0)
798 stats->pos = iter.pos;
800 if (!pred(c, arg, b, &io_opts, &data_opts))
803 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
804 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
809 bch2_btree_iter_next_node(&iter);
811 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
814 bch2_trans_iter_exit(&trans, &iter);
816 if (kthread && kthread_should_stop())
820 bch2_trans_exit(&trans);
823 bch_err(c, "error in %s(): %s", __func__, bch2_err_str(ret));
825 bch2_btree_interior_updates_flush(c);
827 progress_list_del(c, stats);
831 static bool rereplicate_pred(struct bch_fs *c, void *arg,
833 struct bch_io_opts *io_opts,
834 struct data_update_opts *data_opts)
836 unsigned nr_good = bch2_bkey_durability(c, k);
837 unsigned replicas = bkey_is_btree_ptr(k.k)
838 ? c->opts.metadata_replicas
839 : io_opts->data_replicas;
841 if (!nr_good || nr_good >= replicas)
844 data_opts->target = 0;
845 data_opts->extra_replicas = replicas - nr_good;
846 data_opts->btree_insert_flags = 0;
850 static bool migrate_pred(struct bch_fs *c, void *arg,
852 struct bch_io_opts *io_opts,
853 struct data_update_opts *data_opts)
855 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
856 const struct bch_extent_ptr *ptr;
857 struct bch_ioctl_data *op = arg;
860 data_opts->rewrite_ptrs = 0;
861 data_opts->target = 0;
862 data_opts->extra_replicas = 0;
863 data_opts->btree_insert_flags = 0;
865 bkey_for_each_ptr(ptrs, ptr) {
866 if (ptr->dev == op->migrate.dev)
867 data_opts->rewrite_ptrs |= 1U << i;
871 return data_opts->rewrite_ptrs != 0;
874 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
876 struct bch_io_opts *io_opts,
877 struct data_update_opts *data_opts)
879 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
882 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
884 struct bch_io_opts *io_opts,
885 struct data_update_opts *data_opts)
887 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
890 static bool bformat_needs_redo(struct bkey_format *f)
894 for (i = 0; i < f->nr_fields; i++) {
895 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
896 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
897 u64 field_offset = le64_to_cpu(f->field_offset[i]);
899 if (f->bits_per_field[i] > unpacked_bits)
902 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
905 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
914 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
916 struct bch_io_opts *io_opts,
917 struct data_update_opts *data_opts)
919 if (b->version_ondisk != c->sb.version ||
920 btree_node_need_rewrite(b) ||
921 bformat_needs_redo(&b->format)) {
922 data_opts->target = 0;
923 data_opts->extra_replicas = 0;
924 data_opts->btree_insert_flags = 0;
931 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
935 ret = bch2_move_btree(c,
937 BTREE_ID_NR, SPOS_MAX,
938 rewrite_old_nodes_pred, c, stats);
940 mutex_lock(&c->sb_lock);
941 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
942 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
943 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
945 mutex_unlock(&c->sb_lock);
951 int bch2_data_job(struct bch_fs *c,
952 struct bch_move_stats *stats,
953 struct bch_ioctl_data op)
958 case BCH_DATA_OP_REREPLICATE:
959 bch_move_stats_init(stats, "rereplicate");
960 stats->data_type = BCH_DATA_journal;
961 ret = bch2_journal_flush_device_pins(&c->journal, -1);
963 ret = bch2_move_btree(c,
964 op.start_btree, op.start_pos,
965 op.end_btree, op.end_pos,
966 rereplicate_btree_pred, c, stats) ?: ret;
967 ret = bch2_replicas_gc2(c) ?: ret;
969 ret = bch2_move_data(c,
970 op.start_btree, op.start_pos,
971 op.end_btree, op.end_pos,
974 writepoint_hashed((unsigned long) current),
976 rereplicate_pred, c) ?: ret;
977 ret = bch2_replicas_gc2(c) ?: ret;
979 case BCH_DATA_OP_MIGRATE:
980 if (op.migrate.dev >= c->sb.nr_devices)
983 bch_move_stats_init(stats, "migrate");
984 stats->data_type = BCH_DATA_journal;
985 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
987 ret = bch2_move_btree(c,
988 op.start_btree, op.start_pos,
989 op.end_btree, op.end_pos,
990 migrate_btree_pred, &op, stats) ?: ret;
991 ret = bch2_replicas_gc2(c) ?: ret;
993 ret = bch2_move_data(c,
994 op.start_btree, op.start_pos,
995 op.end_btree, op.end_pos,
998 writepoint_hashed((unsigned long) current),
1000 migrate_pred, &op) ?: ret;
1001 ret = bch2_replicas_gc2(c) ?: ret;
1003 case BCH_DATA_OP_REWRITE_OLD_NODES:
1004 bch_move_stats_init(stats, "rewrite_old_nodes");
1005 ret = bch2_scan_old_btree_nodes(c, stats);