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"
27 #include <linux/ioprio.h>
28 #include <linux/kthread.h>
30 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k)
32 if (trace_move_extent_enabled()) {
33 struct printbuf buf = PRINTBUF;
35 bch2_bkey_val_to_text(&buf, c, k);
36 trace_move_extent(c, buf.buf);
41 static void trace_move_extent_read2(struct bch_fs *c, struct bkey_s_c k)
43 if (trace_move_extent_read_enabled()) {
44 struct printbuf buf = PRINTBUF;
46 bch2_bkey_val_to_text(&buf, c, k);
47 trace_move_extent_read(c, buf.buf);
52 static void trace_move_extent_alloc_mem_fail2(struct bch_fs *c, struct bkey_s_c k)
54 if (trace_move_extent_alloc_mem_fail_enabled()) {
55 struct printbuf buf = PRINTBUF;
57 bch2_bkey_val_to_text(&buf, c, k);
58 trace_move_extent_alloc_mem_fail(c, buf.buf);
64 struct list_head read_list;
65 struct list_head io_list;
66 struct move_bucket_in_flight *b;
70 unsigned read_sectors;
71 unsigned write_sectors;
73 struct bch_read_bio rbio;
75 struct data_update write;
76 /* Must be last since it is variable size */
77 struct bio_vec bi_inline_vecs[0];
80 static void move_free(struct moving_io *io)
82 struct moving_context *ctxt = io->write.ctxt;
85 atomic_dec(&io->b->count);
87 bch2_data_update_exit(&io->write);
89 mutex_lock(&ctxt->lock);
90 list_del(&io->io_list);
92 mutex_unlock(&ctxt->lock);
97 static void move_write_done(struct bch_write_op *op)
99 struct moving_io *io = container_of(op, struct moving_io, write.op);
100 struct moving_context *ctxt = io->write.ctxt;
102 if (io->write.op.error)
103 ctxt->write_error = true;
105 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
106 atomic_dec(&io->write.ctxt->write_ios);
108 closure_put(&ctxt->cl);
111 static void move_write(struct moving_io *io)
113 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
118 closure_get(&io->write.ctxt->cl);
119 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
120 atomic_inc(&io->write.ctxt->write_ios);
122 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
125 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
127 struct moving_io *io =
128 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
130 return io && io->read_completed ? io : NULL;
133 static void move_read_endio(struct bio *bio)
135 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
136 struct moving_context *ctxt = io->write.ctxt;
138 atomic_sub(io->read_sectors, &ctxt->read_sectors);
139 atomic_dec(&ctxt->read_ios);
140 io->read_completed = true;
142 wake_up(&ctxt->wait);
143 closure_put(&ctxt->cl);
146 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt)
148 struct moving_io *io;
150 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
151 bch2_trans_unlock_long(ctxt->trans);
152 list_del(&io->read_list);
157 void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
159 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
161 move_ctxt_wait_event(ctxt,
162 !atomic_read(&ctxt->write_sectors) ||
163 atomic_read(&ctxt->write_sectors) != sectors_pending);
166 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
168 struct bch_fs *c = ctxt->trans->c;
170 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
171 closure_sync(&ctxt->cl);
173 EBUG_ON(atomic_read(&ctxt->write_sectors));
174 EBUG_ON(atomic_read(&ctxt->write_ios));
175 EBUG_ON(atomic_read(&ctxt->read_sectors));
176 EBUG_ON(atomic_read(&ctxt->read_ios));
178 mutex_lock(&c->moving_context_lock);
179 list_del(&ctxt->list);
180 mutex_unlock(&c->moving_context_lock);
182 bch2_trans_put(ctxt->trans);
183 memset(ctxt, 0, sizeof(*ctxt));
186 void bch2_moving_ctxt_init(struct moving_context *ctxt,
188 struct bch_ratelimit *rate,
189 struct bch_move_stats *stats,
190 struct write_point_specifier wp,
193 memset(ctxt, 0, sizeof(*ctxt));
195 ctxt->trans = bch2_trans_get(c);
196 ctxt->fn = (void *) _RET_IP_;
200 ctxt->wait_on_copygc = wait_on_copygc;
202 closure_init_stack(&ctxt->cl);
204 mutex_init(&ctxt->lock);
205 INIT_LIST_HEAD(&ctxt->reads);
206 INIT_LIST_HEAD(&ctxt->ios);
207 init_waitqueue_head(&ctxt->wait);
209 mutex_lock(&c->moving_context_lock);
210 list_add(&ctxt->list, &c->moving_context_list);
211 mutex_unlock(&c->moving_context_lock);
214 void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
216 trace_move_data(c, stats);
219 void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
221 memset(stats, 0, sizeof(*stats));
222 stats->data_type = BCH_DATA_user;
223 scnprintf(stats->name, sizeof(stats->name), "%s", name);
226 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
227 struct btree_iter *iter,
229 struct data_update_opts data_opts)
231 struct bch_fs *c = trans->c;
235 n = bch2_bkey_make_mut_noupdate(trans, k);
236 ret = PTR_ERR_OR_ZERO(n);
240 while (data_opts.kill_ptrs) {
241 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
242 struct bch_extent_ptr *ptr;
244 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
245 data_opts.kill_ptrs ^= 1U << drop;
249 * If the new extent no longer has any pointers, bch2_extent_normalize()
250 * will do the appropriate thing with it (turning it into a
251 * KEY_TYPE_error key, or just a discard if it was a cached extent)
253 bch2_extent_normalize(c, bkey_i_to_s(n));
256 * Since we're not inserting through an extent iterator
257 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
258 * we aren't using the extent overwrite path to delete, we're
259 * just using the normal key deletion path:
261 if (bkey_deleted(&n->k))
264 return bch2_trans_relock(trans) ?:
265 bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
266 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
269 int bch2_move_extent(struct moving_context *ctxt,
270 struct move_bucket_in_flight *bucket_in_flight,
271 struct btree_iter *iter,
273 struct bch_io_opts io_opts,
274 struct data_update_opts data_opts)
276 struct btree_trans *trans = ctxt->trans;
277 struct bch_fs *c = trans->c;
278 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
279 struct moving_io *io;
280 const union bch_extent_entry *entry;
281 struct extent_ptr_decoded p;
282 unsigned sectors = k.k->size, pages;
286 ctxt->stats->pos = BBPOS(iter->btree_id, iter->pos);
287 trace_move_extent2(c, k);
289 bch2_data_update_opts_normalize(k, &data_opts);
291 if (!data_opts.rewrite_ptrs &&
292 !data_opts.extra_replicas) {
293 if (data_opts.kill_ptrs)
294 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
299 * Before memory allocations & taking nocow locks in
300 * bch2_data_update_init():
302 bch2_trans_unlock(trans);
304 /* write path might have to decompress data: */
305 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
306 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
308 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
309 io = kzalloc(sizeof(struct moving_io) +
310 sizeof(struct bio_vec) * pages, GFP_KERNEL);
314 INIT_LIST_HEAD(&io->io_list);
315 io->write.ctxt = ctxt;
316 io->read_sectors = k.k->size;
317 io->write_sectors = k.k->size;
319 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
320 bio_set_prio(&io->write.op.wbio.bio,
321 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
323 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
328 io->rbio.opts = io_opts;
329 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
330 io->rbio.bio.bi_vcnt = pages;
331 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
332 io->rbio.bio.bi_iter.bi_size = sectors << 9;
334 io->rbio.bio.bi_opf = REQ_OP_READ;
335 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
336 io->rbio.bio.bi_end_io = move_read_endio;
338 ret = bch2_data_update_init(trans, ctxt, &io->write, ctxt->wp,
339 io_opts, data_opts, iter->btree_id, k);
340 if (ret && ret != -BCH_ERR_unwritten_extent_update)
343 if (ret == -BCH_ERR_unwritten_extent_update) {
344 bch2_update_unwritten_extent(trans, &io->write);
351 io->write.op.end_io = move_write_done;
354 bch2_ratelimit_increment(ctxt->rate, k.k->size);
357 atomic64_inc(&ctxt->stats->keys_moved);
358 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
361 if (bucket_in_flight) {
362 io->b = bucket_in_flight;
363 atomic_inc(&io->b->count);
366 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
367 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
368 trace_move_extent_read2(c, k);
370 mutex_lock(&ctxt->lock);
371 atomic_add(io->read_sectors, &ctxt->read_sectors);
372 atomic_inc(&ctxt->read_ios);
374 list_add_tail(&io->read_list, &ctxt->reads);
375 list_add_tail(&io->io_list, &ctxt->ios);
376 mutex_unlock(&ctxt->lock);
379 * dropped by move_read_endio() - guards against use after free of
380 * ctxt when doing wakeup
382 closure_get(&ctxt->cl);
383 bch2_read_extent(trans, &io->rbio,
385 iter->btree_id, k, 0,
387 BCH_READ_LAST_FRAGMENT);
390 bio_free_pages(&io->write.op.wbio.bio);
394 this_cpu_inc(c->counters[BCH_COUNTER_move_extent_alloc_mem_fail]);
395 trace_move_extent_alloc_mem_fail2(c, k);
399 struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *trans,
400 struct per_snapshot_io_opts *io_opts,
401 struct bkey_s_c extent_k)
403 struct bch_fs *c = trans->c;
404 u32 restart_count = trans->restart_count;
407 if (io_opts->cur_inum != extent_k.k->p.inode) {
408 struct btree_iter iter;
413 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, extent_k.k->p.inode),
414 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
415 if (k.k->p.offset != extent_k.k->p.inode)
418 if (!bkey_is_inode(k.k))
421 struct bch_inode_unpacked inode;
422 BUG_ON(bch2_inode_unpack(k, &inode));
424 struct snapshot_io_opts_entry e = { .snapshot = k.k->p.snapshot };
425 bch2_inode_opts_get(&e.io_opts, trans->c, &inode);
427 ret = darray_push(&io_opts->d, e);
431 bch2_trans_iter_exit(trans, &iter);
432 io_opts->cur_inum = extent_k.k->p.inode;
435 ret = ret ?: trans_was_restarted(trans, restart_count);
439 if (extent_k.k->p.snapshot) {
440 struct snapshot_io_opts_entry *i;
441 darray_for_each(io_opts->d, i)
442 if (bch2_snapshot_is_ancestor(c, extent_k.k->p.snapshot, i->snapshot))
446 return &io_opts->fs_io_opts;
449 int bch2_move_get_io_opts_one(struct btree_trans *trans,
450 struct bch_io_opts *io_opts,
451 struct bkey_s_c extent_k)
453 struct btree_iter iter;
458 if (!extent_k.k->p.inode) {
459 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
463 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
464 SPOS(0, extent_k.k->p.inode, extent_k.k->p.snapshot),
467 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
470 if (!ret && bkey_is_inode(k.k)) {
471 struct bch_inode_unpacked inode;
472 bch2_inode_unpack(k, &inode);
473 bch2_inode_opts_get(io_opts, trans->c, &inode);
475 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
478 bch2_trans_iter_exit(trans, &iter);
482 int bch2_move_ratelimit(struct moving_context *ctxt)
484 struct bch_fs *c = ctxt->trans->c;
487 if (ctxt->wait_on_copygc && !c->copygc_running) {
488 bch2_trans_unlock_long(ctxt->trans);
489 wait_event_killable(c->copygc_running_wq,
490 !c->copygc_running ||
491 kthread_should_stop());
495 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
500 bch2_trans_unlock_long(ctxt->trans);
502 bch2_trans_unlock(ctxt->trans);
503 set_current_state(TASK_INTERRUPTIBLE);
506 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
507 __set_current_state(TASK_RUNNING);
512 schedule_timeout(delay);
514 if (unlikely(freezing(current))) {
515 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
521 * XXX: these limits really ought to be per device, SSDs and hard drives
522 * will want different limits
524 move_ctxt_wait_event(ctxt,
525 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
526 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
527 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
528 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
533 static int bch2_move_data_btree(struct moving_context *ctxt,
536 move_pred_fn pred, void *arg,
537 enum btree_id btree_id)
539 struct btree_trans *trans = ctxt->trans;
540 struct bch_fs *c = trans->c;
541 struct per_snapshot_io_opts snapshot_io_opts;
542 struct bch_io_opts *io_opts;
544 struct btree_iter iter;
546 struct data_update_opts data_opts;
549 per_snapshot_io_opts_init(&snapshot_io_opts, c);
550 bch2_bkey_buf_init(&sk);
553 ctxt->stats->data_type = BCH_DATA_user;
554 ctxt->stats->pos = BBPOS(btree_id, start);
557 bch2_trans_iter_init(trans, &iter, btree_id, start,
559 BTREE_ITER_ALL_SNAPSHOTS);
562 bch2_ratelimit_reset(ctxt->rate);
564 while (!bch2_move_ratelimit(ctxt)) {
565 bch2_trans_begin(trans);
567 k = bch2_btree_iter_peek(&iter);
572 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
577 if (bkey_ge(bkey_start_pos(k.k), end))
581 ctxt->stats->pos = BBPOS(iter.btree_id, iter.pos);
583 if (!bkey_extent_is_direct_data(k.k))
586 io_opts = bch2_move_get_io_opts(trans, &snapshot_io_opts, k);
587 ret = PTR_ERR_OR_ZERO(io_opts);
591 memset(&data_opts, 0, sizeof(data_opts));
592 if (!pred(c, arg, k, io_opts, &data_opts))
596 * The iterator gets unlocked by __bch2_read_extent - need to
597 * save a copy of @k elsewhere:
599 bch2_bkey_buf_reassemble(&sk, c, k);
600 k = bkey_i_to_s_c(sk.k);
602 ret2 = bch2_move_extent(ctxt, NULL, &iter, k, *io_opts, data_opts);
604 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
607 if (ret2 == -ENOMEM) {
608 /* memory allocation failure, wait for some IO to finish */
609 bch2_move_ctxt_wait_for_io(ctxt);
613 /* XXX signal failure */
618 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
620 bch2_btree_iter_advance(&iter);
623 bch2_trans_iter_exit(trans, &iter);
624 bch2_bkey_buf_exit(&sk, c);
625 per_snapshot_io_opts_exit(&snapshot_io_opts);
630 int __bch2_move_data(struct moving_context *ctxt,
633 move_pred_fn pred, void *arg)
635 struct bch_fs *c = ctxt->trans->c;
639 for (id = start.btree;
640 id <= min_t(unsigned, end.btree, btree_id_nr_alive(c) - 1);
642 ctxt->stats->pos = BBPOS(id, POS_MIN);
644 if (!btree_type_has_ptrs(id) ||
645 !bch2_btree_id_root(c, id)->b)
648 ret = bch2_move_data_btree(ctxt,
649 id == start.btree ? start.pos : POS_MIN,
650 id == end.btree ? end.pos : POS_MAX,
659 int bch2_move_data(struct bch_fs *c,
662 struct bch_ratelimit *rate,
663 struct bch_move_stats *stats,
664 struct write_point_specifier wp,
666 move_pred_fn pred, void *arg)
669 struct moving_context ctxt;
672 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
673 ret = __bch2_move_data(&ctxt, start, end, pred, arg);
674 bch2_moving_ctxt_exit(&ctxt);
679 int __bch2_evacuate_bucket(struct moving_context *ctxt,
680 struct move_bucket_in_flight *bucket_in_flight,
681 struct bpos bucket, int gen,
682 struct data_update_opts _data_opts)
684 struct btree_trans *trans = ctxt->trans;
685 struct bch_fs *c = trans->c;
686 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
687 struct btree_iter iter;
689 struct bch_backpointer bp;
690 struct bch_alloc_v4 a_convert;
691 const struct bch_alloc_v4 *a;
693 struct data_update_opts data_opts;
694 unsigned dirty_sectors, bucket_size;
696 struct bpos bp_pos = POS_MIN;
699 trace_bucket_evacuate(c, &bucket);
701 bch2_bkey_buf_init(&sk);
704 * We're not run in a context that handles transaction restarts:
706 bch2_trans_begin(trans);
708 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
709 bucket, BTREE_ITER_CACHED);
710 ret = lockrestart_do(trans,
711 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
712 bch2_trans_iter_exit(trans, &iter);
715 bch_err_msg(c, ret, "looking up alloc key");
719 a = bch2_alloc_to_v4(k, &a_convert);
720 dirty_sectors = a->dirty_sectors;
721 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
722 fragmentation = a->fragmentation_lru;
724 ret = bch2_btree_write_buffer_flush(trans);
726 bch_err_msg(c, ret, "flushing btree write buffer");
730 while (!(ret = bch2_move_ratelimit(ctxt))) {
731 bch2_trans_begin(trans);
733 ret = bch2_get_next_backpointer(trans, bucket, gen,
736 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
740 if (bkey_eq(bp_pos, POS_MAX))
744 const struct bch_extent_ptr *ptr;
747 k = bch2_backpointer_get_key(trans, &iter, bp_pos, bp, 0);
749 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
756 bch2_bkey_buf_reassemble(&sk, c, k);
757 k = bkey_i_to_s_c(sk.k);
759 ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
761 bch2_trans_iter_exit(trans, &iter);
765 data_opts = _data_opts;
766 data_opts.target = io_opts.background_target;
767 data_opts.rewrite_ptrs = 0;
769 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
770 if (ptr->dev == bucket.inode) {
771 data_opts.rewrite_ptrs |= 1U << i;
773 bch2_trans_iter_exit(trans, &iter);
780 ret = bch2_move_extent(ctxt, bucket_in_flight,
781 &iter, k, io_opts, data_opts);
782 bch2_trans_iter_exit(trans, &iter);
784 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
786 if (ret == -ENOMEM) {
787 /* memory allocation failure, wait for some IO to finish */
788 bch2_move_ctxt_wait_for_io(ctxt);
795 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
799 b = bch2_backpointer_get_node(trans, &iter, bp_pos, bp);
800 ret = PTR_ERR_OR_ZERO(b);
801 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
803 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
810 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
811 bch2_trans_iter_exit(trans, &iter);
813 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
819 bch2_ratelimit_increment(ctxt->rate,
820 c->opts.btree_node_size >> 9);
822 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
823 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
827 bp_pos = bpos_nosnap_successor(bp_pos);
830 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
832 bch2_bkey_buf_exit(&sk, c);
836 int bch2_evacuate_bucket(struct bch_fs *c,
837 struct bpos bucket, int gen,
838 struct data_update_opts data_opts,
839 struct bch_ratelimit *rate,
840 struct bch_move_stats *stats,
841 struct write_point_specifier wp,
844 struct moving_context ctxt;
847 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
848 ret = __bch2_evacuate_bucket(&ctxt, NULL, bucket, gen, data_opts);
849 bch2_moving_ctxt_exit(&ctxt);
854 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
855 struct btree *, struct bch_io_opts *,
856 struct data_update_opts *);
858 static int bch2_move_btree(struct bch_fs *c,
859 enum btree_id start_btree_id, struct bpos start_pos,
860 enum btree_id end_btree_id, struct bpos end_pos,
861 move_btree_pred pred, void *arg,
862 struct bch_move_stats *stats)
864 bool kthread = (current->flags & PF_KTHREAD) != 0;
865 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
866 struct moving_context ctxt;
867 struct btree_trans *trans;
868 struct btree_iter iter;
871 struct data_update_opts data_opts;
874 bch2_moving_ctxt_init(&ctxt, c, NULL, stats,
875 writepoint_ptr(&c->btree_write_point),
879 stats->data_type = BCH_DATA_btree;
881 for (id = start_btree_id;
882 id <= min_t(unsigned, end_btree_id, btree_id_nr_alive(c) - 1);
884 stats->pos = BBPOS(id, POS_MIN);
886 if (!bch2_btree_id_root(c, id)->b)
889 bch2_trans_node_iter_init(trans, &iter, id, POS_MIN, 0, 0,
890 BTREE_ITER_PREFETCH);
893 while (bch2_trans_begin(trans),
894 (b = bch2_btree_iter_peek_node(&iter)) &&
895 !(ret = PTR_ERR_OR_ZERO(b))) {
896 if (kthread && kthread_should_stop())
899 if ((cmp_int(id, end_btree_id) ?:
900 bpos_cmp(b->key.k.p, end_pos)) > 0)
903 stats->pos = BBPOS(iter.btree_id, iter.pos);
905 if (!pred(c, arg, b, &io_opts, &data_opts))
908 ret = bch2_btree_node_rewrite(trans, &iter, b, 0) ?: ret;
909 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
914 bch2_btree_iter_next_node(&iter);
916 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
919 bch2_trans_iter_exit(trans, &iter);
921 if (kthread && kthread_should_stop())
926 bch2_moving_ctxt_exit(&ctxt);
927 bch2_btree_interior_updates_flush(c);
932 static bool rereplicate_pred(struct bch_fs *c, void *arg,
934 struct bch_io_opts *io_opts,
935 struct data_update_opts *data_opts)
937 unsigned nr_good = bch2_bkey_durability(c, k);
938 unsigned replicas = bkey_is_btree_ptr(k.k)
939 ? c->opts.metadata_replicas
940 : io_opts->data_replicas;
942 if (!nr_good || nr_good >= replicas)
945 data_opts->target = 0;
946 data_opts->extra_replicas = replicas - nr_good;
947 data_opts->btree_insert_flags = 0;
951 static bool migrate_pred(struct bch_fs *c, void *arg,
953 struct bch_io_opts *io_opts,
954 struct data_update_opts *data_opts)
956 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
957 const struct bch_extent_ptr *ptr;
958 struct bch_ioctl_data *op = arg;
961 data_opts->rewrite_ptrs = 0;
962 data_opts->target = 0;
963 data_opts->extra_replicas = 0;
964 data_opts->btree_insert_flags = 0;
966 bkey_for_each_ptr(ptrs, ptr) {
967 if (ptr->dev == op->migrate.dev)
968 data_opts->rewrite_ptrs |= 1U << i;
972 return data_opts->rewrite_ptrs != 0;
975 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
977 struct bch_io_opts *io_opts,
978 struct data_update_opts *data_opts)
980 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
983 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
985 struct bch_io_opts *io_opts,
986 struct data_update_opts *data_opts)
988 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
991 static bool bformat_needs_redo(struct bkey_format *f)
995 for (i = 0; i < f->nr_fields; i++) {
996 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
997 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
998 u64 field_offset = le64_to_cpu(f->field_offset[i]);
1000 if (f->bits_per_field[i] > unpacked_bits)
1003 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
1006 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1015 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1017 struct bch_io_opts *io_opts,
1018 struct data_update_opts *data_opts)
1020 if (b->version_ondisk != c->sb.version ||
1021 btree_node_need_rewrite(b) ||
1022 bformat_needs_redo(&b->format)) {
1023 data_opts->target = 0;
1024 data_opts->extra_replicas = 0;
1025 data_opts->btree_insert_flags = 0;
1032 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1036 ret = bch2_move_btree(c,
1038 BTREE_ID_NR, SPOS_MAX,
1039 rewrite_old_nodes_pred, c, stats);
1041 mutex_lock(&c->sb_lock);
1042 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1043 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1044 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1045 bch2_write_super(c);
1046 mutex_unlock(&c->sb_lock);
1053 int bch2_data_job(struct bch_fs *c,
1054 struct bch_move_stats *stats,
1055 struct bch_ioctl_data op)
1060 case BCH_DATA_OP_REREPLICATE:
1061 bch2_move_stats_init(stats, "rereplicate");
1062 stats->data_type = BCH_DATA_journal;
1063 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1065 ret = bch2_move_btree(c,
1066 op.start_btree, op.start_pos,
1067 op.end_btree, op.end_pos,
1068 rereplicate_btree_pred, c, stats) ?: ret;
1069 ret = bch2_replicas_gc2(c) ?: ret;
1071 ret = bch2_move_data(c,
1072 (struct bbpos) { op.start_btree, op.start_pos },
1073 (struct bbpos) { op.end_btree, op.end_pos },
1076 writepoint_hashed((unsigned long) current),
1078 rereplicate_pred, c) ?: ret;
1079 ret = bch2_replicas_gc2(c) ?: ret;
1081 bch2_move_stats_exit(stats, c);
1083 case BCH_DATA_OP_MIGRATE:
1084 if (op.migrate.dev >= c->sb.nr_devices)
1087 bch2_move_stats_init(stats, "migrate");
1088 stats->data_type = BCH_DATA_journal;
1089 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1091 ret = bch2_move_btree(c,
1092 op.start_btree, op.start_pos,
1093 op.end_btree, op.end_pos,
1094 migrate_btree_pred, &op, stats) ?: ret;
1095 ret = bch2_replicas_gc2(c) ?: ret;
1097 ret = bch2_move_data(c,
1098 (struct bbpos) { op.start_btree, op.start_pos },
1099 (struct bbpos) { op.end_btree, op.end_pos },
1102 writepoint_hashed((unsigned long) current),
1104 migrate_pred, &op) ?: ret;
1105 ret = bch2_replicas_gc2(c) ?: ret;
1107 bch2_move_stats_exit(stats, c);
1109 case BCH_DATA_OP_REWRITE_OLD_NODES:
1110 bch2_move_stats_init(stats, "rewrite_old_nodes");
1111 ret = bch2_scan_old_btree_nodes(c, stats);
1112 bch2_move_stats_exit(stats, c);
1121 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
1123 prt_printf(out, "%s: data type=%s pos=",
1125 bch2_data_types[stats->data_type]);
1126 bch2_bbpos_to_text(out, stats->pos);
1128 printbuf_indent_add(out, 2);
1130 prt_str(out, "keys moved: ");
1131 prt_u64(out, atomic64_read(&stats->keys_moved));
1134 prt_str(out, "keys raced: ");
1135 prt_u64(out, atomic64_read(&stats->keys_raced));
1138 prt_str(out, "bytes seen: ");
1139 prt_human_readable_u64(out, atomic64_read(&stats->sectors_seen) << 9);
1142 prt_str(out, "bytes moved: ");
1143 prt_human_readable_u64(out, atomic64_read(&stats->sectors_moved) << 9);
1146 prt_str(out, "bytes raced: ");
1147 prt_human_readable_u64(out, atomic64_read(&stats->sectors_raced) << 9);
1150 printbuf_indent_sub(out, 2);
1153 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct bch_fs *c, struct moving_context *ctxt)
1155 struct moving_io *io;
1157 bch2_move_stats_to_text(out, ctxt->stats);
1158 printbuf_indent_add(out, 2);
1160 prt_printf(out, "reads: ios %u/%u sectors %u/%u",
1161 atomic_read(&ctxt->read_ios),
1162 c->opts.move_ios_in_flight,
1163 atomic_read(&ctxt->read_sectors),
1164 c->opts.move_bytes_in_flight >> 9);
1167 prt_printf(out, "writes: ios %u/%u sectors %u/%u",
1168 atomic_read(&ctxt->write_ios),
1169 c->opts.move_ios_in_flight,
1170 atomic_read(&ctxt->write_sectors),
1171 c->opts.move_bytes_in_flight >> 9);
1174 printbuf_indent_add(out, 2);
1176 mutex_lock(&ctxt->lock);
1177 list_for_each_entry(io, &ctxt->ios, io_list)
1178 bch2_write_op_to_text(out, &io->write.op);
1179 mutex_unlock(&ctxt->lock);
1181 printbuf_indent_sub(out, 4);
1184 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1186 struct moving_context *ctxt;
1188 mutex_lock(&c->moving_context_lock);
1189 list_for_each_entry(ctxt, &c->moving_context_list, list)
1190 bch2_moving_ctxt_to_text(out, c, ctxt);
1191 mutex_unlock(&c->moving_context_lock);
1194 void bch2_fs_move_init(struct bch_fs *c)
1196 INIT_LIST_HEAD(&c->moving_context_list);
1197 mutex_init(&c->moving_context_lock);