Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
3 | #include "bcachefs.h" | |
7b3f84ea | 4 | #include "alloc_foreground.h" |
1c6fdbd8 KO |
5 | #include "btree_gc.h" |
6 | #include "btree_update.h" | |
7 | #include "buckets.h" | |
4628529f | 8 | #include "disk_groups.h" |
1c6fdbd8 KO |
9 | #include "inode.h" |
10 | #include "io.h" | |
11 | #include "journal_reclaim.h" | |
12 | #include "keylist.h" | |
13 | #include "move.h" | |
14 | #include "replicas.h" | |
15 | #include "super-io.h" | |
16 | #include "trace.h" | |
17 | ||
18 | #include <linux/ioprio.h> | |
19 | #include <linux/kthread.h> | |
20 | ||
21 | #define SECTORS_IN_FLIGHT_PER_DEVICE 2048 | |
22 | ||
23 | struct moving_io { | |
24 | struct list_head list; | |
25 | struct closure cl; | |
26 | bool read_completed; | |
27 | ||
28 | unsigned read_sectors; | |
29 | unsigned write_sectors; | |
30 | ||
31 | struct bch_read_bio rbio; | |
32 | ||
33 | struct migrate_write write; | |
34 | /* Must be last since it is variable size */ | |
35 | struct bio_vec bi_inline_vecs[0]; | |
36 | }; | |
37 | ||
38 | struct moving_context { | |
39 | /* Closure for waiting on all reads and writes to complete */ | |
40 | struct closure cl; | |
41 | ||
42 | struct bch_move_stats *stats; | |
43 | ||
44 | struct list_head reads; | |
45 | ||
46 | /* in flight sectors: */ | |
47 | atomic_t read_sectors; | |
48 | atomic_t write_sectors; | |
49 | ||
50 | wait_queue_head_t wait; | |
51 | }; | |
52 | ||
53 | static int bch2_migrate_index_update(struct bch_write_op *op) | |
54 | { | |
55 | struct bch_fs *c = op->c; | |
56 | struct migrate_write *m = | |
57 | container_of(op, struct migrate_write, op); | |
58 | struct keylist *keys = &op->insert_keys; | |
59 | struct btree_iter iter; | |
60 | int ret = 0; | |
61 | ||
62 | bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS, | |
63 | bkey_start_pos(&bch2_keylist_front(keys)->k), | |
64 | BTREE_ITER_SLOTS|BTREE_ITER_INTENT); | |
65 | ||
66 | while (1) { | |
67 | struct bkey_s_c k = bch2_btree_iter_peek_slot(&iter); | |
68 | struct bkey_i_extent *insert, *new = | |
69 | bkey_i_to_extent(bch2_keylist_front(keys)); | |
70 | BKEY_PADDED(k) _new, _insert; | |
1742237b KO |
71 | const union bch_extent_entry *entry; |
72 | struct extent_ptr_decoded p; | |
1c6fdbd8 KO |
73 | bool did_work = false; |
74 | int nr; | |
75 | ||
76 | if (btree_iter_err(k)) { | |
77 | ret = bch2_btree_iter_unlock(&iter); | |
78 | break; | |
79 | } | |
80 | ||
81 | if (bversion_cmp(k.k->version, new->k.version) || | |
82 | !bkey_extent_is_data(k.k) || | |
83 | !bch2_extent_matches_ptr(c, bkey_s_c_to_extent(k), | |
84 | m->ptr, m->offset)) | |
85 | goto nomatch; | |
86 | ||
87 | if (m->data_cmd == DATA_REWRITE && | |
88 | !bch2_extent_has_device(bkey_s_c_to_extent(k), | |
89 | m->data_opts.rewrite_dev)) | |
90 | goto nomatch; | |
91 | ||
92 | bkey_reassemble(&_insert.k, k); | |
93 | insert = bkey_i_to_extent(&_insert.k); | |
94 | ||
95 | bkey_copy(&_new.k, bch2_keylist_front(keys)); | |
96 | new = bkey_i_to_extent(&_new.k); | |
97 | ||
98 | bch2_cut_front(iter.pos, &insert->k_i); | |
99 | bch2_cut_back(new->k.p, &insert->k); | |
100 | bch2_cut_back(insert->k.p, &new->k); | |
101 | ||
a2753581 | 102 | if (m->data_cmd == DATA_REWRITE) |
26609b61 KO |
103 | bch2_bkey_drop_device(extent_i_to_s(insert).s, |
104 | m->data_opts.rewrite_dev); | |
1c6fdbd8 | 105 | |
1742237b KO |
106 | extent_for_each_ptr_decode(extent_i_to_s(new), p, entry) { |
107 | if (bch2_extent_has_device(extent_i_to_s_c(insert), p.ptr.dev)) { | |
1c6fdbd8 KO |
108 | /* |
109 | * raced with another move op? extent already | |
110 | * has a pointer to the device we just wrote | |
111 | * data to | |
112 | */ | |
113 | continue; | |
114 | } | |
115 | ||
71c9e0ba | 116 | bch2_extent_ptr_decoded_append(insert, &p); |
1c6fdbd8 KO |
117 | did_work = true; |
118 | } | |
119 | ||
120 | if (!did_work) | |
121 | goto nomatch; | |
122 | ||
123 | bch2_extent_narrow_crcs(insert, | |
124 | (struct bch_extent_crc_unpacked) { 0 }); | |
125 | bch2_extent_normalize(c, extent_i_to_s(insert).s); | |
126 | bch2_extent_mark_replicas_cached(c, extent_i_to_s(insert), | |
127 | op->opts.background_target, | |
128 | op->opts.data_replicas); | |
129 | ||
130 | /* | |
131 | * It's possible we race, and for whatever reason the extent now | |
132 | * has fewer replicas than when we last looked at it - meaning | |
133 | * we need to get a disk reservation here: | |
134 | */ | |
26609b61 KO |
135 | nr = bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(&insert->k_i)) - |
136 | (bch2_bkey_nr_dirty_ptrs(k) + m->nr_ptrs_reserved); | |
1c6fdbd8 KO |
137 | if (nr > 0) { |
138 | /* | |
139 | * can't call bch2_disk_reservation_add() with btree | |
140 | * locks held, at least not without a song and dance | |
141 | */ | |
142 | bch2_btree_iter_unlock(&iter); | |
143 | ||
144 | ret = bch2_disk_reservation_add(c, &op->res, | |
145 | keylist_sectors(keys) * nr, 0); | |
146 | if (ret) | |
147 | goto out; | |
148 | ||
149 | m->nr_ptrs_reserved += nr; | |
150 | goto next; | |
151 | } | |
152 | ||
1c6fdbd8 | 153 | ret = bch2_btree_insert_at(c, &op->res, |
fc3268c1 | 154 | op_journal_seq(op), |
1c6fdbd8 KO |
155 | BTREE_INSERT_ATOMIC| |
156 | BTREE_INSERT_NOFAIL| | |
157 | BTREE_INSERT_USE_RESERVE| | |
158 | m->data_opts.btree_insert_flags, | |
159 | BTREE_INSERT_ENTRY(&iter, &insert->k_i)); | |
160 | if (!ret) | |
161 | atomic_long_inc(&c->extent_migrate_done); | |
162 | if (ret == -EINTR) | |
163 | ret = 0; | |
164 | if (ret) | |
165 | break; | |
166 | next: | |
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)) | |
170 | goto out; | |
171 | } | |
172 | ||
173 | bch2_cut_front(iter.pos, bch2_keylist_front(keys)); | |
174 | continue; | |
175 | nomatch: | |
176 | if (m->ctxt) | |
177 | atomic64_add(k.k->p.offset - iter.pos.offset, | |
178 | &m->ctxt->stats->sectors_raced); | |
179 | atomic_long_inc(&c->extent_migrate_raced); | |
180 | trace_move_race(&new->k); | |
181 | bch2_btree_iter_next_slot(&iter); | |
182 | goto next; | |
183 | } | |
184 | out: | |
185 | bch2_btree_iter_unlock(&iter); | |
186 | return ret; | |
187 | } | |
188 | ||
189 | void bch2_migrate_read_done(struct migrate_write *m, struct bch_read_bio *rbio) | |
190 | { | |
191 | /* write bio must own pages: */ | |
192 | BUG_ON(!m->op.wbio.bio.bi_vcnt); | |
193 | ||
194 | m->ptr = rbio->pick.ptr; | |
195 | m->offset = rbio->pos.offset - rbio->pick.crc.offset; | |
196 | m->op.devs_have = rbio->devs_have; | |
197 | m->op.pos = rbio->pos; | |
198 | m->op.version = rbio->version; | |
199 | m->op.crc = rbio->pick.crc; | |
200 | m->op.wbio.bio.bi_iter.bi_size = m->op.crc.compressed_size << 9; | |
201 | ||
202 | if (bch2_csum_type_is_encryption(m->op.crc.csum_type)) { | |
203 | m->op.nonce = m->op.crc.nonce + m->op.crc.offset; | |
204 | m->op.csum_type = m->op.crc.csum_type; | |
205 | } | |
206 | ||
207 | if (m->data_cmd == DATA_REWRITE) | |
208 | bch2_dev_list_drop_dev(&m->op.devs_have, m->data_opts.rewrite_dev); | |
209 | } | |
210 | ||
211 | int bch2_migrate_write_init(struct bch_fs *c, struct migrate_write *m, | |
212 | struct write_point_specifier wp, | |
213 | struct bch_io_opts io_opts, | |
214 | enum data_cmd data_cmd, | |
215 | struct data_opts data_opts, | |
216 | struct bkey_s_c k) | |
217 | { | |
218 | int ret; | |
219 | ||
220 | m->data_cmd = data_cmd; | |
221 | m->data_opts = data_opts; | |
222 | m->nr_ptrs_reserved = 0; | |
223 | ||
224 | bch2_write_op_init(&m->op, c, io_opts); | |
225 | m->op.compression_type = | |
226 | bch2_compression_opt_to_type[io_opts.background_compression ?: | |
227 | io_opts.compression]; | |
228 | m->op.target = data_opts.target, | |
229 | m->op.write_point = wp; | |
230 | ||
231 | if (m->data_opts.btree_insert_flags & BTREE_INSERT_USE_RESERVE) | |
232 | m->op.alloc_reserve = RESERVE_MOVINGGC; | |
233 | ||
234 | m->op.flags |= BCH_WRITE_ONLY_SPECIFIED_DEVS| | |
235 | BCH_WRITE_PAGES_STABLE| | |
236 | BCH_WRITE_PAGES_OWNED| | |
1d25849c | 237 | BCH_WRITE_DATA_ENCODED; |
1c6fdbd8 KO |
238 | |
239 | m->op.nr_replicas = 1; | |
240 | m->op.nr_replicas_required = 1; | |
241 | m->op.index_update_fn = bch2_migrate_index_update; | |
242 | ||
243 | switch (data_cmd) { | |
244 | case DATA_ADD_REPLICAS: { | |
245 | int nr = (int) io_opts.data_replicas - | |
26609b61 | 246 | bch2_bkey_nr_dirty_ptrs(k); |
1c6fdbd8 KO |
247 | |
248 | if (nr > 0) { | |
249 | m->op.nr_replicas = m->nr_ptrs_reserved = nr; | |
250 | ||
251 | ret = bch2_disk_reservation_get(c, &m->op.res, | |
252 | k.k->size, m->op.nr_replicas, 0); | |
253 | if (ret) | |
254 | return ret; | |
255 | } | |
256 | break; | |
257 | } | |
4628529f KO |
258 | case DATA_REWRITE: { |
259 | const union bch_extent_entry *entry; | |
260 | struct extent_ptr_decoded p; | |
261 | unsigned compressed_sectors = 0; | |
262 | ||
263 | extent_for_each_ptr_decode(bkey_s_c_to_extent(k), p, entry) | |
264 | if (!p.ptr.cached && | |
265 | p.crc.compression_type != BCH_COMPRESSION_NONE && | |
266 | bch2_dev_in_target(c, p.ptr.dev, data_opts.target)) | |
267 | compressed_sectors += p.crc.compressed_size; | |
268 | ||
269 | if (compressed_sectors) { | |
270 | ret = bch2_disk_reservation_add(c, &m->op.res, | |
271 | compressed_sectors, | |
272 | BCH_DISK_RESERVATION_NOFAIL); | |
273 | if (ret) | |
274 | return ret; | |
275 | } | |
1c6fdbd8 | 276 | break; |
4628529f | 277 | } |
1c6fdbd8 KO |
278 | case DATA_PROMOTE: |
279 | m->op.flags |= BCH_WRITE_ALLOC_NOWAIT; | |
280 | m->op.flags |= BCH_WRITE_CACHED; | |
281 | break; | |
282 | default: | |
283 | BUG(); | |
284 | } | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
289 | static void move_free(struct closure *cl) | |
290 | { | |
291 | struct moving_io *io = container_of(cl, struct moving_io, cl); | |
292 | struct moving_context *ctxt = io->write.ctxt; | |
293 | struct bvec_iter_all iter; | |
294 | struct bio_vec *bv; | |
295 | ||
296 | bch2_disk_reservation_put(io->write.op.c, &io->write.op.res); | |
297 | ||
298 | bio_for_each_segment_all(bv, &io->write.op.wbio.bio, iter) | |
299 | if (bv->bv_page) | |
300 | __free_page(bv->bv_page); | |
301 | ||
302 | wake_up(&ctxt->wait); | |
303 | ||
304 | kfree(io); | |
305 | } | |
306 | ||
307 | static void move_write_done(struct closure *cl) | |
308 | { | |
309 | struct moving_io *io = container_of(cl, struct moving_io, cl); | |
310 | ||
311 | atomic_sub(io->write_sectors, &io->write.ctxt->write_sectors); | |
312 | closure_return_with_destructor(cl, move_free); | |
313 | } | |
314 | ||
315 | static void move_write(struct closure *cl) | |
316 | { | |
317 | struct moving_io *io = container_of(cl, struct moving_io, cl); | |
318 | ||
319 | if (unlikely(io->rbio.bio.bi_status || io->rbio.hole)) { | |
320 | closure_return_with_destructor(cl, move_free); | |
321 | return; | |
322 | } | |
323 | ||
324 | bch2_migrate_read_done(&io->write, &io->rbio); | |
325 | ||
326 | atomic_add(io->write_sectors, &io->write.ctxt->write_sectors); | |
327 | closure_call(&io->write.op.cl, bch2_write, NULL, cl); | |
328 | continue_at(cl, move_write_done, NULL); | |
329 | } | |
330 | ||
331 | static inline struct moving_io *next_pending_write(struct moving_context *ctxt) | |
332 | { | |
333 | struct moving_io *io = | |
334 | list_first_entry_or_null(&ctxt->reads, struct moving_io, list); | |
335 | ||
336 | return io && io->read_completed ? io : NULL; | |
337 | } | |
338 | ||
339 | static void move_read_endio(struct bio *bio) | |
340 | { | |
341 | struct moving_io *io = container_of(bio, struct moving_io, rbio.bio); | |
342 | struct moving_context *ctxt = io->write.ctxt; | |
343 | ||
344 | atomic_sub(io->read_sectors, &ctxt->read_sectors); | |
345 | io->read_completed = true; | |
346 | ||
347 | if (next_pending_write(ctxt)) | |
348 | wake_up(&ctxt->wait); | |
349 | ||
350 | closure_put(&ctxt->cl); | |
351 | } | |
352 | ||
353 | static void do_pending_writes(struct moving_context *ctxt) | |
354 | { | |
355 | struct moving_io *io; | |
356 | ||
357 | while ((io = next_pending_write(ctxt))) { | |
358 | list_del(&io->list); | |
359 | closure_call(&io->cl, move_write, NULL, &ctxt->cl); | |
360 | } | |
361 | } | |
362 | ||
363 | #define move_ctxt_wait_event(_ctxt, _cond) \ | |
364 | do { \ | |
365 | do_pending_writes(_ctxt); \ | |
366 | \ | |
367 | if (_cond) \ | |
368 | break; \ | |
369 | __wait_event((_ctxt)->wait, \ | |
370 | next_pending_write(_ctxt) || (_cond)); \ | |
371 | } while (1) | |
372 | ||
373 | static void bch2_move_ctxt_wait_for_io(struct moving_context *ctxt) | |
374 | { | |
375 | unsigned sectors_pending = atomic_read(&ctxt->write_sectors); | |
376 | ||
377 | move_ctxt_wait_event(ctxt, | |
378 | !atomic_read(&ctxt->write_sectors) || | |
379 | atomic_read(&ctxt->write_sectors) != sectors_pending); | |
380 | } | |
381 | ||
382 | static int bch2_move_extent(struct bch_fs *c, | |
383 | struct moving_context *ctxt, | |
384 | struct write_point_specifier wp, | |
385 | struct bch_io_opts io_opts, | |
386 | struct bkey_s_c_extent e, | |
387 | enum data_cmd data_cmd, | |
388 | struct data_opts data_opts) | |
389 | { | |
390 | struct moving_io *io; | |
1742237b KO |
391 | const union bch_extent_entry *entry; |
392 | struct extent_ptr_decoded p; | |
1c6fdbd8 KO |
393 | unsigned sectors = e.k->size, pages; |
394 | int ret = -ENOMEM; | |
395 | ||
396 | move_ctxt_wait_event(ctxt, | |
397 | atomic_read(&ctxt->write_sectors) < | |
398 | SECTORS_IN_FLIGHT_PER_DEVICE); | |
399 | ||
400 | move_ctxt_wait_event(ctxt, | |
401 | atomic_read(&ctxt->read_sectors) < | |
402 | SECTORS_IN_FLIGHT_PER_DEVICE); | |
403 | ||
404 | /* write path might have to decompress data: */ | |
1742237b KO |
405 | extent_for_each_ptr_decode(e, p, entry) |
406 | sectors = max_t(unsigned, sectors, p.crc.uncompressed_size); | |
1c6fdbd8 KO |
407 | |
408 | pages = DIV_ROUND_UP(sectors, PAGE_SECTORS); | |
409 | io = kzalloc(sizeof(struct moving_io) + | |
410 | sizeof(struct bio_vec) * pages, GFP_KERNEL); | |
411 | if (!io) | |
412 | goto err; | |
413 | ||
414 | io->write.ctxt = ctxt; | |
415 | io->read_sectors = e.k->size; | |
416 | io->write_sectors = e.k->size; | |
417 | ||
418 | bio_init(&io->write.op.wbio.bio, NULL, io->bi_inline_vecs, pages, 0); | |
419 | bio_set_prio(&io->write.op.wbio.bio, | |
420 | IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); | |
421 | ||
422 | if (bch2_bio_alloc_pages(&io->write.op.wbio.bio, sectors << 9, | |
423 | GFP_KERNEL)) | |
424 | goto err_free; | |
425 | ||
426 | io->rbio.opts = io_opts; | |
427 | bio_init(&io->rbio.bio, NULL, io->bi_inline_vecs, pages, 0); | |
428 | io->rbio.bio.bi_vcnt = pages; | |
429 | bio_set_prio(&io->rbio.bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); | |
430 | io->rbio.bio.bi_iter.bi_size = sectors << 9; | |
431 | ||
432 | io->rbio.bio.bi_opf = REQ_OP_READ; | |
433 | io->rbio.bio.bi_iter.bi_sector = bkey_start_offset(e.k); | |
434 | io->rbio.bio.bi_end_io = move_read_endio; | |
435 | ||
436 | ret = bch2_migrate_write_init(c, &io->write, wp, io_opts, | |
437 | data_cmd, data_opts, e.s_c); | |
438 | if (ret) | |
439 | goto err_free_pages; | |
440 | ||
441 | atomic64_inc(&ctxt->stats->keys_moved); | |
442 | atomic64_add(e.k->size, &ctxt->stats->sectors_moved); | |
443 | ||
444 | trace_move_extent(e.k); | |
445 | ||
446 | atomic_add(io->read_sectors, &ctxt->read_sectors); | |
447 | list_add_tail(&io->list, &ctxt->reads); | |
448 | ||
449 | /* | |
450 | * dropped by move_read_endio() - guards against use after free of | |
451 | * ctxt when doing wakeup | |
452 | */ | |
453 | closure_get(&ctxt->cl); | |
454 | bch2_read_extent(c, &io->rbio, e.s_c, | |
455 | BCH_READ_NODECODE| | |
456 | BCH_READ_LAST_FRAGMENT); | |
457 | return 0; | |
458 | err_free_pages: | |
459 | bio_free_pages(&io->write.op.wbio.bio); | |
460 | err_free: | |
461 | kfree(io); | |
462 | err: | |
463 | trace_move_alloc_fail(e.k); | |
464 | return ret; | |
465 | } | |
466 | ||
467 | int bch2_move_data(struct bch_fs *c, | |
468 | struct bch_ratelimit *rate, | |
469 | struct write_point_specifier wp, | |
470 | struct bpos start, | |
471 | struct bpos end, | |
472 | move_pred_fn pred, void *arg, | |
473 | struct bch_move_stats *stats) | |
474 | { | |
475 | bool kthread = (current->flags & PF_KTHREAD) != 0; | |
476 | struct moving_context ctxt = { .stats = stats }; | |
477 | struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts); | |
478 | BKEY_PADDED(k) tmp; | |
479 | struct bkey_s_c k; | |
1c6fdbd8 KO |
480 | struct data_opts data_opts; |
481 | enum data_cmd data_cmd; | |
c2fcff59 | 482 | u64 delay, cur_inum = U64_MAX; |
1c6fdbd8 KO |
483 | int ret = 0, ret2; |
484 | ||
485 | closure_init_stack(&ctxt.cl); | |
486 | INIT_LIST_HEAD(&ctxt.reads); | |
487 | init_waitqueue_head(&ctxt.wait); | |
488 | ||
489 | stats->data_type = BCH_DATA_USER; | |
490 | bch2_btree_iter_init(&stats->iter, c, BTREE_ID_EXTENTS, start, | |
491 | BTREE_ITER_PREFETCH); | |
492 | ||
493 | if (rate) | |
494 | bch2_ratelimit_reset(rate); | |
495 | ||
c2fcff59 KO |
496 | while (1) { |
497 | do { | |
498 | delay = rate ? bch2_ratelimit_delay(rate) : 0; | |
499 | ||
500 | if (delay) { | |
501 | bch2_btree_iter_unlock(&stats->iter); | |
502 | set_current_state(TASK_INTERRUPTIBLE); | |
503 | } | |
504 | ||
505 | if (kthread && (ret = kthread_should_stop())) { | |
506 | __set_current_state(TASK_RUNNING); | |
507 | goto out; | |
508 | } | |
509 | ||
510 | if (delay) | |
511 | schedule_timeout(delay); | |
512 | ||
513 | if (unlikely(freezing(current))) { | |
514 | bch2_btree_iter_unlock(&stats->iter); | |
515 | move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads)); | |
c2fcff59 KO |
516 | try_to_freeze(); |
517 | } | |
518 | } while (delay); | |
1c6fdbd8 KO |
519 | peek: |
520 | k = bch2_btree_iter_peek(&stats->iter); | |
521 | if (!k.k) | |
522 | break; | |
523 | ret = btree_iter_err(k); | |
524 | if (ret) | |
525 | break; | |
526 | if (bkey_cmp(bkey_start_pos(k.k), end) >= 0) | |
527 | break; | |
528 | ||
529 | if (!bkey_extent_is_data(k.k)) | |
530 | goto next_nondata; | |
531 | ||
1c6fdbd8 KO |
532 | if (cur_inum != k.k->p.inode) { |
533 | struct bch_inode_unpacked inode; | |
534 | ||
535 | /* don't hold btree locks while looking up inode: */ | |
536 | bch2_btree_iter_unlock(&stats->iter); | |
537 | ||
538 | io_opts = bch2_opts_to_inode_opts(c->opts); | |
539 | if (!bch2_inode_find_by_inum(c, k.k->p.inode, &inode)) | |
540 | bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode)); | |
541 | cur_inum = k.k->p.inode; | |
542 | goto peek; | |
543 | } | |
544 | ||
26609b61 | 545 | switch ((data_cmd = pred(c, arg, k, &io_opts, &data_opts))) { |
1c6fdbd8 KO |
546 | case DATA_SKIP: |
547 | goto next; | |
548 | case DATA_SCRUB: | |
549 | BUG(); | |
550 | case DATA_ADD_REPLICAS: | |
551 | case DATA_REWRITE: | |
552 | case DATA_PROMOTE: | |
553 | break; | |
554 | default: | |
555 | BUG(); | |
556 | } | |
557 | ||
558 | /* unlock before doing IO: */ | |
559 | bkey_reassemble(&tmp.k, k); | |
560 | k = bkey_i_to_s_c(&tmp.k); | |
561 | bch2_btree_iter_unlock(&stats->iter); | |
562 | ||
563 | ret2 = bch2_move_extent(c, &ctxt, wp, io_opts, | |
564 | bkey_s_c_to_extent(k), | |
565 | data_cmd, data_opts); | |
566 | if (ret2) { | |
567 | if (ret2 == -ENOMEM) { | |
568 | /* memory allocation failure, wait for some IO to finish */ | |
569 | bch2_move_ctxt_wait_for_io(&ctxt); | |
570 | continue; | |
571 | } | |
572 | ||
573 | /* XXX signal failure */ | |
574 | goto next; | |
575 | } | |
576 | ||
577 | if (rate) | |
578 | bch2_ratelimit_increment(rate, k.k->size); | |
579 | next: | |
26609b61 | 580 | atomic64_add(k.k->size * bch2_bkey_nr_dirty_ptrs(k), |
1c6fdbd8 KO |
581 | &stats->sectors_seen); |
582 | next_nondata: | |
583 | bch2_btree_iter_next(&stats->iter); | |
584 | bch2_btree_iter_cond_resched(&stats->iter); | |
585 | } | |
c2fcff59 | 586 | out: |
1c6fdbd8 KO |
587 | bch2_btree_iter_unlock(&stats->iter); |
588 | ||
589 | move_ctxt_wait_event(&ctxt, list_empty(&ctxt.reads)); | |
590 | closure_sync(&ctxt.cl); | |
591 | ||
592 | EBUG_ON(atomic_read(&ctxt.write_sectors)); | |
593 | ||
594 | trace_move_data(c, | |
595 | atomic64_read(&stats->sectors_moved), | |
596 | atomic64_read(&stats->keys_moved)); | |
597 | ||
598 | return ret; | |
599 | } | |
600 | ||
601 | static int bch2_gc_data_replicas(struct bch_fs *c) | |
602 | { | |
603 | struct btree_iter iter; | |
604 | struct bkey_s_c k; | |
605 | int ret; | |
606 | ||
607 | mutex_lock(&c->replicas_gc_lock); | |
608 | bch2_replicas_gc_start(c, (1 << BCH_DATA_USER)|(1 << BCH_DATA_CACHED)); | |
609 | ||
610 | for_each_btree_key(&iter, c, BTREE_ID_EXTENTS, POS_MIN, | |
611 | BTREE_ITER_PREFETCH, k) { | |
26609b61 | 612 | ret = bch2_mark_bkey_replicas(c, k); |
1c6fdbd8 KO |
613 | if (ret) |
614 | break; | |
615 | } | |
616 | ret = bch2_btree_iter_unlock(&iter) ?: ret; | |
617 | ||
618 | bch2_replicas_gc_end(c, ret); | |
619 | mutex_unlock(&c->replicas_gc_lock); | |
620 | ||
621 | return ret; | |
622 | } | |
623 | ||
624 | static int bch2_gc_btree_replicas(struct bch_fs *c) | |
625 | { | |
626 | struct btree_iter iter; | |
627 | struct btree *b; | |
628 | unsigned id; | |
629 | int ret = 0; | |
630 | ||
631 | mutex_lock(&c->replicas_gc_lock); | |
632 | bch2_replicas_gc_start(c, 1 << BCH_DATA_BTREE); | |
633 | ||
634 | for (id = 0; id < BTREE_ID_NR; id++) { | |
635 | for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) { | |
26609b61 | 636 | ret = bch2_mark_bkey_replicas(c, bkey_i_to_s_c(&b->key)); |
1c6fdbd8 KO |
637 | |
638 | bch2_btree_iter_cond_resched(&iter); | |
639 | } | |
640 | ||
641 | ret = bch2_btree_iter_unlock(&iter) ?: ret; | |
642 | } | |
643 | ||
644 | bch2_replicas_gc_end(c, ret); | |
645 | mutex_unlock(&c->replicas_gc_lock); | |
646 | ||
647 | return ret; | |
648 | } | |
649 | ||
650 | static int bch2_move_btree(struct bch_fs *c, | |
651 | move_pred_fn pred, | |
652 | void *arg, | |
653 | struct bch_move_stats *stats) | |
654 | { | |
655 | struct bch_io_opts io_opts = bch2_opts_to_inode_opts(c->opts); | |
656 | struct btree *b; | |
657 | unsigned id; | |
658 | struct data_opts data_opts; | |
659 | enum data_cmd cmd; | |
660 | int ret = 0; | |
661 | ||
662 | stats->data_type = BCH_DATA_BTREE; | |
663 | ||
664 | for (id = 0; id < BTREE_ID_NR; id++) { | |
665 | for_each_btree_node(&stats->iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) { | |
26609b61 KO |
666 | switch ((cmd = pred(c, arg, |
667 | bkey_i_to_s_c(&b->key), | |
668 | &io_opts, &data_opts))) { | |
1c6fdbd8 KO |
669 | case DATA_SKIP: |
670 | goto next; | |
671 | case DATA_SCRUB: | |
672 | BUG(); | |
673 | case DATA_ADD_REPLICAS: | |
674 | case DATA_REWRITE: | |
675 | break; | |
676 | default: | |
677 | BUG(); | |
678 | } | |
679 | ||
680 | ret = bch2_btree_node_rewrite(c, &stats->iter, | |
681 | b->data->keys.seq, 0) ?: ret; | |
682 | next: | |
683 | bch2_btree_iter_cond_resched(&stats->iter); | |
684 | } | |
685 | ||
686 | ret = bch2_btree_iter_unlock(&stats->iter) ?: ret; | |
687 | } | |
688 | ||
689 | return ret; | |
690 | } | |
691 | ||
692 | #if 0 | |
693 | static enum data_cmd scrub_pred(struct bch_fs *c, void *arg, | |
26609b61 | 694 | struct bkey_s_c k, |
1c6fdbd8 KO |
695 | struct bch_io_opts *io_opts, |
696 | struct data_opts *data_opts) | |
697 | { | |
698 | return DATA_SCRUB; | |
699 | } | |
700 | #endif | |
701 | ||
702 | static enum data_cmd rereplicate_pred(struct bch_fs *c, void *arg, | |
26609b61 | 703 | struct bkey_s_c k, |
1c6fdbd8 KO |
704 | struct bch_io_opts *io_opts, |
705 | struct data_opts *data_opts) | |
706 | { | |
26609b61 KO |
707 | unsigned nr_good = bch2_bkey_durability(c, k); |
708 | unsigned replicas = 0; | |
709 | ||
710 | switch (k.k->type) { | |
711 | case KEY_TYPE_btree_ptr: | |
712 | replicas = c->opts.metadata_replicas; | |
713 | break; | |
714 | case KEY_TYPE_extent: | |
715 | replicas = io_opts->data_replicas; | |
716 | break; | |
717 | } | |
1c6fdbd8 KO |
718 | |
719 | if (!nr_good || nr_good >= replicas) | |
720 | return DATA_SKIP; | |
721 | ||
722 | data_opts->target = 0; | |
26609b61 | 723 | data_opts->btree_insert_flags = 0; |
1c6fdbd8 KO |
724 | return DATA_ADD_REPLICAS; |
725 | } | |
726 | ||
727 | static enum data_cmd migrate_pred(struct bch_fs *c, void *arg, | |
26609b61 | 728 | struct bkey_s_c k, |
1c6fdbd8 KO |
729 | struct bch_io_opts *io_opts, |
730 | struct data_opts *data_opts) | |
731 | { | |
732 | struct bch_ioctl_data *op = arg; | |
733 | ||
26609b61 | 734 | if (!bch2_bkey_has_device(k, op->migrate.dev)) |
1c6fdbd8 KO |
735 | return DATA_SKIP; |
736 | ||
737 | data_opts->target = 0; | |
738 | data_opts->btree_insert_flags = 0; | |
739 | data_opts->rewrite_dev = op->migrate.dev; | |
740 | return DATA_REWRITE; | |
741 | } | |
742 | ||
743 | int bch2_data_job(struct bch_fs *c, | |
744 | struct bch_move_stats *stats, | |
745 | struct bch_ioctl_data op) | |
746 | { | |
747 | int ret = 0; | |
748 | ||
749 | switch (op.op) { | |
750 | case BCH_DATA_OP_REREPLICATE: | |
751 | stats->data_type = BCH_DATA_JOURNAL; | |
752 | ret = bch2_journal_flush_device_pins(&c->journal, -1); | |
753 | ||
754 | ret = bch2_move_btree(c, rereplicate_pred, c, stats) ?: ret; | |
755 | ret = bch2_gc_btree_replicas(c) ?: ret; | |
756 | ||
757 | ret = bch2_move_data(c, NULL, | |
758 | writepoint_hashed((unsigned long) current), | |
759 | op.start, | |
760 | op.end, | |
761 | rereplicate_pred, c, stats) ?: ret; | |
762 | ret = bch2_gc_data_replicas(c) ?: ret; | |
763 | break; | |
764 | case BCH_DATA_OP_MIGRATE: | |
765 | if (op.migrate.dev >= c->sb.nr_devices) | |
766 | return -EINVAL; | |
767 | ||
768 | stats->data_type = BCH_DATA_JOURNAL; | |
769 | ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev); | |
770 | ||
771 | ret = bch2_move_btree(c, migrate_pred, &op, stats) ?: ret; | |
772 | ret = bch2_gc_btree_replicas(c) ?: ret; | |
773 | ||
774 | ret = bch2_move_data(c, NULL, | |
775 | writepoint_hashed((unsigned long) current), | |
776 | op.start, | |
777 | op.end, | |
778 | migrate_pred, &op, stats) ?: ret; | |
779 | ret = bch2_gc_data_replicas(c) ?: ret; | |
780 | break; | |
781 | default: | |
782 | ret = -EINVAL; | |
783 | } | |
784 | ||
785 | return ret; | |
786 | } |