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