1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
5 #include "bkey_on_stack.h"
7 #include "btree_update.h"
8 #include "btree_update_interior.h"
10 #include "disk_groups.h"
13 #include "journal_reclaim.h"
19 #include <linux/ioprio.h>
20 #include <linux/kthread.h>
22 #include <trace/events/bcachefs.h>
24 #define SECTORS_IN_FLIGHT_PER_DEVICE 2048
27 struct list_head list;
31 unsigned read_sectors;
32 unsigned write_sectors;
34 struct bch_read_bio rbio;
36 struct migrate_write write;
37 /* Must be last since it is variable size */
38 struct bio_vec bi_inline_vecs[0];
41 struct moving_context {
42 /* Closure for waiting on all reads and writes to complete */
45 struct bch_move_stats *stats;
47 struct list_head reads;
49 /* in flight sectors: */
50 atomic_t read_sectors;
51 atomic_t write_sectors;
53 wait_queue_head_t wait;
56 static int bch2_migrate_index_update(struct bch_write_op *op)
58 struct bch_fs *c = op->c;
59 struct btree_trans trans;
60 struct btree_iter *iter;
61 struct migrate_write *m =
62 container_of(op, struct migrate_write, op);
63 struct keylist *keys = &op->insert_keys;
66 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
68 iter = bch2_trans_get_iter(&trans, m->btree_id,
69 bkey_start_pos(&bch2_keylist_front(keys)->k),
70 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
73 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
74 struct bkey_i *insert;
75 struct bkey_i_extent *new =
76 bkey_i_to_extent(bch2_keylist_front(keys));
77 BKEY_PADDED(k) _new, _insert;
78 const union bch_extent_entry *entry;
79 struct extent_ptr_decoded p;
80 bool did_work = false;
87 if (bversion_cmp(k.k->version, new->k.version) ||
88 !bch2_bkey_matches_ptr(c, k, m->ptr, m->offset))
91 if (m->data_cmd == DATA_REWRITE &&
92 !bch2_bkey_has_device(k, m->data_opts.rewrite_dev))
95 bkey_reassemble(&_insert.k, k);
98 bkey_copy(&_new.k, bch2_keylist_front(keys));
99 new = bkey_i_to_extent(&_new.k);
100 bch2_cut_front(iter->pos, &new->k_i);
102 bch2_cut_front(iter->pos, insert);
103 bch2_cut_back(new->k.p, insert);
104 bch2_cut_back(insert->k.p, &new->k_i);
106 if (m->data_cmd == DATA_REWRITE)
107 bch2_bkey_drop_device(bkey_i_to_s(insert),
108 m->data_opts.rewrite_dev);
110 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
111 if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
113 * raced with another move op? extent already
114 * has a pointer to the device we just wrote
120 bch2_extent_ptr_decoded_append(insert, &p);
127 bch2_bkey_narrow_crcs(insert,
128 (struct bch_extent_crc_unpacked) { 0 });
129 bch2_extent_normalize(c, bkey_i_to_s(insert));
130 bch2_bkey_mark_replicas_cached(c, bkey_i_to_s(insert),
131 op->opts.background_target,
132 op->opts.data_replicas);
135 * If we're not fully overwriting @k, and it's compressed, we
136 * need a reservation for all the pointers in @insert
138 nr = bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(insert)) -
141 if (insert->k.size < k.k->size &&
142 bch2_bkey_sectors_compressed(k) &&
144 ret = bch2_disk_reservation_add(c, &op->res,
145 keylist_sectors(keys) * nr, 0);
149 m->nr_ptrs_reserved += nr;
153 bch2_trans_update(&trans, iter, insert);
155 ret = bch2_trans_commit(&trans, &op->res,
158 BTREE_INSERT_USE_RESERVE|
159 m->data_opts.btree_insert_flags);
161 atomic_long_inc(&c->extent_migrate_done);
167 while (bkey_cmp(iter->pos, bch2_keylist_front(keys)->k.p) >= 0) {
168 bch2_keylist_pop_front(keys);
169 if (bch2_keylist_empty(keys))
175 atomic64_add(k.k->p.offset - iter->pos.offset,
176 &m->ctxt->stats->sectors_raced);
177 atomic_long_inc(&c->extent_migrate_raced);
178 trace_move_race(&new->k);
179 bch2_btree_iter_next_slot(iter);
183 bch2_trans_exit(&trans);
184 BUG_ON(ret == -EINTR);
188 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
190 /* write bio must own pages: */
191 BUG_ON(!m->op.wbio.bio.bi_vcnt);
193 m->ptr = rbio->pick.ptr;
194 m->offset = rbio->pos.offset - rbio->pick.crc.offset;
195 m->op.devs_have = rbio->devs_have;
196 m->op.pos = rbio->pos;
197 m->op.version = rbio->version;
198 m->op.crc = rbio->pick.crc;
199 m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
201 if (bch2_csum_type_is_encryption(m->op.crc.csum_type)) {
202 m->op.nonce = m->op.crc.nonce + m->op.crc.offset;
203 m->op.csum_type = m->op.crc.csum_type;
206 if (m->data_cmd == DATA_REWRITE)
207 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
210 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
211 struct write_point_specifier wp,
212 struct bch_io_opts io_opts,
213 enum data_cmd data_cmd,
214 struct data_opts data_opts,
215 enum btree_id btree_id,
220 m->btree_id = btree_id;
221 m->data_cmd = data_cmd;
222 m->data_opts = data_opts;
223 m->nr_ptrs_reserved = 0;
225 bch2_write_op_init(&m->op, c, io_opts);
226 m->op.compression_type =
227 bch2_compression_opt_to_type[io_opts.background_compression ?:
228 io_opts.compression];
229 m->op.target = data_opts.target,
230 m->op.write_point = wp;
232 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
233 m->op.alloc_reserve = RESERVE_MOVINGGC;
235 m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS|
236 BCH_WRITE_PAGES_STABLE|
237 BCH_WRITE_PAGES_OWNED|
238 BCH_WRITE_DATA_ENCODED;
240 m->op.nr_replicas = 1;
241 m->op.nr_replicas_required = 1;
242 m->op.index_update_fn = bch2_migrate_index_update;
245 case DATA_ADD_REPLICAS: {
247 * DATA_ADD_REPLICAS is used for moving data to a different
248 * device in the background, and due to compression the new copy
249 * might take up more space than the old copy:
252 int nr = (int) io_opts.data_replicas -
253 bch2_bkey_nr_ptrs_allocated(k);
255 int nr = (int) io_opts.data_replicas;
258 m->op.nr_replicas = m->nr_ptrs_reserved = nr;
260 ret = bch2_disk_reservation_get(c, &m->op.res,
261 k.k->size, m->op.nr_replicas, 0);
268 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
269 const union bch_extent_entry *entry;
270 struct extent_ptr_decoded p;
271 unsigned compressed_sectors = 0;
273 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
275 p.crc.compression_type != BCH_COMPRESSION_TYPE_none &&
276 bch2_dev_in_target(c, p.ptr.dev, data_opts.target))
277 compressed_sectors += p.crc.compressed_size;
279 if (compressed_sectors) {
280 ret = bch2_disk_reservation_add(c, &m->op.res,
282 BCH_DISK_RESERVATION_NOFAIL);
289 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
290 m->op.flags |= BCH_WRITE_CACHED;
299 static void move_free(struct closure *cl)
301 struct moving_io *io = container_of(cl, struct moving_io, cl);
302 struct moving_context *ctxt = io->write.ctxt;
303 struct bvec_iter_all iter;
306 bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
308 bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter)
310 __free_page(bv->bv_page);
312 wake_up(&ctxt->wait);
317 static void move_write_done(struct closure *cl)
319 struct moving_io *io = container_of(cl, struct moving_io, cl);
321 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
322 closure_return_with_destructor(cl, move_free);
325 static void move_write(struct closure *cl)
327 struct moving_io *io = container_of(cl, struct moving_io, cl);
329 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
330 closure_return_with_destructor(cl, move_free);
334 bch2_migrate_read_done(&io->write, &io->rbio);
336 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
337 closure_call(&io->write.op.cl, bch2_write, NULL, cl);
338 continue_at(cl, move_write_done, NULL);
341 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
343 struct moving_io *io =
344 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
346 return io && io->read_completed ? io : NULL;
349 static void move_read_endio(struct bio *bio)
351 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
352 struct moving_context *ctxt = io->write.ctxt;
354 atomic_sub(io->read_sectors, &ctxt->read_sectors);
355 io->read_completed = true;
357 if (next_pending_write(ctxt))
358 wake_up(&ctxt->wait);
360 closure_put(&ctxt->cl);
363 static void do_pending_writes(struct moving_context *ctxt)
365 struct moving_io *io;
367 while ((io = next_pending_write(ctxt))) {
369 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
373 #define move_ctxt_wait_event(_ctxt, _cond) \
375 do_pending_writes(_ctxt); \
379 __wait_event((_ctxt)->wait, \
380 next_pending_write(_ctxt) || (_cond)); \
383 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
385 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
387 move_ctxt_wait_event(ctxt,
388 !atomic_read(&ctxt->write_sectors) ||
389 atomic_read(&ctxt->write_sectors) != sectors_pending);
392 static int bch2_move_extent(struct bch_fs *c,
393 struct moving_context *ctxt,
394 struct write_point_specifier wp,
395 struct bch_io_opts io_opts,
396 enum btree_id btree_id,
398 enum data_cmd data_cmd,
399 struct data_opts data_opts)
401 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
402 struct moving_io *io;
403 const union bch_extent_entry *entry;
404 struct extent_ptr_decoded p;
405 unsigned sectors = k.k->size, pages;
408 move_ctxt_wait_event(ctxt,
409 atomic_read(&ctxt->write_sectors) <
410 SECTORS_IN_FLIGHT_PER_DEVICE);
412 move_ctxt_wait_event(ctxt,
413 atomic_read(&ctxt->read_sectors) <
414 SECTORS_IN_FLIGHT_PER_DEVICE);
416 /* write path might have to decompress data: */
417 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
418 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
420 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
421 io = kzalloc(sizeof(struct moving_io) +
422 sizeof(struct bio_vec) * pages, GFP_KERNEL);
426 io->write.ctxt = ctxt;
427 io->read_sectors = k.k->size;
428 io->write_sectors = k.k->size;
430 bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
431 bio_set_prio(&io->write.op.wbio.bio,
432 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
434 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
439 io->rbio.opts = io_opts;
440 bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
441 io->rbio.bio.bi_vcnt = pages;
442 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
443 io->rbio.bio.bi_iter.bi_size = sectors << 9;
445 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
446 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
447 io->rbio.bio.bi_end_io = move_read_endio;
449 ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
450 data_cmd, data_opts, btree_id, k);
454 atomic64_inc(&ctxt->stats->keys_moved);
455 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
457 trace_move_extent(k.k);
459 atomic_add(io->read_sectors, &ctxt->read_sectors);
460 list_add_tail(&io->list, &ctxt->reads);
463 * dropped by move_read_endio() - guards against use after free of
464 * ctxt when doing wakeup
466 closure_get(&ctxt->cl);
467 bch2_read_extent(c, &io->rbio, k, 0,
469 BCH_READ_LAST_FRAGMENT);
472 bio_free_pages(&io->write.op.wbio.bio);
476 trace_move_alloc_fail(k.k);
480 static int __bch2_move_data(struct bch_fs *c,
481 struct moving_context *ctxt,
482 struct bch_ratelimit *rate,
483 struct write_point_specifier wp,
486 move_pred_fn pred, void *arg,
487 struct bch_move_stats *stats,
488 enum btree_id btree_id)
490 bool kthread = (current->flags & PF_KTHREAD) != 0;
491 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
492 struct bkey_on_stack sk;
493 struct btree_trans trans;
494 struct btree_iter *iter;
496 struct data_opts data_opts;
497 enum data_cmd data_cmd;
498 u64 delay, cur_inum = U64_MAX;
501 bkey_on_stack_init(&sk);
502 bch2_trans_init(&trans, c, 0, 0);
504 stats->data_type = BCH_DATA_USER;
505 stats->btree_id = btree_id;
506 stats->pos = POS_MIN;
508 iter = bch2_trans_get_iter(&trans, btree_id, start,
509 BTREE_ITER_PREFETCH);
512 bch2_ratelimit_reset(rate);
516 delay = rate ? bch2_ratelimit_delay(rate) : 0;
519 bch2_trans_unlock(&trans);
520 set_current_state(TASK_INTERRUPTIBLE);
523 if (kthread && (ret = kthread_should_stop())) {
524 __set_current_state(TASK_RUNNING);
529 schedule_timeout(delay);
531 if (unlikely(freezing(current))) {
532 bch2_trans_unlock(&trans);
533 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
538 k = bch2_btree_iter_peek(iter);
540 stats->pos = iter->pos;
547 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
550 if (!bkey_extent_is_direct_data(k.k))
553 if (btree_id == BTREE_ID_EXTENTS &&
554 cur_inum != k.k->p.inode) {
555 struct bch_inode_unpacked inode;
557 /* don't hold btree locks while looking up inode: */
558 bch2_trans_unlock(&trans);
560 io_opts = bch2_opts_to_inode_opts(c->opts);
561 if (!bch2_inode_find_by_inum(c, k.k->p.inode, &inode))
562 bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
563 cur_inum = k.k->p.inode;
567 switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) {
572 case DATA_ADD_REPLICAS:
580 /* unlock before doing IO: */
581 bkey_on_stack_reassemble(&sk, c, k);
582 k = bkey_i_to_s_c(sk.k);
583 bch2_trans_unlock(&trans);
585 ret2 = bch2_move_extent(c, ctxt, wp, io_opts, btree_id, k,
586 data_cmd, data_opts);
588 if (ret2 == -ENOMEM) {
589 /* memory allocation failure, wait for some IO to finish */
590 bch2_move_ctxt_wait_for_io(ctxt);
594 /* XXX signal failure */
599 bch2_ratelimit_increment(rate, k.k->size);
601 atomic64_add(k.k->size * bch2_bkey_nr_ptrs_allocated(k),
602 &stats->sectors_seen);
604 bch2_btree_iter_next(iter);
605 bch2_trans_cond_resched(&trans);
608 ret = bch2_trans_exit(&trans) ?: ret;
609 bkey_on_stack_exit(&sk, c);
614 int bch2_move_data(struct bch_fs *c,
615 struct bch_ratelimit *rate,
616 struct write_point_specifier wp,
619 move_pred_fn pred, void *arg,
620 struct bch_move_stats *stats)
622 struct moving_context ctxt = { .stats = stats };
625 closure_init_stack(&ctxt.cl);
626 INIT_LIST_HEAD(&ctxt.reads);
627 init_waitqueue_head(&ctxt.wait);
629 stats->data_type = BCH_DATA_USER;
631 ret = __bch2_move_data(c, &ctxt, rate, wp, start, end,
632 pred, arg, stats, BTREE_ID_EXTENTS) ?:
633 __bch2_move_data(c, &ctxt, rate, wp, start, end,
634 pred, arg, stats, BTREE_ID_REFLINK);
636 move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
637 closure_sync(&ctxt.cl);
639 EBUG_ON(atomic_read(&ctxt.write_sectors));
642 atomic64_read(&stats->sectors_moved),
643 atomic64_read(&stats->keys_moved));
648 static int bch2_move_btree(struct bch_fs *c,
651 struct bch_move_stats *stats)
653 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
654 struct btree_trans trans;
655 struct btree_iter *iter;
658 struct data_opts data_opts;
662 bch2_trans_init(&trans, c, 0, 0);
664 stats->data_type = BCH_DATA_BTREE;
666 for (id = 0; id < BTREE_ID_NR; id++) {
667 stats->btree_id = id;
669 for_each_btree_node(&trans, iter, id, POS_MIN,
670 BTREE_ITER_PREFETCH, b) {
671 stats->pos = iter->pos;
673 switch ((cmd = pred(c, arg,
674 bkey_i_to_s_c(&b->key),
675 &io_opts, &data_opts))) {
680 case DATA_ADD_REPLICAS:
687 ret = bch2_btree_node_rewrite(c, iter,
688 b->data->keys.seq, 0) ?: ret;
690 bch2_trans_cond_resched(&trans);
693 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
696 bch2_trans_exit(&trans);
702 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
704 struct bch_io_opts *io_opts,
705 struct data_opts *data_opts)
711 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
713 struct bch_io_opts *io_opts,
714 struct data_opts *data_opts)
716 unsigned nr_good = bch2_bkey_durability(c, k);
717 unsigned replicas = 0;
720 case KEY_TYPE_btree_ptr:
721 replicas = c->opts.metadata_replicas;
723 case KEY_TYPE_extent:
724 replicas = io_opts->data_replicas;
728 if (!nr_good || nr_good >= replicas)
731 data_opts->target = 0;
732 data_opts->btree_insert_flags = 0;
733 return DATA_ADD_REPLICAS;
736 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
738 struct bch_io_opts *io_opts,
739 struct data_opts *data_opts)
741 struct bch_ioctl_data *op = arg;
743 if (!bch2_bkey_has_device(k, op->migrate.dev))
746 data_opts->target = 0;
747 data_opts->btree_insert_flags = 0;
748 data_opts->rewrite_dev = op->migrate.dev;
752 int bch2_data_job(struct bch_fs *c,
753 struct bch_move_stats *stats,
754 struct bch_ioctl_data op)
759 case BCH_DATA_OP_REREPLICATE:
760 stats->data_type = BCH_DATA_JOURNAL;
761 ret = bch2_journal_flush_device_pins(&c->journal, -1);
763 ret = bch2_move_btree(c, rereplicate_pred, c, stats) ?: ret;
766 closure_wait_event(&c->btree_interior_update_wait,
767 !bch2_btree_interior_updates_nr_pending(c) ||
768 c->btree_roots_dirty);
769 if (!bch2_btree_interior_updates_nr_pending(c))
771 bch2_journal_meta(&c->journal);
774 ret = bch2_replicas_gc2(c) ?: ret;
776 ret = bch2_move_data(c, NULL,
777 writepoint_hashed((unsigned long) current),
780 rereplicate_pred, c, stats) ?: ret;
781 ret = bch2_replicas_gc2(c) ?: ret;
783 case BCH_DATA_OP_MIGRATE:
784 if (op.migrate.dev >= c->sb.nr_devices)
787 stats->data_type = BCH_DATA_JOURNAL;
788 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
790 ret = bch2_move_btree(c, migrate_pred, &op, stats) ?: ret;
791 ret = bch2_replicas_gc2(c) ?: ret;
793 ret = bch2_move_data(c, NULL,
794 writepoint_hashed((unsigned long) current),
797 migrate_pred, &op, stats) ?: ret;
798 ret = bch2_replicas_gc2(c) ?: ret;