xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 10850:bb29ac73664d)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51489Swebaker  * Common Development and Distribution License (the "License").
61489Swebaker  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
228876SLin.Ling@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens #include <sys/spa.h>
286423Sgw25295 #include <sys/refcount.h>
29789Sahrens #include <sys/vdev_disk.h>
30789Sahrens #include <sys/vdev_impl.h>
31789Sahrens #include <sys/fs/zfs.h>
32789Sahrens #include <sys/zio.h>
331171Seschrock #include <sys/sunldi.h>
346976Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens 
36789Sahrens /*
37789Sahrens  * Virtual device vector for disks.
38789Sahrens  */
39789Sahrens 
40789Sahrens extern ldi_ident_t zfs_li;
41789Sahrens 
42789Sahrens typedef struct vdev_disk_buf {
43789Sahrens 	buf_t	vdb_buf;
44789Sahrens 	zio_t	*vdb_io;
45789Sahrens } vdev_disk_buf_t;
46789Sahrens 
47789Sahrens static int
487754SJeff.Bonwick@Sun.COM vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
49789Sahrens {
508241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
51789Sahrens 	vdev_disk_t *dvd;
527754SJeff.Bonwick@Sun.COM 	struct dk_minfo dkm;
537754SJeff.Bonwick@Sun.COM 	int error;
545329Sgw25295 	dev_t dev;
557754SJeff.Bonwick@Sun.COM 	int otyp;
56789Sahrens 
57789Sahrens 	/*
58789Sahrens 	 * We must have a pathname, and it must be absolute.
59789Sahrens 	 */
60789Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
61789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
62789Sahrens 		return (EINVAL);
63789Sahrens 	}
64789Sahrens 
65*10850SGeorge.Wilson@Sun.COM 	/*
66*10850SGeorge.Wilson@Sun.COM 	 * Reopen the device if it's not currently open. Otherwise,
67*10850SGeorge.Wilson@Sun.COM 	 * just update the physical size of the device.
68*10850SGeorge.Wilson@Sun.COM 	 */
69*10850SGeorge.Wilson@Sun.COM 	if (vd->vdev_tsd != NULL) {
70*10850SGeorge.Wilson@Sun.COM 		ASSERT(vd->vdev_reopening);
71*10850SGeorge.Wilson@Sun.COM 		dvd = vd->vdev_tsd;
72*10850SGeorge.Wilson@Sun.COM 		goto skip_open;
73*10850SGeorge.Wilson@Sun.COM 	}
74*10850SGeorge.Wilson@Sun.COM 
75789Sahrens 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
76789Sahrens 
77789Sahrens 	/*
78789Sahrens 	 * When opening a disk device, we want to preserve the user's original
79789Sahrens 	 * intent.  We always want to open the device by the path the user gave
80789Sahrens 	 * us, even if it is one of multiple paths to the save device.  But we
81789Sahrens 	 * also want to be able to survive disks being removed/recabled.
82789Sahrens 	 * Therefore the sequence of opening devices is:
83789Sahrens 	 *
841171Seschrock 	 * 1. Try opening the device by path.  For legacy pools without the
851171Seschrock 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
86789Sahrens 	 *
87789Sahrens 	 * 2. If the devid of the device matches the stored value, return
88789Sahrens 	 *    success.
89789Sahrens 	 *
90789Sahrens 	 * 3. Otherwise, the device may have moved.  Try opening the device
91789Sahrens 	 *    by the devid instead.
92789Sahrens 	 */
93789Sahrens 	if (vd->vdev_devid != NULL) {
94789Sahrens 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
95789Sahrens 		    &dvd->vd_minor) != 0) {
96789Sahrens 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
97789Sahrens 			return (EINVAL);
98789Sahrens 		}
99789Sahrens 	}
100789Sahrens 
101789Sahrens 	error = EINVAL;		/* presume failure */
102789Sahrens 
103*10850SGeorge.Wilson@Sun.COM 	if (vd->vdev_path != NULL) {
104789Sahrens 		ddi_devid_t devid;
105789Sahrens 
1061171Seschrock 		if (vd->vdev_wholedisk == -1ULL) {
1071171Seschrock 			size_t len = strlen(vd->vdev_path) + 3;
1081171Seschrock 			char *buf = kmem_alloc(len, KM_SLEEP);
1091171Seschrock 			ldi_handle_t lh;
1101171Seschrock 
1111171Seschrock 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
112789Sahrens 
1138241SJeff.Bonwick@Sun.COM 			if (ldi_open_by_name(buf, spa_mode(spa), kcred,
1141171Seschrock 			    &lh, zfs_li) == 0) {
1151171Seschrock 				spa_strfree(vd->vdev_path);
1161171Seschrock 				vd->vdev_path = buf;
1171171Seschrock 				vd->vdev_wholedisk = 1ULL;
1188241SJeff.Bonwick@Sun.COM 				(void) ldi_close(lh, spa_mode(spa), kcred);
1191171Seschrock 			} else {
1201171Seschrock 				kmem_free(buf, len);
1211171Seschrock 			}
1221171Seschrock 		}
123789Sahrens 
1248241SJeff.Bonwick@Sun.COM 		error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
1251171Seschrock 		    &dvd->vd_lh, zfs_li);
126789Sahrens 
127789Sahrens 		/*
128789Sahrens 		 * Compare the devid to the stored value.
129789Sahrens 		 */
130789Sahrens 		if (error == 0 && vd->vdev_devid != NULL &&
131789Sahrens 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
132789Sahrens 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
133789Sahrens 				error = EINVAL;
1348241SJeff.Bonwick@Sun.COM 				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
1358241SJeff.Bonwick@Sun.COM 				    kcred);
136789Sahrens 				dvd->vd_lh = NULL;
137789Sahrens 			}
138789Sahrens 			ddi_devid_free(devid);
139789Sahrens 		}
1401171Seschrock 
1411171Seschrock 		/*
1421171Seschrock 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
1431171Seschrock 		 * is not yet set, then this must be a slice.
1441171Seschrock 		 */
1451171Seschrock 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
1461171Seschrock 			vd->vdev_wholedisk = 0;
147789Sahrens 	}
148789Sahrens 
149789Sahrens 	/*
150789Sahrens 	 * If we were unable to open by path, or the devid check fails, open by
151789Sahrens 	 * devid instead.
152789Sahrens 	 */
153789Sahrens 	if (error != 0 && vd->vdev_devid != NULL)
154789Sahrens 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
1558241SJeff.Bonwick@Sun.COM 		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
156789Sahrens 
1574451Seschrock 	/*
1584451Seschrock 	 * If all else fails, then try opening by physical path (if available)
1594451Seschrock 	 * or the logical path (if we failed due to the devid check).  While not
1604451Seschrock 	 * as reliable as the devid, this will give us something, and the higher
1614451Seschrock 	 * level vdev validation will prevent us from opening the wrong device.
1624451Seschrock 	 */
1634451Seschrock 	if (error) {
1644451Seschrock 		if (vd->vdev_physpath != NULL &&
1658269SMark.Musante@Sun.COM 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
1668241SJeff.Bonwick@Sun.COM 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
1674451Seschrock 			    kcred, &dvd->vd_lh, zfs_li);
1684451Seschrock 
1694451Seschrock 		/*
1704451Seschrock 		 * Note that we don't support the legacy auto-wholedisk support
1714451Seschrock 		 * as above.  This hasn't been used in a very long time and we
1724451Seschrock 		 * don't need to propagate its oddities to this edge condition.
1734451Seschrock 		 */
174*10850SGeorge.Wilson@Sun.COM 		if (error && vd->vdev_path != NULL)
1758241SJeff.Bonwick@Sun.COM 			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
1768241SJeff.Bonwick@Sun.COM 			    kcred, &dvd->vd_lh, zfs_li);
1774451Seschrock 	}
1784451Seschrock 
1797754SJeff.Bonwick@Sun.COM 	if (error) {
180789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
1817754SJeff.Bonwick@Sun.COM 		return (error);
1827754SJeff.Bonwick@Sun.COM 	}
1835329Sgw25295 
184789Sahrens 	/*
1854451Seschrock 	 * Once a device is opened, verify that the physical device path (if
1864451Seschrock 	 * available) is up to date.
1874451Seschrock 	 */
1884451Seschrock 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
1894451Seschrock 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
1905329Sgw25295 		char *physpath, *minorname;
1915329Sgw25295 
1924451Seschrock 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1934451Seschrock 		minorname = NULL;
1944451Seschrock 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
1954451Seschrock 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
1964451Seschrock 		    (vd->vdev_physpath == NULL ||
1974451Seschrock 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
1984451Seschrock 			if (vd->vdev_physpath)
1994451Seschrock 				spa_strfree(vd->vdev_physpath);
2004451Seschrock 			(void) strlcat(physpath, ":", MAXPATHLEN);
2014451Seschrock 			(void) strlcat(physpath, minorname, MAXPATHLEN);
2024451Seschrock 			vd->vdev_physpath = spa_strdup(physpath);
2034451Seschrock 		}
2044451Seschrock 		if (minorname)
2054451Seschrock 			kmem_free(minorname, strlen(minorname) + 1);
2064451Seschrock 		kmem_free(physpath, MAXPATHLEN);
2074451Seschrock 	}
2084451Seschrock 
209*10850SGeorge.Wilson@Sun.COM skip_open:
2104451Seschrock 	/*
211789Sahrens 	 * Determine the actual size of the device.
212789Sahrens 	 */
213789Sahrens 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
214789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
215789Sahrens 		return (EINVAL);
216789Sahrens 	}
217789Sahrens 
2181732Sbonwick 	/*
2191732Sbonwick 	 * If we own the whole disk, try to enable disk write caching.
2201732Sbonwick 	 * We ignore errors because it's OK if we can't do it.
2211732Sbonwick 	 */
2221489Swebaker 	if (vd->vdev_wholedisk == 1) {
2231732Sbonwick 		int wce = 1;
2241732Sbonwick 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
2251732Sbonwick 		    FKIOCTL, kcred, NULL);
2261732Sbonwick 	}
2271489Swebaker 
2281732Sbonwick 	/*
2291732Sbonwick 	 * Determine the device's minimum transfer size.
2301732Sbonwick 	 * If the ioctl isn't supported, assume DEV_BSIZE.
2311732Sbonwick 	 */
2321732Sbonwick 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
2331732Sbonwick 	    FKIOCTL, kcred, NULL) != 0)
2341732Sbonwick 		dkm.dki_lbsize = DEV_BSIZE;
2351489Swebaker 
2361732Sbonwick 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
2371489Swebaker 
2381773Seschrock 	/*
2391773Seschrock 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
2401773Seschrock 	 * try again.
2411773Seschrock 	 */
2421773Seschrock 	vd->vdev_nowritecache = B_FALSE;
2431773Seschrock 
244789Sahrens 	return (0);
245789Sahrens }
246789Sahrens 
247789Sahrens static void
248789Sahrens vdev_disk_close(vdev_t *vd)
249789Sahrens {
250789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
251789Sahrens 
252*10850SGeorge.Wilson@Sun.COM 	if (vd->vdev_reopening || dvd == NULL)
253789Sahrens 		return;
254789Sahrens 
255789Sahrens 	if (dvd->vd_minor != NULL)
256789Sahrens 		ddi_devid_str_free(dvd->vd_minor);
257789Sahrens 
258789Sahrens 	if (dvd->vd_devid != NULL)
259789Sahrens 		ddi_devid_free(dvd->vd_devid);
260789Sahrens 
261789Sahrens 	if (dvd->vd_lh != NULL)
2628241SJeff.Bonwick@Sun.COM 		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
263789Sahrens 
264789Sahrens 	kmem_free(dvd, sizeof (vdev_disk_t));
265789Sahrens 	vd->vdev_tsd = NULL;
266789Sahrens }
267789Sahrens 
2686423Sgw25295 int
2696423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
2706423Sgw25295     uint64_t offset, int flags)
2716423Sgw25295 {
2726423Sgw25295 	buf_t *bp;
2736423Sgw25295 	int error = 0;
2746423Sgw25295 
2756423Sgw25295 	if (vd_lh == NULL)
2766423Sgw25295 		return (EINVAL);
2776423Sgw25295 
2786423Sgw25295 	ASSERT(flags & B_READ || flags & B_WRITE);
2796423Sgw25295 
2806423Sgw25295 	bp = getrbuf(KM_SLEEP);
2816423Sgw25295 	bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
2826423Sgw25295 	bp->b_bcount = size;
2836423Sgw25295 	bp->b_un.b_addr = (void *)data;
2846423Sgw25295 	bp->b_lblkno = lbtodb(offset);
2856423Sgw25295 	bp->b_bufsize = size;
2866423Sgw25295 
2876423Sgw25295 	error = ldi_strategy(vd_lh, bp);
2886423Sgw25295 	ASSERT(error == 0);
2896423Sgw25295 	if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
2906423Sgw25295 		error = EIO;
2916423Sgw25295 	freerbuf(bp);
2926423Sgw25295 
2936423Sgw25295 	return (error);
2946423Sgw25295 }
2956423Sgw25295 
296789Sahrens static void
297789Sahrens vdev_disk_io_intr(buf_t *bp)
298789Sahrens {
299789Sahrens 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
300789Sahrens 	zio_t *zio = vdb->vdb_io;
301789Sahrens 
3026976Seschrock 	/*
3036976Seschrock 	 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
3046976Seschrock 	 * Rather than teach the rest of the stack about other error
3056976Seschrock 	 * possibilities (EFAULT, etc), we normalize the error value here.
3066976Seschrock 	 */
3076976Seschrock 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
3086976Seschrock 
3096976Seschrock 	if (zio->io_error == 0 && bp->b_resid != 0)
310789Sahrens 		zio->io_error = EIO;
311789Sahrens 
312789Sahrens 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
313789Sahrens 
3145530Sbonwick 	zio_interrupt(zio);
315789Sahrens }
316789Sahrens 
317789Sahrens static void
3187762SJeff.Bonwick@Sun.COM vdev_disk_ioctl_free(zio_t *zio)
3197762SJeff.Bonwick@Sun.COM {
3207762SJeff.Bonwick@Sun.COM 	kmem_free(zio->io_vsd, sizeof (struct dk_callback));
3217762SJeff.Bonwick@Sun.COM }
3227762SJeff.Bonwick@Sun.COM 
32310614SJonathan.Adams@Sun.COM static const zio_vsd_ops_t vdev_disk_vsd_ops = {
32410614SJonathan.Adams@Sun.COM 	vdev_disk_ioctl_free,
32510614SJonathan.Adams@Sun.COM 	zio_vsd_default_cksum_report
32610614SJonathan.Adams@Sun.COM };
32710614SJonathan.Adams@Sun.COM 
3287762SJeff.Bonwick@Sun.COM static void
329789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
330789Sahrens {
331789Sahrens 	zio_t *zio = zio_arg;
332789Sahrens 
333789Sahrens 	zio->io_error = error;
334789Sahrens 
3355530Sbonwick 	zio_interrupt(zio);
336789Sahrens }
337789Sahrens 
3385530Sbonwick static int
339789Sahrens vdev_disk_io_start(zio_t *zio)
340789Sahrens {
341789Sahrens 	vdev_t *vd = zio->io_vd;
342789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
343789Sahrens 	vdev_disk_buf_t *vdb;
3447754SJeff.Bonwick@Sun.COM 	struct dk_callback *dkc;
345789Sahrens 	buf_t *bp;
3467754SJeff.Bonwick@Sun.COM 	int error;
347789Sahrens 
348789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
349789Sahrens 		/* XXPOLICY */
3505329Sgw25295 		if (!vdev_readable(vd)) {
351789Sahrens 			zio->io_error = ENXIO;
3525530Sbonwick 			return (ZIO_PIPELINE_CONTINUE);
353789Sahrens 		}
354789Sahrens 
355789Sahrens 		switch (zio->io_cmd) {
356789Sahrens 
357789Sahrens 		case DKIOCFLUSHWRITECACHE:
358789Sahrens 
3592885Sahrens 			if (zfs_nocacheflush)
3602885Sahrens 				break;
3612885Sahrens 
3621773Seschrock 			if (vd->vdev_nowritecache) {
3631773Seschrock 				zio->io_error = ENOTSUP;
3641773Seschrock 				break;
3651773Seschrock 			}
3661773Seschrock 
3677754SJeff.Bonwick@Sun.COM 			zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
36810614SJonathan.Adams@Sun.COM 			zio->io_vsd_ops = &vdev_disk_vsd_ops;
3697754SJeff.Bonwick@Sun.COM 
3707754SJeff.Bonwick@Sun.COM 			dkc->dkc_callback = vdev_disk_ioctl_done;
3717754SJeff.Bonwick@Sun.COM 			dkc->dkc_flag = FLUSH_VOLATILE;
3727754SJeff.Bonwick@Sun.COM 			dkc->dkc_cookie = zio;
373789Sahrens 
374789Sahrens 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
3757754SJeff.Bonwick@Sun.COM 			    (uintptr_t)dkc, FKIOCTL, kcred, NULL);
376789Sahrens 
377789Sahrens 			if (error == 0) {
378789Sahrens 				/*
379789Sahrens 				 * The ioctl will be done asychronously,
380789Sahrens 				 * and will call vdev_disk_ioctl_done()
381789Sahrens 				 * upon completion.
382789Sahrens 				 */
3835530Sbonwick 				return (ZIO_PIPELINE_STOP);
3845530Sbonwick 			}
3855530Sbonwick 
3865530Sbonwick 			if (error == ENOTSUP || error == ENOTTY) {
3871773Seschrock 				/*
3884455Smishra 				 * If we get ENOTSUP or ENOTTY, we know that
3894455Smishra 				 * no future attempts will ever succeed.
3904455Smishra 				 * In this case we set a persistent bit so
3914455Smishra 				 * that we don't bother with the ioctl in the
3924455Smishra 				 * future.
3931773Seschrock 				 */
3941773Seschrock 				vd->vdev_nowritecache = B_TRUE;
395789Sahrens 			}
396789Sahrens 			zio->io_error = error;
3971773Seschrock 
398789Sahrens 			break;
399789Sahrens 
400789Sahrens 		default:
401789Sahrens 			zio->io_error = ENOTSUP;
402789Sahrens 		}
403789Sahrens 
4045530Sbonwick 		return (ZIO_PIPELINE_CONTINUE);
405789Sahrens 	}
406789Sahrens 
407789Sahrens 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
408789Sahrens 
409789Sahrens 	vdb->vdb_io = zio;
410789Sahrens 	bp = &vdb->vdb_buf;
411789Sahrens 
412789Sahrens 	bioinit(bp);
4137754SJeff.Bonwick@Sun.COM 	bp->b_flags = B_BUSY | B_NOCACHE |
4149725SEric.Schrock@Sun.COM 	    (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
4159725SEric.Schrock@Sun.COM 	if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
4169725SEric.Schrock@Sun.COM 		bp->b_flags |= B_FAILFAST;
417789Sahrens 	bp->b_bcount = zio->io_size;
418789Sahrens 	bp->b_un.b_addr = zio->io_data;
419789Sahrens 	bp->b_lblkno = lbtodb(zio->io_offset);
420789Sahrens 	bp->b_bufsize = zio->io_size;
421789Sahrens 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
422789Sahrens 
423789Sahrens 	/* ldi_strategy() will return non-zero only on programming errors */
4247754SJeff.Bonwick@Sun.COM 	VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
4255530Sbonwick 
4265530Sbonwick 	return (ZIO_PIPELINE_STOP);
427789Sahrens }
428789Sahrens 
4297754SJeff.Bonwick@Sun.COM static void
430789Sahrens vdev_disk_io_done(zio_t *zio)
431789Sahrens {
4327754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd;
4331544Seschrock 
4344451Seschrock 	/*
4354451Seschrock 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
4364451Seschrock 	 * the device has been removed.  If this is the case, then we trigger an
4375329Sgw25295 	 * asynchronous removal of the device. Otherwise, probe the device and
4385369Sgw25295 	 * make sure it's still accessible.
4394451Seschrock 	 */
44010575SEric.Schrock@Sun.COM 	if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
4415329Sgw25295 		vdev_disk_t *dvd = vd->vdev_tsd;
4427754SJeff.Bonwick@Sun.COM 		int state = DKIO_NONE;
4435329Sgw25295 
4447754SJeff.Bonwick@Sun.COM 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
4457754SJeff.Bonwick@Sun.COM 		    FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
44610575SEric.Schrock@Sun.COM 			/*
44710575SEric.Schrock@Sun.COM 			 * We post the resource as soon as possible, instead of
44810575SEric.Schrock@Sun.COM 			 * when the async removal actually happens, because the
44910575SEric.Schrock@Sun.COM 			 * DE is using this information to discard previous I/O
45010575SEric.Schrock@Sun.COM 			 * errors.
45110575SEric.Schrock@Sun.COM 			 */
45210575SEric.Schrock@Sun.COM 			zfs_post_remove(zio->io_spa, vd);
4534451Seschrock 			vd->vdev_remove_wanted = B_TRUE;
4544451Seschrock 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
4554451Seschrock 		}
4564451Seschrock 	}
457789Sahrens }
458789Sahrens 
459789Sahrens vdev_ops_t vdev_disk_ops = {
460789Sahrens 	vdev_disk_open,
461789Sahrens 	vdev_disk_close,
462789Sahrens 	vdev_default_asize,
463789Sahrens 	vdev_disk_io_start,
464789Sahrens 	vdev_disk_io_done,
465789Sahrens 	NULL,
466789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
467789Sahrens 	B_TRUE			/* leaf vdev */
468789Sahrens };
4696423Sgw25295 
4706423Sgw25295 /*
4717147Staylor  * Given the root disk device devid or pathname, read the label from
4727147Staylor  * the device, and construct a configuration nvlist.
4736423Sgw25295  */
4747539SLin.Ling@Sun.COM int
4757539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
4766423Sgw25295 {
4776423Sgw25295 	ldi_handle_t vd_lh;
4786423Sgw25295 	vdev_label_t *label;
4796423Sgw25295 	uint64_t s, size;
4806423Sgw25295 	int l;
4817147Staylor 	ddi_devid_t tmpdevid;
4827687SLin.Ling@Sun.COM 	int error = -1;
4837147Staylor 	char *minor_name;
4846423Sgw25295 
4856423Sgw25295 	/*
4866423Sgw25295 	 * Read the device label and build the nvlist.
4876423Sgw25295 	 */
4887687SLin.Ling@Sun.COM 	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
4897147Staylor 	    &minor_name) == 0) {
4907147Staylor 		error = ldi_open_by_devid(tmpdevid, minor_name,
4918241SJeff.Bonwick@Sun.COM 		    FREAD, kcred, &vd_lh, zfs_li);
4927147Staylor 		ddi_devid_free(tmpdevid);
4937147Staylor 		ddi_devid_str_free(minor_name);
4947147Staylor 	}
4957147Staylor 
4967687SLin.Ling@Sun.COM 	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
4977687SLin.Ling@Sun.COM 	    zfs_li)))
4987539SLin.Ling@Sun.COM 		return (error);
4996423Sgw25295 
5006673Seschrock 	if (ldi_get_size(vd_lh, &s)) {
5016673Seschrock 		(void) ldi_close(vd_lh, FREAD, kcred);
5027539SLin.Ling@Sun.COM 		return (EIO);
5036673Seschrock 	}
5046423Sgw25295 
5056423Sgw25295 	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
5066423Sgw25295 	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
5076423Sgw25295 
5089616SEric.Taylor@Sun.COM 	*config = NULL;
5096423Sgw25295 	for (l = 0; l < VDEV_LABELS; l++) {
5106423Sgw25295 		uint64_t offset, state, txg = 0;
5116423Sgw25295 
5126423Sgw25295 		/* read vdev label */
5136423Sgw25295 		offset = vdev_label_offset(size, l, 0);
5146423Sgw25295 		if (vdev_disk_physio(vd_lh, (caddr_t)label,
5158876SLin.Ling@Sun.COM 		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
5166423Sgw25295 			continue;
5176423Sgw25295 
5186423Sgw25295 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
5197539SLin.Ling@Sun.COM 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
5207539SLin.Ling@Sun.COM 			*config = NULL;
5216423Sgw25295 			continue;
5226423Sgw25295 		}
5236423Sgw25295 
5247539SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
5256423Sgw25295 		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
5267539SLin.Ling@Sun.COM 			nvlist_free(*config);
5277539SLin.Ling@Sun.COM 			*config = NULL;
5286423Sgw25295 			continue;
5296423Sgw25295 		}
5306423Sgw25295 
5317539SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
5326423Sgw25295 		    &txg) != 0 || txg == 0) {
5337539SLin.Ling@Sun.COM 			nvlist_free(*config);
5347539SLin.Ling@Sun.COM 			*config = NULL;
5356423Sgw25295 			continue;
5366423Sgw25295 		}
5376423Sgw25295 
5386423Sgw25295 		break;
5396423Sgw25295 	}
5406423Sgw25295 
5416423Sgw25295 	kmem_free(label, sizeof (vdev_label_t));
5426673Seschrock 	(void) ldi_close(vd_lh, FREAD, kcred);
5439616SEric.Taylor@Sun.COM 	if (*config == NULL)
5449616SEric.Taylor@Sun.COM 		error = EIDRM;
5456673Seschrock 
5467539SLin.Ling@Sun.COM 	return (error);
5476423Sgw25295 }
548