7938 Port ZOL #3712 disable LBA weighting on files and SSDs

   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, 2015 by Delphix. All rights reserved.
  24  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
  25  * Copyright (c) 2013 Joyent, Inc.  All rights reserved.
  26  * Copyright (c) 2017 James S Blachly, MD <james.blachly@gmail.com>
  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/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         /*
 133          * Ignore events other than offline.
 134          */
 135         if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
 136                 return;
 137 
 138         /*
 139          * We have already closed the LDI handle in notify.
 140          * Clean up the LDI event callbacks and free vd->vdev_tsd.
 141          */
 142         vdev_disk_free(vd);
 143 
 144         /*
 145          * Request that the vdev be reopened if the offline state change was
 146          * unsuccessful.
 147          */
 148         if (ldi_result != LDI_EV_SUCCESS) {
 149                 vd->vdev_probe_wanted = B_TRUE;
 150                 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
 151         }
 152 }
 153 
 154 static ldi_ev_callback_t vdev_disk_off_callb = {
 155         .cb_vers = LDI_EV_CB_VERS,
 156         .cb_notify = vdev_disk_off_notify,
 157         .cb_finalize = vdev_disk_off_finalize
 158 };
 159 
 160 /* ARGSUSED */
 161 static void
 162 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
 163     int ldi_result, void *arg, void *ev_data)
 164 {
 165         vdev_t *vd = (vdev_t *)arg;
 166 
 167         /*
 168          * Ignore events other than degrade.
 169          */
 170         if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
 171                 return;
 172 
 173         /*
 174          * Degrade events always succeed. Mark the vdev as degraded.
 175          * This status is purely informative for the user.
 176          */
 177         (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
 178 }
 179 
 180 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
 181         .cb_vers = LDI_EV_CB_VERS,
 182         .cb_notify = NULL,
 183         .cb_finalize = vdev_disk_dgrd_finalize
 184 };
 185 
 186 static void
 187 vdev_disk_hold(vdev_t *vd)
 188 {
 189         ddi_devid_t devid;
 190         char *minor;
 191 
 192         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
 193 
 194         /*
 195          * We must have a pathname, and it must be absolute.
 196          */
 197         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
 198                 return;
 199 
 200         /*
 201          * Only prefetch path and devid info if the device has
 202          * never been opened.
 203          */
 204         if (vd->vdev_tsd != NULL)
 205                 return;
 206 
 207         if (vd->vdev_wholedisk == -1ULL) {
 208                 size_t len = strlen(vd->vdev_path) + 3;
 209                 char *buf = kmem_alloc(len, KM_SLEEP);
 210 
 211                 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
 212 
 213                 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
 214                 kmem_free(buf, len);
 215         }
 216 
 217         if (vd->vdev_name_vp == NULL)
 218                 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
 219 
 220         if (vd->vdev_devid != NULL &&
 221             ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
 222                 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
 223                 ddi_devid_str_free(minor);
 224                 ddi_devid_free(devid);
 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         int device_solid_state;
 276         char *vendorp;  /* will point to inquiry-vendor-id */
 277 
 278         /*
 279          * We must have a pathname, and it must be absolute.
 280          */
 281         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
 282                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 283                 return (SET_ERROR(EINVAL));
 284         }
 285 
 286         /*
 287          * Reopen the device if it's not currently open. Otherwise,
 288          * just update the physical size of the device.
 289          */
 290         if (dvd != NULL) {
 291                 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
 292                         /*
 293                          * If we are opening a device in its offline notify
 294                          * context, the LDI handle was just closed. Clean
 295                          * up the LDI event callbacks and free vd->vdev_tsd.
 296                          */
 297                         vdev_disk_free(vd);
 298                 } else {
 299                         ASSERT(vd->vdev_reopening);
 300                         goto skip_open;
 301                 }
 302         }
 303 
 304         /*
 305          * Create vd->vdev_tsd.
 306          */
 307         vdev_disk_alloc(vd);
 308         dvd = vd->vdev_tsd;
 309 
 310         /*
 311          * When opening a disk device, we want to preserve the user's original
 312          * intent.  We always want to open the device by the path the user gave
 313          * us, even if it is one of multiple paths to the same device.  But we
 314          * also want to be able to survive disks being removed/recabled.
 315          * Therefore the sequence of opening devices is:
 316          *
 317          * 1. Try opening the device by path.  For legacy pools without the
 318          *    'whole_disk' property, attempt to fix the path by appending 's0'.
 319          *
 320          * 2. If the devid of the device matches the stored value, return
 321          *    success.
 322          *
 323          * 3. Otherwise, the device may have moved.  Try opening the device
 324          *    by the devid instead.
 325          */
 326         if (vd->vdev_devid != NULL) {
 327                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
 328                     &dvd->vd_minor) != 0) {
 329                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 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;
 350                         } else {
 351                                 kmem_free(buf, len);
 352                         }
 353                 }
 354 
 355                 /*
 356                  * If we have not yet opened the device, try to open it by the
 357                  * specified path.
 358                  */
 359                 if (error != 0) {
 360                         error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
 361                             kcred, &dvd->vd_lh, zfs_li);
 362                 }
 363 
 364                 /*
 365                  * Compare the devid to the stored value.
 366                  */
 367                 if (error == 0 && vd->vdev_devid != NULL &&
 368                     ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 369                         if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 370                                 error = SET_ERROR(EINVAL);
 371                                 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
 372                                     kcred);
 373                                 dvd->vd_lh = NULL;
 374                         }
 375                         ddi_devid_free(devid);
 376                 }
 377 
 378                 /*
 379                  * If we succeeded in opening the device, but 'vdev_wholedisk'
 380                  * is not yet set, then this must be a slice.
 381                  */
 382                 if (error == 0 && vd->vdev_wholedisk == -1ULL)
 383                         vd->vdev_wholedisk = 0;
 384         }
 385 
 386         /*
 387          * If we were unable to open by path, or the devid check fails, open by
 388          * devid instead.
 389          */
 390         if (error != 0 && vd->vdev_devid != NULL) {
 391                 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
 392                     spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
 393         }
 394 
 395         /*
 396          * If all else fails, then try opening by physical path (if available)
 397          * or the logical path (if we failed due to the devid check).  While not
 398          * as reliable as the devid, this will give us something, and the higher
 399          * level vdev validation will prevent us from opening the wrong device.
 400          */
 401         if (error) {
 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                 return (error);
 423         }
 424 
 425         /*
 426          * Now that the device has been successfully opened, update the devid
 427          * if necessary.
 428          */
 429         if (validate_devid && spa_writeable(spa) &&
 430             ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 431                 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 432                         char *vd_devid;
 433 
 434                         vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
 435                         zfs_dbgmsg("vdev %s: update devid from %s, "
 436                             "to %s", vd->vdev_path, vd->vdev_devid, vd_devid);
 437                         spa_strfree(vd->vdev_devid);
 438                         vd->vdev_devid = spa_strdup(vd_devid);
 439                         ddi_devid_str_free(vd_devid);
 440                 }
 441                 ddi_devid_free(devid);
 442         }
 443 
 444         /*
 445          * Once a device is opened, verify that the physical device path (if
 446          * available) is up to date.
 447          */
 448         if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
 449             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
 450                 char *physpath, *minorname;
 451 
 452                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 453                 minorname = NULL;
 454                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
 455                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
 456                     (vd->vdev_physpath == NULL ||
 457                     strcmp(vd->vdev_physpath, physpath) != 0)) {
 458                         if (vd->vdev_physpath)
 459                                 spa_strfree(vd->vdev_physpath);
 460                         (void) strlcat(physpath, ":", MAXPATHLEN);
 461                         (void) strlcat(physpath, minorname, MAXPATHLEN);
 462                         vd->vdev_physpath = spa_strdup(physpath);
 463                 }
 464                 if (minorname)
 465                         kmem_free(minorname, strlen(minorname) + 1);
 466                 kmem_free(physpath, MAXPATHLEN);
 467         }
 468 
 469         /*
 470          * Register callbacks for the LDI offline event.
 471          */
 472         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
 473             LDI_EV_SUCCESS) {
 474                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 475                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 476                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 477                     &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
 478         }
 479 
 480         /*
 481          * Register callbacks for the LDI degrade event.
 482          */
 483         if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
 484             LDI_EV_SUCCESS) {
 485                 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
 486                 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
 487                 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
 488                     &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
 489         }
 490 skip_open:
 491         /*
 492          * Determine the actual size of the device.
 493          */
 494         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
 495                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 496                 return (SET_ERROR(EINVAL));
 497         }
 498 
 499         *max_psize = *psize;
 500 
 501         /*
 502          * Determine the device's minimum transfer size.
 503          * If the ioctl isn't supported, assume DEV_BSIZE.
 504          */
 505         if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
 506             (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
 507                 capacity = dkmext->dki_capacity - 1;
 508                 blksz = dkmext->dki_lbsize;
 509                 pbsize = dkmext->dki_pbsize;
 510         } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
 511             (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
 512                 VDEV_DEBUG(
 513                     "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
 514                     vd->vdev_path);
 515                 capacity = dkm->dki_capacity - 1;
 516                 blksz = dkm->dki_lbsize;
 517                 pbsize = blksz;
 518         } else {
 519                 VDEV_DEBUG("vdev_disk_open(\"%s\"): "
 520                     "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
 521                     vd->vdev_path, error);
 522                 pbsize = DEV_BSIZE;
 523         }
 524 
 525         *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
 526 
 527         if (vd->vdev_wholedisk == 1) {
 528                 int wce = 1;
 529 
 530                 if (error == 0) {
 531                         /*
 532                          * If we have the capability to expand, we'd have
 533                          * found out via success from DKIOCGMEDIAINFO{,EXT}.
 534                          * Adjust max_psize upward accordingly since we know
 535                          * we own the whole disk now.
 536                          */
 537                         *max_psize = capacity * blksz;
 538                 }
 539 
 540                 /*
 541                  * Since we own the whole disk, try to enable disk write
 542                  * caching.  We ignore errors because it's OK if we can't do it.
 543                  */
 544                 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
 545                     FKIOCTL, kcred, NULL);
 546         }
 547 
 548         /*
 549          * Inform the ZIO pipeline if we are non-rotational:
 550          * 1. Check if device is SSD
 551          * 2. If not SSD, check if device is Virtio
 552          */
 553         device_solid_state = ldi_prop_get_int(dvd->vd_lh, LDI_DEV_T_ANY,
 554             "device-solid-state", 0);
 555         vd->vdev_nonrot = (device_solid_state ? B_TRUE : B_FALSE);
 556 
 557         if (device_solid_state == 0 &&
 558             ldi_prop_exists(dvd->vd_lh, LDI_DEV_T_ANY, "inquiry-vendor-id")) {
 559                 ldi_prop_lookup_string(dvd->vd_lh, LDI_DEV_T_ANY,
 560                     "inquiry-vendor-id", &vendorp);
 561                 if (strncmp(vendorp, "Virtio", 6) == 0)
 562                     vd->vdev_nonrot = B_TRUE;
 563                 ddi_prop_free(vendorp);
 564         }
 565 
 566         cmn_err(CE_NOTE, "[vdev_disk_open] %s :: device-solid-state "
 567             "== %d :: vd->vdev_nonrot == %d\n", vd->vdev_path,
 568             device_solid_state, (int) vd->vdev_nonrot);
 569 
 570         /*
 571          * Clear the nowritecache bit, so that on a vdev_reopen() we will
 572          * try again.
 573          */
 574         vd->vdev_nowritecache = B_FALSE;
 575 
 576         return (0);
 577 }
 578 
 579 static void
 580 vdev_disk_close(vdev_t *vd)
 581 {
 582         vdev_disk_t *dvd = vd->vdev_tsd;
 583 
 584         if (vd->vdev_reopening || dvd == NULL)
 585                 return;
 586 
 587         if (dvd->vd_minor != NULL) {
 588                 ddi_devid_str_free(dvd->vd_minor);
 589                 dvd->vd_minor = NULL;
 590         }
 591 
 592         if (dvd->vd_devid != NULL) {
 593                 ddi_devid_free(dvd->vd_devid);
 594                 dvd->vd_devid = NULL;
 595         }
 596 
 597         if (dvd->vd_lh != NULL) {
 598                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
 599                 dvd->vd_lh = NULL;
 600         }
 601 
 602         vd->vdev_delayed_close = B_FALSE;
 603         /*
 604          * If we closed the LDI handle due to an offline notify from LDI,
 605          * don't free vd->vdev_tsd or unregister the callbacks here;
 606          * the offline finalize callback or a reopen will take care of it.
 607          */
 608         if (dvd->vd_ldi_offline)
 609                 return;
 610 
 611         vdev_disk_free(vd);
 612 }
 613 
 614 int
 615 vdev_disk_physio(vdev_t *vd, caddr_t data,
 616     size_t size, uint64_t offset, int flags, boolean_t isdump)
 617 {
 618         vdev_disk_t *dvd = vd->vdev_tsd;
 619 
 620         /*
 621          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 622          * Nothing to be done here but return failure.
 623          */
 624         if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
 625                 return (EIO);
 626 
 627         ASSERT(vd->vdev_ops == &vdev_disk_ops);
 628 
 629         /*
 630          * If in the context of an active crash dump, use the ldi_dump(9F)
 631          * call instead of ldi_strategy(9F) as usual.
 632          */
 633         if (isdump) {
 634                 ASSERT3P(dvd, !=, NULL);
 635                 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
 636                     lbtodb(size)));
 637         }
 638 
 639         return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
 640 }
 641 
 642 int
 643 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
 644     size_t size, uint64_t offset, int flags)
 645 {
 646         buf_t *bp;
 647         int error = 0;
 648 
 649         if (vd_lh == NULL)
 650                 return (SET_ERROR(EINVAL));
 651 
 652         ASSERT(flags & B_READ || flags & B_WRITE);
 653 
 654         bp = getrbuf(KM_SLEEP);
 655         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
 656         bp->b_bcount = size;
 657         bp->b_un.b_addr = (void *)data;
 658         bp->b_lblkno = lbtodb(offset);
 659         bp->b_bufsize = size;
 660 
 661         error = ldi_strategy(vd_lh, bp);
 662         ASSERT(error == 0);
 663         if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
 664                 error = SET_ERROR(EIO);
 665         freerbuf(bp);
 666 
 667         return (error);
 668 }
 669 
 670 static void
 671 vdev_disk_io_intr(buf_t *bp)
 672 {
 673         vdev_buf_t *vb = (vdev_buf_t *)bp;
 674         zio_t *zio = vb->vb_io;
 675 
 676         /*
 677          * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
 678          * Rather than teach the rest of the stack about other error
 679          * possibilities (EFAULT, etc), we normalize the error value here.
 680          */
 681         zio->io_error = (geterror(bp) != 0 ? EIO : 0);
 682 
 683         if (zio->io_error == 0 && bp->b_resid != 0)
 684                 zio->io_error = SET_ERROR(EIO);
 685 
 686         kmem_free(vb, sizeof (vdev_buf_t));
 687 
 688         zio_delay_interrupt(zio);
 689 }
 690 
 691 static void
 692 vdev_disk_ioctl_free(zio_t *zio)
 693 {
 694         kmem_free(zio->io_vsd, sizeof (struct dk_callback));
 695 }
 696 
 697 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
 698         vdev_disk_ioctl_free,
 699         zio_vsd_default_cksum_report
 700 };
 701 
 702 static void
 703 vdev_disk_ioctl_done(void *zio_arg, int error)
 704 {
 705         zio_t *zio = zio_arg;
 706 
 707         zio->io_error = error;
 708 
 709         zio_interrupt(zio);
 710 }
 711 
 712 static void
 713 vdev_disk_io_start(zio_t *zio)
 714 {
 715         vdev_t *vd = zio->io_vd;
 716         vdev_disk_t *dvd = vd->vdev_tsd;
 717         vdev_buf_t *vb;
 718         struct dk_callback *dkc;
 719         buf_t *bp;
 720         int error;
 721 
 722         /*
 723          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 724          * Nothing to be done here but return failure.
 725          */
 726         if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
 727                 zio->io_error = ENXIO;
 728                 zio_interrupt(zio);
 729                 return;
 730         }
 731 
 732         if (zio->io_type == ZIO_TYPE_IOCTL) {
 733                 /* XXPOLICY */
 734                 if (!vdev_readable(vd)) {
 735                         zio->io_error = SET_ERROR(ENXIO);
 736                         zio_interrupt(zio);
 737                         return;
 738                 }
 739 
 740                 switch (zio->io_cmd) {
 741 
 742                 case DKIOCFLUSHWRITECACHE:
 743 
 744                         if (zfs_nocacheflush)
 745                                 break;
 746 
 747                         if (vd->vdev_nowritecache) {
 748                                 zio->io_error = SET_ERROR(ENOTSUP);
 749                                 break;
 750                         }
 751 
 752                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
 753                         zio->io_vsd_ops = &vdev_disk_vsd_ops;
 754 
 755                         dkc->dkc_callback = vdev_disk_ioctl_done;
 756                         dkc->dkc_flag = FLUSH_VOLATILE;
 757                         dkc->dkc_cookie = zio;
 758 
 759                         error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
 760                             (uintptr_t)dkc, FKIOCTL, kcred, NULL);
 761 
 762                         if (error == 0) {
 763                                 /*
 764                                  * The ioctl will be done asychronously,
 765                                  * and will call vdev_disk_ioctl_done()
 766                                  * upon completion.
 767                                  */
 768                                 return;
 769                         }
 770 
 771                         zio->io_error = error;
 772 
 773                         break;
 774 
 775                 default:
 776                         zio->io_error = SET_ERROR(ENOTSUP);
 777                 }
 778 
 779                 zio_execute(zio);
 780                 return;
 781         }
 782 
 783         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
 784         zio->io_target_timestamp = zio_handle_io_delay(zio);
 785 
 786         vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
 787 
 788         vb->vb_io = zio;
 789         bp = &vb->vb_buf;
 790 
 791         bioinit(bp);
 792         bp->b_flags = B_BUSY | B_NOCACHE |
 793             (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
 794         if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
 795                 bp->b_flags |= B_FAILFAST;
 796         bp->b_bcount = zio->io_size;
 797         bp->b_un.b_addr = zio->io_data;
 798         bp->b_lblkno = lbtodb(zio->io_offset);
 799         bp->b_bufsize = zio->io_size;
 800         bp->b_iodone = (int (*)())vdev_disk_io_intr;
 801 
 802         /* ldi_strategy() will return non-zero only on programming errors */
 803         VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
 804 }
 805 
 806 static void
 807 vdev_disk_io_done(zio_t *zio)
 808 {
 809         vdev_t *vd = zio->io_vd;
 810 
 811         /*
 812          * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
 813          * the device has been removed.  If this is the case, then we trigger an
 814          * asynchronous removal of the device. Otherwise, probe the device and
 815          * make sure it's still accessible.
 816          */
 817         if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
 818                 vdev_disk_t *dvd = vd->vdev_tsd;
 819                 int state = DKIO_NONE;
 820 
 821                 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
 822                     FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
 823                         /*
 824                          * We post the resource as soon as possible, instead of
 825                          * when the async removal actually happens, because the
 826                          * DE is using this information to discard previous I/O
 827                          * errors.
 828                          */
 829                         zfs_post_remove(zio->io_spa, vd);
 830                         vd->vdev_remove_wanted = B_TRUE;
 831                         spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
 832                 } else if (!vd->vdev_delayed_close) {
 833                         vd->vdev_delayed_close = B_TRUE;
 834                 }
 835         }
 836 }
 837 
 838 vdev_ops_t vdev_disk_ops = {
 839         vdev_disk_open,
 840         vdev_disk_close,
 841         vdev_default_asize,
 842         vdev_disk_io_start,
 843         vdev_disk_io_done,
 844         NULL,
 845         vdev_disk_hold,
 846         vdev_disk_rele,
 847         VDEV_TYPE_DISK,         /* name of this vdev type */
 848         B_TRUE                  /* leaf vdev */
 849 };
 850 
 851 /*
 852  * Given the root disk device devid or pathname, read the label from
 853  * the device, and construct a configuration nvlist.
 854  */
 855 int
 856 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
 857 {
 858         ldi_handle_t vd_lh;
 859         vdev_label_t *label;
 860         uint64_t s, size;
 861         int l;
 862         ddi_devid_t tmpdevid;
 863         int error = -1;
 864         char *minor_name;
 865 
 866         /*
 867          * Read the device label and build the nvlist.
 868          */
 869         if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
 870             &minor_name) == 0) {
 871                 error = ldi_open_by_devid(tmpdevid, minor_name,
 872                     FREAD, kcred, &vd_lh, zfs_li);
 873                 ddi_devid_free(tmpdevid);
 874                 ddi_devid_str_free(minor_name);
 875         }
 876 
 877         if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
 878             zfs_li)))
 879                 return (error);
 880 
 881         if (ldi_get_size(vd_lh, &s)) {
 882                 (void) ldi_close(vd_lh, FREAD, kcred);
 883                 return (SET_ERROR(EIO));
 884         }
 885 
 886         size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
 887         label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
 888 
 889         *config = NULL;
 890         for (l = 0; l < VDEV_LABELS; l++) {
 891                 uint64_t offset, state, txg = 0;
 892 
 893                 /* read vdev label */
 894                 offset = vdev_label_offset(size, l, 0);
 895                 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
 896                     VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
 897                         continue;
 898 
 899                 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
 900                     sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
 901                         *config = NULL;
 902                         continue;
 903                 }
 904 
 905                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
 906                     &state) != 0 || state >= POOL_STATE_DESTROYED) {
 907                         nvlist_free(*config);
 908                         *config = NULL;
 909                         continue;
 910                 }
 911 
 912                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
 913                     &txg) != 0 || txg == 0) {
 914                         nvlist_free(*config);
 915                         *config = NULL;
 916                         continue;
 917                 }
 918 
 919                 break;
 920         }
 921 
 922         kmem_free(label, sizeof (vdev_label_t));
 923         (void) ldi_close(vd_lh, FREAD, kcred);
 924         if (*config == NULL)
 925                 error = SET_ERROR(EIDRM);
 926 
 927         return (error);
 928 }
--- EOF ---