xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 5329:33cb98223b2d)
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
48*5329Sgw25295 vdev_disk_open_common(vdev_t *vd)
49789Sahrens {
50789Sahrens 	vdev_disk_t *dvd;
51*5329Sgw25295 	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 
166*5329Sgw25295 	if (error)
167789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
168*5329Sgw25295 
169*5329Sgw25295 	return (error);
170*5329Sgw25295 }
171*5329Sgw25295 
172*5329Sgw25295 static int
173*5329Sgw25295 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
174*5329Sgw25295 {
175*5329Sgw25295 	vdev_disk_t *dvd;
176*5329Sgw25295 	struct dk_minfo dkm;
177*5329Sgw25295 	int error;
178*5329Sgw25295 	dev_t dev;
179*5329Sgw25295 	int otyp;
180*5329Sgw25295 
181*5329Sgw25295 	error = vdev_disk_open_common(vd);
182*5329Sgw25295 	if (error)
183789Sahrens 		return (error);
184789Sahrens 
185*5329Sgw25295 	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) {
192*5329Sgw25295 		char *physpath, *minorname;
193*5329Sgw25295 
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 
269*5329Sgw25295 static int
270*5329Sgw25295 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset,
271*5329Sgw25295     int flags)
272*5329Sgw25295 {
273*5329Sgw25295 	buf_t buf;
274*5329Sgw25295 	int error = 0;
275*5329Sgw25295 	vdev_disk_t *dvd = vd->vdev_tsd;
276*5329Sgw25295 
277*5329Sgw25295 	if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL)
278*5329Sgw25295 		return (EINVAL);
279*5329Sgw25295 
280*5329Sgw25295 	ASSERT(flags & B_READ || flags & B_WRITE);
281*5329Sgw25295 
282*5329Sgw25295 	bioinit(&buf);
283*5329Sgw25295 	buf.b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
284*5329Sgw25295 	buf.b_bcount = size;
285*5329Sgw25295 	buf.b_un.b_addr = (void *)data;
286*5329Sgw25295 	buf.b_lblkno = lbtodb(offset);
287*5329Sgw25295 	buf.b_bufsize = size;
288*5329Sgw25295 
289*5329Sgw25295 	error = ldi_strategy(dvd->vd_lh, &buf);
290*5329Sgw25295 	ASSERT(error == 0);
291*5329Sgw25295 	error = biowait(&buf);
292*5329Sgw25295 
293*5329Sgw25295 	if (zio_injection_enabled && error == 0)
294*5329Sgw25295 		error = zio_handle_device_injection(vd, EIO);
295*5329Sgw25295 
296*5329Sgw25295 	return (error);
297*5329Sgw25295 }
298*5329Sgw25295 
299*5329Sgw25295 static int
300*5329Sgw25295 vdev_disk_probe(vdev_t *vd)
301*5329Sgw25295 {
302*5329Sgw25295 	uint64_t offset;
303*5329Sgw25295 	vdev_t *nvd;
304*5329Sgw25295 	int l, error = 0, retries = 0;
305*5329Sgw25295 	char *vl_pad;
306*5329Sgw25295 
307*5329Sgw25295 	if (vd == NULL)
308*5329Sgw25295 		return (EINVAL);
309*5329Sgw25295 
310*5329Sgw25295 	/* Hijack the current vdev */
311*5329Sgw25295 	nvd = vd;
312*5329Sgw25295 
313*5329Sgw25295 	/*
314*5329Sgw25295 	 * Pick a random label to rewrite.
315*5329Sgw25295 	 */
316*5329Sgw25295 	l = spa_get_random(VDEV_LABELS);
317*5329Sgw25295 	ASSERT(l < VDEV_LABELS);
318*5329Sgw25295 
319*5329Sgw25295 	offset = vdev_label_offset(vd->vdev_psize, l,
320*5329Sgw25295 	    offsetof(vdev_label_t, vl_pad));
321*5329Sgw25295 
322*5329Sgw25295 	vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP);
323*5329Sgw25295 
324*5329Sgw25295 	/*
325*5329Sgw25295 	 * Try to read and write to a special location on the
326*5329Sgw25295 	 * label. We use the existing vdev initially and only
327*5329Sgw25295 	 * try to create and reopen it if we encounter a failure.
328*5329Sgw25295 	 */
329*5329Sgw25295 	while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
330*5329Sgw25295 	    offset, B_READ)) != 0 && retries == 0) {
331*5329Sgw25295 
332*5329Sgw25295 		nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
333*5329Sgw25295 		if (vd->vdev_path)
334*5329Sgw25295 			nvd->vdev_path = spa_strdup(vd->vdev_path);
335*5329Sgw25295 		if (vd->vdev_physpath)
336*5329Sgw25295 			nvd->vdev_physpath = spa_strdup(vd->vdev_physpath);
337*5329Sgw25295 		if (vd->vdev_devid)
338*5329Sgw25295 			nvd->vdev_devid = spa_strdup(vd->vdev_devid);
339*5329Sgw25295 		nvd->vdev_wholedisk = vd->vdev_wholedisk;
340*5329Sgw25295 		nvd->vdev_guid = vd->vdev_guid;
341*5329Sgw25295 		retries++;
342*5329Sgw25295 
343*5329Sgw25295 		error = vdev_disk_open_common(nvd);
344*5329Sgw25295 		if (error) {
345*5329Sgw25295 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
346*5329Sgw25295 			    nvd->vdev_stat.vs_aux);
347*5329Sgw25295 			break;
348*5329Sgw25295 		}
349*5329Sgw25295 	}
350*5329Sgw25295 
351*5329Sgw25295 	if (!error) {
352*5329Sgw25295 		error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
353*5329Sgw25295 		    offset, B_WRITE);
354*5329Sgw25295 	}
355*5329Sgw25295 
356*5329Sgw25295 	/* Clean up if we allocated a new vdev */
357*5329Sgw25295 	if (retries) {
358*5329Sgw25295 		vdev_disk_close(nvd);
359*5329Sgw25295 		if (nvd->vdev_path)
360*5329Sgw25295 			spa_strfree(nvd->vdev_path);
361*5329Sgw25295 		if (nvd->vdev_physpath)
362*5329Sgw25295 			spa_strfree(nvd->vdev_physpath);
363*5329Sgw25295 		if (nvd->vdev_devid)
364*5329Sgw25295 			spa_strfree(nvd->vdev_devid);
365*5329Sgw25295 		kmem_free(nvd, sizeof (vdev_t));
366*5329Sgw25295 	}
367*5329Sgw25295 	kmem_free(vl_pad, VDEV_SKIP_SIZE);
368*5329Sgw25295 
369*5329Sgw25295 	/* Reset the failing flag */
370*5329Sgw25295 	if (!error)
371*5329Sgw25295 		vd->vdev_is_failing = B_FALSE;
372*5329Sgw25295 
373*5329Sgw25295 	return (error);
374*5329Sgw25295 }
375*5329Sgw25295 
376789Sahrens static void
377789Sahrens vdev_disk_io_intr(buf_t *bp)
378789Sahrens {
379789Sahrens 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
380789Sahrens 	zio_t *zio = vdb->vdb_io;
381789Sahrens 
382789Sahrens 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
383789Sahrens 		zio->io_error = EIO;
384789Sahrens 
385789Sahrens 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
386789Sahrens 
387789Sahrens 	zio_next_stage_async(zio);
388789Sahrens }
389789Sahrens 
390789Sahrens static void
391789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
392789Sahrens {
393789Sahrens 	zio_t *zio = zio_arg;
394789Sahrens 
395789Sahrens 	zio->io_error = error;
396789Sahrens 
397789Sahrens 	zio_next_stage_async(zio);
398789Sahrens }
399789Sahrens 
400789Sahrens static void
401789Sahrens vdev_disk_io_start(zio_t *zio)
402789Sahrens {
403789Sahrens 	vdev_t *vd = zio->io_vd;
404789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
405789Sahrens 	vdev_disk_buf_t *vdb;
406789Sahrens 	buf_t *bp;
407789Sahrens 	int flags, error;
408789Sahrens 
409789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
410789Sahrens 		zio_vdev_io_bypass(zio);
411789Sahrens 
412789Sahrens 		/* XXPOLICY */
413*5329Sgw25295 		if (!vdev_readable(vd)) {
414789Sahrens 			zio->io_error = ENXIO;
415789Sahrens 			zio_next_stage_async(zio);
416789Sahrens 			return;
417789Sahrens 		}
418789Sahrens 
419789Sahrens 		switch (zio->io_cmd) {
420789Sahrens 
421789Sahrens 		case DKIOCFLUSHWRITECACHE:
422789Sahrens 
4232885Sahrens 			if (zfs_nocacheflush)
4242885Sahrens 				break;
4252885Sahrens 
4261773Seschrock 			if (vd->vdev_nowritecache) {
4271773Seschrock 				zio->io_error = ENOTSUP;
4281773Seschrock 				break;
4291773Seschrock 			}
4301773Seschrock 
431789Sahrens 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
4325065Sgz161490 			zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE;
433789Sahrens 			zio->io_dk_callback.dkc_cookie = zio;
434789Sahrens 
435789Sahrens 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
436789Sahrens 			    (uintptr_t)&zio->io_dk_callback,
437789Sahrens 			    FKIOCTL, kcred, NULL);
438789Sahrens 
439789Sahrens 			if (error == 0) {
440789Sahrens 				/*
441789Sahrens 				 * The ioctl will be done asychronously,
442789Sahrens 				 * and will call vdev_disk_ioctl_done()
443789Sahrens 				 * upon completion.
444789Sahrens 				 */
445789Sahrens 				return;
4464455Smishra 			} else if (error == ENOTSUP || error == ENOTTY) {
4471773Seschrock 				/*
4484455Smishra 				 * If we get ENOTSUP or ENOTTY, we know that
4494455Smishra 				 * no future attempts will ever succeed.
4504455Smishra 				 * In this case we set a persistent bit so
4514455Smishra 				 * that we don't bother with the ioctl in the
4524455Smishra 				 * future.
4531773Seschrock 				 */
4541773Seschrock 				vd->vdev_nowritecache = B_TRUE;
455789Sahrens 			}
456789Sahrens 			zio->io_error = error;
4571773Seschrock 
458789Sahrens 			break;
459789Sahrens 
460789Sahrens 		default:
461789Sahrens 			zio->io_error = ENOTSUP;
462789Sahrens 		}
463789Sahrens 
464789Sahrens 		zio_next_stage_async(zio);
465789Sahrens 		return;
466789Sahrens 	}
467789Sahrens 
468789Sahrens 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
469789Sahrens 		return;
470789Sahrens 
471789Sahrens 	if ((zio = vdev_queue_io(zio)) == NULL)
472789Sahrens 		return;
473789Sahrens 
474789Sahrens 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
475789Sahrens 	flags |= B_BUSY | B_NOCACHE;
476789Sahrens 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
477789Sahrens 		flags |= B_FAILFAST;
478789Sahrens 
479789Sahrens 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
480789Sahrens 
481789Sahrens 	vdb->vdb_io = zio;
482789Sahrens 	bp = &vdb->vdb_buf;
483789Sahrens 
484789Sahrens 	bioinit(bp);
485789Sahrens 	bp->b_flags = flags;
486789Sahrens 	bp->b_bcount = zio->io_size;
487789Sahrens 	bp->b_un.b_addr = zio->io_data;
488789Sahrens 	bp->b_lblkno = lbtodb(zio->io_offset);
489789Sahrens 	bp->b_bufsize = zio->io_size;
490789Sahrens 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
491789Sahrens 
492789Sahrens 	/* XXPOLICY */
493*5329Sgw25295 	if (zio->io_type == ZIO_TYPE_WRITE)
494*5329Sgw25295 		error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
495*5329Sgw25295 	else
496*5329Sgw25295 		error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
497*5329Sgw25295 	error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error;
498789Sahrens 	if (error) {
499789Sahrens 		zio->io_error = error;
500789Sahrens 		bioerror(bp, error);
501789Sahrens 		bp->b_resid = bp->b_bcount;
502789Sahrens 		bp->b_iodone(bp);
503789Sahrens 		return;
504789Sahrens 	}
505789Sahrens 
506789Sahrens 	error = ldi_strategy(dvd->vd_lh, bp);
507789Sahrens 	/* ldi_strategy() will return non-zero only on programming errors */
508789Sahrens 	ASSERT(error == 0);
509789Sahrens }
510789Sahrens 
511789Sahrens static void
512789Sahrens vdev_disk_io_done(zio_t *zio)
513789Sahrens {
514789Sahrens 	vdev_queue_io_done(zio);
515789Sahrens 
516789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE)
517789Sahrens 		vdev_cache_write(zio);
518789Sahrens 
5191544Seschrock 	if (zio_injection_enabled && zio->io_error == 0)
5201544Seschrock 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
5211544Seschrock 
5224451Seschrock 	/*
5234451Seschrock 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
5244451Seschrock 	 * the device has been removed.  If this is the case, then we trigger an
525*5329Sgw25295 	 * asynchronous removal of the device. Otherwise, probe the device and
526*5329Sgw25295 	 * make sure it's still functional.
5274451Seschrock 	 */
5284451Seschrock 	if (zio->io_error == EIO) {
529*5329Sgw25295 		vdev_t *vd = zio->io_vd;
530*5329Sgw25295 		vdev_disk_t *dvd = vd->vdev_tsd;
531*5329Sgw25295 		int state;
532*5329Sgw25295 
5334451Seschrock 		state = DKIO_NONE;
534*5329Sgw25295 		if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
5354451Seschrock 		    FKIOCTL, kcred, NULL) == 0 &&
5364451Seschrock 		    state != DKIO_INSERTED) {
5374451Seschrock 			vd->vdev_remove_wanted = B_TRUE;
5384451Seschrock 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
539*5329Sgw25295 		} else if (vdev_probe(vd) != 0) {
540*5329Sgw25295 			ASSERT(vd->vdev_ops->vdev_op_leaf);
541*5329Sgw25295 			vd->vdev_is_failing = B_TRUE;
5424451Seschrock 		}
5434451Seschrock 	}
5444451Seschrock 
545789Sahrens 	zio_next_stage(zio);
546789Sahrens }
547789Sahrens 
548789Sahrens vdev_ops_t vdev_disk_ops = {
549789Sahrens 	vdev_disk_open,
550789Sahrens 	vdev_disk_close,
551*5329Sgw25295 	vdev_disk_probe,
552789Sahrens 	vdev_default_asize,
553789Sahrens 	vdev_disk_io_start,
554789Sahrens 	vdev_disk_io_done,
555789Sahrens 	NULL,
556789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
557789Sahrens 	B_TRUE			/* leaf vdev */
558789Sahrens };
559