]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob
36b5ec9
[thirdparty/kernel/stable.git] /
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 * Copyright (C) 2022 Christoph Hellwig.
5 */
6
7 #include <linux/bio.h>
8 #include "bio.h"
9 #include "ctree.h"
10 #include "volumes.h"
11 #include "raid56.h"
12 #include "async-thread.h"
13 #include "check-integrity.h"
14 #include "dev-replace.h"
15 #include "rcu-string.h"
16 #include "zoned.h"
17 #include "file-item.h"
18 #include "raid-stripe-tree.h"
19
20 static struct bio_set btrfs_bioset;
21 static struct bio_set btrfs_clone_bioset;
22 static struct bio_set btrfs_repair_bioset;
23 static mempool_t btrfs_failed_bio_pool;
24
25 struct btrfs_failed_bio {
26 struct btrfs_bio *bbio;
27 int num_copies;
28 atomic_t repair_count;
29 };
30
31 /* Is this a data path I/O that needs storage layer checksum and repair? */
32 static inline bool is_data_bbio(struct btrfs_bio *bbio)
33 {
34 return bbio->inode && is_data_inode(&bbio->inode->vfs_inode);
35 }
36
37 static bool bbio_has_ordered_extent(struct btrfs_bio *bbio)
38 {
39 return is_data_bbio(bbio) && btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE;
40 }
41
42 /*
43 * Initialize a btrfs_bio structure. This skips the embedded bio itself as it
44 * is already initialized by the block layer.
45 */
46 void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_fs_info *fs_info,
47 btrfs_bio_end_io_t end_io, void *private)
48 {
49 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
50 bbio->fs_info = fs_info;
51 bbio->end_io = end_io;
52 bbio->private = private;
53 atomic_set(&bbio->pending_ios, 1);
54 }
55
56 /*
57 * Allocate a btrfs_bio structure. The btrfs_bio is the main I/O container for
58 * btrfs, and is used for all I/O submitted through btrfs_submit_bio.
59 *
60 * Just like the underlying bio_alloc_bioset it will not fail as it is backed by
61 * a mempool.
62 */
63 struct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
64 struct btrfs_fs_info *fs_info,
65 btrfs_bio_end_io_t end_io, void *private)
66 {
67 struct btrfs_bio *bbio;
68 struct bio *bio;
69
70 bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset);
71 bbio = btrfs_bio(bio);
72 btrfs_bio_init(bbio, fs_info, end_io, private);
73 return bbio;
74 }
75
76 static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info,
77 struct btrfs_bio *orig_bbio,
78 u64 map_length, bool use_append)
79 {
80 struct btrfs_bio *bbio;
81 struct bio *bio;
82
83 if (use_append) {
84 unsigned int nr_segs;
85
86 bio = bio_split_rw(&orig_bbio->bio, &fs_info->limits, &nr_segs,
87 &btrfs_clone_bioset, map_length);
88 } else {
89 bio = bio_split(&orig_bbio->bio, map_length >> SECTOR_SHIFT,
90 GFP_NOFS, &btrfs_clone_bioset);
91 }
92 bbio = btrfs_bio(bio);
93 btrfs_bio_init(bbio, fs_info, NULL, orig_bbio);
94 bbio->inode = orig_bbio->inode;
95 bbio->file_offset = orig_bbio->file_offset;
96 orig_bbio->file_offset += map_length;
97 if (bbio_has_ordered_extent(bbio)) {
98 refcount_inc(&orig_bbio->ordered->refs);
99 bbio->ordered = orig_bbio->ordered;
100 }
101 atomic_inc(&orig_bbio->pending_ios);
102 return bbio;
103 }
104
105 /* Free a bio that was never submitted to the underlying device. */
106 static void btrfs_cleanup_bio(struct btrfs_bio *bbio)
107 {
108 if (bbio_has_ordered_extent(bbio))
109 btrfs_put_ordered_extent(bbio->ordered);
110 bio_put(&bbio->bio);
111 }
112
113 static void __btrfs_bio_end_io(struct btrfs_bio *bbio)
114 {
115 if (bbio_has_ordered_extent(bbio)) {
116 struct btrfs_ordered_extent *ordered = bbio->ordered;
117
118 bbio->end_io(bbio);
119 btrfs_put_ordered_extent(ordered);
120 } else {
121 bbio->end_io(bbio);
122 }
123 }
124
125 static void btrfs_orig_write_end_io(struct bio *bio);
126
127 static void btrfs_bbio_propagate_error(struct btrfs_bio *bbio,
128 struct btrfs_bio *orig_bbio)
129 {
130 /*
131 * For writes we tolerate nr_mirrors - 1 write failures, so we can't
132 * just blindly propagate a write failure here. Instead increment the
133 * error count in the original I/O context so that it is guaranteed to
134 * be larger than the error tolerance.
135 */
136 if (bbio->bio.bi_end_io == &btrfs_orig_write_end_io) {
137 struct btrfs_io_stripe *orig_stripe = orig_bbio->bio.bi_private;
138 struct btrfs_io_context *orig_bioc = orig_stripe->bioc;
139
140 atomic_add(orig_bioc->max_errors, &orig_bioc->error);
141 } else {
142 orig_bbio->bio.bi_status = bbio->bio.bi_status;
143 }
144 }
145
146 void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
147 {
148 bbio->bio.bi_status = status;
149 if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
150 struct btrfs_bio *orig_bbio = bbio->private;
151
152 if (bbio->bio.bi_status)
153 btrfs_bbio_propagate_error(bbio, orig_bbio);
154 btrfs_cleanup_bio(bbio);
155 bbio = orig_bbio;
156 }
157
158 if (atomic_dec_and_test(&bbio->pending_ios))
159 __btrfs_bio_end_io(bbio);
160 }
161
162 static int next_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
163 {
164 if (cur_mirror == fbio->num_copies)
165 return cur_mirror + 1 - fbio->num_copies;
166 return cur_mirror + 1;
167 }
168
169 static int prev_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
170 {
171 if (cur_mirror == 1)
172 return fbio->num_copies;
173 return cur_mirror - 1;
174 }
175
176 static void btrfs_repair_done(struct btrfs_failed_bio *fbio)
177 {
178 if (atomic_dec_and_test(&fbio->repair_count)) {
179 btrfs_bio_end_io(fbio->bbio, fbio->bbio->bio.bi_status);
180 mempool_free(fbio, &btrfs_failed_bio_pool);
181 }
182 }
183
184 static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio,
185 struct btrfs_device *dev)
186 {
187 struct btrfs_failed_bio *fbio = repair_bbio->private;
188 struct btrfs_inode *inode = repair_bbio->inode;
189 struct btrfs_fs_info *fs_info = inode->root->fs_info;
190 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);
191 int mirror = repair_bbio->mirror_num;
192
193 if (repair_bbio->bio.bi_status ||
194 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {
195 bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ);
196 repair_bbio->bio.bi_iter = repair_bbio->saved_iter;
197
198 mirror = next_repair_mirror(fbio, mirror);
199 if (mirror == fbio->bbio->mirror_num) {
200 btrfs_debug(fs_info, "no mirror left");
201 fbio->bbio->bio.bi_status = BLK_STS_IOERR;
202 goto done;
203 }
204
205 btrfs_submit_bio(repair_bbio, mirror);
206 return;
207 }
208
209 do {
210 mirror = prev_repair_mirror(fbio, mirror);
211 btrfs_repair_io_failure(fs_info, btrfs_ino(inode),
212 repair_bbio->file_offset, fs_info->sectorsize,
213 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT,
214 bv->bv_page, bv->bv_offset, mirror);
215 } while (mirror != fbio->bbio->mirror_num);
216
217 done:
218 btrfs_repair_done(fbio);
219 bio_put(&repair_bbio->bio);
220 }
221
222 /*
223 * Try to kick off a repair read to the next available mirror for a bad sector.
224 *
225 * This primarily tries to recover good data to serve the actual read request,
226 * but also tries to write the good data back to the bad mirror(s) when a
227 * read succeeded to restore the redundancy.
228 */
229 static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio,
230 u32 bio_offset,
231 struct bio_vec *bv,
232 struct btrfs_failed_bio *fbio)
233 {
234 struct btrfs_inode *inode = failed_bbio->inode;
235 struct btrfs_fs_info *fs_info = inode->root->fs_info;
236 const u32 sectorsize = fs_info->sectorsize;
237 const u64 logical = (failed_bbio->saved_iter.bi_sector << SECTOR_SHIFT);
238 struct btrfs_bio *repair_bbio;
239 struct bio *repair_bio;
240 int num_copies;
241 int mirror;
242
243 btrfs_debug(fs_info, "repair read error: read error at %llu",
244 failed_bbio->file_offset + bio_offset);
245
246 num_copies = btrfs_num_copies(fs_info, logical, sectorsize);
247 if (num_copies == 1) {
248 btrfs_debug(fs_info, "no copy to repair from");
249 failed_bbio->bio.bi_status = BLK_STS_IOERR;
250 return fbio;
251 }
252
253 if (!fbio) {
254 fbio = mempool_alloc(&btrfs_failed_bio_pool, GFP_NOFS);
255 fbio->bbio = failed_bbio;
256 fbio->num_copies = num_copies;
257 atomic_set(&fbio->repair_count, 1);
258 }
259
260 atomic_inc(&fbio->repair_count);
261
262 repair_bio = bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS,
263 &btrfs_repair_bioset);
264 repair_bio->bi_iter.bi_sector = failed_bbio->saved_iter.bi_sector;
265 __bio_add_page(repair_bio, bv->bv_page, bv->bv_len, bv->bv_offset);
266
267 repair_bbio = btrfs_bio(repair_bio);
268 btrfs_bio_init(repair_bbio, fs_info, NULL, fbio);
269 repair_bbio->inode = failed_bbio->inode;
270 repair_bbio->file_offset = failed_bbio->file_offset + bio_offset;
271
272 mirror = next_repair_mirror(fbio, failed_bbio->mirror_num);
273 btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror);
274 btrfs_submit_bio(repair_bbio, mirror);
275 return fbio;
276 }
277
278 static void btrfs_check_read_bio(struct btrfs_bio *bbio, struct btrfs_device *dev)
279 {
280 struct btrfs_inode *inode = bbio->inode;
281 struct btrfs_fs_info *fs_info = inode->root->fs_info;
282 u32 sectorsize = fs_info->sectorsize;
283 struct bvec_iter *iter = &bbio->saved_iter;
284 blk_status_t status = bbio->bio.bi_status;
285 struct btrfs_failed_bio *fbio = NULL;
286 u32 offset = 0;
287
288 /* Read-repair requires the inode field to be set by the submitter. */
289 ASSERT(inode);
290
291 /*
292 * Hand off repair bios to the repair code as there is no upper level
293 * submitter for them.
294 */
295 if (bbio->bio.bi_pool == &btrfs_repair_bioset) {
296 btrfs_end_repair_bio(bbio, dev);
297 return;
298 }
299
300 /* Clear the I/O error. A failed repair will reset it. */
301 bbio->bio.bi_status = BLK_STS_OK;
302
303 while (iter->bi_size) {
304 struct bio_vec bv = bio_iter_iovec(&bbio->bio, *iter);
305
306 bv.bv_len = min(bv.bv_len, sectorsize);
307 if (status || !btrfs_data_csum_ok(bbio, dev, offset, &bv))
308 fbio = repair_one_sector(bbio, offset, &bv, fbio);
309
310 bio_advance_iter_single(&bbio->bio, iter, sectorsize);
311 offset += sectorsize;
312 }
313
314 if (bbio->csum != bbio->csum_inline)
315 kfree(bbio->csum);
316
317 if (fbio)
318 btrfs_repair_done(fbio);
319 else
320 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
321 }
322
323 static void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev)
324 {
325 if (!dev || !dev->bdev)
326 return;
327 if (bio->bi_status != BLK_STS_IOERR && bio->bi_status != BLK_STS_TARGET)
328 return;
329
330 if (btrfs_op(bio) == BTRFS_MAP_WRITE)
331 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
332 else if (!(bio->bi_opf & REQ_RAHEAD))
333 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
334 if (bio->bi_opf & REQ_PREFLUSH)
335 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_FLUSH_ERRS);
336 }
337
338 static struct workqueue_struct *btrfs_end_io_wq(struct btrfs_fs_info *fs_info,
339 struct bio *bio)
340 {
341 if (bio->bi_opf & REQ_META)
342 return fs_info->endio_meta_workers;
343 return fs_info->endio_workers;
344 }
345
346 static void btrfs_end_bio_work(struct work_struct *work)
347 {
348 struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work);
349
350 /* Metadata reads are checked and repaired by the submitter. */
351 if (is_data_bbio(bbio))
352 btrfs_check_read_bio(bbio, bbio->bio.bi_private);
353 else
354 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
355 }
356
357 static void btrfs_simple_end_io(struct bio *bio)
358 {
359 struct btrfs_bio *bbio = btrfs_bio(bio);
360 struct btrfs_device *dev = bio->bi_private;
361 struct btrfs_fs_info *fs_info = bbio->fs_info;
362
363 btrfs_bio_counter_dec(fs_info);
364
365 if (bio->bi_status)
366 btrfs_log_dev_io_error(bio, dev);
367
368 if (bio_op(bio) == REQ_OP_READ) {
369 INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work);
370 queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
371 } else {
372 if (bio_op(bio) == REQ_OP_ZONE_APPEND && !bio->bi_status)
373 btrfs_record_physical_zoned(bbio);
374 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
375 }
376 }
377
378 static void btrfs_raid56_end_io(struct bio *bio)
379 {
380 struct btrfs_io_context *bioc = bio->bi_private;
381 struct btrfs_bio *bbio = btrfs_bio(bio);
382
383 btrfs_bio_counter_dec(bioc->fs_info);
384 bbio->mirror_num = bioc->mirror_num;
385 if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio))
386 btrfs_check_read_bio(bbio, NULL);
387 else
388 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
389
390 btrfs_put_bioc(bioc);
391 }
392
393 static void btrfs_orig_write_end_io(struct bio *bio)
394 {
395 struct btrfs_io_stripe *stripe = bio->bi_private;
396 struct btrfs_io_context *bioc = stripe->bioc;
397 struct btrfs_bio *bbio = btrfs_bio(bio);
398
399 btrfs_bio_counter_dec(bioc->fs_info);
400
401 if (bio->bi_status) {
402 atomic_inc(&bioc->error);
403 btrfs_log_dev_io_error(bio, stripe->dev);
404 }
405
406 /*
407 * Only send an error to the higher layers if it is beyond the tolerance
408 * threshold.
409 */
410 if (atomic_read(&bioc->error) > bioc->max_errors)
411 bio->bi_status = BLK_STS_IOERR;
412 else
413 bio->bi_status = BLK_STS_OK;
414
415 if (bio_op(bio) == REQ_OP_ZONE_APPEND && !bio->bi_status)
416 stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
417
418 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
419 btrfs_put_bioc(bioc);
420 }
421
422 static void btrfs_clone_write_end_io(struct bio *bio)
423 {
424 struct btrfs_io_stripe *stripe = bio->bi_private;
425
426 if (bio->bi_status) {
427 atomic_inc(&stripe->bioc->error);
428 btrfs_log_dev_io_error(bio, stripe->dev);
429 } else if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
430 stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
431 }
432
433 /* Pass on control to the original bio this one was cloned from */
434 bio_endio(stripe->bioc->orig_bio);
435 bio_put(bio);
436 }
437
438 static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio)
439 {
440 if (!dev || !dev->bdev ||
441 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
442 (btrfs_op(bio) == BTRFS_MAP_WRITE &&
443 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
444 bio_io_error(bio);
445 return;
446 }
447
448 bio_set_dev(bio, dev->bdev);
449
450 /*
451 * For zone append writing, bi_sector must point the beginning of the
452 * zone
453 */
454 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
455 u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
456 u64 zone_start = round_down(physical, dev->fs_info->zone_size);
457
458 ASSERT(btrfs_dev_is_sequential(dev, physical));
459 bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
460 }
461 btrfs_debug_in_rcu(dev->fs_info,
462 "%s: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
463 __func__, bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,
464 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),
465 dev->devid, bio->bi_iter.bi_size);
466
467 btrfsic_check_bio(bio);
468
469 if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT)
470 blkcg_punt_bio_submit(bio);
471 else
472 submit_bio(bio);
473 }
474
475 static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
476 {
477 struct bio *orig_bio = bioc->orig_bio, *bio;
478
479 ASSERT(bio_op(orig_bio) != REQ_OP_READ);
480
481 /* Reuse the bio embedded into the btrfs_bio for the last mirror */
482 if (dev_nr == bioc->num_stripes - 1) {
483 bio = orig_bio;
484 bio->bi_end_io = btrfs_orig_write_end_io;
485 } else {
486 bio = bio_alloc_clone(NULL, orig_bio, GFP_NOFS, &fs_bio_set);
487 bio_inc_remaining(orig_bio);
488 bio->bi_end_io = btrfs_clone_write_end_io;
489 }
490
491 bio->bi_private = &bioc->stripes[dev_nr];
492 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;
493 bioc->stripes[dev_nr].bioc = bioc;
494 bioc->size = bio->bi_iter.bi_size;
495 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
496 }
497
498 static void __btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc,
499 struct btrfs_io_stripe *smap, int mirror_num)
500 {
501 if (!bioc) {
502 /* Single mirror read/write fast path. */
503 btrfs_bio(bio)->mirror_num = mirror_num;
504 if (bio_op(bio) != REQ_OP_READ)
505 btrfs_bio(bio)->orig_physical = smap->physical;
506 bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;
507 if (bio_op(bio) != REQ_OP_READ)
508 btrfs_bio(bio)->orig_physical = smap->physical;
509 bio->bi_private = smap->dev;
510 bio->bi_end_io = btrfs_simple_end_io;
511 btrfs_submit_dev_bio(smap->dev, bio);
512 } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
513 /* Parity RAID write or read recovery. */
514 bio->bi_private = bioc;
515 bio->bi_end_io = btrfs_raid56_end_io;
516 if (bio_op(bio) == REQ_OP_READ)
517 raid56_parity_recover(bio, bioc, mirror_num);
518 else
519 raid56_parity_write(bio, bioc);
520 } else {
521 /* Write to multiple mirrors. */
522 int total_devs = bioc->num_stripes;
523
524 bioc->orig_bio = bio;
525 for (int dev_nr = 0; dev_nr < total_devs; dev_nr++)
526 btrfs_submit_mirrored_bio(bioc, dev_nr);
527 }
528 }
529
530 static blk_status_t btrfs_bio_csum(struct btrfs_bio *bbio)
531 {
532 if (bbio->bio.bi_opf & REQ_META)
533 return btree_csum_one_bio(bbio);
534 return btrfs_csum_one_bio(bbio);
535 }
536
537 /*
538 * Async submit bios are used to offload expensive checksumming onto the worker
539 * threads.
540 */
541 struct async_submit_bio {
542 struct btrfs_bio *bbio;
543 struct btrfs_io_context *bioc;
544 struct btrfs_io_stripe smap;
545 int mirror_num;
546 struct btrfs_work work;
547 };
548
549 /*
550 * In order to insert checksums into the metadata in large chunks, we wait
551 * until bio submission time. All the pages in the bio are checksummed and
552 * sums are attached onto the ordered extent record.
553 *
554 * At IO completion time the csums attached on the ordered extent record are
555 * inserted into the btree.
556 */
557 static void run_one_async_start(struct btrfs_work *work)
558 {
559 struct async_submit_bio *async =
560 container_of(work, struct async_submit_bio, work);
561 blk_status_t ret;
562
563 ret = btrfs_bio_csum(async->bbio);
564 if (ret)
565 async->bbio->bio.bi_status = ret;
566 }
567
568 /*
569 * In order to insert checksums into the metadata in large chunks, we wait
570 * until bio submission time. All the pages in the bio are checksummed and
571 * sums are attached onto the ordered extent record.
572 *
573 * At IO completion time the csums attached on the ordered extent record are
574 * inserted into the tree.
575 */
576 static void run_one_async_done(struct btrfs_work *work)
577 {
578 struct async_submit_bio *async =
579 container_of(work, struct async_submit_bio, work);
580 struct bio *bio = &async->bbio->bio;
581
582 /* If an error occurred we just want to clean up the bio and move on. */
583 if (bio->bi_status) {
584 btrfs_bio_end_io(async->bbio, async->bbio->bio.bi_status);
585 return;
586 }
587
588 /*
589 * All of the bios that pass through here are from async helpers.
590 * Use REQ_BTRFS_CGROUP_PUNT to issue them from the owning cgroup's
591 * context. This changes nothing when cgroups aren't in use.
592 */
593 bio->bi_opf |= REQ_BTRFS_CGROUP_PUNT;
594 __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);
595 }
596
597 static void run_one_async_free(struct btrfs_work *work)
598 {
599 kfree(container_of(work, struct async_submit_bio, work));
600 }
601
602 static bool should_async_write(struct btrfs_bio *bbio)
603 {
604 /* Submit synchronously if the checksum implementation is fast. */
605 if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &bbio->fs_info->flags))
606 return false;
607
608 /*
609 * Try to defer the submission to a workqueue to parallelize the
610 * checksum calculation unless the I/O is issued synchronously.
611 */
612 if (op_is_sync(bbio->bio.bi_opf))
613 return false;
614
615 /* Zoned devices require I/O to be submitted in order. */
616 if ((bbio->bio.bi_opf & REQ_META) && btrfs_is_zoned(bbio->fs_info))
617 return false;
618
619 return true;
620 }
621
622 /*
623 * Submit bio to an async queue.
624 *
625 * Return true if the work has been succesfuly submitted, else false.
626 */
627 static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio,
628 struct btrfs_io_context *bioc,
629 struct btrfs_io_stripe *smap, int mirror_num)
630 {
631 struct btrfs_fs_info *fs_info = bbio->fs_info;
632 struct async_submit_bio *async;
633
634 async = kmalloc(sizeof(*async), GFP_NOFS);
635 if (!async)
636 return false;
637
638 async->bbio = bbio;
639 async->bioc = bioc;
640 async->smap = *smap;
641 async->mirror_num = mirror_num;
642
643 btrfs_init_work(&async->work, run_one_async_start, run_one_async_done,
644 run_one_async_free);
645 btrfs_queue_work(fs_info->workers, &async->work);
646 return true;
647 }
648
649 static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
650 {
651 struct btrfs_inode *inode = bbio->inode;
652 struct btrfs_fs_info *fs_info = bbio->fs_info;
653 struct bio *bio = &bbio->bio;
654 u64 logical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
655 u64 length = bio->bi_iter.bi_size;
656 u64 map_length = length;
657 bool use_append = btrfs_use_zone_append(bbio);
658 struct btrfs_io_context *bioc = NULL;
659 struct btrfs_io_stripe smap;
660 blk_status_t ret;
661 int error;
662
663 btrfs_bio_counter_inc_blocked(fs_info);
664 error = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
665 &bioc, &smap, &mirror_num, 1);
666 if (error) {
667 ret = errno_to_blk_status(error);
668 goto fail;
669 }
670
671 map_length = min(map_length, length);
672 if (use_append)
673 map_length = min(map_length, fs_info->max_zone_append_size);
674
675 if (map_length < length) {
676 bbio = btrfs_split_bio(fs_info, bbio, map_length, use_append);
677 bio = &bbio->bio;
678 }
679
680 /*
681 * Save the iter for the end_io handler and preload the checksums for
682 * data reads.
683 */
684 if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio)) {
685 bbio->saved_iter = bio->bi_iter;
686 ret = btrfs_lookup_bio_sums(bbio);
687 if (ret)
688 goto fail;
689 }
690
691 if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
692 if (use_append) {
693 bio->bi_opf &= ~REQ_OP_WRITE;
694 bio->bi_opf |= REQ_OP_ZONE_APPEND;
695 }
696
697 if (is_data_bbio(bbio) && bioc &&
698 btrfs_need_stripe_tree_update(bioc->fs_info, bioc->map_type)) {
699 /*
700 * No locking for the list update, as we only add to
701 * the list in the I/O submission path, and list
702 * iteration only happens in the completion path, which
703 * can't happen until after the last submission.
704 */
705 btrfs_get_bioc(bioc);
706 list_add_tail(&bioc->rst_ordered_entry, &bbio->ordered->bioc_list);
707 }
708
709 /*
710 * Csum items for reloc roots have already been cloned at this
711 * point, so they are handled as part of the no-checksum case.
712 */
713 if (inode && !(inode->flags & BTRFS_INODE_NODATASUM) &&
714 !test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state) &&
715 !btrfs_is_data_reloc_root(inode->root)) {
716 if (should_async_write(bbio) &&
717 btrfs_wq_submit_bio(bbio, bioc, &smap, mirror_num))
718 goto done;
719
720 ret = btrfs_bio_csum(bbio);
721 if (ret)
722 goto fail;
723 } else if (use_append ||
724 (btrfs_is_zoned(fs_info) && inode &&
725 inode->flags & BTRFS_INODE_NODATASUM)) {
726 ret = btrfs_alloc_dummy_sum(bbio);
727 if (ret)
728 goto fail;
729 }
730 }
731
732 __btrfs_submit_bio(bio, bioc, &smap, mirror_num);
733 done:
734 return map_length == length;
735
736 fail:
737 btrfs_bio_counter_dec(fs_info);
738 /*
739 * We have split the original bbio, now we have to end both the current
740 * @bbio and remaining one, as the remaining one will never be submitted.
741 */
742 if (map_length < length) {
743 struct btrfs_bio *remaining = bbio->private;
744
745 ASSERT(bbio->bio.bi_pool == &btrfs_clone_bioset);
746 ASSERT(remaining);
747
748 btrfs_bio_end_io(remaining, ret);
749 }
750 btrfs_bio_end_io(bbio, ret);
751 /* Do not submit another chunk */
752 return true;
753 }
754
755 void btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num)
756 {
757 /* If bbio->inode is not populated, its file_offset must be 0. */
758 ASSERT(bbio->inode || bbio->file_offset == 0);
759
760 while (!btrfs_submit_chunk(bbio, mirror_num))
761 ;
762 }
763
764 /*
765 * Submit a repair write.
766 *
767 * This bypasses btrfs_submit_bio deliberately, as that writes all copies in a
768 * RAID setup. Here we only want to write the one bad copy, so we do the
769 * mapping ourselves and submit the bio directly.
770 *
771 * The I/O is issued synchronously to block the repair read completion from
772 * freeing the bio.
773 */
774 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
775 u64 length, u64 logical, struct page *page,
776 unsigned int pg_offset, int mirror_num)
777 {
778 struct btrfs_io_stripe smap = { 0 };
779 struct bio_vec bvec;
780 struct bio bio;
781 int ret = 0;
782
783 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
784 BUG_ON(!mirror_num);
785
786 if (btrfs_repair_one_zone(fs_info, logical))
787 return 0;
788
789 /*
790 * Avoid races with device replace and make sure our bioc has devices
791 * associated to its stripes that don't go away while we are doing the
792 * read repair operation.
793 */
794 btrfs_bio_counter_inc_blocked(fs_info);
795 ret = btrfs_map_repair_block(fs_info, &smap, logical, length, mirror_num);
796 if (ret < 0)
797 goto out_counter_dec;
798
799 if (!smap.dev->bdev ||
800 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &smap.dev->dev_state)) {
801 ret = -EIO;
802 goto out_counter_dec;
803 }
804
805 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
806 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
807 __bio_add_page(&bio, page, length, pg_offset);
808
809 btrfsic_check_bio(&bio);
810 ret = submit_bio_wait(&bio);
811 if (ret) {
812 /* try to remap that extent elsewhere? */
813 btrfs_dev_stat_inc_and_print(smap.dev, BTRFS_DEV_STAT_WRITE_ERRS);
814 goto out_bio_uninit;
815 }
816
817 btrfs_info_rl_in_rcu(fs_info,
818 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
819 ino, start, btrfs_dev_name(smap.dev),
820 smap.physical >> SECTOR_SHIFT);
821 ret = 0;
822
823 out_bio_uninit:
824 bio_uninit(&bio);
825 out_counter_dec:
826 btrfs_bio_counter_dec(fs_info);
827 return ret;
828 }
829
830 /*
831 * Submit a btrfs_bio based repair write.
832 *
833 * If @dev_replace is true, the write would be submitted to dev-replace target.
834 */
835 void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace)
836 {
837 struct btrfs_fs_info *fs_info = bbio->fs_info;
838 u64 logical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
839 u64 length = bbio->bio.bi_iter.bi_size;
840 struct btrfs_io_stripe smap = { 0 };
841 int ret;
842
843 ASSERT(fs_info);
844 ASSERT(mirror_num > 0);
845 ASSERT(btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE);
846 ASSERT(!bbio->inode);
847
848 btrfs_bio_counter_inc_blocked(fs_info);
849 ret = btrfs_map_repair_block(fs_info, &smap, logical, length, mirror_num);
850 if (ret < 0)
851 goto fail;
852
853 if (dev_replace) {
854 ASSERT(smap.dev == fs_info->dev_replace.srcdev);
855 smap.dev = fs_info->dev_replace.tgtdev;
856 }
857 __btrfs_submit_bio(&bbio->bio, NULL, &smap, mirror_num);
858 return;
859
860 fail:
861 btrfs_bio_counter_dec(fs_info);
862 btrfs_bio_end_io(bbio, errno_to_blk_status(ret));
863 }
864
865 int __init btrfs_bioset_init(void)
866 {
867 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
868 offsetof(struct btrfs_bio, bio),
869 BIOSET_NEED_BVECS))
870 return -ENOMEM;
871 if (bioset_init(&btrfs_clone_bioset, BIO_POOL_SIZE,
872 offsetof(struct btrfs_bio, bio), 0))
873 goto out_free_bioset;
874 if (bioset_init(&btrfs_repair_bioset, BIO_POOL_SIZE,
875 offsetof(struct btrfs_bio, bio),
876 BIOSET_NEED_BVECS))
877 goto out_free_clone_bioset;
878 if (mempool_init_kmalloc_pool(&btrfs_failed_bio_pool, BIO_POOL_SIZE,
879 sizeof(struct btrfs_failed_bio)))
880 goto out_free_repair_bioset;
881 return 0;
882
883 out_free_repair_bioset:
884 bioset_exit(&btrfs_repair_bioset);
885 out_free_clone_bioset:
886 bioset_exit(&btrfs_clone_bioset);
887 out_free_bioset:
888 bioset_exit(&btrfs_bioset);
889 return -ENOMEM;
890 }
891
892 void __cold btrfs_bioset_exit(void)
893 {
894 mempool_exit(&btrfs_failed_bio_pool);
895 bioset_exit(&btrfs_repair_bioset);
896 bioset_exit(&btrfs_clone_bioset);
897 bioset_exit(&btrfs_bioset);
898 }