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);
74 struct bkey_i *insert;
75 struct bkey_i_extent *new;
76 BKEY_PADDED(k) _new, _insert;
77 const union bch_extent_entry *entry;
78 struct extent_ptr_decoded p;
79 bool did_work = false;
82 bch2_trans_reset(&trans, 0);
84 k = bch2_btree_iter_peek_slot(iter);
92 new = bkey_i_to_extent(bch2_keylist_front(keys));
94 if (bversion_cmp(k.k->version, new->k.version) ||
95 !bch2_bkey_matches_ptr(c, k, m->ptr, m->offset))
98 if (m->data_cmd == DATA_REWRITE &&
99 !bch2_bkey_has_device(k, m->data_opts.rewrite_dev))
102 bkey_reassemble(&_insert.k, k);
105 bkey_copy(&_new.k, bch2_keylist_front(keys));
106 new = bkey_i_to_extent(&_new.k);
107 bch2_cut_front(iter->pos, &new->k_i);
109 bch2_cut_front(iter->pos, insert);
110 bch2_cut_back(new->k.p, insert);
111 bch2_cut_back(insert->k.p, &new->k_i);
113 if (m->data_cmd == DATA_REWRITE)
114 bch2_bkey_drop_device(bkey_i_to_s(insert),
115 m->data_opts.rewrite_dev);
117 extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) {
118 if (bch2_bkey_has_device(bkey_i_to_s_c(insert), p.ptr.dev)) {
120 * raced with another move op? extent already
121 * has a pointer to the device we just wrote
127 bch2_extent_ptr_decoded_append(insert, &p);
134 bch2_bkey_narrow_crcs(insert,
135 (struct bch_extent_crc_unpacked) { 0 });
136 bch2_extent_normalize(c, bkey_i_to_s(insert));
137 bch2_bkey_mark_replicas_cached(c, bkey_i_to_s(insert),
138 op->opts.background_target,
139 op->opts.data_replicas);
142 * If we're not fully overwriting @k, and it's compressed, we
143 * need a reservation for all the pointers in @insert
145 nr = bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(insert)) -
148 if (insert->k.size < k.k->size &&
149 bch2_bkey_sectors_compressed(k) &&
151 ret = bch2_disk_reservation_add(c, &op->res,
152 keylist_sectors(keys) * nr, 0);
156 m->nr_ptrs_reserved += nr;
160 bch2_trans_update(&trans, iter, insert, 0);
162 ret = bch2_trans_commit(&trans, &op->res,
165 BTREE_INSERT_USE_RESERVE|
166 m->data_opts.btree_insert_flags);
168 atomic_long_inc(&c->extent_migrate_done);
174 while (bkey_cmp(iter->pos, bch2_keylist_front(keys)->k.p) >= 0) {
175 bch2_keylist_pop_front(keys);
176 if (bch2_keylist_empty(keys))
182 BUG_ON(k.k->p.offset <= iter->pos.offset);
183 atomic64_inc(&m->ctxt->stats->keys_raced);
184 atomic64_add(k.k->p.offset - iter->pos.offset,
185 &m->ctxt->stats->sectors_raced);
187 atomic_long_inc(&c->extent_migrate_raced);
188 trace_move_race(&new->k);
189 bch2_btree_iter_next_slot(iter);
193 bch2_trans_exit(&trans);
194 BUG_ON(ret == -EINTR);
198 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
200 /* write bio must own pages: */
201 BUG_ON(!m->op.wbio.bio.bi_vcnt);
203 m->ptr = rbio->pick.ptr;
204 m->offset = rbio->pos.offset - rbio->pick.crc.offset;
205 m->op.devs_have = rbio->devs_have;
206 m->op.pos = rbio->pos;
207 m->op.version = rbio->version;
208 m->op.crc = rbio->pick.crc;
209 m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
211 if (bch2_csum_type_is_encryption(m->op.crc.csum_type)) {
212 m->op.nonce = m->op.crc.nonce + m->op.crc.offset;
213 m->op.csum_type = m->op.crc.csum_type;
216 if (m->data_cmd == DATA_REWRITE)
217 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
220 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
221 struct write_point_specifier wp,
222 struct bch_io_opts io_opts,
223 enum data_cmd data_cmd,
224 struct data_opts data_opts,
225 enum btree_id btree_id,
228 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
229 const union bch_extent_entry *entry;
230 struct extent_ptr_decoded p;
233 m->btree_id = btree_id;
234 m->data_cmd = data_cmd;
235 m->data_opts = data_opts;
236 m->nr_ptrs_reserved = 0;
238 bch2_write_op_init(&m->op, c, io_opts);
240 if (!bch2_bkey_is_incompressible(k))
241 m->op.compression_type =
242 bch2_compression_opt_to_type[io_opts.background_compression ?:
243 io_opts.compression];
245 m->op.incompressible = true;
247 m->op.target = data_opts.target,
248 m->op.write_point = wp;
250 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE) {
251 m->op.alloc_reserve = RESERVE_MOVINGGC;
252 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
254 /* XXX: this should probably be passed in */
255 m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS;
258 m->op.flags |= BCH_WRITE_PAGES_STABLE|
259 BCH_WRITE_PAGES_OWNED|
260 BCH_WRITE_DATA_ENCODED|
261 BCH_WRITE_FROM_INTERNAL;
263 m->op.nr_replicas = 1;
264 m->op.nr_replicas_required = 1;
265 m->op.index_update_fn = bch2_migrate_index_update;
268 case DATA_ADD_REPLICAS: {
270 * DATA_ADD_REPLICAS is used for moving data to a different
271 * device in the background, and due to compression the new copy
272 * might take up more space than the old copy:
275 int nr = (int) io_opts.data_replicas -
276 bch2_bkey_nr_ptrs_allocated(k);
278 int nr = (int) io_opts.data_replicas;
281 m->op.nr_replicas = m->nr_ptrs_reserved = nr;
283 ret = bch2_disk_reservation_get(c, &m->op.res,
284 k.k->size, m->op.nr_replicas, 0);
291 unsigned compressed_sectors = 0;
293 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
295 crc_is_compressed(p.crc) &&
296 bch2_dev_in_target(c, p.ptr.dev, data_opts.target))
297 compressed_sectors += p.crc.compressed_size;
299 if (compressed_sectors) {
300 ret = bch2_disk_reservation_add(c, &m->op.res,
302 BCH_DISK_RESERVATION_NOFAIL);
309 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
310 m->op.flags |= BCH_WRITE_CACHED;
319 static void move_free(struct closure *cl)
321 struct moving_io *io = container_of(cl, struct moving_io, cl);
322 struct moving_context *ctxt = io->write.ctxt;
323 struct bvec_iter_all iter;
326 bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
328 bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter)
330 __free_page(bv->bv_page);
332 wake_up(&ctxt->wait);
337 static void move_write_done(struct closure *cl)
339 struct moving_io *io = container_of(cl, struct moving_io, cl);
341 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
342 closure_return_with_destructor(cl, move_free);
345 static void move_write(struct closure *cl)
347 struct moving_io *io = container_of(cl, struct moving_io, cl);
349 if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) {
350 closure_return_with_destructor(cl, move_free);
354 bch2_migrate_read_done(&io->write, &io->rbio);
356 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
357 closure_call(&io->write.op.cl, bch2_write, NULL, cl);
358 continue_at(cl, move_write_done, NULL);
361 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
363 struct moving_io *io =
364 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
366 return io && io->read_completed ? io : NULL;
369 static void move_read_endio(struct bio *bio)
371 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
372 struct moving_context *ctxt = io->write.ctxt;
374 atomic_sub(io->read_sectors, &ctxt->read_sectors);
375 io->read_completed = true;
377 if (next_pending_write(ctxt))
378 wake_up(&ctxt->wait);
380 closure_put(&ctxt->cl);
383 static void do_pending_writes(struct moving_context *ctxt)
385 struct moving_io *io;
387 while ((io = next_pending_write(ctxt))) {
389 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
393 #define move_ctxt_wait_event(_ctxt, _cond) \
395 do_pending_writes(_ctxt); \
399 __wait_event((_ctxt)->wait, \
400 next_pending_write(_ctxt) || (_cond)); \
403 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
405 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
407 move_ctxt_wait_event(ctxt,
408 !atomic_read(&ctxt->write_sectors) ||
409 atomic_read(&ctxt->write_sectors) != sectors_pending);
412 static int bch2_move_extent(struct bch_fs *c,
413 struct moving_context *ctxt,
414 struct write_point_specifier wp,
415 struct bch_io_opts io_opts,
416 enum btree_id btree_id,
418 enum data_cmd data_cmd,
419 struct data_opts data_opts)
421 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
422 struct moving_io *io;
423 const union bch_extent_entry *entry;
424 struct extent_ptr_decoded p;
425 unsigned sectors = k.k->size, pages;
428 move_ctxt_wait_event(ctxt,
429 atomic_read(&ctxt->write_sectors) <
430 SECTORS_IN_FLIGHT_PER_DEVICE);
432 move_ctxt_wait_event(ctxt,
433 atomic_read(&ctxt->read_sectors) <
434 SECTORS_IN_FLIGHT_PER_DEVICE);
436 /* write path might have to decompress data: */
437 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
438 sectors = max_t(unsigned, sectors, p.crc.uncompressed_size);
440 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
441 io = kzalloc(sizeof(struct moving_io) +
442 sizeof(struct bio_vec) * pages, GFP_KERNEL);
446 io->write.ctxt = ctxt;
447 io->read_sectors = k.k->size;
448 io->write_sectors = k.k->size;
450 bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
451 bio_set_prio(&io->write.op.wbio.bio,
452 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
454 if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9,
459 io->rbio.opts = io_opts;
460 bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
461 io->rbio.bio.bi_vcnt = pages;
462 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
463 io->rbio.bio.bi_iter.bi_size = sectors << 9;
465 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
466 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(k.k);
467 io->rbio.bio.bi_end_io = move_read_endio;
469 ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
470 data_cmd, data_opts, btree_id, k);
474 atomic64_inc(&ctxt->stats->keys_moved);
475 atomic64_add(k.k->size, &ctxt->stats->sectors_moved);
477 trace_move_extent(k.k);
479 atomic_add(io->read_sectors, &ctxt->read_sectors);
480 list_add_tail(&io->list, &ctxt->reads);
483 * dropped by move_read_endio() - guards against use after free of
484 * ctxt when doing wakeup
486 closure_get(&ctxt->cl);
487 bch2_read_extent(c, &io->rbio, k, 0,
489 BCH_READ_LAST_FRAGMENT);
492 bio_free_pages(&io->write.op.wbio.bio);
496 trace_move_alloc_fail(k.k);
500 static int __bch2_move_data(struct bch_fs *c,
501 struct moving_context *ctxt,
502 struct bch_ratelimit *rate,
503 struct write_point_specifier wp,
506 move_pred_fn pred, void *arg,
507 struct bch_move_stats *stats,
508 enum btree_id btree_id)
510 bool kthread = (current->flags & PF_KTHREAD) != 0;
511 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
512 struct bkey_on_stack sk;
513 struct btree_trans trans;
514 struct btree_iter *iter;
516 struct data_opts data_opts;
517 enum data_cmd data_cmd;
518 u64 delay, cur_inum = U64_MAX;
521 bkey_on_stack_init(&sk);
522 bch2_trans_init(&trans, c, 0, 0);
524 stats->data_type = BCH_DATA_user;
525 stats->btree_id = btree_id;
526 stats->pos = POS_MIN;
528 iter = bch2_trans_get_iter(&trans, btree_id, start,
529 BTREE_ITER_PREFETCH);
532 bch2_ratelimit_reset(rate);
536 delay = rate ? bch2_ratelimit_delay(rate) : 0;
539 bch2_trans_unlock(&trans);
540 set_current_state(TASK_INTERRUPTIBLE);
543 if (kthread && (ret = kthread_should_stop())) {
544 __set_current_state(TASK_RUNNING);
549 schedule_timeout(delay);
551 if (unlikely(freezing(current))) {
552 bch2_trans_unlock(&trans);
553 move_ctxt_wait_event(ctxt, list_empty(&ctxt->reads));
558 k = bch2_btree_iter_peek(iter);
560 stats->pos = iter->pos;
567 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
570 if (!bkey_extent_is_direct_data(k.k))
573 if (btree_id == BTREE_ID_EXTENTS &&
574 cur_inum != k.k->p.inode) {
575 struct bch_inode_unpacked inode;
577 /* don't hold btree locks while looking up inode: */
578 bch2_trans_unlock(&trans);
580 io_opts = bch2_opts_to_inode_opts(c->opts);
581 if (!bch2_inode_find_by_inum(c, k.k->p.inode, &inode))
582 bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
583 cur_inum = k.k->p.inode;
587 switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) {
592 case DATA_ADD_REPLICAS:
600 /* unlock before doing IO: */
601 bkey_on_stack_reassemble(&sk, c, k);
602 k = bkey_i_to_s_c(sk.k);
603 bch2_trans_unlock(&trans);
605 ret2 = bch2_move_extent(c, ctxt, wp, io_opts, btree_id, k,
606 data_cmd, data_opts);
608 if (ret2 == -ENOMEM) {
609 /* memory allocation failure, wait for some IO to finish */
610 bch2_move_ctxt_wait_for_io(ctxt);
614 /* XXX signal failure */
619 bch2_ratelimit_increment(rate, k.k->size);
621 atomic64_add(k.k->size * bch2_bkey_nr_ptrs_allocated(k),
622 &stats->sectors_seen);
624 bch2_btree_iter_next(iter);
625 bch2_trans_cond_resched(&trans);
628 ret = bch2_trans_exit(&trans) ?: ret;
629 bkey_on_stack_exit(&sk, c);
634 int bch2_move_data(struct bch_fs *c,
635 struct bch_ratelimit *rate,
636 struct write_point_specifier wp,
639 move_pred_fn pred, void *arg,
640 struct bch_move_stats *stats)
642 struct moving_context ctxt = { .stats = stats };
645 closure_init_stack(&ctxt.cl);
646 INIT_LIST_HEAD(&ctxt.reads);
647 init_waitqueue_head(&ctxt.wait);
649 stats->data_type = BCH_DATA_user;
651 ret = __bch2_move_data(c, &ctxt, rate, wp, start, end,
652 pred, arg, stats, BTREE_ID_EXTENTS) ?:
653 __bch2_move_data(c, &ctxt, rate, wp, start, end,
654 pred, arg, stats, BTREE_ID_REFLINK);
656 move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
657 closure_sync(&ctxt.cl);
659 EBUG_ON(atomic_read(&ctxt.write_sectors));
662 atomic64_read(&stats->sectors_moved),
663 atomic64_read(&stats->keys_moved));
668 static int bch2_move_btree(struct bch_fs *c,
671 struct bch_move_stats *stats)
673 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
674 struct btree_trans trans;
675 struct btree_iter *iter;
678 struct data_opts data_opts;
682 bch2_trans_init(&trans, c, 0, 0);
684 stats->data_type = BCH_DATA_btree;
686 for (id = 0; id < BTREE_ID_NR; id++) {
687 stats->btree_id = id;
689 for_each_btree_node(&trans, iter, id, POS_MIN,
690 BTREE_ITER_PREFETCH, b) {
691 stats->pos = iter->pos;
693 switch ((cmd = pred(c, arg,
694 bkey_i_to_s_c(&b->key),
695 &io_opts, &data_opts))) {
700 case DATA_ADD_REPLICAS:
707 ret = bch2_btree_node_rewrite(c, iter,
708 b->data->keys.seq, 0) ?: ret;
710 bch2_trans_cond_resched(&trans);
713 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
716 bch2_trans_exit(&trans);
722 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
724 struct bch_io_opts *io_opts,
725 struct data_opts *data_opts)
731 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
733 struct bch_io_opts *io_opts,
734 struct data_opts *data_opts)
736 unsigned nr_good = bch2_bkey_durability(c, k);
737 unsigned replicas = 0;
740 case KEY_TYPE_btree_ptr:
741 replicas = c->opts.metadata_replicas;
743 case KEY_TYPE_extent:
744 replicas = io_opts->data_replicas;
748 if (!nr_good || nr_good >= replicas)
751 data_opts->target = 0;
752 data_opts->btree_insert_flags = 0;
753 return DATA_ADD_REPLICAS;
756 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
758 struct bch_io_opts *io_opts,
759 struct data_opts *data_opts)
761 struct bch_ioctl_data *op = arg;
763 if (!bch2_bkey_has_device(k, op->migrate.dev))
766 data_opts->target = 0;
767 data_opts->btree_insert_flags = 0;
768 data_opts->rewrite_dev = op->migrate.dev;
772 int bch2_data_job(struct bch_fs *c,
773 struct bch_move_stats *stats,
774 struct bch_ioctl_data op)
779 case BCH_DATA_OP_REREPLICATE:
780 stats->data_type = BCH_DATA_journal;
781 ret = bch2_journal_flush_device_pins(&c->journal, -1);
783 ret = bch2_move_btree(c, rereplicate_pred, c, stats) ?: ret;
785 closure_wait_event(&c->btree_interior_update_wait,
786 !bch2_btree_interior_updates_nr_pending(c));
788 ret = bch2_replicas_gc2(c) ?: ret;
790 ret = bch2_move_data(c, NULL,
791 writepoint_hashed((unsigned long) current),
794 rereplicate_pred, c, stats) ?: ret;
795 ret = bch2_replicas_gc2(c) ?: ret;
797 case BCH_DATA_OP_MIGRATE:
798 if (op.migrate.dev >= c->sb.nr_devices)
801 stats->data_type = BCH_DATA_journal;
802 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
804 ret = bch2_move_btree(c, migrate_pred, &op, stats) ?: ret;
805 ret = bch2_replicas_gc2(c) ?: ret;
807 ret = bch2_move_data(c, NULL,
808 writepoint_hashed((unsigned long) current),
811 migrate_pred, &op, stats) ?: ret;
812 ret = bch2_replicas_gc2(c) ?: ret;