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"
24 #include <linux/ioprio.h>
25 #include <linux/kthread.h>
27 #include <trace/events/bcachefs.h>
29 static void progress_list_add(struct bch_fs *c, struct bch_move_stats *stats)
31 mutex_lock(&c->data_progress_lock);
32 list_add(&stats->list, &c->data_progress_list);
33 mutex_unlock(&c->data_progress_lock);
36 static void progress_list_del(struct bch_fs *c, struct bch_move_stats *stats)
38 mutex_lock(&c->data_progress_lock);
39 list_del(&stats->list);
40 mutex_unlock(&c->data_progress_lock);
44 struct list_head read_list;
45 struct list_head io_list;
46 struct move_bucket_in_flight *b;
50 unsigned read_sectors;
51 unsigned write_sectors;
53 struct bch_read_bio rbio;
55 struct data_update write;
56 /* Must be last since it is variable size */
57 struct bio_vec bi_inline_vecs[0];
60 static void move_free(struct moving_io *io)
62 struct moving_context *ctxt = io->write.ctxt;
65 atomic_dec(&io->b->count);
67 bch2_data_update_exit(&io->write);
69 mutex_lock(&ctxt->lock);
70 list_del(&io->io_list);
72 mutex_unlock(&ctxt->lock);
77 static void move_write_done(struct bch_write_op *op)
79 struct moving_io *io = container_of(op, struct moving_io, write.op);
80 struct moving_context *ctxt = io->write.ctxt;
82 if (io->write.op.error)
83 ctxt->write_error = true;
85 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
86 atomic_dec(&io->write.ctxt->write_ios);
88 closure_put(&ctxt->cl);
91 static void move_write(struct moving_io *io)
93 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
98 closure_get(&io->write.ctxt->cl);
99 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
100 atomic_inc(&io->write.ctxt->write_ios);
102 bch2_data_update_read_done(&io->write, io->rbio.pick.crc);
105 struct moving_io *bch2_moving_ctxt_next_pending_write(struct moving_context *ctxt)
107 struct moving_io *io =
108 list_first_entry_or_null(&ctxt->reads, struct moving_io, read_list);
110 return io && io->read_completed ? io : NULL;
113 static void move_read_endio(struct bio *bio)
115 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
116 struct moving_context *ctxt = io->write.ctxt;
118 atomic_sub(io->read_sectors, &ctxt->read_sectors);
119 atomic_dec(&ctxt->read_ios);
120 io->read_completed = true;
122 wake_up(&ctxt->wait);
123 closure_put(&ctxt->cl);
126 void bch2_moving_ctxt_do_pending_writes(struct moving_context *ctxt,
127 struct btree_trans *trans)
129 struct moving_io *io;
132 bch2_trans_unlock(trans);
134 while ((io = bch2_moving_ctxt_next_pending_write(ctxt))) {
135 list_del(&io->read_list);
140 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
141 struct btree_trans *trans)
143 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
145 move_ctxt_wait_event(ctxt, trans,
146 !atomic_read(&ctxt->write_sectors) ||
147 atomic_read(&ctxt->write_sectors) != sectors_pending);
150 void bch2_moving_ctxt_exit(struct moving_context *ctxt)
152 struct bch_fs *c = ctxt->c;
154 move_ctxt_wait_event(ctxt, NULL, list_empty(&ctxt->reads));
155 closure_sync(&ctxt->cl);
157 EBUG_ON(atomic_read(&ctxt->write_sectors));
158 EBUG_ON(atomic_read(&ctxt->write_ios));
159 EBUG_ON(atomic_read(&ctxt->read_sectors));
160 EBUG_ON(atomic_read(&ctxt->read_ios));
163 progress_list_del(c, ctxt->stats);
165 atomic64_read(&ctxt->stats->sectors_moved),
166 atomic64_read(&ctxt->stats->keys_moved));
169 mutex_lock(&c->moving_context_lock);
170 list_del(&ctxt->list);
171 mutex_unlock(&c->moving_context_lock);
174 void bch2_moving_ctxt_init(struct moving_context *ctxt,
176 struct bch_ratelimit *rate,
177 struct bch_move_stats *stats,
178 struct write_point_specifier wp,
181 memset(ctxt, 0, sizeof(*ctxt));
184 ctxt->fn = (void *) _RET_IP_;
188 ctxt->wait_on_copygc = wait_on_copygc;
190 closure_init_stack(&ctxt->cl);
192 mutex_init(&ctxt->lock);
193 INIT_LIST_HEAD(&ctxt->reads);
194 INIT_LIST_HEAD(&ctxt->ios);
195 init_waitqueue_head(&ctxt->wait);
197 mutex_lock(&c->moving_context_lock);
198 list_add(&ctxt->list, &c->moving_context_list);
199 mutex_unlock(&c->moving_context_lock);
202 progress_list_add(c, stats);
203 stats->data_type = BCH_DATA_user;
207 void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
209 memset(stats, 0, sizeof(*stats));
210 scnprintf(stats->name, sizeof(stats->name), "%s", name);
213 static int bch2_extent_drop_ptrs(struct btree_trans *trans,
214 struct btree_iter *iter,
216 struct data_update_opts data_opts)
218 struct bch_fs *c = trans->c;
222 n = bch2_bkey_make_mut(trans, k);
223 ret = PTR_ERR_OR_ZERO(n);
227 while (data_opts.kill_ptrs) {
228 unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
229 struct bch_extent_ptr *ptr;
231 bch2_bkey_drop_ptrs(bkey_i_to_s(n), ptr, i++ == drop);
232 data_opts.kill_ptrs ^= 1U << drop;
236 * If the new extent no longer has any pointers, bch2_extent_normalize()
237 * will do the appropriate thing with it (turning it into a
238 * KEY_TYPE_error key, or just a discard if it was a cached extent)
240 bch2_extent_normalize(c, bkey_i_to_s(n));
243 * Since we're not inserting through an extent iterator
244 * (BTREE_ITER_ALL_SNAPSHOTS iterators aren't extent iterators),
245 * we aren't using the extent overwrite path to delete, we're
246 * just using the normal key deletion path:
248 if (bkey_deleted(&n->k))
251 return bch2_trans_relock(trans) ?:
252 bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
253 bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
256 static int bch2_move_extent(struct btree_trans *trans,
257 struct btree_iter *iter,
258 struct moving_context *ctxt,
259 struct move_bucket_in_flight *bucket_in_flight,
260 struct bch_io_opts io_opts,
261 enum btree_id btree_id,
263 struct data_update_opts data_opts)
265 struct bch_fs *c = trans->c;
266 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
267 struct moving_io *io;
268 const union bch_extent_entry *entry;
269 struct extent_ptr_decoded p;
270 unsigned sectors = k.k->size, pages;
273 bch2_data_update_opts_normalize(k, &data_opts);
275 if (!data_opts.rewrite_ptrs &&
276 !data_opts.extra_replicas) {
277 if (data_opts.kill_ptrs)
278 return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
283 * Before memory allocations & taking nocow locks in
284 * bch2_data_update_init():
286 bch2_trans_unlock(trans);
288 /* write path might have to decompress data: */
289 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
290 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
292 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
293 io = kzalloc(sizeof(struct moving_io) +
294 sizeof(struct bio_vec) * pages, GFP_KERNEL);
298 INIT_LIST_HEAD(&io->io_list);
299 io->write.ctxt = ctxt;
300 io->read_sectors = k.k->size;
301 io->write_sectors = k.k->size;
303 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
304 bio_set_prio(&io->write.op.wbio.bio,
305 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
307 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
312 io->rbio.opts = io_opts;
313 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
314 io->rbio.bio.bi_vcnt = pages;
315 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
316 io->rbio.bio.bi_iter.bi_size = sectors << 9;
318 io->rbio.bio.bi_opf = REQ_OP_READ;
319 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
320 io->rbio.bio.bi_end_io = move_read_endio;
322 ret = bch2_data_update_init(trans, ctxt, &io->write, ctxt->wp,
323 io_opts, data_opts, btree_id, k);
324 if (ret && ret != -BCH_ERR_unwritten_extent_update)
327 if (ret == -BCH_ERR_unwritten_extent_update) {
328 bch2_update_unwritten_extent(trans, &io->write);
335 io->write.ctxt = ctxt;
336 io->write.op.end_io = move_write_done;
339 atomic64_inc(&ctxt->stats->keys_moved);
340 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
343 if (bucket_in_flight) {
344 io->b = bucket_in_flight;
345 atomic_inc(&io->b->count);
348 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
349 this_cpu_add(c->counters[BCH_COUNTER_move_extent_read], k.k->size);
350 trace_move_extent_read(k.k);
353 mutex_lock(&ctxt->lock);
354 atomic_add(io->read_sectors, &ctxt->read_sectors);
355 atomic_inc(&ctxt->read_ios);
357 list_add_tail(&io->read_list, &ctxt->reads);
358 list_add_tail(&io->io_list, &ctxt->ios);
359 mutex_unlock(&ctxt->lock);
362 * dropped by move_read_endio() - guards against use after free of
363 * ctxt when doing wakeup
365 closure_get(&ctxt->cl);
366 bch2_read_extent(trans, &io->rbio,
370 BCH_READ_LAST_FRAGMENT);
373 bio_free_pages(&io->write.op.wbio.bio);
377 trace_and_count(c, move_extent_alloc_mem_fail, k.k);
381 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
382 struct bch_inode_unpacked *inode)
384 struct btree_iter iter;
388 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
389 BTREE_ITER_ALL_SNAPSHOTS);
390 k = bch2_btree_iter_peek(&iter);
395 if (!k.k || !bkey_eq(k.k->p, pos)) {
400 ret = bkey_is_inode(k.k) ? 0 : -EIO;
404 ret = bch2_inode_unpack(k, inode);
408 bch2_trans_iter_exit(trans, &iter);
412 static int move_ratelimit(struct btree_trans *trans,
413 struct moving_context *ctxt)
415 struct bch_fs *c = trans->c;
418 if (ctxt->wait_on_copygc) {
419 bch2_trans_unlock(trans);
420 wait_event_killable(c->copygc_running_wq,
421 !c->copygc_running ||
422 kthread_should_stop());
426 delay = ctxt->rate ? bch2_ratelimit_delay(ctxt->rate) : 0;
429 bch2_trans_unlock(trans);
430 set_current_state(TASK_INTERRUPTIBLE);
433 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
434 __set_current_state(TASK_RUNNING);
439 schedule_timeout(delay);
441 if (unlikely(freezing(current))) {
442 move_ctxt_wait_event(ctxt, trans, list_empty(&ctxt->reads));
448 * XXX: these limits really ought to be per device, SSDs and hard drives
449 * will want different limits
451 move_ctxt_wait_event(ctxt, trans,
452 atomic_read(&ctxt->write_sectors) < c->opts.move_bytes_in_flight >> 9 &&
453 atomic_read(&ctxt->read_sectors) < c->opts.move_bytes_in_flight >> 9 &&
454 atomic_read(&ctxt->write_ios) < c->opts.move_ios_in_flight &&
455 atomic_read(&ctxt->read_ios) < c->opts.move_ios_in_flight);
460 static int move_get_io_opts(struct btree_trans *trans,
461 struct bch_io_opts *io_opts,
462 struct bkey_s_c k, u64 *cur_inum)
464 struct bch_inode_unpacked inode;
467 if (*cur_inum == k.k->p.inode)
470 ret = lookup_inode(trans,
471 SPOS(0, k.k->p.inode, k.k->p.snapshot),
473 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
477 bch2_inode_opts_get(io_opts, trans->c, &inode);
479 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
480 *cur_inum = k.k->p.inode;
484 static int __bch2_move_data(struct moving_context *ctxt,
487 move_pred_fn pred, void *arg,
488 enum btree_id btree_id)
490 struct bch_fs *c = ctxt->c;
491 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
493 struct btree_trans trans;
494 struct btree_iter iter;
496 struct data_update_opts data_opts;
497 u64 cur_inum = U64_MAX;
500 bch2_bkey_buf_init(&sk);
501 bch2_trans_init(&trans, c, 0, 0);
504 ctxt->stats->data_type = BCH_DATA_user;
505 ctxt->stats->btree_id = btree_id;
506 ctxt->stats->pos = start;
509 bch2_trans_iter_init(&trans, &iter, btree_id, start,
511 BTREE_ITER_ALL_SNAPSHOTS);
514 bch2_ratelimit_reset(ctxt->rate);
516 while (!move_ratelimit(&trans, ctxt)) {
517 bch2_trans_begin(&trans);
519 k = bch2_btree_iter_peek(&iter);
524 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
529 if (bkey_ge(bkey_start_pos(k.k), end))
533 ctxt->stats->pos = iter.pos;
535 if (!bkey_extent_is_direct_data(k.k))
538 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
542 memset(&data_opts, 0, sizeof(data_opts));
543 if (!pred(c, arg, k, &io_opts, &data_opts))
547 * The iterator gets unlocked by __bch2_read_extent - need to
548 * save a copy of @k elsewhere:
550 bch2_bkey_buf_reassemble(&sk, c, k);
551 k = bkey_i_to_s_c(sk.k);
552 bch2_trans_unlock(&trans);
554 ret2 = bch2_move_extent(&trans, &iter, ctxt, NULL,
555 io_opts, btree_id, k, data_opts);
557 if (bch2_err_matches(ret2, BCH_ERR_transaction_restart))
560 if (ret2 == -ENOMEM) {
561 /* memory allocation failure, wait for some IO to finish */
562 bch2_move_ctxt_wait_for_io(ctxt, &trans);
566 /* XXX signal failure */
571 bch2_ratelimit_increment(ctxt->rate, k.k->size);
574 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
576 bch2_btree_iter_advance(&iter);
579 bch2_trans_iter_exit(&trans, &iter);
580 bch2_trans_exit(&trans);
581 bch2_bkey_buf_exit(&sk, c);
586 int bch2_move_data(struct bch_fs *c,
587 enum btree_id start_btree_id, struct bpos start_pos,
588 enum btree_id end_btree_id, struct bpos end_pos,
589 struct bch_ratelimit *rate,
590 struct bch_move_stats *stats,
591 struct write_point_specifier wp,
593 move_pred_fn pred, void *arg)
595 struct moving_context ctxt;
599 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
601 for (id = start_btree_id;
602 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
604 stats->btree_id = id;
606 if (id != BTREE_ID_extents &&
607 id != BTREE_ID_reflink)
610 ret = __bch2_move_data(&ctxt,
611 id == start_btree_id ? start_pos : POS_MIN,
612 id == end_btree_id ? end_pos : POS_MAX,
618 bch2_moving_ctxt_exit(&ctxt);
623 void bch2_verify_bucket_evacuated(struct btree_trans *trans, struct bpos bucket, int gen)
625 struct bch_fs *c = trans->c;
626 struct btree_iter iter;
628 struct printbuf buf = PRINTBUF;
629 struct bch_backpointer bp;
633 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
634 bucket, BTREE_ITER_CACHED);
636 ret = lockrestart_do(trans,
637 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
639 if (!ret && k.k->type == KEY_TYPE_alloc_v4) {
640 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
642 if (a.v->gen == gen &&
643 a.v->dirty_sectors) {
644 if (a.v->data_type == BCH_DATA_btree) {
645 bch2_trans_unlock(trans);
646 if (bch2_btree_interior_updates_flush(c))
648 goto failed_to_evacuate;
653 bch2_trans_iter_exit(trans, &iter);
656 bch2_trans_iter_exit(trans, &iter);
658 if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
661 prt_printf(&buf, bch2_log_msg(c, "failed to evacuate bucket "));
662 bch2_bkey_val_to_text(&buf, c, k);
665 bch2_trans_begin(trans);
667 ret = bch2_get_next_backpointer(trans, bucket, gen,
670 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
674 if (bp_offset == U64_MAX)
677 k = bch2_backpointer_get_key(trans, &iter,
678 bucket, bp_offset, bp);
680 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
687 bch2_bkey_val_to_text(&buf, c, k);
688 bch2_trans_iter_exit(trans, &iter);
691 bch2_print_string_as_lines(KERN_ERR, buf.buf);
695 int __bch2_evacuate_bucket(struct btree_trans *trans,
696 struct moving_context *ctxt,
697 struct move_bucket_in_flight *bucket_in_flight,
698 struct bpos bucket, int gen,
699 struct data_update_opts _data_opts)
701 struct bch_fs *c = ctxt->c;
702 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
703 struct btree_iter iter;
705 struct bch_backpointer bp;
706 struct bch_alloc_v4 a_convert;
707 const struct bch_alloc_v4 *a;
709 struct data_update_opts data_opts;
710 unsigned dirty_sectors, bucket_size;
712 u64 bp_offset = 0, cur_inum = U64_MAX;
715 bch2_bkey_buf_init(&sk);
717 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
718 bucket, BTREE_ITER_CACHED);
719 ret = lockrestart_do(trans,
720 bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
721 bch2_trans_iter_exit(trans, &iter);
724 bch_err(c, "%s: error looking up alloc key: %s", __func__, bch2_err_str(ret));
728 a = bch2_alloc_to_v4(k, &a_convert);
729 dirty_sectors = a->dirty_sectors;
730 bucket_size = bch_dev_bkey_exists(c, bucket.inode)->mi.bucket_size;
731 fragmentation = a->fragmentation_lru;
733 ret = bch2_btree_write_buffer_flush(trans);
735 bch_err(c, "%s: error flushing btree write buffer: %s", __func__, bch2_err_str(ret));
739 while (!(ret = move_ratelimit(trans, ctxt))) {
740 bch2_trans_begin(trans);
742 ret = bch2_get_next_backpointer(trans, bucket, gen,
745 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
749 if (bp_offset == U64_MAX)
753 const struct bch_extent_ptr *ptr;
757 k = bch2_backpointer_get_key(trans, &iter,
758 bucket, bp_offset, bp);
760 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
767 bch2_bkey_buf_reassemble(&sk, c, k);
768 k = bkey_i_to_s_c(sk.k);
770 ret = move_get_io_opts(trans, &io_opts, k, &cur_inum);
772 bch2_trans_iter_exit(trans, &iter);
776 data_opts = _data_opts;
777 data_opts.target = io_opts.background_target;
778 data_opts.rewrite_ptrs = 0;
780 bkey_for_each_ptr(bch2_bkey_ptrs_c(k), ptr) {
781 if (ptr->dev == bucket.inode) {
782 data_opts.rewrite_ptrs |= 1U << i;
784 bch2_trans_iter_exit(trans, &iter);
791 ret = bch2_move_extent(trans, &iter, ctxt,
793 io_opts, bp.btree_id, k, data_opts);
794 bch2_trans_iter_exit(trans, &iter);
796 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
798 if (ret == -ENOMEM) {
799 /* memory allocation failure, wait for some IO to finish */
800 bch2_move_ctxt_wait_for_io(ctxt, trans);
807 bch2_ratelimit_increment(ctxt->rate, k.k->size);
809 atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
813 b = bch2_backpointer_get_node(trans, &iter,
814 bucket, bp_offset, bp);
815 ret = PTR_ERR_OR_ZERO(b);
816 if (ret == -BCH_ERR_backpointer_to_overwritten_btree_node)
818 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
825 ret = bch2_btree_node_rewrite(trans, &iter, b, 0);
826 bch2_trans_iter_exit(trans, &iter);
828 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
834 bch2_ratelimit_increment(ctxt->rate,
835 c->opts.btree_node_size >> 9);
837 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_seen);
838 atomic64_add(c->opts.btree_node_size >> 9, &ctxt->stats->sectors_moved);
845 trace_evacuate_bucket(c, &bucket, dirty_sectors, bucket_size, fragmentation, ret);
847 bch2_bkey_buf_exit(&sk, c);
851 int bch2_evacuate_bucket(struct bch_fs *c,
852 struct bpos bucket, int gen,
853 struct data_update_opts data_opts,
854 struct bch_ratelimit *rate,
855 struct bch_move_stats *stats,
856 struct write_point_specifier wp,
859 struct btree_trans trans;
860 struct moving_context ctxt;
863 bch2_trans_init(&trans, c, 0, 0);
864 bch2_moving_ctxt_init(&ctxt, c, rate, stats, wp, wait_on_copygc);
865 ret = __bch2_evacuate_bucket(&trans, &ctxt, NULL, bucket, gen, data_opts);
866 bch2_moving_ctxt_exit(&ctxt);
867 bch2_trans_exit(&trans);
872 typedef bool (*move_btree_pred)(struct bch_fs *, void *,
873 struct btree *, struct bch_io_opts *,
874 struct data_update_opts *);
876 static int bch2_move_btree(struct bch_fs *c,
877 enum btree_id start_btree_id, struct bpos start_pos,
878 enum btree_id end_btree_id, struct bpos end_pos,
879 move_btree_pred pred, void *arg,
880 struct bch_move_stats *stats)
882 bool kthread = (current->flags & PF_KTHREAD) != 0;
883 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
884 struct btree_trans trans;
885 struct btree_iter iter;
888 struct data_update_opts data_opts;
891 bch2_trans_init(&trans, c, 0, 0);
892 progress_list_add(c, stats);
894 stats->data_type = BCH_DATA_btree;
896 for (id = start_btree_id;
897 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
899 stats->btree_id = id;
901 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
902 BTREE_ITER_PREFETCH);
905 while (bch2_trans_begin(&trans),
906 (b = bch2_btree_iter_peek_node(&iter)) &&
907 !(ret = PTR_ERR_OR_ZERO(b))) {
908 if (kthread && kthread_should_stop())
911 if ((cmp_int(id, end_btree_id) ?:
912 bpos_cmp(b->key.k.p, end_pos)) > 0)
915 stats->pos = iter.pos;
917 if (!pred(c, arg, b, &io_opts, &data_opts))
920 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
921 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
926 bch2_btree_iter_next_node(&iter);
928 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
931 bch2_trans_iter_exit(&trans, &iter);
933 if (kthread && kthread_should_stop())
937 bch2_trans_exit(&trans);
940 bch_err(c, "error in %s(): %s", __func__, bch2_err_str(ret));
942 bch2_btree_interior_updates_flush(c);
944 progress_list_del(c, stats);
948 static bool rereplicate_pred(struct bch_fs *c, void *arg,
950 struct bch_io_opts *io_opts,
951 struct data_update_opts *data_opts)
953 unsigned nr_good = bch2_bkey_durability(c, k);
954 unsigned replicas = bkey_is_btree_ptr(k.k)
955 ? c->opts.metadata_replicas
956 : io_opts->data_replicas;
958 if (!nr_good || nr_good >= replicas)
961 data_opts->target = 0;
962 data_opts->extra_replicas = replicas - nr_good;
963 data_opts->btree_insert_flags = 0;
967 static bool migrate_pred(struct bch_fs *c, void *arg,
969 struct bch_io_opts *io_opts,
970 struct data_update_opts *data_opts)
972 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
973 const struct bch_extent_ptr *ptr;
974 struct bch_ioctl_data *op = arg;
977 data_opts->rewrite_ptrs = 0;
978 data_opts->target = 0;
979 data_opts->extra_replicas = 0;
980 data_opts->btree_insert_flags = 0;
982 bkey_for_each_ptr(ptrs, ptr) {
983 if (ptr->dev == op->migrate.dev)
984 data_opts->rewrite_ptrs |= 1U << i;
988 return data_opts->rewrite_ptrs != 0;
991 static bool rereplicate_btree_pred(struct bch_fs *c, void *arg,
993 struct bch_io_opts *io_opts,
994 struct data_update_opts *data_opts)
996 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
999 static bool migrate_btree_pred(struct bch_fs *c, void *arg,
1001 struct bch_io_opts *io_opts,
1002 struct data_update_opts *data_opts)
1004 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1007 static bool bformat_needs_redo(struct bkey_format *f)
1011 for (i = 0; i < f->nr_fields; i++) {
1012 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
1013 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
1014 u64 field_offset = le64_to_cpu(f->field_offset[i]);
1016 if (f->bits_per_field[i] > unpacked_bits)
1019 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
1022 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1031 static bool rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1033 struct bch_io_opts *io_opts,
1034 struct data_update_opts *data_opts)
1036 if (b->version_ondisk != c->sb.version ||
1037 btree_node_need_rewrite(b) ||
1038 bformat_needs_redo(&b->format)) {
1039 data_opts->target = 0;
1040 data_opts->extra_replicas = 0;
1041 data_opts->btree_insert_flags = 0;
1048 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1052 ret = bch2_move_btree(c,
1054 BTREE_ID_NR, SPOS_MAX,
1055 rewrite_old_nodes_pred, c, stats);
1057 mutex_lock(&c->sb_lock);
1058 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1059 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1060 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1061 bch2_write_super(c);
1062 mutex_unlock(&c->sb_lock);
1068 int bch2_data_job(struct bch_fs *c,
1069 struct bch_move_stats *stats,
1070 struct bch_ioctl_data op)
1075 case BCH_DATA_OP_REREPLICATE:
1076 bch2_move_stats_init(stats, "rereplicate");
1077 stats->data_type = BCH_DATA_journal;
1078 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1080 ret = bch2_move_btree(c,
1081 op.start_btree, op.start_pos,
1082 op.end_btree, op.end_pos,
1083 rereplicate_btree_pred, c, stats) ?: ret;
1084 ret = bch2_replicas_gc2(c) ?: ret;
1086 ret = bch2_move_data(c,
1087 op.start_btree, op.start_pos,
1088 op.end_btree, op.end_pos,
1091 writepoint_hashed((unsigned long) current),
1093 rereplicate_pred, c) ?: ret;
1094 ret = bch2_replicas_gc2(c) ?: ret;
1096 case BCH_DATA_OP_MIGRATE:
1097 if (op.migrate.dev >= c->sb.nr_devices)
1100 bch2_move_stats_init(stats, "migrate");
1101 stats->data_type = BCH_DATA_journal;
1102 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1104 ret = bch2_move_btree(c,
1105 op.start_btree, op.start_pos,
1106 op.end_btree, op.end_pos,
1107 migrate_btree_pred, &op, stats) ?: ret;
1108 ret = bch2_replicas_gc2(c) ?: ret;
1110 ret = bch2_move_data(c,
1111 op.start_btree, op.start_pos,
1112 op.end_btree, op.end_pos,
1115 writepoint_hashed((unsigned long) current),
1117 migrate_pred, &op) ?: ret;
1118 ret = bch2_replicas_gc2(c) ?: ret;
1120 case BCH_DATA_OP_REWRITE_OLD_NODES:
1121 bch2_move_stats_init(stats, "rewrite_old_nodes");
1122 ret = bch2_scan_old_btree_nodes(c, stats);
1131 void bch2_data_jobs_to_text(struct printbuf *out, struct bch_fs *c)
1133 struct bch_move_stats *stats;
1135 mutex_lock(&c->data_progress_lock);
1136 list_for_each_entry(stats, &c->data_progress_list, list) {
1137 prt_printf(out, "%s: data type %s btree_id %s position: ",
1139 bch2_data_types[stats->data_type],
1140 bch2_btree_ids[stats->btree_id]);
1141 bch2_bpos_to_text(out, stats->pos);
1142 prt_printf(out, "%s", "\n");
1144 mutex_unlock(&c->data_progress_lock);
1147 static void bch2_moving_ctxt_to_text(struct printbuf *out, struct moving_context *ctxt)
1149 struct moving_io *io;
1151 prt_printf(out, "%ps:", ctxt->fn);
1153 printbuf_indent_add(out, 2);
1155 prt_printf(out, "reads: %u sectors %u",
1156 atomic_read(&ctxt->read_ios),
1157 atomic_read(&ctxt->read_sectors));
1160 prt_printf(out, "writes: %u sectors %u",
1161 atomic_read(&ctxt->write_ios),
1162 atomic_read(&ctxt->write_sectors));
1165 printbuf_indent_add(out, 2);
1167 mutex_lock(&ctxt->lock);
1168 list_for_each_entry(io, &ctxt->ios, io_list) {
1169 bch2_write_op_to_text(out, &io->write.op);
1171 mutex_unlock(&ctxt->lock);
1173 printbuf_indent_sub(out, 4);
1176 void bch2_fs_moving_ctxts_to_text(struct printbuf *out, struct bch_fs *c)
1178 struct moving_context *ctxt;
1180 mutex_lock(&c->moving_context_lock);
1181 list_for_each_entry(ctxt, &c->moving_context_list, list)
1182 bch2_moving_ctxt_to_text(out, ctxt);
1183 mutex_unlock(&c->moving_context_lock);
1186 void bch2_fs_move_init(struct bch_fs *c)
1188 INIT_LIST_HEAD(&c->moving_context_list);
1189 mutex_init(&c->moving_context_lock);
1191 INIT_LIST_HEAD(&c->data_progress_list);
1192 mutex_init(&c->data_progress_lock);