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