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