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