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