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