xref: /titanic_50/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 2032ea7bd13069ba4884066ed18f83bf12b3e247)
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 2007 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 	dev_t dev;
54 	char *physpath, *minorname;
55 	int otyp;
56 
57 	/*
58 	 * We must have a pathname, and it must be absolute.
59 	 */
60 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
61 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
62 		return (EINVAL);
63 	}
64 
65 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
66 
67 	/*
68 	 * When opening a disk device, we want to preserve the user's original
69 	 * intent.  We always want to open the device by the path the user gave
70 	 * us, even if it is one of multiple paths to the save device.  But we
71 	 * also want to be able to survive disks being removed/recabled.
72 	 * Therefore the sequence of opening devices is:
73 	 *
74 	 * 1. Try opening the device by path.  For legacy pools without the
75 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
76 	 *
77 	 * 2. If the devid of the device matches the stored value, return
78 	 *    success.
79 	 *
80 	 * 3. Otherwise, the device may have moved.  Try opening the device
81 	 *    by the devid instead.
82 	 *
83 	 */
84 	if (vd->vdev_devid != NULL) {
85 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
86 		    &dvd->vd_minor) != 0) {
87 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
88 			return (EINVAL);
89 		}
90 	}
91 
92 	error = EINVAL;		/* presume failure */
93 
94 	if (vd->vdev_path != NULL) {
95 		ddi_devid_t devid;
96 
97 		if (vd->vdev_wholedisk == -1ULL) {
98 			size_t len = strlen(vd->vdev_path) + 3;
99 			char *buf = kmem_alloc(len, KM_SLEEP);
100 			ldi_handle_t lh;
101 
102 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
103 
104 			if (ldi_open_by_name(buf, spa_mode, kcred,
105 			    &lh, zfs_li) == 0) {
106 				spa_strfree(vd->vdev_path);
107 				vd->vdev_path = buf;
108 				vd->vdev_wholedisk = 1ULL;
109 				(void) ldi_close(lh, spa_mode, kcred);
110 			} else {
111 				kmem_free(buf, len);
112 			}
113 		}
114 
115 		error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
116 		    &dvd->vd_lh, zfs_li);
117 
118 		/*
119 		 * Compare the devid to the stored value.
120 		 */
121 		if (error == 0 && vd->vdev_devid != NULL &&
122 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
123 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
124 				error = EINVAL;
125 				(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
126 				dvd->vd_lh = NULL;
127 			}
128 			ddi_devid_free(devid);
129 		}
130 
131 		/*
132 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
133 		 * is not yet set, then this must be a slice.
134 		 */
135 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
136 			vd->vdev_wholedisk = 0;
137 	}
138 
139 	/*
140 	 * If we were unable to open by path, or the devid check fails, open by
141 	 * devid instead.
142 	 */
143 	if (error != 0 && vd->vdev_devid != NULL)
144 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
145 		    spa_mode, kcred, &dvd->vd_lh, zfs_li);
146 
147 	/*
148 	 * If all else fails, then try opening by physical path (if available)
149 	 * or the logical path (if we failed due to the devid check).  While not
150 	 * as reliable as the devid, this will give us something, and the higher
151 	 * level vdev validation will prevent us from opening the wrong device.
152 	 */
153 	if (error) {
154 		if (vd->vdev_physpath != NULL &&
155 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV)
156 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode,
157 			    kcred, &dvd->vd_lh, zfs_li);
158 
159 		/*
160 		 * Note that we don't support the legacy auto-wholedisk support
161 		 * as above.  This hasn't been used in a very long time and we
162 		 * don't need to propagate its oddities to this edge condition.
163 		 */
164 		if (error && vd->vdev_path != NULL)
165 			error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
166 			    &dvd->vd_lh, zfs_li);
167 	}
168 
169 	if (error) {
170 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
171 		return (error);
172 	}
173 
174 	/*
175 	 * Once a device is opened, verify that the physical device path (if
176 	 * available) is up to date.
177 	 */
178 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
179 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
180 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
181 		minorname = NULL;
182 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
183 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
184 		    (vd->vdev_physpath == NULL ||
185 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
186 			if (vd->vdev_physpath)
187 				spa_strfree(vd->vdev_physpath);
188 			(void) strlcat(physpath, ":", MAXPATHLEN);
189 			(void) strlcat(physpath, minorname, MAXPATHLEN);
190 			vd->vdev_physpath = spa_strdup(physpath);
191 		}
192 		if (minorname)
193 			kmem_free(minorname, strlen(minorname) + 1);
194 		kmem_free(physpath, MAXPATHLEN);
195 	}
196 
197 	/*
198 	 * Determine the actual size of the device.
199 	 */
200 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
201 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
202 		return (EINVAL);
203 	}
204 
205 	/*
206 	 * If we own the whole disk, try to enable disk write caching.
207 	 * We ignore errors because it's OK if we can't do it.
208 	 */
209 	if (vd->vdev_wholedisk == 1) {
210 		int wce = 1;
211 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
212 		    FKIOCTL, kcred, NULL);
213 	}
214 
215 	/*
216 	 * Determine the device's minimum transfer size.
217 	 * If the ioctl isn't supported, assume DEV_BSIZE.
218 	 */
219 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
220 	    FKIOCTL, kcred, NULL) != 0)
221 		dkm.dki_lbsize = DEV_BSIZE;
222 
223 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
224 
225 	/*
226 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
227 	 * try again.
228 	 */
229 	vd->vdev_nowritecache = B_FALSE;
230 
231 	return (0);
232 }
233 
234 static void
235 vdev_disk_close(vdev_t *vd)
236 {
237 	vdev_disk_t *dvd = vd->vdev_tsd;
238 
239 	if (dvd == NULL)
240 		return;
241 
242 	if (dvd->vd_minor != NULL)
243 		ddi_devid_str_free(dvd->vd_minor);
244 
245 	if (dvd->vd_devid != NULL)
246 		ddi_devid_free(dvd->vd_devid);
247 
248 	if (dvd->vd_lh != NULL)
249 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
250 
251 	kmem_free(dvd, sizeof (vdev_disk_t));
252 	vd->vdev_tsd = NULL;
253 }
254 
255 static void
256 vdev_disk_io_intr(buf_t *bp)
257 {
258 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
259 	zio_t *zio = vdb->vdb_io;
260 
261 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
262 		zio->io_error = EIO;
263 
264 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
265 
266 	zio_next_stage_async(zio);
267 }
268 
269 static void
270 vdev_disk_ioctl_done(void *zio_arg, int error)
271 {
272 	zio_t *zio = zio_arg;
273 
274 	zio->io_error = error;
275 
276 	zio_next_stage_async(zio);
277 }
278 
279 static void
280 vdev_disk_io_start(zio_t *zio)
281 {
282 	vdev_t *vd = zio->io_vd;
283 	vdev_disk_t *dvd = vd->vdev_tsd;
284 	vdev_disk_buf_t *vdb;
285 	buf_t *bp;
286 	int flags, error;
287 
288 	if (zio->io_type == ZIO_TYPE_IOCTL) {
289 		zio_vdev_io_bypass(zio);
290 
291 		/* XXPOLICY */
292 		if (vdev_is_dead(vd)) {
293 			zio->io_error = ENXIO;
294 			zio_next_stage_async(zio);
295 			return;
296 		}
297 
298 		switch (zio->io_cmd) {
299 
300 		case DKIOCFLUSHWRITECACHE:
301 
302 			if (zfs_nocacheflush)
303 				break;
304 
305 			if (vd->vdev_nowritecache) {
306 				zio->io_error = ENOTSUP;
307 				break;
308 			}
309 
310 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
311 			zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE;
312 			zio->io_dk_callback.dkc_cookie = zio;
313 
314 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
315 			    (uintptr_t)&zio->io_dk_callback,
316 			    FKIOCTL, kcred, NULL);
317 
318 			if (error == 0) {
319 				/*
320 				 * The ioctl will be done asychronously,
321 				 * and will call vdev_disk_ioctl_done()
322 				 * upon completion.
323 				 */
324 				return;
325 			} else if (error == ENOTSUP || error == ENOTTY) {
326 				/*
327 				 * If we get ENOTSUP or ENOTTY, we know that
328 				 * no future attempts will ever succeed.
329 				 * In this case we set a persistent bit so
330 				 * that we don't bother with the ioctl in the
331 				 * future.
332 				 */
333 				vd->vdev_nowritecache = B_TRUE;
334 			}
335 			zio->io_error = error;
336 
337 			break;
338 
339 		default:
340 			zio->io_error = ENOTSUP;
341 		}
342 
343 		zio_next_stage_async(zio);
344 		return;
345 	}
346 
347 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
348 		return;
349 
350 	if ((zio = vdev_queue_io(zio)) == NULL)
351 		return;
352 
353 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
354 	flags |= B_BUSY | B_NOCACHE;
355 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
356 		flags |= B_FAILFAST;
357 
358 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
359 
360 	vdb->vdb_io = zio;
361 	bp = &vdb->vdb_buf;
362 
363 	bioinit(bp);
364 	bp->b_flags = flags;
365 	bp->b_bcount = zio->io_size;
366 	bp->b_un.b_addr = zio->io_data;
367 	bp->b_lblkno = lbtodb(zio->io_offset);
368 	bp->b_bufsize = zio->io_size;
369 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
370 
371 	/* XXPOLICY */
372 	error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio);
373 	if (error) {
374 		zio->io_error = error;
375 		bioerror(bp, error);
376 		bp->b_resid = bp->b_bcount;
377 		bp->b_iodone(bp);
378 		return;
379 	}
380 
381 	error = ldi_strategy(dvd->vd_lh, bp);
382 	/* ldi_strategy() will return non-zero only on programming errors */
383 	ASSERT(error == 0);
384 }
385 
386 static void
387 vdev_disk_io_done(zio_t *zio)
388 {
389 	vdev_t *vd = zio->io_vd;
390 	vdev_disk_t *dvd = vd->vdev_tsd;
391 	int state;
392 
393 	vdev_queue_io_done(zio);
394 
395 	if (zio->io_type == ZIO_TYPE_WRITE)
396 		vdev_cache_write(zio);
397 
398 	if (zio_injection_enabled && zio->io_error == 0)
399 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
400 
401 	/*
402 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
403 	 * the device has been removed.  If this is the case, then we trigger an
404 	 * asynchronous removal of the device.
405 	 */
406 	if (zio->io_error == EIO) {
407 		state = DKIO_NONE;
408 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
409 		    FKIOCTL, kcred, NULL) == 0 &&
410 		    state != DKIO_INSERTED) {
411 			vd->vdev_remove_wanted = B_TRUE;
412 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
413 		}
414 	}
415 
416 	zio_next_stage(zio);
417 }
418 
419 vdev_ops_t vdev_disk_ops = {
420 	vdev_disk_open,
421 	vdev_disk_close,
422 	vdev_default_asize,
423 	vdev_disk_io_start,
424 	vdev_disk_io_done,
425 	NULL,
426 	VDEV_TYPE_DISK,		/* name of this vdev type */
427 	B_TRUE			/* leaf vdev */
428 };
429