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 "disk_groups.h"
17 #include "journal_reclaim.h"
23 #include <linux/ioprio.h>
24 #include <linux/kthread.h>
26 #include <trace/events/bcachefs.h>
28 static void progress_list_add(struct bch_fs *c, struct bch_move_stats *stats)
30 mutex_lock(&c->data_progress_lock);
31 list_add(&stats->list, &c->data_progress_list);
32 mutex_unlock(&c->data_progress_lock);
35 static void progress_list_del(struct bch_fs *c, struct bch_move_stats *stats)
37 mutex_lock(&c->data_progress_lock);
38 list_del(&stats->list);
39 mutex_unlock(&c->data_progress_lock);
43 struct list_head list;
47 unsigned read_sectors;
48 unsigned write_sectors;
50 struct bch_read_bio rbio;
52 struct data_update write;
53 /* Must be last since it is variable size */
54 struct bio_vec bi_inline_vecs[0];
57 static void move_free(struct moving_io *io)
59 struct moving_context *ctxt = io->write.ctxt;
60 struct bch_fs *c = ctxt->c;
62 bch2_data_update_exit(&io->write);
64 bch2_write_ref_put(c, BCH_WRITE_REF_move);
68 static void move_write_done(struct bch_write_op *op)
70 struct moving_io *io = container_of(op, struct moving_io, write.op);
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 atomic_dec(&io->write.ctxt->write_ios);
79 closure_put(&ctxt->cl);
82 static void move_write(struct moving_io *io)
84 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
89 closure_get(&io->write.ctxt->cl);
90 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
91 atomic_inc(&io->write.ctxt->write_ios);
93 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
96 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
98 struct moving_io *io =
99 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
101 return io && io->read_completed ? io : NULL;
104 static void move_read_endio(struct bio *bio)
106 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
107 struct moving_context *ctxt = io->write.ctxt;
109 atomic_sub(io->read_sectors, &ctxt->read_sectors);
110 atomic_dec(&ctxt->read_ios);
111 io->read_completed = true;
113 wake_up(&ctxt->wait);
114 closure_put(&ctxt->cl);
117 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt,
118 struct btree_trans *trans)
120 struct moving_io *io;
123 bch2_trans_unlock(trans);
125 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
131 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
132 struct btree_trans *trans)
134 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
136 move_ctxt_wait_event(ctxt, trans,
137 !atomic_read(&ctxt->write_sectors) ||
138 atomic_read(&ctxt->write_sectors) != sectors_pending);
141 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
143 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
144 closure_sync(&ctxt->cl);
146 EBUG_ON(atomic_read(&ctxt->write_sectors));
147 EBUG_ON(atomic_read(&ctxt->write_ios));
148 EBUG_ON(atomic_read(&ctxt->read_sectors));
149 EBUG_ON(atomic_read(&ctxt->read_ios));
152 progress_list_del(ctxt->c, ctxt->stats);
154 trace_move_data(ctxt->c,
155 atomic64_read(&ctxt->stats->sectors_moved),
156 atomic64_read(&ctxt->stats->keys_moved));
160 void bch2_moving_ctxt_init(struct moving_context *ctxt,
162 struct bch_ratelimit *rate,
163 struct bch_move_stats *stats,
164 struct write_point_specifier wp,
167 memset(ctxt, 0, sizeof(*ctxt));
173 ctxt->wait_on_copygc = wait_on_copygc;
175 closure_init_stack(&ctxt->cl);
176 INIT_LIST_HEAD(&ctxt->reads);
177 init_waitqueue_head(&ctxt->wait);
180 progress_list_add(c, stats);
181 stats->data_type = BCH_DATA_user;
185 void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
187 memset(stats, 0, sizeof(*stats));
188 scnprintf(stats->name, sizeof(stats->name), "%s", name);
191 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
192 struct btree_iter *iter,
194 struct data_update_opts data_opts)
196 struct bch_fs *c = trans->c;
200 n = bch2_bkey_make_mut(trans, k);
201 ret = PTR_ERR_OR_ZERO(n);
205 while (data_opts.kill_ptrs) {
206 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
207 struct bch_extent_ptr *ptr;
209 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
210 data_opts.kill_ptrs ^= 1U << drop;
214 * If the new extent no longer has any pointers, bch2_extent_normalize()
215 * will do the appropriate thing with it (turning it into a
216 * KEY_TYPE_error key, or just a discard if it was a cached extent)
218 bch2_extent_normalize(c, bkey_i_to_s(n));
221 * Since we're not inserting through an extent iterator
222 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
223 * we aren't using the extent overwrite path to delete, we're
224 * just using the normal key deletion path:
226 if (bkey_deleted(&n->k))
229 return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
230 bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
233 static int bch2_move_extent(struct btree_trans *trans,
234 struct btree_iter *iter,
235 struct moving_context *ctxt,
236 struct bch_io_opts io_opts,
237 enum btree_id btree_id,
239 struct data_update_opts data_opts)
241 struct bch_fs *c = trans->c;
242 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
243 struct moving_io *io;
244 const union bch_extent_entry *entry;
245 struct extent_ptr_decoded p;
246 unsigned sectors = k.k->size, pages;
249 bch2_data_update_opts_normalize(k, &data_opts);
251 if (!data_opts.rewrite_ptrs &&
252 !data_opts.extra_replicas) {
253 if (data_opts.kill_ptrs)
254 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
258 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_move))
259 return -BCH_ERR_erofs_no_writes;
262 * Before memory allocations & taking nocow locks in
263 * bch2_data_update_init():
265 bch2_trans_unlock(trans);
267 /* write path might have to decompress data: */
268 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
269 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
271 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
272 io = kzalloc(sizeof(struct moving_io) +
273 sizeof(struct bio_vec) * pages, GFP_KERNEL);
277 io->write.ctxt = ctxt;
278 io->read_sectors = k.k->size;
279 io->write_sectors = k.k->size;
281 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
282 bio_set_prio(&io->write.op.wbio.bio,
283 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
285 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
290 io->rbio.opts = io_opts;
291 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
292 io->rbio.bio.bi_vcnt = pages;
293 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
294 io->rbio.bio.bi_iter.bi_size = sectors << 9;
296 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
297 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
298 io->rbio.bio.bi_end_io = move_read_endio;
300 ret = bch2_data_update_init(trans, ctxt, &io->write, ctxt->wp,
301 io_opts, data_opts, btree_id, k);
302 if (ret && ret != -BCH_ERR_unwritten_extent_update)
305 io->write.ctxt = ctxt;
306 io->write.op.end_io = move_write_done;
308 atomic64_inc(&ctxt->stats->keys_moved);
309 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
311 if (ret == -BCH_ERR_unwritten_extent_update) {
312 bch2_update_unwritten_extent(trans, &io->write);
319 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
320 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
321 trace_move_extent_read(k.k);
323 atomic_add(io->read_sectors, &ctxt->read_sectors);
324 atomic_inc(&ctxt->read_ios);
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 bch2_write_ref_put(c, BCH_WRITE_REF_move);
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));
415 * XXX: these limits really ought to be per device, SSDs and hard drives
416 * will want different limits
418 move_ctxt_wait_event(ctxt, trans,
419 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
420 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
421 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
422 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
427 static int move_get_io_opts(struct btree_trans *trans,
428 struct bch_io_opts *io_opts,
429 struct bkey_s_c k, u64 *cur_inum)
431 struct bch_inode_unpacked inode;
434 if (*cur_inum == k.k->p.inode)
437 ret = lookup_inode(trans,
438 SPOS(0, k.k->p.inode, k.k->p.snapshot),
440 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
444 bch2_inode_opts_get(io_opts, trans->c, &inode);
446 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
447 *cur_inum = k.k->p.inode;
451 static int __bch2_move_data(struct moving_context *ctxt,
454 move_pred_fn pred, void *arg,
455 enum btree_id btree_id)
457 struct bch_fs *c = ctxt->c;
458 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
460 struct btree_trans trans;
461 struct btree_iter iter;
463 struct data_update_opts data_opts;
464 u64 cur_inum = U64_MAX;
467 bch2_bkey_buf_init(&sk);
468 bch2_trans_init(&trans, c, 0, 0);
470 ctxt->stats->data_type = BCH_DATA_user;
471 ctxt->stats->btree_id = btree_id;
472 ctxt->stats->pos = start;
474 bch2_trans_iter_init(&trans, &iter, btree_id, start,
476 BTREE_ITER_ALL_SNAPSHOTS);
479 bch2_ratelimit_reset(ctxt->rate);
481 while (!move_ratelimit(&trans, ctxt)) {
482 bch2_trans_begin(&trans);
484 k = bch2_btree_iter_peek(&iter);
489 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
494 if (bkey_ge(bkey_start_pos(k.k), end))
497 ctxt->stats->pos = iter.pos;
499 if (!bkey_extent_is_direct_data(k.k))
502 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
506 memset(&data_opts, 0, sizeof(data_opts));
507 if (!pred(c, arg, k, &io_opts, &data_opts))
511 * The iterator gets unlocked by __bch2_read_extent - need to
512 * save a copy of @k elsewhere:
514 bch2_bkey_buf_reassemble(&sk, c, k);
515 k = bkey_i_to_s_c(sk.k);
516 bch2_trans_unlock(&trans);
518 ret2 = bch2_move_extent(&trans, &iter, ctxt, io_opts,
519 btree_id, k, data_opts);
521 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
524 if (ret2 == -ENOMEM) {
525 /* memory allocation failure, wait for some IO to finish */
526 bch2_move_ctxt_wait_for_io(ctxt, &trans);
530 /* XXX signal failure */
535 bch2_ratelimit_increment(ctxt->rate, k.k->size);
537 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
539 bch2_btree_iter_advance(&iter);
542 bch2_trans_iter_exit(&trans, &iter);
543 bch2_trans_exit(&trans);
544 bch2_bkey_buf_exit(&sk, c);
549 int bch2_move_data(struct bch_fs *c,
550 enum btree_id start_btree_id, struct bpos start_pos,
551 enum btree_id end_btree_id, struct bpos end_pos,
552 struct bch_ratelimit *rate,
553 struct bch_move_stats *stats,
554 struct write_point_specifier wp,
556 move_pred_fn pred, void *arg)
558 struct moving_context ctxt;
562 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
564 for (id = start_btree_id;
565 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
567 stats->btree_id = id;
569 if (id != BTREE_ID_extents &&
570 id != BTREE_ID_reflink)
573 ret = __bch2_move_data(&ctxt,
574 id == start_btree_id ? start_pos : POS_MIN,
575 id == end_btree_id ? end_pos : POS_MAX,
581 bch2_moving_ctxt_exit(&ctxt);
586 static int verify_bucket_evacuated(struct btree_trans *trans, struct bpos bucket, int gen)
588 struct bch_fs *c = trans->c;
589 struct btree_iter iter;
591 struct printbuf buf = PRINTBUF;
592 struct bch_backpointer bp;
596 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
597 bucket, BTREE_ITER_CACHED);
599 k = bch2_btree_iter_peek_slot(&iter);
602 if (!ret && k.k->type == KEY_TYPE_alloc_v4) {
603 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
605 if (a.v->gen == gen &&
606 a.v->dirty_sectors) {
607 if (a.v->data_type == BCH_DATA_btree) {
608 bch2_trans_unlock(trans);
609 if (bch2_btree_interior_updates_flush(c))
611 goto failed_to_evacuate;
616 bch2_trans_iter_exit(trans, &iter);
619 bch2_trans_iter_exit(trans, &iter);
621 prt_printf(&buf, bch2_log_msg(c, "failed to evacuate bucket "));
622 bch2_bkey_val_to_text(&buf, c, k);
625 bch2_trans_begin(trans);
627 ret = bch2_get_next_backpointer(trans, bucket, gen,
630 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
634 if (bp_offset == U64_MAX)
637 k = bch2_backpointer_get_key(trans, &iter,
638 bucket, bp_offset, bp);
640 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
647 bch2_bkey_val_to_text(&buf, c, k);
648 bch2_trans_iter_exit(trans, &iter);
651 bch2_print_string_as_lines(KERN_ERR, buf.buf);
656 int __bch2_evacuate_bucket(struct moving_context *ctxt,
657 struct bpos bucket, int gen,
658 struct data_update_opts _data_opts)
660 struct bch_fs *c = ctxt->c;
661 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
662 struct btree_trans trans;
663 struct btree_iter iter;
665 struct bch_backpointer bp;
666 struct bch_alloc_v4 a_convert;
667 const struct bch_alloc_v4 *a;
669 struct data_update_opts data_opts;
670 unsigned dirty_sectors, bucket_size;
671 u64 bp_offset = 0, cur_inum = U64_MAX;
674 bch2_bkey_buf_init(&sk);
675 bch2_trans_init(&trans, c, 0, 0);
677 bch2_trans_iter_init(&trans, &iter, BTREE_ID_alloc,
678 bucket, BTREE_ITER_CACHED);
679 ret = lockrestart_do(&trans,
680 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
681 bch2_trans_iter_exit(&trans, &iter);
684 a = bch2_alloc_to_v4(k, &a_convert);
685 dirty_sectors = a->dirty_sectors;
686 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
689 while (!(ret = move_ratelimit(&trans, ctxt))) {
690 bch2_trans_begin(&trans);
692 ret = bch2_get_next_backpointer(&trans, bucket, gen,
695 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
699 if (bp_offset == U64_MAX)
703 const struct bch_extent_ptr *ptr;
707 k = bch2_backpointer_get_key(&trans, &iter,
708 bucket, bp_offset, bp);
710 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
717 bch2_bkey_buf_reassemble(&sk, c, k);
718 k = bkey_i_to_s_c(sk.k);
720 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
722 bch2_trans_iter_exit(&trans, &iter);
726 data_opts = _data_opts;
727 data_opts.target = io_opts.background_target;
728 data_opts.rewrite_ptrs = 0;
730 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
731 if (ptr->dev == bucket.inode)
732 data_opts.rewrite_ptrs |= 1U << i;
736 ret = bch2_move_extent(&trans, &iter, ctxt, io_opts,
737 bp.btree_id, k, data_opts);
738 bch2_trans_iter_exit(&trans, &iter);
740 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
742 if (ret == -ENOMEM) {
743 /* memory allocation failure, wait for some IO to finish */
744 bch2_move_ctxt_wait_for_io(ctxt, &trans);
751 bch2_ratelimit_increment(ctxt->rate, k.k->size);
752 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
756 b = bch2_backpointer_get_node(&trans, &iter,
757 bucket, bp_offset, bp);
758 ret = PTR_ERR_OR_ZERO(b);
759 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
761 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
768 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0);
769 bch2_trans_iter_exit(&trans, &iter);
771 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
777 bch2_ratelimit_increment(ctxt->rate,
778 c->opts.btree_node_size >> 9);
779 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
780 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
786 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, ret);
788 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && gen >= 0) {
789 bch2_trans_unlock(&trans);
790 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
791 closure_sync(&ctxt->cl);
792 if (!ctxt->write_error)
793 lockrestart_do(&trans, verify_bucket_evacuated(&trans, bucket, gen));
796 bch2_trans_exit(&trans);
797 bch2_bkey_buf_exit(&sk, c);
801 int bch2_evacuate_bucket(struct bch_fs *c,
802 struct bpos bucket, int gen,
803 struct data_update_opts data_opts,
804 struct bch_ratelimit *rate,
805 struct bch_move_stats *stats,
806 struct write_point_specifier wp,
809 struct moving_context ctxt;
812 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
813 ret = __bch2_evacuate_bucket(&ctxt, bucket, gen, data_opts);
814 bch2_moving_ctxt_exit(&ctxt);
819 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
820 struct btree *, struct bch_io_opts *,
821 struct data_update_opts *);
823 static int bch2_move_btree(struct bch_fs *c,
824 enum btree_id start_btree_id, struct bpos start_pos,
825 enum btree_id end_btree_id, struct bpos end_pos,
826 move_btree_pred pred, void *arg,
827 struct bch_move_stats *stats)
829 bool kthread = (current->flags & PF_KTHREAD) != 0;
830 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
831 struct btree_trans trans;
832 struct btree_iter iter;
835 struct data_update_opts data_opts;
838 bch2_trans_init(&trans, c, 0, 0);
839 progress_list_add(c, stats);
841 stats->data_type = BCH_DATA_btree;
843 for (id = start_btree_id;
844 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
846 stats->btree_id = id;
848 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
849 BTREE_ITER_PREFETCH);
852 while (bch2_trans_begin(&trans),
853 (b = bch2_btree_iter_peek_node(&iter)) &&
854 !(ret = PTR_ERR_OR_ZERO(b))) {
855 if (kthread && kthread_should_stop())
858 if ((cmp_int(id, end_btree_id) ?:
859 bpos_cmp(b->key.k.p, end_pos)) > 0)
862 stats->pos = iter.pos;
864 if (!pred(c, arg, b, &io_opts, &data_opts))
867 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
868 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
873 bch2_btree_iter_next_node(&iter);
875 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
878 bch2_trans_iter_exit(&trans, &iter);
880 if (kthread && kthread_should_stop())
884 bch2_trans_exit(&trans);
887 bch_err(c, "error in %s(): %s", __func__, bch2_err_str(ret));
889 bch2_btree_interior_updates_flush(c);
891 progress_list_del(c, stats);
895 static bool rereplicate_pred(struct bch_fs *c, void *arg,
897 struct bch_io_opts *io_opts,
898 struct data_update_opts *data_opts)
900 unsigned nr_good = bch2_bkey_durability(c, k);
901 unsigned replicas = bkey_is_btree_ptr(k.k)
902 ? c->opts.metadata_replicas
903 : io_opts->data_replicas;
905 if (!nr_good || nr_good >= replicas)
908 data_opts->target = 0;
909 data_opts->extra_replicas = replicas - nr_good;
910 data_opts->btree_insert_flags = 0;
914 static bool migrate_pred(struct bch_fs *c, void *arg,
916 struct bch_io_opts *io_opts,
917 struct data_update_opts *data_opts)
919 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
920 const struct bch_extent_ptr *ptr;
921 struct bch_ioctl_data *op = arg;
924 data_opts->rewrite_ptrs = 0;
925 data_opts->target = 0;
926 data_opts->extra_replicas = 0;
927 data_opts->btree_insert_flags = 0;
929 bkey_for_each_ptr(ptrs, ptr) {
930 if (ptr->dev == op->migrate.dev)
931 data_opts->rewrite_ptrs |= 1U << i;
935 return data_opts->rewrite_ptrs != 0;
938 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
940 struct bch_io_opts *io_opts,
941 struct data_update_opts *data_opts)
943 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
946 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
948 struct bch_io_opts *io_opts,
949 struct data_update_opts *data_opts)
951 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
954 static bool bformat_needs_redo(struct bkey_format *f)
958 for (i = 0; i < f->nr_fields; i++) {
959 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
960 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
961 u64 field_offset = le64_to_cpu(f->field_offset[i]);
963 if (f->bits_per_field[i] > unpacked_bits)
966 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
969 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
978 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
980 struct bch_io_opts *io_opts,
981 struct data_update_opts *data_opts)
983 if (b->version_ondisk != c->sb.version ||
984 btree_node_need_rewrite(b) ||
985 bformat_needs_redo(&b->format)) {
986 data_opts->target = 0;
987 data_opts->extra_replicas = 0;
988 data_opts->btree_insert_flags = 0;
995 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
999 ret = bch2_move_btree(c,
1001 BTREE_ID_NR, SPOS_MAX,
1002 rewrite_old_nodes_pred, c, stats);
1004 mutex_lock(&c->sb_lock);
1005 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1006 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1007 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1008 bch2_write_super(c);
1009 mutex_unlock(&c->sb_lock);
1015 int bch2_data_job(struct bch_fs *c,
1016 struct bch_move_stats *stats,
1017 struct bch_ioctl_data op)
1022 case BCH_DATA_OP_REREPLICATE:
1023 bch2_move_stats_init(stats, "rereplicate");
1024 stats->data_type = BCH_DATA_journal;
1025 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1027 ret = bch2_move_btree(c,
1028 op.start_btree, op.start_pos,
1029 op.end_btree, op.end_pos,
1030 rereplicate_btree_pred, c, stats) ?: ret;
1031 ret = bch2_replicas_gc2(c) ?: ret;
1033 ret = bch2_move_data(c,
1034 op.start_btree, op.start_pos,
1035 op.end_btree, op.end_pos,
1038 writepoint_hashed((unsigned long) current),
1040 rereplicate_pred, c) ?: ret;
1041 ret = bch2_replicas_gc2(c) ?: ret;
1043 case BCH_DATA_OP_MIGRATE:
1044 if (op.migrate.dev >= c->sb.nr_devices)
1047 bch2_move_stats_init(stats, "migrate");
1048 stats->data_type = BCH_DATA_journal;
1049 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1051 ret = bch2_move_btree(c,
1052 op.start_btree, op.start_pos,
1053 op.end_btree, op.end_pos,
1054 migrate_btree_pred, &op, stats) ?: ret;
1055 ret = bch2_replicas_gc2(c) ?: ret;
1057 ret = bch2_move_data(c,
1058 op.start_btree, op.start_pos,
1059 op.end_btree, op.end_pos,
1062 writepoint_hashed((unsigned long) current),
1064 migrate_pred, &op) ?: ret;
1065 ret = bch2_replicas_gc2(c) ?: ret;
1067 case BCH_DATA_OP_REWRITE_OLD_NODES:
1068 bch2_move_stats_init(stats, "rewrite_old_nodes");
1069 ret = bch2_scan_old_btree_nodes(c, stats);