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 /* 228876SLin.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 * 839988SGeorge.Wilson@Sun.COM * If the vdev is part of the root pool, we avoid opening it by path 849988SGeorge.Wilson@Sun.COM * unless we're adding (i.e. attaching) it to the vdev namespace. 856673Seschrock * We do this because there is no /dev path available early in boot, 866673Seschrock * and if we try to open the device by path at a later point, we can 876673Seschrock * deadlock when devfsadm attempts to open the underlying backing store 886673Seschrock * file. 89789Sahrens */ 90789Sahrens if (vd->vdev_devid != NULL) { 91789Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 92789Sahrens &dvd->vd_minor) != 0) { 93789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 94789Sahrens return (EINVAL); 95789Sahrens } 96789Sahrens } 97789Sahrens 98789Sahrens error = EINVAL; /* presume failure */ 99789Sahrens 1009988SGeorge.Wilson@Sun.COM if (vd->vdev_path != NULL && (!spa_is_root(spa) || 1019988SGeorge.Wilson@Sun.COM spa_lookup_by_guid(spa, vd->vdev_guid, B_FALSE) == NULL)) { 102789Sahrens ddi_devid_t devid; 103789Sahrens 1041171Seschrock if (vd->vdev_wholedisk == -1ULL) { 1051171Seschrock size_t len = strlen(vd->vdev_path) + 3; 1061171Seschrock char *buf = kmem_alloc(len, KM_SLEEP); 1071171Seschrock ldi_handle_t lh; 1081171Seschrock 1091171Seschrock (void) snprintf(buf, len, "%ss0", vd->vdev_path); 110789Sahrens 1118241SJeff.Bonwick@Sun.COM if (ldi_open_by_name(buf, spa_mode(spa), kcred, 1121171Seschrock &lh, zfs_li) == 0) { 1131171Seschrock spa_strfree(vd->vdev_path); 1141171Seschrock vd->vdev_path = buf; 1151171Seschrock vd->vdev_wholedisk = 1ULL; 1168241SJeff.Bonwick@Sun.COM (void) ldi_close(lh, spa_mode(spa), kcred); 1171171Seschrock } else { 1181171Seschrock kmem_free(buf, len); 1191171Seschrock } 1201171Seschrock } 121789Sahrens 1228241SJeff.Bonwick@Sun.COM error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred, 1231171Seschrock &dvd->vd_lh, zfs_li); 124789Sahrens 125789Sahrens /* 126789Sahrens * Compare the devid to the stored value. 127789Sahrens */ 128789Sahrens if (error == 0 && vd->vdev_devid != NULL && 129789Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) { 130789Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 131789Sahrens error = EINVAL; 1328241SJeff.Bonwick@Sun.COM (void) ldi_close(dvd->vd_lh, spa_mode(spa), 1338241SJeff.Bonwick@Sun.COM kcred); 134789Sahrens dvd->vd_lh = NULL; 135789Sahrens } 136789Sahrens ddi_devid_free(devid); 137789Sahrens } 1381171Seschrock 1391171Seschrock /* 1401171Seschrock * If we succeeded in opening the device, but 'vdev_wholedisk' 1411171Seschrock * is not yet set, then this must be a slice. 1421171Seschrock */ 1431171Seschrock if (error == 0 && vd->vdev_wholedisk == -1ULL) 1441171Seschrock vd->vdev_wholedisk = 0; 145789Sahrens } 146789Sahrens 147789Sahrens /* 148789Sahrens * If we were unable to open by path, or the devid check fails, open by 149789Sahrens * devid instead. 150789Sahrens */ 151789Sahrens if (error != 0 && vd->vdev_devid != NULL) 152789Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 1538241SJeff.Bonwick@Sun.COM spa_mode(spa), kcred, &dvd->vd_lh, zfs_li); 154789Sahrens 1554451Seschrock /* 1564451Seschrock * If all else fails, then try opening by physical path (if available) 1574451Seschrock * or the logical path (if we failed due to the devid check). While not 1584451Seschrock * as reliable as the devid, this will give us something, and the higher 1594451Seschrock * level vdev validation will prevent us from opening the wrong device. 1604451Seschrock */ 1614451Seschrock if (error) { 1624451Seschrock if (vd->vdev_physpath != NULL && 1638269SMark.Musante@Sun.COM (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV) 1648241SJeff.Bonwick@Sun.COM error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa), 1654451Seschrock kcred, &dvd->vd_lh, zfs_li); 1664451Seschrock 1674451Seschrock /* 1684451Seschrock * Note that we don't support the legacy auto-wholedisk support 1694451Seschrock * as above. This hasn't been used in a very long time and we 1704451Seschrock * don't need to propagate its oddities to this edge condition. 1714451Seschrock */ 1729988SGeorge.Wilson@Sun.COM if (error && vd->vdev_path != NULL && (!spa_is_root(spa) || 1739988SGeorge.Wilson@Sun.COM spa_lookup_by_guid(spa, vd->vdev_guid, B_FALSE) == NULL)) 1748241SJeff.Bonwick@Sun.COM error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 1758241SJeff.Bonwick@Sun.COM kcred, &dvd->vd_lh, zfs_li); 1764451Seschrock } 1774451Seschrock 1787754SJeff.Bonwick@Sun.COM if (error) { 179789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 1807754SJeff.Bonwick@Sun.COM return (error); 1817754SJeff.Bonwick@Sun.COM } 1825329Sgw25295 183789Sahrens /* 1844451Seschrock * Once a device is opened, verify that the physical device path (if 1854451Seschrock * available) is up to date. 1864451Seschrock */ 1874451Seschrock if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 1884451Seschrock ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 1895329Sgw25295 char *physpath, *minorname; 1905329Sgw25295 1914451Seschrock physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1924451Seschrock minorname = NULL; 1934451Seschrock if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 1944451Seschrock ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 1954451Seschrock (vd->vdev_physpath == NULL || 1964451Seschrock strcmp(vd->vdev_physpath, physpath) != 0)) { 1974451Seschrock if (vd->vdev_physpath) 1984451Seschrock spa_strfree(vd->vdev_physpath); 1994451Seschrock (void) strlcat(physpath, ":", MAXPATHLEN); 2004451Seschrock (void) strlcat(physpath, minorname, MAXPATHLEN); 2014451Seschrock vd->vdev_physpath = spa_strdup(physpath); 2024451Seschrock } 2034451Seschrock if (minorname) 2044451Seschrock kmem_free(minorname, strlen(minorname) + 1); 2054451Seschrock kmem_free(physpath, MAXPATHLEN); 2064451Seschrock } 2074451Seschrock 2084451Seschrock /* 209789Sahrens * Determine the actual size of the device. 210789Sahrens */ 211789Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) { 212789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 213789Sahrens return (EINVAL); 214789Sahrens } 215789Sahrens 2161732Sbonwick /* 2171732Sbonwick * If we own the whole disk, try to enable disk write caching. 2181732Sbonwick * We ignore errors because it's OK if we can't do it. 2191732Sbonwick */ 2201489Swebaker if (vd->vdev_wholedisk == 1) { 2211732Sbonwick int wce = 1; 2221732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 2231732Sbonwick FKIOCTL, kcred, NULL); 2241732Sbonwick } 2251489Swebaker 2261732Sbonwick /* 2271732Sbonwick * Determine the device's minimum transfer size. 2281732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 2291732Sbonwick */ 2301732Sbonwick if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 2311732Sbonwick FKIOCTL, kcred, NULL) != 0) 2321732Sbonwick dkm.dki_lbsize = DEV_BSIZE; 2331489Swebaker 2341732Sbonwick *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 2351489Swebaker 2361773Seschrock /* 2371773Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will 2381773Seschrock * try again. 2391773Seschrock */ 2401773Seschrock vd->vdev_nowritecache = B_FALSE; 2411773Seschrock 242789Sahrens return (0); 243789Sahrens } 244789Sahrens 245789Sahrens static void 246789Sahrens vdev_disk_close(vdev_t *vd) 247789Sahrens { 248789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 249789Sahrens 250789Sahrens if (dvd == NULL) 251789Sahrens return; 252789Sahrens 253789Sahrens if (dvd->vd_minor != NULL) 254789Sahrens ddi_devid_str_free(dvd->vd_minor); 255789Sahrens 256789Sahrens if (dvd->vd_devid != NULL) 257789Sahrens ddi_devid_free(dvd->vd_devid); 258789Sahrens 259789Sahrens if (dvd->vd_lh != NULL) 2608241SJeff.Bonwick@Sun.COM (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 261789Sahrens 262789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 263789Sahrens vd->vdev_tsd = NULL; 264789Sahrens } 265789Sahrens 2666423Sgw25295 int 2676423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 2686423Sgw25295 uint64_t offset, int flags) 2696423Sgw25295 { 2706423Sgw25295 buf_t *bp; 2716423Sgw25295 int error = 0; 2726423Sgw25295 2736423Sgw25295 if (vd_lh == NULL) 2746423Sgw25295 return (EINVAL); 2756423Sgw25295 2766423Sgw25295 ASSERT(flags & B_READ || flags & B_WRITE); 2776423Sgw25295 2786423Sgw25295 bp = getrbuf(KM_SLEEP); 2796423Sgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 2806423Sgw25295 bp->b_bcount = size; 2816423Sgw25295 bp->b_un.b_addr = (void *)data; 2826423Sgw25295 bp->b_lblkno = lbtodb(offset); 2836423Sgw25295 bp->b_bufsize = size; 2846423Sgw25295 2856423Sgw25295 error = ldi_strategy(vd_lh, bp); 2866423Sgw25295 ASSERT(error == 0); 2876423Sgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 2886423Sgw25295 error = EIO; 2896423Sgw25295 freerbuf(bp); 2906423Sgw25295 2916423Sgw25295 return (error); 2926423Sgw25295 } 2936423Sgw25295 294789Sahrens static void 295789Sahrens vdev_disk_io_intr(buf_t *bp) 296789Sahrens { 297789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 298789Sahrens zio_t *zio = vdb->vdb_io; 299789Sahrens 3006976Seschrock /* 3016976Seschrock * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 3026976Seschrock * Rather than teach the rest of the stack about other error 3036976Seschrock * possibilities (EFAULT, etc), we normalize the error value here. 3046976Seschrock */ 3056976Seschrock zio->io_error = (geterror(bp) != 0 ? EIO : 0); 3066976Seschrock 3076976Seschrock if (zio->io_error == 0 && bp->b_resid != 0) 308789Sahrens zio->io_error = EIO; 309789Sahrens 310789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 311789Sahrens 3125530Sbonwick zio_interrupt(zio); 313789Sahrens } 314789Sahrens 315789Sahrens static void 3167762SJeff.Bonwick@Sun.COM vdev_disk_ioctl_free(zio_t *zio) 3177762SJeff.Bonwick@Sun.COM { 3187762SJeff.Bonwick@Sun.COM kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 3197762SJeff.Bonwick@Sun.COM } 3207762SJeff.Bonwick@Sun.COM 3217762SJeff.Bonwick@Sun.COM static void 322789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 323789Sahrens { 324789Sahrens zio_t *zio = zio_arg; 325789Sahrens 326789Sahrens zio->io_error = error; 327789Sahrens 3285530Sbonwick zio_interrupt(zio); 329789Sahrens } 330789Sahrens 3315530Sbonwick static int 332789Sahrens vdev_disk_io_start(zio_t *zio) 333789Sahrens { 334789Sahrens vdev_t *vd = zio->io_vd; 335789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 336789Sahrens vdev_disk_buf_t *vdb; 3377754SJeff.Bonwick@Sun.COM struct dk_callback *dkc; 338789Sahrens buf_t *bp; 3397754SJeff.Bonwick@Sun.COM int error; 340789Sahrens 341789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 342789Sahrens /* XXPOLICY */ 3435329Sgw25295 if (!vdev_readable(vd)) { 344789Sahrens zio->io_error = ENXIO; 3455530Sbonwick return (ZIO_PIPELINE_CONTINUE); 346789Sahrens } 347789Sahrens 348789Sahrens switch (zio->io_cmd) { 349789Sahrens 350789Sahrens case DKIOCFLUSHWRITECACHE: 351789Sahrens 3522885Sahrens if (zfs_nocacheflush) 3532885Sahrens break; 3542885Sahrens 3551773Seschrock if (vd->vdev_nowritecache) { 3561773Seschrock zio->io_error = ENOTSUP; 3571773Seschrock break; 3581773Seschrock } 3591773Seschrock 3607754SJeff.Bonwick@Sun.COM zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 3617762SJeff.Bonwick@Sun.COM zio->io_vsd_free = vdev_disk_ioctl_free; 3627754SJeff.Bonwick@Sun.COM 3637754SJeff.Bonwick@Sun.COM dkc->dkc_callback = vdev_disk_ioctl_done; 3647754SJeff.Bonwick@Sun.COM dkc->dkc_flag = FLUSH_VOLATILE; 3657754SJeff.Bonwick@Sun.COM dkc->dkc_cookie = zio; 366789Sahrens 367789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 3687754SJeff.Bonwick@Sun.COM (uintptr_t)dkc, FKIOCTL, kcred, NULL); 369789Sahrens 370789Sahrens if (error == 0) { 371789Sahrens /* 372789Sahrens * The ioctl will be done asychronously, 373789Sahrens * and will call vdev_disk_ioctl_done() 374789Sahrens * upon completion. 375789Sahrens */ 3765530Sbonwick return (ZIO_PIPELINE_STOP); 3775530Sbonwick } 3785530Sbonwick 3795530Sbonwick if (error == ENOTSUP || error == ENOTTY) { 3801773Seschrock /* 3814455Smishra * If we get ENOTSUP or ENOTTY, we know that 3824455Smishra * no future attempts will ever succeed. 3834455Smishra * In this case we set a persistent bit so 3844455Smishra * that we don't bother with the ioctl in the 3854455Smishra * future. 3861773Seschrock */ 3871773Seschrock vd->vdev_nowritecache = B_TRUE; 388789Sahrens } 389789Sahrens zio->io_error = error; 3901773Seschrock 391789Sahrens break; 392789Sahrens 393789Sahrens default: 394789Sahrens zio->io_error = ENOTSUP; 395789Sahrens } 396789Sahrens 3975530Sbonwick return (ZIO_PIPELINE_CONTINUE); 398789Sahrens } 399789Sahrens 400789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 401789Sahrens 402789Sahrens vdb->vdb_io = zio; 403789Sahrens bp = &vdb->vdb_buf; 404789Sahrens 405789Sahrens bioinit(bp); 4067754SJeff.Bonwick@Sun.COM bp->b_flags = B_BUSY | B_NOCACHE | 4079725SEric.Schrock@Sun.COM (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 4089725SEric.Schrock@Sun.COM if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 4099725SEric.Schrock@Sun.COM bp->b_flags |= B_FAILFAST; 410789Sahrens bp->b_bcount = zio->io_size; 411789Sahrens bp->b_un.b_addr = zio->io_data; 412789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 413789Sahrens bp->b_bufsize = zio->io_size; 414789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 415789Sahrens 416789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 4177754SJeff.Bonwick@Sun.COM VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0); 4185530Sbonwick 4195530Sbonwick return (ZIO_PIPELINE_STOP); 420789Sahrens } 421789Sahrens 4227754SJeff.Bonwick@Sun.COM static void 423789Sahrens vdev_disk_io_done(zio_t *zio) 424789Sahrens { 4257754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd; 4261544Seschrock 4274451Seschrock /* 4284451Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 4294451Seschrock * the device has been removed. If this is the case, then we trigger an 4305329Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and 4315369Sgw25295 * make sure it's still accessible. 4324451Seschrock */ 433*10575SEric.Schrock@Sun.COM if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 4345329Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd; 4357754SJeff.Bonwick@Sun.COM int state = DKIO_NONE; 4365329Sgw25295 4377754SJeff.Bonwick@Sun.COM if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 4387754SJeff.Bonwick@Sun.COM FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 439*10575SEric.Schrock@Sun.COM /* 440*10575SEric.Schrock@Sun.COM * We post the resource as soon as possible, instead of 441*10575SEric.Schrock@Sun.COM * when the async removal actually happens, because the 442*10575SEric.Schrock@Sun.COM * DE is using this information to discard previous I/O 443*10575SEric.Schrock@Sun.COM * errors. 444*10575SEric.Schrock@Sun.COM */ 445*10575SEric.Schrock@Sun.COM zfs_post_remove(zio->io_spa, vd); 4464451Seschrock vd->vdev_remove_wanted = B_TRUE; 4474451Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 4484451Seschrock } 4494451Seschrock } 450789Sahrens } 451789Sahrens 452789Sahrens vdev_ops_t vdev_disk_ops = { 453789Sahrens vdev_disk_open, 454789Sahrens vdev_disk_close, 455789Sahrens vdev_default_asize, 456789Sahrens vdev_disk_io_start, 457789Sahrens vdev_disk_io_done, 458789Sahrens NULL, 459789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 460789Sahrens B_TRUE /* leaf vdev */ 461789Sahrens }; 4626423Sgw25295 4636423Sgw25295 /* 4647147Staylor * Given the root disk device devid or pathname, read the label from 4657147Staylor * the device, and construct a configuration nvlist. 4666423Sgw25295 */ 4677539SLin.Ling@Sun.COM int 4687539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 4696423Sgw25295 { 4706423Sgw25295 ldi_handle_t vd_lh; 4716423Sgw25295 vdev_label_t *label; 4726423Sgw25295 uint64_t s, size; 4736423Sgw25295 int l; 4747147Staylor ddi_devid_t tmpdevid; 4757687SLin.Ling@Sun.COM int error = -1; 4767147Staylor char *minor_name; 4776423Sgw25295 4786423Sgw25295 /* 4796423Sgw25295 * Read the device label and build the nvlist. 4806423Sgw25295 */ 4817687SLin.Ling@Sun.COM if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 4827147Staylor &minor_name) == 0) { 4837147Staylor error = ldi_open_by_devid(tmpdevid, minor_name, 4848241SJeff.Bonwick@Sun.COM FREAD, kcred, &vd_lh, zfs_li); 4857147Staylor ddi_devid_free(tmpdevid); 4867147Staylor ddi_devid_str_free(minor_name); 4877147Staylor } 4887147Staylor 4897687SLin.Ling@Sun.COM if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 4907687SLin.Ling@Sun.COM zfs_li))) 4917539SLin.Ling@Sun.COM return (error); 4926423Sgw25295 4936673Seschrock if (ldi_get_size(vd_lh, &s)) { 4946673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 4957539SLin.Ling@Sun.COM return (EIO); 4966673Seschrock } 4976423Sgw25295 4986423Sgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 4996423Sgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 5006423Sgw25295 5019616SEric.Taylor@Sun.COM *config = NULL; 5026423Sgw25295 for (l = 0; l < VDEV_LABELS; l++) { 5036423Sgw25295 uint64_t offset, state, txg = 0; 5046423Sgw25295 5056423Sgw25295 /* read vdev label */ 5066423Sgw25295 offset = vdev_label_offset(size, l, 0); 5076423Sgw25295 if (vdev_disk_physio(vd_lh, (caddr_t)label, 5088876SLin.Ling@Sun.COM VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 5096423Sgw25295 continue; 5106423Sgw25295 5116423Sgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 5127539SLin.Ling@Sun.COM sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 5137539SLin.Ling@Sun.COM *config = NULL; 5146423Sgw25295 continue; 5156423Sgw25295 } 5166423Sgw25295 5177539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 5186423Sgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) { 5197539SLin.Ling@Sun.COM nvlist_free(*config); 5207539SLin.Ling@Sun.COM *config = NULL; 5216423Sgw25295 continue; 5226423Sgw25295 } 5236423Sgw25295 5247539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 5256423Sgw25295 &txg) != 0 || txg == 0) { 5267539SLin.Ling@Sun.COM nvlist_free(*config); 5277539SLin.Ling@Sun.COM *config = NULL; 5286423Sgw25295 continue; 5296423Sgw25295 } 5306423Sgw25295 5316423Sgw25295 break; 5326423Sgw25295 } 5336423Sgw25295 5346423Sgw25295 kmem_free(label, sizeof (vdev_label_t)); 5356673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 5369616SEric.Taylor@Sun.COM if (*config == NULL) 5379616SEric.Taylor@Sun.COM error = EIDRM; 5386673Seschrock 5397539SLin.Ling@Sun.COM return (error); 5406423Sgw25295 } 541