xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 80cb75f4)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.h>
28 #include <sys/refcount.h>
29 #include <sys/vdev_disk.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/fs/zfs.h>
32 #include <sys/zio.h>
33 #include <sys/sunldi.h>
34 #include <sys/fm/fs/zfs.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 void
48 vdev_disk_hold(vdev_t *vd)
49 {
50 	ddi_devid_t devid;
51 	char *minor;
52 
53 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
54 
55 	/*
56 	 * We must have a pathname, and it must be absolute.
57 	 */
58 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
59 		return;
60 
61 	/*
62 	 * Only prefetch path and devid info if the device has
63 	 * never been opened.
64 	 */
65 	if (vd->vdev_tsd != NULL)
66 		return;
67 
68 	if (vd->vdev_wholedisk == -1ULL) {
69 		size_t len = strlen(vd->vdev_path) + 3;
70 		char *buf = kmem_alloc(len, KM_SLEEP);
71 
72 		(void) snprintf(buf, len, "%ss0", vd->vdev_path);
73 
74 		(void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
75 		kmem_free(buf, len);
76 	}
77 
78 	if (vd->vdev_name_vp == NULL)
79 		(void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
80 
81 	if (vd->vdev_devid != NULL &&
82 	    ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
83 		(void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
84 		ddi_devid_str_free(minor);
85 		ddi_devid_free(devid);
86 	}
87 }
88 
89 static void
90 vdev_disk_rele(vdev_t *vd)
91 {
92 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
93 
94 	if (vd->vdev_name_vp) {
95 		VN_RELE_ASYNC(vd->vdev_name_vp,
96 		    dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
97 		vd->vdev_name_vp = NULL;
98 	}
99 	if (vd->vdev_devid_vp) {
100 		VN_RELE_ASYNC(vd->vdev_devid_vp,
101 		    dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
102 		vd->vdev_devid_vp = NULL;
103 	}
104 }
105 
106 static int
107 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
108 {
109 	spa_t *spa = vd->vdev_spa;
110 	vdev_disk_t *dvd;
111 	struct dk_minfo dkm;
112 	int error;
113 	dev_t dev;
114 	int otyp;
115 
116 	/*
117 	 * We must have a pathname, and it must be absolute.
118 	 */
119 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
120 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
121 		return (EINVAL);
122 	}
123 
124 	/*
125 	 * Reopen the device if it's not currently open. Otherwise,
126 	 * just update the physical size of the device.
127 	 */
128 	if (vd->vdev_tsd != NULL) {
129 		ASSERT(vd->vdev_reopening);
130 		dvd = vd->vdev_tsd;
131 		goto skip_open;
132 	}
133 
134 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
135 
136 	/*
137 	 * When opening a disk device, we want to preserve the user's original
138 	 * intent.  We always want to open the device by the path the user gave
139 	 * us, even if it is one of multiple paths to the save device.  But we
140 	 * also want to be able to survive disks being removed/recabled.
141 	 * Therefore the sequence of opening devices is:
142 	 *
143 	 * 1. Try opening the device by path.  For legacy pools without the
144 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
145 	 *
146 	 * 2. If the devid of the device matches the stored value, return
147 	 *    success.
148 	 *
149 	 * 3. Otherwise, the device may have moved.  Try opening the device
150 	 *    by the devid instead.
151 	 */
152 	if (vd->vdev_devid != NULL) {
153 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
154 		    &dvd->vd_minor) != 0) {
155 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
156 			return (EINVAL);
157 		}
158 	}
159 
160 	error = EINVAL;		/* presume failure */
161 
162 	if (vd->vdev_path != NULL) {
163 		ddi_devid_t devid;
164 
165 		if (vd->vdev_wholedisk == -1ULL) {
166 			size_t len = strlen(vd->vdev_path) + 3;
167 			char *buf = kmem_alloc(len, KM_SLEEP);
168 			ldi_handle_t lh;
169 
170 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
171 
172 			if (ldi_open_by_name(buf, spa_mode(spa), kcred,
173 			    &lh, zfs_li) == 0) {
174 				spa_strfree(vd->vdev_path);
175 				vd->vdev_path = buf;
176 				vd->vdev_wholedisk = 1ULL;
177 				(void) ldi_close(lh, spa_mode(spa), kcred);
178 			} else {
179 				kmem_free(buf, len);
180 			}
181 		}
182 
183 		error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
184 		    &dvd->vd_lh, zfs_li);
185 
186 		/*
187 		 * Compare the devid to the stored value.
188 		 */
189 		if (error == 0 && vd->vdev_devid != NULL &&
190 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
191 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
192 				error = EINVAL;
193 				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
194 				    kcred);
195 				dvd->vd_lh = NULL;
196 			}
197 			ddi_devid_free(devid);
198 		}
199 
200 		/*
201 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
202 		 * is not yet set, then this must be a slice.
203 		 */
204 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
205 			vd->vdev_wholedisk = 0;
206 	}
207 
208 	/*
209 	 * If we were unable to open by path, or the devid check fails, open by
210 	 * devid instead.
211 	 */
212 	if (error != 0 && vd->vdev_devid != NULL)
213 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
214 		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
215 
216 	/*
217 	 * If all else fails, then try opening by physical path (if available)
218 	 * or the logical path (if we failed due to the devid check).  While not
219 	 * as reliable as the devid, this will give us something, and the higher
220 	 * level vdev validation will prevent us from opening the wrong device.
221 	 */
222 	if (error) {
223 		if (vd->vdev_physpath != NULL &&
224 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
225 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
226 			    kcred, &dvd->vd_lh, zfs_li);
227 
228 		/*
229 		 * Note that we don't support the legacy auto-wholedisk support
230 		 * as above.  This hasn't been used in a very long time and we
231 		 * don't need to propagate its oddities to this edge condition.
232 		 */
233 		if (error && vd->vdev_path != NULL)
234 			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
235 			    kcred, &dvd->vd_lh, zfs_li);
236 	}
237 
238 	if (error) {
239 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
240 		return (error);
241 	}
242 
243 	/*
244 	 * Once a device is opened, verify that the physical device path (if
245 	 * available) is up to date.
246 	 */
247 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
248 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
249 		char *physpath, *minorname;
250 
251 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
252 		minorname = NULL;
253 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
254 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
255 		    (vd->vdev_physpath == NULL ||
256 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
257 			if (vd->vdev_physpath)
258 				spa_strfree(vd->vdev_physpath);
259 			(void) strlcat(physpath, ":", MAXPATHLEN);
260 			(void) strlcat(physpath, minorname, MAXPATHLEN);
261 			vd->vdev_physpath = spa_strdup(physpath);
262 		}
263 		if (minorname)
264 			kmem_free(minorname, strlen(minorname) + 1);
265 		kmem_free(physpath, MAXPATHLEN);
266 	}
267 
268 skip_open:
269 	/*
270 	 * Determine the actual size of the device.
271 	 */
272 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
273 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
274 		return (EINVAL);
275 	}
276 
277 	/*
278 	 * If we own the whole disk, try to enable disk write caching.
279 	 * We ignore errors because it's OK if we can't do it.
280 	 */
281 	if (vd->vdev_wholedisk == 1) {
282 		int wce = 1;
283 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
284 		    FKIOCTL, kcred, NULL);
285 	}
286 
287 	/*
288 	 * Determine the device's minimum transfer size.
289 	 * If the ioctl isn't supported, assume DEV_BSIZE.
290 	 */
291 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
292 	    FKIOCTL, kcred, NULL) != 0)
293 		dkm.dki_lbsize = DEV_BSIZE;
294 
295 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
296 
297 	/*
298 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
299 	 * try again.
300 	 */
301 	vd->vdev_nowritecache = B_FALSE;
302 
303 	return (0);
304 }
305 
306 static void
307 vdev_disk_close(vdev_t *vd)
308 {
309 	vdev_disk_t *dvd = vd->vdev_tsd;
310 
311 	if (vd->vdev_reopening || dvd == NULL)
312 		return;
313 
314 	if (dvd->vd_minor != NULL)
315 		ddi_devid_str_free(dvd->vd_minor);
316 
317 	if (dvd->vd_devid != NULL)
318 		ddi_devid_free(dvd->vd_devid);
319 
320 	if (dvd->vd_lh != NULL)
321 		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
322 
323 	kmem_free(dvd, sizeof (vdev_disk_t));
324 	vd->vdev_tsd = NULL;
325 }
326 
327 int
328 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
329     uint64_t offset, int flags)
330 {
331 	buf_t *bp;
332 	int error = 0;
333 
334 	if (vd_lh == NULL)
335 		return (EINVAL);
336 
337 	ASSERT(flags & B_READ || flags & B_WRITE);
338 
339 	bp = getrbuf(KM_SLEEP);
340 	bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
341 	bp->b_bcount = size;
342 	bp->b_un.b_addr = (void *)data;
343 	bp->b_lblkno = lbtodb(offset);
344 	bp->b_bufsize = size;
345 
346 	error = ldi_strategy(vd_lh, bp);
347 	ASSERT(error == 0);
348 	if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
349 		error = EIO;
350 	freerbuf(bp);
351 
352 	return (error);
353 }
354 
355 static void
356 vdev_disk_io_intr(buf_t *bp)
357 {
358 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
359 	zio_t *zio = vdb->vdb_io;
360 
361 	/*
362 	 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
363 	 * Rather than teach the rest of the stack about other error
364 	 * possibilities (EFAULT, etc), we normalize the error value here.
365 	 */
366 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
367 
368 	if (zio->io_error == 0 && bp->b_resid != 0)
369 		zio->io_error = EIO;
370 
371 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
372 
373 	zio_interrupt(zio);
374 }
375 
376 static void
377 vdev_disk_ioctl_free(zio_t *zio)
378 {
379 	kmem_free(zio->io_vsd, sizeof (struct dk_callback));
380 }
381 
382 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
383 	vdev_disk_ioctl_free,
384 	zio_vsd_default_cksum_report
385 };
386 
387 static void
388 vdev_disk_ioctl_done(void *zio_arg, int error)
389 {
390 	zio_t *zio = zio_arg;
391 
392 	zio->io_error = error;
393 
394 	zio_interrupt(zio);
395 }
396 
397 static int
398 vdev_disk_io_start(zio_t *zio)
399 {
400 	vdev_t *vd = zio->io_vd;
401 	vdev_disk_t *dvd = vd->vdev_tsd;
402 	vdev_disk_buf_t *vdb;
403 	struct dk_callback *dkc;
404 	buf_t *bp;
405 	int error;
406 
407 	if (zio->io_type == ZIO_TYPE_IOCTL) {
408 		/* XXPOLICY */
409 		if (!vdev_readable(vd)) {
410 			zio->io_error = ENXIO;
411 			return (ZIO_PIPELINE_CONTINUE);
412 		}
413 
414 		switch (zio->io_cmd) {
415 
416 		case DKIOCFLUSHWRITECACHE:
417 
418 			if (zfs_nocacheflush)
419 				break;
420 
421 			if (vd->vdev_nowritecache) {
422 				zio->io_error = ENOTSUP;
423 				break;
424 			}
425 
426 			zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
427 			zio->io_vsd_ops = &vdev_disk_vsd_ops;
428 
429 			dkc->dkc_callback = vdev_disk_ioctl_done;
430 			dkc->dkc_flag = FLUSH_VOLATILE;
431 			dkc->dkc_cookie = zio;
432 
433 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
434 			    (uintptr_t)dkc, FKIOCTL, kcred, NULL);
435 
436 			if (error == 0) {
437 				/*
438 				 * The ioctl will be done asychronously,
439 				 * and will call vdev_disk_ioctl_done()
440 				 * upon completion.
441 				 */
442 				return (ZIO_PIPELINE_STOP);
443 			}
444 
445 			if (error == ENOTSUP || error == ENOTTY) {
446 				/*
447 				 * If we get ENOTSUP or ENOTTY, we know that
448 				 * no future attempts will ever succeed.
449 				 * In this case we set a persistent bit so
450 				 * that we don't bother with the ioctl in the
451 				 * future.
452 				 */
453 				vd->vdev_nowritecache = B_TRUE;
454 			}
455 			zio->io_error = error;
456 
457 			break;
458 
459 		default:
460 			zio->io_error = ENOTSUP;
461 		}
462 
463 		return (ZIO_PIPELINE_CONTINUE);
464 	}
465 
466 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
467 
468 	vdb->vdb_io = zio;
469 	bp = &vdb->vdb_buf;
470 
471 	bioinit(bp);
472 	bp->b_flags = B_BUSY | B_NOCACHE |
473 	    (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
474 	if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
475 		bp->b_flags |= B_FAILFAST;
476 	bp->b_bcount = zio->io_size;
477 	bp->b_un.b_addr = zio->io_data;
478 	bp->b_lblkno = lbtodb(zio->io_offset);
479 	bp->b_bufsize = zio->io_size;
480 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
481 
482 	/* ldi_strategy() will return non-zero only on programming errors */
483 	VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
484 
485 	return (ZIO_PIPELINE_STOP);
486 }
487 
488 static void
489 vdev_disk_io_done(zio_t *zio)
490 {
491 	vdev_t *vd = zio->io_vd;
492 
493 	/*
494 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
495 	 * the device has been removed.  If this is the case, then we trigger an
496 	 * asynchronous removal of the device. Otherwise, probe the device and
497 	 * make sure it's still accessible.
498 	 */
499 	if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
500 		vdev_disk_t *dvd = vd->vdev_tsd;
501 		int state = DKIO_NONE;
502 
503 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
504 		    FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
505 			/*
506 			 * We post the resource as soon as possible, instead of
507 			 * when the async removal actually happens, because the
508 			 * DE is using this information to discard previous I/O
509 			 * errors.
510 			 */
511 			zfs_post_remove(zio->io_spa, vd);
512 			vd->vdev_remove_wanted = B_TRUE;
513 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
514 		}
515 	}
516 }
517 
518 vdev_ops_t vdev_disk_ops = {
519 	vdev_disk_open,
520 	vdev_disk_close,
521 	vdev_default_asize,
522 	vdev_disk_io_start,
523 	vdev_disk_io_done,
524 	NULL,
525 	vdev_disk_hold,
526 	vdev_disk_rele,
527 	VDEV_TYPE_DISK,		/* name of this vdev type */
528 	B_TRUE			/* leaf vdev */
529 };
530 
531 /*
532  * Given the root disk device devid or pathname, read the label from
533  * the device, and construct a configuration nvlist.
534  */
535 int
536 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
537 {
538 	ldi_handle_t vd_lh;
539 	vdev_label_t *label;
540 	uint64_t s, size;
541 	int l;
542 	ddi_devid_t tmpdevid;
543 	int error = -1;
544 	char *minor_name;
545 
546 	/*
547 	 * Read the device label and build the nvlist.
548 	 */
549 	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
550 	    &minor_name) == 0) {
551 		error = ldi_open_by_devid(tmpdevid, minor_name,
552 		    FREAD, kcred, &vd_lh, zfs_li);
553 		ddi_devid_free(tmpdevid);
554 		ddi_devid_str_free(minor_name);
555 	}
556 
557 	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
558 	    zfs_li)))
559 		return (error);
560 
561 	if (ldi_get_size(vd_lh, &s)) {
562 		(void) ldi_close(vd_lh, FREAD, kcred);
563 		return (EIO);
564 	}
565 
566 	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
567 	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
568 
569 	*config = NULL;
570 	for (l = 0; l < VDEV_LABELS; l++) {
571 		uint64_t offset, state, txg = 0;
572 
573 		/* read vdev label */
574 		offset = vdev_label_offset(size, l, 0);
575 		if (vdev_disk_physio(vd_lh, (caddr_t)label,
576 		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
577 			continue;
578 
579 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
580 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
581 			*config = NULL;
582 			continue;
583 		}
584 
585 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
586 		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
587 			nvlist_free(*config);
588 			*config = NULL;
589 			continue;
590 		}
591 
592 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
593 		    &txg) != 0 || txg == 0) {
594 			nvlist_free(*config);
595 			*config = NULL;
596 			continue;
597 		}
598 
599 		break;
600 	}
601 
602 	kmem_free(label, sizeof (vdev_label_t));
603 	(void) ldi_close(vd_lh, FREAD, kcred);
604 	if (*config == NULL)
605 		error = EIDRM;
606 
607 	return (error);
608 }
609