xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 1489:fa842259660e)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
5*1489Swebaker  * Common Development and Distribution License (the "License").
6*1489Swebaker  * 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*1489Swebaker  * Copyright 2006 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
48789Sahrens vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
49789Sahrens {
50789Sahrens 	vdev_disk_t *dvd;
51789Sahrens 	int error;
52789Sahrens 
53789Sahrens 	/*
54789Sahrens 	 * We must have a pathname, and it must be absolute.
55789Sahrens 	 */
56789Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
57789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
58789Sahrens 		return (EINVAL);
59789Sahrens 	}
60789Sahrens 
61789Sahrens 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
62789Sahrens 
63789Sahrens 	/*
64789Sahrens 	 * When opening a disk device, we want to preserve the user's original
65789Sahrens 	 * intent.  We always want to open the device by the path the user gave
66789Sahrens 	 * us, even if it is one of multiple paths to the save device.  But we
67789Sahrens 	 * also want to be able to survive disks being removed/recabled.
68789Sahrens 	 * Therefore the sequence of opening devices is:
69789Sahrens 	 *
701171Seschrock 	 * 1. Try opening the device by path.  For legacy pools without the
711171Seschrock 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
72789Sahrens 	 *
73789Sahrens 	 * 2. If the devid of the device matches the stored value, return
74789Sahrens 	 *    success.
75789Sahrens 	 *
76789Sahrens 	 * 3. Otherwise, the device may have moved.  Try opening the device
77789Sahrens 	 *    by the devid instead.
78789Sahrens 	 *
79789Sahrens 	 */
80789Sahrens 	if (vd->vdev_devid != NULL) {
81789Sahrens 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
82789Sahrens 		    &dvd->vd_minor) != 0) {
83789Sahrens 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
84789Sahrens 			return (EINVAL);
85789Sahrens 		}
86789Sahrens 	}
87789Sahrens 
88789Sahrens 	error = EINVAL;		/* presume failure */
89789Sahrens 
90789Sahrens 	if (vd->vdev_path != NULL) {
91789Sahrens 		ddi_devid_t devid;
92789Sahrens 
931171Seschrock 		if (vd->vdev_wholedisk == -1ULL) {
941171Seschrock 			size_t len = strlen(vd->vdev_path) + 3;
951171Seschrock 			char *buf = kmem_alloc(len, KM_SLEEP);
961171Seschrock 			ldi_handle_t lh;
971171Seschrock 
981171Seschrock 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
99789Sahrens 
1001171Seschrock 			if (ldi_open_by_name(buf, spa_mode, kcred,
1011171Seschrock 			    &lh, zfs_li) == 0) {
1021171Seschrock 				spa_strfree(vd->vdev_path);
1031171Seschrock 				vd->vdev_path = buf;
1041171Seschrock 				vd->vdev_wholedisk = 1ULL;
1051171Seschrock 				(void) ldi_close(lh, spa_mode, kcred);
1061171Seschrock 			} else {
1071171Seschrock 				kmem_free(buf, len);
1081171Seschrock 			}
1091171Seschrock 		}
110789Sahrens 
1111171Seschrock 		error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
1121171Seschrock 		    &dvd->vd_lh, zfs_li);
113789Sahrens 
114789Sahrens 		/*
115789Sahrens 		 * Compare the devid to the stored value.
116789Sahrens 		 */
117789Sahrens 		if (error == 0 && vd->vdev_devid != NULL &&
118789Sahrens 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
119789Sahrens 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
120789Sahrens 				error = EINVAL;
121789Sahrens 				(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
122789Sahrens 				dvd->vd_lh = NULL;
123789Sahrens 			}
124789Sahrens 			ddi_devid_free(devid);
125789Sahrens 		}
1261171Seschrock 
1271171Seschrock 		/*
1281171Seschrock 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
1291171Seschrock 		 * is not yet set, then this must be a slice.
1301171Seschrock 		 */
1311171Seschrock 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
1321171Seschrock 			vd->vdev_wholedisk = 0;
133789Sahrens 	}
134789Sahrens 
135789Sahrens 	/*
136789Sahrens 	 * If we were unable to open by path, or the devid check fails, open by
137789Sahrens 	 * devid instead.
138789Sahrens 	 */
139789Sahrens 	if (error != 0 && vd->vdev_devid != NULL)
140789Sahrens 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
141789Sahrens 		    spa_mode, kcred, &dvd->vd_lh, zfs_li);
142789Sahrens 
143789Sahrens 	if (error) {
144789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
145789Sahrens 		return (error);
146789Sahrens 	}
147789Sahrens 
148789Sahrens 	/*
149789Sahrens 	 * Determine the actual size of the device.
150789Sahrens 	 */
151789Sahrens 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
152789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
153789Sahrens 		return (EINVAL);
154789Sahrens 	}
155789Sahrens 
156789Sahrens 	*ashift = SPA_MINBLOCKSHIFT;
157789Sahrens 
158*1489Swebaker 
159*1489Swebaker 	if (vd->vdev_wholedisk == 1) {
160*1489Swebaker 
161*1489Swebaker 		int wce, rc;
162*1489Swebaker 
163*1489Swebaker 		/*
164*1489Swebaker 		 * Enable disk write caching if we own the whole disk.
165*1489Swebaker 		 * Ignore errors as this is a performance optimization,
166*1489Swebaker 		 * we work just fine w/o it.
167*1489Swebaker 		 */
168*1489Swebaker 		error = 0;
169*1489Swebaker 		wce = 1;
170*1489Swebaker 		rc = ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
171*1489Swebaker 			FKIOCTL, kcred, &error);
172*1489Swebaker 
173*1489Swebaker 		if (rc || error)
174*1489Swebaker 			dprintf("%s: DKIOCSETWCE failed %d,%d",
175*1489Swebaker 				vdev_description(vd), rc, error);
176*1489Swebaker 	}
177*1489Swebaker 
178789Sahrens 	return (0);
179789Sahrens }
180789Sahrens 
181789Sahrens static void
182789Sahrens vdev_disk_close(vdev_t *vd)
183789Sahrens {
184789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
185789Sahrens 
186789Sahrens 	if (dvd == NULL)
187789Sahrens 		return;
188789Sahrens 
189789Sahrens 	dprintf("removing disk %s, devid %s\n",
190789Sahrens 	    vd->vdev_path ? vd->vdev_path : "<none>",
191789Sahrens 	    vd->vdev_devid ? vd->vdev_devid : "<none>");
192789Sahrens 
193789Sahrens 	if (dvd->vd_minor != NULL)
194789Sahrens 		ddi_devid_str_free(dvd->vd_minor);
195789Sahrens 
196789Sahrens 	if (dvd->vd_devid != NULL)
197789Sahrens 		ddi_devid_free(dvd->vd_devid);
198789Sahrens 
199789Sahrens 	if (dvd->vd_lh != NULL)
200789Sahrens 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
201789Sahrens 
202789Sahrens 	kmem_free(dvd, sizeof (vdev_disk_t));
203789Sahrens 	vd->vdev_tsd = NULL;
204789Sahrens }
205789Sahrens 
206789Sahrens static void
207789Sahrens vdev_disk_io_intr(buf_t *bp)
208789Sahrens {
209789Sahrens 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
210789Sahrens 	zio_t *zio = vdb->vdb_io;
211789Sahrens 
212789Sahrens 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
213789Sahrens 		zio->io_error = EIO;
214789Sahrens 
215789Sahrens 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
216789Sahrens 
217789Sahrens 	zio_next_stage_async(zio);
218789Sahrens }
219789Sahrens 
220789Sahrens static void
221789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
222789Sahrens {
223789Sahrens 	zio_t *zio = zio_arg;
224789Sahrens 
225789Sahrens 	zio->io_error = error;
226789Sahrens 
227789Sahrens 	zio_next_stage_async(zio);
228789Sahrens }
229789Sahrens 
230789Sahrens static void
231789Sahrens vdev_disk_io_start(zio_t *zio)
232789Sahrens {
233789Sahrens 	vdev_t *vd = zio->io_vd;
234789Sahrens 	vdev_disk_t *dvd = vd->vdev_tsd;
235789Sahrens 	vdev_disk_buf_t *vdb;
236789Sahrens 	buf_t *bp;
237789Sahrens 	int flags, error;
238789Sahrens 
239789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
240789Sahrens 		zio_vdev_io_bypass(zio);
241789Sahrens 
242789Sahrens 		/* XXPOLICY */
243789Sahrens 		if (vdev_is_dead(vd)) {
244789Sahrens 			zio->io_error = ENXIO;
245789Sahrens 			zio_next_stage_async(zio);
246789Sahrens 			return;
247789Sahrens 		}
248789Sahrens 
249789Sahrens 		switch (zio->io_cmd) {
250789Sahrens 
251789Sahrens 		case DKIOCFLUSHWRITECACHE:
252789Sahrens 
253789Sahrens 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
254789Sahrens 			zio->io_dk_callback.dkc_cookie = zio;
255789Sahrens 
256789Sahrens 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
257789Sahrens 			    (uintptr_t)&zio->io_dk_callback,
258789Sahrens 			    FKIOCTL, kcred, NULL);
259789Sahrens 
260789Sahrens 			if (error == 0) {
261789Sahrens 				/*
262789Sahrens 				 * The ioctl will be done asychronously,
263789Sahrens 				 * and will call vdev_disk_ioctl_done()
264789Sahrens 				 * upon completion.
265789Sahrens 				 */
266789Sahrens 				return;
267789Sahrens 			}
268789Sahrens 			zio->io_error = error;
269789Sahrens 			break;
270789Sahrens 
271789Sahrens 		default:
272789Sahrens 			zio->io_error = ENOTSUP;
273789Sahrens 		}
274789Sahrens 
275789Sahrens 		zio_next_stage_async(zio);
276789Sahrens 		return;
277789Sahrens 	}
278789Sahrens 
279789Sahrens 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
280789Sahrens 		return;
281789Sahrens 
282789Sahrens 	if ((zio = vdev_queue_io(zio)) == NULL)
283789Sahrens 		return;
284789Sahrens 
285789Sahrens 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
286789Sahrens 	flags |= B_BUSY | B_NOCACHE;
287789Sahrens 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
288789Sahrens 		flags |= B_FAILFAST;
289789Sahrens 
290789Sahrens 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
291789Sahrens 
292789Sahrens 	vdb->vdb_io = zio;
293789Sahrens 	bp = &vdb->vdb_buf;
294789Sahrens 
295789Sahrens 	bioinit(bp);
296789Sahrens 	bp->b_flags = flags;
297789Sahrens 	bp->b_bcount = zio->io_size;
298789Sahrens 	bp->b_un.b_addr = zio->io_data;
299789Sahrens 	bp->b_lblkno = lbtodb(zio->io_offset);
300789Sahrens 	bp->b_bufsize = zio->io_size;
301789Sahrens 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
302789Sahrens 
303789Sahrens 	/* XXPOLICY */
304789Sahrens 	error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio);
305789Sahrens 	if (error) {
306789Sahrens 		zio->io_error = error;
307789Sahrens 		bioerror(bp, error);
308789Sahrens 		bp->b_resid = bp->b_bcount;
309789Sahrens 		bp->b_iodone(bp);
310789Sahrens 		return;
311789Sahrens 	}
312789Sahrens 
313789Sahrens 	error = ldi_strategy(dvd->vd_lh, bp);
314789Sahrens 	/* ldi_strategy() will return non-zero only on programming errors */
315789Sahrens 	ASSERT(error == 0);
316789Sahrens }
317789Sahrens 
318789Sahrens static void
319789Sahrens vdev_disk_io_done(zio_t *zio)
320789Sahrens {
321789Sahrens 	vdev_queue_io_done(zio);
322789Sahrens 
323789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE)
324789Sahrens 		vdev_cache_write(zio);
325789Sahrens 
326789Sahrens 	zio_next_stage(zio);
327789Sahrens }
328789Sahrens 
329789Sahrens vdev_ops_t vdev_disk_ops = {
330789Sahrens 	vdev_disk_open,
331789Sahrens 	vdev_disk_close,
332789Sahrens 	vdev_default_asize,
333789Sahrens 	vdev_disk_io_start,
334789Sahrens 	vdev_disk_io_done,
335789Sahrens 	NULL,
336789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
337789Sahrens 	B_TRUE			/* leaf vdev */
338789Sahrens };
339