xref: /titanic_52/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 9df258ac654d4990e45ba8bff1661d2d0d3b0d2a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/vdev_disk.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/fs/zfs.h>
33 #include <sys/zio.h>
34 #include <sys/sunldi.h>
35 
36 /*
37  * Virtual device vector for disks.
38  */
39 
40 extern ldi_ident_t zfs_li;
41 
42 typedef struct vdev_disk_buf {
43 	buf_t	vdb_buf;
44 	zio_t	*vdb_io;
45 } vdev_disk_buf_t;
46 
47 static int
48 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
49 {
50 	vdev_disk_t *dvd;
51 	struct dk_minfo dkm;
52 	int error;
53 
54 	/*
55 	 * We must have a pathname, and it must be absolute.
56 	 */
57 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
58 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
59 		return (EINVAL);
60 	}
61 
62 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
63 
64 	/*
65 	 * When opening a disk device, we want to preserve the user's original
66 	 * intent.  We always want to open the device by the path the user gave
67 	 * us, even if it is one of multiple paths to the save device.  But we
68 	 * also want to be able to survive disks being removed/recabled.
69 	 * Therefore the sequence of opening devices is:
70 	 *
71 	 * 1. Try opening the device by path.  For legacy pools without the
72 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
73 	 *
74 	 * 2. If the devid of the device matches the stored value, return
75 	 *    success.
76 	 *
77 	 * 3. Otherwise, the device may have moved.  Try opening the device
78 	 *    by the devid instead.
79 	 *
80 	 */
81 	if (vd->vdev_devid != NULL) {
82 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
83 		    &dvd->vd_minor) != 0) {
84 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
85 			return (EINVAL);
86 		}
87 	}
88 
89 	error = EINVAL;		/* presume failure */
90 
91 	if (vd->vdev_path != NULL) {
92 		ddi_devid_t devid;
93 
94 		if (vd->vdev_wholedisk == -1ULL) {
95 			size_t len = strlen(vd->vdev_path) + 3;
96 			char *buf = kmem_alloc(len, KM_SLEEP);
97 			ldi_handle_t lh;
98 
99 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
100 
101 			if (ldi_open_by_name(buf, spa_mode, kcred,
102 			    &lh, zfs_li) == 0) {
103 				spa_strfree(vd->vdev_path);
104 				vd->vdev_path = buf;
105 				vd->vdev_wholedisk = 1ULL;
106 				(void) ldi_close(lh, spa_mode, kcred);
107 			} else {
108 				kmem_free(buf, len);
109 			}
110 		}
111 
112 		error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
113 		    &dvd->vd_lh, zfs_li);
114 
115 		/*
116 		 * Compare the devid to the stored value.
117 		 */
118 		if (error == 0 && vd->vdev_devid != NULL &&
119 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
120 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
121 				error = EINVAL;
122 				(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
123 				dvd->vd_lh = NULL;
124 			}
125 			ddi_devid_free(devid);
126 		}
127 
128 		/*
129 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
130 		 * is not yet set, then this must be a slice.
131 		 */
132 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
133 			vd->vdev_wholedisk = 0;
134 	}
135 
136 	/*
137 	 * If we were unable to open by path, or the devid check fails, open by
138 	 * devid instead.
139 	 */
140 	if (error != 0 && vd->vdev_devid != NULL)
141 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
142 		    spa_mode, kcred, &dvd->vd_lh, zfs_li);
143 
144 	if (error) {
145 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
146 		return (error);
147 	}
148 
149 	/*
150 	 * Determine the actual size of the device.
151 	 */
152 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
153 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
154 		return (EINVAL);
155 	}
156 
157 	/*
158 	 * If we own the whole disk, try to enable disk write caching.
159 	 * We ignore errors because it's OK if we can't do it.
160 	 */
161 	if (vd->vdev_wholedisk == 1) {
162 		int wce = 1;
163 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
164 		    FKIOCTL, kcred, NULL);
165 	}
166 
167 	/*
168 	 * Determine the device's minimum transfer size.
169 	 * If the ioctl isn't supported, assume DEV_BSIZE.
170 	 */
171 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
172 	    FKIOCTL, kcred, NULL) != 0)
173 		dkm.dki_lbsize = DEV_BSIZE;
174 
175 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
176 
177 	/*
178 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
179 	 * try again.
180 	 */
181 	vd->vdev_nowritecache = B_FALSE;
182 
183 	return (0);
184 }
185 
186 static void
187 vdev_disk_close(vdev_t *vd)
188 {
189 	vdev_disk_t *dvd = vd->vdev_tsd;
190 
191 	if (dvd == NULL)
192 		return;
193 
194 	dprintf("removing disk %s, devid %s\n",
195 	    vd->vdev_path ? vd->vdev_path : "<none>",
196 	    vd->vdev_devid ? vd->vdev_devid : "<none>");
197 
198 	if (dvd->vd_minor != NULL)
199 		ddi_devid_str_free(dvd->vd_minor);
200 
201 	if (dvd->vd_devid != NULL)
202 		ddi_devid_free(dvd->vd_devid);
203 
204 	if (dvd->vd_lh != NULL)
205 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
206 
207 	kmem_free(dvd, sizeof (vdev_disk_t));
208 	vd->vdev_tsd = NULL;
209 }
210 
211 static void
212 vdev_disk_io_intr(buf_t *bp)
213 {
214 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
215 	zio_t *zio = vdb->vdb_io;
216 
217 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
218 		zio->io_error = EIO;
219 
220 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
221 
222 	zio_next_stage_async(zio);
223 }
224 
225 static void
226 vdev_disk_ioctl_done(void *zio_arg, int error)
227 {
228 	zio_t *zio = zio_arg;
229 
230 	zio->io_error = error;
231 
232 	zio_next_stage_async(zio);
233 }
234 
235 static void
236 vdev_disk_io_start(zio_t *zio)
237 {
238 	vdev_t *vd = zio->io_vd;
239 	vdev_disk_t *dvd = vd->vdev_tsd;
240 	vdev_disk_buf_t *vdb;
241 	buf_t *bp;
242 	int flags, error;
243 
244 	if (zio->io_type == ZIO_TYPE_IOCTL) {
245 		zio_vdev_io_bypass(zio);
246 
247 		/* XXPOLICY */
248 		if (vdev_is_dead(vd)) {
249 			zio->io_error = ENXIO;
250 			zio_next_stage_async(zio);
251 			return;
252 		}
253 
254 		switch (zio->io_cmd) {
255 
256 		case DKIOCFLUSHWRITECACHE:
257 
258 			if (vd->vdev_nowritecache) {
259 				zio->io_error = ENOTSUP;
260 				break;
261 			}
262 
263 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
264 			zio->io_dk_callback.dkc_cookie = zio;
265 
266 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
267 			    (uintptr_t)&zio->io_dk_callback,
268 			    FKIOCTL, kcred, NULL);
269 
270 			if (error == 0) {
271 				/*
272 				 * The ioctl will be done asychronously,
273 				 * and will call vdev_disk_ioctl_done()
274 				 * upon completion.
275 				 */
276 				return;
277 			} else if (error == ENOTSUP) {
278 				/*
279 				 * If we get ENOTSUP, we know that no future
280 				 * attempts will ever succeed.  In this case we
281 				 * set a persistent bit so that we don't bother
282 				 * with the ioctl in the future.
283 				 */
284 				vd->vdev_nowritecache = B_TRUE;
285 			}
286 			zio->io_error = error;
287 
288 			break;
289 
290 		default:
291 			zio->io_error = ENOTSUP;
292 		}
293 
294 		zio_next_stage_async(zio);
295 		return;
296 	}
297 
298 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
299 		return;
300 
301 	if ((zio = vdev_queue_io(zio)) == NULL)
302 		return;
303 
304 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
305 	flags |= B_BUSY | B_NOCACHE;
306 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
307 		flags |= B_FAILFAST;
308 
309 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
310 
311 	vdb->vdb_io = zio;
312 	bp = &vdb->vdb_buf;
313 
314 	bioinit(bp);
315 	bp->b_flags = flags;
316 	bp->b_bcount = zio->io_size;
317 	bp->b_un.b_addr = zio->io_data;
318 	bp->b_lblkno = lbtodb(zio->io_offset);
319 	bp->b_bufsize = zio->io_size;
320 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
321 
322 	/* XXPOLICY */
323 	error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio);
324 	if (error) {
325 		zio->io_error = error;
326 		bioerror(bp, error);
327 		bp->b_resid = bp->b_bcount;
328 		bp->b_iodone(bp);
329 		return;
330 	}
331 
332 	error = ldi_strategy(dvd->vd_lh, bp);
333 	/* ldi_strategy() will return non-zero only on programming errors */
334 	ASSERT(error == 0);
335 }
336 
337 static void
338 vdev_disk_io_done(zio_t *zio)
339 {
340 	vdev_queue_io_done(zio);
341 
342 	if (zio->io_type == ZIO_TYPE_WRITE)
343 		vdev_cache_write(zio);
344 
345 	if (zio_injection_enabled && zio->io_error == 0)
346 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
347 
348 	zio_next_stage(zio);
349 }
350 
351 vdev_ops_t vdev_disk_ops = {
352 	vdev_disk_open,
353 	vdev_disk_close,
354 	vdev_default_asize,
355 	vdev_disk_io_start,
356 	vdev_disk_io_done,
357 	NULL,
358 	VDEV_TYPE_DISK,		/* name of this vdev type */
359 	B_TRUE			/* leaf vdev */
360 };
361