xref: /titanic_50/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 3c4993fb5a74112f361d71dab20997bdc749a7fb)
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_common(vdev_t *vd)
49 {
50 	vdev_disk_t *dvd;
51 	dev_t dev;
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 	/*
145 	 * If all else fails, then try opening by physical path (if available)
146 	 * or the logical path (if we failed due to the devid check).  While not
147 	 * as reliable as the devid, this will give us something, and the higher
148 	 * level vdev validation will prevent us from opening the wrong device.
149 	 */
150 	if (error) {
151 		if (vd->vdev_physpath != NULL &&
152 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV)
153 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode,
154 			    kcred, &dvd->vd_lh, zfs_li);
155 
156 		/*
157 		 * Note that we don't support the legacy auto-wholedisk support
158 		 * as above.  This hasn't been used in a very long time and we
159 		 * don't need to propagate its oddities to this edge condition.
160 		 */
161 		if (error && vd->vdev_path != NULL)
162 			error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
163 			    &dvd->vd_lh, zfs_li);
164 	}
165 
166 	if (error)
167 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
168 
169 	return (error);
170 }
171 
172 static int
173 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
174 {
175 	vdev_disk_t *dvd;
176 	struct dk_minfo dkm;
177 	int error;
178 	dev_t dev;
179 	int otyp;
180 
181 	error = vdev_disk_open_common(vd);
182 	if (error)
183 		return (error);
184 
185 	dvd = vd->vdev_tsd;
186 	/*
187 	 * Once a device is opened, verify that the physical device path (if
188 	 * available) is up to date.
189 	 */
190 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
191 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
192 		char *physpath, *minorname;
193 
194 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
195 		minorname = NULL;
196 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
197 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
198 		    (vd->vdev_physpath == NULL ||
199 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
200 			if (vd->vdev_physpath)
201 				spa_strfree(vd->vdev_physpath);
202 			(void) strlcat(physpath, ":", MAXPATHLEN);
203 			(void) strlcat(physpath, minorname, MAXPATHLEN);
204 			vd->vdev_physpath = spa_strdup(physpath);
205 		}
206 		if (minorname)
207 			kmem_free(minorname, strlen(minorname) + 1);
208 		kmem_free(physpath, MAXPATHLEN);
209 	}
210 
211 	/*
212 	 * Determine the actual size of the device.
213 	 */
214 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
215 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
216 		return (EINVAL);
217 	}
218 
219 	/*
220 	 * If we own the whole disk, try to enable disk write caching.
221 	 * We ignore errors because it's OK if we can't do it.
222 	 */
223 	if (vd->vdev_wholedisk == 1) {
224 		int wce = 1;
225 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
226 		    FKIOCTL, kcred, NULL);
227 	}
228 
229 	/*
230 	 * Determine the device's minimum transfer size.
231 	 * If the ioctl isn't supported, assume DEV_BSIZE.
232 	 */
233 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
234 	    FKIOCTL, kcred, NULL) != 0)
235 		dkm.dki_lbsize = DEV_BSIZE;
236 
237 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
238 
239 	/*
240 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
241 	 * try again.
242 	 */
243 	vd->vdev_nowritecache = B_FALSE;
244 
245 	return (0);
246 }
247 
248 static void
249 vdev_disk_close(vdev_t *vd)
250 {
251 	vdev_disk_t *dvd = vd->vdev_tsd;
252 
253 	if (dvd == NULL)
254 		return;
255 
256 	if (dvd->vd_minor != NULL)
257 		ddi_devid_str_free(dvd->vd_minor);
258 
259 	if (dvd->vd_devid != NULL)
260 		ddi_devid_free(dvd->vd_devid);
261 
262 	if (dvd->vd_lh != NULL)
263 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
264 
265 	kmem_free(dvd, sizeof (vdev_disk_t));
266 	vd->vdev_tsd = NULL;
267 }
268 
269 static int
270 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset,
271     int flags)
272 {
273 	buf_t buf;
274 	int error = 0;
275 	vdev_disk_t *dvd = vd->vdev_tsd;
276 
277 	if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL)
278 		return (EINVAL);
279 
280 	ASSERT(flags & B_READ || flags & B_WRITE);
281 
282 	bioinit(&buf);
283 	buf.b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
284 	buf.b_bcount = size;
285 	buf.b_un.b_addr = (void *)data;
286 	buf.b_lblkno = lbtodb(offset);
287 	buf.b_bufsize = size;
288 
289 	error = ldi_strategy(dvd->vd_lh, &buf);
290 	ASSERT(error == 0);
291 	error = biowait(&buf);
292 
293 	if (zio_injection_enabled && error == 0)
294 		error = zio_handle_device_injection(vd, EIO);
295 
296 	return (error);
297 }
298 
299 static int
300 vdev_disk_probe(vdev_t *vd)
301 {
302 	uint64_t offset;
303 	vdev_t *nvd;
304 	int l, error = 0, retries = 0;
305 	char *vl_pad;
306 
307 	if (vd == NULL)
308 		return (EINVAL);
309 
310 	/* Hijack the current vdev */
311 	nvd = vd;
312 
313 	/*
314 	 * Pick a random label to rewrite.
315 	 */
316 	l = spa_get_random(VDEV_LABELS);
317 	ASSERT(l < VDEV_LABELS);
318 
319 	offset = vdev_label_offset(vd->vdev_psize, l,
320 	    offsetof(vdev_label_t, vl_pad));
321 
322 	vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP);
323 
324 	/*
325 	 * Try to read and write to a special location on the
326 	 * label. We use the existing vdev initially and only
327 	 * try to create and reopen it if we encounter a failure.
328 	 */
329 	while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
330 	    offset, B_READ)) != 0 && retries == 0) {
331 
332 		nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
333 		if (vd->vdev_path)
334 			nvd->vdev_path = spa_strdup(vd->vdev_path);
335 		if (vd->vdev_physpath)
336 			nvd->vdev_physpath = spa_strdup(vd->vdev_physpath);
337 		if (vd->vdev_devid)
338 			nvd->vdev_devid = spa_strdup(vd->vdev_devid);
339 		nvd->vdev_wholedisk = vd->vdev_wholedisk;
340 		nvd->vdev_guid = vd->vdev_guid;
341 		retries++;
342 
343 		error = vdev_disk_open_common(nvd);
344 		if (error) {
345 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
346 			    nvd->vdev_stat.vs_aux);
347 			break;
348 		}
349 	}
350 
351 	if (!error) {
352 		error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
353 		    offset, B_WRITE);
354 	}
355 
356 	/* Clean up if we allocated a new vdev */
357 	if (retries) {
358 		vdev_disk_close(nvd);
359 		if (nvd->vdev_path)
360 			spa_strfree(nvd->vdev_path);
361 		if (nvd->vdev_physpath)
362 			spa_strfree(nvd->vdev_physpath);
363 		if (nvd->vdev_devid)
364 			spa_strfree(nvd->vdev_devid);
365 		kmem_free(nvd, sizeof (vdev_t));
366 	}
367 	kmem_free(vl_pad, VDEV_SKIP_SIZE);
368 
369 	/* Reset the failing flag */
370 	if (!error)
371 		vd->vdev_is_failing = B_FALSE;
372 
373 	return (error);
374 }
375 
376 static void
377 vdev_disk_io_intr(buf_t *bp)
378 {
379 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
380 	zio_t *zio = vdb->vdb_io;
381 
382 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
383 		zio->io_error = EIO;
384 
385 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
386 
387 	zio_next_stage_async(zio);
388 }
389 
390 static void
391 vdev_disk_ioctl_done(void *zio_arg, int error)
392 {
393 	zio_t *zio = zio_arg;
394 
395 	zio->io_error = error;
396 
397 	zio_next_stage_async(zio);
398 }
399 
400 static void
401 vdev_disk_io_start(zio_t *zio)
402 {
403 	vdev_t *vd = zio->io_vd;
404 	vdev_disk_t *dvd = vd->vdev_tsd;
405 	vdev_disk_buf_t *vdb;
406 	buf_t *bp;
407 	int flags, error;
408 
409 	if (zio->io_type == ZIO_TYPE_IOCTL) {
410 		zio_vdev_io_bypass(zio);
411 
412 		/* XXPOLICY */
413 		if (!vdev_readable(vd)) {
414 			zio->io_error = ENXIO;
415 			zio_next_stage_async(zio);
416 			return;
417 		}
418 
419 		switch (zio->io_cmd) {
420 
421 		case DKIOCFLUSHWRITECACHE:
422 
423 			if (zfs_nocacheflush)
424 				break;
425 
426 			if (vd->vdev_nowritecache) {
427 				zio->io_error = ENOTSUP;
428 				break;
429 			}
430 
431 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
432 			zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE;
433 			zio->io_dk_callback.dkc_cookie = zio;
434 
435 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
436 			    (uintptr_t)&zio->io_dk_callback,
437 			    FKIOCTL, kcred, NULL);
438 
439 			if (error == 0) {
440 				/*
441 				 * The ioctl will be done asychronously,
442 				 * and will call vdev_disk_ioctl_done()
443 				 * upon completion.
444 				 */
445 				return;
446 			} else if (error == ENOTSUP || error == ENOTTY) {
447 				/*
448 				 * If we get ENOTSUP or ENOTTY, we know that
449 				 * no future attempts will ever succeed.
450 				 * In this case we set a persistent bit so
451 				 * that we don't bother with the ioctl in the
452 				 * future.
453 				 */
454 				vd->vdev_nowritecache = B_TRUE;
455 			}
456 			zio->io_error = error;
457 
458 			break;
459 
460 		default:
461 			zio->io_error = ENOTSUP;
462 		}
463 
464 		zio_next_stage_async(zio);
465 		return;
466 	}
467 
468 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
469 		return;
470 
471 	if ((zio = vdev_queue_io(zio)) == NULL)
472 		return;
473 
474 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
475 	flags |= B_BUSY | B_NOCACHE;
476 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
477 		flags |= B_FAILFAST;
478 
479 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
480 
481 	vdb->vdb_io = zio;
482 	bp = &vdb->vdb_buf;
483 
484 	bioinit(bp);
485 	bp->b_flags = flags;
486 	bp->b_bcount = zio->io_size;
487 	bp->b_un.b_addr = zio->io_data;
488 	bp->b_lblkno = lbtodb(zio->io_offset);
489 	bp->b_bufsize = zio->io_size;
490 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
491 
492 	/* XXPOLICY */
493 	if (zio->io_type == ZIO_TYPE_WRITE)
494 		error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
495 	else
496 		error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
497 	error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error;
498 	if (error) {
499 		zio->io_error = error;
500 		bioerror(bp, error);
501 		bp->b_resid = bp->b_bcount;
502 		bp->b_iodone(bp);
503 		return;
504 	}
505 
506 	error = ldi_strategy(dvd->vd_lh, bp);
507 	/* ldi_strategy() will return non-zero only on programming errors */
508 	ASSERT(error == 0);
509 }
510 
511 static void
512 vdev_disk_io_done(zio_t *zio)
513 {
514 	vdev_queue_io_done(zio);
515 
516 	if (zio->io_type == ZIO_TYPE_WRITE)
517 		vdev_cache_write(zio);
518 
519 	if (zio_injection_enabled && zio->io_error == 0)
520 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
521 
522 	/*
523 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
524 	 * the device has been removed.  If this is the case, then we trigger an
525 	 * asynchronous removal of the device. Otherwise, probe the device and
526 	 * make sure it's still functional.
527 	 */
528 	if (zio->io_error == EIO) {
529 		vdev_t *vd = zio->io_vd;
530 		vdev_disk_t *dvd = vd->vdev_tsd;
531 		int state;
532 
533 		state = DKIO_NONE;
534 		if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
535 		    FKIOCTL, kcred, NULL) == 0 &&
536 		    state != DKIO_INSERTED) {
537 			vd->vdev_remove_wanted = B_TRUE;
538 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
539 		} else if (vdev_probe(vd) != 0) {
540 			ASSERT(vd->vdev_ops->vdev_op_leaf);
541 			vd->vdev_is_failing = B_TRUE;
542 		}
543 	}
544 
545 	zio_next_stage(zio);
546 }
547 
548 vdev_ops_t vdev_disk_ops = {
549 	vdev_disk_open,
550 	vdev_disk_close,
551 	vdev_disk_probe,
552 	vdev_default_asize,
553 	vdev_disk_io_start,
554 	vdev_disk_io_done,
555 	NULL,
556 	VDEV_TYPE_DISK,		/* name of this vdev type */
557 	B_TRUE			/* leaf vdev */
558 };
559