1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 * Copyright (C) 2022 Christoph Hellwig.
12 #include "async-thread.h"
13 #include "check-integrity.h"
14 #include "dev-replace.h"
15 #include "rcu-string.h"
17 #include "file-item.h"
19 static struct bio_set btrfs_bioset;
20 static struct bio_set btrfs_clone_bioset;
21 static struct bio_set btrfs_repair_bioset;
22 static mempool_t btrfs_failed_bio_pool;
24 struct btrfs_failed_bio {
25 struct btrfs_bio *bbio;
27 atomic_t repair_count;
31 * Initialize a btrfs_bio structure. This skips the embedded bio itself as it
32 * is already initialized by the block layer.
34 void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_inode *inode,
35 btrfs_bio_end_io_t end_io, void *private)
37 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
39 bbio->end_io = end_io;
40 bbio->private = private;
41 atomic_set(&bbio->pending_ios, 1);
45 * Allocate a btrfs_bio structure. The btrfs_bio is the main I/O container for
46 * btrfs, and is used for all I/O submitted through btrfs_submit_bio.
48 * Just like the underlying bio_alloc_bioset it will not fail as it is backed by
51 struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
52 struct btrfs_inode *inode,
53 btrfs_bio_end_io_t end_io, void *private)
57 bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset);
58 btrfs_bio_init(btrfs_bio(bio), inode, end_io, private);
62 static struct bio *btrfs_split_bio(struct btrfs_fs_info *fs_info,
63 struct bio *orig, u64 map_length,
66 struct btrfs_bio *orig_bbio = btrfs_bio(orig);
72 bio = bio_split_rw(orig, &fs_info->limits, &nr_segs,
73 &btrfs_clone_bioset, map_length);
75 bio = bio_split(orig, map_length >> SECTOR_SHIFT, GFP_NOFS,
78 btrfs_bio_init(btrfs_bio(bio), orig_bbio->inode, NULL, orig_bbio);
80 btrfs_bio(bio)->file_offset = orig_bbio->file_offset;
81 if (!(orig->bi_opf & REQ_BTRFS_ONE_ORDERED))
82 orig_bbio->file_offset += map_length;
84 atomic_inc(&orig_bbio->pending_ios);
88 static void btrfs_orig_write_end_io(struct bio *bio);
90 static void btrfs_bbio_propagate_error(struct btrfs_bio *bbio,
91 struct btrfs_bio *orig_bbio)
94 * For writes we tolerate nr_mirrors - 1 write failures, so we can't
95 * just blindly propagate a write failure here. Instead increment the
96 * error count in the original I/O context so that it is guaranteed to
97 * be larger than the error tolerance.
99 if (bbio->bio.bi_end_io == &btrfs_orig_write_end_io) {
100 struct btrfs_io_stripe *orig_stripe = orig_bbio->bio.bi_private;
101 struct btrfs_io_context *orig_bioc = orig_stripe->bioc;
103 atomic_add(orig_bioc->max_errors, &orig_bioc->error);
105 orig_bbio->bio.bi_status = bbio->bio.bi_status;
109 static void btrfs_orig_bbio_end_io(struct btrfs_bio *bbio)
111 if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
112 struct btrfs_bio *orig_bbio = bbio->private;
114 if (bbio->bio.bi_status)
115 btrfs_bbio_propagate_error(bbio, orig_bbio);
120 if (atomic_dec_and_test(&bbio->pending_ios))
124 static int next_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
126 if (cur_mirror == fbio->num_copies)
127 return cur_mirror + 1 - fbio->num_copies;
128 return cur_mirror + 1;
131 static int prev_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
134 return fbio->num_copies;
135 return cur_mirror - 1;
138 static void btrfs_repair_done(struct btrfs_failed_bio *fbio)
140 if (atomic_dec_and_test(&fbio->repair_count)) {
141 btrfs_orig_bbio_end_io(fbio->bbio);
142 mempool_free(fbio, &btrfs_failed_bio_pool);
146 static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio,
147 struct btrfs_device *dev)
149 struct btrfs_failed_bio *fbio = repair_bbio->private;
150 struct btrfs_inode *inode = repair_bbio->inode;
151 struct btrfs_fs_info *fs_info = inode->root->fs_info;
152 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);
153 int mirror = repair_bbio->mirror_num;
155 if (repair_bbio->bio.bi_status ||
156 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {
157 bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ);
158 repair_bbio->bio.bi_iter = repair_bbio->saved_iter;
160 mirror = next_repair_mirror(fbio, mirror);
161 if (mirror == fbio->bbio->mirror_num) {
162 btrfs_debug(fs_info, "no mirror left");
163 fbio->bbio->bio.bi_status = BLK_STS_IOERR;
167 btrfs_submit_bio(&repair_bbio->bio, mirror);
172 mirror = prev_repair_mirror(fbio, mirror);
173 btrfs_repair_io_failure(fs_info, btrfs_ino(inode),
174 repair_bbio->file_offset, fs_info->sectorsize,
175 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT,
176 bv->bv_page, bv->bv_offset, mirror);
177 } while (mirror != fbio->bbio->mirror_num);
180 btrfs_repair_done(fbio);
181 bio_put(&repair_bbio->bio);
185 * Try to kick off a repair read to the next available mirror for a bad sector.
187 * This primarily tries to recover good data to serve the actual read request,
188 * but also tries to write the good data back to the bad mirror(s) when a
189 * read succeeded to restore the redundancy.
191 static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio,
194 struct btrfs_failed_bio *fbio)
196 struct btrfs_inode *inode = failed_bbio->inode;
197 struct btrfs_fs_info *fs_info = inode->root->fs_info;
198 const u32 sectorsize = fs_info->sectorsize;
199 const u64 logical = (failed_bbio->saved_iter.bi_sector << SECTOR_SHIFT);
200 struct btrfs_bio *repair_bbio;
201 struct bio *repair_bio;
205 btrfs_debug(fs_info, "repair read error: read error at %llu",
206 failed_bbio->file_offset + bio_offset);
208 num_copies = btrfs_num_copies(fs_info, logical, sectorsize);
209 if (num_copies == 1) {
210 btrfs_debug(fs_info, "no copy to repair from");
211 failed_bbio->bio.bi_status = BLK_STS_IOERR;
216 fbio = mempool_alloc(&btrfs_failed_bio_pool, GFP_NOFS);
217 fbio->bbio = failed_bbio;
218 fbio->num_copies = num_copies;
219 atomic_set(&fbio->repair_count, 1);
222 atomic_inc(&fbio->repair_count);
224 repair_bio = bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS,
225 &btrfs_repair_bioset);
226 repair_bio->bi_iter.bi_sector = failed_bbio->saved_iter.bi_sector;
227 bio_add_page(repair_bio, bv->bv_page, bv->bv_len, bv->bv_offset);
229 repair_bbio = btrfs_bio(repair_bio);
230 btrfs_bio_init(repair_bbio, failed_bbio->inode, NULL, fbio);
231 repair_bbio->file_offset = failed_bbio->file_offset + bio_offset;
233 mirror = next_repair_mirror(fbio, failed_bbio->mirror_num);
234 btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror);
235 btrfs_submit_bio(repair_bio, mirror);
239 static void btrfs_check_read_bio(struct btrfs_bio *bbio, struct btrfs_device *dev)
241 struct btrfs_inode *inode = bbio->inode;
242 struct btrfs_fs_info *fs_info = inode->root->fs_info;
243 u32 sectorsize = fs_info->sectorsize;
244 struct bvec_iter *iter = &bbio->saved_iter;
245 blk_status_t status = bbio->bio.bi_status;
246 struct btrfs_failed_bio *fbio = NULL;
250 * Hand off repair bios to the repair code as there is no upper level
251 * submitter for them.
253 if (bbio->bio.bi_pool == &btrfs_repair_bioset) {
254 btrfs_end_repair_bio(bbio, dev);
258 /* Clear the I/O error. A failed repair will reset it. */
259 bbio->bio.bi_status = BLK_STS_OK;
261 while (iter->bi_size) {
262 struct bio_vec bv = bio_iter_iovec(&bbio->bio, *iter);
264 bv.bv_len = min(bv.bv_len, sectorsize);
265 if (status || !btrfs_data_csum_ok(bbio, dev, offset, &bv))
266 fbio = repair_one_sector(bbio, offset, &bv, fbio);
268 bio_advance_iter_single(&bbio->bio, iter, sectorsize);
269 offset += sectorsize;
272 if (bbio->csum != bbio->csum_inline)
276 btrfs_repair_done(fbio);
278 btrfs_orig_bbio_end_io(bbio);
281 static void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev)
283 if (!dev || !dev->bdev)
285 if (bio->bi_status != BLK_STS_IOERR && bio->bi_status != BLK_STS_TARGET)
288 if (btrfs_op(bio) == BTRFS_MAP_WRITE)
289 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
290 if (!(bio->bi_opf & REQ_RAHEAD))
291 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
292 if (bio->bi_opf & REQ_PREFLUSH)
293 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_FLUSH_ERRS);
296 static struct workqueue_struct *btrfs_end_io_wq(struct btrfs_fs_info *fs_info,
299 if (bio->bi_opf & REQ_META)
300 return fs_info->endio_meta_workers;
301 return fs_info->endio_workers;
304 static void btrfs_end_bio_work(struct work_struct *work)
306 struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work);
308 /* Metadata reads are checked and repaired by the submitter. */
309 if (bbio->bio.bi_opf & REQ_META)
312 btrfs_check_read_bio(bbio, bbio->bio.bi_private);
315 static void btrfs_simple_end_io(struct bio *bio)
317 struct btrfs_bio *bbio = btrfs_bio(bio);
318 struct btrfs_device *dev = bio->bi_private;
319 struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
321 btrfs_bio_counter_dec(fs_info);
324 btrfs_log_dev_io_error(bio, dev);
326 if (bio_op(bio) == REQ_OP_READ) {
327 INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work);
328 queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
330 if (bio_op(bio) == REQ_OP_ZONE_APPEND)
331 btrfs_record_physical_zoned(bbio);
332 btrfs_orig_bbio_end_io(bbio);
336 static void btrfs_raid56_end_io(struct bio *bio)
338 struct btrfs_io_context *bioc = bio->bi_private;
339 struct btrfs_bio *bbio = btrfs_bio(bio);
341 btrfs_bio_counter_dec(bioc->fs_info);
342 bbio->mirror_num = bioc->mirror_num;
343 if (bio_op(bio) == REQ_OP_READ && !(bbio->bio.bi_opf & REQ_META))
344 btrfs_check_read_bio(bbio, NULL);
346 btrfs_orig_bbio_end_io(bbio);
348 btrfs_put_bioc(bioc);
351 static void btrfs_orig_write_end_io(struct bio *bio)
353 struct btrfs_io_stripe *stripe = bio->bi_private;
354 struct btrfs_io_context *bioc = stripe->bioc;
355 struct btrfs_bio *bbio = btrfs_bio(bio);
357 btrfs_bio_counter_dec(bioc->fs_info);
359 if (bio->bi_status) {
360 atomic_inc(&bioc->error);
361 btrfs_log_dev_io_error(bio, stripe->dev);
365 * Only send an error to the higher layers if it is beyond the tolerance
368 if (atomic_read(&bioc->error) > bioc->max_errors)
369 bio->bi_status = BLK_STS_IOERR;
371 bio->bi_status = BLK_STS_OK;
373 btrfs_orig_bbio_end_io(bbio);
374 btrfs_put_bioc(bioc);
377 static void btrfs_clone_write_end_io(struct bio *bio)
379 struct btrfs_io_stripe *stripe = bio->bi_private;
381 if (bio->bi_status) {
382 atomic_inc(&stripe->bioc->error);
383 btrfs_log_dev_io_error(bio, stripe->dev);
386 /* Pass on control to the original bio this one was cloned from */
387 bio_endio(stripe->bioc->orig_bio);
391 static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio)
393 if (!dev || !dev->bdev ||
394 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
395 (btrfs_op(bio) == BTRFS_MAP_WRITE &&
396 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
401 bio_set_dev(bio, dev->bdev);
404 * For zone append writing, bi_sector must point the beginning of the
407 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
408 u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
409 u64 zone_start = round_down(physical, dev->fs_info->zone_size);
411 ASSERT(btrfs_dev_is_sequential(dev, physical));
412 bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
414 btrfs_debug_in_rcu(dev->fs_info,
415 "%s: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
416 __func__, bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,
417 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),
418 dev->devid, bio->bi_iter.bi_size);
420 btrfsic_check_bio(bio);
424 static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
426 struct bio *orig_bio = bioc->orig_bio, *bio;
428 ASSERT(bio_op(orig_bio) != REQ_OP_READ);
430 /* Reuse the bio embedded into the btrfs_bio for the last mirror */
431 if (dev_nr == bioc->num_stripes - 1) {
433 bio->bi_end_io = btrfs_orig_write_end_io;
435 bio = bio_alloc_clone(NULL, orig_bio, GFP_NOFS, &fs_bio_set);
436 bio_inc_remaining(orig_bio);
437 bio->bi_end_io = btrfs_clone_write_end_io;
440 bio->bi_private = &bioc->stripes[dev_nr];
441 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;
442 bioc->stripes[dev_nr].bioc = bioc;
443 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
446 static void __btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc,
447 struct btrfs_io_stripe *smap, int mirror_num)
449 /* Do not leak our private flag into the block layer. */
450 bio->bi_opf &= ~REQ_BTRFS_ONE_ORDERED;
453 /* Single mirror read/write fast path. */
454 btrfs_bio(bio)->mirror_num = mirror_num;
455 bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;
456 bio->bi_private = smap->dev;
457 bio->bi_end_io = btrfs_simple_end_io;
458 btrfs_submit_dev_bio(smap->dev, bio);
459 } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
460 /* Parity RAID write or read recovery. */
461 bio->bi_private = bioc;
462 bio->bi_end_io = btrfs_raid56_end_io;
463 if (bio_op(bio) == REQ_OP_READ)
464 raid56_parity_recover(bio, bioc, mirror_num);
466 raid56_parity_write(bio, bioc);
468 /* Write to multiple mirrors. */
469 int total_devs = bioc->num_stripes;
471 bioc->orig_bio = bio;
472 for (int dev_nr = 0; dev_nr < total_devs; dev_nr++)
473 btrfs_submit_mirrored_bio(bioc, dev_nr);
477 static blk_status_t btrfs_bio_csum(struct btrfs_bio *bbio)
479 if (bbio->bio.bi_opf & REQ_META)
480 return btree_csum_one_bio(bbio);
481 return btrfs_csum_one_bio(bbio);
485 * Async submit bios are used to offload expensive checksumming onto the worker
488 struct async_submit_bio {
489 struct btrfs_bio *bbio;
490 struct btrfs_io_context *bioc;
491 struct btrfs_io_stripe smap;
493 struct btrfs_work work;
497 * In order to insert checksums into the metadata in large chunks, we wait
498 * until bio submission time. All the pages in the bio are checksummed and
499 * sums are attached onto the ordered extent record.
501 * At IO completion time the csums attached on the ordered extent record are
502 * inserted into the btree.
504 static void run_one_async_start(struct btrfs_work *work)
506 struct async_submit_bio *async =
507 container_of(work, struct async_submit_bio, work);
510 ret = btrfs_bio_csum(async->bbio);
512 async->bbio->bio.bi_status = ret;
516 * In order to insert checksums into the metadata in large chunks, we wait
517 * until bio submission time. All the pages in the bio are checksummed and
518 * sums are attached onto the ordered extent record.
520 * At IO completion time the csums attached on the ordered extent record are
521 * inserted into the tree.
523 static void run_one_async_done(struct btrfs_work *work)
525 struct async_submit_bio *async =
526 container_of(work, struct async_submit_bio, work);
527 struct bio *bio = &async->bbio->bio;
529 /* If an error occurred we just want to clean up the bio and move on. */
530 if (bio->bi_status) {
531 btrfs_orig_bbio_end_io(async->bbio);
536 * All of the bios that pass through here are from async helpers.
537 * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context.
538 * This changes nothing when cgroups aren't in use.
540 bio->bi_opf |= REQ_CGROUP_PUNT;
541 __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);
544 static void run_one_async_free(struct btrfs_work *work)
546 kfree(container_of(work, struct async_submit_bio, work));
549 static bool should_async_write(struct btrfs_bio *bbio)
552 * If the I/O is not issued by fsync and friends, (->sync_writers != 0),
553 * then try to defer the submission to a workqueue to parallelize the
554 * checksum calculation.
556 if (atomic_read(&bbio->inode->sync_writers))
560 * Submit metadata writes synchronously if the checksum implementation
561 * is fast, or we are on a zoned device that wants I/O to be submitted
564 if (bbio->bio.bi_opf & REQ_META) {
565 struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
567 if (btrfs_is_zoned(fs_info))
569 if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags))
577 * Submit bio to an async queue.
579 * Return true if the work has been succesfuly submitted, else false.
581 static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio,
582 struct btrfs_io_context *bioc,
583 struct btrfs_io_stripe *smap, int mirror_num)
585 struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
586 struct async_submit_bio *async;
588 async = kmalloc(sizeof(*async), GFP_NOFS);
595 async->mirror_num = mirror_num;
597 btrfs_init_work(&async->work, run_one_async_start, run_one_async_done,
599 if (op_is_sync(bbio->bio.bi_opf))
600 btrfs_queue_work(fs_info->hipri_workers, &async->work);
602 btrfs_queue_work(fs_info->workers, &async->work);
606 static bool btrfs_submit_chunk(struct bio *bio, int mirror_num)
608 struct btrfs_bio *bbio = btrfs_bio(bio);
609 struct btrfs_inode *inode = bbio->inode;
610 struct btrfs_fs_info *fs_info = inode->root->fs_info;
611 struct btrfs_bio *orig_bbio = bbio;
612 u64 logical = bio->bi_iter.bi_sector << 9;
613 u64 length = bio->bi_iter.bi_size;
614 u64 map_length = length;
615 bool use_append = btrfs_use_zone_append(bbio);
616 struct btrfs_io_context *bioc = NULL;
617 struct btrfs_io_stripe smap;
621 btrfs_bio_counter_inc_blocked(fs_info);
622 error = __btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
623 &bioc, &smap, &mirror_num, 1);
625 ret = errno_to_blk_status(error);
629 map_length = min(map_length, length);
631 map_length = min(map_length, fs_info->max_zone_append_size);
633 if (map_length < length) {
634 bio = btrfs_split_bio(fs_info, bio, map_length, use_append);
635 bbio = btrfs_bio(bio);
639 * Save the iter for the end_io handler and preload the checksums for
642 if (bio_op(bio) == REQ_OP_READ && !(bio->bi_opf & REQ_META)) {
643 bbio->saved_iter = bio->bi_iter;
644 ret = btrfs_lookup_bio_sums(bbio);
649 if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
651 bio->bi_opf &= ~REQ_OP_WRITE;
652 bio->bi_opf |= REQ_OP_ZONE_APPEND;
653 ret = btrfs_extract_ordered_extent(btrfs_bio(bio));
659 * Csum items for reloc roots have already been cloned at this
660 * point, so they are handled as part of the no-checksum case.
662 if (!(inode->flags & BTRFS_INODE_NODATASUM) &&
663 !test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state) &&
664 !btrfs_is_data_reloc_root(inode->root)) {
665 if (should_async_write(bbio) &&
666 btrfs_wq_submit_bio(bbio, bioc, &smap, mirror_num))
669 ret = btrfs_bio_csum(bbio);
675 __btrfs_submit_bio(bio, bioc, &smap, mirror_num);
677 return map_length == length;
680 if (map_length < length)
683 btrfs_bio_counter_dec(fs_info);
684 btrfs_bio_end_io(orig_bbio, ret);
685 /* Do not submit another chunk */
689 void btrfs_submit_bio(struct bio *bio, int mirror_num)
691 while (!btrfs_submit_chunk(bio, mirror_num))
696 * Submit a repair write.
698 * This bypasses btrfs_submit_bio deliberately, as that writes all copies in a
699 * RAID setup. Here we only want to write the one bad copy, so we do the
700 * mapping ourselves and submit the bio directly.
702 * The I/O is issued synchronously to block the repair read completion from
705 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
706 u64 length, u64 logical, struct page *page,
707 unsigned int pg_offset, int mirror_num)
709 struct btrfs_device *dev;
714 struct btrfs_io_context *bioc = NULL;
717 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
720 if (btrfs_repair_one_zone(fs_info, logical))
726 * Avoid races with device replace and make sure our bioc has devices
727 * associated to its stripes that don't go away while we are doing the
728 * read repair operation.
730 btrfs_bio_counter_inc_blocked(fs_info);
731 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
733 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
734 * to update all raid stripes, but here we just want to correct
735 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
736 * stripe's dev and sector.
738 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
739 &map_length, &bioc, 0);
741 goto out_counter_dec;
742 ASSERT(bioc->mirror_num == 1);
744 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
745 &map_length, &bioc, mirror_num);
747 goto out_counter_dec;
749 * This happens when dev-replace is also running, and the
750 * mirror_num indicates the dev-replace target.
752 * In this case, we don't need to do anything, as the read
753 * error just means the replace progress hasn't reached our
754 * read range, and later replace routine would handle it well.
756 if (mirror_num != bioc->mirror_num)
757 goto out_counter_dec;
760 sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9;
761 dev = bioc->stripes[bioc->mirror_num - 1].dev;
762 btrfs_put_bioc(bioc);
764 if (!dev || !dev->bdev ||
765 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
767 goto out_counter_dec;
770 bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
771 bio.bi_iter.bi_sector = sector;
772 __bio_add_page(&bio, page, length, pg_offset);
774 btrfsic_check_bio(&bio);
775 ret = submit_bio_wait(&bio);
777 /* try to remap that extent elsewhere? */
778 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
782 btrfs_info_rl_in_rcu(fs_info,
783 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
784 ino, start, btrfs_dev_name(dev), sector);
790 btrfs_bio_counter_dec(fs_info);
794 int __init btrfs_bioset_init(void)
796 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
797 offsetof(struct btrfs_bio, bio),
800 if (bioset_init(&btrfs_clone_bioset, BIO_POOL_SIZE,
801 offsetof(struct btrfs_bio, bio), 0))
802 goto out_free_bioset;
803 if (bioset_init(&btrfs_repair_bioset, BIO_POOL_SIZE,
804 offsetof(struct btrfs_bio, bio),
806 goto out_free_clone_bioset;
807 if (mempool_init_kmalloc_pool(&btrfs_failed_bio_pool, BIO_POOL_SIZE,
808 sizeof(struct btrfs_failed_bio)))
809 goto out_free_repair_bioset;
812 out_free_repair_bioset:
813 bioset_exit(&btrfs_repair_bioset);
814 out_free_clone_bioset:
815 bioset_exit(&btrfs_clone_bioset);
817 bioset_exit(&btrfs_bioset);
821 void __cold btrfs_bioset_exit(void)
823 mempool_exit(&btrfs_failed_bio_pool);
824 bioset_exit(&btrfs_repair_bioset);
825 bioset_exit(&btrfs_clone_bioset);
826 bioset_exit(&btrfs_bioset);