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"
13 #include "journal_reclaim.h"
16 #include "subvolume.h"
20 #include <linux/ioprio.h>
21 #include <linux/kthread.h>
23 #include <trace/events/bcachefs.h>
25 #define SECTORS_IN_FLIGHT_PER_DEVICE 2048
28 struct list_head list;
32 unsigned read_sectors;
33 unsigned write_sectors;
35 struct bch_read_bio rbio;
37 struct migrate_write write;
38 /* Must be last since it is variable size */
39 struct bio_vec bi_inline_vecs[0];
42 struct moving_context {
43 /* Closure for waiting on all reads and writes to complete */
46 struct bch_move_stats *stats;
48 struct list_head reads;
50 /* in flight sectors: */
51 atomic_t read_sectors;
52 atomic_t write_sectors;
54 wait_queue_head_t wait;
57 static int insert_snapshot_whiteouts(struct btree_trans *trans,
62 struct bch_fs *c = trans->c;
63 struct btree_iter iter, update_iter;
65 struct snapshots_seen s;
68 if (!btree_type_has_snapshots(id))
71 snapshots_seen_init(&s);
73 if (!bkey_cmp(old_pos, new_pos))
76 if (!snapshot_t(c, old_pos.snapshot)->children[0])
79 bch2_trans_iter_init(trans, &iter, id, old_pos,
80 BTREE_ITER_NOT_EXTENTS|
81 BTREE_ITER_ALL_SNAPSHOTS);
84 k = bch2_btree_iter_prev(&iter);
89 if (bkey_cmp(old_pos, k.k->p))
92 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) {
93 struct bkey_i *update;
96 for (i = 0; i < s.nr; i++)
97 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, s.d[i]))
100 update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
102 ret = PTR_ERR_OR_ZERO(update);
106 bkey_init(&update->k);
107 update->k.p = new_pos;
108 update->k.p.snapshot = k.k->p.snapshot;
110 bch2_trans_iter_init(trans, &update_iter, id, update->k.p,
111 BTREE_ITER_NOT_EXTENTS|
112 BTREE_ITER_ALL_SNAPSHOTS|
114 ret = bch2_btree_iter_traverse(&update_iter) ?:
115 bch2_trans_update(trans, &update_iter, update,
116 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
117 bch2_trans_iter_exit(trans, &update_iter);
121 ret = snapshots_seen_add(c, &s, k.k->p.snapshot);
126 bch2_trans_iter_exit(trans, &iter);
132 static int bch2_migrate_index_update(struct bch_write_op *op)
134 struct bch_fs *c = op->c;
135 struct btree_trans trans;
136 struct btree_iter iter;
137 struct migrate_write *m =
138 container_of(op, struct migrate_write, op);
139 struct keylist *keys = &op->insert_keys;
140 struct bkey_buf _new, _insert;
143 bch2_bkey_buf_init(&_new);
144 bch2_bkey_buf_init(&_insert);
145 bch2_bkey_buf_realloc(&_insert, c, U8_MAX);
147 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
149 bch2_trans_iter_init(&trans, &iter, m->btree_id,
150 bkey_start_pos(&bch2_keylist_front(keys)->k),
151 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
155 struct bkey_i *insert;
156 struct bkey_i_extent *new;
157 const union bch_extent_entry *entry;
158 struct extent_ptr_decoded p;
159 struct bpos next_pos;
160 bool did_work = false;
161 bool extending = false, should_check_enospc;
162 s64 i_sectors_delta = 0, disk_sectors_delta = 0;
164 bch2_trans_begin(&trans);
166 k = bch2_btree_iter_peek_slot(&iter);
171 new = bkey_i_to_extent(bch2_keylist_front(keys));
173 if (bversion_cmp(k.k->version, new->k.version) ||
174 !bch2_bkey_matches_ptr(c, k, m->ptr, m->offset))
177 bkey_reassemble(_insert.k, k);
180 bch2_bkey_buf_copy(&_new, c, bch2_keylist_front(keys));
181 new = bkey_i_to_extent(_new.k);
182 bch2_cut_front(iter.pos, &new->k_i);
184 bch2_cut_front(iter.pos, insert);
185 bch2_cut_back(new->k.p, insert);
186 bch2_cut_back(insert->k.p, &new->k_i);
188 if (m->data_cmd == DATA_REWRITE) {
189 struct bch_extent_ptr *new_ptr, *old_ptr = (void *)
190 bch2_bkey_has_device(bkey_i_to_s_c(insert),
191 m->data_opts.rewrite_dev);
196 extent_for_each_ptr(extent_i_to_s(new), new_ptr)
197 new_ptr->cached = true;
199 __bch2_bkey_drop_ptr(bkey_i_to_s(insert), old_ptr);
202 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
203 if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
205 * raced with another move op? extent already
206 * has a pointer to the device we just wrote
212 bch2_extent_ptr_decoded_append(insert, &p);
219 bch2_bkey_narrow_crcs(insert,
220 (struct bch_extent_crc_unpacked) { 0 });
221 bch2_extent_normalize(c, bkey_i_to_s(insert));
222 bch2_bkey_mark_replicas_cached(c, bkey_i_to_s(insert),
223 op->opts.background_target,
224 op->opts.data_replicas);
226 ret = bch2_sum_sector_overwrites(&trans, &iter, insert,
228 &should_check_enospc,
230 &disk_sectors_delta);
234 if (disk_sectors_delta > (s64) op->res.sectors) {
235 ret = bch2_disk_reservation_add(c, &op->res,
236 disk_sectors_delta - op->res.sectors,
238 ? BCH_DISK_RESERVATION_NOFAIL : 0);
243 next_pos = insert->k.p;
245 ret = insert_snapshot_whiteouts(&trans, m->btree_id,
246 k.k->p, insert->k.p) ?:
247 bch2_trans_update(&trans, &iter, insert,
248 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
249 bch2_trans_commit(&trans, &op->res,
252 m->data_opts.btree_insert_flags);
254 bch2_btree_iter_set_pos(&iter, next_pos);
255 atomic_long_inc(&c->extent_migrate_done);
263 while (bkey_cmp(iter.pos, bch2_keylist_front(keys)->k.p) >= 0) {
264 bch2_keylist_pop_front(keys);
265 if (bch2_keylist_empty(keys))
271 BUG_ON(k.k->p.offset <= iter.pos.offset);
272 atomic64_inc(&m->ctxt->stats->keys_raced);
273 atomic64_add(k.k->p.offset - iter.pos.offset,
274 &m->ctxt->stats->sectors_raced);
276 atomic_long_inc(&c->extent_migrate_raced);
277 trace_move_race(&new->k);
278 bch2_btree_iter_advance(&iter);
282 bch2_trans_iter_exit(&trans, &iter);
283 bch2_trans_exit(&trans);
284 bch2_bkey_buf_exit(&_insert, c);
285 bch2_bkey_buf_exit(&_new, c);
286 BUG_ON(ret == -EINTR);
290 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
292 /* write bio must own pages: */
293 BUG_ON(!m->op.wbio.bio.bi_vcnt);
295 m->ptr = rbio->pick.ptr;
296 m->offset = rbio->data_pos.offset - rbio->pick.crc.offset;
297 m->op.devs_have = rbio->devs_have;
298 m->op.pos = rbio->data_pos;
299 m->op.version = rbio->version;
300 m->op.crc = rbio->pick.crc;
301 m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
303 if (m->data_cmd == DATA_REWRITE)
304 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
307 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
308 struct write_point_specifier wp,
309 struct bch_io_opts io_opts,
310 enum data_cmd data_cmd,
311 struct data_opts data_opts,
312 enum btree_id btree_id,
315 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
316 const union bch_extent_entry *entry;
317 struct bch_extent_crc_unpacked crc;
318 struct extent_ptr_decoded p;
321 m->btree_id = btree_id;
322 m->data_cmd = data_cmd;
323 m->data_opts = data_opts;
324 m->nr_ptrs_reserved = 0;
326 bch2_write_op_init(&m->op, c, io_opts);
328 if (!bch2_bkey_is_incompressible(k))
329 m->op.compression_type =
330 bch2_compression_opt_to_type[io_opts.background_compression ?:
331 io_opts.compression];
333 m->op.incompressible = true;
335 m->op.target = data_opts.target,
336 m->op.write_point = wp;
339 * op->csum_type is normally initialized from the fs/file's current
340 * options - but if an extent is encrypted, we require that it stays
343 bkey_for_each_crc(k.k, ptrs, crc, entry)
344 if (bch2_csum_type_is_encryption(crc.csum_type)) {
345 m->op.nonce = crc.nonce + crc.offset;
346 m->op.csum_type = crc.csum_type;
350 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE) {
351 m->op.alloc_reserve = RESERVE_MOVINGGC;
352 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
354 /* XXX: this should probably be passed in */
355 m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS;
358 m->op.flags |= BCH_WRITE_PAGES_STABLE|
359 BCH_WRITE_PAGES_OWNED|
360 BCH_WRITE_DATA_ENCODED|
361 BCH_WRITE_FROM_INTERNAL;
363 m->op.nr_replicas = data_opts.nr_replicas;
364 m->op.nr_replicas_required = data_opts.nr_replicas;
365 m->op.index_update_fn = bch2_migrate_index_update;
368 case DATA_ADD_REPLICAS: {
370 * DATA_ADD_REPLICAS is used for moving data to a different
371 * device in the background, and due to compression the new copy
372 * might take up more space than the old copy:
375 int nr = (int) io_opts.data_replicas -
376 bch2_bkey_nr_ptrs_allocated(k);
378 int nr = (int) io_opts.data_replicas;
381 m->op.nr_replicas = m->nr_ptrs_reserved = nr;
383 ret = bch2_disk_reservation_get(c, &m->op.res,
384 k.k->size, m->op.nr_replicas, 0);
391 unsigned compressed_sectors = 0;
393 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
394 if (p.ptr.dev == data_opts.rewrite_dev &&
396 crc_is_compressed(p.crc))
397 compressed_sectors += p.crc.compressed_size;
399 if (compressed_sectors) {
400 ret = bch2_disk_reservation_add(c, &m->op.res,
401 k.k->size * m->op.nr_replicas,
402 BCH_DISK_RESERVATION_NOFAIL);
409 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
410 m->op.flags |= BCH_WRITE_CACHED;
419 static void move_free(struct closure *cl)
421 struct moving_io *io = container_of(cl, struct moving_io, cl);
422 struct moving_context *ctxt = io->write.ctxt;
423 struct bvec_iter_all iter;
426 bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
428 bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter)
430 __free_page(bv->bv_page);
432 wake_up(&ctxt->wait);
437 static void move_write_done(struct closure *cl)
439 struct moving_io *io = container_of(cl, struct moving_io, cl);
441 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
442 closure_return_with_destructor(cl, move_free);
445 static void move_write(struct closure *cl)
447 struct moving_io *io = container_of(cl, struct moving_io, cl);
449 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
450 closure_return_with_destructor(cl, move_free);
454 bch2_migrate_read_done(&io->write, &io->rbio);
456 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
457 closure_call(&io->write.op.cl, bch2_write, NULL, cl);
458 continue_at(cl, move_write_done, NULL);
461 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
463 struct moving_io *io =
464 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
466 return io && io->read_completed ? io : NULL;
469 static void move_read_endio(struct bio *bio)
471 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
472 struct moving_context *ctxt = io->write.ctxt;
474 atomic_sub(io->read_sectors, &ctxt->read_sectors);
475 io->read_completed = true;
477 if (next_pending_write(ctxt))
478 wake_up(&ctxt->wait);
480 closure_put(&ctxt->cl);
483 static void do_pending_writes(struct moving_context *ctxt)
485 struct moving_io *io;
487 while ((io = next_pending_write(ctxt))) {
489 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
493 #define move_ctxt_wait_event(_ctxt, _cond) \
495 do_pending_writes(_ctxt); \
499 __wait_event((_ctxt)->wait, \
500 next_pending_write(_ctxt) || (_cond)); \
503 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
505 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
507 move_ctxt_wait_event(ctxt,
508 !atomic_read(&ctxt->write_sectors) ||
509 atomic_read(&ctxt->write_sectors) != sectors_pending);
512 static int bch2_move_extent(struct btree_trans *trans,
513 struct moving_context *ctxt,
514 struct write_point_specifier wp,
515 struct bch_io_opts io_opts,
516 enum btree_id btree_id,
518 enum data_cmd data_cmd,
519 struct data_opts data_opts)
521 struct bch_fs *c = trans->c;
522 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
523 struct moving_io *io;
524 const union bch_extent_entry *entry;
525 struct extent_ptr_decoded p;
526 unsigned sectors = k.k->size, pages;
529 move_ctxt_wait_event(ctxt,
530 atomic_read(&ctxt->write_sectors) <
531 SECTORS_IN_FLIGHT_PER_DEVICE);
533 move_ctxt_wait_event(ctxt,
534 atomic_read(&ctxt->read_sectors) <
535 SECTORS_IN_FLIGHT_PER_DEVICE);
537 /* write path might have to decompress data: */
538 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
539 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
541 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
542 io = kzalloc(sizeof(struct moving_io) +
543 sizeof(struct bio_vec) * pages, GFP_KERNEL);
547 io->write.ctxt = ctxt;
548 io->read_sectors = k.k->size;
549 io->write_sectors = k.k->size;
551 bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
552 bio_set_prio(&io->write.op.wbio.bio,
553 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
555 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
560 io->rbio.opts = io_opts;
561 bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
562 io->rbio.bio.bi_vcnt = pages;
563 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
564 io->rbio.bio.bi_iter.bi_size = sectors << 9;
566 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
567 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
568 io->rbio.bio.bi_end_io = move_read_endio;
570 ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
571 data_cmd, data_opts, btree_id, k);
575 atomic64_inc(&ctxt->stats->keys_moved);
576 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
578 trace_move_extent(k.k);
580 atomic_add(io->read_sectors, &ctxt->read_sectors);
581 list_add_tail(&io->list, &ctxt->reads);
584 * dropped by move_read_endio() - guards against use after free of
585 * ctxt when doing wakeup
587 closure_get(&ctxt->cl);
588 bch2_read_extent(trans, &io->rbio,
592 BCH_READ_LAST_FRAGMENT);
595 bio_free_pages(&io->write.op.wbio.bio);
599 trace_move_alloc_fail(k.k);
603 static int lookup_inode(struct btree_trans *trans, struct bpos pos,
604 struct bch_inode_unpacked *inode)
606 struct btree_iter iter;
610 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes, pos,
611 BTREE_ITER_ALL_SNAPSHOTS);
612 k = bch2_btree_iter_peek(&iter);
617 if (!k.k || bkey_cmp(k.k->p, pos)) {
622 ret = k.k->type == KEY_TYPE_inode ? 0 : -EIO;
626 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
630 bch2_trans_iter_exit(trans, &iter);
634 static int __bch2_move_data(struct bch_fs *c,
635 struct moving_context *ctxt,
636 struct bch_ratelimit *rate,
637 struct write_point_specifier wp,
640 move_pred_fn pred, void *arg,
641 struct bch_move_stats *stats,
642 enum btree_id btree_id)
644 bool kthread = (current->flags & PF_KTHREAD) != 0;
645 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
647 struct btree_trans trans;
648 struct btree_iter iter;
650 struct data_opts data_opts;
651 enum data_cmd data_cmd;
652 u64 delay, cur_inum = U64_MAX;
655 bch2_bkey_buf_init(&sk);
656 bch2_trans_init(&trans, c, 0, 0);
658 stats->data_type = BCH_DATA_user;
659 stats->btree_id = btree_id;
662 bch2_trans_iter_init(&trans, &iter, btree_id, start,
664 BTREE_ITER_ALL_SNAPSHOTS);
667 bch2_ratelimit_reset(rate);
671 delay = rate ? bch2_ratelimit_delay(rate) : 0;
674 bch2_trans_unlock(&trans);
675 set_current_state(TASK_INTERRUPTIBLE);
678 if (kthread && (ret = kthread_should_stop())) {
679 __set_current_state(TASK_RUNNING);
684 schedule_timeout(delay);
686 if (unlikely(freezing(current))) {
687 bch2_trans_unlock(&trans);
688 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
693 bch2_trans_begin(&trans);
695 k = bch2_btree_iter_peek(&iter);
697 stats->pos = iter.pos;
704 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
707 if (!bkey_extent_is_direct_data(k.k))
710 if (btree_id == BTREE_ID_extents &&
711 cur_inum != k.k->p.inode) {
712 struct bch_inode_unpacked inode;
714 io_opts = bch2_opts_to_inode_opts(c->opts);
716 ret = lookup_inode(&trans,
717 SPOS(0, k.k->p.inode, k.k->p.snapshot),
723 bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
725 cur_inum = k.k->p.inode;
728 switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) {
733 case DATA_ADD_REPLICAS:
741 /* unlock before doing IO: */
742 bch2_bkey_buf_reassemble(&sk, c, k);
743 k = bkey_i_to_s_c(sk.k);
744 bch2_trans_unlock(&trans);
746 ret2 = bch2_move_extent(&trans, ctxt, wp, io_opts, btree_id, k,
747 data_cmd, data_opts);
749 if (ret2 == -EINTR) {
750 bch2_trans_begin(&trans);
754 if (ret2 == -ENOMEM) {
755 /* memory allocation failure, wait for some IO to finish */
756 bch2_move_ctxt_wait_for_io(ctxt);
760 /* XXX signal failure */
765 bch2_ratelimit_increment(rate, k.k->size);
767 atomic64_add(k.k->size * bch2_bkey_nr_ptrs_allocated(k),
768 &stats->sectors_seen);
770 bch2_btree_iter_advance(&iter);
771 bch2_trans_cond_resched(&trans);
775 bch2_trans_iter_exit(&trans, &iter);
776 bch2_trans_exit(&trans);
777 bch2_bkey_buf_exit(&sk, c);
782 inline void bch_move_stats_init(struct bch_move_stats *stats, char *name)
784 memset(stats, 0, sizeof(*stats));
786 scnprintf(stats->name, sizeof(stats->name),
790 static inline void progress_list_add(struct bch_fs *c,
791 struct bch_move_stats *stats)
793 mutex_lock(&c->data_progress_lock);
794 list_add(&stats->list, &c->data_progress_list);
795 mutex_unlock(&c->data_progress_lock);
798 static inline void progress_list_del(struct bch_fs *c,
799 struct bch_move_stats *stats)
801 mutex_lock(&c->data_progress_lock);
802 list_del(&stats->list);
803 mutex_unlock(&c->data_progress_lock);
806 int bch2_move_data(struct bch_fs *c,
807 enum btree_id start_btree_id, struct bpos start_pos,
808 enum btree_id end_btree_id, struct bpos end_pos,
809 struct bch_ratelimit *rate,
810 struct write_point_specifier wp,
811 move_pred_fn pred, void *arg,
812 struct bch_move_stats *stats)
814 struct moving_context ctxt = { .stats = stats };
818 progress_list_add(c, stats);
819 closure_init_stack(&ctxt.cl);
820 INIT_LIST_HEAD(&ctxt.reads);
821 init_waitqueue_head(&ctxt.wait);
823 stats->data_type = BCH_DATA_user;
825 for (id = start_btree_id;
826 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
828 stats->btree_id = id;
830 if (id != BTREE_ID_extents &&
831 id != BTREE_ID_reflink)
834 ret = __bch2_move_data(c, &ctxt, rate, wp,
835 id == start_btree_id ? start_pos : POS_MIN,
836 id == end_btree_id ? end_pos : POS_MAX,
837 pred, arg, stats, id);
843 move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
844 closure_sync(&ctxt.cl);
846 EBUG_ON(atomic_read(&ctxt.write_sectors));
849 atomic64_read(&stats->sectors_moved),
850 atomic64_read(&stats->keys_moved));
852 progress_list_del(c, stats);
856 typedef enum data_cmd (*move_btree_pred)(struct bch_fs *, void *,
857 struct btree *, struct bch_io_opts *,
860 static int bch2_move_btree(struct bch_fs *c,
861 enum btree_id start_btree_id, struct bpos start_pos,
862 enum btree_id end_btree_id, struct bpos end_pos,
863 move_btree_pred pred, void *arg,
864 struct bch_move_stats *stats)
866 bool kthread = (current->flags & PF_KTHREAD) != 0;
867 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
868 struct btree_trans trans;
869 struct btree_iter iter;
872 struct data_opts data_opts;
876 bch2_trans_init(&trans, c, 0, 0);
877 progress_list_add(c, stats);
879 stats->data_type = BCH_DATA_btree;
881 for (id = start_btree_id;
882 id <= min_t(unsigned, end_btree_id, BTREE_ID_NR - 1);
884 stats->btree_id = id;
886 bch2_trans_node_iter_init(&trans, &iter, id, POS_MIN, 0, 0,
887 BTREE_ITER_PREFETCH);
890 while (bch2_trans_begin(&trans),
891 (b = bch2_btree_iter_peek_node(&iter)) &&
892 !(ret = PTR_ERR_OR_ZERO(b))) {
893 if (kthread && kthread_should_stop())
896 if ((cmp_int(id, end_btree_id) ?:
897 bpos_cmp(b->key.k.p, end_pos)) > 0)
900 stats->pos = iter.pos;
902 switch ((cmd = pred(c, arg, b, &io_opts, &data_opts))) {
907 case DATA_ADD_REPLICAS:
914 ret = bch2_btree_node_rewrite(&trans, &iter,
915 b->data->keys.seq, 0) ?: ret;
917 bch2_trans_cond_resched(&trans);
918 bch2_btree_iter_next_node(&iter);
923 bch2_trans_iter_exit(&trans, &iter);
925 if (kthread && kthread_should_stop())
929 bch2_trans_exit(&trans);
932 bch_err(c, "error %i in bch2_move_btree", ret);
934 progress_list_del(c, stats);
939 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
941 struct bch_io_opts *io_opts,
942 struct data_opts *data_opts)
948 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
950 struct bch_io_opts *io_opts,
951 struct data_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->nr_replicas = 1;
963 data_opts->btree_insert_flags = 0;
964 return DATA_ADD_REPLICAS;
967 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
969 struct bch_io_opts *io_opts,
970 struct data_opts *data_opts)
972 struct bch_ioctl_data *op = arg;
974 if (!bch2_bkey_has_device(k, op->migrate.dev))
977 data_opts->target = 0;
978 data_opts->nr_replicas = 1;
979 data_opts->btree_insert_flags = 0;
980 data_opts->rewrite_dev = op->migrate.dev;
984 static enum data_cmd rereplicate_btree_pred(struct bch_fs *c, void *arg,
986 struct bch_io_opts *io_opts,
987 struct data_opts *data_opts)
989 return rereplicate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
992 static enum data_cmd migrate_btree_pred(struct bch_fs *c, void *arg,
994 struct bch_io_opts *io_opts,
995 struct data_opts *data_opts)
997 return migrate_pred(c, arg, bkey_i_to_s_c(&b->key), io_opts, data_opts);
1000 static bool bformat_needs_redo(struct bkey_format *f)
1004 for (i = 0; i < f->nr_fields; i++) {
1005 unsigned unpacked_bits = bch2_bkey_format_current.bits_per_field[i];
1006 u64 unpacked_mask = ~((~0ULL << 1) << (unpacked_bits - 1));
1007 u64 field_offset = le64_to_cpu(f->field_offset[i]);
1009 if (f->bits_per_field[i] > unpacked_bits)
1012 if ((f->bits_per_field[i] == unpacked_bits) && field_offset)
1015 if (((field_offset + ((1ULL << f->bits_per_field[i]) - 1)) &
1024 static enum data_cmd rewrite_old_nodes_pred(struct bch_fs *c, void *arg,
1026 struct bch_io_opts *io_opts,
1027 struct data_opts *data_opts)
1029 if (b->version_ondisk != c->sb.version ||
1030 btree_node_need_rewrite(b) ||
1031 bformat_needs_redo(&b->format)) {
1032 data_opts->target = 0;
1033 data_opts->nr_replicas = 1;
1034 data_opts->btree_insert_flags = 0;
1035 return DATA_REWRITE;
1041 int bch2_scan_old_btree_nodes(struct bch_fs *c, struct bch_move_stats *stats)
1045 ret = bch2_move_btree(c,
1047 BTREE_ID_NR, SPOS_MAX,
1048 rewrite_old_nodes_pred, c, stats);
1050 mutex_lock(&c->sb_lock);
1051 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done);
1052 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done);
1053 c->disk_sb.sb->version_min = c->disk_sb.sb->version;
1054 bch2_write_super(c);
1055 mutex_unlock(&c->sb_lock);
1061 int bch2_data_job(struct bch_fs *c,
1062 struct bch_move_stats *stats,
1063 struct bch_ioctl_data op)
1068 case BCH_DATA_OP_REREPLICATE:
1069 bch_move_stats_init(stats, "rereplicate");
1070 stats->data_type = BCH_DATA_journal;
1071 ret = bch2_journal_flush_device_pins(&c->journal, -1);
1073 ret = bch2_move_btree(c,
1074 op.start_btree, op.start_pos,
1075 op.end_btree, op.end_pos,
1076 rereplicate_btree_pred, c, stats) ?: ret;
1078 closure_wait_event(&c->btree_interior_update_wait,
1079 !bch2_btree_interior_updates_nr_pending(c));
1081 ret = bch2_replicas_gc2(c) ?: ret;
1083 ret = bch2_move_data(c,
1084 op.start_btree, op.start_pos,
1085 op.end_btree, op.end_pos,
1086 NULL, writepoint_hashed((unsigned long) current),
1087 rereplicate_pred, c, stats) ?: ret;
1088 ret = bch2_replicas_gc2(c) ?: ret;
1090 case BCH_DATA_OP_MIGRATE:
1091 if (op.migrate.dev >= c->sb.nr_devices)
1094 bch_move_stats_init(stats, "migrate");
1095 stats->data_type = BCH_DATA_journal;
1096 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
1098 ret = bch2_move_btree(c,
1099 op.start_btree, op.start_pos,
1100 op.end_btree, op.end_pos,
1101 migrate_btree_pred, &op, stats) ?: ret;
1102 ret = bch2_replicas_gc2(c) ?: ret;
1104 ret = bch2_move_data(c,
1105 op.start_btree, op.start_pos,
1106 op.end_btree, op.end_pos,
1107 NULL, writepoint_hashed((unsigned long) current),
1108 migrate_pred, &op, stats) ?: ret;
1109 ret = bch2_replicas_gc2(c) ?: ret;
1111 case BCH_DATA_OP_REWRITE_OLD_NODES:
1112 bch_move_stats_init(stats, "rewrite_old_nodes");
1113 ret = bch2_scan_old_btree_nodes(c, stats);