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