1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51489Swebaker * Common Development and Distribution License (the "License"). 61489Swebaker * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 22*8876SLin.Ling@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/zfs_context.h> 27789Sahrens #include <sys/spa.h> 286423Sgw25295 #include <sys/refcount.h> 29789Sahrens #include <sys/vdev_disk.h> 30789Sahrens #include <sys/vdev_impl.h> 31789Sahrens #include <sys/fs/zfs.h> 32789Sahrens #include <sys/zio.h> 331171Seschrock #include <sys/sunldi.h> 346976Seschrock #include <sys/fm/fs/zfs.h> 35789Sahrens 36789Sahrens /* 37789Sahrens * Virtual device vector for disks. 38789Sahrens */ 39789Sahrens 40789Sahrens extern ldi_ident_t zfs_li; 41789Sahrens 42789Sahrens typedef struct vdev_disk_buf { 43789Sahrens buf_t vdb_buf; 44789Sahrens zio_t *vdb_io; 45789Sahrens } vdev_disk_buf_t; 46789Sahrens 47789Sahrens static int 487754SJeff.Bonwick@Sun.COM vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 49789Sahrens { 508241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 51789Sahrens vdev_disk_t *dvd; 527754SJeff.Bonwick@Sun.COM struct dk_minfo dkm; 537754SJeff.Bonwick@Sun.COM int error; 545329Sgw25295 dev_t dev; 557754SJeff.Bonwick@Sun.COM int otyp; 56789Sahrens 57789Sahrens /* 58789Sahrens * We must have a pathname, and it must be absolute. 59789Sahrens */ 60789Sahrens if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 61789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 62789Sahrens return (EINVAL); 63789Sahrens } 64789Sahrens 65789Sahrens dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 66789Sahrens 67789Sahrens /* 68789Sahrens * When opening a disk device, we want to preserve the user's original 69789Sahrens * intent. We always want to open the device by the path the user gave 70789Sahrens * us, even if it is one of multiple paths to the save device. But we 71789Sahrens * also want to be able to survive disks being removed/recabled. 72789Sahrens * Therefore the sequence of opening devices is: 73789Sahrens * 741171Seschrock * 1. Try opening the device by path. For legacy pools without the 751171Seschrock * 'whole_disk' property, attempt to fix the path by appending 's0'. 76789Sahrens * 77789Sahrens * 2. If the devid of the device matches the stored value, return 78789Sahrens * success. 79789Sahrens * 80789Sahrens * 3. Otherwise, the device may have moved. Try opening the device 81789Sahrens * by the devid instead. 82789Sahrens * 836673Seschrock * If the vdev is part of the root pool, we avoid opening it by path. 846673Seschrock * We do this because there is no /dev path available early in boot, 856673Seschrock * and if we try to open the device by path at a later point, we can 866673Seschrock * deadlock when devfsadm attempts to open the underlying backing store 876673Seschrock * file. 88789Sahrens */ 89789Sahrens if (vd->vdev_devid != NULL) { 90789Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 91789Sahrens &dvd->vd_minor) != 0) { 92789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 93789Sahrens return (EINVAL); 94789Sahrens } 95789Sahrens } 96789Sahrens 97789Sahrens error = EINVAL; /* presume failure */ 98789Sahrens 998241SJeff.Bonwick@Sun.COM if (vd->vdev_path != NULL && !spa_is_root(spa)) { 100789Sahrens ddi_devid_t devid; 101789Sahrens 1021171Seschrock if (vd->vdev_wholedisk == -1ULL) { 1031171Seschrock size_t len = strlen(vd->vdev_path) + 3; 1041171Seschrock char *buf = kmem_alloc(len, KM_SLEEP); 1051171Seschrock ldi_handle_t lh; 1061171Seschrock 1071171Seschrock (void) snprintf(buf, len, "%ss0", vd->vdev_path); 108789Sahrens 1098241SJeff.Bonwick@Sun.COM if (ldi_open_by_name(buf, spa_mode(spa), kcred, 1101171Seschrock &lh, zfs_li) == 0) { 1111171Seschrock spa_strfree(vd->vdev_path); 1121171Seschrock vd->vdev_path = buf; 1131171Seschrock vd->vdev_wholedisk = 1ULL; 1148241SJeff.Bonwick@Sun.COM (void) ldi_close(lh, spa_mode(spa), kcred); 1151171Seschrock } else { 1161171Seschrock kmem_free(buf, len); 1171171Seschrock } 1181171Seschrock } 119789Sahrens 1208241SJeff.Bonwick@Sun.COM error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred, 1211171Seschrock &dvd->vd_lh, zfs_li); 122789Sahrens 123789Sahrens /* 124789Sahrens * Compare the devid to the stored value. 125789Sahrens */ 126789Sahrens if (error == 0 && vd->vdev_devid != NULL && 127789Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) { 128789Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 129789Sahrens error = EINVAL; 1308241SJeff.Bonwick@Sun.COM (void) ldi_close(dvd->vd_lh, spa_mode(spa), 1318241SJeff.Bonwick@Sun.COM kcred); 132789Sahrens dvd->vd_lh = NULL; 133789Sahrens } 134789Sahrens ddi_devid_free(devid); 135789Sahrens } 1361171Seschrock 1371171Seschrock /* 1381171Seschrock * If we succeeded in opening the device, but 'vdev_wholedisk' 1391171Seschrock * is not yet set, then this must be a slice. 1401171Seschrock */ 1411171Seschrock if (error == 0 && vd->vdev_wholedisk == -1ULL) 1421171Seschrock vd->vdev_wholedisk = 0; 143789Sahrens } 144789Sahrens 145789Sahrens /* 146789Sahrens * If we were unable to open by path, or the devid check fails, open by 147789Sahrens * devid instead. 148789Sahrens */ 149789Sahrens if (error != 0 && vd->vdev_devid != NULL) 150789Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 1518241SJeff.Bonwick@Sun.COM spa_mode(spa), kcred, &dvd->vd_lh, zfs_li); 152789Sahrens 1534451Seschrock /* 1544451Seschrock * If all else fails, then try opening by physical path (if available) 1554451Seschrock * or the logical path (if we failed due to the devid check). While not 1564451Seschrock * as reliable as the devid, this will give us something, and the higher 1574451Seschrock * level vdev validation will prevent us from opening the wrong device. 1584451Seschrock */ 1594451Seschrock if (error) { 1604451Seschrock if (vd->vdev_physpath != NULL && 1618269SMark.Musante@Sun.COM (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV) 1628241SJeff.Bonwick@Sun.COM error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa), 1634451Seschrock kcred, &dvd->vd_lh, zfs_li); 1644451Seschrock 1654451Seschrock /* 1664451Seschrock * Note that we don't support the legacy auto-wholedisk support 1674451Seschrock * as above. This hasn't been used in a very long time and we 1684451Seschrock * don't need to propagate its oddities to this edge condition. 1694451Seschrock */ 1708241SJeff.Bonwick@Sun.COM if (error && vd->vdev_path != NULL && !spa_is_root(spa)) 1718241SJeff.Bonwick@Sun.COM error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 1728241SJeff.Bonwick@Sun.COM kcred, &dvd->vd_lh, zfs_li); 1734451Seschrock } 1744451Seschrock 1757754SJeff.Bonwick@Sun.COM if (error) { 176789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 1777754SJeff.Bonwick@Sun.COM return (error); 1787754SJeff.Bonwick@Sun.COM } 1795329Sgw25295 180789Sahrens /* 1814451Seschrock * Once a device is opened, verify that the physical device path (if 1824451Seschrock * available) is up to date. 1834451Seschrock */ 1844451Seschrock if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 1854451Seschrock ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 1865329Sgw25295 char *physpath, *minorname; 1875329Sgw25295 1884451Seschrock physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1894451Seschrock minorname = NULL; 1904451Seschrock if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 1914451Seschrock ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 1924451Seschrock (vd->vdev_physpath == NULL || 1934451Seschrock strcmp(vd->vdev_physpath, physpath) != 0)) { 1944451Seschrock if (vd->vdev_physpath) 1954451Seschrock spa_strfree(vd->vdev_physpath); 1964451Seschrock (void) strlcat(physpath, ":", MAXPATHLEN); 1974451Seschrock (void) strlcat(physpath, minorname, MAXPATHLEN); 1984451Seschrock vd->vdev_physpath = spa_strdup(physpath); 1994451Seschrock } 2004451Seschrock if (minorname) 2014451Seschrock kmem_free(minorname, strlen(minorname) + 1); 2024451Seschrock kmem_free(physpath, MAXPATHLEN); 2034451Seschrock } 2044451Seschrock 2054451Seschrock /* 206789Sahrens * Determine the actual size of the device. 207789Sahrens */ 208789Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) { 209789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 210789Sahrens return (EINVAL); 211789Sahrens } 212789Sahrens 2131732Sbonwick /* 2141732Sbonwick * If we own the whole disk, try to enable disk write caching. 2151732Sbonwick * We ignore errors because it's OK if we can't do it. 2161732Sbonwick */ 2171489Swebaker if (vd->vdev_wholedisk == 1) { 2181732Sbonwick int wce = 1; 2191732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 2201732Sbonwick FKIOCTL, kcred, NULL); 2211732Sbonwick } 2221489Swebaker 2231732Sbonwick /* 2241732Sbonwick * Determine the device's minimum transfer size. 2251732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 2261732Sbonwick */ 2271732Sbonwick if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 2281732Sbonwick FKIOCTL, kcred, NULL) != 0) 2291732Sbonwick dkm.dki_lbsize = DEV_BSIZE; 2301489Swebaker 2311732Sbonwick *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 2321489Swebaker 2331773Seschrock /* 2341773Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will 2351773Seschrock * try again. 2361773Seschrock */ 2371773Seschrock vd->vdev_nowritecache = B_FALSE; 2381773Seschrock 239789Sahrens return (0); 240789Sahrens } 241789Sahrens 242789Sahrens static void 243789Sahrens vdev_disk_close(vdev_t *vd) 244789Sahrens { 245789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 246789Sahrens 247789Sahrens if (dvd == NULL) 248789Sahrens return; 249789Sahrens 250789Sahrens if (dvd->vd_minor != NULL) 251789Sahrens ddi_devid_str_free(dvd->vd_minor); 252789Sahrens 253789Sahrens if (dvd->vd_devid != NULL) 254789Sahrens ddi_devid_free(dvd->vd_devid); 255789Sahrens 256789Sahrens if (dvd->vd_lh != NULL) 2578241SJeff.Bonwick@Sun.COM (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 258789Sahrens 259789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 260789Sahrens vd->vdev_tsd = NULL; 261789Sahrens } 262789Sahrens 2636423Sgw25295 int 2646423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 2656423Sgw25295 uint64_t offset, int flags) 2666423Sgw25295 { 2676423Sgw25295 buf_t *bp; 2686423Sgw25295 int error = 0; 2696423Sgw25295 2706423Sgw25295 if (vd_lh == NULL) 2716423Sgw25295 return (EINVAL); 2726423Sgw25295 2736423Sgw25295 ASSERT(flags & B_READ || flags & B_WRITE); 2746423Sgw25295 2756423Sgw25295 bp = getrbuf(KM_SLEEP); 2766423Sgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 2776423Sgw25295 bp->b_bcount = size; 2786423Sgw25295 bp->b_un.b_addr = (void *)data; 2796423Sgw25295 bp->b_lblkno = lbtodb(offset); 2806423Sgw25295 bp->b_bufsize = size; 2816423Sgw25295 2826423Sgw25295 error = ldi_strategy(vd_lh, bp); 2836423Sgw25295 ASSERT(error == 0); 2846423Sgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 2856423Sgw25295 error = EIO; 2866423Sgw25295 freerbuf(bp); 2876423Sgw25295 2886423Sgw25295 return (error); 2896423Sgw25295 } 2906423Sgw25295 291789Sahrens static void 292789Sahrens vdev_disk_io_intr(buf_t *bp) 293789Sahrens { 294789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 295789Sahrens zio_t *zio = vdb->vdb_io; 296789Sahrens 2976976Seschrock /* 2986976Seschrock * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 2996976Seschrock * Rather than teach the rest of the stack about other error 3006976Seschrock * possibilities (EFAULT, etc), we normalize the error value here. 3016976Seschrock */ 3026976Seschrock zio->io_error = (geterror(bp) != 0 ? EIO : 0); 3036976Seschrock 3046976Seschrock if (zio->io_error == 0 && bp->b_resid != 0) 305789Sahrens zio->io_error = EIO; 306789Sahrens 307789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 308789Sahrens 3095530Sbonwick zio_interrupt(zio); 310789Sahrens } 311789Sahrens 312789Sahrens static void 3137762SJeff.Bonwick@Sun.COM vdev_disk_ioctl_free(zio_t *zio) 3147762SJeff.Bonwick@Sun.COM { 3157762SJeff.Bonwick@Sun.COM kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 3167762SJeff.Bonwick@Sun.COM } 3177762SJeff.Bonwick@Sun.COM 3187762SJeff.Bonwick@Sun.COM static void 319789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 320789Sahrens { 321789Sahrens zio_t *zio = zio_arg; 322789Sahrens 323789Sahrens zio->io_error = error; 324789Sahrens 3255530Sbonwick zio_interrupt(zio); 326789Sahrens } 327789Sahrens 3285530Sbonwick static int 329789Sahrens vdev_disk_io_start(zio_t *zio) 330789Sahrens { 331789Sahrens vdev_t *vd = zio->io_vd; 332789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 333789Sahrens vdev_disk_buf_t *vdb; 3347754SJeff.Bonwick@Sun.COM struct dk_callback *dkc; 335789Sahrens buf_t *bp; 3367754SJeff.Bonwick@Sun.COM int error; 337789Sahrens 338789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 339789Sahrens /* XXPOLICY */ 3405329Sgw25295 if (!vdev_readable(vd)) { 341789Sahrens zio->io_error = ENXIO; 3425530Sbonwick return (ZIO_PIPELINE_CONTINUE); 343789Sahrens } 344789Sahrens 345789Sahrens switch (zio->io_cmd) { 346789Sahrens 347789Sahrens case DKIOCFLUSHWRITECACHE: 348789Sahrens 3492885Sahrens if (zfs_nocacheflush) 3502885Sahrens break; 3512885Sahrens 3521773Seschrock if (vd->vdev_nowritecache) { 3531773Seschrock zio->io_error = ENOTSUP; 3541773Seschrock break; 3551773Seschrock } 3561773Seschrock 3577754SJeff.Bonwick@Sun.COM zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 3587762SJeff.Bonwick@Sun.COM zio->io_vsd_free = vdev_disk_ioctl_free; 3597754SJeff.Bonwick@Sun.COM 3607754SJeff.Bonwick@Sun.COM dkc->dkc_callback = vdev_disk_ioctl_done; 3617754SJeff.Bonwick@Sun.COM dkc->dkc_flag = FLUSH_VOLATILE; 3627754SJeff.Bonwick@Sun.COM dkc->dkc_cookie = zio; 363789Sahrens 364789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 3657754SJeff.Bonwick@Sun.COM (uintptr_t)dkc, FKIOCTL, kcred, NULL); 366789Sahrens 367789Sahrens if (error == 0) { 368789Sahrens /* 369789Sahrens * The ioctl will be done asychronously, 370789Sahrens * and will call vdev_disk_ioctl_done() 371789Sahrens * upon completion. 372789Sahrens */ 3735530Sbonwick return (ZIO_PIPELINE_STOP); 3745530Sbonwick } 3755530Sbonwick 3765530Sbonwick if (error == ENOTSUP || error == ENOTTY) { 3771773Seschrock /* 3784455Smishra * If we get ENOTSUP or ENOTTY, we know that 3794455Smishra * no future attempts will ever succeed. 3804455Smishra * In this case we set a persistent bit so 3814455Smishra * that we don't bother with the ioctl in the 3824455Smishra * future. 3831773Seschrock */ 3841773Seschrock vd->vdev_nowritecache = B_TRUE; 385789Sahrens } 386789Sahrens zio->io_error = error; 3871773Seschrock 388789Sahrens break; 389789Sahrens 390789Sahrens default: 391789Sahrens zio->io_error = ENOTSUP; 392789Sahrens } 393789Sahrens 3945530Sbonwick return (ZIO_PIPELINE_CONTINUE); 395789Sahrens } 396789Sahrens 397789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 398789Sahrens 399789Sahrens vdb->vdb_io = zio; 400789Sahrens bp = &vdb->vdb_buf; 401789Sahrens 402789Sahrens bioinit(bp); 4037754SJeff.Bonwick@Sun.COM bp->b_flags = B_BUSY | B_NOCACHE | 4047754SJeff.Bonwick@Sun.COM (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE) | 4057754SJeff.Bonwick@Sun.COM ((zio->io_flags & ZIO_FLAG_IO_RETRY) ? 0 : B_FAILFAST); 406789Sahrens bp->b_bcount = zio->io_size; 407789Sahrens bp->b_un.b_addr = zio->io_data; 408789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 409789Sahrens bp->b_bufsize = zio->io_size; 410789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 411789Sahrens 412789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 4137754SJeff.Bonwick@Sun.COM VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0); 4145530Sbonwick 4155530Sbonwick return (ZIO_PIPELINE_STOP); 416789Sahrens } 417789Sahrens 4187754SJeff.Bonwick@Sun.COM static void 419789Sahrens vdev_disk_io_done(zio_t *zio) 420789Sahrens { 4217754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd; 4221544Seschrock 4234451Seschrock /* 4244451Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 4254451Seschrock * the device has been removed. If this is the case, then we trigger an 4265329Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and 4275369Sgw25295 * make sure it's still accessible. 4284451Seschrock */ 4294451Seschrock if (zio->io_error == EIO) { 4305329Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd; 4317754SJeff.Bonwick@Sun.COM int state = DKIO_NONE; 4325329Sgw25295 4337754SJeff.Bonwick@Sun.COM if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 4347754SJeff.Bonwick@Sun.COM FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 4354451Seschrock vd->vdev_remove_wanted = B_TRUE; 4364451Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 4374451Seschrock } 4384451Seschrock } 439789Sahrens } 440789Sahrens 441789Sahrens vdev_ops_t vdev_disk_ops = { 442789Sahrens vdev_disk_open, 443789Sahrens vdev_disk_close, 444789Sahrens vdev_default_asize, 445789Sahrens vdev_disk_io_start, 446789Sahrens vdev_disk_io_done, 447789Sahrens NULL, 448789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 449789Sahrens B_TRUE /* leaf vdev */ 450789Sahrens }; 4516423Sgw25295 4526423Sgw25295 /* 4537147Staylor * Given the root disk device devid or pathname, read the label from 4547147Staylor * the device, and construct a configuration nvlist. 4556423Sgw25295 */ 4567539SLin.Ling@Sun.COM int 4577539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 4586423Sgw25295 { 4596423Sgw25295 ldi_handle_t vd_lh; 4606423Sgw25295 vdev_label_t *label; 4616423Sgw25295 uint64_t s, size; 4626423Sgw25295 int l; 4637147Staylor ddi_devid_t tmpdevid; 4647687SLin.Ling@Sun.COM int error = -1; 4657147Staylor char *minor_name; 4666423Sgw25295 4676423Sgw25295 /* 4686423Sgw25295 * Read the device label and build the nvlist. 4696423Sgw25295 */ 4707687SLin.Ling@Sun.COM if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 4717147Staylor &minor_name) == 0) { 4727147Staylor error = ldi_open_by_devid(tmpdevid, minor_name, 4738241SJeff.Bonwick@Sun.COM FREAD, kcred, &vd_lh, zfs_li); 4747147Staylor ddi_devid_free(tmpdevid); 4757147Staylor ddi_devid_str_free(minor_name); 4767147Staylor } 4777147Staylor 4787687SLin.Ling@Sun.COM if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 4797687SLin.Ling@Sun.COM zfs_li))) 4807539SLin.Ling@Sun.COM return (error); 4816423Sgw25295 4826673Seschrock if (ldi_get_size(vd_lh, &s)) { 4836673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 4847539SLin.Ling@Sun.COM return (EIO); 4856673Seschrock } 4866423Sgw25295 4876423Sgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 4886423Sgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 4896423Sgw25295 4906423Sgw25295 for (l = 0; l < VDEV_LABELS; l++) { 4916423Sgw25295 uint64_t offset, state, txg = 0; 4926423Sgw25295 4936423Sgw25295 /* read vdev label */ 4946423Sgw25295 offset = vdev_label_offset(size, l, 0); 4956423Sgw25295 if (vdev_disk_physio(vd_lh, (caddr_t)label, 496*8876SLin.Ling@Sun.COM VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 4976423Sgw25295 continue; 4986423Sgw25295 4996423Sgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 5007539SLin.Ling@Sun.COM sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 5017539SLin.Ling@Sun.COM *config = NULL; 5026423Sgw25295 continue; 5036423Sgw25295 } 5046423Sgw25295 5057539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 5066423Sgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) { 5077539SLin.Ling@Sun.COM nvlist_free(*config); 5087539SLin.Ling@Sun.COM *config = NULL; 5096423Sgw25295 continue; 5106423Sgw25295 } 5116423Sgw25295 5127539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 5136423Sgw25295 &txg) != 0 || txg == 0) { 5147539SLin.Ling@Sun.COM nvlist_free(*config); 5157539SLin.Ling@Sun.COM *config = NULL; 5166423Sgw25295 continue; 5176423Sgw25295 } 5186423Sgw25295 5196423Sgw25295 break; 5206423Sgw25295 } 5216423Sgw25295 5226423Sgw25295 kmem_free(label, sizeof (vdev_label_t)); 5236673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 5246673Seschrock 5257539SLin.Ling@Sun.COM return (error); 5266423Sgw25295 } 527