1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
7 #include "btree_update.h"
8 #include "btree_update_interior.h"
10 #include "disk_groups.h"
14 #include "journal_reclaim.h"
17 #include "subvolume.h"
21 #include <linux/ioprio.h>
22 #include <linux/kthread.h>
24 #include <trace/events/bcachefs.h>
26 #define SECTORS_IN_FLIGHT_PER_DEVICE 2048
29 struct list_head list;
33 unsigned read_sectors;
34 unsigned write_sectors;
36 struct bch_read_bio rbio;
38 struct migrate_write write;
39 /* Must be last since it is variable size */
40 struct bio_vec bi_inline_vecs[0];
43 struct moving_context {
44 /* Closure for waiting on all reads and writes to complete */
47 struct bch_move_stats *stats;
49 struct list_head reads;
51 /* in flight sectors: */
52 atomic_t read_sectors;
53 atomic_t write_sectors;
55 wait_queue_head_t wait;
58 static int insert_snapshot_whiteouts(struct btree_trans *trans,
63 struct bch_fs *c = trans->c;
64 struct btree_iter iter, update_iter;
66 struct snapshots_seen s;
69 if (!btree_type_has_snapshots(id))
72 snapshots_seen_init(&s);
74 if (!bkey_cmp(old_pos, new_pos))
77 if (!snapshot_t(c, old_pos.snapshot)->children[0])
80 bch2_trans_iter_init(trans, &iter, id, old_pos,
81 BTREE_ITER_NOT_EXTENTS|
82 BTREE_ITER_ALL_SNAPSHOTS);
85 k = bch2_btree_iter_prev(&iter);
90 if (bkey_cmp(old_pos, k.k->p))
93 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) {
94 struct bkey_i *update;
97 for (i = 0; i < s.nr; i++)
98 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, s.d[i]))
101 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
103 ret = PTR_ERR_OR_ZERO(update);
107 bkey_init(&update->k);
108 update->k.p = new_pos;
109 update->k.p.snapshot = k.k->p.snapshot;
111 bch2_trans_iter_init(trans, &update_iter, id, update->k.p,
112 BTREE_ITER_NOT_EXTENTS|
113 BTREE_ITER_ALL_SNAPSHOTS|
115 ret = bch2_btree_iter_traverse(&update_iter) ?:
116 bch2_trans_update(trans, &update_iter, update,
117 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
118 bch2_trans_iter_exit(trans, &update_iter);
122 ret = snapshots_seen_add(c, &s, k.k->p.snapshot);
127 bch2_trans_iter_exit(trans, &iter);
133 static int bch2_migrate_index_update(struct bch_write_op *op)
135 struct bch_fs *c = op->c;
136 struct btree_trans trans;
137 struct btree_iter iter;
138 struct migrate_write *m =
139 container_of(op, struct migrate_write, op);
140 struct open_bucket *ec_ob = ec_open_bucket(c, &op->open_buckets);
141 struct keylist *keys = &op->insert_keys;
142 struct bkey_buf _new, _insert;
145 bch2_bkey_buf_init(&_new);
146 bch2_bkey_buf_init(&_insert);
147 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
149 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
151 bch2_trans_iter_init(&trans, &iter, m->btree_id,
152 bkey_start_pos(&bch2_keylist_front(keys)->k),
153 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
157 struct bkey_i *insert;
158 struct bkey_i_extent *new;
159 const union bch_extent_entry *entry;
160 struct extent_ptr_decoded p;
161 struct bpos next_pos;
162 bool did_work = false;
163 bool extending = false, should_check_enospc;
164 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
166 bch2_trans_begin(&trans);
168 k = bch2_btree_iter_peek_slot(&iter);
173 new = bkey_i_to_extent(bch2_keylist_front(keys));
175 if (bversion_cmp(k.k->version, new->k.version) ||
176 !bch2_bkey_matches_ptr(c, k, m->ptr, m->offset))
179 bkey_reassemble(_insert.k, k);
182 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
183 new = bkey_i_to_extent(_new.k);
184 bch2_cut_front(iter.pos, &new->k_i);
186 bch2_cut_front(iter.pos, insert);
187 bch2_cut_back(new->k.p, insert);
188 bch2_cut_back(insert->k.p, &new->k_i);
190 if (m->data_cmd == DATA_REWRITE) {
191 struct bch_extent_ptr *new_ptr, *old_ptr = (void *)
192 bch2_bkey_has_device(bkey_i_to_s_c(insert),
193 m->data_opts.rewrite_dev);
198 extent_for_each_ptr(extent_i_to_s(new), new_ptr)
199 new_ptr->cached = true;
201 __bch2_bkey_drop_ptr(bkey_i_to_s(insert), old_ptr);
204 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
205 if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
207 * raced with another move op? extent already
208 * has a pointer to the device we just wrote
214 bch2_extent_ptr_decoded_append(insert, &p);
221 bch2_bkey_narrow_crcs(insert,
222 (struct bch_extent_crc_unpacked) { 0 });
223 bch2_extent_normalize(c, bkey_i_to_s(insert));
224 bch2_bkey_mark_replicas_cached(c, bkey_i_to_s(insert),
225 op->opts.background_target,
226 op->opts.data_replicas);
228 ret = bch2_sum_sector_overwrites(&trans, &iter, insert,
230 &should_check_enospc,
232 &disk_sectors_delta);
236 if (disk_sectors_delta > (s64) op->res.sectors) {
237 ret = bch2_disk_reservation_add(c, &op->res,
238 disk_sectors_delta - op->res.sectors,
240 ? BCH_DISK_RESERVATION_NOFAIL : 0);
245 next_pos = insert->k.p;
247 ret = insert_snapshot_whiteouts(&trans, m->btree_id,
248 k.k->p, insert->k.p) ?:
249 bch2_trans_update(&trans, &iter, insert,
250 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
251 bch2_trans_commit(&trans, &op->res,
254 m->data_opts.btree_insert_flags);
256 bch2_btree_iter_set_pos(&iter, next_pos);
257 atomic_long_inc(&c->extent_migrate_done);
259 bch2_ob_add_backpointer(c, ec_ob, &insert->k);
267 while (bkey_cmp(iter.pos, bch2_keylist_front(keys)->k.p) >= 0) {
268 bch2_keylist_pop_front(keys);
269 if (bch2_keylist_empty(keys))
275 BUG_ON(k.k->p.offset <= iter.pos.offset);
276 atomic64_inc(&m->ctxt->stats->keys_raced);
277 atomic64_add(k.k->p.offset - iter.pos.offset,
278 &m->ctxt->stats->sectors_raced);
280 atomic_long_inc(&c->extent_migrate_raced);
281 trace_move_race(&new->k);
282 bch2_btree_iter_advance(&iter);
286 bch2_trans_iter_exit(&trans, &iter);
287 bch2_trans_exit(&trans);
288 bch2_bkey_buf_exit(&_insert, c);
289 bch2_bkey_buf_exit(&_new, c);
290 BUG_ON(ret == -EINTR);
294 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
296 /* write bio must own pages: */
297 BUG_ON(!m->op.wbio.bio.bi_vcnt);
299 m->ptr = rbio->pick.ptr;
300 m->offset = rbio->data_pos.offset - rbio->pick.crc.offset;
301 m->op.devs_have = rbio->devs_have;
302 m->op.pos = rbio->data_pos;
303 m->op.version = rbio->version;
304 m->op.crc = rbio->pick.crc;
305 m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
307 if (m->data_cmd == DATA_REWRITE)
308 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
311 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
312 struct write_point_specifier wp,
313 struct bch_io_opts io_opts,
314 enum data_cmd data_cmd,
315 struct data_opts data_opts,
316 enum btree_id btree_id,
319 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
320 const union bch_extent_entry *entry;
321 struct bch_extent_crc_unpacked crc;
322 struct extent_ptr_decoded p;
325 m->btree_id = btree_id;
326 m->data_cmd = data_cmd;
327 m->data_opts = data_opts;
328 m->nr_ptrs_reserved = 0;
330 bch2_write_op_init(&m->op, c, io_opts);
332 if (!bch2_bkey_is_incompressible(k))
333 m->op.compression_type =
334 bch2_compression_opt_to_type[io_opts.background_compression ?:
335 io_opts.compression];
337 m->op.incompressible = true;
339 m->op.target = data_opts.target,
340 m->op.write_point = wp;
343 * op->csum_type is normally initialized from the fs/file's current
344 * options - but if an extent is encrypted, we require that it stays
347 bkey_for_each_crc(k.k, ptrs, crc, entry)
348 if (bch2_csum_type_is_encryption(crc.csum_type)) {
349 m->op.nonce = crc.nonce + crc.offset;
350 m->op.csum_type = crc.csum_type;
354 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE) {
355 m->op.alloc_reserve = RESERVE_MOVINGGC;
356 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
358 /* XXX: this should probably be passed in */
359 m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS;
362 m->op.flags |= BCH_WRITE_PAGES_STABLE|
363 BCH_WRITE_PAGES_OWNED|
364 BCH_WRITE_DATA_ENCODED|
365 BCH_WRITE_FROM_INTERNAL;
367 m->op.nr_replicas = data_opts.nr_replicas;
368 m->op.nr_replicas_required = data_opts.nr_replicas;
369 m->op.index_update_fn = bch2_migrate_index_update;
372 case DATA_ADD_REPLICAS: {
374 * DATA_ADD_REPLICAS is used for moving data to a different
375 * device in the background, and due to compression the new copy
376 * might take up more space than the old copy:
379 int nr = (int) io_opts.data_replicas -
380 bch2_bkey_nr_ptrs_allocated(k);
382 int nr = (int) io_opts.data_replicas;
385 m->op.nr_replicas = m->nr_ptrs_reserved = nr;
387 ret = bch2_disk_reservation_get(c, &m->op.res,
388 k.k->size, m->op.nr_replicas, 0);
395 unsigned compressed_sectors = 0;
397 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
398 if (p.ptr.dev == data_opts.rewrite_dev &&
400 crc_is_compressed(p.crc))
401 compressed_sectors += p.crc.compressed_size;
403 if (compressed_sectors) {
404 ret = bch2_disk_reservation_add(c, &m->op.res,
405 k.k->size * m->op.nr_replicas,
406 BCH_DISK_RESERVATION_NOFAIL);
413 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
414 m->op.flags |= BCH_WRITE_CACHED;
423 static void move_free(struct closure *cl)
425 struct moving_io *io = container_of(cl, struct moving_io, cl);
426 struct moving_context *ctxt = io->write.ctxt;
427 struct bvec_iter_all iter;
430 bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
432 bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter)
434 __free_page(bv->bv_page);
436 wake_up(&ctxt->wait);
441 static void move_write_done(struct closure *cl)
443 struct moving_io *io = container_of(cl, struct moving_io, cl);
445 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
446 closure_return_with_destructor(cl, move_free);
449 static void move_write(struct closure *cl)
451 struct moving_io *io = container_of(cl, struct moving_io, cl);
453 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
454 closure_return_with_destructor(cl, move_free);
458 bch2_migrate_read_done(&io->write, &io->rbio);
460 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
461 closure_call(&io->write.op.cl, bch2_write, NULL, cl);
462 continue_at(cl, move_write_done, NULL);
465 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
467 struct moving_io *io =
468 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
470 return io && io->read_completed ? io : NULL;
473 static void move_read_endio(struct bio *bio)
475 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
476 struct moving_context *ctxt = io->write.ctxt;
478 atomic_sub(io->read_sectors, &ctxt->read_sectors);
479 io->read_completed = true;
481 if (next_pending_write(ctxt))
482 wake_up(&ctxt->wait);
484 closure_put(&ctxt->cl);
487 static void do_pending_writes(struct moving_context *ctxt)
489 struct moving_io *io;
491 while ((io = next_pending_write(ctxt))) {
493 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
497 #define move_ctxt_wait_event(_ctxt, _cond) \
499 do_pending_writes(_ctxt); \
503 __wait_event((_ctxt)->wait, \
504 next_pending_write(_ctxt) || (_cond)); \
507 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
509 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
511 move_ctxt_wait_event(ctxt,
512 !atomic_read(&ctxt->write_sectors) ||
513 atomic_read(&ctxt->write_sectors) != sectors_pending);
516 static int bch2_move_extent(struct btree_trans *trans,
517 struct moving_context *ctxt,
518 struct write_point_specifier wp,
519 struct bch_io_opts io_opts,
520 enum btree_id btree_id,
522 enum data_cmd data_cmd,
523 struct data_opts data_opts)
525 struct bch_fs *c = trans->c;
526 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
527 struct moving_io *io;
528 const union bch_extent_entry *entry;
529 struct extent_ptr_decoded p;
530 unsigned sectors = k.k->size, pages;
533 move_ctxt_wait_event(ctxt,
534 atomic_read(&ctxt->write_sectors) <
535 SECTORS_IN_FLIGHT_PER_DEVICE);
537 move_ctxt_wait_event(ctxt,
538 atomic_read(&ctxt->read_sectors) <
539 SECTORS_IN_FLIGHT_PER_DEVICE);
541 /* write path might have to decompress data: */
542 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
543 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
545 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
546 io = kzalloc(sizeof(struct moving_io) +
547 sizeof(struct bio_vec) * pages, GFP_KERNEL);
551 io->write.ctxt = ctxt;
552 io->read_sectors = k.k->size;
553 io->write_sectors = k.k->size;
555 bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
556 bio_set_prio(&io->write.op.wbio.bio,
557 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
559 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
564 io->rbio.opts = io_opts;
565 bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
566 io->rbio.bio.bi_vcnt = pages;
567 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
568 io->rbio.bio.bi_iter.bi_size = sectors << 9;
570 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
571 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
572 io->rbio.bio.bi_end_io = move_read_endio;
574 ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
575 data_cmd, data_opts, btree_id, k);
579 atomic64_inc(&ctxt->stats->keys_moved);
580 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
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_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 __bch2_move_data(struct bch_fs *c,
639 struct moving_context *ctxt,
640 struct bch_ratelimit *rate,
641 struct write_point_specifier wp,
644 move_pred_fn pred, void *arg,
645 struct bch_move_stats *stats,
646 enum btree_id btree_id)
648 bool kthread = (current->flags & PF_KTHREAD) != 0;
649 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
651 struct btree_trans trans;
652 struct btree_iter iter;
654 struct data_opts data_opts;
655 enum data_cmd data_cmd;
656 u64 delay, cur_inum = U64_MAX;
659 bch2_bkey_buf_init(&sk);
660 bch2_trans_init(&trans, c, 0, 0);
662 stats->data_type = BCH_DATA_user;
663 stats->btree_id = btree_id;
666 bch2_trans_iter_init(&trans, &iter, btree_id, start,
668 BTREE_ITER_ALL_SNAPSHOTS);
671 bch2_ratelimit_reset(rate);
675 delay = rate ? bch2_ratelimit_delay(rate) : 0;
678 bch2_trans_unlock(&trans);
679 set_current_state(TASK_INTERRUPTIBLE);
682 if (kthread && (ret = kthread_should_stop())) {
683 __set_current_state(TASK_RUNNING);
688 schedule_timeout(delay);
690 if (unlikely(freezing(current))) {
691 bch2_trans_unlock(&trans);
692 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
697 bch2_trans_begin(&trans);
699 k = bch2_btree_iter_peek(&iter);
701 stats->pos = iter.pos;
708 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
711 if (!bkey_extent_is_direct_data(k.k))
714 if (btree_id == BTREE_ID_extents &&
715 cur_inum != k.k->p.inode) {
716 struct bch_inode_unpacked inode;
718 io_opts = bch2_opts_to_inode_opts(c->opts);
720 ret = lookup_inode(&trans,
721 SPOS(0, k.k->p.inode, k.k->p.snapshot),
727 bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
729 cur_inum = k.k->p.inode;
732 switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) {
737 case DATA_ADD_REPLICAS:
745 /* unlock before doing IO: */
746 bch2_bkey_buf_reassemble(&sk, c, k);
747 k = bkey_i_to_s_c(sk.k);
748 bch2_trans_unlock(&trans);
750 ret2 = bch2_move_extent(&trans, ctxt, wp, io_opts, btree_id, k,
751 data_cmd, data_opts);
753 if (ret2 == -EINTR) {
754 bch2_trans_begin(&trans);
758 if (ret2 == -ENOMEM) {
759 /* memory allocation failure, wait for some IO to finish */
760 bch2_move_ctxt_wait_for_io(ctxt);
764 /* XXX signal failure */
769 bch2_ratelimit_increment(rate, k.k->size);
771 atomic64_add(k.k->size * bch2_bkey_nr_ptrs_allocated(k),
772 &stats->sectors_seen);
774 bch2_btree_iter_advance(&iter);
778 bch2_trans_iter_exit(&trans, &iter);
779 bch2_trans_exit(&trans);
780 bch2_bkey_buf_exit(&sk, c);
785 inline void bch_move_stats_init(struct bch_move_stats *stats, char *name)
787 memset(stats, 0, sizeof(*stats));
789 scnprintf(stats->name, sizeof(stats->name),
793 static inline void progress_list_add(struct bch_fs *c,
794 struct bch_move_stats *stats)
796 mutex_lock(&c->data_progress_lock);
797 list_add(&stats->list, &c->data_progress_list);
798 mutex_unlock(&c->data_progress_lock);
801 static inline void progress_list_del(struct bch_fs *c,
802 struct bch_move_stats *stats)
804 mutex_lock(&c->data_progress_lock);
805 list_del(&stats->list);
806 mutex_unlock(&c->data_progress_lock);
809 int bch2_move_data(struct bch_fs *c,
810 enum btree_id start_btree_id, struct bpos start_pos,
811 enum btree_id end_btree_id, struct bpos end_pos,
812 struct bch_ratelimit *rate,
813 struct write_point_specifier wp,
814 move_pred_fn pred, void *arg,
815 struct bch_move_stats *stats)
817 struct moving_context ctxt = { .stats = stats };
821 progress_list_add(c, stats);
822 closure_init_stack(&ctxt.cl);
823 INIT_LIST_HEAD(&ctxt.reads);
824 init_waitqueue_head(&ctxt.wait);
826 stats->data_type = BCH_DATA_user;
828 for (id = start_btree_id;
829 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
831 stats->btree_id = id;
833 if (id != BTREE_ID_extents &&
834 id != BTREE_ID_reflink)
837 ret = __bch2_move_data(c, &ctxt, rate, wp,
838 id == start_btree_id ? start_pos : POS_MIN,
839 id == end_btree_id ? end_pos : POS_MAX,
840 pred, arg, stats, id);
846 move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
847 closure_sync(&ctxt.cl);
849 EBUG_ON(atomic_read(&ctxt.write_sectors));
852 atomic64_read(&stats->sectors_moved),
853 atomic64_read(&stats->keys_moved));
855 progress_list_del(c, stats);
859 typedef enum data_cmd (*move_btree_pred)(struct bch_fs *, void *,
860 struct btree *, struct bch_io_opts *,
863 static int bch2_move_btree(struct bch_fs *c,
864 enum btree_id start_btree_id, struct bpos start_pos,
865 enum btree_id end_btree_id, struct bpos end_pos,
866 move_btree_pred pred, void *arg,
867 struct bch_move_stats *stats)
869 bool kthread = (current->flags & PF_KTHREAD) != 0;
870 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
871 struct btree_trans trans;
872 struct btree_iter iter;
875 struct data_opts data_opts;
879 bch2_trans_init(&trans, c, 0, 0);
880 progress_list_add(c, stats);
882 stats->data_type = BCH_DATA_btree;
884 for (id = start_btree_id;
885 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
887 stats->btree_id = id;
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 = iter.pos;
905 switch ((cmd = pred(c, arg, b, &io_opts, &data_opts))) {
910 case DATA_ADD_REPLICAS:
917 ret = bch2_btree_node_rewrite(&trans, &iter, b, 0) ?: ret;
923 bch2_btree_iter_next_node(&iter);
928 bch2_trans_iter_exit(&trans, &iter);
930 if (kthread && kthread_should_stop())
934 bch2_trans_exit(&trans);
937 bch_err(c, "error %i in bch2_move_btree", ret);
939 /* flush relevant btree updates */
940 closure_wait_event(&c->btree_interior_update_wait,
941 !bch2_btree_interior_updates_nr_pending(c));
943 progress_list_del(c, stats);
948 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
950 struct bch_io_opts *io_opts,
951 struct data_opts *data_opts)
957 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
959 struct bch_io_opts *io_opts,
960 struct data_opts *data_opts)
962 unsigned nr_good = bch2_bkey_durability(c, k);
963 unsigned replicas = bkey_is_btree_ptr(k.k)
964 ? c->opts.metadata_replicas
965 : io_opts->data_replicas;
967 if (!nr_good || nr_good >= replicas)
970 data_opts->target = 0;
971 data_opts->nr_replicas = 1;
972 data_opts->btree_insert_flags = 0;
973 return DATA_ADD_REPLICAS;
976 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
978 struct bch_io_opts *io_opts,
979 struct data_opts *data_opts)
981 struct bch_ioctl_data *op = arg;
983 if (!bch2_bkey_has_device(k, op->migrate.dev))
986 data_opts->target = 0;
987 data_opts->nr_replicas = 1;
988 data_opts->btree_insert_flags = 0;
989 data_opts->rewrite_dev = op->migrate.dev;
993 static enum data_cmd rereplicate_btree_pred(struct bch_fs *c, void *arg,
995 struct bch_io_opts *io_opts,
996 struct data_opts *data_opts)
998 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1001 static enum data_cmd migrate_btree_pred(struct bch_fs *c, void *arg,
1003 struct bch_io_opts *io_opts,
1004 struct data_opts *data_opts)
1006 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1009 static bool bformat_needs_redo(struct bkey_format *f)
1013 for (i = 0; i < f->nr_fields; i++) {
1014 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
1015 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
1016 u64 field_offset = le64_to_cpu(f->field_offset[i]);
1018 if (f->bits_per_field[i] > unpacked_bits)
1021 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
1024 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1033 static enum data_cmd rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1035 struct bch_io_opts *io_opts,
1036 struct data_opts *data_opts)
1038 if (b->version_ondisk != c->sb.version ||
1039 btree_node_need_rewrite(b) ||
1040 bformat_needs_redo(&b->format)) {
1041 data_opts->target = 0;
1042 data_opts->nr_replicas = 1;
1043 data_opts->btree_insert_flags = 0;
1044 return DATA_REWRITE;
1050 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1054 ret = bch2_move_btree(c,
1056 BTREE_ID_NR, SPOS_MAX,
1057 rewrite_old_nodes_pred, c, stats);
1059 mutex_lock(&c->sb_lock);
1060 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1061 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1062 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1063 bch2_write_super(c);
1064 mutex_unlock(&c->sb_lock);
1070 int bch2_data_job(struct bch_fs *c,
1071 struct bch_move_stats *stats,
1072 struct bch_ioctl_data op)
1077 case BCH_DATA_OP_REREPLICATE:
1078 bch_move_stats_init(stats, "rereplicate");
1079 stats->data_type = BCH_DATA_journal;
1080 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1082 ret = bch2_move_btree(c,
1083 op.start_btree, op.start_pos,
1084 op.end_btree, op.end_pos,
1085 rereplicate_btree_pred, c, stats) ?: ret;
1086 ret = bch2_replicas_gc2(c) ?: ret;
1088 ret = bch2_move_data(c,
1089 op.start_btree, op.start_pos,
1090 op.end_btree, op.end_pos,
1091 NULL, writepoint_hashed((unsigned long) current),
1092 rereplicate_pred, c, stats) ?: ret;
1093 ret = bch2_replicas_gc2(c) ?: ret;
1095 case BCH_DATA_OP_MIGRATE:
1096 if (op.migrate.dev >= c->sb.nr_devices)
1099 bch_move_stats_init(stats, "migrate");
1100 stats->data_type = BCH_DATA_journal;
1101 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1103 ret = bch2_move_btree(c,
1104 op.start_btree, op.start_pos,
1105 op.end_btree, op.end_pos,
1106 migrate_btree_pred, &op, stats) ?: ret;
1107 ret = bch2_replicas_gc2(c) ?: ret;
1109 ret = bch2_move_data(c,
1110 op.start_btree, op.start_pos,
1111 op.end_btree, op.end_pos,
1112 NULL, writepoint_hashed((unsigned long) current),
1113 migrate_pred, &op, stats) ?: ret;
1114 ret = bch2_replicas_gc2(c) ?: ret;
1116 case BCH_DATA_OP_REWRITE_OLD_NODES:
1117 bch_move_stats_init(stats, "rewrite_old_nodes");
1118 ret = bch2_scan_old_btree_nodes(c, stats);