4 #include "btree_update.h"
8 #include "journal_reclaim.h"
14 #include <linux/ioprio.h>
15 #include <linux/kthread.h>
17 #include <trace/events/bcachefs.h>
19 #define SECTORS_IN_FLIGHT_PER_DEVICE 2048
22 struct list_head list;
26 unsigned read_sectors;
27 unsigned write_sectors;
29 struct bch_read_bio rbio;
31 struct migrate_write write;
32 /* Must be last since it is variable size */
33 struct bio_vec bi_inline_vecs[0];
36 struct moving_context {
37 /* Closure for waiting on all reads and writes to complete */
40 struct bch_move_stats *stats;
42 struct list_head reads;
44 /* in flight sectors: */
45 atomic_t read_sectors;
46 atomic_t write_sectors;
48 wait_queue_head_t wait;
51 static int bch2_migrate_index_update(struct bch_write_op *op)
53 struct bch_fs *c = op->c;
54 struct migrate_write *m =
55 container_of(op, struct migrate_write, op);
56 struct keylist *keys = &op->insert_keys;
57 struct btree_iter iter;
60 bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS,
61 bkey_start_pos(&bch2_keylist_front(keys)->k),
62 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
65 struct bkey_s_c k = bch2_btree_iter_peek_slot(&iter);
66 struct bkey_i_extent *insert, *new =
67 bkey_i_to_extent(bch2_keylist_front(keys));
68 BKEY_PADDED(k) _new, _insert;
69 struct bch_extent_ptr *ptr;
70 struct bch_extent_crc_unpacked crc;
71 bool did_work = false;
74 if (btree_iter_err(k)) {
75 ret = bch2_btree_iter_unlock(&iter);
79 if (bversion_cmp(k.k->version, new->k.version) ||
80 !bkey_extent_is_data(k.k) ||
81 !bch2_extent_matches_ptr(c, bkey_s_c_to_extent(k),
85 if (m->data_cmd == DATA_REWRITE &&
86 !bch2_extent_has_device(bkey_s_c_to_extent(k),
87 m->data_opts.rewrite_dev))
90 bkey_reassemble(&_insert.k, k);
91 insert = bkey_i_to_extent(&_insert.k);
93 bkey_copy(&_new.k, bch2_keylist_front(keys));
94 new = bkey_i_to_extent(&_new.k);
96 bch2_cut_front(iter.pos, &insert->k_i);
97 bch2_cut_back(new->k.p, &insert->k);
98 bch2_cut_back(insert->k.p, &new->k);
100 if (m->data_cmd == DATA_REWRITE) {
101 ptr = (struct bch_extent_ptr *)
102 bch2_extent_has_device(extent_i_to_s_c(insert),
103 m->data_opts.rewrite_dev);
104 bch2_extent_drop_ptr(extent_i_to_s(insert), ptr);
107 extent_for_each_ptr_crc(extent_i_to_s(new), ptr, crc) {
108 if (bch2_extent_has_device(extent_i_to_s_c(insert), ptr->dev)) {
110 * raced with another move op? extent already
111 * has a pointer to the device we just wrote
117 bch2_extent_crc_append(insert, crc);
118 extent_ptr_append(insert, *ptr);
125 bch2_extent_narrow_crcs(insert,
126 (struct bch_extent_crc_unpacked) { 0 });
127 bch2_extent_normalize(c, extent_i_to_s(insert).s);
128 bch2_extent_mark_replicas_cached(c, extent_i_to_s(insert),
129 op->opts.background_target,
130 op->opts.data_replicas);
133 * It's possible we race, and for whatever reason the extent now
134 * has fewer replicas than when we last looked at it - meaning
135 * we need to get a disk reservation here:
137 nr = bch2_extent_nr_dirty_ptrs(bkey_i_to_s_c(&insert->k_i)) -
138 (bch2_extent_nr_dirty_ptrs(k) + m->nr_ptrs_reserved);
141 * can't call bch2_disk_reservation_add() with btree
142 * locks held, at least not without a song and dance
144 bch2_btree_iter_unlock(&iter);
146 ret = bch2_disk_reservation_add(c, &op->res,
147 keylist_sectors(keys) * nr, 0);
151 m->nr_ptrs_reserved += nr;
155 ret = bch2_mark_bkey_replicas(c, BCH_DATA_USER,
156 extent_i_to_s_c(insert).s_c);
160 ret = bch2_btree_insert_at(c, &op->res,
161 NULL, op_journal_seq(op),
164 BTREE_INSERT_USE_RESERVE|
165 m->data_opts.btree_insert_flags,
166 BTREE_INSERT_ENTRY(&iter, &insert->k_i));
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))
180 bch2_cut_front(iter.pos, bch2_keylist_front(keys));
184 atomic64_add(k.k->p.offset - iter.pos.offset,
185 &m->ctxt->stats->sectors_raced);
186 atomic_long_inc(&c->extent_migrate_raced);
187 trace_move_race(&new->k);
188 bch2_btree_iter_next_slot(&iter);
192 bch2_btree_iter_unlock(&iter);
196 void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio)
198 /* write bio must own pages: */
199 BUG_ON(!m->op.wbio.bio.bi_vcnt);
201 m->ptr = rbio->pick.ptr;
202 m->offset = rbio->pos.offset - rbio->pick.crc.offset;
203 m->op.devs_have = rbio->devs_have;
204 m->op.pos = rbio->pos;
205 m->op.version = rbio->version;
206 m->op.crc = rbio->pick.crc;
207 m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9;
209 if (bch2_csum_type_is_encryption(m->op.crc.csum_type)) {
210 m->op.nonce = m->op.crc.nonce + m->op.crc.offset;
211 m->op.csum_type = m->op.crc.csum_type;
214 if (m->data_cmd == DATA_REWRITE)
215 bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev);
218 int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m,
219 struct write_point_specifier wp,
220 struct bch_io_opts io_opts,
221 enum data_cmd data_cmd,
222 struct data_opts data_opts,
227 m->data_cmd = data_cmd;
228 m->data_opts = data_opts;
229 m->nr_ptrs_reserved = 0;
231 bch2_write_op_init(&m->op, c, io_opts);
232 m->op.compression_type =
233 bch2_compression_opt_to_type[io_opts.background_compression ?:
234 io_opts.compression];
235 m->op.target = data_opts.target,
236 m->op.write_point = wp;
238 if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE)
239 m->op.alloc_reserve = RESERVE_MOVINGGC;
241 m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS|
242 BCH_WRITE_PAGES_STABLE|
243 BCH_WRITE_PAGES_OWNED|
244 BCH_WRITE_DATA_ENCODED|
245 BCH_WRITE_NOMARK_REPLICAS;
247 m->op.nr_replicas = 1;
248 m->op.nr_replicas_required = 1;
249 m->op.index_update_fn = bch2_migrate_index_update;
252 case DATA_ADD_REPLICAS: {
253 int nr = (int) io_opts.data_replicas -
254 bch2_extent_nr_dirty_ptrs(k);
257 m->op.nr_replicas = m->nr_ptrs_reserved = nr;
259 ret = bch2_disk_reservation_get(c, &m->op.res,
260 k.k->size, m->op.nr_replicas, 0);
269 m->op.flags |= BCH_WRITE_ALLOC_NOWAIT;
270 m->op.flags |= BCH_WRITE_CACHED;
279 static void move_free(struct closure *cl)
281 struct moving_io *io = container_of(cl, struct moving_io, cl);
282 struct moving_context *ctxt = io->write.ctxt;
286 bch2_disk_reservation_put(io->write.op.c, &io->write.op.res);
288 bio_for_each_segment_all(bv, &io->write.op.wbio.bio, i)
290 __free_page(bv->bv_page);
292 wake_up(&ctxt->wait);
297 static void move_write_done(struct closure *cl)
299 struct moving_io *io = container_of(cl, struct moving_io, cl);
301 atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors);
302 closure_return_with_destructor(cl, move_free);
305 static void move_write(struct closure *cl)
307 struct moving_io *io = container_of(cl, struct moving_io, cl);
309 if (likely(!io->rbio.bio.bi_status &&
311 bch2_migrate_read_done(&io->write, &io->rbio);
313 atomic_add(io->write_sectors, &io->write.ctxt->write_sectors);
314 closure_call(&io->write.op.cl, bch2_write, NULL, cl);
315 continue_at(cl, move_write_done, NULL);
318 closure_return_with_destructor(cl, move_free);
321 static inline struct moving_io *next_pending_write(struct moving_context *ctxt)
323 struct moving_io *io =
324 list_first_entry_or_null(&ctxt->reads, struct moving_io, list);
326 return io && io->read_completed ? io : NULL;
329 static void move_read_endio(struct bio *bio)
331 struct moving_io *io = container_of(bio, struct moving_io, rbio.bio);
332 struct moving_context *ctxt = io->write.ctxt;
334 atomic_sub(io->read_sectors, &ctxt->read_sectors);
335 io->read_completed = true;
337 if (next_pending_write(ctxt))
338 wake_up(&ctxt->wait);
340 closure_put(&ctxt->cl);
343 static void do_pending_writes(struct moving_context *ctxt)
345 struct moving_io *io;
347 while ((io = next_pending_write(ctxt))) {
349 closure_call(&io->cl, move_write, NULL, &ctxt->cl);
353 #define move_ctxt_wait_event(_ctxt, _cond) \
355 do_pending_writes(_ctxt); \
359 __wait_event((_ctxt)->wait, \
360 next_pending_write(_ctxt) || (_cond)); \
363 static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt)
365 unsigned sectors_pending = atomic_read(&ctxt->write_sectors);
367 move_ctxt_wait_event(ctxt,
368 !atomic_read(&ctxt->write_sectors) ||
369 atomic_read(&ctxt->write_sectors) != sectors_pending);
372 static int bch2_move_extent(struct bch_fs *c,
373 struct moving_context *ctxt,
374 struct write_point_specifier wp,
375 struct bch_io_opts io_opts,
376 struct bkey_s_c_extent e,
377 enum data_cmd data_cmd,
378 struct data_opts data_opts)
380 struct moving_io *io;
381 const struct bch_extent_ptr *ptr;
382 struct bch_extent_crc_unpacked crc;
383 unsigned sectors = e.k->size, pages;
386 move_ctxt_wait_event(ctxt,
387 atomic_read(&ctxt->write_sectors) <
388 SECTORS_IN_FLIGHT_PER_DEVICE);
390 move_ctxt_wait_event(ctxt,
391 atomic_read(&ctxt->read_sectors) <
392 SECTORS_IN_FLIGHT_PER_DEVICE);
394 /* write path might have to decompress data: */
395 extent_for_each_ptr_crc(e, ptr, crc)
396 sectors = max_t(unsigned, sectors, crc.uncompressed_size);
398 pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
399 io = kzalloc(sizeof(struct moving_io) +
400 sizeof(struct bio_vec) * pages, GFP_KERNEL);
404 io->write.ctxt = ctxt;
405 io->read_sectors = e.k->size;
406 io->write_sectors = e.k->size;
408 bio_init(&io->write.op.wbio.bio, io->bi_inline_vecs, pages);
409 bio_set_prio(&io->write.op.wbio.bio,
410 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
411 io->write.op.wbio.bio.bi_iter.bi_size = sectors << 9;
413 bch2_bio_map(&io->write.op.wbio.bio, NULL);
414 if (bio_alloc_pages(&io->write.op.wbio.bio, GFP_KERNEL))
417 io->rbio.opts = io_opts;
418 bio_init(&io->rbio.bio, io->bi_inline_vecs, pages);
419 io->rbio.bio.bi_vcnt = pages;
420 bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
421 io->rbio.bio.bi_iter.bi_size = sectors << 9;
423 bio_set_op_attrs(&io->rbio.bio, REQ_OP_READ, 0);
424 io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(e.k);
425 io->rbio.bio.bi_end_io = move_read_endio;
427 ret = bch2_migrate_write_init(c, &io->write, wp, io_opts,
428 data_cmd, data_opts, e.s_c);
432 atomic64_inc(&ctxt->stats->keys_moved);
433 atomic64_add(e.k->size, &ctxt->stats->sectors_moved);
435 trace_move_extent(e.k);
437 atomic_add(io->read_sectors, &ctxt->read_sectors);
438 list_add_tail(&io->list, &ctxt->reads);
441 * dropped by move_read_endio() - guards against use after free of
442 * ctxt when doing wakeup
444 closure_get(&ctxt->cl);
445 bch2_read_extent(c, &io->rbio, e.s_c,
447 BCH_READ_LAST_FRAGMENT);
450 bio_free_pages(&io->write.op.wbio.bio);
454 trace_move_alloc_fail(e.k);
458 int bch2_move_data(struct bch_fs *c,
459 struct bch_ratelimit *rate,
460 struct write_point_specifier wp,
463 move_pred_fn pred, void *arg,
464 struct bch_move_stats *stats)
466 bool kthread = (current->flags & PF_KTHREAD) != 0;
467 struct moving_context ctxt = { .stats = stats };
468 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
471 struct bkey_s_c_extent e;
472 struct data_opts data_opts;
473 enum data_cmd data_cmd;
474 u64 cur_inum = U64_MAX;
477 closure_init_stack(&ctxt.cl);
478 INIT_LIST_HEAD(&ctxt.reads);
479 init_waitqueue_head(&ctxt.wait);
481 stats->data_type = BCH_DATA_USER;
482 bch2_btree_iter_init(&stats->iter, c, BTREE_ID_EXTENTS, start,
483 BTREE_ITER_PREFETCH);
486 bch2_ratelimit_reset(rate);
488 while (!kthread || !(ret = kthread_should_stop())) {
490 bch2_ratelimit_delay(rate) &&
491 (bch2_btree_iter_unlock(&stats->iter),
492 (ret = bch2_ratelimit_wait_freezable_stoppable(rate))))
495 k = bch2_btree_iter_peek(&stats->iter);
498 ret = btree_iter_err(k);
501 if (bkey_cmp(bkey_start_pos(k.k), end) >= 0)
504 if (!bkey_extent_is_data(k.k))
507 e = bkey_s_c_to_extent(k);
509 if (cur_inum != k.k->p.inode) {
510 struct bch_inode_unpacked inode;
512 /* don't hold btree locks while looking up inode: */
513 bch2_btree_iter_unlock(&stats->iter);
515 io_opts = bch2_opts_to_inode_opts(c->opts);
516 if (!bch2_inode_find_by_inum(c, k.k->p.inode, &inode))
517 bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
518 cur_inum = k.k->p.inode;
522 switch ((data_cmd = pred(c, arg, BKEY_TYPE_EXTENTS, e,
523 &io_opts, &data_opts))) {
528 case DATA_ADD_REPLICAS:
536 /* unlock before doing IO: */
537 bkey_reassemble(&tmp.k, k);
538 k = bkey_i_to_s_c(&tmp.k);
539 bch2_btree_iter_unlock(&stats->iter);
541 ret2 = bch2_move_extent(c, &ctxt, wp, io_opts,
542 bkey_s_c_to_extent(k),
543 data_cmd, data_opts);
545 if (ret2 == -ENOMEM) {
546 /* memory allocation failure, wait for some IO to finish */
547 bch2_move_ctxt_wait_for_io(&ctxt);
551 /* XXX signal failure */
556 bch2_ratelimit_increment(rate, k.k->size);
558 atomic64_add(k.k->size * bch2_extent_nr_dirty_ptrs(k),
559 &stats->sectors_seen);
561 bch2_btree_iter_next(&stats->iter);
562 bch2_btree_iter_cond_resched(&stats->iter);
565 bch2_btree_iter_unlock(&stats->iter);
567 move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads));
568 closure_sync(&ctxt.cl);
570 EBUG_ON(atomic_read(&ctxt.write_sectors));
573 atomic64_read(&stats->sectors_moved),
574 atomic64_read(&stats->keys_moved));
579 static int bch2_gc_data_replicas(struct bch_fs *c)
581 struct btree_iter iter;
585 mutex_lock(&c->replicas_gc_lock);
586 bch2_replicas_gc_start(c, (1 << BCH_DATA_USER)|(1 << BCH_DATA_CACHED));
588 for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN,
589 BTREE_ITER_PREFETCH, k) {
590 ret = bch2_mark_bkey_replicas(c, BCH_DATA_USER, k);
594 ret = bch2_btree_iter_unlock(&iter) ?: ret;
596 bch2_replicas_gc_end(c, ret);
597 mutex_unlock(&c->replicas_gc_lock);
602 static int bch2_gc_btree_replicas(struct bch_fs *c)
604 struct btree_iter iter;
609 mutex_lock(&c->replicas_gc_lock);
610 bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE);
612 for (id = 0; id < BTREE_ID_NR; id++) {
613 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
614 ret = bch2_mark_bkey_replicas(c, BCH_DATA_BTREE,
615 bkey_i_to_s_c(&b->key));
617 bch2_btree_iter_cond_resched(&iter);
620 ret = bch2_btree_iter_unlock(&iter) ?: ret;
623 bch2_replicas_gc_end(c, ret);
624 mutex_unlock(&c->replicas_gc_lock);
629 static int bch2_move_btree(struct bch_fs *c,
632 struct bch_move_stats *stats)
634 struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts);
637 struct data_opts data_opts;
641 stats->data_type = BCH_DATA_BTREE;
643 for (id = 0; id < BTREE_ID_NR; id++) {
644 for_each_btree_node(&stats->iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
645 switch ((cmd = pred(c, arg, BKEY_TYPE_BTREE,
646 bkey_i_to_s_c_extent(&b->key),
653 case DATA_ADD_REPLICAS:
660 ret = bch2_btree_node_rewrite(c, &stats->iter,
661 b->data->keys.seq, 0) ?: ret;
663 bch2_btree_iter_cond_resched(&stats->iter);
666 ret = bch2_btree_iter_unlock(&stats->iter) ?: ret;
673 static enum data_cmd scrub_pred(struct bch_fs *c, void *arg,
675 struct bkey_s_c_extent e,
676 struct bch_io_opts *io_opts,
677 struct data_opts *data_opts)
683 static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg,
685 struct bkey_s_c_extent e,
686 struct bch_io_opts *io_opts,
687 struct data_opts *data_opts)
689 unsigned nr_good = bch2_extent_durability(c, e);
690 unsigned replicas = type == BKEY_TYPE_BTREE
691 ? c->opts.metadata_replicas
692 : io_opts->data_replicas;
694 if (!nr_good || nr_good >= replicas)
697 data_opts->target = 0;
698 data_opts->btree_insert_flags = 0;
699 return DATA_ADD_REPLICAS;
702 static enum data_cmd migrate_pred(struct bch_fs *c, void *arg,
704 struct bkey_s_c_extent e,
705 struct bch_io_opts *io_opts,
706 struct data_opts *data_opts)
708 struct bch_ioctl_data *op = arg;
710 if (!bch2_extent_has_device(e, op->migrate.dev))
713 data_opts->target = 0;
714 data_opts->btree_insert_flags = 0;
715 data_opts->rewrite_dev = op->migrate.dev;
719 int bch2_data_job(struct bch_fs *c,
720 struct bch_move_stats *stats,
721 struct bch_ioctl_data op)
726 case BCH_DATA_OP_REREPLICATE:
727 stats->data_type = BCH_DATA_JOURNAL;
728 ret = bch2_journal_flush_device_pins(&c->journal, -1);
730 ret = bch2_move_btree(c, rereplicate_pred, c, stats) ?: ret;
731 ret = bch2_gc_btree_replicas(c) ?: ret;
733 ret = bch2_move_data(c, NULL,
734 writepoint_hashed((unsigned long) current),
737 rereplicate_pred, c, stats) ?: ret;
738 ret = bch2_gc_data_replicas(c) ?: ret;
740 case BCH_DATA_OP_MIGRATE:
741 if (op.migrate.dev >= c->sb.nr_devices)
744 stats->data_type = BCH_DATA_JOURNAL;
745 ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
747 ret = bch2_move_btree(c, migrate_pred, &op, stats) ?: ret;
748 ret = bch2_gc_btree_replicas(c) ?: ret;
750 ret = bch2_move_data(c, NULL,
751 writepoint_hashed((unsigned long) current),
754 migrate_pred, &op, stats) ?: ret;
755 ret = bch2_gc_data_replicas(c) ?: ret;