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 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 static void
  46 vdev_disk_hold(vdev_t *vd)
  47 {
  48         ddi_devid_t devid;
  49         char *minor;
  50 
  51         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
  52 
  53         /*
  54          * We must have a pathname, and it must be absolute.
  55          */
  56         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
  57                 return;
  58 
  59         /*
  60          * Only prefetch path and devid info if the device has
  61          * never been opened.
  62          */
  63         if (vd->vdev_tsd != NULL)
  64                 return;
  65 
  66         if (vd->vdev_wholedisk == -1ULL) {
  67                 size_t len = strlen(vd->vdev_path) + 3;
  68                 char *buf = kmem_alloc(len, KM_SLEEP);
  69 
  70                 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
  71 
  72                 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
  73                 kmem_free(buf, len);
  74         }
  75 
  76         if (vd->vdev_name_vp == NULL)
  77                 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
  78 
  79         if (vd->vdev_devid != NULL &&
  80             ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
  81                 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
  82                 ddi_devid_str_free(minor);
  83                 ddi_devid_free(devid);
  84         }
  85 }
  86 
  87 static void
  88 vdev_disk_rele(vdev_t *vd)
  89 {
  90         ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
  91 
  92         if (vd->vdev_name_vp) {
  93                 VN_RELE_ASYNC(vd->vdev_name_vp,
  94                     dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
  95                 vd->vdev_name_vp = NULL;
  96         }
  97         if (vd->vdev_devid_vp) {
  98                 VN_RELE_ASYNC(vd->vdev_devid_vp,
  99                     dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
 100                 vd->vdev_devid_vp = NULL;
 101         }
 102 }
 103 
 104 static uint64_t
 105 vdev_disk_get_space(vdev_t *vd, uint64_t capacity, uint_t blksz)
 106 {
 107         ASSERT(vd->vdev_wholedisk);
 108 
 109         vdev_disk_t *dvd = vd->vdev_tsd;
 110         dk_efi_t dk_ioc;
 111         efi_gpt_t *efi;
 112         uint64_t avail_space = 0;
 113         int efisize = EFI_LABEL_SIZE * 2;
 114 
 115         dk_ioc.dki_data = kmem_alloc(efisize, KM_SLEEP);
 116         dk_ioc.dki_lba = 1;
 117         dk_ioc.dki_length = efisize;
 118         dk_ioc.dki_data_64 = (uint64_t)(uintptr_t)dk_ioc.dki_data;
 119         efi = dk_ioc.dki_data;
 120 
 121         if (ldi_ioctl(dvd->vd_lh, DKIOCGETEFI, (intptr_t)&dk_ioc,
 122             FKIOCTL, kcred, NULL) == 0) {
 123                 uint64_t efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
 124 
 125                 zfs_dbgmsg("vdev %s, capacity %llu, altern lba %llu",
 126                     vd->vdev_path, capacity, efi_altern_lba);
 127                 if (capacity > efi_altern_lba)
 128                         avail_space = (capacity - efi_altern_lba) * blksz;
 129         }
 130         kmem_free(dk_ioc.dki_data, efisize);
 131         return (avail_space);
 132 }
 133 
 134 static int
 135 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
 136     uint64_t *ashift)
 137 {
 138         spa_t *spa = vd->vdev_spa;
 139         vdev_disk_t *dvd;
 140         struct dk_minfo_ext dkmext;
 141         int error;
 142         dev_t dev;
 143         int otyp;
 144 
 145         /*
 146          * We must have a pathname, and it must be absolute.
 147          */
 148         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
 149                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 150                 return (EINVAL);
 151         }
 152 
 153         /*
 154          * Reopen the device if it's not currently open. Otherwise,
 155          * just update the physical size of the device.
 156          */
 157         if (vd->vdev_tsd != NULL) {
 158                 ASSERT(vd->vdev_reopening);
 159                 dvd = vd->vdev_tsd;
 160                 goto skip_open;
 161         }
 162 
 163         dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
 164 
 165         /*
 166          * When opening a disk device, we want to preserve the user's original
 167          * intent.  We always want to open the device by the path the user gave
 168          * us, even if it is one of multiple paths to the same device.  But we
 169          * also want to be able to survive disks being removed/recabled.
 170          * Therefore the sequence of opening devices is:
 171          *
 172          * 1. Try opening the device by path.  For legacy pools without the
 173          *    'whole_disk' property, attempt to fix the path by appending 's0'.
 174          *
 175          * 2. If the devid of the device matches the stored value, return
 176          *    success.
 177          *
 178          * 3. Otherwise, the device may have moved.  Try opening the device
 179          *    by the devid instead.
 180          */
 181         if (vd->vdev_devid != NULL) {
 182                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
 183                     &dvd->vd_minor) != 0) {
 184                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
 185                         return (EINVAL);
 186                 }
 187         }
 188 
 189         error = EINVAL;         /* presume failure */
 190 
 191         if (vd->vdev_path != NULL) {
 192                 ddi_devid_t devid;
 193 
 194                 if (vd->vdev_wholedisk == -1ULL) {
 195                         size_t len = strlen(vd->vdev_path) + 3;
 196                         char *buf = kmem_alloc(len, KM_SLEEP);
 197                         ldi_handle_t lh;
 198 
 199                         (void) snprintf(buf, len, "%ss0", vd->vdev_path);
 200 
 201                         if (ldi_open_by_name(buf, spa_mode(spa), kcred,
 202                             &lh, zfs_li) == 0) {
 203                                 spa_strfree(vd->vdev_path);
 204                                 vd->vdev_path = buf;
 205                                 vd->vdev_wholedisk = 1ULL;
 206                                 (void) ldi_close(lh, spa_mode(spa), kcred);
 207                         } else {
 208                                 kmem_free(buf, len);
 209                         }
 210                 }
 211 
 212                 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
 213                     &dvd->vd_lh, zfs_li);
 214 
 215                 /*
 216                  * Compare the devid to the stored value.
 217                  */
 218                 if (error == 0 && vd->vdev_devid != NULL &&
 219                     ldi_get_devid(dvd->vd_lh, &devid) == 0) {
 220                         if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
 221                                 error = EINVAL;
 222                                 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
 223                                     kcred);
 224                                 dvd->vd_lh = NULL;
 225                         }
 226                         ddi_devid_free(devid);
 227                 }
 228 
 229                 /*
 230                  * If we succeeded in opening the device, but 'vdev_wholedisk'
 231                  * is not yet set, then this must be a slice.
 232                  */
 233                 if (error == 0 && vd->vdev_wholedisk == -1ULL)
 234                         vd->vdev_wholedisk = 0;
 235         }
 236 
 237         /*
 238          * If we were unable to open by path, or the devid check fails, open by
 239          * devid instead.
 240          */
 241         if (error != 0 && vd->vdev_devid != NULL)
 242                 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
 243                     spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
 244 
 245         /*
 246          * If all else fails, then try opening by physical path (if available)
 247          * or the logical path (if we failed due to the devid check).  While not
 248          * as reliable as the devid, this will give us something, and the higher
 249          * level vdev validation will prevent us from opening the wrong device.
 250          */
 251         if (error) {
 252                 if (vd->vdev_physpath != NULL &&
 253                     (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
 254                         error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
 255                             kcred, &dvd->vd_lh, zfs_li);
 256 
 257                 /*
 258                  * Note that we don't support the legacy auto-wholedisk support
 259                  * as above.  This hasn't been used in a very long time and we
 260                  * don't need to propagate its oddities to this edge condition.
 261                  */
 262                 if (error && vd->vdev_path != NULL)
 263                         error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
 264                             kcred, &dvd->vd_lh, zfs_li);
 265         }
 266 
 267         if (error) {
 268                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 269                 return (error);
 270         }
 271 
 272         /*
 273          * Once a device is opened, verify that the physical device path (if
 274          * available) is up to date.
 275          */
 276         if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
 277             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
 278                 char *physpath, *minorname;
 279 
 280                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 281                 minorname = NULL;
 282                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
 283                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
 284                     (vd->vdev_physpath == NULL ||
 285                     strcmp(vd->vdev_physpath, physpath) != 0)) {
 286                         if (vd->vdev_physpath)
 287                                 spa_strfree(vd->vdev_physpath);
 288                         (void) strlcat(physpath, ":", MAXPATHLEN);
 289                         (void) strlcat(physpath, minorname, MAXPATHLEN);
 290                         vd->vdev_physpath = spa_strdup(physpath);
 291                 }
 292                 if (minorname)
 293                         kmem_free(minorname, strlen(minorname) + 1);
 294                 kmem_free(physpath, MAXPATHLEN);
 295         }
 296 
 297 skip_open:
 298         /*
 299          * Determine the actual size of the device.
 300          */
 301         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
 302                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 303                 return (EINVAL);
 304         }
 305 
 306         /*
 307          * Determine the device's minimum transfer size.
 308          * If the ioctl isn't supported, assume DEV_BSIZE.
 309          */
 310         if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext,
 311             FKIOCTL, kcred, NULL) != 0)
 312                 dkmext.dki_pbsize = DEV_BSIZE;
 313 
 314         *ashift = highbit(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1;
 315 
 316         if (vd->vdev_wholedisk == 1) {
 317                 uint64_t capacity = dkmext.dki_capacity - 1;
 318                 uint64_t blksz = dkmext.dki_lbsize;
 319                 int wce = 1;
 320 
 321                 /*
 322                  * If we own the whole disk, try to enable disk write caching.
 323                  * We ignore errors because it's OK if we can't do it.
 324                  */
 325                 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
 326                     FKIOCTL, kcred, NULL);
 327 
 328                 *max_psize = *psize + vdev_disk_get_space(vd, capacity, blksz);
 329                 zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
 330                     "max_psize %llu", vd->vdev_path, *psize, *max_psize);
 331         } else {
 332                 *max_psize = *psize;
 333         }
 334 
 335         /*
 336          * Clear the nowritecache bit, so that on a vdev_reopen() we will
 337          * try again.
 338          */
 339         vd->vdev_nowritecache = B_FALSE;
 340 
 341         return (0);
 342 }
 343 
 344 static void
 345 vdev_disk_close(vdev_t *vd)
 346 {
 347         vdev_disk_t *dvd = vd->vdev_tsd;
 348 
 349         if (vd->vdev_reopening || dvd == NULL)
 350                 return;
 351 
 352         if (dvd->vd_minor != NULL)
 353                 ddi_devid_str_free(dvd->vd_minor);
 354 
 355         if (dvd->vd_devid != NULL)
 356                 ddi_devid_free(dvd->vd_devid);
 357 
 358         if (dvd->vd_lh != NULL)
 359                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
 360 
 361         vd->vdev_delayed_close = B_FALSE;
 362         kmem_free(dvd, sizeof (vdev_disk_t));
 363         vd->vdev_tsd = NULL;
 364 }
 365 
 366 int
 367 vdev_disk_physio(vdev_t *vd, caddr_t data,
 368     size_t size, uint64_t offset, int flags)
 369 {
 370         vdev_disk_t *dvd = vd->vdev_tsd;
 371 
 372         /*
 373          * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
 374          * Nothing to be done here but return failure.
 375          */
 376         if (dvd == NULL)
 377                 return (EIO);
 378 
 379         ASSERT(vd->vdev_ops == &vdev_disk_ops);
 380         return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
 381 }
 382 
 383 int
 384 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
 385     size_t size, uint64_t offset, int flags)
 386 {
 387         buf_t *bp;
 388         int error = 0;
 389 
 390         if (vd_lh == NULL)
 391                 return (EINVAL);
 392 
 393         ASSERT(flags & B_READ || flags & B_WRITE);
 394 
 395         bp = getrbuf(KM_SLEEP);
 396         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
 397         bp->b_bcount = size;
 398         bp->b_un.b_addr = (void *)data;
 399         bp->b_lblkno = lbtodb(offset);
 400         bp->b_bufsize = size;
 401 
 402         error = ldi_strategy(vd_lh, bp);
 403         ASSERT(error == 0);
 404         if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
 405                 error = EIO;
 406         freerbuf(bp);
 407 
 408         return (error);
 409 }
 410 
 411 static void
 412 vdev_disk_io_intr(buf_t *bp)
 413 {
 414         vdev_buf_t *vb = (vdev_buf_t *)bp;
 415         zio_t *zio = vb->vb_io;
 416 
 417         /*
 418          * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
 419          * Rather than teach the rest of the stack about other error
 420          * possibilities (EFAULT, etc), we normalize the error value here.
 421          */
 422         zio->io_error = (geterror(bp) != 0 ? EIO : 0);
 423 
 424         if (zio->io_error == 0 && bp->b_resid != 0)
 425                 zio->io_error = EIO;
 426 
 427         kmem_free(vb, sizeof (vdev_buf_t));
 428 
 429         zio_interrupt(zio);
 430 }
 431 
 432 static void
 433 vdev_disk_ioctl_free(zio_t *zio)
 434 {
 435         kmem_free(zio->io_vsd, sizeof (struct dk_callback));
 436 }
 437 
 438 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
 439         vdev_disk_ioctl_free,
 440         zio_vsd_default_cksum_report
 441 };
 442 
 443 static void
 444 vdev_disk_ioctl_done(void *zio_arg, int error)
 445 {
 446         zio_t *zio = zio_arg;
 447 
 448         zio->io_error = error;
 449 
 450         zio_interrupt(zio);
 451 }
 452 
 453 static int
 454 vdev_disk_io_start(zio_t *zio)
 455 {
 456         vdev_t *vd = zio->io_vd;
 457         vdev_disk_t *dvd = vd->vdev_tsd;
 458         vdev_buf_t *vb;
 459         struct dk_callback *dkc;
 460         buf_t *bp;
 461         int error;
 462 
 463         if (zio->io_type == ZIO_TYPE_IOCTL) {
 464                 /* XXPOLICY */
 465                 if (!vdev_readable(vd)) {
 466                         zio->io_error = ENXIO;
 467                         return (ZIO_PIPELINE_CONTINUE);
 468                 }
 469 
 470                 switch (zio->io_cmd) {
 471 
 472                 case DKIOCFLUSHWRITECACHE:
 473 
 474                         if (zfs_nocacheflush)
 475                                 break;
 476 
 477                         if (vd->vdev_nowritecache) {
 478                                 zio->io_error = ENOTSUP;
 479                                 break;
 480                         }
 481 
 482                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
 483                         zio->io_vsd_ops = &vdev_disk_vsd_ops;
 484 
 485                         dkc->dkc_callback = vdev_disk_ioctl_done;
 486                         dkc->dkc_flag = FLUSH_VOLATILE;
 487                         dkc->dkc_cookie = zio;
 488 
 489                         error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
 490                             (uintptr_t)dkc, FKIOCTL, kcred, NULL);
 491 
 492                         if (error == 0) {
 493                                 /*
 494                                  * The ioctl will be done asychronously,
 495                                  * and will call vdev_disk_ioctl_done()
 496                                  * upon completion.
 497                                  */
 498                                 return (ZIO_PIPELINE_STOP);
 499                         }
 500 
 501                         if (error == ENOTSUP || error == ENOTTY) {
 502                                 /*
 503                                  * If we get ENOTSUP or ENOTTY, we know that
 504                                  * no future attempts will ever succeed.
 505                                  * In this case we set a persistent bit so
 506                                  * that we don't bother with the ioctl in the
 507                                  * future.
 508                                  */
 509                                 vd->vdev_nowritecache = B_TRUE;
 510                         }
 511                         zio->io_error = error;
 512 
 513                         break;
 514 
 515                 default:
 516                         zio->io_error = ENOTSUP;
 517                 }
 518 
 519                 return (ZIO_PIPELINE_CONTINUE);
 520         }
 521 
 522         vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
 523 
 524         vb->vb_io = zio;
 525         bp = &vb->vb_buf;
 526 
 527         bioinit(bp);
 528         bp->b_flags = B_BUSY | B_NOCACHE |
 529             (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
 530         if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
 531                 bp->b_flags |= B_FAILFAST;
 532         bp->b_bcount = zio->io_size;
 533         bp->b_un.b_addr = zio->io_data;
 534         bp->b_lblkno = lbtodb(zio->io_offset);
 535         bp->b_bufsize = zio->io_size;
 536         bp->b_iodone = (int (*)())vdev_disk_io_intr;
 537 
 538         zfs_zone_zio_start(zio);
 539 
 540         /* ldi_strategy() will return non-zero only on programming errors */
 541         VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
 542 
 543         return (ZIO_PIPELINE_STOP);
 544 }
 545 
 546 static void
 547 vdev_disk_io_done(zio_t *zio)
 548 {
 549         vdev_t *vd = zio->io_vd;
 550 
 551         zfs_zone_zio_done(zio);
 552 
 553         /*
 554          * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
 555          * the device has been removed.  If this is the case, then we trigger an
 556          * asynchronous removal of the device. Otherwise, probe the device and
 557          * make sure it's still accessible.
 558          */
 559         if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
 560                 vdev_disk_t *dvd = vd->vdev_tsd;
 561                 int state = DKIO_NONE;
 562 
 563                 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
 564                     FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
 565                         /*
 566                          * We post the resource as soon as possible, instead of
 567                          * when the async removal actually happens, because the
 568                          * DE is using this information to discard previous I/O
 569                          * errors.
 570                          */
 571                         zfs_post_remove(zio->io_spa, vd);
 572                         vd->vdev_remove_wanted = B_TRUE;
 573                         spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
 574                 } else if (!vd->vdev_delayed_close) {
 575                         vd->vdev_delayed_close = B_TRUE;
 576                 }
 577         }
 578 }
 579 
 580 vdev_ops_t vdev_disk_ops = {
 581         vdev_disk_open,
 582         vdev_disk_close,
 583         vdev_default_asize,
 584         vdev_disk_io_start,
 585         vdev_disk_io_done,
 586         NULL,
 587         vdev_disk_hold,
 588         vdev_disk_rele,
 589         VDEV_TYPE_DISK,         /* name of this vdev type */
 590         B_TRUE                  /* leaf vdev */
 591 };
 592 
 593 /*
 594  * Given the root disk device devid or pathname, read the label from
 595  * the device, and construct a configuration nvlist.
 596  */
 597 int
 598 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
 599 {
 600         ldi_handle_t vd_lh;
 601         vdev_label_t *label;
 602         uint64_t s, size;
 603         int l;
 604         ddi_devid_t tmpdevid;
 605         int error = -1;
 606         char *minor_name;
 607 
 608         /*
 609          * Read the device label and build the nvlist.
 610          */
 611         if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
 612             &minor_name) == 0) {
 613                 error = ldi_open_by_devid(tmpdevid, minor_name,
 614                     FREAD, kcred, &vd_lh, zfs_li);
 615                 ddi_devid_free(tmpdevid);
 616                 ddi_devid_str_free(minor_name);
 617         }
 618 
 619         if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
 620             zfs_li)))
 621                 return (error);
 622 
 623         if (ldi_get_size(vd_lh, &s)) {
 624                 (void) ldi_close(vd_lh, FREAD, kcred);
 625                 return (EIO);
 626         }
 627 
 628         size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
 629         label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
 630 
 631         *config = NULL;
 632         for (l = 0; l < VDEV_LABELS; l++) {
 633                 uint64_t offset, state, txg = 0;
 634 
 635                 /* read vdev label */
 636                 offset = vdev_label_offset(size, l, 0);
 637                 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
 638                     VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
 639                         continue;
 640 
 641                 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
 642                     sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
 643                         *config = NULL;
 644                         continue;
 645                 }
 646 
 647                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
 648                     &state) != 0 || state >= POOL_STATE_DESTROYED) {
 649                         nvlist_free(*config);
 650                         *config = NULL;
 651                         continue;
 652                 }
 653 
 654                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
 655                     &txg) != 0 || txg == 0) {
 656                         nvlist_free(*config);
 657                         *config = NULL;
 658                         continue;
 659                 }
 660 
 661                 break;
 662         }
 663 
 664         kmem_free(label, sizeof (vdev_label_t));
 665         (void) ldi_close(vd_lh, FREAD, kcred);
 666         if (*config == NULL)
 667                 error = EIDRM;
 668 
 669         return (error);
 670 }