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