xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 11958:575ffe1e978d)
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 /*
22*11958SGeorge.Wilson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
27*11958SGeorge.Wilson@Sun.COM #include <sys/spa_impl.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 
47*11958SGeorge.Wilson@Sun.COM static void
48*11958SGeorge.Wilson@Sun.COM vdev_disk_hold(vdev_t *vd)
49*11958SGeorge.Wilson@Sun.COM {
50*11958SGeorge.Wilson@Sun.COM 	ddi_devid_t devid;
51*11958SGeorge.Wilson@Sun.COM 	char *minor;
52*11958SGeorge.Wilson@Sun.COM 
53*11958SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
54*11958SGeorge.Wilson@Sun.COM 
55*11958SGeorge.Wilson@Sun.COM 	/*
56*11958SGeorge.Wilson@Sun.COM 	 * We must have a pathname, and it must be absolute.
57*11958SGeorge.Wilson@Sun.COM 	 */
58*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
59*11958SGeorge.Wilson@Sun.COM 		return;
60*11958SGeorge.Wilson@Sun.COM 
61*11958SGeorge.Wilson@Sun.COM 	/*
62*11958SGeorge.Wilson@Sun.COM 	 * Only prefetch path and devid info if the device has
63*11958SGeorge.Wilson@Sun.COM 	 * never been opened.
64*11958SGeorge.Wilson@Sun.COM 	 */
65*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_tsd != NULL)
66*11958SGeorge.Wilson@Sun.COM 		return;
67*11958SGeorge.Wilson@Sun.COM 
68*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_wholedisk == -1ULL) {
69*11958SGeorge.Wilson@Sun.COM 		size_t len = strlen(vd->vdev_path) + 3;
70*11958SGeorge.Wilson@Sun.COM 		char *buf = kmem_alloc(len, KM_SLEEP);
71*11958SGeorge.Wilson@Sun.COM 
72*11958SGeorge.Wilson@Sun.COM 		(void) snprintf(buf, len, "%ss0", vd->vdev_path);
73*11958SGeorge.Wilson@Sun.COM 
74*11958SGeorge.Wilson@Sun.COM 		(void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
75*11958SGeorge.Wilson@Sun.COM 		kmem_free(buf, len);
76*11958SGeorge.Wilson@Sun.COM 	}
77*11958SGeorge.Wilson@Sun.COM 
78*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_name_vp == NULL)
79*11958SGeorge.Wilson@Sun.COM 		(void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
80*11958SGeorge.Wilson@Sun.COM 
81*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_devid != NULL &&
82*11958SGeorge.Wilson@Sun.COM 	    ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
83*11958SGeorge.Wilson@Sun.COM 		(void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
84*11958SGeorge.Wilson@Sun.COM 		ddi_devid_str_free(minor);
85*11958SGeorge.Wilson@Sun.COM 		ddi_devid_free(devid);
86*11958SGeorge.Wilson@Sun.COM 	}
87*11958SGeorge.Wilson@Sun.COM }
88*11958SGeorge.Wilson@Sun.COM 
89*11958SGeorge.Wilson@Sun.COM static void
90*11958SGeorge.Wilson@Sun.COM vdev_disk_rele(vdev_t *vd)
91*11958SGeorge.Wilson@Sun.COM {
92*11958SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
93*11958SGeorge.Wilson@Sun.COM 
94*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_name_vp) {
95*11958SGeorge.Wilson@Sun.COM 		VN_RELE_ASYNC(vd->vdev_name_vp,
96*11958SGeorge.Wilson@Sun.COM 		    dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
97*11958SGeorge.Wilson@Sun.COM 		vd->vdev_name_vp = NULL;
98*11958SGeorge.Wilson@Sun.COM 	}
99*11958SGeorge.Wilson@Sun.COM 	if (vd->vdev_devid_vp) {
100*11958SGeorge.Wilson@Sun.COM 		VN_RELE_ASYNC(vd->vdev_devid_vp,
101*11958SGeorge.Wilson@Sun.COM 		    dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
102*11958SGeorge.Wilson@Sun.COM 		vd->vdev_devid_vp = NULL;
103*11958SGeorge.Wilson@Sun.COM 	}
104*11958SGeorge.Wilson@Sun.COM }
105*11958SGeorge.Wilson@Sun.COM 
106789Sahrens static int
1077754SJeff.Bonwick@Sun.COM vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
108789Sahrens {
1098241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
110789Sahrens 	vdev_disk_t *dvd;
1117754SJeff.Bonwick@Sun.COM 	struct dk_minfo dkm;
1127754SJeff.Bonwick@Sun.COM 	int error;
1135329Sgw25295 	dev_t dev;
1147754SJeff.Bonwick@Sun.COM 	int otyp;
115789Sahrens 
116789Sahrens 	/*
117789Sahrens 	 * We must have a pathname, and it must be absolute.
118789Sahrens 	 */
119789Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
120789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
121789Sahrens 		return (EINVAL);
122789Sahrens 	}
123789Sahrens 
12410850SGeorge.Wilson@Sun.COM 	/*
12510850SGeorge.Wilson@Sun.COM 	 * Reopen the device if it's not currently open. Otherwise,
12610850SGeorge.Wilson@Sun.COM 	 * just update the physical size of the device.
12710850SGeorge.Wilson@Sun.COM 	 */
12810850SGeorge.Wilson@Sun.COM 	if (vd->vdev_tsd != NULL) {
12910850SGeorge.Wilson@Sun.COM 		ASSERT(vd->vdev_reopening);
13010850SGeorge.Wilson@Sun.COM 		dvd = vd->vdev_tsd;
13110850SGeorge.Wilson@Sun.COM 		goto skip_open;
13210850SGeorge.Wilson@Sun.COM 	}
13310850SGeorge.Wilson@Sun.COM 
134789Sahrens 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
135789Sahrens 
136789Sahrens 	/*
137789Sahrens 	 * When opening a disk device, we want to preserve the user's original
138789Sahrens 	 * intent.  We always want to open the device by the path the user gave
139789Sahrens 	 * us, even if it is one of multiple paths to the save device.  But we
140789Sahrens 	 * also want to be able to survive disks being removed/recabled.
141789Sahrens 	 * Therefore the sequence of opening devices is:
142789Sahrens 	 *
1431171Seschrock 	 * 1. Try opening the device by path.  For legacy pools without the
1441171Seschrock 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
145789Sahrens 	 *
146789Sahrens 	 * 2. If the devid of the device matches the stored value, return
147789Sahrens 	 *    success.
148789Sahrens 	 *
149789Sahrens 	 * 3. Otherwise, the device may have moved.  Try opening the device
150789Sahrens 	 *    by the devid instead.
151789Sahrens 	 */
152789Sahrens 	if (vd->vdev_devid != NULL) {
153789Sahrens 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
154789Sahrens 		    &dvd->vd_minor) != 0) {
155789Sahrens 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
156789Sahrens 			return (EINVAL);
157789Sahrens 		}
158789Sahrens 	}
159789Sahrens 
160789Sahrens 	error = EINVAL;		/* presume failure */
161789Sahrens 
16210850SGeorge.Wilson@Sun.COM 	if (vd->vdev_path != NULL) {
163789Sahrens 		ddi_devid_t devid;
164789Sahrens 
1651171Seschrock 		if (vd->vdev_wholedisk == -1ULL) {
1661171Seschrock 			size_t len = strlen(vd->vdev_path) + 3;
1671171Seschrock 			char *buf = kmem_alloc(len, KM_SLEEP);
1681171Seschrock 			ldi_handle_t lh;
1691171Seschrock 
1701171Seschrock 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
171789Sahrens 
1728241SJeff.Bonwick@Sun.COM 			if (ldi_open_by_name(buf, spa_mode(spa), kcred,
1731171Seschrock 			    &lh, zfs_li) == 0) {
1741171Seschrock 				spa_strfree(vd->vdev_path);
1751171Seschrock 				vd->vdev_path = buf;
1761171Seschrock 				vd->vdev_wholedisk = 1ULL;
1778241SJeff.Bonwick@Sun.COM 				(void) ldi_close(lh, spa_mode(spa), kcred);
1781171Seschrock 			} else {
1791171Seschrock 				kmem_free(buf, len);
1801171Seschrock 			}
1811171Seschrock 		}
182789Sahrens 
1838241SJeff.Bonwick@Sun.COM 		error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
1841171Seschrock 		    &dvd->vd_lh, zfs_li);
185789Sahrens 
186789Sahrens 		/*
187789Sahrens 		 * Compare the devid to the stored value.
188789Sahrens 		 */
189789Sahrens 		if (error == 0 && vd->vdev_devid != NULL &&
190789Sahrens 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
191789Sahrens 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
192789Sahrens 				error = EINVAL;
1938241SJeff.Bonwick@Sun.COM 				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
1948241SJeff.Bonwick@Sun.COM 				    kcred);
195789Sahrens 				dvd->vd_lh = NULL;
196789Sahrens 			}
197789Sahrens 			ddi_devid_free(devid);
198789Sahrens 		}
1991171Seschrock 
2001171Seschrock 		/*
2011171Seschrock 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
2021171Seschrock 		 * is not yet set, then this must be a slice.
2031171Seschrock 		 */
2041171Seschrock 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
2051171Seschrock 			vd->vdev_wholedisk = 0;
206789Sahrens 	}
207789Sahrens 
208789Sahrens 	/*
209789Sahrens 	 * If we were unable to open by path, or the devid check fails, open by
210789Sahrens 	 * devid instead.
211789Sahrens 	 */
212789Sahrens 	if (error != 0 && vd->vdev_devid != NULL)
213789Sahrens 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
2148241SJeff.Bonwick@Sun.COM 		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
215789Sahrens 
2164451Seschrock 	/*
2174451Seschrock 	 * If all else fails, then try opening by physical path (if available)
2184451Seschrock 	 * or the logical path (if we failed due to the devid check).  While not
2194451Seschrock 	 * as reliable as the devid, this will give us something, and the higher
2204451Seschrock 	 * level vdev validation will prevent us from opening the wrong device.
2214451Seschrock 	 */
2224451Seschrock 	if (error) {
2234451Seschrock 		if (vd->vdev_physpath != NULL &&
2248269SMark.Musante@Sun.COM 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
2258241SJeff.Bonwick@Sun.COM 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
2264451Seschrock 			    kcred, &dvd->vd_lh, zfs_li);
2274451Seschrock 
2284451Seschrock 		/*
2294451Seschrock 		 * Note that we don't support the legacy auto-wholedisk support
2304451Seschrock 		 * as above.  This hasn't been used in a very long time and we
2314451Seschrock 		 * don't need to propagate its oddities to this edge condition.
2324451Seschrock 		 */
23310850SGeorge.Wilson@Sun.COM 		if (error && vd->vdev_path != NULL)
2348241SJeff.Bonwick@Sun.COM 			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
2358241SJeff.Bonwick@Sun.COM 			    kcred, &dvd->vd_lh, zfs_li);
2364451Seschrock 	}
2374451Seschrock 
2387754SJeff.Bonwick@Sun.COM 	if (error) {
239789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
2407754SJeff.Bonwick@Sun.COM 		return (error);
2417754SJeff.Bonwick@Sun.COM 	}
2425329Sgw25295 
243789Sahrens 	/*
2444451Seschrock 	 * Once a device is opened, verify that the physical device path (if
2454451Seschrock 	 * available) is up to date.
2464451Seschrock 	 */
2474451Seschrock 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
2484451Seschrock 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
2495329Sgw25295 		char *physpath, *minorname;
2505329Sgw25295 
2514451Seschrock 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2524451Seschrock 		minorname = NULL;
2534451Seschrock 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
2544451Seschrock 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
2554451Seschrock 		    (vd->vdev_physpath == NULL ||
2564451Seschrock 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
2574451Seschrock 			if (vd->vdev_physpath)
2584451Seschrock 				spa_strfree(vd->vdev_physpath);
2594451Seschrock 			(void) strlcat(physpath, ":", MAXPATHLEN);
2604451Seschrock 			(void) strlcat(physpath, minorname, MAXPATHLEN);
2614451Seschrock 			vd->vdev_physpath = spa_strdup(physpath);
2624451Seschrock 		}
2634451Seschrock 		if (minorname)
2644451Seschrock 			kmem_free(minorname, strlen(minorname) + 1);
2654451Seschrock 		kmem_free(physpath, MAXPATHLEN);
2664451Seschrock 	}
2674451Seschrock 
26810850SGeorge.Wilson@Sun.COM skip_open:
2694451Seschrock 	/*
270789Sahrens 	 * Determine the actual size of the device.
271789Sahrens 	 */
272789Sahrens 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
273789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
274789Sahrens 		return (EINVAL);
275789Sahrens 	}
276789Sahrens 
2771732Sbonwick 	/*
2781732Sbonwick 	 * If we own the whole disk, try to enable disk write caching.
2791732Sbonwick 	 * We ignore errors because it's OK if we can't do it.
2801732Sbonwick 	 */
2811489Swebaker 	if (vd->vdev_wholedisk == 1) {
2821732Sbonwick 		int wce = 1;
2831732Sbonwick 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
2841732Sbonwick 		    FKIOCTL, kcred, NULL);
2851732Sbonwick 	}
2861489Swebaker 
2871732Sbonwick 	/*
2881732Sbonwick 	 * Determine the device's minimum transfer size.
2891732Sbonwick 	 * If the ioctl isn't supported, assume DEV_BSIZE.
2901732Sbonwick 	 */
2911732Sbonwick 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
2921732Sbonwick 	    FKIOCTL, kcred, NULL) != 0)
2931732Sbonwick 		dkm.dki_lbsize = DEV_BSIZE;
2941489Swebaker 
2951732Sbonwick 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
2961489Swebaker 
2971773Seschrock 	/*
2981773Seschrock 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
2991773Seschrock 	 * try again.
3001773Seschrock 	 */
3011773Seschrock 	vd->vdev_nowritecache = B_FALSE;
3021773Seschrock 
303789Sahrens 	return (0);
304789Sahrens }
305789Sahrens 
306789Sahrens static void
307789Sahrens vdev_disk_close(vdev_t *vd)
308789Sahrens {
309789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
310789Sahrens 
31110850SGeorge.Wilson@Sun.COM 	if (vd->vdev_reopening || dvd == NULL)
312789Sahrens 		return;
313789Sahrens 
314789Sahrens 	if (dvd->vd_minor != NULL)
315789Sahrens 		ddi_devid_str_free(dvd->vd_minor);
316789Sahrens 
317789Sahrens 	if (dvd->vd_devid != NULL)
318789Sahrens 		ddi_devid_free(dvd->vd_devid);
319789Sahrens 
320789Sahrens 	if (dvd->vd_lh != NULL)
3218241SJeff.Bonwick@Sun.COM 		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
322789Sahrens 
323789Sahrens 	kmem_free(dvd, sizeof (vdev_disk_t));
324789Sahrens 	vd->vdev_tsd = NULL;
325789Sahrens }
326789Sahrens 
3276423Sgw25295 int
3286423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
3296423Sgw25295     uint64_t offset, int flags)
3306423Sgw25295 {
3316423Sgw25295 	buf_t *bp;
3326423Sgw25295 	int error = 0;
3336423Sgw25295 
3346423Sgw25295 	if (vd_lh == NULL)
3356423Sgw25295 		return (EINVAL);
3366423Sgw25295 
3376423Sgw25295 	ASSERT(flags & B_READ || flags & B_WRITE);
3386423Sgw25295 
3396423Sgw25295 	bp = getrbuf(KM_SLEEP);
3406423Sgw25295 	bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
3416423Sgw25295 	bp->b_bcount = size;
3426423Sgw25295 	bp->b_un.b_addr = (void *)data;
3436423Sgw25295 	bp->b_lblkno = lbtodb(offset);
3446423Sgw25295 	bp->b_bufsize = size;
3456423Sgw25295 
3466423Sgw25295 	error = ldi_strategy(vd_lh, bp);
3476423Sgw25295 	ASSERT(error == 0);
3486423Sgw25295 	if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
3496423Sgw25295 		error = EIO;
3506423Sgw25295 	freerbuf(bp);
3516423Sgw25295 
3526423Sgw25295 	return (error);
3536423Sgw25295 }
3546423Sgw25295 
355789Sahrens static void
356789Sahrens vdev_disk_io_intr(buf_t *bp)
357789Sahrens {
358789Sahrens 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
359789Sahrens 	zio_t *zio = vdb->vdb_io;
360789Sahrens 
3616976Seschrock 	/*
3626976Seschrock 	 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
3636976Seschrock 	 * Rather than teach the rest of the stack about other error
3646976Seschrock 	 * possibilities (EFAULT, etc), we normalize the error value here.
3656976Seschrock 	 */
3666976Seschrock 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
3676976Seschrock 
3686976Seschrock 	if (zio->io_error == 0 && bp->b_resid != 0)
369789Sahrens 		zio->io_error = EIO;
370789Sahrens 
371789Sahrens 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
372789Sahrens 
3735530Sbonwick 	zio_interrupt(zio);
374789Sahrens }
375789Sahrens 
376789Sahrens static void
3777762SJeff.Bonwick@Sun.COM vdev_disk_ioctl_free(zio_t *zio)
3787762SJeff.Bonwick@Sun.COM {
3797762SJeff.Bonwick@Sun.COM 	kmem_free(zio->io_vsd, sizeof (struct dk_callback));
3807762SJeff.Bonwick@Sun.COM }
3817762SJeff.Bonwick@Sun.COM 
38210614SJonathan.Adams@Sun.COM static const zio_vsd_ops_t vdev_disk_vsd_ops = {
38310614SJonathan.Adams@Sun.COM 	vdev_disk_ioctl_free,
38410614SJonathan.Adams@Sun.COM 	zio_vsd_default_cksum_report
38510614SJonathan.Adams@Sun.COM };
38610614SJonathan.Adams@Sun.COM 
3877762SJeff.Bonwick@Sun.COM static void
388789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
389789Sahrens {
390789Sahrens 	zio_t *zio = zio_arg;
391789Sahrens 
392789Sahrens 	zio->io_error = error;
393789Sahrens 
3945530Sbonwick 	zio_interrupt(zio);
395789Sahrens }
396789Sahrens 
3975530Sbonwick static int
398789Sahrens vdev_disk_io_start(zio_t *zio)
399789Sahrens {
400789Sahrens 	vdev_t *vd = zio->io_vd;
401789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
402789Sahrens 	vdev_disk_buf_t *vdb;
4037754SJeff.Bonwick@Sun.COM 	struct dk_callback *dkc;
404789Sahrens 	buf_t *bp;
4057754SJeff.Bonwick@Sun.COM 	int error;
406789Sahrens 
407789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
408789Sahrens 		/* XXPOLICY */
4095329Sgw25295 		if (!vdev_readable(vd)) {
410789Sahrens 			zio->io_error = ENXIO;
4115530Sbonwick 			return (ZIO_PIPELINE_CONTINUE);
412789Sahrens 		}
413789Sahrens 
414789Sahrens 		switch (zio->io_cmd) {
415789Sahrens 
416789Sahrens 		case DKIOCFLUSHWRITECACHE:
417789Sahrens 
4182885Sahrens 			if (zfs_nocacheflush)
4192885Sahrens 				break;
4202885Sahrens 
4211773Seschrock 			if (vd->vdev_nowritecache) {
4221773Seschrock 				zio->io_error = ENOTSUP;
4231773Seschrock 				break;
4241773Seschrock 			}
4251773Seschrock 
4267754SJeff.Bonwick@Sun.COM 			zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
42710614SJonathan.Adams@Sun.COM 			zio->io_vsd_ops = &vdev_disk_vsd_ops;
4287754SJeff.Bonwick@Sun.COM 
4297754SJeff.Bonwick@Sun.COM 			dkc->dkc_callback = vdev_disk_ioctl_done;
4307754SJeff.Bonwick@Sun.COM 			dkc->dkc_flag = FLUSH_VOLATILE;
4317754SJeff.Bonwick@Sun.COM 			dkc->dkc_cookie = zio;
432789Sahrens 
433789Sahrens 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
4347754SJeff.Bonwick@Sun.COM 			    (uintptr_t)dkc, FKIOCTL, kcred, NULL);
435789Sahrens 
436789Sahrens 			if (error == 0) {
437789Sahrens 				/*
438789Sahrens 				 * The ioctl will be done asychronously,
439789Sahrens 				 * and will call vdev_disk_ioctl_done()
440789Sahrens 				 * upon completion.
441789Sahrens 				 */
4425530Sbonwick 				return (ZIO_PIPELINE_STOP);
4435530Sbonwick 			}
4445530Sbonwick 
4455530Sbonwick 			if (error == ENOTSUP || error == ENOTTY) {
4461773Seschrock 				/*
4474455Smishra 				 * If we get ENOTSUP or ENOTTY, we know that
4484455Smishra 				 * no future attempts will ever succeed.
4494455Smishra 				 * In this case we set a persistent bit so
4504455Smishra 				 * that we don't bother with the ioctl in the
4514455Smishra 				 * future.
4521773Seschrock 				 */
4531773Seschrock 				vd->vdev_nowritecache = B_TRUE;
454789Sahrens 			}
455789Sahrens 			zio->io_error = error;
4561773Seschrock 
457789Sahrens 			break;
458789Sahrens 
459789Sahrens 		default:
460789Sahrens 			zio->io_error = ENOTSUP;
461789Sahrens 		}
462789Sahrens 
4635530Sbonwick 		return (ZIO_PIPELINE_CONTINUE);
464789Sahrens 	}
465789Sahrens 
466789Sahrens 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
467789Sahrens 
468789Sahrens 	vdb->vdb_io = zio;
469789Sahrens 	bp = &vdb->vdb_buf;
470789Sahrens 
471789Sahrens 	bioinit(bp);
4727754SJeff.Bonwick@Sun.COM 	bp->b_flags = B_BUSY | B_NOCACHE |
4739725SEric.Schrock@Sun.COM 	    (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
4749725SEric.Schrock@Sun.COM 	if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
4759725SEric.Schrock@Sun.COM 		bp->b_flags |= B_FAILFAST;
476789Sahrens 	bp->b_bcount = zio->io_size;
477789Sahrens 	bp->b_un.b_addr = zio->io_data;
478789Sahrens 	bp->b_lblkno = lbtodb(zio->io_offset);
479789Sahrens 	bp->b_bufsize = zio->io_size;
480789Sahrens 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
481789Sahrens 
482789Sahrens 	/* ldi_strategy() will return non-zero only on programming errors */
4837754SJeff.Bonwick@Sun.COM 	VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
4845530Sbonwick 
4855530Sbonwick 	return (ZIO_PIPELINE_STOP);
486789Sahrens }
487789Sahrens 
4887754SJeff.Bonwick@Sun.COM static void
489789Sahrens vdev_disk_io_done(zio_t *zio)
490789Sahrens {
4917754SJeff.Bonwick@Sun.COM 	vdev_t *vd = zio->io_vd;
4921544Seschrock 
4934451Seschrock 	/*
4944451Seschrock 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
4954451Seschrock 	 * the device has been removed.  If this is the case, then we trigger an
4965329Sgw25295 	 * asynchronous removal of the device. Otherwise, probe the device and
4975369Sgw25295 	 * make sure it's still accessible.
4984451Seschrock 	 */
49910575SEric.Schrock@Sun.COM 	if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
5005329Sgw25295 		vdev_disk_t *dvd = vd->vdev_tsd;
5017754SJeff.Bonwick@Sun.COM 		int state = DKIO_NONE;
5025329Sgw25295 
5037754SJeff.Bonwick@Sun.COM 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
5047754SJeff.Bonwick@Sun.COM 		    FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
50510575SEric.Schrock@Sun.COM 			/*
50610575SEric.Schrock@Sun.COM 			 * We post the resource as soon as possible, instead of
50710575SEric.Schrock@Sun.COM 			 * when the async removal actually happens, because the
50810575SEric.Schrock@Sun.COM 			 * DE is using this information to discard previous I/O
50910575SEric.Schrock@Sun.COM 			 * errors.
51010575SEric.Schrock@Sun.COM 			 */
51110575SEric.Schrock@Sun.COM 			zfs_post_remove(zio->io_spa, vd);
5124451Seschrock 			vd->vdev_remove_wanted = B_TRUE;
5134451Seschrock 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
5144451Seschrock 		}
5154451Seschrock 	}
516789Sahrens }
517789Sahrens 
518789Sahrens vdev_ops_t vdev_disk_ops = {
519789Sahrens 	vdev_disk_open,
520789Sahrens 	vdev_disk_close,
521789Sahrens 	vdev_default_asize,
522789Sahrens 	vdev_disk_io_start,
523789Sahrens 	vdev_disk_io_done,
524789Sahrens 	NULL,
525*11958SGeorge.Wilson@Sun.COM 	vdev_disk_hold,
526*11958SGeorge.Wilson@Sun.COM 	vdev_disk_rele,
527789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
528789Sahrens 	B_TRUE			/* leaf vdev */
529789Sahrens };
5306423Sgw25295 
5316423Sgw25295 /*
5327147Staylor  * Given the root disk device devid or pathname, read the label from
5337147Staylor  * the device, and construct a configuration nvlist.
5346423Sgw25295  */
5357539SLin.Ling@Sun.COM int
5367539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
5376423Sgw25295 {
5386423Sgw25295 	ldi_handle_t vd_lh;
5396423Sgw25295 	vdev_label_t *label;
5406423Sgw25295 	uint64_t s, size;
5416423Sgw25295 	int l;
5427147Staylor 	ddi_devid_t tmpdevid;
5437687SLin.Ling@Sun.COM 	int error = -1;
5447147Staylor 	char *minor_name;
5456423Sgw25295 
5466423Sgw25295 	/*
5476423Sgw25295 	 * Read the device label and build the nvlist.
5486423Sgw25295 	 */
5497687SLin.Ling@Sun.COM 	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
5507147Staylor 	    &minor_name) == 0) {
5517147Staylor 		error = ldi_open_by_devid(tmpdevid, minor_name,
5528241SJeff.Bonwick@Sun.COM 		    FREAD, kcred, &vd_lh, zfs_li);
5537147Staylor 		ddi_devid_free(tmpdevid);
5547147Staylor 		ddi_devid_str_free(minor_name);
5557147Staylor 	}
5567147Staylor 
5577687SLin.Ling@Sun.COM 	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
5587687SLin.Ling@Sun.COM 	    zfs_li)))
5597539SLin.Ling@Sun.COM 		return (error);
5606423Sgw25295 
5616673Seschrock 	if (ldi_get_size(vd_lh, &s)) {
5626673Seschrock 		(void) ldi_close(vd_lh, FREAD, kcred);
5637539SLin.Ling@Sun.COM 		return (EIO);
5646673Seschrock 	}
5656423Sgw25295 
5666423Sgw25295 	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
5676423Sgw25295 	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
5686423Sgw25295 
5699616SEric.Taylor@Sun.COM 	*config = NULL;
5706423Sgw25295 	for (l = 0; l < VDEV_LABELS; l++) {
5716423Sgw25295 		uint64_t offset, state, txg = 0;
5726423Sgw25295 
5736423Sgw25295 		/* read vdev label */
5746423Sgw25295 		offset = vdev_label_offset(size, l, 0);
5756423Sgw25295 		if (vdev_disk_physio(vd_lh, (caddr_t)label,
5768876SLin.Ling@Sun.COM 		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
5776423Sgw25295 			continue;
5786423Sgw25295 
5796423Sgw25295 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
5807539SLin.Ling@Sun.COM 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
5817539SLin.Ling@Sun.COM 			*config = NULL;
5826423Sgw25295 			continue;
5836423Sgw25295 		}
5846423Sgw25295 
5857539SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
5866423Sgw25295 		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
5877539SLin.Ling@Sun.COM 			nvlist_free(*config);
5887539SLin.Ling@Sun.COM 			*config = NULL;
5896423Sgw25295 			continue;
5906423Sgw25295 		}
5916423Sgw25295 
5927539SLin.Ling@Sun.COM 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
5936423Sgw25295 		    &txg) != 0 || txg == 0) {
5947539SLin.Ling@Sun.COM 			nvlist_free(*config);
5957539SLin.Ling@Sun.COM 			*config = NULL;
5966423Sgw25295 			continue;
5976423Sgw25295 		}
5986423Sgw25295 
5996423Sgw25295 		break;
6006423Sgw25295 	}
6016423Sgw25295 
6026423Sgw25295 	kmem_free(label, sizeof (vdev_label_t));
6036673Seschrock 	(void) ldi_close(vd_lh, FREAD, kcred);
6049616SEric.Taylor@Sun.COM 	if (*config == NULL)
6059616SEric.Taylor@Sun.COM 		error = EIDRM;
6066673Seschrock 
6077539SLin.Ling@Sun.COM 	return (error);
6086423Sgw25295 }
609