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 /* 226423Sgw25295 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/spa.h> 306423Sgw25295 #include <sys/refcount.h> 31789Sahrens #include <sys/vdev_disk.h> 32789Sahrens #include <sys/vdev_impl.h> 33789Sahrens #include <sys/fs/zfs.h> 34789Sahrens #include <sys/zio.h> 351171Seschrock #include <sys/sunldi.h> 36789Sahrens 37789Sahrens /* 38789Sahrens * Virtual device vector for disks. 39789Sahrens */ 40789Sahrens 41789Sahrens extern ldi_ident_t zfs_li; 42789Sahrens 43789Sahrens typedef struct vdev_disk_buf { 44789Sahrens buf_t vdb_buf; 45789Sahrens zio_t *vdb_io; 46789Sahrens } vdev_disk_buf_t; 47789Sahrens 48789Sahrens static int 495329Sgw25295 vdev_disk_open_common(vdev_t *vd) 50789Sahrens { 51789Sahrens vdev_disk_t *dvd; 525329Sgw25295 dev_t dev; 53789Sahrens int error; 54789Sahrens 55789Sahrens /* 56789Sahrens * We must have a pathname, and it must be absolute. 57789Sahrens */ 58789Sahrens if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 59789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 60789Sahrens return (EINVAL); 61789Sahrens } 62789Sahrens 63789Sahrens dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 64789Sahrens 65789Sahrens /* 66789Sahrens * When opening a disk device, we want to preserve the user's original 67789Sahrens * intent. We always want to open the device by the path the user gave 68789Sahrens * us, even if it is one of multiple paths to the save device. But we 69789Sahrens * also want to be able to survive disks being removed/recabled. 70789Sahrens * Therefore the sequence of opening devices is: 71789Sahrens * 721171Seschrock * 1. Try opening the device by path. For legacy pools without the 731171Seschrock * 'whole_disk' property, attempt to fix the path by appending 's0'. 74789Sahrens * 75789Sahrens * 2. If the devid of the device matches the stored value, return 76789Sahrens * success. 77789Sahrens * 78789Sahrens * 3. Otherwise, the device may have moved. Try opening the device 79789Sahrens * by the devid instead. 80789Sahrens * 81789Sahrens */ 82789Sahrens if (vd->vdev_devid != NULL) { 83789Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 84789Sahrens &dvd->vd_minor) != 0) { 85789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 86789Sahrens return (EINVAL); 87789Sahrens } 88789Sahrens } 89789Sahrens 90789Sahrens error = EINVAL; /* presume failure */ 91789Sahrens 92789Sahrens if (vd->vdev_path != NULL) { 93789Sahrens ddi_devid_t devid; 94789Sahrens 951171Seschrock if (vd->vdev_wholedisk == -1ULL) { 961171Seschrock size_t len = strlen(vd->vdev_path) + 3; 971171Seschrock char *buf = kmem_alloc(len, KM_SLEEP); 981171Seschrock ldi_handle_t lh; 991171Seschrock 1001171Seschrock (void) snprintf(buf, len, "%ss0", vd->vdev_path); 101789Sahrens 1021171Seschrock if (ldi_open_by_name(buf, spa_mode, kcred, 1031171Seschrock &lh, zfs_li) == 0) { 1041171Seschrock spa_strfree(vd->vdev_path); 1051171Seschrock vd->vdev_path = buf; 1061171Seschrock vd->vdev_wholedisk = 1ULL; 1071171Seschrock (void) ldi_close(lh, spa_mode, kcred); 1081171Seschrock } else { 1091171Seschrock kmem_free(buf, len); 1101171Seschrock } 1111171Seschrock } 112789Sahrens 1131171Seschrock error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 1141171Seschrock &dvd->vd_lh, zfs_li); 115789Sahrens 116789Sahrens /* 117789Sahrens * Compare the devid to the stored value. 118789Sahrens */ 119789Sahrens if (error == 0 && vd->vdev_devid != NULL && 120789Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) { 121789Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 122789Sahrens error = EINVAL; 123789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 124789Sahrens dvd->vd_lh = NULL; 125789Sahrens } 126789Sahrens ddi_devid_free(devid); 127789Sahrens } 1281171Seschrock 1291171Seschrock /* 1301171Seschrock * If we succeeded in opening the device, but 'vdev_wholedisk' 1311171Seschrock * is not yet set, then this must be a slice. 1321171Seschrock */ 1331171Seschrock if (error == 0 && vd->vdev_wholedisk == -1ULL) 1341171Seschrock vd->vdev_wholedisk = 0; 135789Sahrens } 136789Sahrens 137789Sahrens /* 138789Sahrens * If we were unable to open by path, or the devid check fails, open by 139789Sahrens * devid instead. 140789Sahrens */ 141789Sahrens if (error != 0 && vd->vdev_devid != NULL) 142789Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 143789Sahrens spa_mode, kcred, &dvd->vd_lh, zfs_li); 144789Sahrens 1454451Seschrock /* 1464451Seschrock * If all else fails, then try opening by physical path (if available) 1474451Seschrock * or the logical path (if we failed due to the devid check). While not 1484451Seschrock * as reliable as the devid, this will give us something, and the higher 1494451Seschrock * level vdev validation will prevent us from opening the wrong device. 1504451Seschrock */ 1514451Seschrock if (error) { 1524451Seschrock if (vd->vdev_physpath != NULL && 1534451Seschrock (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV) 1544451Seschrock error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode, 1554451Seschrock kcred, &dvd->vd_lh, zfs_li); 1564451Seschrock 1574451Seschrock /* 1584451Seschrock * Note that we don't support the legacy auto-wholedisk support 1594451Seschrock * as above. This hasn't been used in a very long time and we 1604451Seschrock * don't need to propagate its oddities to this edge condition. 1614451Seschrock */ 1624451Seschrock if (error && vd->vdev_path != NULL) 1634451Seschrock error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 1644451Seschrock &dvd->vd_lh, zfs_li); 1654451Seschrock } 1664451Seschrock 1675329Sgw25295 if (error) 168789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 1695329Sgw25295 1705329Sgw25295 return (error); 1715329Sgw25295 } 1725329Sgw25295 1735329Sgw25295 static int 1745329Sgw25295 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 1755329Sgw25295 { 1765329Sgw25295 vdev_disk_t *dvd; 1775329Sgw25295 struct dk_minfo dkm; 1785329Sgw25295 int error; 1795329Sgw25295 dev_t dev; 1805329Sgw25295 int otyp; 1815329Sgw25295 1825329Sgw25295 error = vdev_disk_open_common(vd); 1835329Sgw25295 if (error) 184789Sahrens return (error); 185789Sahrens 1865329Sgw25295 dvd = vd->vdev_tsd; 187789Sahrens /* 1884451Seschrock * Once a device is opened, verify that the physical device path (if 1894451Seschrock * available) is up to date. 1904451Seschrock */ 1914451Seschrock if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 1924451Seschrock ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 1935329Sgw25295 char *physpath, *minorname; 1945329Sgw25295 1954451Seschrock physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1964451Seschrock minorname = NULL; 1974451Seschrock if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 1984451Seschrock ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 1994451Seschrock (vd->vdev_physpath == NULL || 2004451Seschrock strcmp(vd->vdev_physpath, physpath) != 0)) { 2014451Seschrock if (vd->vdev_physpath) 2024451Seschrock spa_strfree(vd->vdev_physpath); 2034451Seschrock (void) strlcat(physpath, ":", MAXPATHLEN); 2044451Seschrock (void) strlcat(physpath, minorname, MAXPATHLEN); 2054451Seschrock vd->vdev_physpath = spa_strdup(physpath); 2064451Seschrock } 2074451Seschrock if (minorname) 2084451Seschrock kmem_free(minorname, strlen(minorname) + 1); 2094451Seschrock kmem_free(physpath, MAXPATHLEN); 2104451Seschrock } 2114451Seschrock 2124451Seschrock /* 213789Sahrens * Determine the actual size of the device. 214789Sahrens */ 215789Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) { 216789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 217789Sahrens return (EINVAL); 218789Sahrens } 219789Sahrens 2201732Sbonwick /* 2211732Sbonwick * If we own the whole disk, try to enable disk write caching. 2221732Sbonwick * We ignore errors because it's OK if we can't do it. 2231732Sbonwick */ 2241489Swebaker if (vd->vdev_wholedisk == 1) { 2251732Sbonwick int wce = 1; 2261732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 2271732Sbonwick FKIOCTL, kcred, NULL); 2281732Sbonwick } 2291489Swebaker 2301732Sbonwick /* 2311732Sbonwick * Determine the device's minimum transfer size. 2321732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 2331732Sbonwick */ 2341732Sbonwick if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 2351732Sbonwick FKIOCTL, kcred, NULL) != 0) 2361732Sbonwick dkm.dki_lbsize = DEV_BSIZE; 2371489Swebaker 2381732Sbonwick *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 2391489Swebaker 2401773Seschrock /* 2411773Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will 2421773Seschrock * try again. 2431773Seschrock */ 2441773Seschrock vd->vdev_nowritecache = B_FALSE; 2451773Seschrock 246789Sahrens return (0); 247789Sahrens } 248789Sahrens 249789Sahrens static void 250789Sahrens vdev_disk_close(vdev_t *vd) 251789Sahrens { 252789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 253789Sahrens 254789Sahrens if (dvd == NULL) 255789Sahrens return; 256789Sahrens 257789Sahrens if (dvd->vd_minor != NULL) 258789Sahrens ddi_devid_str_free(dvd->vd_minor); 259789Sahrens 260789Sahrens if (dvd->vd_devid != NULL) 261789Sahrens ddi_devid_free(dvd->vd_devid); 262789Sahrens 263789Sahrens if (dvd->vd_lh != NULL) 264789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 265789Sahrens 266789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 267789Sahrens vd->vdev_tsd = NULL; 268789Sahrens } 269789Sahrens 2706423Sgw25295 int 2716423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 2726423Sgw25295 uint64_t offset, int flags) 2736423Sgw25295 { 2746423Sgw25295 buf_t *bp; 2756423Sgw25295 int error = 0; 2766423Sgw25295 2776423Sgw25295 if (vd_lh == NULL) 2786423Sgw25295 return (EINVAL); 2796423Sgw25295 2806423Sgw25295 ASSERT(flags & B_READ || flags & B_WRITE); 2816423Sgw25295 2826423Sgw25295 bp = getrbuf(KM_SLEEP); 2836423Sgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 2846423Sgw25295 bp->b_bcount = size; 2856423Sgw25295 bp->b_un.b_addr = (void *)data; 2866423Sgw25295 bp->b_lblkno = lbtodb(offset); 2876423Sgw25295 bp->b_bufsize = size; 2886423Sgw25295 2896423Sgw25295 error = ldi_strategy(vd_lh, bp); 2906423Sgw25295 ASSERT(error == 0); 2916423Sgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 2926423Sgw25295 error = EIO; 2936423Sgw25295 freerbuf(bp); 2946423Sgw25295 2956423Sgw25295 return (error); 2966423Sgw25295 } 2976423Sgw25295 2985329Sgw25295 static int 2995329Sgw25295 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset, 3005329Sgw25295 int flags) 3015329Sgw25295 { 3025329Sgw25295 int error = 0; 3036523Sek110237 vdev_disk_t *dvd = vd ? vd->vdev_tsd : NULL; 3045329Sgw25295 3055329Sgw25295 if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL) 3065329Sgw25295 return (EINVAL); 3075329Sgw25295 3086423Sgw25295 error = vdev_disk_physio(dvd->vd_lh, data, size, offset, flags); 3095329Sgw25295 3105329Sgw25295 if (zio_injection_enabled && error == 0) 3115329Sgw25295 error = zio_handle_device_injection(vd, EIO); 3125329Sgw25295 3135329Sgw25295 return (error); 3145329Sgw25295 } 3155329Sgw25295 3165369Sgw25295 /* 3175369Sgw25295 * Determine if the underlying device is accessible by reading and writing 3185369Sgw25295 * to a known location. We must be able to do this during syncing context 3195369Sgw25295 * and thus we cannot set the vdev state directly. 3205369Sgw25295 */ 3215329Sgw25295 static int 3225329Sgw25295 vdev_disk_probe(vdev_t *vd) 3235329Sgw25295 { 3245329Sgw25295 uint64_t offset; 3255329Sgw25295 vdev_t *nvd; 3265329Sgw25295 int l, error = 0, retries = 0; 3275329Sgw25295 char *vl_pad; 3285329Sgw25295 3295329Sgw25295 if (vd == NULL) 3305329Sgw25295 return (EINVAL); 3315329Sgw25295 3325329Sgw25295 /* Hijack the current vdev */ 3335329Sgw25295 nvd = vd; 3345329Sgw25295 3355329Sgw25295 /* 3365329Sgw25295 * Pick a random label to rewrite. 3375329Sgw25295 */ 3385329Sgw25295 l = spa_get_random(VDEV_LABELS); 3395329Sgw25295 ASSERT(l < VDEV_LABELS); 3405329Sgw25295 3415329Sgw25295 offset = vdev_label_offset(vd->vdev_psize, l, 3425329Sgw25295 offsetof(vdev_label_t, vl_pad)); 3435329Sgw25295 3445329Sgw25295 vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP); 3455329Sgw25295 3465329Sgw25295 /* 3475329Sgw25295 * Try to read and write to a special location on the 3485329Sgw25295 * label. We use the existing vdev initially and only 3495329Sgw25295 * try to create and reopen it if we encounter a failure. 3505329Sgw25295 */ 3515329Sgw25295 while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE, 3525329Sgw25295 offset, B_READ)) != 0 && retries == 0) { 3535329Sgw25295 3545329Sgw25295 nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 3555329Sgw25295 if (vd->vdev_path) 3565329Sgw25295 nvd->vdev_path = spa_strdup(vd->vdev_path); 3575329Sgw25295 if (vd->vdev_physpath) 3585329Sgw25295 nvd->vdev_physpath = spa_strdup(vd->vdev_physpath); 3595329Sgw25295 if (vd->vdev_devid) 3605329Sgw25295 nvd->vdev_devid = spa_strdup(vd->vdev_devid); 3615329Sgw25295 nvd->vdev_wholedisk = vd->vdev_wholedisk; 3625329Sgw25295 nvd->vdev_guid = vd->vdev_guid; 3635329Sgw25295 retries++; 3645329Sgw25295 3655329Sgw25295 error = vdev_disk_open_common(nvd); 3665369Sgw25295 if (error) 3675329Sgw25295 break; 3685329Sgw25295 } 3695329Sgw25295 3705329Sgw25295 if (!error) { 3715329Sgw25295 error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE, 3725329Sgw25295 offset, B_WRITE); 3735329Sgw25295 } 3745329Sgw25295 3755329Sgw25295 /* Clean up if we allocated a new vdev */ 3765329Sgw25295 if (retries) { 3775329Sgw25295 vdev_disk_close(nvd); 3785329Sgw25295 if (nvd->vdev_path) 3795329Sgw25295 spa_strfree(nvd->vdev_path); 3805329Sgw25295 if (nvd->vdev_physpath) 3815329Sgw25295 spa_strfree(nvd->vdev_physpath); 3825329Sgw25295 if (nvd->vdev_devid) 3835329Sgw25295 spa_strfree(nvd->vdev_devid); 3845329Sgw25295 kmem_free(nvd, sizeof (vdev_t)); 3855329Sgw25295 } 3865329Sgw25295 kmem_free(vl_pad, VDEV_SKIP_SIZE); 3875329Sgw25295 3885329Sgw25295 /* Reset the failing flag */ 3895329Sgw25295 if (!error) 3905329Sgw25295 vd->vdev_is_failing = B_FALSE; 3915329Sgw25295 3925329Sgw25295 return (error); 3935329Sgw25295 } 3945329Sgw25295 395789Sahrens static void 396789Sahrens vdev_disk_io_intr(buf_t *bp) 397789Sahrens { 398789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 399789Sahrens zio_t *zio = vdb->vdb_io; 400789Sahrens 401789Sahrens if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0) 402789Sahrens zio->io_error = EIO; 403789Sahrens 404789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 405789Sahrens 4065530Sbonwick zio_interrupt(zio); 407789Sahrens } 408789Sahrens 409789Sahrens static void 410789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 411789Sahrens { 412789Sahrens zio_t *zio = zio_arg; 413789Sahrens 414789Sahrens zio->io_error = error; 415789Sahrens 4165530Sbonwick zio_interrupt(zio); 417789Sahrens } 418789Sahrens 4195530Sbonwick static int 420789Sahrens vdev_disk_io_start(zio_t *zio) 421789Sahrens { 422789Sahrens vdev_t *vd = zio->io_vd; 423789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 424789Sahrens vdev_disk_buf_t *vdb; 425789Sahrens buf_t *bp; 426789Sahrens int flags, error; 427789Sahrens 428789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 429789Sahrens zio_vdev_io_bypass(zio); 430789Sahrens 431789Sahrens /* XXPOLICY */ 4325329Sgw25295 if (!vdev_readable(vd)) { 433789Sahrens zio->io_error = ENXIO; 4345530Sbonwick return (ZIO_PIPELINE_CONTINUE); 435789Sahrens } 436789Sahrens 437789Sahrens switch (zio->io_cmd) { 438789Sahrens 439789Sahrens case DKIOCFLUSHWRITECACHE: 440789Sahrens 4412885Sahrens if (zfs_nocacheflush) 4422885Sahrens break; 4432885Sahrens 4441773Seschrock if (vd->vdev_nowritecache) { 4451773Seschrock zio->io_error = ENOTSUP; 4461773Seschrock break; 4471773Seschrock } 4481773Seschrock 449789Sahrens zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done; 4505065Sgz161490 zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE; 451789Sahrens zio->io_dk_callback.dkc_cookie = zio; 452789Sahrens 453789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 454789Sahrens (uintptr_t)&zio->io_dk_callback, 455789Sahrens FKIOCTL, kcred, NULL); 456789Sahrens 457789Sahrens if (error == 0) { 458789Sahrens /* 459789Sahrens * The ioctl will be done asychronously, 460789Sahrens * and will call vdev_disk_ioctl_done() 461789Sahrens * upon completion. 462789Sahrens */ 4635530Sbonwick return (ZIO_PIPELINE_STOP); 4645530Sbonwick } 4655530Sbonwick 4665530Sbonwick if (error == ENOTSUP || error == ENOTTY) { 4671773Seschrock /* 4684455Smishra * If we get ENOTSUP or ENOTTY, we know that 4694455Smishra * no future attempts will ever succeed. 4704455Smishra * In this case we set a persistent bit so 4714455Smishra * that we don't bother with the ioctl in the 4724455Smishra * future. 4731773Seschrock */ 4741773Seschrock vd->vdev_nowritecache = B_TRUE; 475789Sahrens } 476789Sahrens zio->io_error = error; 4771773Seschrock 478789Sahrens break; 479789Sahrens 480789Sahrens default: 481789Sahrens zio->io_error = ENOTSUP; 482789Sahrens } 483789Sahrens 4845530Sbonwick return (ZIO_PIPELINE_CONTINUE); 485789Sahrens } 486789Sahrens 487789Sahrens if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0) 4885530Sbonwick return (ZIO_PIPELINE_STOP); 489789Sahrens 490789Sahrens if ((zio = vdev_queue_io(zio)) == NULL) 4915530Sbonwick return (ZIO_PIPELINE_STOP); 4925530Sbonwick 4935530Sbonwick if (zio->io_type == ZIO_TYPE_WRITE) 4945530Sbonwick error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO; 4955530Sbonwick else 4965530Sbonwick error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO; 4975530Sbonwick error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error; 4985530Sbonwick 4995530Sbonwick if (error) { 5005530Sbonwick zio->io_error = error; 5015530Sbonwick zio_interrupt(zio); 5025530Sbonwick return (ZIO_PIPELINE_STOP); 5035530Sbonwick } 504789Sahrens 505789Sahrens flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 506789Sahrens flags |= B_BUSY | B_NOCACHE; 507789Sahrens if (zio->io_flags & ZIO_FLAG_FAILFAST) 508789Sahrens flags |= B_FAILFAST; 509789Sahrens 510789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 511789Sahrens 512789Sahrens vdb->vdb_io = zio; 513789Sahrens bp = &vdb->vdb_buf; 514789Sahrens 515789Sahrens bioinit(bp); 516789Sahrens bp->b_flags = flags; 517789Sahrens bp->b_bcount = zio->io_size; 518789Sahrens bp->b_un.b_addr = zio->io_data; 519789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 520789Sahrens bp->b_bufsize = zio->io_size; 521789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 522789Sahrens 523789Sahrens error = ldi_strategy(dvd->vd_lh, bp); 524789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 525789Sahrens ASSERT(error == 0); 5265530Sbonwick 5275530Sbonwick return (ZIO_PIPELINE_STOP); 528789Sahrens } 529789Sahrens 5305530Sbonwick static int 531789Sahrens vdev_disk_io_done(zio_t *zio) 532789Sahrens { 533789Sahrens vdev_queue_io_done(zio); 534789Sahrens 535789Sahrens if (zio->io_type == ZIO_TYPE_WRITE) 536789Sahrens vdev_cache_write(zio); 537789Sahrens 5381544Seschrock if (zio_injection_enabled && zio->io_error == 0) 5391544Seschrock zio->io_error = zio_handle_device_injection(zio->io_vd, EIO); 5401544Seschrock 5414451Seschrock /* 5424451Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 5434451Seschrock * the device has been removed. If this is the case, then we trigger an 5445329Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and 5455369Sgw25295 * make sure it's still accessible. 5464451Seschrock */ 5474451Seschrock if (zio->io_error == EIO) { 5485329Sgw25295 vdev_t *vd = zio->io_vd; 5495329Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd; 5505329Sgw25295 int state; 5515329Sgw25295 5524451Seschrock state = DKIO_NONE; 5535329Sgw25295 if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 5544451Seschrock FKIOCTL, kcred, NULL) == 0 && 5554451Seschrock state != DKIO_INSERTED) { 5564451Seschrock vd->vdev_remove_wanted = B_TRUE; 5574451Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 5585329Sgw25295 } else if (vdev_probe(vd) != 0) { 5595329Sgw25295 ASSERT(vd->vdev_ops->vdev_op_leaf); 5605329Sgw25295 vd->vdev_is_failing = B_TRUE; 5614451Seschrock } 5624451Seschrock } 5634451Seschrock 564*6615Sgw25295 if (zio_injection_enabled && zio->io_error == 0) 565*6615Sgw25295 zio->io_error = zio_handle_label_injection(zio, EIO); 566*6615Sgw25295 5675530Sbonwick return (ZIO_PIPELINE_CONTINUE); 568789Sahrens } 569789Sahrens 570789Sahrens vdev_ops_t vdev_disk_ops = { 571789Sahrens vdev_disk_open, 572789Sahrens vdev_disk_close, 5735329Sgw25295 vdev_disk_probe, 574789Sahrens vdev_default_asize, 575789Sahrens vdev_disk_io_start, 576789Sahrens vdev_disk_io_done, 577789Sahrens NULL, 578789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 579789Sahrens B_TRUE /* leaf vdev */ 580789Sahrens }; 5816423Sgw25295 5826423Sgw25295 /* 5836423Sgw25295 * Given the root disk device pathname, read the label from the device, 5846423Sgw25295 * and construct a configuration nvlist. 5856423Sgw25295 */ 5866423Sgw25295 nvlist_t * 5876423Sgw25295 vdev_disk_read_rootlabel(char *devpath) 5886423Sgw25295 { 5896423Sgw25295 nvlist_t *config = NULL; 5906423Sgw25295 ldi_handle_t vd_lh; 5916423Sgw25295 vdev_label_t *label; 5926423Sgw25295 uint64_t s, size; 5936423Sgw25295 int l; 5946423Sgw25295 5956423Sgw25295 /* 5966423Sgw25295 * Read the device label and build the nvlist. 5976423Sgw25295 */ 5986423Sgw25295 if (ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, zfs_li)) 5996423Sgw25295 return (NULL); 6006423Sgw25295 6016423Sgw25295 if (ldi_get_size(vd_lh, &s)) 6026423Sgw25295 return (NULL); 6036423Sgw25295 6046423Sgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 6056423Sgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 6066423Sgw25295 6076423Sgw25295 for (l = 0; l < VDEV_LABELS; l++) { 6086423Sgw25295 uint64_t offset, state, txg = 0; 6096423Sgw25295 6106423Sgw25295 /* read vdev label */ 6116423Sgw25295 offset = vdev_label_offset(size, l, 0); 6126423Sgw25295 if (vdev_disk_physio(vd_lh, (caddr_t)label, 6136423Sgw25295 VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE + 6146423Sgw25295 VDEV_PHYS_SIZE, offset, B_READ) != 0) 6156423Sgw25295 continue; 6166423Sgw25295 6176423Sgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 6186423Sgw25295 sizeof (label->vl_vdev_phys.vp_nvlist), &config, 0) != 0) { 6196423Sgw25295 config = NULL; 6206423Sgw25295 continue; 6216423Sgw25295 } 6226423Sgw25295 6236423Sgw25295 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 6246423Sgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) { 6256423Sgw25295 nvlist_free(config); 6266423Sgw25295 config = NULL; 6276423Sgw25295 continue; 6286423Sgw25295 } 6296423Sgw25295 6306423Sgw25295 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, 6316423Sgw25295 &txg) != 0 || txg == 0) { 6326423Sgw25295 nvlist_free(config); 6336423Sgw25295 config = NULL; 6346423Sgw25295 continue; 6356423Sgw25295 } 6366423Sgw25295 6376423Sgw25295 break; 6386423Sgw25295 } 6396423Sgw25295 6406423Sgw25295 kmem_free(label, sizeof (vdev_label_t)); 6416423Sgw25295 return (config); 6426423Sgw25295 } 643