Print this page
LOCAL: listen for ldi notifications of disk offline/degrade


   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 by Delphix. All rights reserved.
  24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.

  25  */
  26 
  27 #include <sys/zfs_context.h>
  28 #include <sys/zfs_zone.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/fs/zfs.h>
  34 #include <sys/zio.h>
  35 #include <sys/sunldi.h>
  36 #include <sys/efi_partition.h>
  37 #include <sys/fm/fs/zfs.h>
  38 
  39 /*
  40  * Virtual device vector for disks.
  41  */
  42 
  43 extern ldi_ident_t zfs_li;
  44 


  45 typedef struct vdev_disk_buf {
  46         buf_t   vdb_buf;
  47         zio_t   *vdb_io;
  48 } vdev_disk_buf_t;
  49 









































































  50 static void






























































  51 vdev_disk_hold(vdev_t *vd)
  52 {
  53         ddi_devid_t devid;
  54         char *minor;
  55 
  56         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
  57 
  58         /*
  59          * We must have a pathname, and it must be absolute.
  60          */
  61         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
  62                 return;
  63 
  64         /*
  65          * Only prefetch path and devid info if the device has
  66          * never been opened.
  67          */
  68         if (vd->vdev_tsd != NULL)
  69                 return;
  70 


 124         efi = dk_ioc.dki_data;
 125 
 126         if (ldi_ioctl(dvd->vd_lh, DKIOCGETEFI, (intptr_t)&dk_ioc,
 127             FKIOCTL, kcred, NULL) == 0) {
 128                 uint64_t efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
 129 
 130                 zfs_dbgmsg("vdev %s, capacity %llu, altern lba %llu",
 131                     vd->vdev_path, capacity, efi_altern_lba);
 132                 if (capacity > efi_altern_lba)
 133                         avail_space = (capacity - efi_altern_lba) * blksz;
 134         }
 135         kmem_free(dk_ioc.dki_data, efisize);
 136         return (avail_space);
 137 }
 138 
 139 static int
 140 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
 141     uint64_t *ashift)
 142 {
 143         spa_t *spa = vd->vdev_spa;
 144         vdev_disk_t *dvd;
 145         struct dk_minfo_ext dkmext;


 146         int error;
 147         dev_t dev;
 148         int otyp;
 149 
 150         /*
 151          * We must have a pathname, and it must be absolute.
 152          */
 153         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
 154                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 155                 return (EINVAL);
 156         }
 157 
 158         /*
 159          * Reopen the device if it's not currently open. Otherwise,
 160          * just update the physical size of the device.
 161          */
 162         if (vd->vdev_tsd != NULL) {
 163                 ASSERT(vd->vdev_reopening);
 164                 dvd = vd->vdev_tsd;







 165                 goto skip_open;
 166         }

 167 
 168         dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);




 169 
 170         /*
 171          * When opening a disk device, we want to preserve the user's original
 172          * intent.  We always want to open the device by the path the user gave
 173          * us, even if it is one of multiple paths to the save device.  But we
 174          * also want to be able to survive disks being removed/recabled.
 175          * Therefore the sequence of opening devices is:
 176          *
 177          * 1. Try opening the device by path.  For legacy pools without the
 178          *    'whole_disk' property, attempt to fix the path by appending 's0'.
 179          *
 180          * 2. If the devid of the device matches the stored value, return
 181          *    success.
 182          *
 183          * 3. Otherwise, the device may have moved.  Try opening the device
 184          *    by the devid instead.
 185          */
 186         if (vd->vdev_devid != NULL) {
 187                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
 188                     &dvd->vd_minor) != 0) {
 189                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 190                         return (EINVAL);
 191                 }
 192         }
 193 
 194         error = EINVAL;         /* presume failure */
 195 
 196         if (vd->vdev_path != NULL) {
 197                 ddi_devid_t devid;
 198 
 199                 if (vd->vdev_wholedisk == -1ULL) {
 200                         size_t len = strlen(vd->vdev_path) + 3;
 201                         char *buf = kmem_alloc(len, KM_SLEEP);
 202                         ldi_handle_t lh;
 203 
 204                         (void) snprintf(buf, len, "%ss0", vd->vdev_path);
 205 
 206                         if (ldi_open_by_name(buf, spa_mode(spa), kcred,
 207                             &lh, zfs_li) == 0) {

 208                                 spa_strfree(vd->vdev_path);
 209                                 vd->vdev_path = buf;
 210                                 vd->vdev_wholedisk = 1ULL;
 211                                 (void) ldi_close(lh, spa_mode(spa), kcred);
 212                         } else {
 213                                 kmem_free(buf, len);
 214                         }
 215                 }
 216 
 217                 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
 218                     &dvd->vd_lh, zfs_li);






 219 
 220                 /*
 221                  * Compare the devid to the stored value.
 222                  */
 223                 if (error == 0 && vd->vdev_devid != NULL &&
 224                     ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 225                         if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 226                                 error = EINVAL;
 227                                 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
 228                                     kcred);
 229                                 dvd->vd_lh = NULL;
 230                         }
 231                         ddi_devid_free(devid);
 232                 }
 233 
 234                 /*
 235                  * If we succeeded in opening the device, but 'vdev_wholedisk'
 236                  * is not yet set, then this must be a slice.
 237                  */
 238                 if (error == 0 && vd->vdev_wholedisk == -1ULL)


 282             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
 283                 char *physpath, *minorname;
 284 
 285                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 286                 minorname = NULL;
 287                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
 288                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
 289                     (vd->vdev_physpath == NULL ||
 290                     strcmp(vd->vdev_physpath, physpath) != 0)) {
 291                         if (vd->vdev_physpath)
 292                                 spa_strfree(vd->vdev_physpath);
 293                         (void) strlcat(physpath, ":", MAXPATHLEN);
 294                         (void) strlcat(physpath, minorname, MAXPATHLEN);
 295                         vd->vdev_physpath = spa_strdup(physpath);
 296                 }
 297                 if (minorname)
 298                         kmem_free(minorname, strlen(minorname) + 1);
 299                 kmem_free(physpath, MAXPATHLEN);
 300         }
 301 





















 302 skip_open:
 303         /*
 304          * Determine the actual size of the device.
 305          */
 306         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
 307                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 308                 return (EINVAL);
 309         }
 310 
 311         /*
 312          * Determine the device's minimum transfer size.
 313          * If the ioctl isn't supported, assume DEV_BSIZE.
 314          */
 315         if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext,
 316             FKIOCTL, kcred, NULL) != 0)
 317                 dkmext.dki_pbsize = DEV_BSIZE;
 318 
 319         *ashift = highbit(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1;
 320 
 321         if (vd->vdev_wholedisk == 1) {


 333                 *max_psize = *psize + vdev_disk_get_space(vd, capacity, blksz);
 334                 zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
 335                     "max_psize %llu", vd->vdev_path, *psize, *max_psize);
 336         } else {
 337                 *max_psize = *psize;
 338         }
 339 
 340         /*
 341          * Clear the nowritecache bit, so that on a vdev_reopen() we will
 342          * try again.
 343          */
 344         vd->vdev_nowritecache = B_FALSE;
 345 
 346         return (0);
 347 }
 348 
 349 static void
 350 vdev_disk_close(vdev_t *vd)
 351 {
 352         vdev_disk_t *dvd = vd->vdev_tsd;

 353 
 354         if (vd->vdev_reopening || dvd == NULL)
 355                 return;
 356 
 357         if (dvd->vd_minor != NULL)
 358                 ddi_devid_str_free(dvd->vd_minor);


 359 
 360         if (dvd->vd_devid != NULL)
 361                 ddi_devid_free(dvd->vd_devid);


 362 
 363         if (dvd->vd_lh != NULL)
 364                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);


 365 
 366         vd->vdev_delayed_close = B_FALSE;
 367         kmem_free(dvd, sizeof (vdev_disk_t));
 368         vd->vdev_tsd = NULL;







 369 }
 370 
 371 int
 372 vdev_disk_physio(vdev_t *vd, caddr_t data,
 373     size_t size, uint64_t offset, int flags)
 374 {
 375         vdev_disk_t *dvd = vd->vdev_tsd;
 376 
 377         /*
 378          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 379          * Nothing to be done here but return failure.
 380          */
 381         if (dvd == NULL)
 382                 return (EIO);
 383 
 384         ASSERT(vd->vdev_ops == &vdev_disk_ops);
 385         return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
 386 }
 387 
 388 int
 389 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
 390     size_t size, uint64_t offset, int flags)
 391 {
 392         buf_t *bp;
 393         int error = 0;
 394 
 395         if (vd_lh == NULL)
 396                 return (EINVAL);
 397 
 398         ASSERT(flags & B_READ || flags & B_WRITE);
 399 
 400         bp = getrbuf(KM_SLEEP);
 401         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;


 448 static void
 449 vdev_disk_ioctl_done(void *zio_arg, int error)
 450 {
 451         zio_t *zio = zio_arg;
 452 
 453         zio->io_error = error;
 454 
 455         zio_interrupt(zio);
 456 }
 457 
 458 static int
 459 vdev_disk_io_start(zio_t *zio)
 460 {
 461         vdev_t *vd = zio->io_vd;
 462         vdev_disk_t *dvd = vd->vdev_tsd;
 463         vdev_disk_buf_t *vdb;
 464         struct dk_callback *dkc;
 465         buf_t *bp;
 466         int error;
 467 









 468         if (zio->io_type == ZIO_TYPE_IOCTL) {
 469                 /* XXPOLICY */
 470                 if (!vdev_readable(vd)) {
 471                         zio->io_error = ENXIO;
 472                         return (ZIO_PIPELINE_CONTINUE);
 473                 }
 474 
 475                 switch (zio->io_cmd) {
 476 
 477                 case DKIOCFLUSHWRITECACHE:
 478 
 479                         if (zfs_nocacheflush)
 480                                 break;
 481 
 482                         if (vd->vdev_nowritecache) {
 483                                 zio->io_error = ENOTSUP;
 484                                 break;
 485                         }
 486 
 487                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);




   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 by Delphix. All rights reserved.
  24  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
  25  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
  26  */
  27 
  28 #include <sys/zfs_context.h>
  29 #include <sys/zfs_zone.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/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_buf {
  49         buf_t   vdb_buf;
  50         zio_t   *vdb_io;
  51 } vdev_disk_buf_t;
  52 
  53 typedef struct vdev_disk_ldi_cb {
  54         list_node_t             lcb_next;
  55         ldi_callback_id_t       lcb_id;
  56 } vdev_disk_ldi_cb_t;
  57 
  58 static void vdev_disk_alloc(vdev_t *vd)
  59 {
  60         vdev_disk_t *dvd;
  61 
  62         dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
  63         /*
  64          * Create the LDI event callback list.
  65          */
  66         list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
  67             offsetof(vdev_disk_ldi_cb_t, lcb_next));
  68 }
  69 
  70 static void vdev_disk_free(vdev_t *vd)
  71 {
  72         vdev_disk_t *dvd = vd->vdev_tsd;
  73         vdev_disk_ldi_cb_t *lcb;
  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         vdev_disk_t *dvd = vd->vdev_tsd;
 132         vdev_disk_ldi_cb_t *lcb;
 133 
 134         /*
 135          * Ignore events other than offline.
 136          */
 137         if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
 138                 return;
 139 
 140         /*
 141          * We have already closed the LDI handle in notify.
 142          * Clean up the LDI event callbacks and free vd->vdev_tsd.
 143          */
 144         vdev_disk_free(vd);
 145 
 146         /*
 147          * Request that the vdev be reopened if the offline state change was
 148          * unsuccessful.
 149          */
 150         if (ldi_result != LDI_EV_SUCCESS) {
 151                 vd->vdev_probe_wanted = B_TRUE;
 152                 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
 153         }
 154 }
 155 
 156 static ldi_ev_callback_t vdev_disk_off_callb = {
 157         .cb_vers = LDI_EV_CB_VERS,
 158         .cb_notify = vdev_disk_off_notify,
 159         .cb_finalize = vdev_disk_off_finalize
 160 };
 161 
 162 /* ARGSUSED */
 163 static void
 164 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
 165     int ldi_result, void *arg, void *ev_data)
 166 {
 167         vdev_t *vd = (vdev_t *)arg;
 168 
 169         /*
 170          * Ignore events other than degrade.
 171          */
 172         if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
 173                 return;
 174 
 175         /*
 176          * Degrade events always succeed. Mark the vdev as degraded.
 177          * This status is purely informative for the user.
 178          */
 179         (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
 180 }
 181 
 182 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
 183         .cb_vers = LDI_EV_CB_VERS,
 184         .cb_notify = NULL,
 185         .cb_finalize = vdev_disk_dgrd_finalize
 186 };
 187 
 188 static void
 189 vdev_disk_hold(vdev_t *vd)
 190 {
 191         ddi_devid_t devid;
 192         char *minor;
 193 
 194         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
 195 
 196         /*
 197          * We must have a pathname, and it must be absolute.
 198          */
 199         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
 200                 return;
 201 
 202         /*
 203          * Only prefetch path and devid info if the device has
 204          * never been opened.
 205          */
 206         if (vd->vdev_tsd != NULL)
 207                 return;
 208 


 262         efi = dk_ioc.dki_data;
 263 
 264         if (ldi_ioctl(dvd->vd_lh, DKIOCGETEFI, (intptr_t)&dk_ioc,
 265             FKIOCTL, kcred, NULL) == 0) {
 266                 uint64_t efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
 267 
 268                 zfs_dbgmsg("vdev %s, capacity %llu, altern lba %llu",
 269                     vd->vdev_path, capacity, efi_altern_lba);
 270                 if (capacity > efi_altern_lba)
 271                         avail_space = (capacity - efi_altern_lba) * blksz;
 272         }
 273         kmem_free(dk_ioc.dki_data, efisize);
 274         return (avail_space);
 275 }
 276 
 277 static int
 278 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
 279     uint64_t *ashift)
 280 {
 281         spa_t *spa = vd->vdev_spa;
 282         vdev_disk_t *dvd = vd->vdev_tsd;
 283         struct dk_minfo_ext dkmext;
 284         ldi_ev_cookie_t ecookie;
 285         vdev_disk_ldi_cb_t *lcb;
 286         int error;
 287         dev_t dev;
 288         int otyp;
 289 
 290         /*
 291          * We must have a pathname, and it must be absolute.
 292          */
 293         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
 294                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 295                 return (EINVAL);
 296         }
 297 
 298         /*
 299          * Reopen the device if it's not currently open. Otherwise,
 300          * just update the physical size of the device.
 301          */
 302         if (dvd != NULL) {
 303                 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
 304                         /*
 305                          * If we are opening a device in its offline notify
 306                          * context, the LDI handle was just closed. Clean
 307                          * up the LDI event callbacks and free vd->vdev_tsd.
 308                          */
 309                         vdev_disk_free(vd);
 310                 } else {
 311                         VERIFY(vd->vdev_reopening);
 312                         goto skip_open;
 313                 }
 314         }
 315 
 316         /*
 317          * Create vd->vdev_tsd.
 318          */
 319         vdev_disk_alloc(vd);
 320         dvd = vd->vdev_tsd;
 321 
 322         /*
 323          * When opening a disk device, we want to preserve the user's original
 324          * intent.  We always want to open the device by the path the user gave
 325          * us, even if it is one of multiple paths to the save device.  But we
 326          * also want to be able to survive disks being removed/recabled.
 327          * Therefore the sequence of opening devices is:
 328          *
 329          * 1. Try opening the device by path.  For legacy pools without the
 330          *    'whole_disk' property, attempt to fix the path by appending 's0'.
 331          *
 332          * 2. If the devid of the device matches the stored value, return
 333          *    success.
 334          *
 335          * 3. Otherwise, the device may have moved.  Try opening the device
 336          *    by the devid instead.
 337          */
 338         if (vd->vdev_devid != NULL) {
 339                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
 340                     &dvd->vd_minor) != 0) {
 341                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 342                         return (EINVAL);
 343                 }
 344         }
 345 
 346         error = EINVAL;         /* presume failure */
 347 
 348         if (vd->vdev_path != NULL) {
 349                 ddi_devid_t devid;
 350 
 351                 if (vd->vdev_wholedisk == -1ULL) {
 352                         size_t len = strlen(vd->vdev_path) + 3;
 353                         char *buf = kmem_alloc(len, KM_SLEEP);

 354 
 355                         (void) snprintf(buf, len, "%ss0", vd->vdev_path);
 356 
 357                         error = ldi_open_by_name(buf, spa_mode(spa), kcred,
 358                             &dvd->vd_lh, zfs_li);
 359                         if (error == 0) {
 360                                 spa_strfree(vd->vdev_path);
 361                                 vd->vdev_path = buf;
 362                                 vd->vdev_wholedisk = 1ULL;

 363                         } else {
 364                                 kmem_free(buf, len);
 365                         }
 366                 }
 367 
 368                 /*
 369                  * If we have not yet opened the device, try to open it by the
 370                  * specified path.
 371                  */
 372                 if (error != 0) {
 373                         error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
 374                             kcred, &dvd->vd_lh, zfs_li);
 375                 }
 376 
 377                 /*
 378                  * Compare the devid to the stored value.
 379                  */
 380                 if (error == 0 && vd->vdev_devid != NULL &&
 381                     ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 382                         if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 383                                 error = EINVAL;
 384                                 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
 385                                     kcred);
 386                                 dvd->vd_lh = NULL;
 387                         }
 388                         ddi_devid_free(devid);
 389                 }
 390 
 391                 /*
 392                  * If we succeeded in opening the device, but 'vdev_wholedisk'
 393                  * is not yet set, then this must be a slice.
 394                  */
 395                 if (error == 0 && vd->vdev_wholedisk == -1ULL)


 439             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
 440                 char *physpath, *minorname;
 441 
 442                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 443                 minorname = NULL;
 444                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
 445                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
 446                     (vd->vdev_physpath == NULL ||
 447                     strcmp(vd->vdev_physpath, physpath) != 0)) {
 448                         if (vd->vdev_physpath)
 449                                 spa_strfree(vd->vdev_physpath);
 450                         (void) strlcat(physpath, ":", MAXPATHLEN);
 451                         (void) strlcat(physpath, minorname, MAXPATHLEN);
 452                         vd->vdev_physpath = spa_strdup(physpath);
 453                 }
 454                 if (minorname)
 455                         kmem_free(minorname, strlen(minorname) + 1);
 456                 kmem_free(physpath, MAXPATHLEN);
 457         }
 458 
 459         /*
 460          * Register callbacks for the LDI offline event.
 461          */
 462         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
 463             LDI_EV_SUCCESS) {
 464                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 465                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 466                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 467                     &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
 468         }
 469 
 470         /*
 471          * Register callbacks for the LDI degrade event.
 472          */
 473         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
 474             LDI_EV_SUCCESS) {
 475                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 476                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 477                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 478                     &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
 479         }
 480 skip_open:
 481         /*
 482          * Determine the actual size of the device.
 483          */
 484         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
 485                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 486                 return (EINVAL);
 487         }
 488 
 489         /*
 490          * Determine the device's minimum transfer size.
 491          * If the ioctl isn't supported, assume DEV_BSIZE.
 492          */
 493         if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext,
 494             FKIOCTL, kcred, NULL) != 0)
 495                 dkmext.dki_pbsize = DEV_BSIZE;
 496 
 497         *ashift = highbit(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1;
 498 
 499         if (vd->vdev_wholedisk == 1) {


 511                 *max_psize = *psize + vdev_disk_get_space(vd, capacity, blksz);
 512                 zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
 513                     "max_psize %llu", vd->vdev_path, *psize, *max_psize);
 514         } else {
 515                 *max_psize = *psize;
 516         }
 517 
 518         /*
 519          * Clear the nowritecache bit, so that on a vdev_reopen() we will
 520          * try again.
 521          */
 522         vd->vdev_nowritecache = B_FALSE;
 523 
 524         return (0);
 525 }
 526 
 527 static void
 528 vdev_disk_close(vdev_t *vd)
 529 {
 530         vdev_disk_t *dvd = vd->vdev_tsd;
 531         vdev_disk_ldi_cb_t *lcb;
 532 
 533         if (vd->vdev_reopening || dvd == NULL)
 534                 return;
 535 
 536         if (dvd->vd_minor != NULL) {
 537                 ddi_devid_str_free(dvd->vd_minor);
 538                 dvd->vd_minor = NULL;
 539         }
 540 
 541         if (dvd->vd_devid != NULL) {
 542                 ddi_devid_free(dvd->vd_devid);
 543                 dvd->vd_devid = NULL;
 544         }
 545 
 546         if (dvd->vd_lh != NULL) {
 547                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
 548                 dvd->vd_lh = NULL;
 549         }
 550 
 551         vd->vdev_delayed_close = B_FALSE;
 552         /*
 553          * If we closed the LDI handle due to an offline notify from LDI,
 554          * don't free vd->vdev_tsd or unregister the callbacks here;
 555          * the offline finalize callback or a reopen will take care of it.
 556          */
 557         if (dvd->vd_ldi_offline)
 558                 return;
 559 
 560         vdev_disk_free(vd);
 561 }
 562 
 563 int
 564 vdev_disk_physio(vdev_t *vd, caddr_t data,
 565     size_t size, uint64_t offset, int flags)
 566 {
 567         vdev_disk_t *dvd = vd->vdev_tsd;
 568 
 569         /*
 570          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 571          * Nothing to be done here but return failure.
 572          */
 573         if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
 574                 return (EIO);
 575 
 576         ASSERT(vd->vdev_ops == &vdev_disk_ops);
 577         return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
 578 }
 579 
 580 int
 581 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
 582     size_t size, uint64_t offset, int flags)
 583 {
 584         buf_t *bp;
 585         int error = 0;
 586 
 587         if (vd_lh == NULL)
 588                 return (EINVAL);
 589 
 590         ASSERT(flags & B_READ || flags & B_WRITE);
 591 
 592         bp = getrbuf(KM_SLEEP);
 593         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;


 640 static void
 641 vdev_disk_ioctl_done(void *zio_arg, int error)
 642 {
 643         zio_t *zio = zio_arg;
 644 
 645         zio->io_error = error;
 646 
 647         zio_interrupt(zio);
 648 }
 649 
 650 static int
 651 vdev_disk_io_start(zio_t *zio)
 652 {
 653         vdev_t *vd = zio->io_vd;
 654         vdev_disk_t *dvd = vd->vdev_tsd;
 655         vdev_disk_buf_t *vdb;
 656         struct dk_callback *dkc;
 657         buf_t *bp;
 658         int error;
 659 
 660         /*
 661          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 662          * Nothing to be done here but return failure.
 663          */
 664         if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
 665                 zio->io_error = ENXIO;
 666                 return (ZIO_PIPELINE_CONTINUE);
 667         }
 668 
 669         if (zio->io_type == ZIO_TYPE_IOCTL) {
 670                 /* XXPOLICY */
 671                 if (!vdev_readable(vd)) {
 672                         zio->io_error = ENXIO;
 673                         return (ZIO_PIPELINE_CONTINUE);
 674                 }
 675 
 676                 switch (zio->io_cmd) {
 677 
 678                 case DKIOCFLUSHWRITECACHE:
 679 
 680                         if (zfs_nocacheflush)
 681                                 break;
 682 
 683                         if (vd->vdev_nowritecache) {
 684                                 zio->io_error = ENOTSUP;
 685                                 break;
 686                         }
 687 
 688                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);