162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2007 Oracle. All rights reserved. 462306a36Sopenharmony_ci * Copyright (C) 2022 Christoph Hellwig. 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <linux/bio.h> 862306a36Sopenharmony_ci#include "bio.h" 962306a36Sopenharmony_ci#include "ctree.h" 1062306a36Sopenharmony_ci#include "volumes.h" 1162306a36Sopenharmony_ci#include "raid56.h" 1262306a36Sopenharmony_ci#include "async-thread.h" 1362306a36Sopenharmony_ci#include "check-integrity.h" 1462306a36Sopenharmony_ci#include "dev-replace.h" 1562306a36Sopenharmony_ci#include "rcu-string.h" 1662306a36Sopenharmony_ci#include "zoned.h" 1762306a36Sopenharmony_ci#include "file-item.h" 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_cistatic struct bio_set btrfs_bioset; 2062306a36Sopenharmony_cistatic struct bio_set btrfs_clone_bioset; 2162306a36Sopenharmony_cistatic struct bio_set btrfs_repair_bioset; 2262306a36Sopenharmony_cistatic mempool_t btrfs_failed_bio_pool; 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cistruct btrfs_failed_bio { 2562306a36Sopenharmony_ci struct btrfs_bio *bbio; 2662306a36Sopenharmony_ci int num_copies; 2762306a36Sopenharmony_ci atomic_t repair_count; 2862306a36Sopenharmony_ci}; 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/* Is this a data path I/O that needs storage layer checksum and repair? */ 3162306a36Sopenharmony_cistatic inline bool is_data_bbio(struct btrfs_bio *bbio) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci return bbio->inode && is_data_inode(&bbio->inode->vfs_inode); 3462306a36Sopenharmony_ci} 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic bool bbio_has_ordered_extent(struct btrfs_bio *bbio) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci return is_data_bbio(bbio) && btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE; 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/* 4262306a36Sopenharmony_ci * Initialize a btrfs_bio structure. This skips the embedded bio itself as it 4362306a36Sopenharmony_ci * is already initialized by the block layer. 4462306a36Sopenharmony_ci */ 4562306a36Sopenharmony_civoid btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_fs_info *fs_info, 4662306a36Sopenharmony_ci btrfs_bio_end_io_t end_io, void *private) 4762306a36Sopenharmony_ci{ 4862306a36Sopenharmony_ci memset(bbio, 0, offsetof(struct btrfs_bio, bio)); 4962306a36Sopenharmony_ci bbio->fs_info = fs_info; 5062306a36Sopenharmony_ci bbio->end_io = end_io; 5162306a36Sopenharmony_ci bbio->private = private; 5262306a36Sopenharmony_ci atomic_set(&bbio->pending_ios, 1); 5362306a36Sopenharmony_ci} 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/* 5662306a36Sopenharmony_ci * Allocate a btrfs_bio structure. The btrfs_bio is the main I/O container for 5762306a36Sopenharmony_ci * btrfs, and is used for all I/O submitted through btrfs_submit_bio. 5862306a36Sopenharmony_ci * 5962306a36Sopenharmony_ci * Just like the underlying bio_alloc_bioset it will not fail as it is backed by 6062306a36Sopenharmony_ci * a mempool. 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_cistruct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf, 6362306a36Sopenharmony_ci struct btrfs_fs_info *fs_info, 6462306a36Sopenharmony_ci btrfs_bio_end_io_t end_io, void *private) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci struct btrfs_bio *bbio; 6762306a36Sopenharmony_ci struct bio *bio; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset); 7062306a36Sopenharmony_ci bbio = btrfs_bio(bio); 7162306a36Sopenharmony_ci btrfs_bio_init(bbio, fs_info, end_io, private); 7262306a36Sopenharmony_ci return bbio; 7362306a36Sopenharmony_ci} 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info, 7662306a36Sopenharmony_ci struct btrfs_bio *orig_bbio, 7762306a36Sopenharmony_ci u64 map_length, bool use_append) 7862306a36Sopenharmony_ci{ 7962306a36Sopenharmony_ci struct btrfs_bio *bbio; 8062306a36Sopenharmony_ci struct bio *bio; 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci if (use_append) { 8362306a36Sopenharmony_ci unsigned int nr_segs; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci bio = bio_split_rw(&orig_bbio->bio, &fs_info->limits, &nr_segs, 8662306a36Sopenharmony_ci &btrfs_clone_bioset, map_length); 8762306a36Sopenharmony_ci } else { 8862306a36Sopenharmony_ci bio = bio_split(&orig_bbio->bio, map_length >> SECTOR_SHIFT, 8962306a36Sopenharmony_ci GFP_NOFS, &btrfs_clone_bioset); 9062306a36Sopenharmony_ci } 9162306a36Sopenharmony_ci bbio = btrfs_bio(bio); 9262306a36Sopenharmony_ci btrfs_bio_init(bbio, fs_info, NULL, orig_bbio); 9362306a36Sopenharmony_ci bbio->inode = orig_bbio->inode; 9462306a36Sopenharmony_ci bbio->file_offset = orig_bbio->file_offset; 9562306a36Sopenharmony_ci orig_bbio->file_offset += map_length; 9662306a36Sopenharmony_ci if (bbio_has_ordered_extent(bbio)) { 9762306a36Sopenharmony_ci refcount_inc(&orig_bbio->ordered->refs); 9862306a36Sopenharmony_ci bbio->ordered = orig_bbio->ordered; 9962306a36Sopenharmony_ci } 10062306a36Sopenharmony_ci atomic_inc(&orig_bbio->pending_ios); 10162306a36Sopenharmony_ci return bbio; 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci/* Free a bio that was never submitted to the underlying device. */ 10562306a36Sopenharmony_cistatic void btrfs_cleanup_bio(struct btrfs_bio *bbio) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci if (bbio_has_ordered_extent(bbio)) 10862306a36Sopenharmony_ci btrfs_put_ordered_extent(bbio->ordered); 10962306a36Sopenharmony_ci bio_put(&bbio->bio); 11062306a36Sopenharmony_ci} 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_cistatic void __btrfs_bio_end_io(struct btrfs_bio *bbio) 11362306a36Sopenharmony_ci{ 11462306a36Sopenharmony_ci if (bbio_has_ordered_extent(bbio)) { 11562306a36Sopenharmony_ci struct btrfs_ordered_extent *ordered = bbio->ordered; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci bbio->end_io(bbio); 11862306a36Sopenharmony_ci btrfs_put_ordered_extent(ordered); 11962306a36Sopenharmony_ci } else { 12062306a36Sopenharmony_ci bbio->end_io(bbio); 12162306a36Sopenharmony_ci } 12262306a36Sopenharmony_ci} 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_civoid btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status) 12562306a36Sopenharmony_ci{ 12662306a36Sopenharmony_ci bbio->bio.bi_status = status; 12762306a36Sopenharmony_ci __btrfs_bio_end_io(bbio); 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_cistatic void btrfs_orig_write_end_io(struct bio *bio); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_cistatic void btrfs_bbio_propagate_error(struct btrfs_bio *bbio, 13362306a36Sopenharmony_ci struct btrfs_bio *orig_bbio) 13462306a36Sopenharmony_ci{ 13562306a36Sopenharmony_ci /* 13662306a36Sopenharmony_ci * For writes we tolerate nr_mirrors - 1 write failures, so we can't 13762306a36Sopenharmony_ci * just blindly propagate a write failure here. Instead increment the 13862306a36Sopenharmony_ci * error count in the original I/O context so that it is guaranteed to 13962306a36Sopenharmony_ci * be larger than the error tolerance. 14062306a36Sopenharmony_ci */ 14162306a36Sopenharmony_ci if (bbio->bio.bi_end_io == &btrfs_orig_write_end_io) { 14262306a36Sopenharmony_ci struct btrfs_io_stripe *orig_stripe = orig_bbio->bio.bi_private; 14362306a36Sopenharmony_ci struct btrfs_io_context *orig_bioc = orig_stripe->bioc; 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci atomic_add(orig_bioc->max_errors, &orig_bioc->error); 14662306a36Sopenharmony_ci } else { 14762306a36Sopenharmony_ci orig_bbio->bio.bi_status = bbio->bio.bi_status; 14862306a36Sopenharmony_ci } 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_cistatic void btrfs_orig_bbio_end_io(struct btrfs_bio *bbio) 15262306a36Sopenharmony_ci{ 15362306a36Sopenharmony_ci if (bbio->bio.bi_pool == &btrfs_clone_bioset) { 15462306a36Sopenharmony_ci struct btrfs_bio *orig_bbio = bbio->private; 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci if (bbio->bio.bi_status) 15762306a36Sopenharmony_ci btrfs_bbio_propagate_error(bbio, orig_bbio); 15862306a36Sopenharmony_ci btrfs_cleanup_bio(bbio); 15962306a36Sopenharmony_ci bbio = orig_bbio; 16062306a36Sopenharmony_ci } 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci if (atomic_dec_and_test(&bbio->pending_ios)) 16362306a36Sopenharmony_ci __btrfs_bio_end_io(bbio); 16462306a36Sopenharmony_ci} 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_cistatic int next_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror) 16762306a36Sopenharmony_ci{ 16862306a36Sopenharmony_ci if (cur_mirror == fbio->num_copies) 16962306a36Sopenharmony_ci return cur_mirror + 1 - fbio->num_copies; 17062306a36Sopenharmony_ci return cur_mirror + 1; 17162306a36Sopenharmony_ci} 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_cistatic int prev_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror) 17462306a36Sopenharmony_ci{ 17562306a36Sopenharmony_ci if (cur_mirror == 1) 17662306a36Sopenharmony_ci return fbio->num_copies; 17762306a36Sopenharmony_ci return cur_mirror - 1; 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_cistatic void btrfs_repair_done(struct btrfs_failed_bio *fbio) 18162306a36Sopenharmony_ci{ 18262306a36Sopenharmony_ci if (atomic_dec_and_test(&fbio->repair_count)) { 18362306a36Sopenharmony_ci btrfs_orig_bbio_end_io(fbio->bbio); 18462306a36Sopenharmony_ci mempool_free(fbio, &btrfs_failed_bio_pool); 18562306a36Sopenharmony_ci } 18662306a36Sopenharmony_ci} 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_cistatic void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio, 18962306a36Sopenharmony_ci struct btrfs_device *dev) 19062306a36Sopenharmony_ci{ 19162306a36Sopenharmony_ci struct btrfs_failed_bio *fbio = repair_bbio->private; 19262306a36Sopenharmony_ci struct btrfs_inode *inode = repair_bbio->inode; 19362306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = inode->root->fs_info; 19462306a36Sopenharmony_ci struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio); 19562306a36Sopenharmony_ci int mirror = repair_bbio->mirror_num; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci if (repair_bbio->bio.bi_status || 19862306a36Sopenharmony_ci !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) { 19962306a36Sopenharmony_ci bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ); 20062306a36Sopenharmony_ci repair_bbio->bio.bi_iter = repair_bbio->saved_iter; 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci mirror = next_repair_mirror(fbio, mirror); 20362306a36Sopenharmony_ci if (mirror == fbio->bbio->mirror_num) { 20462306a36Sopenharmony_ci btrfs_debug(fs_info, "no mirror left"); 20562306a36Sopenharmony_ci fbio->bbio->bio.bi_status = BLK_STS_IOERR; 20662306a36Sopenharmony_ci goto done; 20762306a36Sopenharmony_ci } 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci btrfs_submit_bio(repair_bbio, mirror); 21062306a36Sopenharmony_ci return; 21162306a36Sopenharmony_ci } 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci do { 21462306a36Sopenharmony_ci mirror = prev_repair_mirror(fbio, mirror); 21562306a36Sopenharmony_ci btrfs_repair_io_failure(fs_info, btrfs_ino(inode), 21662306a36Sopenharmony_ci repair_bbio->file_offset, fs_info->sectorsize, 21762306a36Sopenharmony_ci repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT, 21862306a36Sopenharmony_ci bv->bv_page, bv->bv_offset, mirror); 21962306a36Sopenharmony_ci } while (mirror != fbio->bbio->mirror_num); 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_cidone: 22262306a36Sopenharmony_ci btrfs_repair_done(fbio); 22362306a36Sopenharmony_ci bio_put(&repair_bbio->bio); 22462306a36Sopenharmony_ci} 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci/* 22762306a36Sopenharmony_ci * Try to kick off a repair read to the next available mirror for a bad sector. 22862306a36Sopenharmony_ci * 22962306a36Sopenharmony_ci * This primarily tries to recover good data to serve the actual read request, 23062306a36Sopenharmony_ci * but also tries to write the good data back to the bad mirror(s) when a 23162306a36Sopenharmony_ci * read succeeded to restore the redundancy. 23262306a36Sopenharmony_ci */ 23362306a36Sopenharmony_cistatic struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio, 23462306a36Sopenharmony_ci u32 bio_offset, 23562306a36Sopenharmony_ci struct bio_vec *bv, 23662306a36Sopenharmony_ci struct btrfs_failed_bio *fbio) 23762306a36Sopenharmony_ci{ 23862306a36Sopenharmony_ci struct btrfs_inode *inode = failed_bbio->inode; 23962306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = inode->root->fs_info; 24062306a36Sopenharmony_ci const u32 sectorsize = fs_info->sectorsize; 24162306a36Sopenharmony_ci const u64 logical = (failed_bbio->saved_iter.bi_sector << SECTOR_SHIFT); 24262306a36Sopenharmony_ci struct btrfs_bio *repair_bbio; 24362306a36Sopenharmony_ci struct bio *repair_bio; 24462306a36Sopenharmony_ci int num_copies; 24562306a36Sopenharmony_ci int mirror; 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci btrfs_debug(fs_info, "repair read error: read error at %llu", 24862306a36Sopenharmony_ci failed_bbio->file_offset + bio_offset); 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci num_copies = btrfs_num_copies(fs_info, logical, sectorsize); 25162306a36Sopenharmony_ci if (num_copies == 1) { 25262306a36Sopenharmony_ci btrfs_debug(fs_info, "no copy to repair from"); 25362306a36Sopenharmony_ci failed_bbio->bio.bi_status = BLK_STS_IOERR; 25462306a36Sopenharmony_ci return fbio; 25562306a36Sopenharmony_ci } 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ci if (!fbio) { 25862306a36Sopenharmony_ci fbio = mempool_alloc(&btrfs_failed_bio_pool, GFP_NOFS); 25962306a36Sopenharmony_ci fbio->bbio = failed_bbio; 26062306a36Sopenharmony_ci fbio->num_copies = num_copies; 26162306a36Sopenharmony_ci atomic_set(&fbio->repair_count, 1); 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci atomic_inc(&fbio->repair_count); 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci repair_bio = bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, 26762306a36Sopenharmony_ci &btrfs_repair_bioset); 26862306a36Sopenharmony_ci repair_bio->bi_iter.bi_sector = failed_bbio->saved_iter.bi_sector; 26962306a36Sopenharmony_ci __bio_add_page(repair_bio, bv->bv_page, bv->bv_len, bv->bv_offset); 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci repair_bbio = btrfs_bio(repair_bio); 27262306a36Sopenharmony_ci btrfs_bio_init(repair_bbio, fs_info, NULL, fbio); 27362306a36Sopenharmony_ci repair_bbio->inode = failed_bbio->inode; 27462306a36Sopenharmony_ci repair_bbio->file_offset = failed_bbio->file_offset + bio_offset; 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci mirror = next_repair_mirror(fbio, failed_bbio->mirror_num); 27762306a36Sopenharmony_ci btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror); 27862306a36Sopenharmony_ci btrfs_submit_bio(repair_bbio, mirror); 27962306a36Sopenharmony_ci return fbio; 28062306a36Sopenharmony_ci} 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_cistatic void btrfs_check_read_bio(struct btrfs_bio *bbio, struct btrfs_device *dev) 28362306a36Sopenharmony_ci{ 28462306a36Sopenharmony_ci struct btrfs_inode *inode = bbio->inode; 28562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = inode->root->fs_info; 28662306a36Sopenharmony_ci u32 sectorsize = fs_info->sectorsize; 28762306a36Sopenharmony_ci struct bvec_iter *iter = &bbio->saved_iter; 28862306a36Sopenharmony_ci blk_status_t status = bbio->bio.bi_status; 28962306a36Sopenharmony_ci struct btrfs_failed_bio *fbio = NULL; 29062306a36Sopenharmony_ci u32 offset = 0; 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci /* Read-repair requires the inode field to be set by the submitter. */ 29362306a36Sopenharmony_ci ASSERT(inode); 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci /* 29662306a36Sopenharmony_ci * Hand off repair bios to the repair code as there is no upper level 29762306a36Sopenharmony_ci * submitter for them. 29862306a36Sopenharmony_ci */ 29962306a36Sopenharmony_ci if (bbio->bio.bi_pool == &btrfs_repair_bioset) { 30062306a36Sopenharmony_ci btrfs_end_repair_bio(bbio, dev); 30162306a36Sopenharmony_ci return; 30262306a36Sopenharmony_ci } 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ci /* Clear the I/O error. A failed repair will reset it. */ 30562306a36Sopenharmony_ci bbio->bio.bi_status = BLK_STS_OK; 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci while (iter->bi_size) { 30862306a36Sopenharmony_ci struct bio_vec bv = bio_iter_iovec(&bbio->bio, *iter); 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci bv.bv_len = min(bv.bv_len, sectorsize); 31162306a36Sopenharmony_ci if (status || !btrfs_data_csum_ok(bbio, dev, offset, &bv)) 31262306a36Sopenharmony_ci fbio = repair_one_sector(bbio, offset, &bv, fbio); 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci bio_advance_iter_single(&bbio->bio, iter, sectorsize); 31562306a36Sopenharmony_ci offset += sectorsize; 31662306a36Sopenharmony_ci } 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci if (bbio->csum != bbio->csum_inline) 31962306a36Sopenharmony_ci kfree(bbio->csum); 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci if (fbio) 32262306a36Sopenharmony_ci btrfs_repair_done(fbio); 32362306a36Sopenharmony_ci else 32462306a36Sopenharmony_ci btrfs_orig_bbio_end_io(bbio); 32562306a36Sopenharmony_ci} 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_cistatic void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev) 32862306a36Sopenharmony_ci{ 32962306a36Sopenharmony_ci if (!dev || !dev->bdev) 33062306a36Sopenharmony_ci return; 33162306a36Sopenharmony_ci if (bio->bi_status != BLK_STS_IOERR && bio->bi_status != BLK_STS_TARGET) 33262306a36Sopenharmony_ci return; 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_ci if (btrfs_op(bio) == BTRFS_MAP_WRITE) 33562306a36Sopenharmony_ci btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); 33662306a36Sopenharmony_ci else if (!(bio->bi_opf & REQ_RAHEAD)) 33762306a36Sopenharmony_ci btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); 33862306a36Sopenharmony_ci if (bio->bi_opf & REQ_PREFLUSH) 33962306a36Sopenharmony_ci btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_FLUSH_ERRS); 34062306a36Sopenharmony_ci} 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_cistatic struct workqueue_struct *btrfs_end_io_wq(struct btrfs_fs_info *fs_info, 34362306a36Sopenharmony_ci struct bio *bio) 34462306a36Sopenharmony_ci{ 34562306a36Sopenharmony_ci if (bio->bi_opf & REQ_META) 34662306a36Sopenharmony_ci return fs_info->endio_meta_workers; 34762306a36Sopenharmony_ci return fs_info->endio_workers; 34862306a36Sopenharmony_ci} 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_cistatic void btrfs_end_bio_work(struct work_struct *work) 35162306a36Sopenharmony_ci{ 35262306a36Sopenharmony_ci struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work); 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci /* Metadata reads are checked and repaired by the submitter. */ 35562306a36Sopenharmony_ci if (is_data_bbio(bbio)) 35662306a36Sopenharmony_ci btrfs_check_read_bio(bbio, bbio->bio.bi_private); 35762306a36Sopenharmony_ci else 35862306a36Sopenharmony_ci btrfs_orig_bbio_end_io(bbio); 35962306a36Sopenharmony_ci} 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_cistatic void btrfs_simple_end_io(struct bio *bio) 36262306a36Sopenharmony_ci{ 36362306a36Sopenharmony_ci struct btrfs_bio *bbio = btrfs_bio(bio); 36462306a36Sopenharmony_ci struct btrfs_device *dev = bio->bi_private; 36562306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = bbio->fs_info; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci btrfs_bio_counter_dec(fs_info); 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci if (bio->bi_status) 37062306a36Sopenharmony_ci btrfs_log_dev_io_error(bio, dev); 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_ci if (bio_op(bio) == REQ_OP_READ) { 37362306a36Sopenharmony_ci INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work); 37462306a36Sopenharmony_ci queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work); 37562306a36Sopenharmony_ci } else { 37662306a36Sopenharmony_ci if (bio_op(bio) == REQ_OP_ZONE_APPEND && !bio->bi_status) 37762306a36Sopenharmony_ci btrfs_record_physical_zoned(bbio); 37862306a36Sopenharmony_ci btrfs_orig_bbio_end_io(bbio); 37962306a36Sopenharmony_ci } 38062306a36Sopenharmony_ci} 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_cistatic void btrfs_raid56_end_io(struct bio *bio) 38362306a36Sopenharmony_ci{ 38462306a36Sopenharmony_ci struct btrfs_io_context *bioc = bio->bi_private; 38562306a36Sopenharmony_ci struct btrfs_bio *bbio = btrfs_bio(bio); 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci btrfs_bio_counter_dec(bioc->fs_info); 38862306a36Sopenharmony_ci bbio->mirror_num = bioc->mirror_num; 38962306a36Sopenharmony_ci if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio)) 39062306a36Sopenharmony_ci btrfs_check_read_bio(bbio, NULL); 39162306a36Sopenharmony_ci else 39262306a36Sopenharmony_ci btrfs_orig_bbio_end_io(bbio); 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci btrfs_put_bioc(bioc); 39562306a36Sopenharmony_ci} 39662306a36Sopenharmony_ci 39762306a36Sopenharmony_cistatic void btrfs_orig_write_end_io(struct bio *bio) 39862306a36Sopenharmony_ci{ 39962306a36Sopenharmony_ci struct btrfs_io_stripe *stripe = bio->bi_private; 40062306a36Sopenharmony_ci struct btrfs_io_context *bioc = stripe->bioc; 40162306a36Sopenharmony_ci struct btrfs_bio *bbio = btrfs_bio(bio); 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ci btrfs_bio_counter_dec(bioc->fs_info); 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci if (bio->bi_status) { 40662306a36Sopenharmony_ci atomic_inc(&bioc->error); 40762306a36Sopenharmony_ci btrfs_log_dev_io_error(bio, stripe->dev); 40862306a36Sopenharmony_ci } 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ci /* 41162306a36Sopenharmony_ci * Only send an error to the higher layers if it is beyond the tolerance 41262306a36Sopenharmony_ci * threshold. 41362306a36Sopenharmony_ci */ 41462306a36Sopenharmony_ci if (atomic_read(&bioc->error) > bioc->max_errors) 41562306a36Sopenharmony_ci bio->bi_status = BLK_STS_IOERR; 41662306a36Sopenharmony_ci else 41762306a36Sopenharmony_ci bio->bi_status = BLK_STS_OK; 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci btrfs_orig_bbio_end_io(bbio); 42062306a36Sopenharmony_ci btrfs_put_bioc(bioc); 42162306a36Sopenharmony_ci} 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_cistatic void btrfs_clone_write_end_io(struct bio *bio) 42462306a36Sopenharmony_ci{ 42562306a36Sopenharmony_ci struct btrfs_io_stripe *stripe = bio->bi_private; 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ci if (bio->bi_status) { 42862306a36Sopenharmony_ci atomic_inc(&stripe->bioc->error); 42962306a36Sopenharmony_ci btrfs_log_dev_io_error(bio, stripe->dev); 43062306a36Sopenharmony_ci } 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci /* Pass on control to the original bio this one was cloned from */ 43362306a36Sopenharmony_ci bio_endio(stripe->bioc->orig_bio); 43462306a36Sopenharmony_ci bio_put(bio); 43562306a36Sopenharmony_ci} 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_cistatic void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio) 43862306a36Sopenharmony_ci{ 43962306a36Sopenharmony_ci if (!dev || !dev->bdev || 44062306a36Sopenharmony_ci test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || 44162306a36Sopenharmony_ci (btrfs_op(bio) == BTRFS_MAP_WRITE && 44262306a36Sopenharmony_ci !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { 44362306a36Sopenharmony_ci bio_io_error(bio); 44462306a36Sopenharmony_ci return; 44562306a36Sopenharmony_ci } 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci bio_set_dev(bio, dev->bdev); 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci /* 45062306a36Sopenharmony_ci * For zone append writing, bi_sector must point the beginning of the 45162306a36Sopenharmony_ci * zone 45262306a36Sopenharmony_ci */ 45362306a36Sopenharmony_ci if (bio_op(bio) == REQ_OP_ZONE_APPEND) { 45462306a36Sopenharmony_ci u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT; 45562306a36Sopenharmony_ci u64 zone_start = round_down(physical, dev->fs_info->zone_size); 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci ASSERT(btrfs_dev_is_sequential(dev, physical)); 45862306a36Sopenharmony_ci bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT; 45962306a36Sopenharmony_ci } 46062306a36Sopenharmony_ci btrfs_debug_in_rcu(dev->fs_info, 46162306a36Sopenharmony_ci "%s: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", 46262306a36Sopenharmony_ci __func__, bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector, 46362306a36Sopenharmony_ci (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev), 46462306a36Sopenharmony_ci dev->devid, bio->bi_iter.bi_size); 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci btrfsic_check_bio(bio); 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT) 46962306a36Sopenharmony_ci blkcg_punt_bio_submit(bio); 47062306a36Sopenharmony_ci else 47162306a36Sopenharmony_ci submit_bio(bio); 47262306a36Sopenharmony_ci} 47362306a36Sopenharmony_ci 47462306a36Sopenharmony_cistatic void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr) 47562306a36Sopenharmony_ci{ 47662306a36Sopenharmony_ci struct bio *orig_bio = bioc->orig_bio, *bio; 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci ASSERT(bio_op(orig_bio) != REQ_OP_READ); 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_ci /* Reuse the bio embedded into the btrfs_bio for the last mirror */ 48162306a36Sopenharmony_ci if (dev_nr == bioc->num_stripes - 1) { 48262306a36Sopenharmony_ci bio = orig_bio; 48362306a36Sopenharmony_ci bio->bi_end_io = btrfs_orig_write_end_io; 48462306a36Sopenharmony_ci } else { 48562306a36Sopenharmony_ci bio = bio_alloc_clone(NULL, orig_bio, GFP_NOFS, &fs_bio_set); 48662306a36Sopenharmony_ci bio_inc_remaining(orig_bio); 48762306a36Sopenharmony_ci bio->bi_end_io = btrfs_clone_write_end_io; 48862306a36Sopenharmony_ci } 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci bio->bi_private = &bioc->stripes[dev_nr]; 49162306a36Sopenharmony_ci bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT; 49262306a36Sopenharmony_ci bioc->stripes[dev_nr].bioc = bioc; 49362306a36Sopenharmony_ci btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio); 49462306a36Sopenharmony_ci} 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_cistatic void __btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc, 49762306a36Sopenharmony_ci struct btrfs_io_stripe *smap, int mirror_num) 49862306a36Sopenharmony_ci{ 49962306a36Sopenharmony_ci if (!bioc) { 50062306a36Sopenharmony_ci /* Single mirror read/write fast path. */ 50162306a36Sopenharmony_ci btrfs_bio(bio)->mirror_num = mirror_num; 50262306a36Sopenharmony_ci bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT; 50362306a36Sopenharmony_ci if (bio_op(bio) != REQ_OP_READ) 50462306a36Sopenharmony_ci btrfs_bio(bio)->orig_physical = smap->physical; 50562306a36Sopenharmony_ci bio->bi_private = smap->dev; 50662306a36Sopenharmony_ci bio->bi_end_io = btrfs_simple_end_io; 50762306a36Sopenharmony_ci btrfs_submit_dev_bio(smap->dev, bio); 50862306a36Sopenharmony_ci } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { 50962306a36Sopenharmony_ci /* Parity RAID write or read recovery. */ 51062306a36Sopenharmony_ci bio->bi_private = bioc; 51162306a36Sopenharmony_ci bio->bi_end_io = btrfs_raid56_end_io; 51262306a36Sopenharmony_ci if (bio_op(bio) == REQ_OP_READ) 51362306a36Sopenharmony_ci raid56_parity_recover(bio, bioc, mirror_num); 51462306a36Sopenharmony_ci else 51562306a36Sopenharmony_ci raid56_parity_write(bio, bioc); 51662306a36Sopenharmony_ci } else { 51762306a36Sopenharmony_ci /* Write to multiple mirrors. */ 51862306a36Sopenharmony_ci int total_devs = bioc->num_stripes; 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci bioc->orig_bio = bio; 52162306a36Sopenharmony_ci for (int dev_nr = 0; dev_nr < total_devs; dev_nr++) 52262306a36Sopenharmony_ci btrfs_submit_mirrored_bio(bioc, dev_nr); 52362306a36Sopenharmony_ci } 52462306a36Sopenharmony_ci} 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_cistatic blk_status_t btrfs_bio_csum(struct btrfs_bio *bbio) 52762306a36Sopenharmony_ci{ 52862306a36Sopenharmony_ci if (bbio->bio.bi_opf & REQ_META) 52962306a36Sopenharmony_ci return btree_csum_one_bio(bbio); 53062306a36Sopenharmony_ci return btrfs_csum_one_bio(bbio); 53162306a36Sopenharmony_ci} 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci/* 53462306a36Sopenharmony_ci * Async submit bios are used to offload expensive checksumming onto the worker 53562306a36Sopenharmony_ci * threads. 53662306a36Sopenharmony_ci */ 53762306a36Sopenharmony_cistruct async_submit_bio { 53862306a36Sopenharmony_ci struct btrfs_bio *bbio; 53962306a36Sopenharmony_ci struct btrfs_io_context *bioc; 54062306a36Sopenharmony_ci struct btrfs_io_stripe smap; 54162306a36Sopenharmony_ci int mirror_num; 54262306a36Sopenharmony_ci struct btrfs_work work; 54362306a36Sopenharmony_ci}; 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ci/* 54662306a36Sopenharmony_ci * In order to insert checksums into the metadata in large chunks, we wait 54762306a36Sopenharmony_ci * until bio submission time. All the pages in the bio are checksummed and 54862306a36Sopenharmony_ci * sums are attached onto the ordered extent record. 54962306a36Sopenharmony_ci * 55062306a36Sopenharmony_ci * At IO completion time the csums attached on the ordered extent record are 55162306a36Sopenharmony_ci * inserted into the btree. 55262306a36Sopenharmony_ci */ 55362306a36Sopenharmony_cistatic void run_one_async_start(struct btrfs_work *work) 55462306a36Sopenharmony_ci{ 55562306a36Sopenharmony_ci struct async_submit_bio *async = 55662306a36Sopenharmony_ci container_of(work, struct async_submit_bio, work); 55762306a36Sopenharmony_ci blk_status_t ret; 55862306a36Sopenharmony_ci 55962306a36Sopenharmony_ci ret = btrfs_bio_csum(async->bbio); 56062306a36Sopenharmony_ci if (ret) 56162306a36Sopenharmony_ci async->bbio->bio.bi_status = ret; 56262306a36Sopenharmony_ci} 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_ci/* 56562306a36Sopenharmony_ci * In order to insert checksums into the metadata in large chunks, we wait 56662306a36Sopenharmony_ci * until bio submission time. All the pages in the bio are checksummed and 56762306a36Sopenharmony_ci * sums are attached onto the ordered extent record. 56862306a36Sopenharmony_ci * 56962306a36Sopenharmony_ci * At IO completion time the csums attached on the ordered extent record are 57062306a36Sopenharmony_ci * inserted into the tree. 57162306a36Sopenharmony_ci */ 57262306a36Sopenharmony_cistatic void run_one_async_done(struct btrfs_work *work) 57362306a36Sopenharmony_ci{ 57462306a36Sopenharmony_ci struct async_submit_bio *async = 57562306a36Sopenharmony_ci container_of(work, struct async_submit_bio, work); 57662306a36Sopenharmony_ci struct bio *bio = &async->bbio->bio; 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci /* If an error occurred we just want to clean up the bio and move on. */ 57962306a36Sopenharmony_ci if (bio->bi_status) { 58062306a36Sopenharmony_ci btrfs_orig_bbio_end_io(async->bbio); 58162306a36Sopenharmony_ci return; 58262306a36Sopenharmony_ci } 58362306a36Sopenharmony_ci 58462306a36Sopenharmony_ci /* 58562306a36Sopenharmony_ci * All of the bios that pass through here are from async helpers. 58662306a36Sopenharmony_ci * Use REQ_BTRFS_CGROUP_PUNT to issue them from the owning cgroup's 58762306a36Sopenharmony_ci * context. This changes nothing when cgroups aren't in use. 58862306a36Sopenharmony_ci */ 58962306a36Sopenharmony_ci bio->bi_opf |= REQ_BTRFS_CGROUP_PUNT; 59062306a36Sopenharmony_ci __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num); 59162306a36Sopenharmony_ci} 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_cistatic void run_one_async_free(struct btrfs_work *work) 59462306a36Sopenharmony_ci{ 59562306a36Sopenharmony_ci kfree(container_of(work, struct async_submit_bio, work)); 59662306a36Sopenharmony_ci} 59762306a36Sopenharmony_ci 59862306a36Sopenharmony_cistatic bool should_async_write(struct btrfs_bio *bbio) 59962306a36Sopenharmony_ci{ 60062306a36Sopenharmony_ci /* Submit synchronously if the checksum implementation is fast. */ 60162306a36Sopenharmony_ci if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &bbio->fs_info->flags)) 60262306a36Sopenharmony_ci return false; 60362306a36Sopenharmony_ci 60462306a36Sopenharmony_ci /* 60562306a36Sopenharmony_ci * Try to defer the submission to a workqueue to parallelize the 60662306a36Sopenharmony_ci * checksum calculation unless the I/O is issued synchronously. 60762306a36Sopenharmony_ci */ 60862306a36Sopenharmony_ci if (op_is_sync(bbio->bio.bi_opf)) 60962306a36Sopenharmony_ci return false; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci /* Zoned devices require I/O to be submitted in order. */ 61262306a36Sopenharmony_ci if ((bbio->bio.bi_opf & REQ_META) && btrfs_is_zoned(bbio->fs_info)) 61362306a36Sopenharmony_ci return false; 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_ci return true; 61662306a36Sopenharmony_ci} 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_ci/* 61962306a36Sopenharmony_ci * Submit bio to an async queue. 62062306a36Sopenharmony_ci * 62162306a36Sopenharmony_ci * Return true if the work has been succesfuly submitted, else false. 62262306a36Sopenharmony_ci */ 62362306a36Sopenharmony_cistatic bool btrfs_wq_submit_bio(struct btrfs_bio *bbio, 62462306a36Sopenharmony_ci struct btrfs_io_context *bioc, 62562306a36Sopenharmony_ci struct btrfs_io_stripe *smap, int mirror_num) 62662306a36Sopenharmony_ci{ 62762306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = bbio->fs_info; 62862306a36Sopenharmony_ci struct async_submit_bio *async; 62962306a36Sopenharmony_ci 63062306a36Sopenharmony_ci async = kmalloc(sizeof(*async), GFP_NOFS); 63162306a36Sopenharmony_ci if (!async) 63262306a36Sopenharmony_ci return false; 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ci async->bbio = bbio; 63562306a36Sopenharmony_ci async->bioc = bioc; 63662306a36Sopenharmony_ci async->smap = *smap; 63762306a36Sopenharmony_ci async->mirror_num = mirror_num; 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_ci btrfs_init_work(&async->work, run_one_async_start, run_one_async_done, 64062306a36Sopenharmony_ci run_one_async_free); 64162306a36Sopenharmony_ci btrfs_queue_work(fs_info->workers, &async->work); 64262306a36Sopenharmony_ci return true; 64362306a36Sopenharmony_ci} 64462306a36Sopenharmony_ci 64562306a36Sopenharmony_cistatic bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) 64662306a36Sopenharmony_ci{ 64762306a36Sopenharmony_ci struct btrfs_inode *inode = bbio->inode; 64862306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = bbio->fs_info; 64962306a36Sopenharmony_ci struct btrfs_bio *orig_bbio = bbio; 65062306a36Sopenharmony_ci struct bio *bio = &bbio->bio; 65162306a36Sopenharmony_ci u64 logical = bio->bi_iter.bi_sector << SECTOR_SHIFT; 65262306a36Sopenharmony_ci u64 length = bio->bi_iter.bi_size; 65362306a36Sopenharmony_ci u64 map_length = length; 65462306a36Sopenharmony_ci bool use_append = btrfs_use_zone_append(bbio); 65562306a36Sopenharmony_ci struct btrfs_io_context *bioc = NULL; 65662306a36Sopenharmony_ci struct btrfs_io_stripe smap; 65762306a36Sopenharmony_ci blk_status_t ret; 65862306a36Sopenharmony_ci int error; 65962306a36Sopenharmony_ci 66062306a36Sopenharmony_ci btrfs_bio_counter_inc_blocked(fs_info); 66162306a36Sopenharmony_ci error = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, 66262306a36Sopenharmony_ci &bioc, &smap, &mirror_num, 1); 66362306a36Sopenharmony_ci if (error) { 66462306a36Sopenharmony_ci ret = errno_to_blk_status(error); 66562306a36Sopenharmony_ci goto fail; 66662306a36Sopenharmony_ci } 66762306a36Sopenharmony_ci 66862306a36Sopenharmony_ci map_length = min(map_length, length); 66962306a36Sopenharmony_ci if (use_append) 67062306a36Sopenharmony_ci map_length = min(map_length, fs_info->max_zone_append_size); 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci if (map_length < length) { 67362306a36Sopenharmony_ci bbio = btrfs_split_bio(fs_info, bbio, map_length, use_append); 67462306a36Sopenharmony_ci bio = &bbio->bio; 67562306a36Sopenharmony_ci } 67662306a36Sopenharmony_ci 67762306a36Sopenharmony_ci /* 67862306a36Sopenharmony_ci * Save the iter for the end_io handler and preload the checksums for 67962306a36Sopenharmony_ci * data reads. 68062306a36Sopenharmony_ci */ 68162306a36Sopenharmony_ci if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio)) { 68262306a36Sopenharmony_ci bbio->saved_iter = bio->bi_iter; 68362306a36Sopenharmony_ci ret = btrfs_lookup_bio_sums(bbio); 68462306a36Sopenharmony_ci if (ret) 68562306a36Sopenharmony_ci goto fail_put_bio; 68662306a36Sopenharmony_ci } 68762306a36Sopenharmony_ci 68862306a36Sopenharmony_ci if (btrfs_op(bio) == BTRFS_MAP_WRITE) { 68962306a36Sopenharmony_ci if (use_append) { 69062306a36Sopenharmony_ci bio->bi_opf &= ~REQ_OP_WRITE; 69162306a36Sopenharmony_ci bio->bi_opf |= REQ_OP_ZONE_APPEND; 69262306a36Sopenharmony_ci } 69362306a36Sopenharmony_ci 69462306a36Sopenharmony_ci /* 69562306a36Sopenharmony_ci * Csum items for reloc roots have already been cloned at this 69662306a36Sopenharmony_ci * point, so they are handled as part of the no-checksum case. 69762306a36Sopenharmony_ci */ 69862306a36Sopenharmony_ci if (inode && !(inode->flags & BTRFS_INODE_NODATASUM) && 69962306a36Sopenharmony_ci !test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state) && 70062306a36Sopenharmony_ci !btrfs_is_data_reloc_root(inode->root)) { 70162306a36Sopenharmony_ci if (should_async_write(bbio) && 70262306a36Sopenharmony_ci btrfs_wq_submit_bio(bbio, bioc, &smap, mirror_num)) 70362306a36Sopenharmony_ci goto done; 70462306a36Sopenharmony_ci 70562306a36Sopenharmony_ci ret = btrfs_bio_csum(bbio); 70662306a36Sopenharmony_ci if (ret) 70762306a36Sopenharmony_ci goto fail_put_bio; 70862306a36Sopenharmony_ci } else if (use_append) { 70962306a36Sopenharmony_ci ret = btrfs_alloc_dummy_sum(bbio); 71062306a36Sopenharmony_ci if (ret) 71162306a36Sopenharmony_ci goto fail_put_bio; 71262306a36Sopenharmony_ci } 71362306a36Sopenharmony_ci } 71462306a36Sopenharmony_ci 71562306a36Sopenharmony_ci __btrfs_submit_bio(bio, bioc, &smap, mirror_num); 71662306a36Sopenharmony_cidone: 71762306a36Sopenharmony_ci return map_length == length; 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_cifail_put_bio: 72062306a36Sopenharmony_ci if (map_length < length) 72162306a36Sopenharmony_ci btrfs_cleanup_bio(bbio); 72262306a36Sopenharmony_cifail: 72362306a36Sopenharmony_ci btrfs_bio_counter_dec(fs_info); 72462306a36Sopenharmony_ci btrfs_bio_end_io(orig_bbio, ret); 72562306a36Sopenharmony_ci /* Do not submit another chunk */ 72662306a36Sopenharmony_ci return true; 72762306a36Sopenharmony_ci} 72862306a36Sopenharmony_ci 72962306a36Sopenharmony_civoid btrfs_submit_bio(struct btrfs_bio *bbio, int mirror_num) 73062306a36Sopenharmony_ci{ 73162306a36Sopenharmony_ci /* If bbio->inode is not populated, its file_offset must be 0. */ 73262306a36Sopenharmony_ci ASSERT(bbio->inode || bbio->file_offset == 0); 73362306a36Sopenharmony_ci 73462306a36Sopenharmony_ci while (!btrfs_submit_chunk(bbio, mirror_num)) 73562306a36Sopenharmony_ci ; 73662306a36Sopenharmony_ci} 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci/* 73962306a36Sopenharmony_ci * Submit a repair write. 74062306a36Sopenharmony_ci * 74162306a36Sopenharmony_ci * This bypasses btrfs_submit_bio deliberately, as that writes all copies in a 74262306a36Sopenharmony_ci * RAID setup. Here we only want to write the one bad copy, so we do the 74362306a36Sopenharmony_ci * mapping ourselves and submit the bio directly. 74462306a36Sopenharmony_ci * 74562306a36Sopenharmony_ci * The I/O is issued synchronously to block the repair read completion from 74662306a36Sopenharmony_ci * freeing the bio. 74762306a36Sopenharmony_ci */ 74862306a36Sopenharmony_ciint btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, 74962306a36Sopenharmony_ci u64 length, u64 logical, struct page *page, 75062306a36Sopenharmony_ci unsigned int pg_offset, int mirror_num) 75162306a36Sopenharmony_ci{ 75262306a36Sopenharmony_ci struct btrfs_io_stripe smap = { 0 }; 75362306a36Sopenharmony_ci struct bio_vec bvec; 75462306a36Sopenharmony_ci struct bio bio; 75562306a36Sopenharmony_ci int ret = 0; 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); 75862306a36Sopenharmony_ci BUG_ON(!mirror_num); 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci if (btrfs_repair_one_zone(fs_info, logical)) 76162306a36Sopenharmony_ci return 0; 76262306a36Sopenharmony_ci 76362306a36Sopenharmony_ci /* 76462306a36Sopenharmony_ci * Avoid races with device replace and make sure our bioc has devices 76562306a36Sopenharmony_ci * associated to its stripes that don't go away while we are doing the 76662306a36Sopenharmony_ci * read repair operation. 76762306a36Sopenharmony_ci */ 76862306a36Sopenharmony_ci btrfs_bio_counter_inc_blocked(fs_info); 76962306a36Sopenharmony_ci ret = btrfs_map_repair_block(fs_info, &smap, logical, length, mirror_num); 77062306a36Sopenharmony_ci if (ret < 0) 77162306a36Sopenharmony_ci goto out_counter_dec; 77262306a36Sopenharmony_ci 77362306a36Sopenharmony_ci if (!smap.dev->bdev || 77462306a36Sopenharmony_ci !test_bit(BTRFS_DEV_STATE_WRITEABLE, &smap.dev->dev_state)) { 77562306a36Sopenharmony_ci ret = -EIO; 77662306a36Sopenharmony_ci goto out_counter_dec; 77762306a36Sopenharmony_ci } 77862306a36Sopenharmony_ci 77962306a36Sopenharmony_ci bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC); 78062306a36Sopenharmony_ci bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT; 78162306a36Sopenharmony_ci __bio_add_page(&bio, page, length, pg_offset); 78262306a36Sopenharmony_ci 78362306a36Sopenharmony_ci btrfsic_check_bio(&bio); 78462306a36Sopenharmony_ci ret = submit_bio_wait(&bio); 78562306a36Sopenharmony_ci if (ret) { 78662306a36Sopenharmony_ci /* try to remap that extent elsewhere? */ 78762306a36Sopenharmony_ci btrfs_dev_stat_inc_and_print(smap.dev, BTRFS_DEV_STAT_WRITE_ERRS); 78862306a36Sopenharmony_ci goto out_bio_uninit; 78962306a36Sopenharmony_ci } 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_ci btrfs_info_rl_in_rcu(fs_info, 79262306a36Sopenharmony_ci "read error corrected: ino %llu off %llu (dev %s sector %llu)", 79362306a36Sopenharmony_ci ino, start, btrfs_dev_name(smap.dev), 79462306a36Sopenharmony_ci smap.physical >> SECTOR_SHIFT); 79562306a36Sopenharmony_ci ret = 0; 79662306a36Sopenharmony_ci 79762306a36Sopenharmony_ciout_bio_uninit: 79862306a36Sopenharmony_ci bio_uninit(&bio); 79962306a36Sopenharmony_ciout_counter_dec: 80062306a36Sopenharmony_ci btrfs_bio_counter_dec(fs_info); 80162306a36Sopenharmony_ci return ret; 80262306a36Sopenharmony_ci} 80362306a36Sopenharmony_ci 80462306a36Sopenharmony_ci/* 80562306a36Sopenharmony_ci * Submit a btrfs_bio based repair write. 80662306a36Sopenharmony_ci * 80762306a36Sopenharmony_ci * If @dev_replace is true, the write would be submitted to dev-replace target. 80862306a36Sopenharmony_ci */ 80962306a36Sopenharmony_civoid btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace) 81062306a36Sopenharmony_ci{ 81162306a36Sopenharmony_ci struct btrfs_fs_info *fs_info = bbio->fs_info; 81262306a36Sopenharmony_ci u64 logical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT; 81362306a36Sopenharmony_ci u64 length = bbio->bio.bi_iter.bi_size; 81462306a36Sopenharmony_ci struct btrfs_io_stripe smap = { 0 }; 81562306a36Sopenharmony_ci int ret; 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci ASSERT(fs_info); 81862306a36Sopenharmony_ci ASSERT(mirror_num > 0); 81962306a36Sopenharmony_ci ASSERT(btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE); 82062306a36Sopenharmony_ci ASSERT(!bbio->inode); 82162306a36Sopenharmony_ci 82262306a36Sopenharmony_ci btrfs_bio_counter_inc_blocked(fs_info); 82362306a36Sopenharmony_ci ret = btrfs_map_repair_block(fs_info, &smap, logical, length, mirror_num); 82462306a36Sopenharmony_ci if (ret < 0) 82562306a36Sopenharmony_ci goto fail; 82662306a36Sopenharmony_ci 82762306a36Sopenharmony_ci if (dev_replace) { 82862306a36Sopenharmony_ci ASSERT(smap.dev == fs_info->dev_replace.srcdev); 82962306a36Sopenharmony_ci smap.dev = fs_info->dev_replace.tgtdev; 83062306a36Sopenharmony_ci } 83162306a36Sopenharmony_ci __btrfs_submit_bio(&bbio->bio, NULL, &smap, mirror_num); 83262306a36Sopenharmony_ci return; 83362306a36Sopenharmony_ci 83462306a36Sopenharmony_cifail: 83562306a36Sopenharmony_ci btrfs_bio_counter_dec(fs_info); 83662306a36Sopenharmony_ci btrfs_bio_end_io(bbio, errno_to_blk_status(ret)); 83762306a36Sopenharmony_ci} 83862306a36Sopenharmony_ci 83962306a36Sopenharmony_ciint __init btrfs_bioset_init(void) 84062306a36Sopenharmony_ci{ 84162306a36Sopenharmony_ci if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE, 84262306a36Sopenharmony_ci offsetof(struct btrfs_bio, bio), 84362306a36Sopenharmony_ci BIOSET_NEED_BVECS)) 84462306a36Sopenharmony_ci return -ENOMEM; 84562306a36Sopenharmony_ci if (bioset_init(&btrfs_clone_bioset, BIO_POOL_SIZE, 84662306a36Sopenharmony_ci offsetof(struct btrfs_bio, bio), 0)) 84762306a36Sopenharmony_ci goto out_free_bioset; 84862306a36Sopenharmony_ci if (bioset_init(&btrfs_repair_bioset, BIO_POOL_SIZE, 84962306a36Sopenharmony_ci offsetof(struct btrfs_bio, bio), 85062306a36Sopenharmony_ci BIOSET_NEED_BVECS)) 85162306a36Sopenharmony_ci goto out_free_clone_bioset; 85262306a36Sopenharmony_ci if (mempool_init_kmalloc_pool(&btrfs_failed_bio_pool, BIO_POOL_SIZE, 85362306a36Sopenharmony_ci sizeof(struct btrfs_failed_bio))) 85462306a36Sopenharmony_ci goto out_free_repair_bioset; 85562306a36Sopenharmony_ci return 0; 85662306a36Sopenharmony_ci 85762306a36Sopenharmony_ciout_free_repair_bioset: 85862306a36Sopenharmony_ci bioset_exit(&btrfs_repair_bioset); 85962306a36Sopenharmony_ciout_free_clone_bioset: 86062306a36Sopenharmony_ci bioset_exit(&btrfs_clone_bioset); 86162306a36Sopenharmony_ciout_free_bioset: 86262306a36Sopenharmony_ci bioset_exit(&btrfs_bioset); 86362306a36Sopenharmony_ci return -ENOMEM; 86462306a36Sopenharmony_ci} 86562306a36Sopenharmony_ci 86662306a36Sopenharmony_civoid __cold btrfs_bioset_exit(void) 86762306a36Sopenharmony_ci{ 86862306a36Sopenharmony_ci mempool_exit(&btrfs_failed_bio_pool); 86962306a36Sopenharmony_ci bioset_exit(&btrfs_repair_bioset); 87062306a36Sopenharmony_ci bioset_exit(&btrfs_clone_bioset); 87162306a36Sopenharmony_ci bioset_exit(&btrfs_bioset); 87262306a36Sopenharmony_ci} 873