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"
19 #include "journal_reclaim.h"
26 #include <linux/ioprio.h>
27 #include <linux/kthread.h>
29 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k)
31 if (trace_move_extent_enabled()) {
32 struct printbuf buf = PRINTBUF;
34 bch2_bkey_val_to_text(&buf, c, k);
35 trace_move_extent(c, buf.buf);
40 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
42 if (trace_move_extent_read_enabled()) {
43 struct printbuf buf = PRINTBUF;
45 bch2_bkey_val_to_text(&buf, c, k);
46 trace_move_extent_read(c, buf.buf);
51 static void trace_move_extent_alloc_mem_fail2(struct bch_fs *c, struct bkey_s_c k)
53 if (trace_move_extent_alloc_mem_fail_enabled()) {
54 struct printbuf buf = PRINTBUF;
56 bch2_bkey_val_to_text(&buf, c, k);
57 trace_move_extent_alloc_mem_fail(c, buf.buf);
62 static void progress_list_add(struct bch_fs *c, struct bch_move_stats *stats)
64 mutex_lock(&c->data_progress_lock);
65 list_add(&stats->list, &c->data_progress_list);
66 mutex_unlock(&c->data_progress_lock);
69 static void progress_list_del(struct bch_fs *c, struct bch_move_stats *stats)
71 mutex_lock(&c->data_progress_lock);
72 list_del(&stats->list);
73 mutex_unlock(&c->data_progress_lock);
77 struct list_head read_list;
78 struct list_head io_list;
79 struct move_bucket_in_flight *b;
83 unsigned read_sectors;
84 unsigned write_sectors;
86 struct bch_read_bio rbio;
88 struct data_update write;
89 /* Must be last since it is variable size */
90 struct bio_vec bi_inline_vecs[0];
93 static void move_free(struct moving_io *io)
95 struct moving_context *ctxt = io->write.ctxt;
98 atomic_dec(&io->b->count);
100 bch2_data_update_exit(&io->write);
102 mutex_lock(&ctxt->lock);
103 list_del(&io->io_list);
104 wake_up(&ctxt->wait);
105 mutex_unlock(&ctxt->lock);
110 static void move_write_done(struct bch_write_op *op)
112 struct moving_io *io = container_of(op, struct moving_io, write.op);
113 struct moving_context *ctxt = io->write.ctxt;
115 if (io->write.op.error)
116 ctxt->write_error = true;
118 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
119 atomic_dec(&io->write.ctxt->write_ios);
121 closure_put(&ctxt->cl);
124 static void move_write(struct moving_io *io)
126 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
131 closure_get(&io->write.ctxt->cl);
132 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
133 atomic_inc(&io->write.ctxt->write_ios);
135 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
138 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
140 struct moving_io *io =
141 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
143 return io && io->read_completed ? io : NULL;
146 static void move_read_endio(struct bio *bio)
148 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
149 struct moving_context *ctxt = io->write.ctxt;
151 atomic_sub(io->read_sectors, &ctxt->read_sectors);
152 atomic_dec(&ctxt->read_ios);
153 io->read_completed = true;
155 wake_up(&ctxt->wait);
156 closure_put(&ctxt->cl);
159 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt,
160 struct btree_trans *trans)
162 struct moving_io *io;
165 bch2_trans_unlock(trans);
167 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
168 list_del(&io->read_list);
173 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
174 struct btree_trans *trans)
176 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
178 move_ctxt_wait_event(ctxt, trans,
179 !atomic_read(&ctxt->write_sectors) ||
180 atomic_read(&ctxt->write_sectors) != sectors_pending);
183 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
185 struct bch_fs *c = ctxt->c;
187 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
188 closure_sync(&ctxt->cl);
190 EBUG_ON(atomic_read(&ctxt->write_sectors));
191 EBUG_ON(atomic_read(&ctxt->write_ios));
192 EBUG_ON(atomic_read(&ctxt->read_sectors));
193 EBUG_ON(atomic_read(&ctxt->read_ios));
196 progress_list_del(c, ctxt->stats);
198 atomic64_read(&ctxt->stats->sectors_moved),
199 atomic64_read(&ctxt->stats->keys_moved));
202 mutex_lock(&c->moving_context_lock);
203 list_del(&ctxt->list);
204 mutex_unlock(&c->moving_context_lock);
207 void bch2_moving_ctxt_init(struct moving_context *ctxt,
209 struct bch_ratelimit *rate,
210 struct bch_move_stats *stats,
211 struct write_point_specifier wp,
214 memset(ctxt, 0, sizeof(*ctxt));
217 ctxt->fn = (void *) _RET_IP_;
221 ctxt->wait_on_copygc = wait_on_copygc;
223 closure_init_stack(&ctxt->cl);
225 mutex_init(&ctxt->lock);
226 INIT_LIST_HEAD(&ctxt->reads);
227 INIT_LIST_HEAD(&ctxt->ios);
228 init_waitqueue_head(&ctxt->wait);
230 mutex_lock(&c->moving_context_lock);
231 list_add(&ctxt->list, &c->moving_context_list);
232 mutex_unlock(&c->moving_context_lock);
235 progress_list_add(c, stats);
236 stats->data_type = BCH_DATA_user;
240 void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
242 memset(stats, 0, sizeof(*stats));
243 scnprintf(stats->name, sizeof(stats->name), "%s", name);
246 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
247 struct btree_iter *iter,
249 struct data_update_opts data_opts)
251 struct bch_fs *c = trans->c;
255 n = bch2_bkey_make_mut_noupdate(trans, k);
256 ret = PTR_ERR_OR_ZERO(n);
260 while (data_opts.kill_ptrs) {
261 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
262 struct bch_extent_ptr *ptr;
264 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
265 data_opts.kill_ptrs ^= 1U << drop;
269 * If the new extent no longer has any pointers, bch2_extent_normalize()
270 * will do the appropriate thing with it (turning it into a
271 * KEY_TYPE_error key, or just a discard if it was a cached extent)
273 bch2_extent_normalize(c, bkey_i_to_s(n));
276 * Since we're not inserting through an extent iterator
277 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
278 * we aren't using the extent overwrite path to delete, we're
279 * just using the normal key deletion path:
281 if (bkey_deleted(&n->k))
284 return bch2_trans_relock(trans) ?:
285 bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
286 bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
289 static int bch2_move_extent(struct btree_trans *trans,
290 struct btree_iter *iter,
291 struct moving_context *ctxt,
292 struct move_bucket_in_flight *bucket_in_flight,
293 struct bch_io_opts io_opts,
294 enum btree_id btree_id,
296 struct data_update_opts data_opts)
298 struct bch_fs *c = trans->c;
299 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
300 struct moving_io *io;
301 const union bch_extent_entry *entry;
302 struct extent_ptr_decoded p;
303 unsigned sectors = k.k->size, pages;
306 trace_move_extent2(c, k);
308 bch2_data_update_opts_normalize(k, &data_opts);
310 if (!data_opts.rewrite_ptrs &&
311 !data_opts.extra_replicas) {
312 if (data_opts.kill_ptrs)
313 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
318 * Before memory allocations & taking nocow locks in
319 * bch2_data_update_init():
321 bch2_trans_unlock(trans);
323 /* write path might have to decompress data: */
324 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
325 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
327 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
328 io = kzalloc(sizeof(struct moving_io) +
329 sizeof(struct bio_vec) * pages, GFP_KERNEL);
333 INIT_LIST_HEAD(&io->io_list);
334 io->write.ctxt = ctxt;
335 io->read_sectors = k.k->size;
336 io->write_sectors = k.k->size;
338 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
339 bio_set_prio(&io->write.op.wbio.bio,
340 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
342 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
347 io->rbio.opts = io_opts;
348 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
349 io->rbio.bio.bi_vcnt = pages;
350 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
351 io->rbio.bio.bi_iter.bi_size = sectors << 9;
353 io->rbio.bio.bi_opf = REQ_OP_READ;
354 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
355 io->rbio.bio.bi_end_io = move_read_endio;
357 ret = bch2_data_update_init(trans, ctxt, &io->write, ctxt->wp,
358 io_opts, data_opts, btree_id, k);
359 if (ret && ret != -BCH_ERR_unwritten_extent_update)
362 if (ret == -BCH_ERR_unwritten_extent_update) {
363 bch2_update_unwritten_extent(trans, &io->write);
370 io->write.ctxt = ctxt;
371 io->write.op.end_io = move_write_done;
374 atomic64_inc(&ctxt->stats->keys_moved);
375 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
378 if (bucket_in_flight) {
379 io->b = bucket_in_flight;
380 atomic_inc(&io->b->count);
383 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
384 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
385 trace_move_extent_read2(c, k);
387 mutex_lock(&ctxt->lock);
388 atomic_add(io->read_sectors, &ctxt->read_sectors);
389 atomic_inc(&ctxt->read_ios);
391 list_add_tail(&io->read_list, &ctxt->reads);
392 list_add_tail(&io->io_list, &ctxt->ios);
393 mutex_unlock(&ctxt->lock);
396 * dropped by move_read_endio() - guards against use after free of
397 * ctxt when doing wakeup
399 closure_get(&ctxt->cl);
400 bch2_read_extent(trans, &io->rbio,
404 BCH_READ_LAST_FRAGMENT);
407 bio_free_pages(&io->write.op.wbio.bio);
411 this_cpu_inc(c->counters[BCH_COUNTER_move_extent_alloc_mem_fail]);
412 trace_move_extent_alloc_mem_fail2(c, k);
416 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
417 struct bch_inode_unpacked *inode)
419 struct btree_iter iter;
423 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
424 BTREE_ITER_ALL_SNAPSHOTS);
425 k = bch2_btree_iter_peek(&iter);
430 if (!k.k || !bkey_eq(k.k->p, pos)) {
431 ret = -BCH_ERR_ENOENT_inode;
435 ret = bkey_is_inode(k.k) ? 0 : -EIO;
439 ret = bch2_inode_unpack(k, inode);
443 bch2_trans_iter_exit(trans, &iter);
447 static int move_ratelimit(struct btree_trans *trans,
448 struct moving_context *ctxt)
450 struct bch_fs *c = trans->c;
453 if (ctxt->wait_on_copygc) {
454 bch2_trans_unlock(trans);
455 wait_event_killable(c->copygc_running_wq,
456 !c->copygc_running ||
457 kthread_should_stop());
461 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
464 bch2_trans_unlock(trans);
465 set_current_state(TASK_INTERRUPTIBLE);
468 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
469 __set_current_state(TASK_RUNNING);
474 schedule_timeout(delay);
476 if (unlikely(freezing(current))) {
477 move_ctxt_wait_event(ctxt, trans, list_empty(&ctxt->reads));
483 * XXX: these limits really ought to be per device, SSDs and hard drives
484 * will want different limits
486 move_ctxt_wait_event(ctxt, trans,
487 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
488 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
489 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
490 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
495 static int move_get_io_opts(struct btree_trans *trans,
496 struct bch_io_opts *io_opts,
497 struct bkey_s_c k, u64 *cur_inum)
499 struct bch_inode_unpacked inode;
502 if (*cur_inum == k.k->p.inode)
505 ret = lookup_inode(trans,
506 SPOS(0, k.k->p.inode, k.k->p.snapshot),
508 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
512 bch2_inode_opts_get(io_opts, trans->c, &inode);
514 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
515 *cur_inum = k.k->p.inode;
519 static int __bch2_move_data(struct moving_context *ctxt,
522 move_pred_fn pred, void *arg,
523 enum btree_id btree_id)
525 struct bch_fs *c = ctxt->c;
526 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
528 struct btree_trans *trans = bch2_trans_get(c);
529 struct btree_iter iter;
531 struct data_update_opts data_opts;
532 u64 cur_inum = U64_MAX;
535 bch2_bkey_buf_init(&sk);
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_put(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;
728 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
730 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
737 bch2_bkey_buf_reassemble(&sk, c, k);
738 k = bkey_i_to_s_c(sk.k);
740 ret = move_get_io_opts(trans, &io_opts, k, &cur_inum);
742 bch2_trans_iter_exit(trans, &iter);
746 data_opts = _data_opts;
747 data_opts.target = io_opts.background_target;
748 data_opts.rewrite_ptrs = 0;
750 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
751 if (ptr->dev == bucket.inode) {
752 data_opts.rewrite_ptrs |= 1U << i;
754 bch2_trans_iter_exit(trans, &iter);
761 ret = bch2_move_extent(trans, &iter, ctxt,
763 io_opts, bp.btree_id, k, data_opts);
764 bch2_trans_iter_exit(trans, &iter);
766 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
768 if (ret == -ENOMEM) {
769 /* memory allocation failure, wait for some IO to finish */
770 bch2_move_ctxt_wait_for_io(ctxt, trans);
777 bch2_ratelimit_increment(ctxt->rate, k.k->size);
779 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
783 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
784 ret = PTR_ERR_OR_ZERO(b);
785 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
787 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
794 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
795 bch2_trans_iter_exit(trans, &iter);
797 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
803 bch2_ratelimit_increment(ctxt->rate,
804 c->opts.btree_node_size >> 9);
806 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
807 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
811 bp_pos = bpos_nosnap_successor(bp_pos);
814 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
816 bch2_bkey_buf_exit(&sk, c);
820 int bch2_evacuate_bucket(struct bch_fs *c,
821 struct bpos bucket, int gen,
822 struct data_update_opts data_opts,
823 struct bch_ratelimit *rate,
824 struct bch_move_stats *stats,
825 struct write_point_specifier wp,
828 struct btree_trans *trans = bch2_trans_get(c);
829 struct moving_context ctxt;
832 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
833 ret = __bch2_evacuate_bucket(trans, &ctxt, NULL, bucket, gen, data_opts);
834 bch2_moving_ctxt_exit(&ctxt);
835 bch2_trans_put(trans);
840 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
841 struct btree *, struct bch_io_opts *,
842 struct data_update_opts *);
844 static int bch2_move_btree(struct bch_fs *c,
845 enum btree_id start_btree_id, struct bpos start_pos,
846 enum btree_id end_btree_id, struct bpos end_pos,
847 move_btree_pred pred, void *arg,
848 struct bch_move_stats *stats)
850 bool kthread = (current->flags & PF_KTHREAD) != 0;
851 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
852 struct btree_trans *trans = bch2_trans_get(c);
853 struct btree_iter iter;
856 struct data_update_opts data_opts;
859 progress_list_add(c, stats);
861 stats->data_type = BCH_DATA_btree;
863 for (id = start_btree_id;
864 id <= min_t(unsigned, end_btree_id, btree_id_nr_alive(c) - 1);
866 stats->btree_id = id;
868 if (!bch2_btree_id_root(c, id)->b)
871 bch2_trans_node_iter_init(trans, &iter, id, POS_MIN, 0, 0,
872 BTREE_ITER_PREFETCH);
875 while (bch2_trans_begin(trans),
876 (b = bch2_btree_iter_peek_node(&iter)) &&
877 !(ret = PTR_ERR_OR_ZERO(b))) {
878 if (kthread && kthread_should_stop())
881 if ((cmp_int(id, end_btree_id) ?:
882 bpos_cmp(b->key.k.p, end_pos)) > 0)
885 stats->pos = iter.pos;
887 if (!pred(c, arg, b, &io_opts, &data_opts))
890 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
891 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
896 bch2_btree_iter_next_node(&iter);
898 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
901 bch2_trans_iter_exit(trans, &iter);
903 if (kthread && kthread_should_stop())
907 bch2_trans_put(trans);
912 bch2_btree_interior_updates_flush(c);
914 progress_list_del(c, stats);
918 static bool rereplicate_pred(struct bch_fs *c, void *arg,
920 struct bch_io_opts *io_opts,
921 struct data_update_opts *data_opts)
923 unsigned nr_good = bch2_bkey_durability(c, k);
924 unsigned replicas = bkey_is_btree_ptr(k.k)
925 ? c->opts.metadata_replicas
926 : io_opts->data_replicas;
928 if (!nr_good || nr_good >= replicas)
931 data_opts->target = 0;
932 data_opts->extra_replicas = replicas - nr_good;
933 data_opts->btree_insert_flags = 0;
937 static bool migrate_pred(struct bch_fs *c, void *arg,
939 struct bch_io_opts *io_opts,
940 struct data_update_opts *data_opts)
942 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
943 const struct bch_extent_ptr *ptr;
944 struct bch_ioctl_data *op = arg;
947 data_opts->rewrite_ptrs = 0;
948 data_opts->target = 0;
949 data_opts->extra_replicas = 0;
950 data_opts->btree_insert_flags = 0;
952 bkey_for_each_ptr(ptrs, ptr) {
953 if (ptr->dev == op->migrate.dev)
954 data_opts->rewrite_ptrs |= 1U << i;
958 return data_opts->rewrite_ptrs != 0;
961 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
963 struct bch_io_opts *io_opts,
964 struct data_update_opts *data_opts)
966 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
969 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
971 struct bch_io_opts *io_opts,
972 struct data_update_opts *data_opts)
974 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
977 static bool bformat_needs_redo(struct bkey_format *f)
981 for (i = 0; i < f->nr_fields; i++) {
982 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
983 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
984 u64 field_offset = le64_to_cpu(f->field_offset[i]);
986 if (f->bits_per_field[i] > unpacked_bits)
989 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
992 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1001 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1003 struct bch_io_opts *io_opts,
1004 struct data_update_opts *data_opts)
1006 if (b->version_ondisk != c->sb.version ||
1007 btree_node_need_rewrite(b) ||
1008 bformat_needs_redo(&b->format)) {
1009 data_opts->target = 0;
1010 data_opts->extra_replicas = 0;
1011 data_opts->btree_insert_flags = 0;
1018 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1022 ret = bch2_move_btree(c,
1024 BTREE_ID_NR, SPOS_MAX,
1025 rewrite_old_nodes_pred, c, stats);
1027 mutex_lock(&c->sb_lock);
1028 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1029 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1030 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1031 bch2_write_super(c);
1032 mutex_unlock(&c->sb_lock);
1040 int bch2_data_job(struct bch_fs *c,
1041 struct bch_move_stats *stats,
1042 struct bch_ioctl_data op)
1047 case BCH_DATA_OP_REREPLICATE:
1048 bch2_move_stats_init(stats, "rereplicate");
1049 stats->data_type = BCH_DATA_journal;
1050 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1052 ret = bch2_move_btree(c,
1053 op.start_btree, op.start_pos,
1054 op.end_btree, op.end_pos,
1055 rereplicate_btree_pred, c, stats) ?: ret;
1056 ret = bch2_replicas_gc2(c) ?: ret;
1058 ret = bch2_move_data(c,
1059 op.start_btree, op.start_pos,
1060 op.end_btree, op.end_pos,
1063 writepoint_hashed((unsigned long) current),
1065 rereplicate_pred, c) ?: ret;
1066 ret = bch2_replicas_gc2(c) ?: ret;
1068 case BCH_DATA_OP_MIGRATE:
1069 if (op.migrate.dev >= c->sb.nr_devices)
1072 bch2_move_stats_init(stats, "migrate");
1073 stats->data_type = BCH_DATA_journal;
1074 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1076 ret = bch2_move_btree(c,
1077 op.start_btree, op.start_pos,
1078 op.end_btree, op.end_pos,
1079 migrate_btree_pred, &op, stats) ?: ret;
1080 ret = bch2_replicas_gc2(c) ?: ret;
1082 ret = bch2_move_data(c,
1083 op.start_btree, op.start_pos,
1084 op.end_btree, op.end_pos,
1087 writepoint_hashed((unsigned long) current),
1089 migrate_pred, &op) ?: ret;
1090 ret = bch2_replicas_gc2(c) ?: ret;
1092 case BCH_DATA_OP_REWRITE_OLD_NODES:
1093 bch2_move_stats_init(stats, "rewrite_old_nodes");
1094 ret = bch2_scan_old_btree_nodes(c, stats);
1103 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1105 struct bch_move_stats *stats = ctxt->stats;
1106 struct moving_io *io;
1108 prt_printf(out, "%s (%ps):", stats->name, ctxt->fn);
1111 prt_printf(out, " data type %s btree_id %s position: ",
1112 bch2_data_types[stats->data_type],
1113 bch2_btree_ids[stats->btree_id]);
1114 bch2_bpos_to_text(out, stats->pos);
1116 printbuf_indent_add(out, 2);
1118 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1119 atomic_read(&ctxt->read_ios),
1120 c->opts.move_ios_in_flight,
1121 atomic_read(&ctxt->read_sectors),
1122 c->opts.move_bytes_in_flight >> 9);
1125 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1126 atomic_read(&ctxt->write_ios),
1127 c->opts.move_ios_in_flight,
1128 atomic_read(&ctxt->write_sectors),
1129 c->opts.move_bytes_in_flight >> 9);
1132 printbuf_indent_add(out, 2);
1134 mutex_lock(&ctxt->lock);
1135 list_for_each_entry(io, &ctxt->ios, io_list)
1136 bch2_write_op_to_text(out, &io->write.op);
1137 mutex_unlock(&ctxt->lock);
1139 printbuf_indent_sub(out, 4);
1142 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1144 struct moving_context *ctxt;
1146 mutex_lock(&c->moving_context_lock);
1147 list_for_each_entry(ctxt, &c->moving_context_list, list)
1148 bch2_moving_ctxt_to_text(out, c, ctxt);
1149 mutex_unlock(&c->moving_context_lock);
1152 void bch2_fs_move_init(struct bch_fs *c)
1154 INIT_LIST_HEAD(&c->moving_context_list);
1155 mutex_init(&c->moving_context_lock);
1157 INIT_LIST_HEAD(&c->data_progress_list);
1158 mutex_init(&c->data_progress_lock);