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 #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 485329Sgw25295 vdev_disk_open_common(vdev_t *vd) 49789Sahrens { 50789Sahrens vdev_disk_t *dvd; 515329Sgw25295 dev_t dev; 52789Sahrens int error; 53789Sahrens 54789Sahrens /* 55789Sahrens * We must have a pathname, and it must be absolute. 56789Sahrens */ 57789Sahrens if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 58789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 59789Sahrens return (EINVAL); 60789Sahrens } 61789Sahrens 62789Sahrens dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 63789Sahrens 64789Sahrens /* 65789Sahrens * When opening a disk device, we want to preserve the user's original 66789Sahrens * intent. We always want to open the device by the path the user gave 67789Sahrens * us, even if it is one of multiple paths to the save device. But we 68789Sahrens * also want to be able to survive disks being removed/recabled. 69789Sahrens * Therefore the sequence of opening devices is: 70789Sahrens * 711171Seschrock * 1. Try opening the device by path. For legacy pools without the 721171Seschrock * 'whole_disk' property, attempt to fix the path by appending 's0'. 73789Sahrens * 74789Sahrens * 2. If the devid of the device matches the stored value, return 75789Sahrens * success. 76789Sahrens * 77789Sahrens * 3. Otherwise, the device may have moved. Try opening the device 78789Sahrens * by the devid instead. 79789Sahrens * 806673Seschrock * If the vdev is part of the root pool, we avoid opening it by path. 816673Seschrock * We do this because there is no /dev path available early in boot, 826673Seschrock * and if we try to open the device by path at a later point, we can 836673Seschrock * deadlock when devfsadm attempts to open the underlying backing store 846673Seschrock * file. 85789Sahrens */ 86789Sahrens if (vd->vdev_devid != NULL) { 87789Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 88789Sahrens &dvd->vd_minor) != 0) { 89789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 90789Sahrens return (EINVAL); 91789Sahrens } 92789Sahrens } 93789Sahrens 94789Sahrens error = EINVAL; /* presume failure */ 95789Sahrens 966673Seschrock if (vd->vdev_path != NULL && !spa_is_root(vd->vdev_spa)) { 97789Sahrens ddi_devid_t devid; 98789Sahrens 991171Seschrock if (vd->vdev_wholedisk == -1ULL) { 1001171Seschrock size_t len = strlen(vd->vdev_path) + 3; 1011171Seschrock char *buf = kmem_alloc(len, KM_SLEEP); 1021171Seschrock ldi_handle_t lh; 1031171Seschrock 1041171Seschrock (void) snprintf(buf, len, "%ss0", vd->vdev_path); 105789Sahrens 1061171Seschrock if (ldi_open_by_name(buf, spa_mode, kcred, 1071171Seschrock &lh, zfs_li) == 0) { 1081171Seschrock spa_strfree(vd->vdev_path); 1091171Seschrock vd->vdev_path = buf; 1101171Seschrock vd->vdev_wholedisk = 1ULL; 1111171Seschrock (void) ldi_close(lh, spa_mode, kcred); 1121171Seschrock } else { 1131171Seschrock kmem_free(buf, len); 1141171Seschrock } 1151171Seschrock } 116789Sahrens 1171171Seschrock error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 1181171Seschrock &dvd->vd_lh, zfs_li); 119789Sahrens 120789Sahrens /* 121789Sahrens * Compare the devid to the stored value. 122789Sahrens */ 123789Sahrens if (error == 0 && vd->vdev_devid != NULL && 124789Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) { 125789Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 126789Sahrens error = EINVAL; 127789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 128789Sahrens dvd->vd_lh = NULL; 129789Sahrens } 130789Sahrens ddi_devid_free(devid); 131789Sahrens } 1321171Seschrock 1331171Seschrock /* 1341171Seschrock * If we succeeded in opening the device, but 'vdev_wholedisk' 1351171Seschrock * is not yet set, then this must be a slice. 1361171Seschrock */ 1371171Seschrock if (error == 0 && vd->vdev_wholedisk == -1ULL) 1381171Seschrock vd->vdev_wholedisk = 0; 139789Sahrens } 140789Sahrens 141789Sahrens /* 142789Sahrens * If we were unable to open by path, or the devid check fails, open by 143789Sahrens * devid instead. 144789Sahrens */ 145789Sahrens if (error != 0 && vd->vdev_devid != NULL) 146789Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 147789Sahrens spa_mode, kcred, &dvd->vd_lh, zfs_li); 148789Sahrens 1494451Seschrock /* 1504451Seschrock * If all else fails, then try opening by physical path (if available) 1514451Seschrock * or the logical path (if we failed due to the devid check). While not 1524451Seschrock * as reliable as the devid, this will give us something, and the higher 1534451Seschrock * level vdev validation will prevent us from opening the wrong device. 1544451Seschrock */ 1554451Seschrock if (error) { 1564451Seschrock if (vd->vdev_physpath != NULL && 1574451Seschrock (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV) 1584451Seschrock error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode, 1594451Seschrock kcred, &dvd->vd_lh, zfs_li); 1604451Seschrock 1614451Seschrock /* 1624451Seschrock * Note that we don't support the legacy auto-wholedisk support 1634451Seschrock * as above. This hasn't been used in a very long time and we 1644451Seschrock * don't need to propagate its oddities to this edge condition. 1654451Seschrock */ 1666673Seschrock if (error && vd->vdev_path != NULL && 1676673Seschrock !spa_is_root(vd->vdev_spa)) 1684451Seschrock error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 1694451Seschrock &dvd->vd_lh, zfs_li); 1704451Seschrock } 1714451Seschrock 1725329Sgw25295 if (error) 173789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 1745329Sgw25295 1755329Sgw25295 return (error); 1765329Sgw25295 } 1775329Sgw25295 1785329Sgw25295 static int 1795329Sgw25295 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 1805329Sgw25295 { 1815329Sgw25295 vdev_disk_t *dvd; 1825329Sgw25295 struct dk_minfo dkm; 1835329Sgw25295 int error; 1845329Sgw25295 dev_t dev; 1855329Sgw25295 int otyp; 1865329Sgw25295 1875329Sgw25295 error = vdev_disk_open_common(vd); 1885329Sgw25295 if (error) 189789Sahrens return (error); 190789Sahrens 1915329Sgw25295 dvd = vd->vdev_tsd; 192789Sahrens /* 1934451Seschrock * Once a device is opened, verify that the physical device path (if 1944451Seschrock * available) is up to date. 1954451Seschrock */ 1964451Seschrock if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 1974451Seschrock ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 1985329Sgw25295 char *physpath, *minorname; 1995329Sgw25295 2004451Seschrock physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2014451Seschrock minorname = NULL; 2024451Seschrock if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 2034451Seschrock ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 2044451Seschrock (vd->vdev_physpath == NULL || 2054451Seschrock strcmp(vd->vdev_physpath, physpath) != 0)) { 2064451Seschrock if (vd->vdev_physpath) 2074451Seschrock spa_strfree(vd->vdev_physpath); 2084451Seschrock (void) strlcat(physpath, ":", MAXPATHLEN); 2094451Seschrock (void) strlcat(physpath, minorname, MAXPATHLEN); 2104451Seschrock vd->vdev_physpath = spa_strdup(physpath); 2114451Seschrock } 2124451Seschrock if (minorname) 2134451Seschrock kmem_free(minorname, strlen(minorname) + 1); 2144451Seschrock kmem_free(physpath, MAXPATHLEN); 2154451Seschrock } 2164451Seschrock 2174451Seschrock /* 218789Sahrens * Determine the actual size of the device. 219789Sahrens */ 220789Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) { 221789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 222789Sahrens return (EINVAL); 223789Sahrens } 224789Sahrens 2251732Sbonwick /* 2261732Sbonwick * If we own the whole disk, try to enable disk write caching. 2271732Sbonwick * We ignore errors because it's OK if we can't do it. 2281732Sbonwick */ 2291489Swebaker if (vd->vdev_wholedisk == 1) { 2301732Sbonwick int wce = 1; 2311732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 2321732Sbonwick FKIOCTL, kcred, NULL); 2331732Sbonwick } 2341489Swebaker 2351732Sbonwick /* 2361732Sbonwick * Determine the device's minimum transfer size. 2371732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 2381732Sbonwick */ 2391732Sbonwick if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 2401732Sbonwick FKIOCTL, kcred, NULL) != 0) 2411732Sbonwick dkm.dki_lbsize = DEV_BSIZE; 2421489Swebaker 2431732Sbonwick *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 2441489Swebaker 2451773Seschrock /* 2461773Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will 2471773Seschrock * try again. 2481773Seschrock */ 2491773Seschrock vd->vdev_nowritecache = B_FALSE; 2501773Seschrock 251789Sahrens return (0); 252789Sahrens } 253789Sahrens 254789Sahrens static void 255789Sahrens vdev_disk_close(vdev_t *vd) 256789Sahrens { 257789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 258789Sahrens 259789Sahrens if (dvd == NULL) 260789Sahrens return; 261789Sahrens 262789Sahrens if (dvd->vd_minor != NULL) 263789Sahrens ddi_devid_str_free(dvd->vd_minor); 264789Sahrens 265789Sahrens if (dvd->vd_devid != NULL) 266789Sahrens ddi_devid_free(dvd->vd_devid); 267789Sahrens 268789Sahrens if (dvd->vd_lh != NULL) 269789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 270789Sahrens 271789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 272789Sahrens vd->vdev_tsd = NULL; 273789Sahrens } 274789Sahrens 2756423Sgw25295 int 2766423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 2776423Sgw25295 uint64_t offset, int flags) 2786423Sgw25295 { 2796423Sgw25295 buf_t *bp; 2806423Sgw25295 int error = 0; 2816423Sgw25295 2826423Sgw25295 if (vd_lh == NULL) 2836423Sgw25295 return (EINVAL); 2846423Sgw25295 2856423Sgw25295 ASSERT(flags & B_READ || flags & B_WRITE); 2866423Sgw25295 2876423Sgw25295 bp = getrbuf(KM_SLEEP); 2886423Sgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 2896423Sgw25295 bp->b_bcount = size; 2906423Sgw25295 bp->b_un.b_addr = (void *)data; 2916423Sgw25295 bp->b_lblkno = lbtodb(offset); 2926423Sgw25295 bp->b_bufsize = size; 2936423Sgw25295 2946423Sgw25295 error = ldi_strategy(vd_lh, bp); 2956423Sgw25295 ASSERT(error == 0); 2966423Sgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 2976423Sgw25295 error = EIO; 2986423Sgw25295 freerbuf(bp); 2996423Sgw25295 3006423Sgw25295 return (error); 3016423Sgw25295 } 3026423Sgw25295 3035329Sgw25295 static int 3045329Sgw25295 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset, 3055329Sgw25295 int flags) 3065329Sgw25295 { 3075329Sgw25295 int error = 0; 3086523Sek110237 vdev_disk_t *dvd = vd ? vd->vdev_tsd : NULL; 3095329Sgw25295 3105329Sgw25295 if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL) 3115329Sgw25295 return (EINVAL); 3125329Sgw25295 3136423Sgw25295 error = vdev_disk_physio(dvd->vd_lh, data, size, offset, flags); 3145329Sgw25295 3155329Sgw25295 if (zio_injection_enabled && error == 0) 3165329Sgw25295 error = zio_handle_device_injection(vd, EIO); 3175329Sgw25295 3185329Sgw25295 return (error); 3195329Sgw25295 } 3205329Sgw25295 3215369Sgw25295 /* 3225369Sgw25295 * Determine if the underlying device is accessible by reading and writing 3235369Sgw25295 * to a known location. We must be able to do this during syncing context 3245369Sgw25295 * and thus we cannot set the vdev state directly. 3255369Sgw25295 */ 3265329Sgw25295 static int 3275329Sgw25295 vdev_disk_probe(vdev_t *vd) 3285329Sgw25295 { 3295329Sgw25295 uint64_t offset; 3305329Sgw25295 vdev_t *nvd; 3315329Sgw25295 int l, error = 0, retries = 0; 3325329Sgw25295 char *vl_pad; 3335329Sgw25295 3345329Sgw25295 if (vd == NULL) 3355329Sgw25295 return (EINVAL); 3365329Sgw25295 3375329Sgw25295 /* Hijack the current vdev */ 3385329Sgw25295 nvd = vd; 3395329Sgw25295 3405329Sgw25295 /* 3415329Sgw25295 * Pick a random label to rewrite. 3425329Sgw25295 */ 3435329Sgw25295 l = spa_get_random(VDEV_LABELS); 3445329Sgw25295 ASSERT(l < VDEV_LABELS); 3455329Sgw25295 3465329Sgw25295 offset = vdev_label_offset(vd->vdev_psize, l, 3475329Sgw25295 offsetof(vdev_label_t, vl_pad)); 3485329Sgw25295 3495329Sgw25295 vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP); 3505329Sgw25295 3515329Sgw25295 /* 3525329Sgw25295 * Try to read and write to a special location on the 3535329Sgw25295 * label. We use the existing vdev initially and only 3545329Sgw25295 * try to create and reopen it if we encounter a failure. 3555329Sgw25295 */ 3565329Sgw25295 while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE, 3575329Sgw25295 offset, B_READ)) != 0 && retries == 0) { 3585329Sgw25295 3595329Sgw25295 nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 3605329Sgw25295 if (vd->vdev_path) 3615329Sgw25295 nvd->vdev_path = spa_strdup(vd->vdev_path); 3625329Sgw25295 if (vd->vdev_physpath) 3635329Sgw25295 nvd->vdev_physpath = spa_strdup(vd->vdev_physpath); 3645329Sgw25295 if (vd->vdev_devid) 3655329Sgw25295 nvd->vdev_devid = spa_strdup(vd->vdev_devid); 3665329Sgw25295 nvd->vdev_wholedisk = vd->vdev_wholedisk; 3675329Sgw25295 nvd->vdev_guid = vd->vdev_guid; 3686673Seschrock nvd->vdev_spa = vd->vdev_spa; 3695329Sgw25295 retries++; 3705329Sgw25295 3715329Sgw25295 error = vdev_disk_open_common(nvd); 3725369Sgw25295 if (error) 3735329Sgw25295 break; 3745329Sgw25295 } 3755329Sgw25295 3765329Sgw25295 if (!error) { 3775329Sgw25295 error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE, 3785329Sgw25295 offset, B_WRITE); 3795329Sgw25295 } 3805329Sgw25295 3815329Sgw25295 /* Clean up if we allocated a new vdev */ 3825329Sgw25295 if (retries) { 3835329Sgw25295 vdev_disk_close(nvd); 3845329Sgw25295 if (nvd->vdev_path) 3855329Sgw25295 spa_strfree(nvd->vdev_path); 3865329Sgw25295 if (nvd->vdev_physpath) 3875329Sgw25295 spa_strfree(nvd->vdev_physpath); 3885329Sgw25295 if (nvd->vdev_devid) 3895329Sgw25295 spa_strfree(nvd->vdev_devid); 3905329Sgw25295 kmem_free(nvd, sizeof (vdev_t)); 3915329Sgw25295 } 3925329Sgw25295 kmem_free(vl_pad, VDEV_SKIP_SIZE); 3935329Sgw25295 3945329Sgw25295 /* Reset the failing flag */ 3955329Sgw25295 if (!error) 3965329Sgw25295 vd->vdev_is_failing = B_FALSE; 3975329Sgw25295 3985329Sgw25295 return (error); 3995329Sgw25295 } 4005329Sgw25295 401789Sahrens static void 402789Sahrens vdev_disk_io_intr(buf_t *bp) 403789Sahrens { 404789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 405789Sahrens zio_t *zio = vdb->vdb_io; 406789Sahrens 4076976Seschrock /* 4086976Seschrock * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 4096976Seschrock * Rather than teach the rest of the stack about other error 4106976Seschrock * possibilities (EFAULT, etc), we normalize the error value here. 4116976Seschrock */ 4126976Seschrock zio->io_error = (geterror(bp) != 0 ? EIO : 0); 4136976Seschrock 4146976Seschrock if (zio->io_error == 0 && bp->b_resid != 0) 415789Sahrens zio->io_error = EIO; 416789Sahrens 417789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 418789Sahrens 4195530Sbonwick zio_interrupt(zio); 420789Sahrens } 421789Sahrens 422789Sahrens static void 423789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 424789Sahrens { 425789Sahrens zio_t *zio = zio_arg; 426789Sahrens 427789Sahrens zio->io_error = error; 428789Sahrens 4295530Sbonwick zio_interrupt(zio); 430789Sahrens } 431789Sahrens 4325530Sbonwick static int 433789Sahrens vdev_disk_io_start(zio_t *zio) 434789Sahrens { 435789Sahrens vdev_t *vd = zio->io_vd; 436789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 437789Sahrens vdev_disk_buf_t *vdb; 438789Sahrens buf_t *bp; 439789Sahrens int flags, error; 440789Sahrens 441789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 442789Sahrens zio_vdev_io_bypass(zio); 443789Sahrens 444789Sahrens /* XXPOLICY */ 4455329Sgw25295 if (!vdev_readable(vd)) { 446789Sahrens zio->io_error = ENXIO; 4475530Sbonwick return (ZIO_PIPELINE_CONTINUE); 448789Sahrens } 449789Sahrens 450789Sahrens switch (zio->io_cmd) { 451789Sahrens 452789Sahrens case DKIOCFLUSHWRITECACHE: 453789Sahrens 4542885Sahrens if (zfs_nocacheflush) 4552885Sahrens break; 4562885Sahrens 4571773Seschrock if (vd->vdev_nowritecache) { 4581773Seschrock zio->io_error = ENOTSUP; 4591773Seschrock break; 4601773Seschrock } 4611773Seschrock 462789Sahrens zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done; 4635065Sgz161490 zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE; 464789Sahrens zio->io_dk_callback.dkc_cookie = zio; 465789Sahrens 466789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 467789Sahrens (uintptr_t)&zio->io_dk_callback, 468789Sahrens FKIOCTL, kcred, NULL); 469789Sahrens 470789Sahrens if (error == 0) { 471789Sahrens /* 472789Sahrens * The ioctl will be done asychronously, 473789Sahrens * and will call vdev_disk_ioctl_done() 474789Sahrens * upon completion. 475789Sahrens */ 4765530Sbonwick return (ZIO_PIPELINE_STOP); 4775530Sbonwick } 4785530Sbonwick 4795530Sbonwick if (error == ENOTSUP || error == ENOTTY) { 4801773Seschrock /* 4814455Smishra * If we get ENOTSUP or ENOTTY, we know that 4824455Smishra * no future attempts will ever succeed. 4834455Smishra * In this case we set a persistent bit so 4844455Smishra * that we don't bother with the ioctl in the 4854455Smishra * future. 4861773Seschrock */ 4871773Seschrock vd->vdev_nowritecache = B_TRUE; 488789Sahrens } 489789Sahrens zio->io_error = error; 4901773Seschrock 491789Sahrens break; 492789Sahrens 493789Sahrens default: 494789Sahrens zio->io_error = ENOTSUP; 495789Sahrens } 496789Sahrens 4975530Sbonwick return (ZIO_PIPELINE_CONTINUE); 498789Sahrens } 499789Sahrens 500789Sahrens if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0) 5015530Sbonwick return (ZIO_PIPELINE_STOP); 502789Sahrens 503789Sahrens if ((zio = vdev_queue_io(zio)) == NULL) 5045530Sbonwick return (ZIO_PIPELINE_STOP); 5055530Sbonwick 5065530Sbonwick if (zio->io_type == ZIO_TYPE_WRITE) 5075530Sbonwick error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO; 5085530Sbonwick else 5095530Sbonwick error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO; 5105530Sbonwick error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error; 5115530Sbonwick 5125530Sbonwick if (error) { 5135530Sbonwick zio->io_error = error; 5145530Sbonwick zio_interrupt(zio); 5155530Sbonwick return (ZIO_PIPELINE_STOP); 5165530Sbonwick } 517789Sahrens 518789Sahrens flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 519789Sahrens flags |= B_BUSY | B_NOCACHE; 520789Sahrens if (zio->io_flags & ZIO_FLAG_FAILFAST) 521789Sahrens flags |= B_FAILFAST; 522789Sahrens 523789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 524789Sahrens 525789Sahrens vdb->vdb_io = zio; 526789Sahrens bp = &vdb->vdb_buf; 527789Sahrens 528789Sahrens bioinit(bp); 529789Sahrens bp->b_flags = flags; 530789Sahrens bp->b_bcount = zio->io_size; 531789Sahrens bp->b_un.b_addr = zio->io_data; 532789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 533789Sahrens bp->b_bufsize = zio->io_size; 534789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 535789Sahrens 536789Sahrens error = ldi_strategy(dvd->vd_lh, bp); 537789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 538789Sahrens ASSERT(error == 0); 5395530Sbonwick 5405530Sbonwick return (ZIO_PIPELINE_STOP); 541789Sahrens } 542789Sahrens 5435530Sbonwick static int 544789Sahrens vdev_disk_io_done(zio_t *zio) 545789Sahrens { 546789Sahrens vdev_queue_io_done(zio); 547789Sahrens 548789Sahrens if (zio->io_type == ZIO_TYPE_WRITE) 549789Sahrens vdev_cache_write(zio); 550789Sahrens 5511544Seschrock if (zio_injection_enabled && zio->io_error == 0) 5521544Seschrock zio->io_error = zio_handle_device_injection(zio->io_vd, EIO); 5531544Seschrock 5544451Seschrock /* 5554451Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 5564451Seschrock * the device has been removed. If this is the case, then we trigger an 5575329Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and 5585369Sgw25295 * make sure it's still accessible. 5594451Seschrock */ 5604451Seschrock if (zio->io_error == EIO) { 5615329Sgw25295 vdev_t *vd = zio->io_vd; 5625329Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd; 5635329Sgw25295 int state; 5645329Sgw25295 5654451Seschrock state = DKIO_NONE; 5665329Sgw25295 if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 5674451Seschrock FKIOCTL, kcred, NULL) == 0 && 5684451Seschrock state != DKIO_INSERTED) { 5694451Seschrock vd->vdev_remove_wanted = B_TRUE; 5704451Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 5715329Sgw25295 } else if (vdev_probe(vd) != 0) { 5725329Sgw25295 ASSERT(vd->vdev_ops->vdev_op_leaf); 5736976Seschrock if (!vd->vdev_is_failing) { 5746976Seschrock vd->vdev_is_failing = B_TRUE; 5756976Seschrock zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 5766976Seschrock vd->vdev_spa, vd, zio, 0, 0); 5776976Seschrock } 5784451Seschrock } 5794451Seschrock } 5804451Seschrock 5816615Sgw25295 if (zio_injection_enabled && zio->io_error == 0) 5826615Sgw25295 zio->io_error = zio_handle_label_injection(zio, EIO); 5836615Sgw25295 5845530Sbonwick return (ZIO_PIPELINE_CONTINUE); 585789Sahrens } 586789Sahrens 587789Sahrens vdev_ops_t vdev_disk_ops = { 588789Sahrens vdev_disk_open, 589789Sahrens vdev_disk_close, 5905329Sgw25295 vdev_disk_probe, 591789Sahrens vdev_default_asize, 592789Sahrens vdev_disk_io_start, 593789Sahrens vdev_disk_io_done, 594789Sahrens NULL, 595789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 596789Sahrens B_TRUE /* leaf vdev */ 597789Sahrens }; 5986423Sgw25295 5996423Sgw25295 /* 6007147Staylor * Given the root disk device devid or pathname, read the label from 6017147Staylor * the device, and construct a configuration nvlist. 6026423Sgw25295 */ 6037539SLin.Ling@Sun.COM int 6047539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 6056423Sgw25295 { 6066423Sgw25295 ldi_handle_t vd_lh; 6076423Sgw25295 vdev_label_t *label; 6086423Sgw25295 uint64_t s, size; 6096423Sgw25295 int l; 6107147Staylor ddi_devid_t tmpdevid; 611*7687SLin.Ling@Sun.COM int error = -1; 6127147Staylor char *minor_name; 6136423Sgw25295 6146423Sgw25295 /* 6156423Sgw25295 * Read the device label and build the nvlist. 6166423Sgw25295 */ 617*7687SLin.Ling@Sun.COM if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 6187147Staylor &minor_name) == 0) { 6197147Staylor error = ldi_open_by_devid(tmpdevid, minor_name, 6207147Staylor spa_mode, kcred, &vd_lh, zfs_li); 6217147Staylor ddi_devid_free(tmpdevid); 6227147Staylor ddi_devid_str_free(minor_name); 6237147Staylor } 6247147Staylor 625*7687SLin.Ling@Sun.COM if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 626*7687SLin.Ling@Sun.COM zfs_li))) 6277539SLin.Ling@Sun.COM return (error); 6286423Sgw25295 6296673Seschrock if (ldi_get_size(vd_lh, &s)) { 6306673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 6317539SLin.Ling@Sun.COM return (EIO); 6326673Seschrock } 6336423Sgw25295 6346423Sgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 6356423Sgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 6366423Sgw25295 6376423Sgw25295 for (l = 0; l < VDEV_LABELS; l++) { 6386423Sgw25295 uint64_t offset, state, txg = 0; 6396423Sgw25295 6406423Sgw25295 /* read vdev label */ 6416423Sgw25295 offset = vdev_label_offset(size, l, 0); 6426423Sgw25295 if (vdev_disk_physio(vd_lh, (caddr_t)label, 6436423Sgw25295 VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE + 6446423Sgw25295 VDEV_PHYS_SIZE, offset, B_READ) != 0) 6456423Sgw25295 continue; 6466423Sgw25295 6476423Sgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 6487539SLin.Ling@Sun.COM sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 6497539SLin.Ling@Sun.COM *config = NULL; 6506423Sgw25295 continue; 6516423Sgw25295 } 6526423Sgw25295 6537539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 6546423Sgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) { 6557539SLin.Ling@Sun.COM nvlist_free(*config); 6567539SLin.Ling@Sun.COM *config = NULL; 6576423Sgw25295 continue; 6586423Sgw25295 } 6596423Sgw25295 6607539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 6616423Sgw25295 &txg) != 0 || txg == 0) { 6627539SLin.Ling@Sun.COM nvlist_free(*config); 6637539SLin.Ling@Sun.COM *config = NULL; 6646423Sgw25295 continue; 6656423Sgw25295 } 6666423Sgw25295 6676423Sgw25295 break; 6686423Sgw25295 } 6696423Sgw25295 6706423Sgw25295 kmem_free(label, sizeof (vdev_label_t)); 6716673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 6726673Seschrock 6737539SLin.Ling@Sun.COM return (error); 6746423Sgw25295 } 675