1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (C) 2008-2010 Lawrence Livermore National Security, LLC. 23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>. 25 * LLNL-CODE-403049. 26 * Copyright (c) 2012, 2019 by Delphix. All rights reserved. 27 */ 28 29 #include <sys/zfs_context.h> 30 #include <sys/spa_impl.h> 31 #include <sys/vdev_disk.h> 32 #include <sys/vdev_impl.h> 33 #include <sys/vdev_trim.h> 34 #include <sys/abd.h> 35 #include <sys/fs/zfs.h> 36 #include <sys/zio.h> 37 #include <linux/blkpg.h> 38 #include <linux/msdos_fs.h> 39 #include <linux/vfs_compat.h> 40 #ifdef HAVE_LINUX_BLK_CGROUP_HEADER 41 #include <linux/blk-cgroup.h> 42 #endif 43 44 typedef struct vdev_disk { 45 struct block_device *vd_bdev; 46 krwlock_t vd_lock; 47 } vdev_disk_t; 48 49 /* 50 * Unique identifier for the exclusive vdev holder. 51 */ 52 static void *zfs_vdev_holder = VDEV_HOLDER; 53 54 /* 55 * Wait up to zfs_vdev_open_timeout_ms milliseconds before determining the 56 * device is missing. The missing path may be transient since the links 57 * can be briefly removed and recreated in response to udev events. 58 */ 59 static unsigned zfs_vdev_open_timeout_ms = 1000; 60 61 /* 62 * Size of the "reserved" partition, in blocks. 63 */ 64 #define EFI_MIN_RESV_SIZE (16 * 1024) 65 66 /* 67 * Virtual device vector for disks. 68 */ 69 typedef struct dio_request { 70 zio_t *dr_zio; /* Parent ZIO */ 71 atomic_t dr_ref; /* References */ 72 int dr_error; /* Bio error */ 73 int dr_bio_count; /* Count of bio's */ 74 struct bio *dr_bio[0]; /* Attached bio's */ 75 } dio_request_t; 76 77 static fmode_t 78 vdev_bdev_mode(spa_mode_t spa_mode) 79 { 80 fmode_t mode = 0; 81 82 if (spa_mode & SPA_MODE_READ) 83 mode |= FMODE_READ; 84 85 if (spa_mode & SPA_MODE_WRITE) 86 mode |= FMODE_WRITE; 87 88 return (mode); 89 } 90 91 /* 92 * Returns the usable capacity (in bytes) for the partition or disk. 93 */ 94 static uint64_t 95 bdev_capacity(struct block_device *bdev) 96 { 97 return (i_size_read(bdev->bd_inode)); 98 } 99 100 #if !defined(HAVE_BDEV_WHOLE) 101 static inline struct block_device * 102 bdev_whole(struct block_device *bdev) 103 { 104 return (bdev->bd_contains); 105 } 106 #endif 107 108 #if defined(HAVE_BDEVNAME) 109 #define vdev_bdevname(bdev, name) bdevname(bdev, name) 110 #else 111 static inline void 112 vdev_bdevname(struct block_device *bdev, char *name) 113 { 114 snprintf(name, BDEVNAME_SIZE, "%pg", bdev); 115 } 116 #endif 117 118 /* 119 * Returns the maximum expansion capacity of the block device (in bytes). 120 * 121 * It is possible to expand a vdev when it has been created as a wholedisk 122 * and the containing block device has increased in capacity. Or when the 123 * partition containing the pool has been manually increased in size. 124 * 125 * This function is only responsible for calculating the potential expansion 126 * size so it can be reported by 'zpool list'. The efi_use_whole_disk() is 127 * responsible for verifying the expected partition layout in the wholedisk 128 * case, and updating the partition table if appropriate. Once the partition 129 * size has been increased the additional capacity will be visible using 130 * bdev_capacity(). 131 * 132 * The returned maximum expansion capacity is always expected to be larger, or 133 * at the very least equal, to its usable capacity to prevent overestimating 134 * the pool expandsize. 135 */ 136 static uint64_t 137 bdev_max_capacity(struct block_device *bdev, uint64_t wholedisk) 138 { 139 uint64_t psize; 140 int64_t available; 141 142 if (wholedisk && bdev != bdev_whole(bdev)) { 143 /* 144 * When reporting maximum expansion capacity for a wholedisk 145 * deduct any capacity which is expected to be lost due to 146 * alignment restrictions. Over reporting this value isn't 147 * harmful and would only result in slightly less capacity 148 * than expected post expansion. 149 * The estimated available space may be slightly smaller than 150 * bdev_capacity() for devices where the number of sectors is 151 * not a multiple of the alignment size and the partition layout 152 * is keeping less than PARTITION_END_ALIGNMENT bytes after the 153 * "reserved" EFI partition: in such cases return the device 154 * usable capacity. 155 */ 156 available = i_size_read(bdev_whole(bdev)->bd_inode) - 157 ((EFI_MIN_RESV_SIZE + NEW_START_BLOCK + 158 PARTITION_END_ALIGNMENT) << SECTOR_BITS); 159 psize = MAX(available, bdev_capacity(bdev)); 160 } else { 161 psize = bdev_capacity(bdev); 162 } 163 164 return (psize); 165 } 166 167 static void 168 vdev_disk_error(zio_t *zio) 169 { 170 /* 171 * This function can be called in interrupt context, for instance while 172 * handling IRQs coming from a misbehaving disk device; use printk() 173 * which is safe from any context. 174 */ 175 printk(KERN_WARNING "zio pool=%s vdev=%s error=%d type=%d " 176 "offset=%llu size=%llu flags=%x\n", spa_name(zio->io_spa), 177 zio->io_vd->vdev_path, zio->io_error, zio->io_type, 178 (u_longlong_t)zio->io_offset, (u_longlong_t)zio->io_size, 179 zio->io_flags); 180 } 181 182 static int 183 vdev_disk_open(vdev_t *v, uint64_t *psize, uint64_t *max_psize, 184 uint64_t *logical_ashift, uint64_t *physical_ashift) 185 { 186 struct block_device *bdev; 187 fmode_t mode = vdev_bdev_mode(spa_mode(v->vdev_spa)); 188 hrtime_t timeout = MSEC2NSEC(zfs_vdev_open_timeout_ms); 189 vdev_disk_t *vd; 190 191 /* Must have a pathname and it must be absolute. */ 192 if (v->vdev_path == NULL || v->vdev_path[0] != '/') { 193 v->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 194 vdev_dbgmsg(v, "invalid vdev_path"); 195 return (SET_ERROR(EINVAL)); 196 } 197 198 /* 199 * Reopen the device if it is currently open. When expanding a 200 * partition force re-scanning the partition table if userland 201 * did not take care of this already. We need to do this while closed 202 * in order to get an accurate updated block device size. Then 203 * since udev may need to recreate the device links increase the 204 * open retry timeout before reporting the device as unavailable. 205 */ 206 vd = v->vdev_tsd; 207 if (vd) { 208 char disk_name[BDEVNAME_SIZE + 6] = "/dev/"; 209 boolean_t reread_part = B_FALSE; 210 211 rw_enter(&vd->vd_lock, RW_WRITER); 212 bdev = vd->vd_bdev; 213 vd->vd_bdev = NULL; 214 215 if (bdev) { 216 if (v->vdev_expanding && bdev != bdev_whole(bdev)) { 217 vdev_bdevname(bdev_whole(bdev), disk_name + 5); 218 /* 219 * If userland has BLKPG_RESIZE_PARTITION, 220 * then it should have updated the partition 221 * table already. We can detect this by 222 * comparing our current physical size 223 * with that of the device. If they are 224 * the same, then we must not have 225 * BLKPG_RESIZE_PARTITION or it failed to 226 * update the partition table online. We 227 * fallback to rescanning the partition 228 * table from the kernel below. However, 229 * if the capacity already reflects the 230 * updated partition, then we skip 231 * rescanning the partition table here. 232 */ 233 if (v->vdev_psize == bdev_capacity(bdev)) 234 reread_part = B_TRUE; 235 } 236 237 blkdev_put(bdev, mode | FMODE_EXCL); 238 } 239 240 if (reread_part) { 241 bdev = blkdev_get_by_path(disk_name, mode | FMODE_EXCL, 242 zfs_vdev_holder); 243 if (!IS_ERR(bdev)) { 244 int error = vdev_bdev_reread_part(bdev); 245 blkdev_put(bdev, mode | FMODE_EXCL); 246 if (error == 0) { 247 timeout = MSEC2NSEC( 248 zfs_vdev_open_timeout_ms * 2); 249 } 250 } 251 } 252 } else { 253 vd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 254 255 rw_init(&vd->vd_lock, NULL, RW_DEFAULT, NULL); 256 rw_enter(&vd->vd_lock, RW_WRITER); 257 } 258 259 /* 260 * Devices are always opened by the path provided at configuration 261 * time. This means that if the provided path is a udev by-id path 262 * then drives may be re-cabled without an issue. If the provided 263 * path is a udev by-path path, then the physical location information 264 * will be preserved. This can be critical for more complicated 265 * configurations where drives are located in specific physical 266 * locations to maximize the systems tolerance to component failure. 267 * 268 * Alternatively, you can provide your own udev rule to flexibly map 269 * the drives as you see fit. It is not advised that you use the 270 * /dev/[hd]d devices which may be reordered due to probing order. 271 * Devices in the wrong locations will be detected by the higher 272 * level vdev validation. 273 * 274 * The specified paths may be briefly removed and recreated in 275 * response to udev events. This should be exceptionally unlikely 276 * because the zpool command makes every effort to verify these paths 277 * have already settled prior to reaching this point. Therefore, 278 * a ENOENT failure at this point is highly likely to be transient 279 * and it is reasonable to sleep and retry before giving up. In 280 * practice delays have been observed to be on the order of 100ms. 281 * 282 * When ERESTARTSYS is returned it indicates the block device is 283 * a zvol which could not be opened due to the deadlock detection 284 * logic in zvol_open(). Extend the timeout and retry the open 285 * subsequent attempts are expected to eventually succeed. 286 */ 287 hrtime_t start = gethrtime(); 288 bdev = ERR_PTR(-ENXIO); 289 while (IS_ERR(bdev) && ((gethrtime() - start) < timeout)) { 290 bdev = blkdev_get_by_path(v->vdev_path, mode | FMODE_EXCL, 291 zfs_vdev_holder); 292 if (unlikely(PTR_ERR(bdev) == -ENOENT)) { 293 schedule_timeout(MSEC_TO_TICK(10)); 294 } else if (unlikely(PTR_ERR(bdev) == -ERESTARTSYS)) { 295 timeout = MSEC2NSEC(zfs_vdev_open_timeout_ms * 10); 296 continue; 297 } else if (IS_ERR(bdev)) { 298 break; 299 } 300 } 301 302 if (IS_ERR(bdev)) { 303 int error = -PTR_ERR(bdev); 304 vdev_dbgmsg(v, "open error=%d timeout=%llu/%llu", error, 305 (u_longlong_t)(gethrtime() - start), 306 (u_longlong_t)timeout); 307 vd->vd_bdev = NULL; 308 v->vdev_tsd = vd; 309 rw_exit(&vd->vd_lock); 310 return (SET_ERROR(error)); 311 } else { 312 vd->vd_bdev = bdev; 313 v->vdev_tsd = vd; 314 rw_exit(&vd->vd_lock); 315 } 316 317 /* Determine the physical block size */ 318 int physical_block_size = bdev_physical_block_size(vd->vd_bdev); 319 320 /* Determine the logical block size */ 321 int logical_block_size = bdev_logical_block_size(vd->vd_bdev); 322 323 /* Clear the nowritecache bit, causes vdev_reopen() to try again. */ 324 v->vdev_nowritecache = B_FALSE; 325 326 /* Set when device reports it supports TRIM. */ 327 v->vdev_has_trim = bdev_discard_supported(vd->vd_bdev); 328 329 /* Set when device reports it supports secure TRIM. */ 330 v->vdev_has_securetrim = bdev_secure_discard_supported(vd->vd_bdev); 331 332 /* Inform the ZIO pipeline that we are non-rotational */ 333 v->vdev_nonrot = blk_queue_nonrot(bdev_get_queue(vd->vd_bdev)); 334 335 /* Physical volume size in bytes for the partition */ 336 *psize = bdev_capacity(vd->vd_bdev); 337 338 /* Physical volume size in bytes including possible expansion space */ 339 *max_psize = bdev_max_capacity(vd->vd_bdev, v->vdev_wholedisk); 340 341 /* Based on the minimum sector size set the block size */ 342 *physical_ashift = highbit64(MAX(physical_block_size, 343 SPA_MINBLOCKSIZE)) - 1; 344 345 *logical_ashift = highbit64(MAX(logical_block_size, 346 SPA_MINBLOCKSIZE)) - 1; 347 348 return (0); 349 } 350 351 static void 352 vdev_disk_close(vdev_t *v) 353 { 354 vdev_disk_t *vd = v->vdev_tsd; 355 356 if (v->vdev_reopening || vd == NULL) 357 return; 358 359 if (vd->vd_bdev != NULL) { 360 blkdev_put(vd->vd_bdev, 361 vdev_bdev_mode(spa_mode(v->vdev_spa)) | FMODE_EXCL); 362 } 363 364 rw_destroy(&vd->vd_lock); 365 kmem_free(vd, sizeof (vdev_disk_t)); 366 v->vdev_tsd = NULL; 367 } 368 369 static dio_request_t * 370 vdev_disk_dio_alloc(int bio_count) 371 { 372 dio_request_t *dr = kmem_zalloc(sizeof (dio_request_t) + 373 sizeof (struct bio *) * bio_count, KM_SLEEP); 374 atomic_set(&dr->dr_ref, 0); 375 dr->dr_bio_count = bio_count; 376 dr->dr_error = 0; 377 378 for (int i = 0; i < dr->dr_bio_count; i++) 379 dr->dr_bio[i] = NULL; 380 381 return (dr); 382 } 383 384 static void 385 vdev_disk_dio_free(dio_request_t *dr) 386 { 387 int i; 388 389 for (i = 0; i < dr->dr_bio_count; i++) 390 if (dr->dr_bio[i]) 391 bio_put(dr->dr_bio[i]); 392 393 kmem_free(dr, sizeof (dio_request_t) + 394 sizeof (struct bio *) * dr->dr_bio_count); 395 } 396 397 static void 398 vdev_disk_dio_get(dio_request_t *dr) 399 { 400 atomic_inc(&dr->dr_ref); 401 } 402 403 static int 404 vdev_disk_dio_put(dio_request_t *dr) 405 { 406 int rc = atomic_dec_return(&dr->dr_ref); 407 408 /* 409 * Free the dio_request when the last reference is dropped and 410 * ensure zio_interpret is called only once with the correct zio 411 */ 412 if (rc == 0) { 413 zio_t *zio = dr->dr_zio; 414 int error = dr->dr_error; 415 416 vdev_disk_dio_free(dr); 417 418 if (zio) { 419 zio->io_error = error; 420 ASSERT3S(zio->io_error, >=, 0); 421 if (zio->io_error) 422 vdev_disk_error(zio); 423 424 zio_delay_interrupt(zio); 425 } 426 } 427 428 return (rc); 429 } 430 431 BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error) 432 { 433 dio_request_t *dr = bio->bi_private; 434 int rc; 435 436 if (dr->dr_error == 0) { 437 #ifdef HAVE_1ARG_BIO_END_IO_T 438 dr->dr_error = BIO_END_IO_ERROR(bio); 439 #else 440 if (error) 441 dr->dr_error = -(error); 442 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) 443 dr->dr_error = EIO; 444 #endif 445 } 446 447 /* Drop reference acquired by __vdev_disk_physio */ 448 rc = vdev_disk_dio_put(dr); 449 } 450 451 static inline void 452 vdev_submit_bio_impl(struct bio *bio) 453 { 454 #ifdef HAVE_1ARG_SUBMIT_BIO 455 (void) submit_bio(bio); 456 #else 457 (void) submit_bio(bio_data_dir(bio), bio); 458 #endif 459 } 460 461 /* 462 * preempt_schedule_notrace is GPL-only which breaks the ZFS build, so 463 * replace it with preempt_schedule under the following condition: 464 */ 465 #if defined(CONFIG_ARM64) && \ 466 defined(CONFIG_PREEMPTION) && \ 467 defined(CONFIG_BLK_CGROUP) 468 #define preempt_schedule_notrace(x) preempt_schedule(x) 469 #endif 470 471 /* 472 * As for the Linux 5.18 kernel bio_alloc() expects a block_device struct 473 * as an argument removing the need to set it with bio_set_dev(). This 474 * removes the need for all of the following compatibility code. 475 */ 476 #if !defined(HAVE_BIO_ALLOC_4ARG) 477 478 #ifdef HAVE_BIO_SET_DEV 479 #if defined(CONFIG_BLK_CGROUP) && defined(HAVE_BIO_SET_DEV_GPL_ONLY) 480 /* 481 * The Linux 5.5 kernel updated percpu_ref_tryget() which is inlined by 482 * blkg_tryget() to use rcu_read_lock() instead of rcu_read_lock_sched(). 483 * As a side effect the function was converted to GPL-only. Define our 484 * own version when needed which uses rcu_read_lock_sched(). 485 * 486 * The Linux 5.17 kernel split linux/blk-cgroup.h into a private and a public 487 * part, moving blkg_tryget into the private one. Define our own version. 488 */ 489 #if defined(HAVE_BLKG_TRYGET_GPL_ONLY) || !defined(HAVE_BLKG_TRYGET) 490 static inline bool 491 vdev_blkg_tryget(struct blkcg_gq *blkg) 492 { 493 struct percpu_ref *ref = &blkg->refcnt; 494 unsigned long __percpu *count; 495 bool rc; 496 497 rcu_read_lock_sched(); 498 499 if (__ref_is_percpu(ref, &count)) { 500 this_cpu_inc(*count); 501 rc = true; 502 } else { 503 #ifdef ZFS_PERCPU_REF_COUNT_IN_DATA 504 rc = atomic_long_inc_not_zero(&ref->data->count); 505 #else 506 rc = atomic_long_inc_not_zero(&ref->count); 507 #endif 508 } 509 510 rcu_read_unlock_sched(); 511 512 return (rc); 513 } 514 #else 515 #define vdev_blkg_tryget(bg) blkg_tryget(bg) 516 #endif 517 #ifdef HAVE_BIO_SET_DEV_MACRO 518 /* 519 * The Linux 5.0 kernel updated the bio_set_dev() macro so it calls the 520 * GPL-only bio_associate_blkg() symbol thus inadvertently converting 521 * the entire macro. Provide a minimal version which always assigns the 522 * request queue's root_blkg to the bio. 523 */ 524 static inline void 525 vdev_bio_associate_blkg(struct bio *bio) 526 { 527 #if defined(HAVE_BIO_BDEV_DISK) 528 struct request_queue *q = bio->bi_bdev->bd_disk->queue; 529 #else 530 struct request_queue *q = bio->bi_disk->queue; 531 #endif 532 533 ASSERT3P(q, !=, NULL); 534 ASSERT3P(bio->bi_blkg, ==, NULL); 535 536 if (q->root_blkg && vdev_blkg_tryget(q->root_blkg)) 537 bio->bi_blkg = q->root_blkg; 538 } 539 540 #define bio_associate_blkg vdev_bio_associate_blkg 541 #else 542 static inline void 543 vdev_bio_set_dev(struct bio *bio, struct block_device *bdev) 544 { 545 #if defined(HAVE_BIO_BDEV_DISK) 546 struct request_queue *q = bdev->bd_disk->queue; 547 #else 548 struct request_queue *q = bio->bi_disk->queue; 549 #endif 550 bio_clear_flag(bio, BIO_REMAPPED); 551 if (bio->bi_bdev != bdev) 552 bio_clear_flag(bio, BIO_THROTTLED); 553 bio->bi_bdev = bdev; 554 555 ASSERT3P(q, !=, NULL); 556 ASSERT3P(bio->bi_blkg, ==, NULL); 557 558 if (q->root_blkg && vdev_blkg_tryget(q->root_blkg)) 559 bio->bi_blkg = q->root_blkg; 560 } 561 #define bio_set_dev vdev_bio_set_dev 562 #endif 563 #endif 564 #else 565 /* 566 * Provide a bio_set_dev() helper macro for pre-Linux 4.14 kernels. 567 */ 568 static inline void 569 bio_set_dev(struct bio *bio, struct block_device *bdev) 570 { 571 bio->bi_bdev = bdev; 572 } 573 #endif /* HAVE_BIO_SET_DEV */ 574 #endif /* !HAVE_BIO_ALLOC_4ARG */ 575 576 static inline void 577 vdev_submit_bio(struct bio *bio) 578 { 579 struct bio_list *bio_list = current->bio_list; 580 current->bio_list = NULL; 581 vdev_submit_bio_impl(bio); 582 current->bio_list = bio_list; 583 } 584 585 static inline struct bio * 586 vdev_bio_alloc(struct block_device *bdev, gfp_t gfp_mask, 587 unsigned short nr_vecs) 588 { 589 struct bio *bio; 590 591 #ifdef HAVE_BIO_ALLOC_4ARG 592 bio = bio_alloc(bdev, nr_vecs, 0, gfp_mask); 593 #else 594 bio = bio_alloc(gfp_mask, nr_vecs); 595 if (likely(bio != NULL)) 596 bio_set_dev(bio, bdev); 597 #endif 598 599 return (bio); 600 } 601 602 static inline unsigned int 603 vdev_bio_max_segs(zio_t *zio, int bio_size, uint64_t abd_offset) 604 { 605 unsigned long nr_segs = abd_nr_pages_off(zio->io_abd, 606 bio_size, abd_offset); 607 608 #ifdef HAVE_BIO_MAX_SEGS 609 return (bio_max_segs(nr_segs)); 610 #else 611 return (MIN(nr_segs, BIO_MAX_PAGES)); 612 #endif 613 } 614 615 static int 616 __vdev_disk_physio(struct block_device *bdev, zio_t *zio, 617 size_t io_size, uint64_t io_offset, int rw, int flags) 618 { 619 dio_request_t *dr; 620 uint64_t abd_offset; 621 uint64_t bio_offset; 622 int bio_size; 623 int bio_count = 16; 624 int error = 0; 625 struct blk_plug plug; 626 unsigned short nr_vecs; 627 628 /* 629 * Accessing outside the block device is never allowed. 630 */ 631 if (io_offset + io_size > bdev->bd_inode->i_size) { 632 vdev_dbgmsg(zio->io_vd, 633 "Illegal access %llu size %llu, device size %llu", 634 (u_longlong_t)io_offset, 635 (u_longlong_t)io_size, 636 (u_longlong_t)i_size_read(bdev->bd_inode)); 637 return (SET_ERROR(EIO)); 638 } 639 640 retry: 641 dr = vdev_disk_dio_alloc(bio_count); 642 643 if (zio && !(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 644 bio_set_flags_failfast(bdev, &flags); 645 646 dr->dr_zio = zio; 647 648 /* 649 * Since bio's can have up to BIO_MAX_PAGES=256 iovec's, each of which 650 * is at least 512 bytes and at most PAGESIZE (typically 4K), one bio 651 * can cover at least 128KB and at most 1MB. When the required number 652 * of iovec's exceeds this, we are forced to break the IO in multiple 653 * bio's and wait for them all to complete. This is likely if the 654 * recordsize property is increased beyond 1MB. The default 655 * bio_count=16 should typically accommodate the maximum-size zio of 656 * 16MB. 657 */ 658 659 abd_offset = 0; 660 bio_offset = io_offset; 661 bio_size = io_size; 662 for (int i = 0; i <= dr->dr_bio_count; i++) { 663 664 /* Finished constructing bio's for given buffer */ 665 if (bio_size <= 0) 666 break; 667 668 /* 669 * If additional bio's are required, we have to retry, but 670 * this should be rare - see the comment above. 671 */ 672 if (dr->dr_bio_count == i) { 673 vdev_disk_dio_free(dr); 674 bio_count *= 2; 675 goto retry; 676 } 677 678 nr_vecs = vdev_bio_max_segs(zio, bio_size, abd_offset); 679 dr->dr_bio[i] = vdev_bio_alloc(bdev, GFP_NOIO, nr_vecs); 680 if (unlikely(dr->dr_bio[i] == NULL)) { 681 vdev_disk_dio_free(dr); 682 return (SET_ERROR(ENOMEM)); 683 } 684 685 /* Matching put called by vdev_disk_physio_completion */ 686 vdev_disk_dio_get(dr); 687 688 BIO_BI_SECTOR(dr->dr_bio[i]) = bio_offset >> 9; 689 dr->dr_bio[i]->bi_end_io = vdev_disk_physio_completion; 690 dr->dr_bio[i]->bi_private = dr; 691 bio_set_op_attrs(dr->dr_bio[i], rw, flags); 692 693 /* Remaining size is returned to become the new size */ 694 bio_size = abd_bio_map_off(dr->dr_bio[i], zio->io_abd, 695 bio_size, abd_offset); 696 697 /* Advance in buffer and construct another bio if needed */ 698 abd_offset += BIO_BI_SIZE(dr->dr_bio[i]); 699 bio_offset += BIO_BI_SIZE(dr->dr_bio[i]); 700 } 701 702 /* Extra reference to protect dio_request during vdev_submit_bio */ 703 vdev_disk_dio_get(dr); 704 705 if (dr->dr_bio_count > 1) 706 blk_start_plug(&plug); 707 708 /* Submit all bio's associated with this dio */ 709 for (int i = 0; i < dr->dr_bio_count; i++) { 710 if (dr->dr_bio[i]) 711 vdev_submit_bio(dr->dr_bio[i]); 712 } 713 714 if (dr->dr_bio_count > 1) 715 blk_finish_plug(&plug); 716 717 (void) vdev_disk_dio_put(dr); 718 719 return (error); 720 } 721 722 BIO_END_IO_PROTO(vdev_disk_io_flush_completion, bio, error) 723 { 724 zio_t *zio = bio->bi_private; 725 #ifdef HAVE_1ARG_BIO_END_IO_T 726 zio->io_error = BIO_END_IO_ERROR(bio); 727 #else 728 zio->io_error = -error; 729 #endif 730 731 if (zio->io_error && (zio->io_error == EOPNOTSUPP)) 732 zio->io_vd->vdev_nowritecache = B_TRUE; 733 734 bio_put(bio); 735 ASSERT3S(zio->io_error, >=, 0); 736 if (zio->io_error) 737 vdev_disk_error(zio); 738 zio_interrupt(zio); 739 } 740 741 static int 742 vdev_disk_io_flush(struct block_device *bdev, zio_t *zio) 743 { 744 struct request_queue *q; 745 struct bio *bio; 746 747 q = bdev_get_queue(bdev); 748 if (!q) 749 return (SET_ERROR(ENXIO)); 750 751 bio = vdev_bio_alloc(bdev, GFP_NOIO, 0); 752 if (unlikely(bio == NULL)) 753 return (SET_ERROR(ENOMEM)); 754 755 bio->bi_end_io = vdev_disk_io_flush_completion; 756 bio->bi_private = zio; 757 bio_set_flush(bio); 758 vdev_submit_bio(bio); 759 invalidate_bdev(bdev); 760 761 return (0); 762 } 763 764 static int 765 vdev_disk_io_trim(zio_t *zio) 766 { 767 vdev_t *v = zio->io_vd; 768 vdev_disk_t *vd = v->vdev_tsd; 769 770 #if defined(HAVE_BLKDEV_ISSUE_SECURE_ERASE) 771 if (zio->io_trim_flags & ZIO_TRIM_SECURE) { 772 return (-blkdev_issue_secure_erase(vd->vd_bdev, 773 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS)); 774 } else { 775 return (-blkdev_issue_discard(vd->vd_bdev, 776 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS)); 777 } 778 #elif defined(HAVE_BLKDEV_ISSUE_DISCARD) 779 unsigned long trim_flags = 0; 780 #if defined(BLKDEV_DISCARD_SECURE) 781 if (zio->io_trim_flags & ZIO_TRIM_SECURE) 782 trim_flags |= BLKDEV_DISCARD_SECURE; 783 #endif 784 return (-blkdev_issue_discard(vd->vd_bdev, 785 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS, trim_flags)); 786 #else 787 #error "Unsupported kernel" 788 #endif 789 } 790 791 static void 792 vdev_disk_io_start(zio_t *zio) 793 { 794 vdev_t *v = zio->io_vd; 795 vdev_disk_t *vd = v->vdev_tsd; 796 int rw, error; 797 798 /* 799 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 800 * Nothing to be done here but return failure. 801 */ 802 if (vd == NULL) { 803 zio->io_error = ENXIO; 804 zio_interrupt(zio); 805 return; 806 } 807 808 rw_enter(&vd->vd_lock, RW_READER); 809 810 /* 811 * If the vdev is closed, it's likely due to a failed reopen and is 812 * in the UNAVAIL state. Nothing to be done here but return failure. 813 */ 814 if (vd->vd_bdev == NULL) { 815 rw_exit(&vd->vd_lock); 816 zio->io_error = ENXIO; 817 zio_interrupt(zio); 818 return; 819 } 820 821 switch (zio->io_type) { 822 case ZIO_TYPE_IOCTL: 823 824 if (!vdev_readable(v)) { 825 rw_exit(&vd->vd_lock); 826 zio->io_error = SET_ERROR(ENXIO); 827 zio_interrupt(zio); 828 return; 829 } 830 831 switch (zio->io_cmd) { 832 case DKIOCFLUSHWRITECACHE: 833 834 if (zfs_nocacheflush) 835 break; 836 837 if (v->vdev_nowritecache) { 838 zio->io_error = SET_ERROR(ENOTSUP); 839 break; 840 } 841 842 error = vdev_disk_io_flush(vd->vd_bdev, zio); 843 if (error == 0) { 844 rw_exit(&vd->vd_lock); 845 return; 846 } 847 848 zio->io_error = error; 849 850 break; 851 852 default: 853 zio->io_error = SET_ERROR(ENOTSUP); 854 } 855 856 rw_exit(&vd->vd_lock); 857 zio_execute(zio); 858 return; 859 case ZIO_TYPE_WRITE: 860 rw = WRITE; 861 break; 862 863 case ZIO_TYPE_READ: 864 rw = READ; 865 break; 866 867 case ZIO_TYPE_TRIM: 868 zio->io_error = vdev_disk_io_trim(zio); 869 rw_exit(&vd->vd_lock); 870 zio_interrupt(zio); 871 return; 872 873 default: 874 rw_exit(&vd->vd_lock); 875 zio->io_error = SET_ERROR(ENOTSUP); 876 zio_interrupt(zio); 877 return; 878 } 879 880 zio->io_target_timestamp = zio_handle_io_delay(zio); 881 error = __vdev_disk_physio(vd->vd_bdev, zio, 882 zio->io_size, zio->io_offset, rw, 0); 883 rw_exit(&vd->vd_lock); 884 885 if (error) { 886 zio->io_error = error; 887 zio_interrupt(zio); 888 return; 889 } 890 } 891 892 static void 893 vdev_disk_io_done(zio_t *zio) 894 { 895 /* 896 * If the device returned EIO, we revalidate the media. If it is 897 * determined the media has changed this triggers the asynchronous 898 * removal of the device from the configuration. 899 */ 900 if (zio->io_error == EIO) { 901 vdev_t *v = zio->io_vd; 902 vdev_disk_t *vd = v->vdev_tsd; 903 904 if (zfs_check_media_change(vd->vd_bdev)) { 905 invalidate_bdev(vd->vd_bdev); 906 v->vdev_remove_wanted = B_TRUE; 907 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 908 } 909 } 910 } 911 912 static void 913 vdev_disk_hold(vdev_t *vd) 914 { 915 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 916 917 /* We must have a pathname, and it must be absolute. */ 918 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') 919 return; 920 921 /* 922 * Only prefetch path and devid info if the device has 923 * never been opened. 924 */ 925 if (vd->vdev_tsd != NULL) 926 return; 927 928 } 929 930 static void 931 vdev_disk_rele(vdev_t *vd) 932 { 933 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 934 935 /* XXX: Implement me as a vnode rele for the device */ 936 } 937 938 vdev_ops_t vdev_disk_ops = { 939 .vdev_op_init = NULL, 940 .vdev_op_fini = NULL, 941 .vdev_op_open = vdev_disk_open, 942 .vdev_op_close = vdev_disk_close, 943 .vdev_op_asize = vdev_default_asize, 944 .vdev_op_min_asize = vdev_default_min_asize, 945 .vdev_op_min_alloc = NULL, 946 .vdev_op_io_start = vdev_disk_io_start, 947 .vdev_op_io_done = vdev_disk_io_done, 948 .vdev_op_state_change = NULL, 949 .vdev_op_need_resilver = NULL, 950 .vdev_op_hold = vdev_disk_hold, 951 .vdev_op_rele = vdev_disk_rele, 952 .vdev_op_remap = NULL, 953 .vdev_op_xlate = vdev_default_xlate, 954 .vdev_op_rebuild_asize = NULL, 955 .vdev_op_metaslab_init = NULL, 956 .vdev_op_config_generate = NULL, 957 .vdev_op_nparity = NULL, 958 .vdev_op_ndisks = NULL, 959 .vdev_op_type = VDEV_TYPE_DISK, /* name of this vdev type */ 960 .vdev_op_leaf = B_TRUE /* leaf vdev */ 961 }; 962 963 /* 964 * The zfs_vdev_scheduler module option has been deprecated. Setting this 965 * value no longer has any effect. It has not yet been entirely removed 966 * to allow the module to be loaded if this option is specified in the 967 * /etc/modprobe.d/zfs.conf file. The following warning will be logged. 968 */ 969 static int 970 param_set_vdev_scheduler(const char *val, zfs_kernel_param_t *kp) 971 { 972 int error = param_set_charp(val, kp); 973 if (error == 0) { 974 printk(KERN_INFO "The 'zfs_vdev_scheduler' module option " 975 "is not supported.\n"); 976 } 977 978 return (error); 979 } 980 981 static const char *zfs_vdev_scheduler = "unused"; 982 module_param_call(zfs_vdev_scheduler, param_set_vdev_scheduler, 983 param_get_charp, &zfs_vdev_scheduler, 0644); 984 MODULE_PARM_DESC(zfs_vdev_scheduler, "I/O scheduler"); 985 986 int 987 param_set_min_auto_ashift(const char *buf, zfs_kernel_param_t *kp) 988 { 989 uint64_t val; 990 int error; 991 992 error = kstrtoull(buf, 0, &val); 993 if (error < 0) 994 return (SET_ERROR(error)); 995 996 if (val < ASHIFT_MIN || val > zfs_vdev_max_auto_ashift) 997 return (SET_ERROR(-EINVAL)); 998 999 error = param_set_ulong(buf, kp); 1000 if (error < 0) 1001 return (SET_ERROR(error)); 1002 1003 return (0); 1004 } 1005 1006 int 1007 param_set_max_auto_ashift(const char *buf, zfs_kernel_param_t *kp) 1008 { 1009 uint64_t val; 1010 int error; 1011 1012 error = kstrtoull(buf, 0, &val); 1013 if (error < 0) 1014 return (SET_ERROR(error)); 1015 1016 if (val > ASHIFT_MAX || val < zfs_vdev_min_auto_ashift) 1017 return (SET_ERROR(-EINVAL)); 1018 1019 error = param_set_ulong(buf, kp); 1020 if (error < 0) 1021 return (SET_ERROR(error)); 1022 1023 return (0); 1024 } 1025