]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev_disk.c
MFC r260138: MFV r242733:
[FreeBSD/stable/8.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / vdev_disk.c
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 Joyent, 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 static int
134 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
135     uint64_t *ashift)
136 {
137         spa_t *spa = vd->vdev_spa;
138         vdev_disk_t *dvd;
139         struct dk_minfo_ext dkmext;
140         int error;
141         dev_t dev;
142         int otyp;
143
144         /*
145          * We must have a pathname, and it must be absolute.
146          */
147         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
148                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
149                 return (SET_ERROR(EINVAL));
150         }
151
152         /*
153          * Reopen the device if it's not currently open. Otherwise,
154          * just update the physical size of the device.
155          */
156         if (vd->vdev_tsd != NULL) {
157                 ASSERT(vd->vdev_reopening);
158                 dvd = vd->vdev_tsd;
159                 goto skip_open;
160         }
161
162         dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
163
164         /*
165          * When opening a disk device, we want to preserve the user's original
166          * intent.  We always want to open the device by the path the user gave
167          * us, even if it is one of multiple paths to the save device.  But we
168          * also want to be able to survive disks being removed/recabled.
169          * Therefore the sequence of opening devices is:
170          *
171          * 1. Try opening the device by path.  For legacy pools without the
172          *    'whole_disk' property, attempt to fix the path by appending 's0'.
173          *
174          * 2. If the devid of the device matches the stored value, return
175          *    success.
176          *
177          * 3. Otherwise, the device may have moved.  Try opening the device
178          *    by the devid instead.
179          */
180         if (vd->vdev_devid != NULL) {
181                 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
182                     &dvd->vd_minor) != 0) {
183                         vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
184                         return (SET_ERROR(EINVAL));
185                 }
186         }
187
188         error = EINVAL;         /* presume failure */
189
190         if (vd->vdev_path != NULL) {
191                 ddi_devid_t devid;
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          * If all else fails, then try opening by physical path (if available)
246          * or the logical path (if we failed due to the devid check).  While not
247          * as reliable as the devid, this will give us something, and the higher
248          * level vdev validation will prevent us from opening the wrong device.
249          */
250         if (error) {
251                 if (vd->vdev_physpath != NULL &&
252                     (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
253                         error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
254                             kcred, &dvd->vd_lh, zfs_li);
255
256                 /*
257                  * Note that we don't support the legacy auto-wholedisk support
258                  * as above.  This hasn't been used in a very long time and we
259                  * don't need to propagate its oddities to this edge condition.
260                  */
261                 if (error && vd->vdev_path != NULL)
262                         error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
263                             kcred, &dvd->vd_lh, zfs_li);
264         }
265
266         if (error) {
267                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
268                 return (error);
269         }
270
271         /*
272          * Once a device is opened, verify that the physical device path (if
273          * available) is up to date.
274          */
275         if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
276             ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
277                 char *physpath, *minorname;
278
279                 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
280                 minorname = NULL;
281                 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
282                     ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
283                     (vd->vdev_physpath == NULL ||
284                     strcmp(vd->vdev_physpath, physpath) != 0)) {
285                         if (vd->vdev_physpath)
286                                 spa_strfree(vd->vdev_physpath);
287                         (void) strlcat(physpath, ":", MAXPATHLEN);
288                         (void) strlcat(physpath, minorname, MAXPATHLEN);
289                         vd->vdev_physpath = spa_strdup(physpath);
290                 }
291                 if (minorname)
292                         kmem_free(minorname, strlen(minorname) + 1);
293                 kmem_free(physpath, MAXPATHLEN);
294         }
295
296 skip_open:
297         /*
298          * Determine the actual size of the device.
299          */
300         if (ldi_get_size(dvd->vd_lh, psize) != 0) {
301                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
302                 return (SET_ERROR(EINVAL));
303         }
304
305         /*
306          * Determine the device's minimum transfer size.
307          * If the ioctl isn't supported, assume DEV_BSIZE.
308          */
309         if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext,
310             FKIOCTL, kcred, NULL) != 0)
311                 dkmext.dki_pbsize = DEV_BSIZE;
312
313         *ashift = highbit(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1;
314
315         if (vd->vdev_wholedisk == 1) {
316                 uint64_t capacity = dkmext.dki_capacity - 1;
317                 uint64_t blksz = dkmext.dki_lbsize;
318                 int wce = 1;
319
320                 /*
321                  * If we own the whole disk, try to enable disk write caching.
322                  * We ignore errors because it's OK if we can't do it.
323                  */
324                 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
325                     FKIOCTL, kcred, NULL);
326
327                 *max_psize = *psize + vdev_disk_get_space(vd, capacity, blksz);
328                 zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
329                     "max_psize %llu", vd->vdev_path, *psize, *max_psize);
330         } else {
331                 *max_psize = *psize;
332         }
333
334         /*
335          * Clear the nowritecache bit, so that on a vdev_reopen() we will
336          * try again.
337          */
338         vd->vdev_nowritecache = B_FALSE;
339
340         return (0);
341 }
342
343 static void
344 vdev_disk_close(vdev_t *vd)
345 {
346         vdev_disk_t *dvd = vd->vdev_tsd;
347
348         if (vd->vdev_reopening || dvd == NULL)
349                 return;
350
351         if (dvd->vd_minor != NULL)
352                 ddi_devid_str_free(dvd->vd_minor);
353
354         if (dvd->vd_devid != NULL)
355                 ddi_devid_free(dvd->vd_devid);
356
357         if (dvd->vd_lh != NULL)
358                 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
359
360         vd->vdev_delayed_close = B_FALSE;
361         kmem_free(dvd, sizeof (vdev_disk_t));
362         vd->vdev_tsd = NULL;
363 }
364
365 int
366 vdev_disk_physio(vdev_t *vd, caddr_t data,
367     size_t size, uint64_t offset, int flags, boolean_t isdump)
368 {
369         vdev_disk_t *dvd = vd->vdev_tsd;
370
371         ASSERT(vd->vdev_ops == &vdev_disk_ops);
372
373         /*
374          * If in the context of an active crash dump, use the ldi_dump(9F)
375          * call instead of ldi_strategy(9F) as usual.
376          */
377         if (isdump) {
378                 ASSERT3P(dvd, !=, NULL);
379                 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
380                     lbtodb(size)));
381         }
382
383         return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
384 }
385
386 int
387 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
388     size_t size, uint64_t offset, int flags)
389 {
390         buf_t *bp;
391         int error = 0;
392
393         if (vd_lh == NULL)
394                 return (SET_ERROR(EINVAL));
395
396         ASSERT(flags & B_READ || flags & B_WRITE);
397
398         bp = getrbuf(KM_SLEEP);
399         bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
400         bp->b_bcount = size;
401         bp->b_un.b_addr = (void *)data;
402         bp->b_lblkno = lbtodb(offset);
403         bp->b_bufsize = size;
404
405         error = ldi_strategy(vd_lh, bp);
406         ASSERT(error == 0);
407         if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
408                 error = SET_ERROR(EIO);
409         freerbuf(bp);
410
411         return (error);
412 }
413
414 static void
415 vdev_disk_io_intr(buf_t *bp)
416 {
417         vdev_buf_t *vb = (vdev_buf_t *)bp;
418         zio_t *zio = vb->vb_io;
419
420         /*
421          * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
422          * Rather than teach the rest of the stack about other error
423          * possibilities (EFAULT, etc), we normalize the error value here.
424          */
425         zio->io_error = (geterror(bp) != 0 ? EIO : 0);
426
427         if (zio->io_error == 0 && bp->b_resid != 0)
428                 zio->io_error = SET_ERROR(EIO);
429
430         kmem_free(vb, sizeof (vdev_buf_t));
431
432         zio_interrupt(zio);
433 }
434
435 static void
436 vdev_disk_ioctl_free(zio_t *zio)
437 {
438         kmem_free(zio->io_vsd, sizeof (struct dk_callback));
439 }
440
441 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
442         vdev_disk_ioctl_free,
443         zio_vsd_default_cksum_report
444 };
445
446 static void
447 vdev_disk_ioctl_done(void *zio_arg, int error)
448 {
449         zio_t *zio = zio_arg;
450
451         zio->io_error = error;
452
453         zio_interrupt(zio);
454 }
455
456 static int
457 vdev_disk_io_start(zio_t *zio)
458 {
459         vdev_t *vd = zio->io_vd;
460         vdev_disk_t *dvd = vd->vdev_tsd;
461         vdev_buf_t *vb;
462         struct dk_callback *dkc;
463         buf_t *bp;
464         int error;
465
466         if (zio->io_type == ZIO_TYPE_IOCTL) {
467                 /* XXPOLICY */
468                 if (!vdev_readable(vd)) {
469                         zio->io_error = SET_ERROR(ENXIO);
470                         return (ZIO_PIPELINE_CONTINUE);
471                 }
472
473                 switch (zio->io_cmd) {
474
475                 case DKIOCFLUSHWRITECACHE:
476
477                         if (zfs_nocacheflush)
478                                 break;
479
480                         if (vd->vdev_nowritecache) {
481                                 zio->io_error = SET_ERROR(ENOTSUP);
482                                 break;
483                         }
484
485                         zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
486                         zio->io_vsd_ops = &vdev_disk_vsd_ops;
487
488                         dkc->dkc_callback = vdev_disk_ioctl_done;
489                         dkc->dkc_flag = FLUSH_VOLATILE;
490                         dkc->dkc_cookie = zio;
491
492                         error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
493                             (uintptr_t)dkc, FKIOCTL, kcred, NULL);
494
495                         if (error == 0) {
496                                 /*
497                                  * The ioctl will be done asychronously,
498                                  * and will call vdev_disk_ioctl_done()
499                                  * upon completion.
500                                  */
501                                 return (ZIO_PIPELINE_STOP);
502                         }
503
504                         if (error == ENOTSUP || error == ENOTTY) {
505                                 /*
506                                  * If we get ENOTSUP or ENOTTY, we know that
507                                  * no future attempts will ever succeed.
508                                  * In this case we set a persistent bit so
509                                  * that we don't bother with the ioctl in the
510                                  * future.
511                                  */
512                                 vd->vdev_nowritecache = B_TRUE;
513                         }
514                         zio->io_error = error;
515
516                         break;
517
518                 default:
519                         zio->io_error = SET_ERROR(ENOTSUP);
520                 }
521
522                 return (ZIO_PIPELINE_CONTINUE);
523         }
524
525         vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
526
527         vb->vb_io = zio;
528         bp = &vb->vb_buf;
529
530         bioinit(bp);
531         bp->b_flags = B_BUSY | B_NOCACHE |
532             (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
533         if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
534                 bp->b_flags |= B_FAILFAST;
535         bp->b_bcount = zio->io_size;
536         bp->b_un.b_addr = zio->io_data;
537         bp->b_lblkno = lbtodb(zio->io_offset);
538         bp->b_bufsize = zio->io_size;
539         bp->b_iodone = (int (*)())vdev_disk_io_intr;
540
541         /* ldi_strategy() will return non-zero only on programming errors */
542         VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
543
544         return (ZIO_PIPELINE_STOP);
545 }
546
547 static void
548 vdev_disk_io_done(zio_t *zio)
549 {
550         vdev_t *vd = zio->io_vd;
551
552         /*
553          * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
554          * the device has been removed.  If this is the case, then we trigger an
555          * asynchronous removal of the device. Otherwise, probe the device and
556          * make sure it's still accessible.
557          */
558         if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
559                 vdev_disk_t *dvd = vd->vdev_tsd;
560                 int state = DKIO_NONE;
561
562                 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
563                     FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
564                         /*
565                          * We post the resource as soon as possible, instead of
566                          * when the async removal actually happens, because the
567                          * DE is using this information to discard previous I/O
568                          * errors.
569                          */
570                         zfs_post_remove(zio->io_spa, vd);
571                         vd->vdev_remove_wanted = B_TRUE;
572                         spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
573                 } else if (!vd->vdev_delayed_close) {
574                         vd->vdev_delayed_close = B_TRUE;
575                 }
576         }
577 }
578
579 vdev_ops_t vdev_disk_ops = {
580         vdev_disk_open,
581         vdev_disk_close,
582         vdev_default_asize,
583         vdev_disk_io_start,
584         vdev_disk_io_done,
585         NULL,
586         vdev_disk_hold,
587         vdev_disk_rele,
588         VDEV_TYPE_DISK,         /* name of this vdev type */
589         B_TRUE                  /* leaf vdev */
590 };
591
592 /*
593  * Given the root disk device devid or pathname, read the label from
594  * the device, and construct a configuration nvlist.
595  */
596 int
597 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
598 {
599         ldi_handle_t vd_lh;
600         vdev_label_t *label;
601         uint64_t s, size;
602         int l;
603         ddi_devid_t tmpdevid;
604         int error = -1;
605         char *minor_name;
606
607         /*
608          * Read the device label and build the nvlist.
609          */
610         if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
611             &minor_name) == 0) {
612                 error = ldi_open_by_devid(tmpdevid, minor_name,
613                     FREAD, kcred, &vd_lh, zfs_li);
614                 ddi_devid_free(tmpdevid);
615                 ddi_devid_str_free(minor_name);
616         }
617
618         if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
619             zfs_li)))
620                 return (error);
621
622         if (ldi_get_size(vd_lh, &s)) {
623                 (void) ldi_close(vd_lh, FREAD, kcred);
624                 return (SET_ERROR(EIO));
625         }
626
627         size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
628         label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
629
630         *config = NULL;
631         for (l = 0; l < VDEV_LABELS; l++) {
632                 uint64_t offset, state, txg = 0;
633
634                 /* read vdev label */
635                 offset = vdev_label_offset(size, l, 0);
636                 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
637                     VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
638                         continue;
639
640                 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
641                     sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
642                         *config = NULL;
643                         continue;
644                 }
645
646                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
647                     &state) != 0 || state >= POOL_STATE_DESTROYED) {
648                         nvlist_free(*config);
649                         *config = NULL;
650                         continue;
651                 }
652
653                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
654                     &txg) != 0 || txg == 0) {
655                         nvlist_free(*config);
656                         *config = NULL;
657                         continue;
658                 }
659
660                 break;
661         }
662
663         kmem_free(label, sizeof (vdev_label_t));
664         (void) ldi_close(vd_lh, FREAD, kcred);
665         if (*config == NULL)
666                 error = SET_ERROR(EIDRM);
667
668         return (error);
669 }