Print this page
NEX-6353 The "DKIOCSOLIDSTATE failed, assuming non-SSD media" messages don't provide any useful information
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-5736 implement autoreplace matching based on FRU slot number
NEX-6200 hot spares are not reactivated after reinserting into enclosure
NEX-9403 need to update FRU for spare and l2cache devices
NEX-9404 remove lofi autoreplace support from syseventd
NEX-9409 hotsparing doesn't work for vdevs without FRU
NEX-9424 zfs`vdev_online() needs better notification about state changes
Portions contributed by: Alek Pinchuk <alek@nexenta.com>
Portions contributed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Steve Peng <steve.peng@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
NEX-8065 ZFS doesn't notice when disk vdevs have no write cache
Reviewed by: Dan Fields <dan.fields@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
NEX-2846 Enable Automatic/Intelligent Hot Sparing capability
Reviewed by: Jeffry Molanus <jeffry.molanus@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
6494 ASSERT supported zio_types for file and disk vdevs
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Approved by: Albert Lee <trisk@omniti.com>
NEX-3984 On-demand TRIM
Reviewed by: Alek Pinchuk <alek@nexenta.com>
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Conflicts:
        usr/src/common/zfs/zpool_prop.c
        usr/src/uts/common/sys/fs/zfs.h
NEX-3508 CLONE - Port NEX-2946 Add UNMAP/TRIM functionality to ZFS and illumos
Reviewed by: Josef Sipek <josef.sipek@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Conflicts:
    usr/src/uts/common/io/scsi/targets/sd.c
    usr/src/uts/common/sys/scsi/targets/sddef.h
NEX-2933 tip of nza-kernel hangs during zpool offline
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Josef Sipek <josef.sipek@nexenta.com>
NEX-1142 move rwlock to vdev to protect vdev_tsd
not just ldi handle.
This way we serialize open/close, yet allow parallel I/O.
4370 avoid transmitting holes during zfs send
4371 DMU code clean up
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Garrett D'Amore <garrett@damore.org>
NEX-1065 Added serialization to avoid race (fix lint)
NEX-1065 Added serialization to avoid race
between ldi notification and I/O path.
Also fixes OS-124, NEX-1051, NEX-1062.
   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 http://www.opensolaris.org/os/licensing.
  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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  24  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
  25  * Copyright (c) 2013 Joyent, Inc.  All rights reserved.

  26  */
  27 
  28 #include <sys/zfs_context.h>
  29 #include <sys/spa_impl.h>
  30 #include <sys/refcount.h>
  31 #include <sys/vdev_disk.h>
  32 #include <sys/vdev_impl.h>
  33 #include <sys/abd.h>
  34 #include <sys/fs/zfs.h>
  35 #include <sys/zio.h>
  36 #include <sys/sunldi.h>
  37 #include <sys/efi_partition.h>
  38 #include <sys/fm/fs/zfs.h>
  39 
  40 /*
  41  * Virtual device vector for disks.
  42  */
  43 
  44 extern ldi_ident_t zfs_li;
  45 
  46 static void vdev_disk_close(vdev_t *);
  47 
  48 typedef struct vdev_disk_ldi_cb {
  49         list_node_t             lcb_next;
  50         ldi_callback_id_t       lcb_id;
  51 } vdev_disk_ldi_cb_t;
  52 
  53 static void
  54 vdev_disk_alloc(vdev_t *vd)
  55 {
  56         vdev_disk_t *dvd;
  57 
  58         dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
  59         /*
  60          * Create the LDI event callback list.
  61          */
  62         list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
  63             offsetof(vdev_disk_ldi_cb_t, lcb_next));

  64 }
  65 
  66 static void
  67 vdev_disk_free(vdev_t *vd)
  68 {
  69         vdev_disk_t *dvd = vd->vdev_tsd;
  70         vdev_disk_ldi_cb_t *lcb;

  71 


  72         if (dvd == NULL)
  73                 return;
  74 
  75         /*
  76          * We have already closed the LDI handle. Clean up the LDI event
  77          * callbacks and free vd->vdev_tsd.
  78          */

  79         while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
  80                 list_remove(&dvd->vd_ldi_cbs, lcb);
  81                 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
  82                 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
  83         }
  84         list_destroy(&dvd->vd_ldi_cbs);
  85         kmem_free(dvd, sizeof (vdev_disk_t));
  86         vd->vdev_tsd = NULL;
  87 }
  88 








  89 /* ARGSUSED */
  90 static int
  91 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
  92     void *ev_data)
  93 {
  94         vdev_t *vd = (vdev_t *)arg;
  95         vdev_disk_t *dvd = vd->vdev_tsd;
  96 
  97         /*
  98          * Ignore events other than offline.
  99          */
 100         if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
 101                 return (LDI_EV_SUCCESS);
 102 
 103         /*
 104          * All LDI handles must be closed for the state change to succeed, so
 105          * call on vdev_disk_close() to do this.
 106          *
 107          * We inform vdev_disk_close that it is being called from offline
 108          * notify context so it will defer cleanup of LDI event callbacks and
 109          * freeing of vd->vdev_tsd to the offline finalize or a reopen.
 110          */
 111         dvd->vd_ldi_offline = B_TRUE;
 112         vdev_disk_close(vd);
 113 
 114         /*
 115          * Now that the device is closed, request that the spa_async_thread
 116          * mark the device as REMOVED and notify FMA of the removal.
 117          */
 118         zfs_post_remove(vd->vdev_spa, vd);
 119         vd->vdev_remove_wanted = B_TRUE;
 120         spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
 121 
 122         return (LDI_EV_SUCCESS);
 123 }
 124 
 125 /* ARGSUSED */
 126 static void
 127 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
 128     int ldi_result, void *arg, void *ev_data)
 129 {
 130         vdev_t *vd = (vdev_t *)arg;
 131 
 132         /*


 225         }
 226 }
 227 
 228 static void
 229 vdev_disk_rele(vdev_t *vd)
 230 {
 231         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
 232 
 233         if (vd->vdev_name_vp) {
 234                 VN_RELE_ASYNC(vd->vdev_name_vp,
 235                     dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
 236                 vd->vdev_name_vp = NULL;
 237         }
 238         if (vd->vdev_devid_vp) {
 239                 VN_RELE_ASYNC(vd->vdev_devid_vp,
 240                     dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
 241                 vd->vdev_devid_vp = NULL;
 242         }
 243 }
 244 
 245 /*
 246  * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when
 247  * even a fallback to DKIOCGMEDIAINFO fails.
 248  */
 249 #ifdef DEBUG
 250 #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__)
 251 #else
 252 #define VDEV_DEBUG(...) /* Nothing... */
 253 #endif
 254 
 255 static int
 256 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
 257     uint64_t *ashift)
 258 {
 259         spa_t *spa = vd->vdev_spa;
 260         vdev_disk_t *dvd = vd->vdev_tsd;
 261         ldi_ev_cookie_t ecookie;
 262         vdev_disk_ldi_cb_t *lcb;
 263         union {
 264                 struct dk_minfo_ext ude;
 265                 struct dk_minfo ud;
 266         } dks;
 267         struct dk_minfo_ext *dkmext = &dks.ude;
 268         struct dk_minfo *dkm = &dks.ud;
 269         int error;
 270         dev_t dev;
 271         int otyp;
 272         boolean_t validate_devid = B_FALSE;
 273         ddi_devid_t devid;
 274         uint64_t capacity = 0, blksz = 0, pbsize;
 275 
 276         /*
 277          * We must have a pathname, and it must be absolute.
 278          */
 279         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
 280                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 281                 return (SET_ERROR(EINVAL));
 282         }
 283 

 284         /*
 285          * Reopen the device if it's not currently open. Otherwise,
 286          * just update the physical size of the device.
 287          */
 288         if (dvd != NULL) {
 289                 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
 290                         /*
 291                          * If we are opening a device in its offline notify
 292                          * context, the LDI handle was just closed. Clean
 293                          * up the LDI event callbacks and free vd->vdev_tsd.
 294                          */
 295                         vdev_disk_free(vd);
 296                 } else {
 297                         ASSERT(vd->vdev_reopening);




 298                         goto skip_open;
 299                 }
 300         }
 301 
 302         /*
 303          * Create vd->vdev_tsd.
 304          */
 305         vdev_disk_alloc(vd);
 306         dvd = vd->vdev_tsd;
 307 
 308         /*
 309          * When opening a disk device, we want to preserve the user's original
 310          * intent.  We always want to open the device by the path the user gave
 311          * us, even if it is one of multiple paths to the same device.  But we
 312          * also want to be able to survive disks being removed/recabled.
 313          * Therefore the sequence of opening devices is:
 314          *
 315          * 1. Try opening the device by path.  For legacy pools without the
 316          *    'whole_disk' property, attempt to fix the path by appending 's0'.
 317          *
 318          * 2. If the devid of the device matches the stored value, return
 319          *    success.
 320          *
 321          * 3. Otherwise, the device may have moved.  Try opening the device
 322          *    by the devid instead.
 323          */
 324         if (vd->vdev_devid != NULL) {
 325                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
 326                     &dvd->vd_minor) != 0) {
 327                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 328                         vdev_dbgmsg(vd, "vdev_disk_open: invalid "
 329                             "vdev_devid '%s'", vd->vdev_devid);
 330                         return (SET_ERROR(EINVAL));
 331                 }
 332         }
 333 
 334         error = EINVAL;         /* presume failure */
 335 
 336         if (vd->vdev_path != NULL) {
 337 
 338                 if (vd->vdev_wholedisk == -1ULL) {
 339                         size_t len = strlen(vd->vdev_path) + 3;
 340                         char *buf = kmem_alloc(len, KM_SLEEP);
 341 
 342                         (void) snprintf(buf, len, "%ss0", vd->vdev_path);
 343 
 344                         error = ldi_open_by_name(buf, spa_mode(spa), kcred,
 345                             &dvd->vd_lh, zfs_li);
 346                         if (error == 0) {
 347                                 spa_strfree(vd->vdev_path);
 348                                 vd->vdev_path = buf;
 349                                 vd->vdev_wholedisk = 1ULL;


 402                 if (vd->vdev_devid != NULL)
 403                         validate_devid = B_TRUE;
 404 
 405                 if (vd->vdev_physpath != NULL &&
 406                     (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
 407                         error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
 408                             kcred, &dvd->vd_lh, zfs_li);
 409 
 410                 /*
 411                  * Note that we don't support the legacy auto-wholedisk support
 412                  * as above.  This hasn't been used in a very long time and we
 413                  * don't need to propagate its oddities to this edge condition.
 414                  */
 415                 if (error && vd->vdev_path != NULL)
 416                         error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
 417                             kcred, &dvd->vd_lh, zfs_li);
 418         }
 419 
 420         if (error) {
 421                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 422                 vdev_dbgmsg(vd, "vdev_disk_open: failed to open [error=%d]",
 423                     error);
 424                 return (error);
 425         }
 426 
 427         /*
 428          * Now that the device has been successfully opened, update the devid
 429          * if necessary.
 430          */
 431         if (validate_devid && spa_writeable(spa) &&
 432             ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 433                 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 434                         char *vd_devid;
 435 
 436                         vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
 437                         vdev_dbgmsg(vd, "vdev_disk_open: update devid from "
 438                             "'%s' to '%s'", vd->vdev_devid, vd_devid);
 439                         spa_strfree(vd->vdev_devid);
 440                         vd->vdev_devid = spa_strdup(vd_devid);
 441                         ddi_devid_str_free(vd_devid);
 442                 }
 443                 ddi_devid_free(devid);
 444         }
 445 
 446         /*
 447          * Once a device is opened, verify that the physical device path (if
 448          * available) is up to date.
 449          */
 450         if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
 451             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
 452                 char *physpath, *minorname;
 453 
 454                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 455                 minorname = NULL;
 456                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
 457                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
 458                     (vd->vdev_physpath == NULL ||


 472          * Register callbacks for the LDI offline event.
 473          */
 474         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
 475             LDI_EV_SUCCESS) {
 476                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 477                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 478                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 479                     &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
 480         }
 481 
 482         /*
 483          * Register callbacks for the LDI degrade event.
 484          */
 485         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
 486             LDI_EV_SUCCESS) {
 487                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 488                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 489                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 490                     &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
 491         }




 492 skip_open:

 493         /*
 494          * Determine the actual size of the device.
 495          */
 496         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
 497                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 498                 vdev_dbgmsg(vd, "vdev_disk_open: failed to get size");

 499                 return (SET_ERROR(EINVAL));
 500         }
 501 
 502         *max_psize = *psize;
 503 
 504         /*
 505          * Determine the device's minimum transfer size.
 506          * If the ioctl isn't supported, assume DEV_BSIZE.
 507          */
 508         if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
 509             (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
 510                 capacity = dkmext->dki_capacity - 1;
 511                 blksz = dkmext->dki_lbsize;
 512                 pbsize = dkmext->dki_pbsize;
 513         } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
 514             (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
 515                 VDEV_DEBUG(
 516                     "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
 517                     vd->vdev_path);
 518                 capacity = dkm->dki_capacity - 1;
 519                 blksz = dkm->dki_lbsize;
 520                 pbsize = blksz;
 521         } else {
 522                 VDEV_DEBUG("vdev_disk_open(\"%s\"): "
 523                     "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
 524                     vd->vdev_path, error);
 525                 pbsize = DEV_BSIZE;
 526         }
 527 
 528         *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
 529 
 530         if (vd->vdev_wholedisk == 1) {
 531                 int wce = 1;
 532 
 533                 if (error == 0) {
 534                         /*
 535                          * If we have the capability to expand, we'd have
 536                          * found out via success from DKIOCGMEDIAINFO{,EXT}.
 537                          * Adjust max_psize upward accordingly since we know
 538                          * we own the whole disk now.
 539                          */
 540                         *max_psize = capacity * blksz;
 541                 }
 542 
 543                 /*
 544                  * Since we own the whole disk, try to enable disk write
 545                  * caching.  We ignore errors because it's OK if we can't do it.
 546                  */
 547                 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
 548                     FKIOCTL, kcred, NULL);
 549         }
 550 






 551         /*





 552          * Clear the nowritecache bit, so that on a vdev_reopen() we will
 553          * try again.
 554          */
 555         vd->vdev_nowritecache = B_FALSE;
 556 










 557         return (0);
 558 }
 559 
 560 static void
 561 vdev_disk_close(vdev_t *vd)
 562 {
 563         vdev_disk_t *dvd = vd->vdev_tsd;
 564 



 565         if (vd->vdev_reopening || dvd == NULL)
 566                 return;
 567 
 568         if (dvd->vd_minor != NULL) {
 569                 ddi_devid_str_free(dvd->vd_minor);
 570                 dvd->vd_minor = NULL;
 571         }
 572 
 573         if (dvd->vd_devid != NULL) {
 574                 ddi_devid_free(dvd->vd_devid);
 575                 dvd->vd_devid = NULL;
 576         }
 577 
 578         if (dvd->vd_lh != NULL) {
 579                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
 580                 dvd->vd_lh = NULL;
 581         }
 582 
 583         vd->vdev_delayed_close = B_FALSE;
 584         /*
 585          * If we closed the LDI handle due to an offline notify from LDI,
 586          * don't free vd->vdev_tsd or unregister the callbacks here;
 587          * the offline finalize callback or a reopen will take care of it.
 588          */
 589         if (dvd->vd_ldi_offline)
 590                 return;



 591 
 592         vdev_disk_free(vd);



 593 }
 594 
 595 int
 596 vdev_disk_physio(vdev_t *vd, caddr_t data,
 597     size_t size, uint64_t offset, int flags, boolean_t isdump)
 598 {
 599         vdev_disk_t *dvd = vd->vdev_tsd;

 600 


 601         /*
 602          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 603          * Nothing to be done here but return failure.
 604          */
 605         if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
 606                 return (EIO);
 607 
 608         ASSERT(vd->vdev_ops == &vdev_disk_ops);
 609 
 610         /*
 611          * If in the context of an active crash dump, use the ldi_dump(9F)
 612          * call instead of ldi_strategy(9F) as usual.
 613          */
 614         if (isdump) {
 615                 ASSERT3P(dvd, !=, NULL);
 616                 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
 617                     lbtodb(size)));
 618         }
 619 
 620         return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));



 621 }
 622 
 623 int
 624 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
 625     size_t size, uint64_t offset, int flags)
 626 {
 627         buf_t *bp;
 628         int error = 0;
 629 
 630         if (vd_lh == NULL)
 631                 return (SET_ERROR(EINVAL));
 632 
 633         ASSERT(flags & B_READ || flags & B_WRITE);
 634 
 635         bp = getrbuf(KM_SLEEP);
 636         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
 637         bp->b_bcount = size;
 638         bp->b_un.b_addr = (void *)data;
 639         bp->b_lblkno = lbtodb(offset);
 640         bp->b_bufsize = size;


 683 
 684 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
 685         vdev_disk_ioctl_free,
 686         zio_vsd_default_cksum_report
 687 };
 688 
 689 static void
 690 vdev_disk_ioctl_done(void *zio_arg, int error)
 691 {
 692         zio_t *zio = zio_arg;
 693 
 694         zio->io_error = error;
 695 
 696         zio_interrupt(zio);
 697 }
 698 
 699 static void
 700 vdev_disk_io_start(zio_t *zio)
 701 {
 702         vdev_t *vd = zio->io_vd;
 703         vdev_disk_t *dvd = vd->vdev_tsd;
 704         vdev_buf_t *vb;
 705         struct dk_callback *dkc;
 706         buf_t *bp;
 707         int error;
 708 


 709         /*
 710          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 711          * Nothing to be done here but return failure.
 712          */
 713         if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
 714                 zio->io_error = ENXIO;

 715                 zio_interrupt(zio);
 716                 return;
 717         }
 718 
 719         if (zio->io_type == ZIO_TYPE_IOCTL) {
 720                 /* XXPOLICY */
 721                 if (!vdev_readable(vd)) {
 722                         zio->io_error = SET_ERROR(ENXIO);

 723                         zio_interrupt(zio);
 724                         return;
 725                 }
 726 
 727                 switch (zio->io_cmd) {
 728 
 729                 case DKIOCFLUSHWRITECACHE:
 730 
 731                         if (zfs_nocacheflush)
 732                                 break;
 733 
 734                         if (vd->vdev_nowritecache) {
 735                                 zio->io_error = SET_ERROR(ENOTSUP);
 736                                 break;
 737                         }
 738 
 739                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
 740                         zio->io_vsd_ops = &vdev_disk_vsd_ops;
 741 
 742                         dkc->dkc_callback = vdev_disk_ioctl_done;
 743                         dkc->dkc_flag = FLUSH_VOLATILE;
 744                         dkc->dkc_cookie = zio;
 745 
 746                         error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
 747                             (uintptr_t)dkc, FKIOCTL, kcred, NULL);
 748 
 749                         if (error == 0) {
 750                                 /*
 751                                  * The ioctl will be done asychronously,
 752                                  * and will call vdev_disk_ioctl_done()
 753                                  * upon completion.
 754                                  */

 755                                 return;
 756                         }
 757 
 758                         zio->io_error = error;
 759 
 760                         break;
 761 





























 762                 default:
 763                         zio->io_error = SET_ERROR(ENOTSUP);
 764                 }
 765 

 766                 zio_execute(zio);
 767                 return;
 768         }
 769 
 770         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
 771         zio->io_target_timestamp = zio_handle_io_delay(zio);
 772 
 773         vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
 774 
 775         vb->vb_io = zio;
 776         bp = &vb->vb_buf;
 777 
 778         bioinit(bp);
 779         bp->b_flags = B_BUSY | B_NOCACHE |
 780             (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
 781         if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
 782                 bp->b_flags |= B_FAILFAST;
 783         bp->b_bcount = zio->io_size;
 784 
 785         if (zio->io_type == ZIO_TYPE_READ) {
 786                 bp->b_un.b_addr =
 787                     abd_borrow_buf(zio->io_abd, zio->io_size);
 788         } else {
 789                 bp->b_un.b_addr =
 790                     abd_borrow_buf_copy(zio->io_abd, zio->io_size);
 791         }
 792 
 793         bp->b_lblkno = lbtodb(zio->io_offset);
 794         bp->b_bufsize = zio->io_size;
 795         bp->b_iodone = (int (*)())vdev_disk_io_intr;
 796 
 797         /* ldi_strategy() will return non-zero only on programming errors */
 798         VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);

 799 }
 800 
 801 static void
 802 vdev_disk_io_done(zio_t *zio)
 803 {
 804         vdev_t *vd = zio->io_vd;
 805 
 806         /*
 807          * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
 808          * the device has been removed.  If this is the case, then we trigger an
 809          * asynchronous removal of the device. Otherwise, probe the device and
 810          * make sure it's still accessible.
 811          */
 812         if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
 813                 vdev_disk_t *dvd = vd->vdev_tsd;
 814                 int state = DKIO_NONE;
 815 
 816                 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
 817                     FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {





 818                         /*
 819                          * We post the resource as soon as possible, instead of
 820                          * when the async removal actually happens, because the
 821                          * DE is using this information to discard previous I/O
 822                          * errors.
 823                          */
 824                         zfs_post_remove(zio->io_spa, vd);
 825                         vd->vdev_remove_wanted = B_TRUE;
 826                         spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
 827                 } else if (!vd->vdev_delayed_close) {
 828                         vd->vdev_delayed_close = B_TRUE;
 829                 }
 830         }
 831 }
 832 
 833 vdev_ops_t vdev_disk_ops = {
 834         vdev_disk_open,
 835         vdev_disk_close,
 836         vdev_default_asize,
 837         vdev_disk_io_start,


   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 http://www.opensolaris.org/os/licensing.
  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 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.

  25  * Copyright (c) 2013 Joyent, Inc.  All rights reserved.
  26  * Copyright 2017 Nexenta Systems, Inc.
  27  */
  28 
  29 #include <sys/zfs_context.h>
  30 #include <sys/spa_impl.h>
  31 #include <sys/refcount.h>
  32 #include <sys/vdev_disk.h>
  33 #include <sys/vdev_impl.h>
  34 #include <sys/abd.h>
  35 #include <sys/fs/zfs.h>
  36 #include <sys/zio.h>
  37 #include <sys/sunldi.h>
  38 #include <sys/efi_partition.h>
  39 #include <sys/fm/fs/zfs.h>
  40 
  41 /*
  42  * Virtual device vector for disks.
  43  */
  44 
  45 extern ldi_ident_t zfs_li;
  46 
  47 static void vdev_disk_close_impl(vdev_t *, boolean_t);
  48 
  49 typedef struct vdev_disk_ldi_cb {
  50         list_node_t             lcb_next;
  51         ldi_callback_id_t       lcb_id;
  52 } vdev_disk_ldi_cb_t;
  53 
  54 static vdev_disk_t *
  55 vdev_disk_alloc(void)
  56 {
  57         vdev_disk_t *dvd;
  58 
  59         dvd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
  60         /*
  61          * Create the LDI event callback list.
  62          */
  63         list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
  64             offsetof(vdev_disk_ldi_cb_t, lcb_next));
  65         return (dvd);
  66 }
  67 
  68 static void
  69 vdev_disk_free_locked(vdev_t *vd)
  70 {

  71         vdev_disk_ldi_cb_t *lcb;
  72         vdev_disk_t *dvd = vd->vdev_tsd;
  73 
  74         ASSERT(rw_lock_held(&vd->vdev_tsd_lock));
  75 
  76         if (dvd == NULL)
  77                 return;
  78 
  79         /*
  80          * We have already closed the LDI handle. Clean up the LDI event
  81          * callbacks and free vd->vdev_tsd.
  82          */
  83         vd->vdev_tsd = NULL;
  84         while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
  85                 list_remove(&dvd->vd_ldi_cbs, lcb);
  86                 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
  87                 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
  88         }
  89         list_destroy(&dvd->vd_ldi_cbs);
  90         kmem_free(dvd, sizeof (vdev_disk_t));

  91 }
  92 
  93 static void
  94 vdev_disk_free(vdev_t *vd)
  95 {
  96         rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
  97         vdev_disk_free_locked(vd);
  98         rw_exit(&vd->vdev_tsd_lock);
  99 }
 100 
 101 /* ARGSUSED */
 102 static int
 103 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
 104     void *ev_data)
 105 {
 106         vdev_t *vd = (vdev_t *)arg;

 107 
 108         /*
 109          * Ignore events other than offline.
 110          */
 111         if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
 112                 return (LDI_EV_SUCCESS);
 113 
 114         /*
 115          * All LDI handles must be closed for the state change to succeed, so
 116          * call on vdev_disk_close() to do this.
 117          *
 118          * We inform vdev_disk_close that it is being called from offline
 119          * notify context so it will defer cleanup of LDI event callbacks and
 120          * freeing of vd->vdev_tsd to the offline finalize or a reopen.
 121          */
 122         vdev_disk_close_impl(vd, B_TRUE);

 123 
 124         /*
 125          * Now that the device is closed, request that the spa_async_thread
 126          * mark the device as REMOVED and notify FMA of the removal.
 127          */
 128         zfs_post_remove(vd->vdev_spa, vd);
 129         vd->vdev_remove_wanted = B_TRUE;
 130         spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
 131 
 132         return (LDI_EV_SUCCESS);
 133 }
 134 
 135 /* ARGSUSED */
 136 static void
 137 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
 138     int ldi_result, void *arg, void *ev_data)
 139 {
 140         vdev_t *vd = (vdev_t *)arg;
 141 
 142         /*


 235         }
 236 }
 237 
 238 static void
 239 vdev_disk_rele(vdev_t *vd)
 240 {
 241         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
 242 
 243         if (vd->vdev_name_vp) {
 244                 VN_RELE_ASYNC(vd->vdev_name_vp,
 245                     dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
 246                 vd->vdev_name_vp = NULL;
 247         }
 248         if (vd->vdev_devid_vp) {
 249                 VN_RELE_ASYNC(vd->vdev_devid_vp,
 250                     dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
 251                 vd->vdev_devid_vp = NULL;
 252         }
 253 }
 254 










 255 static int
 256 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
 257     uint64_t *ashift)
 258 {
 259         spa_t *spa = vd->vdev_spa;
 260         vdev_disk_t *dvd;
 261         ldi_ev_cookie_t ecookie;
 262         vdev_disk_ldi_cb_t *lcb;
 263         union {
 264                 struct dk_minfo_ext ude;
 265                 struct dk_minfo ud;
 266         } dks;
 267         struct dk_minfo_ext *dkmext = &dks.ude;
 268         struct dk_minfo *dkm = &dks.ud;
 269         int error;
 270         dev_t dev;
 271         int otyp, vdev_ssd;
 272         boolean_t validate_devid = B_FALSE;
 273         ddi_devid_t devid;
 274         uint64_t capacity = 0, blksz = 0, pbsize;
 275 
 276         /*
 277          * We must have a pathname, and it must be absolute.
 278          */
 279         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
 280                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 281                 return (SET_ERROR(EINVAL));
 282         }
 283         rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
 284         dvd = vd->vdev_tsd;
 285         /*
 286          * Reopen the device if it's not currently open. Otherwise,
 287          * just update the physical size of the device.
 288          */
 289         if (dvd != NULL) {
 290                 ASSERT(vd->vdev_reopening);
 291                 /*
 292                  * Here vd_lh is protected by vdev_tsd_lock


 293                  */
 294                 ASSERT(dvd->vd_lh != NULL);
 295                 /* This should not happen, but let's be safe */
 296                 if (dvd->vd_lh == NULL) {
 297                         /* What are we going to do here??? */
 298                         rw_exit(&vd->vdev_tsd_lock);
 299                         return (SET_ERROR(ENXIO));
 300                 }
 301                 goto skip_open;
 302         }


 303         /*
 304          * Create dvd to be used as vd->vdev_tsd.
 305          */
 306         vd->vdev_tsd = dvd = vdev_disk_alloc();

 307 
 308         /*
 309          * When opening a disk device, we want to preserve the user's original
 310          * intent.  We always want to open the device by the path the user gave
 311          * us, even if it is one of multiple paths to the same device.  But we
 312          * also want to be able to survive disks being removed/recabled.
 313          * Therefore the sequence of opening devices is:
 314          *
 315          * 1. Try opening the device by path.  For legacy pools without the
 316          *    'whole_disk' property, attempt to fix the path by appending 's0'.
 317          *
 318          * 2. If the devid of the device matches the stored value, return
 319          *    success.
 320          *
 321          * 3. Otherwise, the device may have moved.  Try opening the device
 322          *    by the devid instead.
 323          */
 324         if (vd->vdev_devid != NULL) {
 325                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
 326                     &dvd->vd_minor) != 0) {
 327                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 328                         vdev_disk_free_locked(vd);
 329                         rw_exit(&vd->vdev_tsd_lock);
 330                         return (SET_ERROR(EINVAL));
 331                 }
 332         }
 333 
 334         error = EINVAL;         /* presume failure */
 335 
 336         if (vd->vdev_path != NULL) {
 337 
 338                 if (vd->vdev_wholedisk == -1ULL) {
 339                         size_t len = strlen(vd->vdev_path) + 3;
 340                         char *buf = kmem_alloc(len, KM_SLEEP);
 341 
 342                         (void) snprintf(buf, len, "%ss0", vd->vdev_path);
 343 
 344                         error = ldi_open_by_name(buf, spa_mode(spa), kcred,
 345                             &dvd->vd_lh, zfs_li);
 346                         if (error == 0) {
 347                                 spa_strfree(vd->vdev_path);
 348                                 vd->vdev_path = buf;
 349                                 vd->vdev_wholedisk = 1ULL;


 402                 if (vd->vdev_devid != NULL)
 403                         validate_devid = B_TRUE;
 404 
 405                 if (vd->vdev_physpath != NULL &&
 406                     (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
 407                         error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
 408                             kcred, &dvd->vd_lh, zfs_li);
 409 
 410                 /*
 411                  * Note that we don't support the legacy auto-wholedisk support
 412                  * as above.  This hasn't been used in a very long time and we
 413                  * don't need to propagate its oddities to this edge condition.
 414                  */
 415                 if (error && vd->vdev_path != NULL)
 416                         error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
 417                             kcred, &dvd->vd_lh, zfs_li);
 418         }
 419 
 420         if (error) {
 421                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 422                 vdev_disk_free_locked(vd);
 423                 rw_exit(&vd->vdev_tsd_lock);
 424                 return (error);
 425         }
 426 
 427         /*
 428          * Now that the device has been successfully opened, update the devid
 429          * if necessary.
 430          */
 431         if (validate_devid && spa_writeable(spa) &&
 432             ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 433                 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 434                         char *vd_devid;
 435 
 436                         vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
 437                         zfs_dbgmsg("vdev %s: update devid from %s, "
 438                             "to %s", vd->vdev_path, vd->vdev_devid, vd_devid);
 439                         spa_strfree(vd->vdev_devid);
 440                         vd->vdev_devid = spa_strdup(vd_devid);
 441                         ddi_devid_str_free(vd_devid);
 442                 }
 443                 ddi_devid_free(devid);
 444         }
 445 
 446         /*
 447          * Once a device is opened, verify that the physical device path (if
 448          * available) is up to date.
 449          */
 450         if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
 451             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
 452                 char *physpath, *minorname;
 453 
 454                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 455                 minorname = NULL;
 456                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
 457                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
 458                     (vd->vdev_physpath == NULL ||


 472          * Register callbacks for the LDI offline event.
 473          */
 474         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
 475             LDI_EV_SUCCESS) {
 476                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 477                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 478                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 479                     &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
 480         }
 481 
 482         /*
 483          * Register callbacks for the LDI degrade event.
 484          */
 485         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
 486             LDI_EV_SUCCESS) {
 487                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 488                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 489                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 490                     &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
 491         }
 492 
 493         /* Reset TRIM flag, as underlying device support may have changed */
 494         vd->vdev_notrim = B_FALSE;
 495 
 496 skip_open:
 497         ASSERT(dvd != NULL);
 498         /*
 499          * Determine the actual size of the device.
 500          */
 501         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
 502                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 503                 vdev_disk_free_locked(vd);
 504                 rw_exit(&vd->vdev_tsd_lock);
 505                 return (SET_ERROR(EINVAL));
 506         }
 507 
 508         *max_psize = *psize;
 509 
 510         /*
 511          * Determine the device's minimum transfer size.
 512          * If the ioctl isn't supported, assume DEV_BSIZE.
 513          */
 514         if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
 515             (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
 516                 capacity = dkmext->dki_capacity - 1;
 517                 blksz = dkmext->dki_lbsize;
 518                 pbsize = dkmext->dki_pbsize;
 519         } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
 520             (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {



 521                 capacity = dkm->dki_capacity - 1;
 522                 blksz = dkm->dki_lbsize;
 523                 pbsize = blksz;
 524         } else {



 525                 pbsize = DEV_BSIZE;
 526         }
 527 
 528         *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
 529 
 530         if (vd->vdev_wholedisk == 1) {
 531                 int wce = 1;
 532 
 533                 if (error == 0) {
 534                         /*
 535                          * If we have the capability to expand, we'd have
 536                          * found out via success from DKIOCGMEDIAINFO{,EXT}.
 537                          * Adjust max_psize upward accordingly since we know
 538                          * we own the whole disk now.
 539                          */
 540                         *max_psize = capacity * blksz;
 541                 }
 542 
 543                 /*
 544                  * Since we own the whole disk, try to enable disk write
 545                  * caching.  We ignore errors because it's OK if we can't do it.
 546                  */
 547                 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
 548                     FKIOCTL, kcred, NULL);
 549         }
 550 
 551         if (ldi_ioctl(dvd->vd_lh, DKIOCSOLIDSTATE, (intptr_t)&vdev_ssd,
 552             FKIOCTL, kcred, NULL) != 0)
 553                 vd->vdev_is_ssd = B_FALSE;
 554         else
 555                 vd->vdev_is_ssd = vdev_ssd ? B_TRUE : B_FALSE;
 556 
 557         /*
 558          * We are done with vd_lh and vdev_tsd, release the vdev_tsd_lock
 559          */
 560         rw_exit(&vd->vdev_tsd_lock);
 561 
 562         /*
 563          * Clear the nowritecache bit, so that on a vdev_reopen() we will
 564          * try again.
 565          */
 566         vd->vdev_nowritecache = B_FALSE;
 567 
 568         /*
 569          * vdev open has succeeded - reset fault flags if last fault was due
 570          * to a failed open since the open fault looks to have been transient
 571          */
 572         if (vd->vdev_removed || (vd->vdev_faulted &&
 573             vd->vdev_label_aux == VDEV_AUX_OPEN_FAILED)) {
 574                 vd->vdev_faulted = vd->vdev_removed = 0ULL;
 575                 vd->vdev_label_aux = VDEV_AUX_NONE;
 576         }
 577 
 578         return (0);
 579 }
 580 
 581 static void
 582 vdev_disk_close_impl(vdev_t *vd, boolean_t ldi_offline)
 583 {
 584         vdev_disk_t *dvd;
 585 
 586         rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
 587         dvd = vd->vdev_tsd;
 588 
 589         if (vd->vdev_reopening || dvd == NULL)
 590                 goto out;
 591 
 592         if (dvd->vd_minor != NULL) {
 593                 ddi_devid_str_free(dvd->vd_minor);
 594                 dvd->vd_minor = NULL;
 595         }
 596 
 597         if (dvd->vd_devid != NULL) {
 598                 ddi_devid_free(dvd->vd_devid);
 599                 dvd->vd_devid = NULL;
 600         }
 601 
 602         if (dvd->vd_lh != NULL) {
 603                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
 604                 dvd->vd_lh = NULL;
 605         }
 606 
 607         vd->vdev_delayed_close = B_FALSE;
 608         /*
 609          * If we closed the LDI handle due to an offline notify from LDI,
 610          * don't free vd->vdev_tsd or unregister the callbacks here;
 611          * the offline finalize callback or a reopen will take care of it.
 612          */
 613         if (!ldi_offline)
 614                 vdev_disk_free_locked(vd);
 615 out:
 616         rw_exit(&vd->vdev_tsd_lock);
 617 }
 618 
 619 static void
 620 vdev_disk_close(vdev_t *vd)
 621 {
 622         vdev_disk_close_impl(vd, B_FALSE);
 623 }
 624 
 625 int
 626 vdev_disk_physio(vdev_t *vd, caddr_t data,
 627     size_t size, uint64_t offset, int flags, boolean_t isdump)
 628 {
 629         int rc = EIO;
 630         vdev_disk_t *dvd;
 631 
 632         rw_enter(&vd->vdev_tsd_lock, RW_READER);
 633         dvd = vd->vdev_tsd;
 634         /*
 635          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 636          * Nothing to be done here but return failure.
 637          */
 638         if (dvd == NULL || dvd->vd_lh == NULL)
 639                 goto out;
 640 
 641         ASSERT(vd->vdev_ops == &vdev_disk_ops);
 642 
 643         /*
 644          * If in the context of an active crash dump, use the ldi_dump(9F)
 645          * call instead of ldi_strategy(9F) as usual.
 646          */
 647         if (isdump) {
 648                 ASSERT3P(dvd, !=, NULL);
 649                 rc = ldi_dump(dvd->vd_lh, data, lbtodb(offset), lbtodb(size));
 650                 goto out;
 651         }
 652 
 653         rc = vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags);
 654 out:
 655         rw_exit(&vd->vdev_tsd_lock);
 656         return (rc);
 657 }
 658 
 659 int
 660 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
 661     size_t size, uint64_t offset, int flags)
 662 {
 663         buf_t *bp;
 664         int error = 0;
 665 
 666         if (vd_lh == NULL)
 667                 return (SET_ERROR(EINVAL));
 668 
 669         ASSERT(flags & B_READ || flags & B_WRITE);
 670 
 671         bp = getrbuf(KM_SLEEP);
 672         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
 673         bp->b_bcount = size;
 674         bp->b_un.b_addr = (void *)data;
 675         bp->b_lblkno = lbtodb(offset);
 676         bp->b_bufsize = size;


 719 
 720 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
 721         vdev_disk_ioctl_free,
 722         zio_vsd_default_cksum_report
 723 };
 724 
 725 static void
 726 vdev_disk_ioctl_done(void *zio_arg, int error)
 727 {
 728         zio_t *zio = zio_arg;
 729 
 730         zio->io_error = error;
 731 
 732         zio_interrupt(zio);
 733 }
 734 
 735 static void
 736 vdev_disk_io_start(zio_t *zio)
 737 {
 738         vdev_t *vd = zio->io_vd;
 739         vdev_disk_t *dvd;
 740         vdev_buf_t *vb;
 741         struct dk_callback *dkc;
 742         buf_t *bp;
 743         int error;
 744 
 745         rw_enter(&vd->vdev_tsd_lock, RW_READER);
 746         dvd = vd->vdev_tsd;
 747         /*
 748          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 749          * Nothing to be done here but return failure.
 750          */
 751         if (dvd == NULL || dvd->vd_lh == NULL) {
 752                 zio->io_error = ENXIO;
 753                 rw_exit(&vd->vdev_tsd_lock);
 754                 zio_interrupt(zio);
 755                 return;
 756         }
 757 
 758         if (zio->io_type == ZIO_TYPE_IOCTL) {
 759                 /* XXPOLICY */
 760                 if (!vdev_readable(vd)) {
 761                         zio->io_error = SET_ERROR(ENXIO);
 762                         rw_exit(&vd->vdev_tsd_lock);
 763                         zio_interrupt(zio);
 764                         return;
 765                 }
 766 
 767                 switch (zio->io_cmd) {
 768 
 769                 case DKIOCFLUSHWRITECACHE:
 770 
 771                         if (zfs_nocacheflush)
 772                                 break;
 773 
 774                         if (vd->vdev_nowritecache) {
 775                                 zio->io_error = SET_ERROR(ENOTSUP);
 776                                 break;
 777                         }
 778 
 779                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
 780                         zio->io_vsd_ops = &vdev_disk_vsd_ops;
 781 
 782                         dkc->dkc_callback = vdev_disk_ioctl_done;
 783                         dkc->dkc_flag = FLUSH_VOLATILE;
 784                         dkc->dkc_cookie = zio;
 785 
 786                         error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
 787                             (uintptr_t)dkc, FKIOCTL, kcred, NULL);
 788 
 789                         if (error == 0) {
 790                                 /*
 791                                  * The ioctl will be done asychronously,
 792                                  * and will call vdev_disk_ioctl_done()
 793                                  * upon completion.
 794                                  */
 795                                 rw_exit(&vd->vdev_tsd_lock);
 796                                 return;
 797                         }
 798 
 799                         zio->io_error = error;
 800 
 801                         break;
 802 
 803                 case DKIOCFREE:
 804                         /*
 805                          * We perform device support checks here instead of
 806                          * in zio_trim(), as zio_trim() might be invoked on
 807                          * top of a top-level vdev, whereas vdev_disk_io_start
 808                          * is guaranteed to be operating a leaf vdev.
 809                          */
 810                         if (vd->vdev_notrim &&
 811                             spa_get_force_trim(vd->vdev_spa) !=
 812                             SPA_FORCE_TRIM_ON) {
 813                                 zio->io_error = SET_ERROR(ENOTSUP);
 814                                 break;
 815                         }
 816 
 817                         /*
 818                          * zio->io_private contains a dkioc_free_list_t
 819                          * specifying which offsets are to be freed
 820                          */
 821                         ASSERT(zio->io_private != NULL);
 822                         error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
 823                             (uintptr_t)zio->io_private, FKIOCTL, kcred, NULL);
 824 
 825                         if (error == ENOTSUP || error == ENOTTY)
 826                                 vd->vdev_notrim = B_TRUE;
 827 
 828                         zio->io_error = error;
 829 
 830                         break;
 831 
 832                 default:
 833                         zio->io_error = SET_ERROR(ENOTSUP);
 834                 }
 835 
 836                 rw_exit(&vd->vdev_tsd_lock);
 837                 zio_execute(zio);
 838                 return;
 839         }
 840 
 841         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
 842         zio->io_target_timestamp = zio_handle_io_delay(zio);
 843 
 844         vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
 845 
 846         vb->vb_io = zio;
 847         bp = &vb->vb_buf;
 848 
 849         bioinit(bp);
 850         bp->b_flags = B_BUSY | B_NOCACHE |
 851             (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
 852         if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
 853                 bp->b_flags |= B_FAILFAST;
 854         bp->b_bcount = zio->io_size;
 855 
 856         if (zio->io_type == ZIO_TYPE_READ) {
 857                 bp->b_un.b_addr =
 858                     abd_borrow_buf(zio->io_abd, zio->io_size);
 859         } else {
 860                 bp->b_un.b_addr =
 861                     abd_borrow_buf_copy(zio->io_abd, zio->io_size);
 862         }
 863 
 864         bp->b_lblkno = lbtodb(zio->io_offset);
 865         bp->b_bufsize = zio->io_size;
 866         bp->b_iodone = (int (*)())vdev_disk_io_intr;
 867 
 868         /* ldi_strategy() will return non-zero only on programming errors */
 869         VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
 870         rw_exit(&vd->vdev_tsd_lock);
 871 }
 872 
 873 static void
 874 vdev_disk_io_done(zio_t *zio)
 875 {
 876         vdev_t *vd = zio->io_vd;
 877 
 878         /*
 879          * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
 880          * the device has been removed.  If this is the case, then we trigger an
 881          * asynchronous removal of the device. Otherwise, probe the device and
 882          * make sure it's still accessible.
 883          */
 884         if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
 885                 vdev_disk_t *dvd;
 886                 int rc = EIO, state = DKIO_NONE;
 887 
 888                 rw_enter(&vd->vdev_tsd_lock, RW_READER);
 889                 dvd = vd->vdev_tsd;
 890                 if (dvd != NULL && dvd->vd_lh != NULL)
 891                         rc = ldi_ioctl(dvd->vd_lh, DKIOCSTATE,
 892                             (intptr_t)&state, FKIOCTL, kcred, NULL);
 893                 rw_exit(&vd->vdev_tsd_lock);
 894                 if (rc == 0 && state != DKIO_INSERTED) {
 895                         /*
 896                          * We post the resource as soon as possible, instead of
 897                          * when the async removal actually happens, because the
 898                          * DE is using this information to discard previous I/O
 899                          * errors.
 900                          */
 901                         zfs_post_remove(zio->io_spa, vd);
 902                         vd->vdev_remove_wanted = B_TRUE;
 903                         spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
 904                 } else if (!vd->vdev_delayed_close) {
 905                         vd->vdev_delayed_close = B_TRUE;
 906                 }
 907         }
 908 }
 909 
 910 vdev_ops_t vdev_disk_ops = {
 911         vdev_disk_open,
 912         vdev_disk_close,
 913         vdev_default_asize,
 914         vdev_disk_io_start,