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