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