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