1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
5 #include "alloc_foreground.h"
6 #include "backpointers.h"
9 #include "btree_update.h"
10 #include "btree_update_interior.h"
11 #include "btree_write_buffer.h"
12 #include "disk_groups.h"
18 #include "journal_reclaim.h"
25 #include <linux/ioprio.h>
26 #include <linux/kthread.h>
28 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k)
30 if (trace_move_extent_enabled()) {
31 struct printbuf buf = PRINTBUF;
33 bch2_bkey_val_to_text(&buf, c, k);
34 trace_move_extent(c, buf.buf);
39 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
41 if (trace_move_extent_read_enabled()) {
42 struct printbuf buf = PRINTBUF;
44 bch2_bkey_val_to_text(&buf, c, k);
45 trace_move_extent_read(c, buf.buf);
50 static void trace_move_extent_alloc_mem_fail2(struct bch_fs *c, struct bkey_s_c k)
52 if (trace_move_extent_alloc_mem_fail_enabled()) {
53 struct printbuf buf = PRINTBUF;
55 bch2_bkey_val_to_text(&buf, c, k);
56 trace_move_extent_alloc_mem_fail(c, buf.buf);
61 static void progress_list_add(struct bch_fs *c, struct bch_move_stats *stats)
63 mutex_lock(&c->data_progress_lock);
64 list_add(&stats->list, &c->data_progress_list);
65 mutex_unlock(&c->data_progress_lock);
68 static void progress_list_del(struct bch_fs *c, struct bch_move_stats *stats)
70 mutex_lock(&c->data_progress_lock);
71 list_del(&stats->list);
72 mutex_unlock(&c->data_progress_lock);
76 struct list_head read_list;
77 struct list_head io_list;
78 struct move_bucket_in_flight *b;
82 unsigned read_sectors;
83 unsigned write_sectors;
85 struct bch_read_bio rbio;
87 struct data_update write;
88 /* Must be last since it is variable size */
89 struct bio_vec bi_inline_vecs[0];
92 static void move_free(struct moving_io *io)
94 struct moving_context *ctxt = io->write.ctxt;
97 atomic_dec(&io->b->count);
99 bch2_data_update_exit(&io->write);
101 mutex_lock(&ctxt->lock);
102 list_del(&io->io_list);
103 wake_up(&ctxt->wait);
104 mutex_unlock(&ctxt->lock);
109 static void move_write_done(struct bch_write_op *op)
111 struct moving_io *io = container_of(op, struct moving_io, write.op);
112 struct moving_context *ctxt = io->write.ctxt;
114 if (io->write.op.error)
115 ctxt->write_error = true;
117 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
118 atomic_dec(&io->write.ctxt->write_ios);
120 closure_put(&ctxt->cl);
123 static void move_write(struct moving_io *io)
125 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
130 closure_get(&io->write.ctxt->cl);
131 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
132 atomic_inc(&io->write.ctxt->write_ios);
134 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
137 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
139 struct moving_io *io =
140 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
142 return io && io->read_completed ? io : NULL;
145 static void move_read_endio(struct bio *bio)
147 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
148 struct moving_context *ctxt = io->write.ctxt;
150 atomic_sub(io->read_sectors, &ctxt->read_sectors);
151 atomic_dec(&ctxt->read_ios);
152 io->read_completed = true;
154 wake_up(&ctxt->wait);
155 closure_put(&ctxt->cl);
158 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt,
159 struct btree_trans *trans)
161 struct moving_io *io;
164 bch2_trans_unlock(trans);
166 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
167 list_del(&io->read_list);
172 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
173 struct btree_trans *trans)
175 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
177 move_ctxt_wait_event(ctxt, trans,
178 !atomic_read(&ctxt->write_sectors) ||
179 atomic_read(&ctxt->write_sectors) != sectors_pending);
182 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
184 struct bch_fs *c = ctxt->c;
186 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
187 closure_sync(&ctxt->cl);
189 EBUG_ON(atomic_read(&ctxt->write_sectors));
190 EBUG_ON(atomic_read(&ctxt->write_ios));
191 EBUG_ON(atomic_read(&ctxt->read_sectors));
192 EBUG_ON(atomic_read(&ctxt->read_ios));
195 progress_list_del(c, ctxt->stats);
197 atomic64_read(&ctxt->stats->sectors_moved),
198 atomic64_read(&ctxt->stats->keys_moved));
201 mutex_lock(&c->moving_context_lock);
202 list_del(&ctxt->list);
203 mutex_unlock(&c->moving_context_lock);
206 void bch2_moving_ctxt_init(struct moving_context *ctxt,
208 struct bch_ratelimit *rate,
209 struct bch_move_stats *stats,
210 struct write_point_specifier wp,
213 memset(ctxt, 0, sizeof(*ctxt));
216 ctxt->fn = (void *) _RET_IP_;
220 ctxt->wait_on_copygc = wait_on_copygc;
222 closure_init_stack(&ctxt->cl);
224 mutex_init(&ctxt->lock);
225 INIT_LIST_HEAD(&ctxt->reads);
226 INIT_LIST_HEAD(&ctxt->ios);
227 init_waitqueue_head(&ctxt->wait);
229 mutex_lock(&c->moving_context_lock);
230 list_add(&ctxt->list, &c->moving_context_list);
231 mutex_unlock(&c->moving_context_lock);
234 progress_list_add(c, stats);
235 stats->data_type = BCH_DATA_user;
239 void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
241 memset(stats, 0, sizeof(*stats));
242 scnprintf(stats->name, sizeof(stats->name), "%s", name);
245 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
246 struct btree_iter *iter,
248 struct data_update_opts data_opts)
250 struct bch_fs *c = trans->c;
254 n = bch2_bkey_make_mut_noupdate(trans, k);
255 ret = PTR_ERR_OR_ZERO(n);
259 while (data_opts.kill_ptrs) {
260 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
261 struct bch_extent_ptr *ptr;
263 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
264 data_opts.kill_ptrs ^= 1U << drop;
268 * If the new extent no longer has any pointers, bch2_extent_normalize()
269 * will do the appropriate thing with it (turning it into a
270 * KEY_TYPE_error key, or just a discard if it was a cached extent)
272 bch2_extent_normalize(c, bkey_i_to_s(n));
275 * Since we're not inserting through an extent iterator
276 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
277 * we aren't using the extent overwrite path to delete, we're
278 * just using the normal key deletion path:
280 if (bkey_deleted(&n->k))
283 return bch2_trans_relock(trans) ?:
284 bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
285 bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
288 static int bch2_move_extent(struct btree_trans *trans,
289 struct btree_iter *iter,
290 struct moving_context *ctxt,
291 struct move_bucket_in_flight *bucket_in_flight,
292 struct bch_io_opts io_opts,
293 enum btree_id btree_id,
295 struct data_update_opts data_opts)
297 struct bch_fs *c = trans->c;
298 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
299 struct moving_io *io;
300 const union bch_extent_entry *entry;
301 struct extent_ptr_decoded p;
302 unsigned sectors = k.k->size, pages;
305 trace_move_extent2(c, k);
307 bch2_data_update_opts_normalize(k, &data_opts);
309 if (!data_opts.rewrite_ptrs &&
310 !data_opts.extra_replicas) {
311 if (data_opts.kill_ptrs)
312 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
317 * Before memory allocations & taking nocow locks in
318 * bch2_data_update_init():
320 bch2_trans_unlock(trans);
322 /* write path might have to decompress data: */
323 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
324 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
326 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
327 io = kzalloc(sizeof(struct moving_io) +
328 sizeof(struct bio_vec) * pages, GFP_KERNEL);
332 INIT_LIST_HEAD(&io->io_list);
333 io->write.ctxt = ctxt;
334 io->read_sectors = k.k->size;
335 io->write_sectors = k.k->size;
337 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
338 bio_set_prio(&io->write.op.wbio.bio,
339 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
341 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
346 io->rbio.opts = io_opts;
347 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
348 io->rbio.bio.bi_vcnt = pages;
349 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
350 io->rbio.bio.bi_iter.bi_size = sectors << 9;
352 io->rbio.bio.bi_opf = REQ_OP_READ;
353 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
354 io->rbio.bio.bi_end_io = move_read_endio;
356 ret = bch2_data_update_init(trans, ctxt, &io->write, ctxt->wp,
357 io_opts, data_opts, btree_id, k);
358 if (ret && ret != -BCH_ERR_unwritten_extent_update)
361 if (ret == -BCH_ERR_unwritten_extent_update) {
362 bch2_update_unwritten_extent(trans, &io->write);
369 io->write.ctxt = ctxt;
370 io->write.op.end_io = move_write_done;
373 atomic64_inc(&ctxt->stats->keys_moved);
374 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
377 if (bucket_in_flight) {
378 io->b = bucket_in_flight;
379 atomic_inc(&io->b->count);
382 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
383 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
384 trace_move_extent_read2(c, k);
386 mutex_lock(&ctxt->lock);
387 atomic_add(io->read_sectors, &ctxt->read_sectors);
388 atomic_inc(&ctxt->read_ios);
390 list_add_tail(&io->read_list, &ctxt->reads);
391 list_add_tail(&io->io_list, &ctxt->ios);
392 mutex_unlock(&ctxt->lock);
395 * dropped by move_read_endio() - guards against use after free of
396 * ctxt when doing wakeup
398 closure_get(&ctxt->cl);
399 bch2_read_extent(trans, &io->rbio,
403 BCH_READ_LAST_FRAGMENT);
406 bio_free_pages(&io->write.op.wbio.bio);
410 this_cpu_inc(c->counters[BCH_COUNTER_move_extent_alloc_mem_fail]);
411 trace_move_extent_alloc_mem_fail2(c, k);
415 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
416 struct bch_inode_unpacked *inode)
418 struct btree_iter iter;
422 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
423 BTREE_ITER_ALL_SNAPSHOTS);
424 k = bch2_btree_iter_peek(&iter);
429 if (!k.k || !bkey_eq(k.k->p, pos)) {
430 ret = -BCH_ERR_ENOENT_inode;
434 ret = bkey_is_inode(k.k) ? 0 : -EIO;
438 ret = bch2_inode_unpack(k, inode);
442 bch2_trans_iter_exit(trans, &iter);
446 static int move_ratelimit(struct btree_trans *trans,
447 struct moving_context *ctxt)
449 struct bch_fs *c = trans->c;
452 if (ctxt->wait_on_copygc) {
453 bch2_trans_unlock(trans);
454 wait_event_killable(c->copygc_running_wq,
455 !c->copygc_running ||
456 kthread_should_stop());
460 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
463 bch2_trans_unlock(trans);
464 set_current_state(TASK_INTERRUPTIBLE);
467 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
468 __set_current_state(TASK_RUNNING);
473 schedule_timeout(delay);
475 if (unlikely(freezing(current))) {
476 move_ctxt_wait_event(ctxt, trans, list_empty(&ctxt->reads));
482 * XXX: these limits really ought to be per device, SSDs and hard drives
483 * will want different limits
485 move_ctxt_wait_event(ctxt, trans,
486 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
487 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
488 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
489 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
494 static int move_get_io_opts(struct btree_trans *trans,
495 struct bch_io_opts *io_opts,
496 struct bkey_s_c k, u64 *cur_inum)
498 struct bch_inode_unpacked inode;
501 if (*cur_inum == k.k->p.inode)
504 ret = lookup_inode(trans,
505 SPOS(0, k.k->p.inode, k.k->p.snapshot),
507 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
511 bch2_inode_opts_get(io_opts, trans->c, &inode);
513 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
514 *cur_inum = k.k->p.inode;
518 static int __bch2_move_data(struct moving_context *ctxt,
521 move_pred_fn pred, void *arg,
522 enum btree_id btree_id)
524 struct bch_fs *c = ctxt->c;
525 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
527 struct btree_trans trans;
528 struct btree_iter iter;
530 struct data_update_opts data_opts;
531 u64 cur_inum = U64_MAX;
534 bch2_bkey_buf_init(&sk);
535 bch2_trans_init(&trans, c, 0, 0);
538 ctxt->stats->data_type = BCH_DATA_user;
539 ctxt->stats->btree_id = btree_id;
540 ctxt->stats->pos = start;
543 bch2_trans_iter_init(&trans, &iter, btree_id, start,
545 BTREE_ITER_ALL_SNAPSHOTS);
548 bch2_ratelimit_reset(ctxt->rate);
550 while (!move_ratelimit(&trans, ctxt)) {
551 bch2_trans_begin(&trans);
553 k = bch2_btree_iter_peek(&iter);
558 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
563 if (bkey_ge(bkey_start_pos(k.k), end))
567 ctxt->stats->pos = iter.pos;
569 if (!bkey_extent_is_direct_data(k.k))
572 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
576 memset(&data_opts, 0, sizeof(data_opts));
577 if (!pred(c, arg, k, &io_opts, &data_opts))
581 * The iterator gets unlocked by __bch2_read_extent - need to
582 * save a copy of @k elsewhere:
584 bch2_bkey_buf_reassemble(&sk, c, k);
585 k = bkey_i_to_s_c(sk.k);
587 ret2 = bch2_move_extent(&trans, &iter, ctxt, NULL,
588 io_opts, btree_id, k, data_opts);
590 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
593 if (ret2 == -ENOMEM) {
594 /* memory allocation failure, wait for some IO to finish */
595 bch2_move_ctxt_wait_for_io(ctxt, &trans);
599 /* XXX signal failure */
604 bch2_ratelimit_increment(ctxt->rate, k.k->size);
607 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
609 bch2_btree_iter_advance(&iter);
612 bch2_trans_iter_exit(&trans, &iter);
613 bch2_trans_exit(&trans);
614 bch2_bkey_buf_exit(&sk, c);
619 int bch2_move_data(struct bch_fs *c,
620 enum btree_id start_btree_id, struct bpos start_pos,
621 enum btree_id end_btree_id, struct bpos end_pos,
622 struct bch_ratelimit *rate,
623 struct bch_move_stats *stats,
624 struct write_point_specifier wp,
626 move_pred_fn pred, void *arg)
628 struct moving_context ctxt;
632 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
634 for (id = start_btree_id;
635 id <= min_t(unsigned, end_btree_id, btree_id_nr_alive(c) - 1);
637 stats->btree_id = id;
639 if (id != BTREE_ID_extents &&
640 id != BTREE_ID_reflink)
643 if (!bch2_btree_id_root(c, id)->b)
646 ret = __bch2_move_data(&ctxt,
647 id == start_btree_id ? start_pos : POS_MIN,
648 id == end_btree_id ? end_pos : POS_MAX,
654 bch2_moving_ctxt_exit(&ctxt);
659 int __bch2_evacuate_bucket(struct btree_trans *trans,
660 struct moving_context *ctxt,
661 struct move_bucket_in_flight *bucket_in_flight,
662 struct bpos bucket, int gen,
663 struct data_update_opts _data_opts)
665 struct bch_fs *c = ctxt->c;
666 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
667 struct btree_iter iter;
669 struct bch_backpointer bp;
670 struct bch_alloc_v4 a_convert;
671 const struct bch_alloc_v4 *a;
673 struct data_update_opts data_opts;
674 unsigned dirty_sectors, bucket_size;
676 u64 cur_inum = U64_MAX;
677 struct bpos bp_pos = POS_MIN;
680 trace_bucket_evacuate(c, &bucket);
682 bch2_bkey_buf_init(&sk);
685 * We're not run in a context that handles transaction restarts:
687 bch2_trans_begin(trans);
689 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
690 bucket, BTREE_ITER_CACHED);
691 ret = lockrestart_do(trans,
692 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
693 bch2_trans_iter_exit(trans, &iter);
696 bch_err_msg(c, ret, "looking up alloc key");
700 a = bch2_alloc_to_v4(k, &a_convert);
701 dirty_sectors = a->dirty_sectors;
702 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
703 fragmentation = a->fragmentation_lru;
705 ret = bch2_btree_write_buffer_flush(trans);
707 bch_err_msg(c, ret, "flushing btree write buffer");
711 while (!(ret = move_ratelimit(trans, ctxt))) {
712 bch2_trans_begin(trans);
714 ret = bch2_get_next_backpointer(trans, bucket, gen,
717 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
721 if (bkey_eq(bp_pos, POS_MAX))
725 const struct bch_extent_ptr *ptr;
729 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
731 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
738 bch2_bkey_buf_reassemble(&sk, c, k);
739 k = bkey_i_to_s_c(sk.k);
741 ret = move_get_io_opts(trans, &io_opts, k, &cur_inum);
743 bch2_trans_iter_exit(trans, &iter);
747 data_opts = _data_opts;
748 data_opts.target = io_opts.background_target;
749 data_opts.rewrite_ptrs = 0;
751 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
752 if (ptr->dev == bucket.inode) {
753 data_opts.rewrite_ptrs |= 1U << i;
755 bch2_trans_iter_exit(trans, &iter);
762 ret = bch2_move_extent(trans, &iter, ctxt,
764 io_opts, bp.btree_id, k, data_opts);
765 bch2_trans_iter_exit(trans, &iter);
767 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
769 if (ret == -ENOMEM) {
770 /* memory allocation failure, wait for some IO to finish */
771 bch2_move_ctxt_wait_for_io(ctxt, trans);
778 bch2_ratelimit_increment(ctxt->rate, k.k->size);
780 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
784 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
785 ret = PTR_ERR_OR_ZERO(b);
786 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
788 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
795 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
796 bch2_trans_iter_exit(trans, &iter);
798 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
804 bch2_ratelimit_increment(ctxt->rate,
805 c->opts.btree_node_size >> 9);
807 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
808 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
812 bp_pos = bpos_nosnap_successor(bp_pos);
815 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
817 bch2_bkey_buf_exit(&sk, c);
821 int bch2_evacuate_bucket(struct bch_fs *c,
822 struct bpos bucket, int gen,
823 struct data_update_opts data_opts,
824 struct bch_ratelimit *rate,
825 struct bch_move_stats *stats,
826 struct write_point_specifier wp,
829 struct btree_trans trans;
830 struct moving_context ctxt;
833 bch2_trans_init(&trans, c, 0, 0);
834 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
835 ret = __bch2_evacuate_bucket(&trans, &ctxt, NULL, bucket, gen, data_opts);
836 bch2_moving_ctxt_exit(&ctxt);
837 bch2_trans_exit(&trans);
842 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
843 struct btree *, struct bch_io_opts *,
844 struct data_update_opts *);
846 static int bch2_move_btree(struct bch_fs *c,
847 enum btree_id start_btree_id, struct bpos start_pos,
848 enum btree_id end_btree_id, struct bpos end_pos,
849 move_btree_pred pred, void *arg,
850 struct bch_move_stats *stats)
852 bool kthread = (current->flags & PF_KTHREAD) != 0;
853 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
854 struct btree_trans trans;
855 struct btree_iter iter;
858 struct data_update_opts data_opts;
861 bch2_trans_init(&trans, c, 0, 0);
862 progress_list_add(c, stats);
864 stats->data_type = BCH_DATA_btree;
866 for (id = start_btree_id;
867 id <= min_t(unsigned, end_btree_id, btree_id_nr_alive(c) - 1);
869 stats->btree_id = id;
871 if (!bch2_btree_id_root(c, id)->b)
874 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
875 BTREE_ITER_PREFETCH);
878 while (bch2_trans_begin(&trans),
879 (b = bch2_btree_iter_peek_node(&iter)) &&
880 !(ret = PTR_ERR_OR_ZERO(b))) {
881 if (kthread && kthread_should_stop())
884 if ((cmp_int(id, end_btree_id) ?:
885 bpos_cmp(b->key.k.p, end_pos)) > 0)
888 stats->pos = iter.pos;
890 if (!pred(c, arg, b, &io_opts, &data_opts))
893 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
894 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
899 bch2_btree_iter_next_node(&iter);
901 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
904 bch2_trans_iter_exit(&trans, &iter);
906 if (kthread && kthread_should_stop())
910 bch2_trans_exit(&trans);
915 bch2_btree_interior_updates_flush(c);
917 progress_list_del(c, stats);
921 static bool rereplicate_pred(struct bch_fs *c, void *arg,
923 struct bch_io_opts *io_opts,
924 struct data_update_opts *data_opts)
926 unsigned nr_good = bch2_bkey_durability(c, k);
927 unsigned replicas = bkey_is_btree_ptr(k.k)
928 ? c->opts.metadata_replicas
929 : io_opts->data_replicas;
931 if (!nr_good || nr_good >= replicas)
934 data_opts->target = 0;
935 data_opts->extra_replicas = replicas - nr_good;
936 data_opts->btree_insert_flags = 0;
940 static bool migrate_pred(struct bch_fs *c, void *arg,
942 struct bch_io_opts *io_opts,
943 struct data_update_opts *data_opts)
945 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
946 const struct bch_extent_ptr *ptr;
947 struct bch_ioctl_data *op = arg;
950 data_opts->rewrite_ptrs = 0;
951 data_opts->target = 0;
952 data_opts->extra_replicas = 0;
953 data_opts->btree_insert_flags = 0;
955 bkey_for_each_ptr(ptrs, ptr) {
956 if (ptr->dev == op->migrate.dev)
957 data_opts->rewrite_ptrs |= 1U << i;
961 return data_opts->rewrite_ptrs != 0;
964 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
966 struct bch_io_opts *io_opts,
967 struct data_update_opts *data_opts)
969 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
972 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
974 struct bch_io_opts *io_opts,
975 struct data_update_opts *data_opts)
977 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
980 static bool bformat_needs_redo(struct bkey_format *f)
984 for (i = 0; i < f->nr_fields; i++) {
985 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
986 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
987 u64 field_offset = le64_to_cpu(f->field_offset[i]);
989 if (f->bits_per_field[i] > unpacked_bits)
992 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
995 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1004 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1006 struct bch_io_opts *io_opts,
1007 struct data_update_opts *data_opts)
1009 if (b->version_ondisk != c->sb.version ||
1010 btree_node_need_rewrite(b) ||
1011 bformat_needs_redo(&b->format)) {
1012 data_opts->target = 0;
1013 data_opts->extra_replicas = 0;
1014 data_opts->btree_insert_flags = 0;
1021 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1025 ret = bch2_move_btree(c,
1027 BTREE_ID_NR, SPOS_MAX,
1028 rewrite_old_nodes_pred, c, stats);
1030 mutex_lock(&c->sb_lock);
1031 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1032 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1033 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1034 bch2_write_super(c);
1035 mutex_unlock(&c->sb_lock);
1043 int bch2_data_job(struct bch_fs *c,
1044 struct bch_move_stats *stats,
1045 struct bch_ioctl_data op)
1050 case BCH_DATA_OP_REREPLICATE:
1051 bch2_move_stats_init(stats, "rereplicate");
1052 stats->data_type = BCH_DATA_journal;
1053 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1055 ret = bch2_move_btree(c,
1056 op.start_btree, op.start_pos,
1057 op.end_btree, op.end_pos,
1058 rereplicate_btree_pred, c, stats) ?: ret;
1059 ret = bch2_replicas_gc2(c) ?: ret;
1061 ret = bch2_move_data(c,
1062 op.start_btree, op.start_pos,
1063 op.end_btree, op.end_pos,
1066 writepoint_hashed((unsigned long) current),
1068 rereplicate_pred, c) ?: ret;
1069 ret = bch2_replicas_gc2(c) ?: ret;
1071 case BCH_DATA_OP_MIGRATE:
1072 if (op.migrate.dev >= c->sb.nr_devices)
1075 bch2_move_stats_init(stats, "migrate");
1076 stats->data_type = BCH_DATA_journal;
1077 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1079 ret = bch2_move_btree(c,
1080 op.start_btree, op.start_pos,
1081 op.end_btree, op.end_pos,
1082 migrate_btree_pred, &op, stats) ?: ret;
1083 ret = bch2_replicas_gc2(c) ?: ret;
1085 ret = bch2_move_data(c,
1086 op.start_btree, op.start_pos,
1087 op.end_btree, op.end_pos,
1090 writepoint_hashed((unsigned long) current),
1092 migrate_pred, &op) ?: ret;
1093 ret = bch2_replicas_gc2(c) ?: ret;
1095 case BCH_DATA_OP_REWRITE_OLD_NODES:
1096 bch2_move_stats_init(stats, "rewrite_old_nodes");
1097 ret = bch2_scan_old_btree_nodes(c, stats);
1106 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1108 struct bch_move_stats *stats = ctxt->stats;
1109 struct moving_io *io;
1111 prt_printf(out, "%s (%ps):", stats->name, ctxt->fn);
1114 prt_printf(out, " data type %s btree_id %s position: ",
1115 bch2_data_types[stats->data_type],
1116 bch2_btree_ids[stats->btree_id]);
1117 bch2_bpos_to_text(out, stats->pos);
1119 printbuf_indent_add(out, 2);
1121 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1122 atomic_read(&ctxt->read_ios),
1123 c->opts.move_ios_in_flight,
1124 atomic_read(&ctxt->read_sectors),
1125 c->opts.move_bytes_in_flight >> 9);
1128 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1129 atomic_read(&ctxt->write_ios),
1130 c->opts.move_ios_in_flight,
1131 atomic_read(&ctxt->write_sectors),
1132 c->opts.move_bytes_in_flight >> 9);
1135 printbuf_indent_add(out, 2);
1137 mutex_lock(&ctxt->lock);
1138 list_for_each_entry(io, &ctxt->ios, io_list)
1139 bch2_write_op_to_text(out, &io->write.op);
1140 mutex_unlock(&ctxt->lock);
1142 printbuf_indent_sub(out, 4);
1145 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1147 struct moving_context *ctxt;
1149 mutex_lock(&c->moving_context_lock);
1150 list_for_each_entry(ctxt, &c->moving_context_list, list)
1151 bch2_moving_ctxt_to_text(out, c, ctxt);
1152 mutex_unlock(&c->moving_context_lock);
1155 void bch2_fs_move_init(struct bch_fs *c)
1157 INIT_LIST_HEAD(&c->moving_context_list);
1158 mutex_init(&c->moving_context_lock);
1160 INIT_LIST_HEAD(&c->data_progress_list);
1161 mutex_init(&c->data_progress_lock);