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