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