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"
12 #include "disk_groups.h"
17 #include "journal_reclaim.h"
20 #include "subvolume.h"
24 #include <linux/ioprio.h>
25 #include <linux/kthread.h>
27 #include <trace/events/bcachefs.h>
29 #define SECTORS_IN_FLIGHT_PER_DEVICE 2048
32 struct list_head list;
36 unsigned read_sectors;
37 unsigned write_sectors;
39 struct bch_read_bio rbio;
41 struct migrate_write write;
42 /* Must be last since it is variable size */
43 struct bio_vec bi_inline_vecs[0];
46 struct moving_context {
47 /* Closure for waiting on all reads and writes to complete */
50 struct bch_move_stats *stats;
52 struct list_head reads;
54 /* in flight sectors: */
55 atomic_t read_sectors;
56 atomic_t write_sectors;
58 wait_queue_head_t wait;
61 static int insert_snapshot_whiteouts(struct btree_trans *trans,
66 struct bch_fs *c = trans->c;
67 struct btree_iter iter, update_iter;
69 struct snapshots_seen s;
72 if (!btree_type_has_snapshots(id))
75 snapshots_seen_init(&s);
77 if (!bkey_cmp(old_pos, new_pos))
80 if (!snapshot_t(c, old_pos.snapshot)->children[0])
83 bch2_trans_iter_init(trans, &iter, id, old_pos,
84 BTREE_ITER_NOT_EXTENTS|
85 BTREE_ITER_ALL_SNAPSHOTS);
88 k = bch2_btree_iter_prev(&iter);
93 if (bkey_cmp(old_pos, k.k->p))
96 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) {
97 struct bkey_i *update;
100 darray_for_each(s.ids, i)
101 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, *i))
104 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
106 ret = PTR_ERR_OR_ZERO(update);
110 bkey_init(&update->k);
111 update->k.p = new_pos;
112 update->k.p.snapshot = k.k->p.snapshot;
114 bch2_trans_iter_init(trans, &update_iter, id, update->k.p,
115 BTREE_ITER_NOT_EXTENTS|
116 BTREE_ITER_ALL_SNAPSHOTS|
118 ret = bch2_btree_iter_traverse(&update_iter) ?:
119 bch2_trans_update(trans, &update_iter, update,
120 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
121 bch2_trans_iter_exit(trans, &update_iter);
125 ret = snapshots_seen_add(c, &s, k.k->p.snapshot);
130 bch2_trans_iter_exit(trans, &iter);
136 static int bch2_migrate_index_update(struct bch_write_op *op)
138 struct bch_fs *c = op->c;
139 struct btree_trans trans;
140 struct btree_iter iter;
141 struct migrate_write *m =
142 container_of(op, struct migrate_write, op);
143 struct open_bucket *ec_ob = ec_open_bucket(c, &op->open_buckets);
144 struct keylist *keys = &op->insert_keys;
145 struct bkey_buf _new, _insert;
148 bch2_bkey_buf_init(&_new);
149 bch2_bkey_buf_init(&_insert);
150 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
152 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
154 bch2_trans_iter_init(&trans, &iter, m->btree_id,
155 bkey_start_pos(&bch2_keylist_front(keys)->k),
156 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
160 struct bkey_i *insert;
161 struct bkey_i_extent *new;
162 const union bch_extent_entry *entry;
163 struct extent_ptr_decoded p;
164 struct bpos next_pos;
165 bool did_work = false;
166 bool should_check_enospc;
167 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
169 bch2_trans_begin(&trans);
171 k = bch2_btree_iter_peek_slot(&iter);
176 new = bkey_i_to_extent(bch2_keylist_front(keys));
178 if (bversion_cmp(k.k->version, new->k.version) ||
179 !bch2_bkey_matches_ptr(c, k, m->ptr, m->offset))
182 bkey_reassemble(_insert.k, k);
185 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
186 new = bkey_i_to_extent(_new.k);
187 bch2_cut_front(iter.pos, &new->k_i);
189 bch2_cut_front(iter.pos, insert);
190 bch2_cut_back(new->k.p, insert);
191 bch2_cut_back(insert->k.p, &new->k_i);
193 if (m->data_cmd == DATA_REWRITE) {
194 struct bch_extent_ptr *new_ptr, *old_ptr = (void *)
195 bch2_bkey_has_device(bkey_i_to_s_c(insert),
196 m->data_opts.rewrite_dev);
201 extent_for_each_ptr(extent_i_to_s(new), new_ptr)
202 new_ptr->cached = true;
204 __bch2_bkey_drop_ptr(bkey_i_to_s(insert), old_ptr);
207 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
208 if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
210 * raced with another move op? extent already
211 * has a pointer to the device we just wrote
217 bch2_extent_ptr_decoded_append(insert, &p);
224 bch2_bkey_narrow_crcs(insert,
225 (struct bch_extent_crc_unpacked) { 0 });
226 bch2_extent_normalize(c, bkey_i_to_s(insert));
227 bch2_bkey_mark_replicas_cached(c, bkey_i_to_s(insert),
228 op->opts.background_target,
229 op->opts.data_replicas);
231 ret = bch2_sum_sector_overwrites(&trans, &iter, insert,
232 &should_check_enospc,
234 &disk_sectors_delta);
238 if (disk_sectors_delta > (s64) op->res.sectors) {
239 ret = bch2_disk_reservation_add(c, &op->res,
240 disk_sectors_delta - op->res.sectors,
242 ? BCH_DISK_RESERVATION_NOFAIL : 0);
247 next_pos = insert->k.p;
249 ret = insert_snapshot_whiteouts(&trans, m->btree_id,
250 k.k->p, insert->k.p) ?:
251 bch2_trans_update(&trans, &iter, insert,
252 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
253 bch2_trans_commit(&trans, &op->res,
256 m->data_opts.btree_insert_flags);
258 bch2_btree_iter_set_pos(&iter, next_pos);
259 atomic_long_inc(&c->extent_migrate_done);
261 bch2_ob_add_backpointer(c, ec_ob, &insert->k);
269 while (bkey_cmp(iter.pos, bch2_keylist_front(keys)->k.p) >= 0) {
270 bch2_keylist_pop_front(keys);
271 if (bch2_keylist_empty(keys))
277 BUG_ON(k.k->p.offset <= iter.pos.offset);
278 atomic64_inc(&m->ctxt->stats->keys_raced);
279 atomic64_add(k.k->p.offset - iter.pos.offset,
280 &m->ctxt->stats->sectors_raced);
282 atomic_long_inc(&c->extent_migrate_raced);
283 trace_move_race(&new->k);
284 bch2_btree_iter_advance(&iter);
288 bch2_trans_iter_exit(&trans, &iter);
289 bch2_trans_exit(&trans);
290 bch2_bkey_buf_exit(&_insert, c);
291 bch2_bkey_buf_exit(&_new, c);
292 BUG_ON(ret == -EINTR);
296 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
298 /* write bio must own pages: */
299 BUG_ON(!m->op.wbio.bio.bi_vcnt);
301 m->ptr = rbio->pick.ptr;
302 m->offset = rbio->data_pos.offset - rbio->pick.crc.offset;
303 m->op.devs_have = rbio->devs_have;
304 m->op.pos = rbio->data_pos;
305 m->op.version = rbio->version;
306 m->op.crc = rbio->pick.crc;
307 m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
309 if (m->data_cmd == DATA_REWRITE)
310 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
313 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
314 struct write_point_specifier wp,
315 struct bch_io_opts io_opts,
316 enum data_cmd data_cmd,
317 struct data_opts data_opts,
318 enum btree_id btree_id,
321 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
322 const union bch_extent_entry *entry;
323 struct bch_extent_crc_unpacked crc;
324 struct extent_ptr_decoded p;
327 m->btree_id = btree_id;
328 m->data_cmd = data_cmd;
329 m->data_opts = data_opts;
330 m->nr_ptrs_reserved = 0;
332 bch2_write_op_init(&m->op, c, io_opts);
334 if (!bch2_bkey_is_incompressible(k))
335 m->op.compression_type =
336 bch2_compression_opt_to_type[io_opts.background_compression ?:
337 io_opts.compression];
339 m->op.incompressible = true;
341 m->op.target = data_opts.target,
342 m->op.write_point = wp;
345 * op->csum_type is normally initialized from the fs/file's current
346 * options - but if an extent is encrypted, we require that it stays
349 bkey_for_each_crc(k.k, ptrs, crc, entry)
350 if (bch2_csum_type_is_encryption(crc.csum_type)) {
351 m->op.nonce = crc.nonce + crc.offset;
352 m->op.csum_type = crc.csum_type;
356 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE) {
357 m->op.alloc_reserve = RESERVE_movinggc;
359 /* XXX: this should probably be passed in */
360 m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS;
363 m->op.flags |= BCH_WRITE_PAGES_STABLE|
364 BCH_WRITE_PAGES_OWNED|
365 BCH_WRITE_DATA_ENCODED|
366 BCH_WRITE_FROM_INTERNAL;
368 m->op.nr_replicas = data_opts.nr_replicas;
369 m->op.nr_replicas_required = data_opts.nr_replicas;
370 m->op.index_update_fn = bch2_migrate_index_update;
373 case DATA_ADD_REPLICAS: {
375 * DATA_ADD_REPLICAS is used for moving data to a different
376 * device in the background, and due to compression the new copy
377 * might take up more space than the old copy:
380 int nr = (int) io_opts.data_replicas -
381 bch2_bkey_nr_ptrs_allocated(k);
383 int nr = (int) io_opts.data_replicas;
386 m->op.nr_replicas = m->nr_ptrs_reserved = nr;
388 ret = bch2_disk_reservation_get(c, &m->op.res,
389 k.k->size, m->op.nr_replicas, 0);
396 unsigned compressed_sectors = 0;
398 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
399 if (p.ptr.dev == data_opts.rewrite_dev) {
401 m->op.flags |= BCH_WRITE_CACHED;
404 crc_is_compressed(p.crc))
405 compressed_sectors += p.crc.compressed_size;
408 if (compressed_sectors) {
409 ret = bch2_disk_reservation_add(c, &m->op.res,
410 k.k->size * m->op.nr_replicas,
411 BCH_DISK_RESERVATION_NOFAIL);
418 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
419 m->op.flags |= BCH_WRITE_CACHED;
428 static void move_free(struct closure *cl)
430 struct moving_io *io = container_of(cl, struct moving_io, cl);
431 struct moving_context *ctxt = io->write.ctxt;
432 struct bvec_iter_all iter;
435 bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
437 bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter)
439 __free_page(bv->bv_page);
441 wake_up(&ctxt->wait);
446 static void move_write_done(struct closure *cl)
448 struct moving_io *io = container_of(cl, struct moving_io, cl);
450 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
451 closure_return_with_destructor(cl, move_free);
454 static void move_write(struct closure *cl)
456 struct moving_io *io = container_of(cl, struct moving_io, cl);
458 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
459 closure_return_with_destructor(cl, move_free);
463 bch2_migrate_read_done(&io->write, &io->rbio);
465 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
466 closure_call(&io->write.op.cl, bch2_write, NULL, cl);
467 continue_at(cl, move_write_done, NULL);
470 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
472 struct moving_io *io =
473 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
475 return io && io->read_completed ? io : NULL;
478 static void move_read_endio(struct bio *bio)
480 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
481 struct moving_context *ctxt = io->write.ctxt;
483 atomic_sub(io->read_sectors, &ctxt->read_sectors);
484 io->read_completed = true;
486 wake_up(&ctxt->wait);
487 closure_put(&ctxt->cl);
490 static void do_pending_writes(struct moving_context *ctxt, struct btree_trans *trans)
492 struct moving_io *io;
495 bch2_trans_unlock(trans);
497 while ((io = next_pending_write(ctxt))) {
499 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
503 #define move_ctxt_wait_event(_ctxt, _trans, _cond) \
505 do_pending_writes(_ctxt, _trans); \
509 __wait_event((_ctxt)->wait, \
510 next_pending_write(_ctxt) || (_cond)); \
513 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt,
514 struct btree_trans *trans)
516 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
518 move_ctxt_wait_event(ctxt, trans,
519 !atomic_read(&ctxt->write_sectors) ||
520 atomic_read(&ctxt->write_sectors) != sectors_pending);
523 static int bch2_move_extent(struct btree_trans *trans,
524 struct moving_context *ctxt,
525 struct write_point_specifier wp,
526 struct bch_io_opts io_opts,
527 enum btree_id btree_id,
529 enum data_cmd data_cmd,
530 struct data_opts data_opts)
532 struct bch_fs *c = trans->c;
533 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
534 struct moving_io *io;
535 const union bch_extent_entry *entry;
536 struct extent_ptr_decoded p;
537 unsigned sectors = k.k->size, pages;
540 /* write path might have to decompress data: */
541 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
542 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
544 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
545 io = kzalloc(sizeof(struct moving_io) +
546 sizeof(struct bio_vec) * pages, GFP_KERNEL);
550 io->write.ctxt = ctxt;
551 io->read_sectors = k.k->size;
552 io->write_sectors = k.k->size;
554 bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0);
555 bio_set_prio(&io->write.op.wbio.bio,
556 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
558 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
563 io->rbio.opts = io_opts;
564 bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0);
565 io->rbio.bio.bi_vcnt = pages;
566 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
567 io->rbio.bio.bi_iter.bi_size = sectors << 9;
569 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
570 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
571 io->rbio.bio.bi_end_io = move_read_endio;
573 ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
574 data_cmd, data_opts, btree_id, k);
578 atomic64_inc(&ctxt->stats->keys_moved);
579 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
580 this_cpu_add(c->counters[BCH_COUNTER_io_move], k.k->size);
582 trace_move_extent(k.k);
584 atomic_add(io->read_sectors, &ctxt->read_sectors);
585 list_add_tail(&io->list, &ctxt->reads);
588 * dropped by move_read_endio() - guards against use after free of
589 * ctxt when doing wakeup
591 closure_get(&ctxt->cl);
592 bch2_read_extent(trans, &io->rbio,
596 BCH_READ_LAST_FRAGMENT);
599 bio_free_pages(&io->write.op.wbio.bio);
603 trace_move_alloc_mem_fail(k.k);
607 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
608 struct bch_inode_unpacked *inode)
610 struct btree_iter iter;
614 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
615 BTREE_ITER_ALL_SNAPSHOTS);
616 k = bch2_btree_iter_peek(&iter);
621 if (!k.k || bkey_cmp(k.k->p, pos)) {
626 ret = bkey_is_inode(k.k) ? 0 : -EIO;
630 ret = bch2_inode_unpack(k, inode);
634 bch2_trans_iter_exit(trans, &iter);
638 static int move_ratelimit(struct btree_trans *trans,
639 struct moving_context *ctxt,
640 struct bch_ratelimit *rate)
645 delay = rate ? bch2_ratelimit_delay(rate) : 0;
648 bch2_trans_unlock(trans);
649 set_current_state(TASK_INTERRUPTIBLE);
652 if ((current->flags & PF_KTHREAD) && kthread_should_stop()) {
653 __set_current_state(TASK_RUNNING);
658 schedule_timeout(delay);
660 if (unlikely(freezing(current))) {
661 move_ctxt_wait_event(ctxt, trans, list_empty(&ctxt->reads));
666 move_ctxt_wait_event(ctxt, trans,
667 atomic_read(&ctxt->write_sectors) <
668 SECTORS_IN_FLIGHT_PER_DEVICE);
670 move_ctxt_wait_event(ctxt, trans,
671 atomic_read(&ctxt->read_sectors) <
672 SECTORS_IN_FLIGHT_PER_DEVICE);
677 static int move_get_io_opts(struct btree_trans *trans,
678 struct bch_io_opts *io_opts,
679 struct bkey_s_c k, u64 *cur_inum)
681 struct bch_inode_unpacked inode;
684 if (*cur_inum == k.k->p.inode)
687 *io_opts = bch2_opts_to_inode_opts(trans->c->opts);
689 ret = lookup_inode(trans,
690 SPOS(0, k.k->p.inode, k.k->p.snapshot),
696 bch2_io_opts_apply(io_opts, bch2_inode_opts_get(&inode));
698 *cur_inum = k.k->p.inode;
702 static int __bch2_move_data(struct bch_fs *c,
703 struct moving_context *ctxt,
704 struct bch_ratelimit *rate,
705 struct write_point_specifier wp,
708 move_pred_fn pred, void *arg,
709 struct bch_move_stats *stats,
710 enum btree_id btree_id)
712 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
714 struct btree_trans trans;
715 struct btree_iter iter;
717 struct data_opts data_opts;
718 enum data_cmd data_cmd;
719 u64 cur_inum = U64_MAX;
722 bch2_bkey_buf_init(&sk);
723 bch2_trans_init(&trans, c, 0, 0);
725 stats->data_type = BCH_DATA_user;
726 stats->btree_id = btree_id;
729 bch2_trans_iter_init(&trans, &iter, btree_id, start,
731 BTREE_ITER_ALL_SNAPSHOTS);
734 bch2_ratelimit_reset(rate);
736 while (!move_ratelimit(&trans, ctxt, rate)) {
737 bch2_trans_begin(&trans);
739 k = bch2_btree_iter_peek(&iter);
749 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
752 stats->pos = iter.pos;
754 if (!bkey_extent_is_direct_data(k.k))
757 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
761 switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) {
766 case DATA_ADD_REPLICAS:
775 * The iterator gets unlocked by __bch2_read_extent - need to
776 * save a copy of @k elsewhere:
778 bch2_bkey_buf_reassemble(&sk, c, k);
779 k = bkey_i_to_s_c(sk.k);
781 ret2 = bch2_move_extent(&trans, ctxt, wp, io_opts, btree_id, k,
782 data_cmd, data_opts);
787 if (ret2 == -ENOMEM) {
788 /* memory allocation failure, wait for some IO to finish */
789 bch2_move_ctxt_wait_for_io(ctxt, &trans);
793 /* XXX signal failure */
798 bch2_ratelimit_increment(rate, k.k->size);
800 atomic64_add(k.k->size, &stats->sectors_seen);
802 bch2_btree_iter_advance(&iter);
805 bch2_trans_iter_exit(&trans, &iter);
806 bch2_trans_exit(&trans);
807 bch2_bkey_buf_exit(&sk, c);
812 inline void bch_move_stats_init(struct bch_move_stats *stats, char *name)
814 memset(stats, 0, sizeof(*stats));
816 scnprintf(stats->name, sizeof(stats->name),
820 static inline void progress_list_add(struct bch_fs *c,
821 struct bch_move_stats *stats)
823 mutex_lock(&c->data_progress_lock);
824 list_add(&stats->list, &c->data_progress_list);
825 mutex_unlock(&c->data_progress_lock);
828 static inline void progress_list_del(struct bch_fs *c,
829 struct bch_move_stats *stats)
831 mutex_lock(&c->data_progress_lock);
832 list_del(&stats->list);
833 mutex_unlock(&c->data_progress_lock);
836 int bch2_move_data(struct bch_fs *c,
837 enum btree_id start_btree_id, struct bpos start_pos,
838 enum btree_id end_btree_id, struct bpos end_pos,
839 struct bch_ratelimit *rate,
840 struct write_point_specifier wp,
841 move_pred_fn pred, void *arg,
842 struct bch_move_stats *stats)
844 struct moving_context ctxt = { .stats = stats };
848 progress_list_add(c, stats);
849 closure_init_stack(&ctxt.cl);
850 INIT_LIST_HEAD(&ctxt.reads);
851 init_waitqueue_head(&ctxt.wait);
853 stats->data_type = BCH_DATA_user;
855 for (id = start_btree_id;
856 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
858 stats->btree_id = id;
860 if (id != BTREE_ID_extents &&
861 id != BTREE_ID_reflink)
864 ret = __bch2_move_data(c, &ctxt, rate, wp,
865 id == start_btree_id ? start_pos : POS_MIN,
866 id == end_btree_id ? end_pos : POS_MAX,
867 pred, arg, stats, id);
872 move_ctxt_wait_event(&ctxt, NULL, list_empty(&ctxt.reads));
873 closure_sync(&ctxt.cl);
875 EBUG_ON(atomic_read(&ctxt.write_sectors));
878 atomic64_read(&stats->sectors_moved),
879 atomic64_read(&stats->keys_moved));
881 progress_list_del(c, stats);
885 static int verify_bucket_evacuated(struct btree_trans *trans, struct bpos bucket, int gen)
887 struct bch_fs *c = trans->c;
888 struct btree_iter iter;
892 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
893 bucket, BTREE_ITER_CACHED);
894 k = bch2_btree_iter_peek_slot(&iter);
897 if (!ret && k.k->type == KEY_TYPE_alloc_v4) {
898 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
900 if (a.v->gen == gen &&
901 a.v->dirty_sectors) {
902 struct printbuf buf = PRINTBUF;
904 prt_str(&buf, "failed to evacuate bucket ");
905 bch2_bkey_val_to_text(&buf, c, k);
907 bch_err_ratelimited(c, "%s", buf.buf);
912 bch2_trans_iter_exit(trans, &iter);
916 int bch2_evacuate_bucket(struct bch_fs *c,
917 struct bpos bucket, int gen,
918 struct bch_ratelimit *rate,
919 struct write_point_specifier wp,
920 enum data_cmd data_cmd,
921 struct data_opts *data_opts,
922 struct bch_move_stats *stats)
924 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
925 struct moving_context ctxt = { .stats = stats };
926 struct btree_trans trans;
927 struct btree_iter iter;
929 struct bch_backpointer bp;
930 u64 bp_offset = 0, cur_inum = U64_MAX;
933 bch2_bkey_buf_init(&sk);
934 bch2_trans_init(&trans, c, 0, 0);
935 progress_list_add(c, stats);
936 closure_init_stack(&ctxt.cl);
937 INIT_LIST_HEAD(&ctxt.reads);
938 init_waitqueue_head(&ctxt.wait);
940 stats->data_type = BCH_DATA_user;
942 while (!(ret = move_ratelimit(&trans, &ctxt, rate))) {
943 bch2_trans_begin(&trans);
945 ret = bch2_get_next_backpointer(&trans, bucket, gen,
951 if (bp_offset == U64_MAX)
957 k = bch2_backpointer_get_key(&trans, &iter,
958 bucket, bp_offset, bp);
967 bch2_bkey_buf_reassemble(&sk, c, k);
968 k = bkey_i_to_s_c(sk.k);
969 bch2_trans_iter_exit(&trans, &iter);
971 ret = move_get_io_opts(&trans, &io_opts, k, &cur_inum);
975 data_opts->target = io_opts.background_target;
976 data_opts->rewrite_dev = bucket.inode;
978 ret = bch2_move_extent(&trans, &ctxt, wp, io_opts, bp.btree_id, k,
979 data_cmd, *data_opts);
982 if (ret == -ENOMEM) {
983 /* memory allocation failure, wait for some IO to finish */
984 bch2_move_ctxt_wait_for_io(&ctxt, &trans);
991 bch2_ratelimit_increment(rate, k.k->size);
992 atomic64_add(k.k->size, &stats->sectors_seen);
996 b = bch2_backpointer_get_node(&trans, &iter,
997 bucket, bp_offset, bp);
998 ret = PTR_ERR_OR_ZERO(b);
1006 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0);
1007 bch2_trans_iter_exit(&trans, &iter);
1015 bch2_ratelimit_increment(rate, c->opts.btree_node_size >> 9);
1016 atomic64_add(c->opts.btree_node_size >> 9, &stats->sectors_seen);
1017 atomic64_add(c->opts.btree_node_size >> 9, &stats->sectors_moved);
1023 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && gen >= 0) {
1024 bch2_trans_unlock(&trans);
1025 move_ctxt_wait_event(&ctxt, NULL, list_empty(&ctxt.reads));
1026 closure_sync(&ctxt.cl);
1027 lockrestart_do(&trans, verify_bucket_evacuated(&trans, bucket, gen));
1030 bch2_trans_exit(&trans);
1031 bch2_bkey_buf_exit(&sk, c);
1033 move_ctxt_wait_event(&ctxt, NULL, list_empty(&ctxt.reads));
1034 closure_sync(&ctxt.cl);
1035 progress_list_del(c, stats);
1037 EBUG_ON(atomic_read(&ctxt.write_sectors));
1040 atomic64_read(&stats->sectors_moved),
1041 atomic64_read(&stats->keys_moved));
1046 typedef enum data_cmd (*move_btree_pred)(struct bch_fs *, void *,
1047 struct btree *, struct bch_io_opts *,
1048 struct data_opts *);
1050 static int bch2_move_btree(struct bch_fs *c,
1051 enum btree_id start_btree_id, struct bpos start_pos,
1052 enum btree_id end_btree_id, struct bpos end_pos,
1053 move_btree_pred pred, void *arg,
1054 struct bch_move_stats *stats)
1056 bool kthread = (current->flags & PF_KTHREAD) != 0;
1057 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
1058 struct btree_trans trans;
1059 struct btree_iter iter;
1062 struct data_opts data_opts;
1066 bch2_trans_init(&trans, c, 0, 0);
1067 progress_list_add(c, stats);
1069 stats->data_type = BCH_DATA_btree;
1071 for (id = start_btree_id;
1072 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
1074 stats->btree_id = id;
1076 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
1077 BTREE_ITER_PREFETCH);
1080 while (bch2_trans_begin(&trans),
1081 (b = bch2_btree_iter_peek_node(&iter)) &&
1082 !(ret = PTR_ERR_OR_ZERO(b))) {
1083 if (kthread && kthread_should_stop())
1086 if ((cmp_int(id, end_btree_id) ?:
1087 bpos_cmp(b->key.k.p, end_pos)) > 0)
1090 stats->pos = iter.pos;
1092 switch ((cmd = pred(c, arg, b, &io_opts, &data_opts))) {
1097 case DATA_ADD_REPLICAS:
1104 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
1110 bch2_btree_iter_next_node(&iter);
1115 bch2_trans_iter_exit(&trans, &iter);
1117 if (kthread && kthread_should_stop())
1121 bch2_trans_exit(&trans);
1124 bch_err(c, "error %i in bch2_move_btree", ret);
1126 bch2_btree_interior_updates_flush(c);
1128 progress_list_del(c, stats);
1133 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
1135 struct bch_io_opts *io_opts,
1136 struct data_opts *data_opts)
1142 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
1144 struct bch_io_opts *io_opts,
1145 struct data_opts *data_opts)
1147 unsigned nr_good = bch2_bkey_durability(c, k);
1148 unsigned replicas = bkey_is_btree_ptr(k.k)
1149 ? c->opts.metadata_replicas
1150 : io_opts->data_replicas;
1152 if (!nr_good || nr_good >= replicas)
1155 data_opts->target = 0;
1156 data_opts->nr_replicas = 1;
1157 data_opts->btree_insert_flags = 0;
1158 return DATA_ADD_REPLICAS;
1161 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
1163 struct bch_io_opts *io_opts,
1164 struct data_opts *data_opts)
1166 struct bch_ioctl_data *op = arg;
1168 if (!bch2_bkey_has_device(k, op->migrate.dev))
1171 data_opts->target = 0;
1172 data_opts->nr_replicas = 1;
1173 data_opts->btree_insert_flags = 0;
1174 data_opts->rewrite_dev = op->migrate.dev;
1175 return DATA_REWRITE;
1178 static enum data_cmd rereplicate_btree_pred(struct bch_fs *c, void *arg,
1180 struct bch_io_opts *io_opts,
1181 struct data_opts *data_opts)
1183 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1186 static enum data_cmd migrate_btree_pred(struct bch_fs *c, void *arg,
1188 struct bch_io_opts *io_opts,
1189 struct data_opts *data_opts)
1191 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1194 static bool bformat_needs_redo(struct bkey_format *f)
1198 for (i = 0; i < f->nr_fields; i++) {
1199 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
1200 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
1201 u64 field_offset = le64_to_cpu(f->field_offset[i]);
1203 if (f->bits_per_field[i] > unpacked_bits)
1206 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
1209 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1218 static enum data_cmd rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1220 struct bch_io_opts *io_opts,
1221 struct data_opts *data_opts)
1223 if (b->version_ondisk != c->sb.version ||
1224 btree_node_need_rewrite(b) ||
1225 bformat_needs_redo(&b->format)) {
1226 data_opts->target = 0;
1227 data_opts->nr_replicas = 1;
1228 data_opts->btree_insert_flags = 0;
1229 return DATA_REWRITE;
1235 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1239 ret = bch2_move_btree(c,
1241 BTREE_ID_NR, SPOS_MAX,
1242 rewrite_old_nodes_pred, c, stats);
1244 mutex_lock(&c->sb_lock);
1245 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1246 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1247 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1248 bch2_write_super(c);
1249 mutex_unlock(&c->sb_lock);
1255 int bch2_data_job(struct bch_fs *c,
1256 struct bch_move_stats *stats,
1257 struct bch_ioctl_data op)
1262 case BCH_DATA_OP_REREPLICATE:
1263 bch_move_stats_init(stats, "rereplicate");
1264 stats->data_type = BCH_DATA_journal;
1265 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1267 ret = bch2_move_btree(c,
1268 op.start_btree, op.start_pos,
1269 op.end_btree, op.end_pos,
1270 rereplicate_btree_pred, c, stats) ?: ret;
1271 ret = bch2_replicas_gc2(c) ?: ret;
1273 ret = bch2_move_data(c,
1274 op.start_btree, op.start_pos,
1275 op.end_btree, op.end_pos,
1276 NULL, writepoint_hashed((unsigned long) current),
1277 rereplicate_pred, c, stats) ?: ret;
1278 ret = bch2_replicas_gc2(c) ?: ret;
1280 case BCH_DATA_OP_MIGRATE:
1281 if (op.migrate.dev >= c->sb.nr_devices)
1284 bch_move_stats_init(stats, "migrate");
1285 stats->data_type = BCH_DATA_journal;
1286 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1288 ret = bch2_move_btree(c,
1289 op.start_btree, op.start_pos,
1290 op.end_btree, op.end_pos,
1291 migrate_btree_pred, &op, stats) ?: ret;
1292 ret = bch2_replicas_gc2(c) ?: ret;
1294 ret = bch2_move_data(c,
1295 op.start_btree, op.start_pos,
1296 op.end_btree, op.end_pos,
1297 NULL, writepoint_hashed((unsigned long) current),
1298 migrate_pred, &op, stats) ?: ret;
1299 ret = bch2_replicas_gc2(c) ?: ret;
1301 case BCH_DATA_OP_REWRITE_OLD_NODES:
1302 bch_move_stats_init(stats, "rewrite_old_nodes");
1303 ret = bch2_scan_old_btree_nodes(c, stats);