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_repair_bioset; 21 static mempool_t btrfs_failed_bio_pool; 22 23 struct btrfs_failed_bio { 24 struct btrfs_bio *bbio; 25 int num_copies; 26 atomic_t repair_count; 27 }; 28 29 /* 30 * Initialize a btrfs_bio structure. This skips the embedded bio itself as it 31 * is already initialized by the block layer. 32 */ 33 static inline void btrfs_bio_init(struct btrfs_bio *bbio, 34 struct btrfs_inode *inode, 35 btrfs_bio_end_io_t end_io, void *private) 36 { 37 memset(bbio, 0, offsetof(struct btrfs_bio, bio)); 38 bbio->inode = inode; 39 bbio->end_io = end_io; 40 bbio->private = private; 41 } 42 43 /* 44 * Allocate a btrfs_bio structure. The btrfs_bio is the main I/O container for 45 * btrfs, and is used for all I/O submitted through btrfs_submit_bio. 46 * 47 * Just like the underlying bio_alloc_bioset it will not fail as it is backed by 48 * a mempool. 49 */ 50 struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf, 51 struct btrfs_inode *inode, 52 btrfs_bio_end_io_t end_io, void *private) 53 { 54 struct bio *bio; 55 56 bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset); 57 btrfs_bio_init(btrfs_bio(bio), inode, end_io, private); 58 return bio; 59 } 60 61 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size, 62 struct btrfs_inode *inode, 63 btrfs_bio_end_io_t end_io, void *private) 64 { 65 struct bio *bio; 66 struct btrfs_bio *bbio; 67 68 ASSERT(offset <= UINT_MAX && size <= UINT_MAX); 69 70 bio = bio_alloc_clone(orig->bi_bdev, orig, GFP_NOFS, &btrfs_bioset); 71 bbio = btrfs_bio(bio); 72 btrfs_bio_init(bbio, inode, end_io, private); 73 74 bio_trim(bio, offset >> 9, size >> 9); 75 return bio; 76 } 77 78 static int next_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror) 79 { 80 if (cur_mirror == fbio->num_copies) 81 return cur_mirror + 1 - fbio->num_copies; 82 return cur_mirror + 1; 83 } 84 85 static int prev_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror) 86 { 87 if (cur_mirror == 1) 88 return fbio->num_copies; 89 return cur_mirror - 1; 90 } 91 92 static void btrfs_repair_done(struct btrfs_failed_bio *fbio) 93 { 94 if (atomic_dec_and_test(&fbio->repair_count)) { 95 fbio->bbio->end_io(fbio->bbio); 96 mempool_free(fbio, &btrfs_failed_bio_pool); 97 } 98 } 99 100 static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio, 101 struct btrfs_device *dev) 102 { 103 struct btrfs_failed_bio *fbio = repair_bbio->private; 104 struct btrfs_inode *inode = repair_bbio->inode; 105 struct btrfs_fs_info *fs_info = inode->root->fs_info; 106 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio); 107 int mirror = repair_bbio->mirror_num; 108 109 if (repair_bbio->bio.bi_status || 110 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) { 111 bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ); 112 repair_bbio->bio.bi_iter = repair_bbio->saved_iter; 113 114 mirror = next_repair_mirror(fbio, mirror); 115 if (mirror == fbio->bbio->mirror_num) { 116 btrfs_debug(fs_info, "no mirror left"); 117 fbio->bbio->bio.bi_status = BLK_STS_IOERR; 118 goto done; 119 } 120 121 btrfs_submit_bio(fs_info, &repair_bbio->bio, mirror); 122 return; 123 } 124 125 do { 126 mirror = prev_repair_mirror(fbio, mirror); 127 btrfs_repair_io_failure(fs_info, btrfs_ino(inode), 128 repair_bbio->file_offset, fs_info->sectorsize, 129 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT, 130 bv->bv_page, bv->bv_offset, mirror); 131 } while (mirror != fbio->bbio->mirror_num); 132 133 done: 134 btrfs_repair_done(fbio); 135 bio_put(&repair_bbio->bio); 136 } 137 138 /* 139 * Try to kick off a repair read to the next available mirror for a bad sector. 140 * 141 * This primarily tries to recover good data to serve the actual read request, 142 * but also tries to write the good data back to the bad mirror(s) when a 143 * read succeeded to restore the redundancy. 144 */ 145 static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio, 146 u32 bio_offset, 147 struct bio_vec *bv, 148 struct btrfs_failed_bio *fbio) 149 { 150 struct btrfs_inode *inode = failed_bbio->inode; 151 struct btrfs_fs_info *fs_info = inode->root->fs_info; 152 const u32 sectorsize = fs_info->sectorsize; 153 const u64 logical = (failed_bbio->saved_iter.bi_sector << SECTOR_SHIFT); 154 struct btrfs_bio *repair_bbio; 155 struct bio *repair_bio; 156 int num_copies; 157 int mirror; 158 159 btrfs_debug(fs_info, "repair read error: read error at %llu", 160 failed_bbio->file_offset + bio_offset); 161 162 num_copies = btrfs_num_copies(fs_info, logical, sectorsize); 163 if (num_copies == 1) { 164 btrfs_debug(fs_info, "no copy to repair from"); 165 failed_bbio->bio.bi_status = BLK_STS_IOERR; 166 return fbio; 167 } 168 169 if (!fbio) { 170 fbio = mempool_alloc(&btrfs_failed_bio_pool, GFP_NOFS); 171 fbio->bbio = failed_bbio; 172 fbio->num_copies = num_copies; 173 atomic_set(&fbio->repair_count, 1); 174 } 175 176 atomic_inc(&fbio->repair_count); 177 178 repair_bio = bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, 179 &btrfs_repair_bioset); 180 repair_bio->bi_iter.bi_sector = failed_bbio->saved_iter.bi_sector; 181 bio_add_page(repair_bio, bv->bv_page, bv->bv_len, bv->bv_offset); 182 183 repair_bbio = btrfs_bio(repair_bio); 184 btrfs_bio_init(repair_bbio, failed_bbio->inode, NULL, fbio); 185 repair_bbio->file_offset = failed_bbio->file_offset + bio_offset; 186 187 mirror = next_repair_mirror(fbio, failed_bbio->mirror_num); 188 btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror); 189 btrfs_submit_bio(fs_info, repair_bio, mirror); 190 return fbio; 191 } 192 193 static void btrfs_check_read_bio(struct btrfs_bio *bbio, struct btrfs_device *dev) 194 { 195 struct btrfs_inode *inode = bbio->inode; 196 struct btrfs_fs_info *fs_info = inode->root->fs_info; 197 u32 sectorsize = fs_info->sectorsize; 198 struct bvec_iter *iter = &bbio->saved_iter; 199 blk_status_t status = bbio->bio.bi_status; 200 struct btrfs_failed_bio *fbio = NULL; 201 u32 offset = 0; 202 203 /* 204 * Hand off repair bios to the repair code as there is no upper level 205 * submitter for them. 206 */ 207 if (bbio->bio.bi_pool == &btrfs_repair_bioset) { 208 btrfs_end_repair_bio(bbio, dev); 209 return; 210 } 211 212 /* Clear the I/O error. A failed repair will reset it. */ 213 bbio->bio.bi_status = BLK_STS_OK; 214 215 while (iter->bi_size) { 216 struct bio_vec bv = bio_iter_iovec(&bbio->bio, *iter); 217 218 bv.bv_len = min(bv.bv_len, sectorsize); 219 if (status || !btrfs_data_csum_ok(bbio, dev, offset, &bv)) 220 fbio = repair_one_sector(bbio, offset, &bv, fbio); 221 222 bio_advance_iter_single(&bbio->bio, iter, sectorsize); 223 offset += sectorsize; 224 } 225 226 if (bbio->csum != bbio->csum_inline) 227 kfree(bbio->csum); 228 229 if (fbio) 230 btrfs_repair_done(fbio); 231 else 232 bbio->end_io(bbio); 233 } 234 235 static void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev) 236 { 237 if (!dev || !dev->bdev) 238 return; 239 if (bio->bi_status != BLK_STS_IOERR && bio->bi_status != BLK_STS_TARGET) 240 return; 241 242 if (btrfs_op(bio) == BTRFS_MAP_WRITE) 243 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); 244 if (!(bio->bi_opf & REQ_RAHEAD)) 245 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); 246 if (bio->bi_opf & REQ_PREFLUSH) 247 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_FLUSH_ERRS); 248 } 249 250 static struct workqueue_struct *btrfs_end_io_wq(struct btrfs_fs_info *fs_info, 251 struct bio *bio) 252 { 253 if (bio->bi_opf & REQ_META) 254 return fs_info->endio_meta_workers; 255 return fs_info->endio_workers; 256 } 257 258 static void btrfs_end_bio_work(struct work_struct *work) 259 { 260 struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work); 261 262 /* Metadata reads are checked and repaired by the submitter. */ 263 if (bbio->bio.bi_opf & REQ_META) 264 bbio->end_io(bbio); 265 else 266 btrfs_check_read_bio(bbio, bbio->bio.bi_private); 267 } 268 269 static void btrfs_simple_end_io(struct bio *bio) 270 { 271 struct btrfs_bio *bbio = btrfs_bio(bio); 272 struct btrfs_device *dev = bio->bi_private; 273 struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info; 274 275 btrfs_bio_counter_dec(fs_info); 276 277 if (bio->bi_status) 278 btrfs_log_dev_io_error(bio, dev); 279 280 if (bio_op(bio) == REQ_OP_READ) { 281 INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work); 282 queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work); 283 } else { 284 bbio->end_io(bbio); 285 } 286 } 287 288 static void btrfs_raid56_end_io(struct bio *bio) 289 { 290 struct btrfs_io_context *bioc = bio->bi_private; 291 struct btrfs_bio *bbio = btrfs_bio(bio); 292 293 btrfs_bio_counter_dec(bioc->fs_info); 294 bbio->mirror_num = bioc->mirror_num; 295 if (bio_op(bio) == REQ_OP_READ && !(bbio->bio.bi_opf & REQ_META)) 296 btrfs_check_read_bio(bbio, NULL); 297 else 298 bbio->end_io(bbio); 299 300 btrfs_put_bioc(bioc); 301 } 302 303 static void btrfs_orig_write_end_io(struct bio *bio) 304 { 305 struct btrfs_io_stripe *stripe = bio->bi_private; 306 struct btrfs_io_context *bioc = stripe->bioc; 307 struct btrfs_bio *bbio = btrfs_bio(bio); 308 309 btrfs_bio_counter_dec(bioc->fs_info); 310 311 if (bio->bi_status) { 312 atomic_inc(&bioc->error); 313 btrfs_log_dev_io_error(bio, stripe->dev); 314 } 315 316 /* 317 * Only send an error to the higher layers if it is beyond the tolerance 318 * threshold. 319 */ 320 if (atomic_read(&bioc->error) > bioc->max_errors) 321 bio->bi_status = BLK_STS_IOERR; 322 else 323 bio->bi_status = BLK_STS_OK; 324 325 bbio->end_io(bbio); 326 btrfs_put_bioc(bioc); 327 } 328 329 static void btrfs_clone_write_end_io(struct bio *bio) 330 { 331 struct btrfs_io_stripe *stripe = bio->bi_private; 332 333 if (bio->bi_status) { 334 atomic_inc(&stripe->bioc->error); 335 btrfs_log_dev_io_error(bio, stripe->dev); 336 } 337 338 /* Pass on control to the original bio this one was cloned from */ 339 bio_endio(stripe->bioc->orig_bio); 340 bio_put(bio); 341 } 342 343 static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio) 344 { 345 if (!dev || !dev->bdev || 346 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || 347 (btrfs_op(bio) == BTRFS_MAP_WRITE && 348 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { 349 bio_io_error(bio); 350 return; 351 } 352 353 bio_set_dev(bio, dev->bdev); 354 355 /* 356 * For zone append writing, bi_sector must point the beginning of the 357 * zone 358 */ 359 if (bio_op(bio) == REQ_OP_ZONE_APPEND) { 360 u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT; 361 362 if (btrfs_dev_is_sequential(dev, physical)) { 363 u64 zone_start = round_down(physical, 364 dev->fs_info->zone_size); 365 366 bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT; 367 } else { 368 bio->bi_opf &= ~REQ_OP_ZONE_APPEND; 369 bio->bi_opf |= REQ_OP_WRITE; 370 } 371 } 372 btrfs_debug_in_rcu(dev->fs_info, 373 "%s: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", 374 __func__, bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector, 375 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev), 376 dev->devid, bio->bi_iter.bi_size); 377 378 btrfsic_check_bio(bio); 379 submit_bio(bio); 380 } 381 382 static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr) 383 { 384 struct bio *orig_bio = bioc->orig_bio, *bio; 385 386 ASSERT(bio_op(orig_bio) != REQ_OP_READ); 387 388 /* Reuse the bio embedded into the btrfs_bio for the last mirror */ 389 if (dev_nr == bioc->num_stripes - 1) { 390 bio = orig_bio; 391 bio->bi_end_io = btrfs_orig_write_end_io; 392 } else { 393 bio = bio_alloc_clone(NULL, orig_bio, GFP_NOFS, &fs_bio_set); 394 bio_inc_remaining(orig_bio); 395 bio->bi_end_io = btrfs_clone_write_end_io; 396 } 397 398 bio->bi_private = &bioc->stripes[dev_nr]; 399 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT; 400 bioc->stripes[dev_nr].bioc = bioc; 401 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio); 402 } 403 404 void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror_num) 405 { 406 struct btrfs_bio *bbio = btrfs_bio(bio); 407 u64 logical = bio->bi_iter.bi_sector << 9; 408 u64 length = bio->bi_iter.bi_size; 409 u64 map_length = length; 410 struct btrfs_io_context *bioc = NULL; 411 struct btrfs_io_stripe smap; 412 blk_status_t ret; 413 int error; 414 415 btrfs_bio_counter_inc_blocked(fs_info); 416 error = __btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, 417 &bioc, &smap, &mirror_num, 1); 418 if (error) { 419 ret = errno_to_blk_status(error); 420 goto fail; 421 } 422 423 if (map_length < length) { 424 btrfs_crit(fs_info, 425 "mapping failed logical %llu bio len %llu len %llu", 426 logical, length, map_length); 427 BUG(); 428 } 429 430 /* 431 * Save the iter for the end_io handler and preload the checksums for 432 * data reads. 433 */ 434 if (bio_op(bio) == REQ_OP_READ && !(bio->bi_opf & REQ_META)) { 435 bbio->saved_iter = bio->bi_iter; 436 ret = btrfs_lookup_bio_sums(bbio); 437 if (ret) 438 goto fail; 439 } 440 441 /* Do not leak our private flag into the block layer. */ 442 bio->bi_opf &= ~REQ_BTRFS_ONE_ORDERED; 443 444 if (!bioc) { 445 /* Single mirror read/write fast path */ 446 bbio->mirror_num = mirror_num; 447 bio->bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT; 448 bio->bi_private = smap.dev; 449 bio->bi_end_io = btrfs_simple_end_io; 450 btrfs_submit_dev_bio(smap.dev, bio); 451 } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { 452 /* Parity RAID write or read recovery */ 453 bio->bi_private = bioc; 454 bio->bi_end_io = btrfs_raid56_end_io; 455 if (bio_op(bio) == REQ_OP_READ) 456 raid56_parity_recover(bio, bioc, mirror_num); 457 else 458 raid56_parity_write(bio, bioc); 459 } else { 460 /* Write to multiple mirrors */ 461 int total_devs = bioc->num_stripes; 462 int dev_nr; 463 464 bioc->orig_bio = bio; 465 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) 466 btrfs_submit_mirrored_bio(bioc, dev_nr); 467 } 468 return; 469 470 fail: 471 btrfs_bio_counter_dec(fs_info); 472 btrfs_bio_end_io(bbio, ret); 473 } 474 475 /* 476 * Submit a repair write. 477 * 478 * This bypasses btrfs_submit_bio deliberately, as that writes all copies in a 479 * RAID setup. Here we only want to write the one bad copy, so we do the 480 * mapping ourselves and submit the bio directly. 481 * 482 * The I/O is issued synchronously to block the repair read completion from 483 * freeing the bio. 484 */ 485 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, 486 u64 length, u64 logical, struct page *page, 487 unsigned int pg_offset, int mirror_num) 488 { 489 struct btrfs_device *dev; 490 struct bio_vec bvec; 491 struct bio bio; 492 u64 map_length = 0; 493 u64 sector; 494 struct btrfs_io_context *bioc = NULL; 495 int ret = 0; 496 497 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); 498 BUG_ON(!mirror_num); 499 500 if (btrfs_repair_one_zone(fs_info, logical)) 501 return 0; 502 503 map_length = length; 504 505 /* 506 * Avoid races with device replace and make sure our bioc has devices 507 * associated to its stripes that don't go away while we are doing the 508 * read repair operation. 509 */ 510 btrfs_bio_counter_inc_blocked(fs_info); 511 if (btrfs_is_parity_mirror(fs_info, logical, length)) { 512 /* 513 * Note that we don't use BTRFS_MAP_WRITE because it's supposed 514 * to update all raid stripes, but here we just want to correct 515 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad 516 * stripe's dev and sector. 517 */ 518 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, 519 &map_length, &bioc, 0); 520 if (ret) 521 goto out_counter_dec; 522 ASSERT(bioc->mirror_num == 1); 523 } else { 524 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, 525 &map_length, &bioc, mirror_num); 526 if (ret) 527 goto out_counter_dec; 528 /* 529 * This happens when dev-replace is also running, and the 530 * mirror_num indicates the dev-replace target. 531 * 532 * In this case, we don't need to do anything, as the read 533 * error just means the replace progress hasn't reached our 534 * read range, and later replace routine would handle it well. 535 */ 536 if (mirror_num != bioc->mirror_num) 537 goto out_counter_dec; 538 } 539 540 sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9; 541 dev = bioc->stripes[bioc->mirror_num - 1].dev; 542 btrfs_put_bioc(bioc); 543 544 if (!dev || !dev->bdev || 545 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { 546 ret = -EIO; 547 goto out_counter_dec; 548 } 549 550 bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC); 551 bio.bi_iter.bi_sector = sector; 552 __bio_add_page(&bio, page, length, pg_offset); 553 554 btrfsic_check_bio(&bio); 555 ret = submit_bio_wait(&bio); 556 if (ret) { 557 /* try to remap that extent elsewhere? */ 558 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); 559 goto out_bio_uninit; 560 } 561 562 btrfs_info_rl_in_rcu(fs_info, 563 "read error corrected: ino %llu off %llu (dev %s sector %llu)", 564 ino, start, btrfs_dev_name(dev), sector); 565 ret = 0; 566 567 out_bio_uninit: 568 bio_uninit(&bio); 569 out_counter_dec: 570 btrfs_bio_counter_dec(fs_info); 571 return ret; 572 } 573 574 int __init btrfs_bioset_init(void) 575 { 576 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE, 577 offsetof(struct btrfs_bio, bio), 578 BIOSET_NEED_BVECS)) 579 return -ENOMEM; 580 if (bioset_init(&btrfs_repair_bioset, BIO_POOL_SIZE, 581 offsetof(struct btrfs_bio, bio), 582 BIOSET_NEED_BVECS)) 583 goto out_free_bioset; 584 if (mempool_init_kmalloc_pool(&btrfs_failed_bio_pool, BIO_POOL_SIZE, 585 sizeof(struct btrfs_failed_bio))) 586 goto out_free_repair_bioset; 587 return 0; 588 589 out_free_repair_bioset: 590 bioset_exit(&btrfs_repair_bioset); 591 out_free_bioset: 592 bioset_exit(&btrfs_bioset); 593 return -ENOMEM; 594 } 595 596 void __cold btrfs_bioset_exit(void) 597 { 598 mempool_exit(&btrfs_failed_bio_pool); 599 bioset_exit(&btrfs_repair_bioset); 600 bioset_exit(&btrfs_bioset); 601 } 602