xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 5369:27c1235ef9a4)
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 /*
224451Seschrock  * Copyright 2007 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>
30789Sahrens #include <sys/vdev_disk.h>
31789Sahrens #include <sys/vdev_impl.h>
32789Sahrens #include <sys/fs/zfs.h>
33789Sahrens #include <sys/zio.h>
341171Seschrock #include <sys/sunldi.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 	 *
80789Sahrens 	 */
81789Sahrens 	if (vd->vdev_devid != NULL) {
82789Sahrens 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
83789Sahrens 		    &dvd->vd_minor) != 0) {
84789Sahrens 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
85789Sahrens 			return (EINVAL);
86789Sahrens 		}
87789Sahrens 	}
88789Sahrens 
89789Sahrens 	error = EINVAL;		/* presume failure */
90789Sahrens 
91789Sahrens 	if (vd->vdev_path != NULL) {
92789Sahrens 		ddi_devid_t devid;
93789Sahrens 
941171Seschrock 		if (vd->vdev_wholedisk == -1ULL) {
951171Seschrock 			size_t len = strlen(vd->vdev_path) + 3;
961171Seschrock 			char *buf = kmem_alloc(len, KM_SLEEP);
971171Seschrock 			ldi_handle_t lh;
981171Seschrock 
991171Seschrock 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
100789Sahrens 
1011171Seschrock 			if (ldi_open_by_name(buf, spa_mode, kcred,
1021171Seschrock 			    &lh, zfs_li) == 0) {
1031171Seschrock 				spa_strfree(vd->vdev_path);
1041171Seschrock 				vd->vdev_path = buf;
1051171Seschrock 				vd->vdev_wholedisk = 1ULL;
1061171Seschrock 				(void) ldi_close(lh, spa_mode, kcred);
1071171Seschrock 			} else {
1081171Seschrock 				kmem_free(buf, len);
1091171Seschrock 			}
1101171Seschrock 		}
111789Sahrens 
1121171Seschrock 		error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
1131171Seschrock 		    &dvd->vd_lh, zfs_li);
114789Sahrens 
115789Sahrens 		/*
116789Sahrens 		 * Compare the devid to the stored value.
117789Sahrens 		 */
118789Sahrens 		if (error == 0 && vd->vdev_devid != NULL &&
119789Sahrens 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
120789Sahrens 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
121789Sahrens 				error = EINVAL;
122789Sahrens 				(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
123789Sahrens 				dvd->vd_lh = NULL;
124789Sahrens 			}
125789Sahrens 			ddi_devid_free(devid);
126789Sahrens 		}
1271171Seschrock 
1281171Seschrock 		/*
1291171Seschrock 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
1301171Seschrock 		 * is not yet set, then this must be a slice.
1311171Seschrock 		 */
1321171Seschrock 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
1331171Seschrock 			vd->vdev_wholedisk = 0;
134789Sahrens 	}
135789Sahrens 
136789Sahrens 	/*
137789Sahrens 	 * If we were unable to open by path, or the devid check fails, open by
138789Sahrens 	 * devid instead.
139789Sahrens 	 */
140789Sahrens 	if (error != 0 && vd->vdev_devid != NULL)
141789Sahrens 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
142789Sahrens 		    spa_mode, kcred, &dvd->vd_lh, zfs_li);
143789Sahrens 
1444451Seschrock 	/*
1454451Seschrock 	 * If all else fails, then try opening by physical path (if available)
1464451Seschrock 	 * or the logical path (if we failed due to the devid check).  While not
1474451Seschrock 	 * as reliable as the devid, this will give us something, and the higher
1484451Seschrock 	 * level vdev validation will prevent us from opening the wrong device.
1494451Seschrock 	 */
1504451Seschrock 	if (error) {
1514451Seschrock 		if (vd->vdev_physpath != NULL &&
1524451Seschrock 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV)
1534451Seschrock 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode,
1544451Seschrock 			    kcred, &dvd->vd_lh, zfs_li);
1554451Seschrock 
1564451Seschrock 		/*
1574451Seschrock 		 * Note that we don't support the legacy auto-wholedisk support
1584451Seschrock 		 * as above.  This hasn't been used in a very long time and we
1594451Seschrock 		 * don't need to propagate its oddities to this edge condition.
1604451Seschrock 		 */
1614451Seschrock 		if (error && vd->vdev_path != NULL)
1624451Seschrock 			error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
1634451Seschrock 			    &dvd->vd_lh, zfs_li);
1644451Seschrock 	}
1654451Seschrock 
1665329Sgw25295 	if (error)
167789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
1685329Sgw25295 
1695329Sgw25295 	return (error);
1705329Sgw25295 }
1715329Sgw25295 
1725329Sgw25295 static int
1735329Sgw25295 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
1745329Sgw25295 {
1755329Sgw25295 	vdev_disk_t *dvd;
1765329Sgw25295 	struct dk_minfo dkm;
1775329Sgw25295 	int error;
1785329Sgw25295 	dev_t dev;
1795329Sgw25295 	int otyp;
1805329Sgw25295 
1815329Sgw25295 	error = vdev_disk_open_common(vd);
1825329Sgw25295 	if (error)
183789Sahrens 		return (error);
184789Sahrens 
1855329Sgw25295 	dvd = vd->vdev_tsd;
186789Sahrens 	/*
1874451Seschrock 	 * Once a device is opened, verify that the physical device path (if
1884451Seschrock 	 * available) is up to date.
1894451Seschrock 	 */
1904451Seschrock 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
1914451Seschrock 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
1925329Sgw25295 		char *physpath, *minorname;
1935329Sgw25295 
1944451Seschrock 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1954451Seschrock 		minorname = NULL;
1964451Seschrock 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
1974451Seschrock 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
1984451Seschrock 		    (vd->vdev_physpath == NULL ||
1994451Seschrock 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
2004451Seschrock 			if (vd->vdev_physpath)
2014451Seschrock 				spa_strfree(vd->vdev_physpath);
2024451Seschrock 			(void) strlcat(physpath, ":", MAXPATHLEN);
2034451Seschrock 			(void) strlcat(physpath, minorname, MAXPATHLEN);
2044451Seschrock 			vd->vdev_physpath = spa_strdup(physpath);
2054451Seschrock 		}
2064451Seschrock 		if (minorname)
2074451Seschrock 			kmem_free(minorname, strlen(minorname) + 1);
2084451Seschrock 		kmem_free(physpath, MAXPATHLEN);
2094451Seschrock 	}
2104451Seschrock 
2114451Seschrock 	/*
212789Sahrens 	 * Determine the actual size of the device.
213789Sahrens 	 */
214789Sahrens 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
215789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
216789Sahrens 		return (EINVAL);
217789Sahrens 	}
218789Sahrens 
2191732Sbonwick 	/*
2201732Sbonwick 	 * If we own the whole disk, try to enable disk write caching.
2211732Sbonwick 	 * We ignore errors because it's OK if we can't do it.
2221732Sbonwick 	 */
2231489Swebaker 	if (vd->vdev_wholedisk == 1) {
2241732Sbonwick 		int wce = 1;
2251732Sbonwick 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
2261732Sbonwick 		    FKIOCTL, kcred, NULL);
2271732Sbonwick 	}
2281489Swebaker 
2291732Sbonwick 	/*
2301732Sbonwick 	 * Determine the device's minimum transfer size.
2311732Sbonwick 	 * If the ioctl isn't supported, assume DEV_BSIZE.
2321732Sbonwick 	 */
2331732Sbonwick 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
2341732Sbonwick 	    FKIOCTL, kcred, NULL) != 0)
2351732Sbonwick 		dkm.dki_lbsize = DEV_BSIZE;
2361489Swebaker 
2371732Sbonwick 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
2381489Swebaker 
2391773Seschrock 	/*
2401773Seschrock 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
2411773Seschrock 	 * try again.
2421773Seschrock 	 */
2431773Seschrock 	vd->vdev_nowritecache = B_FALSE;
2441773Seschrock 
245789Sahrens 	return (0);
246789Sahrens }
247789Sahrens 
248789Sahrens static void
249789Sahrens vdev_disk_close(vdev_t *vd)
250789Sahrens {
251789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
252789Sahrens 
253789Sahrens 	if (dvd == NULL)
254789Sahrens 		return;
255789Sahrens 
256789Sahrens 	if (dvd->vd_minor != NULL)
257789Sahrens 		ddi_devid_str_free(dvd->vd_minor);
258789Sahrens 
259789Sahrens 	if (dvd->vd_devid != NULL)
260789Sahrens 		ddi_devid_free(dvd->vd_devid);
261789Sahrens 
262789Sahrens 	if (dvd->vd_lh != NULL)
263789Sahrens 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
264789Sahrens 
265789Sahrens 	kmem_free(dvd, sizeof (vdev_disk_t));
266789Sahrens 	vd->vdev_tsd = NULL;
267789Sahrens }
268789Sahrens 
2695329Sgw25295 static int
2705329Sgw25295 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset,
2715329Sgw25295     int flags)
2725329Sgw25295 {
2735329Sgw25295 	buf_t buf;
2745329Sgw25295 	int error = 0;
2755329Sgw25295 	vdev_disk_t *dvd = vd->vdev_tsd;
2765329Sgw25295 
2775329Sgw25295 	if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL)
2785329Sgw25295 		return (EINVAL);
2795329Sgw25295 
2805329Sgw25295 	ASSERT(flags & B_READ || flags & B_WRITE);
2815329Sgw25295 
2825329Sgw25295 	bioinit(&buf);
2835329Sgw25295 	buf.b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
2845329Sgw25295 	buf.b_bcount = size;
2855329Sgw25295 	buf.b_un.b_addr = (void *)data;
2865329Sgw25295 	buf.b_lblkno = lbtodb(offset);
2875329Sgw25295 	buf.b_bufsize = size;
2885329Sgw25295 
2895329Sgw25295 	error = ldi_strategy(dvd->vd_lh, &buf);
2905329Sgw25295 	ASSERT(error == 0);
2915329Sgw25295 	error = biowait(&buf);
2925329Sgw25295 
2935329Sgw25295 	if (zio_injection_enabled && error == 0)
2945329Sgw25295 		error = zio_handle_device_injection(vd, EIO);
2955329Sgw25295 
2965329Sgw25295 	return (error);
2975329Sgw25295 }
2985329Sgw25295 
299*5369Sgw25295 /*
300*5369Sgw25295  * Determine if the underlying device is accessible by reading and writing
301*5369Sgw25295  * to a known location. We must be able to do this during syncing context
302*5369Sgw25295  * and thus we cannot set the vdev state directly.
303*5369Sgw25295  */
3045329Sgw25295 static int
3055329Sgw25295 vdev_disk_probe(vdev_t *vd)
3065329Sgw25295 {
3075329Sgw25295 	uint64_t offset;
3085329Sgw25295 	vdev_t *nvd;
3095329Sgw25295 	int l, error = 0, retries = 0;
3105329Sgw25295 	char *vl_pad;
3115329Sgw25295 
3125329Sgw25295 	if (vd == NULL)
3135329Sgw25295 		return (EINVAL);
3145329Sgw25295 
3155329Sgw25295 	/* Hijack the current vdev */
3165329Sgw25295 	nvd = vd;
3175329Sgw25295 
3185329Sgw25295 	/*
3195329Sgw25295 	 * Pick a random label to rewrite.
3205329Sgw25295 	 */
3215329Sgw25295 	l = spa_get_random(VDEV_LABELS);
3225329Sgw25295 	ASSERT(l < VDEV_LABELS);
3235329Sgw25295 
3245329Sgw25295 	offset = vdev_label_offset(vd->vdev_psize, l,
3255329Sgw25295 	    offsetof(vdev_label_t, vl_pad));
3265329Sgw25295 
3275329Sgw25295 	vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP);
3285329Sgw25295 
3295329Sgw25295 	/*
3305329Sgw25295 	 * Try to read and write to a special location on the
3315329Sgw25295 	 * label. We use the existing vdev initially and only
3325329Sgw25295 	 * try to create and reopen it if we encounter a failure.
3335329Sgw25295 	 */
3345329Sgw25295 	while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
3355329Sgw25295 	    offset, B_READ)) != 0 && retries == 0) {
3365329Sgw25295 
3375329Sgw25295 		nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
3385329Sgw25295 		if (vd->vdev_path)
3395329Sgw25295 			nvd->vdev_path = spa_strdup(vd->vdev_path);
3405329Sgw25295 		if (vd->vdev_physpath)
3415329Sgw25295 			nvd->vdev_physpath = spa_strdup(vd->vdev_physpath);
3425329Sgw25295 		if (vd->vdev_devid)
3435329Sgw25295 			nvd->vdev_devid = spa_strdup(vd->vdev_devid);
3445329Sgw25295 		nvd->vdev_wholedisk = vd->vdev_wholedisk;
3455329Sgw25295 		nvd->vdev_guid = vd->vdev_guid;
3465329Sgw25295 		retries++;
3475329Sgw25295 
3485329Sgw25295 		error = vdev_disk_open_common(nvd);
349*5369Sgw25295 		if (error)
3505329Sgw25295 			break;
3515329Sgw25295 	}
3525329Sgw25295 
3535329Sgw25295 	if (!error) {
3545329Sgw25295 		error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
3555329Sgw25295 		    offset, B_WRITE);
3565329Sgw25295 	}
3575329Sgw25295 
3585329Sgw25295 	/* Clean up if we allocated a new vdev */
3595329Sgw25295 	if (retries) {
3605329Sgw25295 		vdev_disk_close(nvd);
3615329Sgw25295 		if (nvd->vdev_path)
3625329Sgw25295 			spa_strfree(nvd->vdev_path);
3635329Sgw25295 		if (nvd->vdev_physpath)
3645329Sgw25295 			spa_strfree(nvd->vdev_physpath);
3655329Sgw25295 		if (nvd->vdev_devid)
3665329Sgw25295 			spa_strfree(nvd->vdev_devid);
3675329Sgw25295 		kmem_free(nvd, sizeof (vdev_t));
3685329Sgw25295 	}
3695329Sgw25295 	kmem_free(vl_pad, VDEV_SKIP_SIZE);
3705329Sgw25295 
3715329Sgw25295 	/* Reset the failing flag */
3725329Sgw25295 	if (!error)
3735329Sgw25295 		vd->vdev_is_failing = B_FALSE;
3745329Sgw25295 
3755329Sgw25295 	return (error);
3765329Sgw25295 }
3775329Sgw25295 
378789Sahrens static void
379789Sahrens vdev_disk_io_intr(buf_t *bp)
380789Sahrens {
381789Sahrens 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
382789Sahrens 	zio_t *zio = vdb->vdb_io;
383789Sahrens 
384789Sahrens 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
385789Sahrens 		zio->io_error = EIO;
386789Sahrens 
387789Sahrens 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
388789Sahrens 
389789Sahrens 	zio_next_stage_async(zio);
390789Sahrens }
391789Sahrens 
392789Sahrens static void
393789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
394789Sahrens {
395789Sahrens 	zio_t *zio = zio_arg;
396789Sahrens 
397789Sahrens 	zio->io_error = error;
398789Sahrens 
399789Sahrens 	zio_next_stage_async(zio);
400789Sahrens }
401789Sahrens 
402789Sahrens static void
403789Sahrens vdev_disk_io_start(zio_t *zio)
404789Sahrens {
405789Sahrens 	vdev_t *vd = zio->io_vd;
406789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
407789Sahrens 	vdev_disk_buf_t *vdb;
408789Sahrens 	buf_t *bp;
409789Sahrens 	int flags, error;
410789Sahrens 
411789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
412789Sahrens 		zio_vdev_io_bypass(zio);
413789Sahrens 
414789Sahrens 		/* XXPOLICY */
4155329Sgw25295 		if (!vdev_readable(vd)) {
416789Sahrens 			zio->io_error = ENXIO;
417789Sahrens 			zio_next_stage_async(zio);
418789Sahrens 			return;
419789Sahrens 		}
420789Sahrens 
421789Sahrens 		switch (zio->io_cmd) {
422789Sahrens 
423789Sahrens 		case DKIOCFLUSHWRITECACHE:
424789Sahrens 
4252885Sahrens 			if (zfs_nocacheflush)
4262885Sahrens 				break;
4272885Sahrens 
4281773Seschrock 			if (vd->vdev_nowritecache) {
4291773Seschrock 				zio->io_error = ENOTSUP;
4301773Seschrock 				break;
4311773Seschrock 			}
4321773Seschrock 
433789Sahrens 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
4345065Sgz161490 			zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE;
435789Sahrens 			zio->io_dk_callback.dkc_cookie = zio;
436789Sahrens 
437789Sahrens 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
438789Sahrens 			    (uintptr_t)&zio->io_dk_callback,
439789Sahrens 			    FKIOCTL, kcred, NULL);
440789Sahrens 
441789Sahrens 			if (error == 0) {
442789Sahrens 				/*
443789Sahrens 				 * The ioctl will be done asychronously,
444789Sahrens 				 * and will call vdev_disk_ioctl_done()
445789Sahrens 				 * upon completion.
446789Sahrens 				 */
447789Sahrens 				return;
4484455Smishra 			} else if (error == ENOTSUP || error == ENOTTY) {
4491773Seschrock 				/*
4504455Smishra 				 * If we get ENOTSUP or ENOTTY, we know that
4514455Smishra 				 * no future attempts will ever succeed.
4524455Smishra 				 * In this case we set a persistent bit so
4534455Smishra 				 * that we don't bother with the ioctl in the
4544455Smishra 				 * future.
4551773Seschrock 				 */
4561773Seschrock 				vd->vdev_nowritecache = B_TRUE;
457789Sahrens 			}
458789Sahrens 			zio->io_error = error;
4591773Seschrock 
460789Sahrens 			break;
461789Sahrens 
462789Sahrens 		default:
463789Sahrens 			zio->io_error = ENOTSUP;
464789Sahrens 		}
465789Sahrens 
466789Sahrens 		zio_next_stage_async(zio);
467789Sahrens 		return;
468789Sahrens 	}
469789Sahrens 
470789Sahrens 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
471789Sahrens 		return;
472789Sahrens 
473789Sahrens 	if ((zio = vdev_queue_io(zio)) == NULL)
474789Sahrens 		return;
475789Sahrens 
476789Sahrens 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
477789Sahrens 	flags |= B_BUSY | B_NOCACHE;
478789Sahrens 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
479789Sahrens 		flags |= B_FAILFAST;
480789Sahrens 
481789Sahrens 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
482789Sahrens 
483789Sahrens 	vdb->vdb_io = zio;
484789Sahrens 	bp = &vdb->vdb_buf;
485789Sahrens 
486789Sahrens 	bioinit(bp);
487789Sahrens 	bp->b_flags = flags;
488789Sahrens 	bp->b_bcount = zio->io_size;
489789Sahrens 	bp->b_un.b_addr = zio->io_data;
490789Sahrens 	bp->b_lblkno = lbtodb(zio->io_offset);
491789Sahrens 	bp->b_bufsize = zio->io_size;
492789Sahrens 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
493789Sahrens 
494789Sahrens 	/* XXPOLICY */
4955329Sgw25295 	if (zio->io_type == ZIO_TYPE_WRITE)
4965329Sgw25295 		error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
4975329Sgw25295 	else
4985329Sgw25295 		error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
4995329Sgw25295 	error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error;
500789Sahrens 	if (error) {
501789Sahrens 		zio->io_error = error;
502789Sahrens 		bioerror(bp, error);
503789Sahrens 		bp->b_resid = bp->b_bcount;
504789Sahrens 		bp->b_iodone(bp);
505789Sahrens 		return;
506789Sahrens 	}
507789Sahrens 
508789Sahrens 	error = ldi_strategy(dvd->vd_lh, bp);
509789Sahrens 	/* ldi_strategy() will return non-zero only on programming errors */
510789Sahrens 	ASSERT(error == 0);
511789Sahrens }
512789Sahrens 
513789Sahrens static void
514789Sahrens vdev_disk_io_done(zio_t *zio)
515789Sahrens {
516789Sahrens 	vdev_queue_io_done(zio);
517789Sahrens 
518789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE)
519789Sahrens 		vdev_cache_write(zio);
520789Sahrens 
5211544Seschrock 	if (zio_injection_enabled && zio->io_error == 0)
5221544Seschrock 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
5231544Seschrock 
5244451Seschrock 	/*
5254451Seschrock 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
5264451Seschrock 	 * the device has been removed.  If this is the case, then we trigger an
5275329Sgw25295 	 * asynchronous removal of the device. Otherwise, probe the device and
528*5369Sgw25295 	 * make sure it's still accessible.
5294451Seschrock 	 */
5304451Seschrock 	if (zio->io_error == EIO) {
5315329Sgw25295 		vdev_t *vd = zio->io_vd;
5325329Sgw25295 		vdev_disk_t *dvd = vd->vdev_tsd;
5335329Sgw25295 		int state;
5345329Sgw25295 
5354451Seschrock 		state = DKIO_NONE;
5365329Sgw25295 		if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
5374451Seschrock 		    FKIOCTL, kcred, NULL) == 0 &&
5384451Seschrock 		    state != DKIO_INSERTED) {
5394451Seschrock 			vd->vdev_remove_wanted = B_TRUE;
5404451Seschrock 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
5415329Sgw25295 		} else if (vdev_probe(vd) != 0) {
5425329Sgw25295 			ASSERT(vd->vdev_ops->vdev_op_leaf);
5435329Sgw25295 			vd->vdev_is_failing = B_TRUE;
5444451Seschrock 		}
5454451Seschrock 	}
5464451Seschrock 
547789Sahrens 	zio_next_stage(zio);
548789Sahrens }
549789Sahrens 
550789Sahrens vdev_ops_t vdev_disk_ops = {
551789Sahrens 	vdev_disk_open,
552789Sahrens 	vdev_disk_close,
5535329Sgw25295 	vdev_disk_probe,
554789Sahrens 	vdev_default_asize,
555789Sahrens 	vdev_disk_io_start,
556789Sahrens 	vdev_disk_io_done,
557789Sahrens 	NULL,
558789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
559789Sahrens 	B_TRUE			/* leaf vdev */
560789Sahrens };
561