xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 6af23589e78469fc9f90db8558854d1a822aaa72)
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  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright 2019 Joyent, Inc.
26  */
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa_impl.h>
30 #include <sys/refcount.h>
31 #include <sys/vdev_disk.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/abd.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/zio.h>
36 #include <sys/sunldi.h>
37 #include <sys/efi_partition.h>
38 #include <sys/fm/fs/zfs.h>
39 
40 /*
41  * Tunable parameter for debugging or performance analysis. Setting this
42  * will cause pool corruption on power loss if a volatile out-of-order
43  * write cache is enabled.
44  */
45 boolean_t zfs_nocacheflush = B_FALSE;
46 
47 /*
48  * Virtual device vector for disks.
49  */
50 
51 extern ldi_ident_t zfs_li;
52 
53 static void vdev_disk_close(vdev_t *);
54 
55 typedef struct vdev_disk_ldi_cb {
56 	list_node_t		lcb_next;
57 	ldi_callback_id_t	lcb_id;
58 } vdev_disk_ldi_cb_t;
59 
60 /*
61  * Bypass the devid when opening a disk vdev.
62  * There have been issues where the devids of several devices were shuffled,
63  * causing pool open failures. Note, that this flag is intended to be used
64  * for pool recovery only.
65  *
66  * Note that if a pool is imported with the devids bypassed, all its vdevs will
67  * cease storing devid information permanently. In practice, the devid is rarely
68  * useful as vdev paths do not tend to change unless the hardware is
69  * reconfigured. That said, if the paths do change and a pool fails to open
70  * automatically at boot, a simple zpool import should re-scan the paths and fix
71  * the issue.
72  */
73 boolean_t vdev_disk_bypass_devid = B_FALSE;
74 
75 static void
76 vdev_disk_alloc(vdev_t *vd)
77 {
78 	vdev_disk_t *dvd;
79 
80 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
81 	/*
82 	 * Create the LDI event callback list.
83 	 */
84 	list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
85 	    offsetof(vdev_disk_ldi_cb_t, lcb_next));
86 }
87 
88 static void
89 vdev_disk_free(vdev_t *vd)
90 {
91 	vdev_disk_t *dvd = vd->vdev_tsd;
92 	vdev_disk_ldi_cb_t *lcb;
93 
94 	if (dvd == NULL)
95 		return;
96 
97 	/*
98 	 * We have already closed the LDI handle. Clean up the LDI event
99 	 * callbacks and free vd->vdev_tsd.
100 	 */
101 	while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
102 		list_remove(&dvd->vd_ldi_cbs, lcb);
103 		(void) ldi_ev_remove_callbacks(lcb->lcb_id);
104 		kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
105 	}
106 	list_destroy(&dvd->vd_ldi_cbs);
107 	kmem_free(dvd, sizeof (vdev_disk_t));
108 	vd->vdev_tsd = NULL;
109 }
110 
111 /* ARGSUSED */
112 static int
113 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
114     void *ev_data)
115 {
116 	vdev_t *vd = (vdev_t *)arg;
117 	vdev_disk_t *dvd = vd->vdev_tsd;
118 
119 	/*
120 	 * Ignore events other than offline.
121 	 */
122 	if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
123 		return (LDI_EV_SUCCESS);
124 
125 	/*
126 	 * All LDI handles must be closed for the state change to succeed, so
127 	 * call on vdev_disk_close() to do this.
128 	 *
129 	 * We inform vdev_disk_close that it is being called from offline
130 	 * notify context so it will defer cleanup of LDI event callbacks and
131 	 * freeing of vd->vdev_tsd to the offline finalize or a reopen.
132 	 */
133 	dvd->vd_ldi_offline = B_TRUE;
134 	vdev_disk_close(vd);
135 
136 	/*
137 	 * Now that the device is closed, request that the spa_async_thread
138 	 * mark the device as REMOVED and notify FMA of the removal.
139 	 */
140 	zfs_post_remove(vd->vdev_spa, vd);
141 	vd->vdev_remove_wanted = B_TRUE;
142 	spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
143 
144 	return (LDI_EV_SUCCESS);
145 }
146 
147 /* ARGSUSED */
148 static void
149 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
150     int ldi_result, void *arg, void *ev_data)
151 {
152 	vdev_t *vd = (vdev_t *)arg;
153 
154 	/*
155 	 * Ignore events other than offline.
156 	 */
157 	if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
158 		return;
159 
160 	/*
161 	 * We have already closed the LDI handle in notify.
162 	 * Clean up the LDI event callbacks and free vd->vdev_tsd.
163 	 */
164 	vdev_disk_free(vd);
165 
166 	/*
167 	 * Request that the vdev be reopened if the offline state change was
168 	 * unsuccessful.
169 	 */
170 	if (ldi_result != LDI_EV_SUCCESS) {
171 		vd->vdev_probe_wanted = B_TRUE;
172 		spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
173 	}
174 }
175 
176 static ldi_ev_callback_t vdev_disk_off_callb = {
177 	.cb_vers = LDI_EV_CB_VERS,
178 	.cb_notify = vdev_disk_off_notify,
179 	.cb_finalize = vdev_disk_off_finalize
180 };
181 
182 /* ARGSUSED */
183 static void
184 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
185     int ldi_result, void *arg, void *ev_data)
186 {
187 	vdev_t *vd = (vdev_t *)arg;
188 
189 	/*
190 	 * Ignore events other than degrade.
191 	 */
192 	if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
193 		return;
194 
195 	/*
196 	 * Degrade events always succeed. Mark the vdev as degraded.
197 	 * This status is purely informative for the user.
198 	 */
199 	(void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
200 }
201 
202 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
203 	.cb_vers = LDI_EV_CB_VERS,
204 	.cb_notify = NULL,
205 	.cb_finalize = vdev_disk_dgrd_finalize
206 };
207 
208 static void
209 vdev_disk_hold(vdev_t *vd)
210 {
211 	ddi_devid_t devid;
212 	char *minor;
213 
214 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
215 
216 	/*
217 	 * We must have a pathname, and it must be absolute.
218 	 */
219 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
220 		return;
221 
222 	/*
223 	 * Only prefetch path and devid info if the device has
224 	 * never been opened.
225 	 */
226 	if (vd->vdev_tsd != NULL)
227 		return;
228 
229 	if (vd->vdev_wholedisk == -1ULL) {
230 		size_t len = strlen(vd->vdev_path) + 3;
231 		char *buf = kmem_alloc(len, KM_SLEEP);
232 
233 		(void) snprintf(buf, len, "%ss0", vd->vdev_path);
234 
235 		(void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
236 		kmem_free(buf, len);
237 	}
238 
239 	if (vd->vdev_name_vp == NULL)
240 		(void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
241 
242 	if (vd->vdev_devid != NULL &&
243 	    ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
244 		(void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
245 		ddi_devid_str_free(minor);
246 		ddi_devid_free(devid);
247 	}
248 }
249 
250 static void
251 vdev_disk_rele(vdev_t *vd)
252 {
253 	ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
254 
255 	if (vd->vdev_name_vp) {
256 		VN_RELE_ASYNC(vd->vdev_name_vp,
257 		    dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
258 		vd->vdev_name_vp = NULL;
259 	}
260 	if (vd->vdev_devid_vp) {
261 		VN_RELE_ASYNC(vd->vdev_devid_vp,
262 		    dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
263 		vd->vdev_devid_vp = NULL;
264 	}
265 }
266 
267 /*
268  * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when
269  * even a fallback to DKIOCGMEDIAINFO fails.
270  */
271 #ifdef DEBUG
272 #define	VDEV_DEBUG(...)	cmn_err(CE_NOTE, __VA_ARGS__)
273 #else
274 #define	VDEV_DEBUG(...)	/* Nothing... */
275 #endif
276 
277 static int
278 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
279     uint64_t *ashift)
280 {
281 	spa_t *spa = vd->vdev_spa;
282 	vdev_disk_t *dvd = vd->vdev_tsd;
283 	ldi_ev_cookie_t ecookie;
284 	vdev_disk_ldi_cb_t *lcb;
285 	union {
286 		struct dk_minfo_ext ude;
287 		struct dk_minfo ud;
288 	} dks;
289 	struct dk_minfo_ext *dkmext = &dks.ude;
290 	struct dk_minfo *dkm = &dks.ud;
291 	int error;
292 	dev_t dev;
293 	int otyp;
294 	boolean_t validate_devid = B_FALSE;
295 	uint64_t capacity = 0, blksz = 0, pbsize;
296 
297 	/*
298 	 * We must have a pathname, and it must be absolute.
299 	 */
300 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
301 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
302 		return (SET_ERROR(EINVAL));
303 	}
304 
305 	/*
306 	 * Reopen the device if it's not currently open. Otherwise,
307 	 * just update the physical size of the device.
308 	 */
309 	if (dvd != NULL) {
310 		if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
311 			/*
312 			 * If we are opening a device in its offline notify
313 			 * context, the LDI handle was just closed. Clean
314 			 * up the LDI event callbacks and free vd->vdev_tsd.
315 			 */
316 			vdev_disk_free(vd);
317 		} else {
318 			ASSERT(vd->vdev_reopening);
319 			goto skip_open;
320 		}
321 	}
322 
323 	/*
324 	 * Create vd->vdev_tsd.
325 	 */
326 	vdev_disk_alloc(vd);
327 	dvd = vd->vdev_tsd;
328 
329 	/*
330 	 * Allow bypassing the devid.
331 	 */
332 	if (vd->vdev_devid != NULL && vdev_disk_bypass_devid) {
333 		vdev_dbgmsg(vd, "vdev_disk_open, devid %s bypassed",
334 		    vd->vdev_devid);
335 		spa_strfree(vd->vdev_devid);
336 		vd->vdev_devid = NULL;
337 	}
338 
339 	/*
340 	 * When opening a disk device, we want to preserve the user's original
341 	 * intent.  We always want to open the device by the path the user gave
342 	 * us, even if it is one of multiple paths to the same device.  But we
343 	 * also want to be able to survive disks being removed/recabled.
344 	 * Therefore the sequence of opening devices is:
345 	 *
346 	 * 1. Try opening the device by path.  For legacy pools without the
347 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
348 	 *
349 	 * 2. If the devid of the device matches the stored value, return
350 	 *    success.
351 	 *
352 	 * 3. Otherwise, the device may have moved.  Try opening the device
353 	 *    by the devid instead.
354 	 */
355 	if (vd->vdev_devid != NULL) {
356 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
357 		    &dvd->vd_minor) != 0) {
358 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
359 			vdev_dbgmsg(vd, "vdev_disk_open: invalid "
360 			    "vdev_devid '%s'", vd->vdev_devid);
361 			return (SET_ERROR(EINVAL));
362 		}
363 	}
364 
365 	error = EINVAL;		/* presume failure */
366 
367 	if (vd->vdev_path != NULL) {
368 
369 		if (vd->vdev_wholedisk == -1ULL) {
370 			size_t len = strlen(vd->vdev_path) + 3;
371 			char *buf = kmem_alloc(len, KM_SLEEP);
372 
373 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
374 
375 			error = ldi_open_by_name(buf, spa_mode(spa), kcred,
376 			    &dvd->vd_lh, zfs_li);
377 			if (error == 0) {
378 				spa_strfree(vd->vdev_path);
379 				vd->vdev_path = buf;
380 				vd->vdev_wholedisk = 1ULL;
381 			} else {
382 				kmem_free(buf, len);
383 			}
384 		}
385 
386 		/*
387 		 * If we have not yet opened the device, try to open it by the
388 		 * specified path.
389 		 */
390 		if (error != 0) {
391 			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
392 			    kcred, &dvd->vd_lh, zfs_li);
393 		}
394 
395 		/*
396 		 * Compare the devid to the stored value.
397 		 */
398 		if (error == 0 && vd->vdev_devid != NULL) {
399 			ddi_devid_t devid = NULL;
400 
401 			if (ldi_get_devid(dvd->vd_lh, &devid) != 0) {
402 				/*
403 				 * We expected a devid on this device but it no
404 				 * longer appears to have one.  The validation
405 				 * step may need to remove it from the
406 				 * configuration.
407 				 */
408 				validate_devid = B_TRUE;
409 
410 			} else if (ddi_devid_compare(devid, dvd->vd_devid) !=
411 			    0) {
412 				/*
413 				 * A mismatch here is unexpected, log it.
414 				 */
415 				char *devid_str = ddi_devid_str_encode(devid,
416 				    dvd->vd_minor);
417 				vdev_dbgmsg(vd, "vdev_disk_open: devid "
418 				    "mismatch: %s != %s", vd->vdev_devid,
419 				    devid_str);
420 				cmn_err(CE_NOTE, "vdev_disk_open %s: devid "
421 				    "mismatch: %s != %s", vd->vdev_path,
422 				    vd->vdev_devid, devid_str);
423 				ddi_devid_str_free(devid_str);
424 
425 				error = SET_ERROR(EINVAL);
426 				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
427 				    kcred);
428 				dvd->vd_lh = NULL;
429 			}
430 
431 			if (devid != NULL) {
432 				ddi_devid_free(devid);
433 			}
434 		}
435 
436 		/*
437 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
438 		 * is not yet set, then this must be a slice.
439 		 */
440 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
441 			vd->vdev_wholedisk = 0;
442 	}
443 
444 	/*
445 	 * If we were unable to open by path, or the devid check fails, open by
446 	 * devid instead.
447 	 */
448 	if (error != 0 && vd->vdev_devid != NULL) {
449 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
450 		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
451 		if (error != 0) {
452 			vdev_dbgmsg(vd, "Failed to open by devid (%s)",
453 			    vd->vdev_devid);
454 		}
455 	}
456 
457 	/*
458 	 * If all else fails, then try opening by physical path (if available)
459 	 * or the logical path (if we failed due to the devid check).  While not
460 	 * as reliable as the devid, this will give us something, and the higher
461 	 * level vdev validation will prevent us from opening the wrong device.
462 	 */
463 	if (error != 0) {
464 		validate_devid = B_TRUE;
465 
466 		if (vd->vdev_physpath != NULL &&
467 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV) {
468 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
469 			    kcred, &dvd->vd_lh, zfs_li);
470 		}
471 
472 		/*
473 		 * Note that we don't support the legacy auto-wholedisk support
474 		 * as above.  This hasn't been used in a very long time and we
475 		 * don't need to propagate its oddities to this edge condition.
476 		 */
477 		if (error != 0 && vd->vdev_path != NULL) {
478 			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
479 			    kcred, &dvd->vd_lh, zfs_li);
480 		}
481 	}
482 
483 	if (error != 0) {
484 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
485 		vdev_dbgmsg(vd, "vdev_disk_open: failed to open [error=%d]",
486 		    error);
487 		return (error);
488 	}
489 
490 	/*
491 	 * Now that the device has been successfully opened, update the devid
492 	 * if necessary.
493 	 */
494 	if (validate_devid) {
495 		ddi_devid_t devid = NULL;
496 		char *minorname = NULL;
497 		char *vd_devid = NULL;
498 		boolean_t remove = B_FALSE, update = B_FALSE;
499 
500 		/*
501 		 * Get the current devid and minor name for the device we
502 		 * opened.
503 		 */
504 		if (ldi_get_devid(dvd->vd_lh, &devid) != 0 ||
505 		    ldi_get_minor_name(dvd->vd_lh, &minorname) != 0) {
506 			/*
507 			 * If we are unable to get the devid or the minor name
508 			 * for the device, we need to remove them from the
509 			 * configuration to prevent potential inconsistencies.
510 			 */
511 			if (dvd->vd_minor != NULL || dvd->vd_devid != NULL ||
512 			    vd->vdev_devid != NULL) {
513 				/*
514 				 * We only need to remove the devid if one
515 				 * exists.
516 				 */
517 				remove = B_TRUE;
518 			}
519 
520 		} else if (dvd->vd_devid == NULL || dvd->vd_minor == NULL) {
521 			/*
522 			 * There was previously no devid at all so we need to
523 			 * add one.
524 			 */
525 			update = B_TRUE;
526 
527 		} else if (ddi_devid_compare(devid, dvd->vd_devid) != 0 ||
528 		    strcmp(minorname, dvd->vd_minor) != 0) {
529 			/*
530 			 * The devid or minor name on file does not match the
531 			 * one from the opened device.
532 			 */
533 			update = B_TRUE;
534 		}
535 
536 		if (update) {
537 			/*
538 			 * Render the new devid and minor name as a string for
539 			 * logging and to store in the vdev configuration.
540 			 */
541 			vd_devid = ddi_devid_str_encode(devid, minorname);
542 		}
543 
544 		if (update || remove) {
545 			vdev_dbgmsg(vd, "vdev_disk_open: update devid from "
546 			    "'%s' to '%s'",
547 			    vd->vdev_devid != NULL ? vd->vdev_devid : "<none>",
548 			    vd_devid != NULL ? vd_devid : "<none>");
549 			cmn_err(CE_NOTE, "vdev_disk_open %s: update devid "
550 			    "from '%s' to '%s'",
551 			    vd->vdev_path != NULL ? vd->vdev_path : "?",
552 			    vd->vdev_devid != NULL ? vd->vdev_devid : "<none>",
553 			    vd_devid != NULL ? vd_devid : "<none>");
554 
555 			/*
556 			 * Remove and free any existing values.
557 			 */
558 			if (dvd->vd_minor != NULL) {
559 				ddi_devid_str_free(dvd->vd_minor);
560 				dvd->vd_minor = NULL;
561 			}
562 			if (dvd->vd_devid != NULL) {
563 				ddi_devid_free(dvd->vd_devid);
564 				dvd->vd_devid = NULL;
565 			}
566 			if (vd->vdev_devid != NULL) {
567 				spa_strfree(vd->vdev_devid);
568 				vd->vdev_devid = NULL;
569 			}
570 		}
571 
572 		if (update) {
573 			/*
574 			 * Install the new values.
575 			 */
576 			vd->vdev_devid = vd_devid;
577 			dvd->vd_minor = minorname;
578 			dvd->vd_devid = devid;
579 
580 		} else {
581 			if (devid != NULL) {
582 				ddi_devid_free(devid);
583 			}
584 			if (minorname != NULL) {
585 				kmem_free(minorname, strlen(minorname) + 1);
586 			}
587 		}
588 	}
589 
590 	/*
591 	 * Once a device is opened, verify that the physical device path (if
592 	 * available) is up to date.
593 	 */
594 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
595 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
596 		char *physpath, *minorname;
597 
598 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
599 		minorname = NULL;
600 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
601 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
602 		    (vd->vdev_physpath == NULL ||
603 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
604 			if (vd->vdev_physpath)
605 				spa_strfree(vd->vdev_physpath);
606 			(void) strlcat(physpath, ":", MAXPATHLEN);
607 			(void) strlcat(physpath, minorname, MAXPATHLEN);
608 			vd->vdev_physpath = spa_strdup(physpath);
609 		}
610 		if (minorname)
611 			kmem_free(minorname, strlen(minorname) + 1);
612 		kmem_free(physpath, MAXPATHLEN);
613 	}
614 
615 	/*
616 	 * Register callbacks for the LDI offline event.
617 	 */
618 	if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
619 	    LDI_EV_SUCCESS) {
620 		lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
621 		list_insert_tail(&dvd->vd_ldi_cbs, lcb);
622 		(void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
623 		    &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
624 	}
625 
626 	/*
627 	 * Register callbacks for the LDI degrade event.
628 	 */
629 	if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
630 	    LDI_EV_SUCCESS) {
631 		lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
632 		list_insert_tail(&dvd->vd_ldi_cbs, lcb);
633 		(void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
634 		    &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
635 	}
636 skip_open:
637 	/*
638 	 * Determine the actual size of the device.
639 	 */
640 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
641 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
642 		vdev_dbgmsg(vd, "vdev_disk_open: failed to get size");
643 		return (SET_ERROR(EINVAL));
644 	}
645 
646 	*max_psize = *psize;
647 
648 	/*
649 	 * Determine the device's minimum transfer size.
650 	 * If the ioctl isn't supported, assume DEV_BSIZE.
651 	 */
652 	if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
653 	    (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
654 		capacity = dkmext->dki_capacity - 1;
655 		blksz = dkmext->dki_lbsize;
656 		pbsize = dkmext->dki_pbsize;
657 	} else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
658 	    (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
659 		VDEV_DEBUG(
660 		    "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
661 		    vd->vdev_path);
662 		capacity = dkm->dki_capacity - 1;
663 		blksz = dkm->dki_lbsize;
664 		pbsize = blksz;
665 	} else {
666 		VDEV_DEBUG("vdev_disk_open(\"%s\"): "
667 		    "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
668 		    vd->vdev_path, error);
669 		pbsize = DEV_BSIZE;
670 	}
671 
672 	*ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
673 
674 	if (vd->vdev_wholedisk == 1) {
675 		int wce = 1;
676 
677 		if (error == 0) {
678 			/*
679 			 * If we have the capability to expand, we'd have
680 			 * found out via success from DKIOCGMEDIAINFO{,EXT}.
681 			 * Adjust max_psize upward accordingly since we know
682 			 * we own the whole disk now.
683 			 */
684 			*max_psize = capacity * blksz;
685 		}
686 
687 		/*
688 		 * Since we own the whole disk, try to enable disk write
689 		 * caching.  We ignore errors because it's OK if we can't do it.
690 		 */
691 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
692 		    FKIOCTL, kcred, NULL);
693 	}
694 
695 	/*
696 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
697 	 * try again.
698 	 */
699 	vd->vdev_nowritecache = B_FALSE;
700 
701 	/* Inform the ZIO pipeline that we are non-rotational */
702 	vd->vdev_nonrot = B_FALSE;
703 	if (ldi_prop_exists(dvd->vd_lh, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
704 	    "device-solid-state")) {
705 		if (ldi_prop_get_int(dvd->vd_lh,
706 		    LDI_DEV_T_ANY | DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
707 		    "device-solid-state", B_FALSE) != 0)
708 			vd->vdev_nonrot = B_TRUE;
709 	}
710 
711 	return (0);
712 }
713 
714 static void
715 vdev_disk_close(vdev_t *vd)
716 {
717 	vdev_disk_t *dvd = vd->vdev_tsd;
718 
719 	if (vd->vdev_reopening || dvd == NULL)
720 		return;
721 
722 	if (dvd->vd_minor != NULL) {
723 		ddi_devid_str_free(dvd->vd_minor);
724 		dvd->vd_minor = NULL;
725 	}
726 
727 	if (dvd->vd_devid != NULL) {
728 		ddi_devid_free(dvd->vd_devid);
729 		dvd->vd_devid = NULL;
730 	}
731 
732 	if (dvd->vd_lh != NULL) {
733 		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
734 		dvd->vd_lh = NULL;
735 	}
736 
737 	vd->vdev_delayed_close = B_FALSE;
738 	/*
739 	 * If we closed the LDI handle due to an offline notify from LDI,
740 	 * don't free vd->vdev_tsd or unregister the callbacks here;
741 	 * the offline finalize callback or a reopen will take care of it.
742 	 */
743 	if (dvd->vd_ldi_offline)
744 		return;
745 
746 	vdev_disk_free(vd);
747 }
748 
749 int
750 vdev_disk_physio(vdev_t *vd, caddr_t data,
751     size_t size, uint64_t offset, int flags, boolean_t isdump)
752 {
753 	vdev_disk_t *dvd = vd->vdev_tsd;
754 
755 	/*
756 	 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
757 	 * Nothing to be done here but return failure.
758 	 */
759 	if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
760 		return (EIO);
761 
762 	ASSERT(vd->vdev_ops == &vdev_disk_ops);
763 
764 	/*
765 	 * If in the context of an active crash dump, use the ldi_dump(9F)
766 	 * call instead of ldi_strategy(9F) as usual.
767 	 */
768 	if (isdump) {
769 		ASSERT3P(dvd, !=, NULL);
770 		return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
771 		    lbtodb(size)));
772 	}
773 
774 	return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
775 }
776 
777 int
778 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
779     size_t size, uint64_t offset, int flags)
780 {
781 	buf_t *bp;
782 	int error = 0;
783 
784 	if (vd_lh == NULL)
785 		return (SET_ERROR(EINVAL));
786 
787 	ASSERT(flags & B_READ || flags & B_WRITE);
788 
789 	bp = getrbuf(KM_SLEEP);
790 	bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
791 	bp->b_bcount = size;
792 	bp->b_un.b_addr = (void *)data;
793 	bp->b_lblkno = lbtodb(offset);
794 	bp->b_bufsize = size;
795 
796 	error = ldi_strategy(vd_lh, bp);
797 	ASSERT(error == 0);
798 	if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
799 		error = SET_ERROR(EIO);
800 	freerbuf(bp);
801 
802 	return (error);
803 }
804 
805 static int
806 vdev_disk_io_intr(buf_t *bp)
807 {
808 	vdev_buf_t *vb = (vdev_buf_t *)bp;
809 	zio_t *zio = vb->vb_io;
810 
811 	/*
812 	 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
813 	 * Rather than teach the rest of the stack about other error
814 	 * possibilities (EFAULT, etc), we normalize the error value here.
815 	 */
816 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
817 
818 	if (zio->io_error == 0 && bp->b_resid != 0)
819 		zio->io_error = SET_ERROR(EIO);
820 
821 	if (zio->io_type == ZIO_TYPE_READ) {
822 		abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
823 	} else {
824 		abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
825 	}
826 
827 	kmem_free(vb, sizeof (vdev_buf_t));
828 
829 	zio_delay_interrupt(zio);
830 	return (0);
831 }
832 
833 static void
834 vdev_disk_ioctl_free(zio_t *zio)
835 {
836 	kmem_free(zio->io_vsd, sizeof (struct dk_callback));
837 }
838 
839 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
840 	vdev_disk_ioctl_free,
841 	zio_vsd_default_cksum_report
842 };
843 
844 static void
845 vdev_disk_ioctl_done(void *zio_arg, int error)
846 {
847 	zio_t *zio = zio_arg;
848 
849 	zio->io_error = error;
850 
851 	zio_interrupt(zio);
852 }
853 
854 static void
855 vdev_disk_io_start(zio_t *zio)
856 {
857 	vdev_t *vd = zio->io_vd;
858 	vdev_disk_t *dvd = vd->vdev_tsd;
859 	vdev_buf_t *vb;
860 	struct dk_callback *dkc;
861 	buf_t *bp;
862 	int error;
863 
864 	/*
865 	 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
866 	 * Nothing to be done here but return failure.
867 	 */
868 	if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
869 		zio->io_error = ENXIO;
870 		zio_interrupt(zio);
871 		return;
872 	}
873 
874 	if (zio->io_type == ZIO_TYPE_IOCTL) {
875 		/* XXPOLICY */
876 		if (!vdev_readable(vd)) {
877 			zio->io_error = SET_ERROR(ENXIO);
878 			zio_interrupt(zio);
879 			return;
880 		}
881 
882 		switch (zio->io_cmd) {
883 
884 		case DKIOCFLUSHWRITECACHE:
885 
886 			if (zfs_nocacheflush)
887 				break;
888 
889 			if (vd->vdev_nowritecache) {
890 				zio->io_error = SET_ERROR(ENOTSUP);
891 				break;
892 			}
893 
894 			zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
895 			zio->io_vsd_ops = &vdev_disk_vsd_ops;
896 
897 			dkc->dkc_callback = vdev_disk_ioctl_done;
898 			dkc->dkc_flag = FLUSH_VOLATILE;
899 			dkc->dkc_cookie = zio;
900 
901 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
902 			    (uintptr_t)dkc, FKIOCTL, kcred, NULL);
903 
904 			if (error == 0) {
905 				/*
906 				 * The ioctl will be done asychronously,
907 				 * and will call vdev_disk_ioctl_done()
908 				 * upon completion.
909 				 */
910 				return;
911 			}
912 
913 			zio->io_error = error;
914 
915 			break;
916 
917 		default:
918 			zio->io_error = SET_ERROR(ENOTSUP);
919 		}
920 
921 		zio_execute(zio);
922 		return;
923 	}
924 
925 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
926 	zio->io_target_timestamp = zio_handle_io_delay(zio);
927 
928 	vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
929 
930 	vb->vb_io = zio;
931 	bp = &vb->vb_buf;
932 
933 	bioinit(bp);
934 	bp->b_flags = B_BUSY | B_NOCACHE |
935 	    (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
936 	if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
937 		bp->b_flags |= B_FAILFAST;
938 	bp->b_bcount = zio->io_size;
939 
940 	if (zio->io_type == ZIO_TYPE_READ) {
941 		bp->b_un.b_addr =
942 		    abd_borrow_buf(zio->io_abd, zio->io_size);
943 	} else {
944 		bp->b_un.b_addr =
945 		    abd_borrow_buf_copy(zio->io_abd, zio->io_size);
946 	}
947 
948 	bp->b_lblkno = lbtodb(zio->io_offset);
949 	bp->b_bufsize = zio->io_size;
950 	bp->b_iodone = vdev_disk_io_intr;
951 
952 	/*
953 	 * In general we would expect ldi_strategy() to return non-zero only
954 	 * because of programming errors, but we've also seen this fail shortly
955 	 * after a disk dies.
956 	 */
957 	if (ldi_strategy(dvd->vd_lh, bp) != 0) {
958 		zio->io_error = ENXIO;
959 		zio_interrupt(zio);
960 	}
961 }
962 
963 static void
964 vdev_disk_io_done(zio_t *zio)
965 {
966 	vdev_t *vd = zio->io_vd;
967 
968 	/*
969 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
970 	 * the device has been removed.  If this is the case, then we trigger an
971 	 * asynchronous removal of the device. Otherwise, probe the device and
972 	 * make sure it's still accessible.
973 	 */
974 	if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
975 		vdev_disk_t *dvd = vd->vdev_tsd;
976 		int state = DKIO_NONE;
977 
978 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
979 		    FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
980 			/*
981 			 * We post the resource as soon as possible, instead of
982 			 * when the async removal actually happens, because the
983 			 * DE is using this information to discard previous I/O
984 			 * errors.
985 			 */
986 			zfs_post_remove(zio->io_spa, vd);
987 			vd->vdev_remove_wanted = B_TRUE;
988 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
989 		} else if (!vd->vdev_delayed_close) {
990 			vd->vdev_delayed_close = B_TRUE;
991 		}
992 	}
993 }
994 
995 vdev_ops_t vdev_disk_ops = {
996 	.vdev_op_open = vdev_disk_open,
997 	.vdev_op_close = vdev_disk_close,
998 	.vdev_op_asize = vdev_default_asize,
999 	.vdev_op_io_start = vdev_disk_io_start,
1000 	.vdev_op_io_done = vdev_disk_io_done,
1001 	.vdev_op_state_change = NULL,
1002 	.vdev_op_need_resilver = NULL,
1003 	.vdev_op_hold = vdev_disk_hold,
1004 	.vdev_op_rele = vdev_disk_rele,
1005 	.vdev_op_remap = NULL,
1006 	.vdev_op_xlate = vdev_default_xlate,
1007 	.vdev_op_type = VDEV_TYPE_DISK,		/* name of this vdev type */
1008 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
1009 };
1010 
1011 /*
1012  * Given the root disk device devid or pathname, read the label from
1013  * the device, and construct a configuration nvlist.
1014  */
1015 int
1016 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
1017 {
1018 	ldi_handle_t vd_lh;
1019 	vdev_label_t *label;
1020 	uint64_t s, size;
1021 	int l;
1022 	ddi_devid_t tmpdevid;
1023 	int error = -1;
1024 	char *minor_name;
1025 
1026 	/*
1027 	 * Read the device label and build the nvlist.
1028 	 */
1029 	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
1030 	    &minor_name) == 0) {
1031 		error = ldi_open_by_devid(tmpdevid, minor_name,
1032 		    FREAD, kcred, &vd_lh, zfs_li);
1033 		ddi_devid_free(tmpdevid);
1034 		ddi_devid_str_free(minor_name);
1035 	}
1036 
1037 	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
1038 	    zfs_li)))
1039 		return (error);
1040 
1041 	if (ldi_get_size(vd_lh, &s)) {
1042 		(void) ldi_close(vd_lh, FREAD, kcred);
1043 		return (SET_ERROR(EIO));
1044 	}
1045 
1046 	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
1047 	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
1048 
1049 	*config = NULL;
1050 	for (l = 0; l < VDEV_LABELS; l++) {
1051 		uint64_t offset, state, txg = 0;
1052 
1053 		/* read vdev label */
1054 		offset = vdev_label_offset(size, l, 0);
1055 		if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
1056 		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
1057 			continue;
1058 
1059 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
1060 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
1061 			*config = NULL;
1062 			continue;
1063 		}
1064 
1065 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
1066 		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
1067 			nvlist_free(*config);
1068 			*config = NULL;
1069 			continue;
1070 		}
1071 
1072 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
1073 		    &txg) != 0 || txg == 0) {
1074 			nvlist_free(*config);
1075 			*config = NULL;
1076 			continue;
1077 		}
1078 
1079 		break;
1080 	}
1081 
1082 	kmem_free(label, sizeof (vdev_label_t));
1083 	(void) ldi_close(vd_lh, FREAD, kcred);
1084 	if (*config == NULL)
1085 		error = SET_ERROR(EIDRM);
1086 
1087 	return (error);
1088 }
1089