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.
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.
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]
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 (c) 2013 Joyent, Inc. All rights reserved.
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>
35 #include <sys/sunldi.h>
36 #include <sys/efi_partition.h>
37 #include <sys/fm/fs/zfs.h>
40 * Virtual device vector for disks.
43 extern ldi_ident_t zfs_li;
45 static void vdev_disk_close(vdev_t *);
47 typedef struct vdev_disk_ldi_cb {
49 ldi_callback_id_t lcb_id;
53 vdev_disk_alloc(vdev_t *vd)
57 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
59 * Create the LDI event callback list.
61 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
62 offsetof(vdev_disk_ldi_cb_t, lcb_next));
66 vdev_disk_free(vdev_t *vd)
68 vdev_disk_t *dvd = vd->vdev_tsd;
69 vdev_disk_ldi_cb_t *lcb;
75 * We have already closed the LDI handle. Clean up the LDI event
76 * callbacks and free vd->vdev_tsd.
78 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
79 list_remove(&dvd->vd_ldi_cbs, lcb);
80 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
81 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
83 list_destroy(&dvd->vd_ldi_cbs);
84 kmem_free(dvd, sizeof (vdev_disk_t));
90 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
93 vdev_t *vd = (vdev_t *)arg;
94 vdev_disk_t *dvd = vd->vdev_tsd;
97 * Ignore events other than offline.
99 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
100 return (LDI_EV_SUCCESS);
103 * All LDI handles must be closed for the state change to succeed, so
104 * call on vdev_disk_close() to do this.
106 * We inform vdev_disk_close that it is being called from offline
107 * notify context so it will defer cleanup of LDI event callbacks and
108 * freeing of vd->vdev_tsd to the offline finalize or a reopen.
110 dvd->vd_ldi_offline = B_TRUE;
114 * Now that the device is closed, request that the spa_async_thread
115 * mark the device as REMOVED and notify FMA of the removal.
117 zfs_post_remove(vd->vdev_spa, vd);
118 vd->vdev_remove_wanted = B_TRUE;
119 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
121 return (LDI_EV_SUCCESS);
126 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
127 int ldi_result, void *arg, void *ev_data)
129 vdev_t *vd = (vdev_t *)arg;
132 * Ignore events other than offline.
134 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
138 * We have already closed the LDI handle in notify.
139 * Clean up the LDI event callbacks and free vd->vdev_tsd.
144 * Request that the vdev be reopened if the offline state change was
147 if (ldi_result != LDI_EV_SUCCESS) {
148 vd->vdev_probe_wanted = B_TRUE;
149 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
153 static ldi_ev_callback_t vdev_disk_off_callb = {
154 .cb_vers = LDI_EV_CB_VERS,
155 .cb_notify = vdev_disk_off_notify,
156 .cb_finalize = vdev_disk_off_finalize
161 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
162 int ldi_result, void *arg, void *ev_data)
164 vdev_t *vd = (vdev_t *)arg;
167 * Ignore events other than degrade.
169 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
173 * Degrade events always succeed. Mark the vdev as degraded.
174 * This status is purely informative for the user.
176 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
179 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
180 .cb_vers = LDI_EV_CB_VERS,
182 .cb_finalize = vdev_disk_dgrd_finalize
186 vdev_disk_hold(vdev_t *vd)
191 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
194 * We must have a pathname, and it must be absolute.
196 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
200 * Only prefetch path and devid info if the device has
203 if (vd->vdev_tsd != NULL)
206 if (vd->vdev_wholedisk == -1ULL) {
207 size_t len = strlen(vd->vdev_path) + 3;
208 char *buf = kmem_alloc(len, KM_SLEEP);
210 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
212 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
216 if (vd->vdev_name_vp == NULL)
217 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
219 if (vd->vdev_devid != NULL &&
220 ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
221 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
222 ddi_devid_str_free(minor);
223 ddi_devid_free(devid);
228 vdev_disk_rele(vdev_t *vd)
230 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
232 if (vd->vdev_name_vp) {
233 VN_RELE_ASYNC(vd->vdev_name_vp,
234 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
235 vd->vdev_name_vp = NULL;
237 if (vd->vdev_devid_vp) {
238 VN_RELE_ASYNC(vd->vdev_devid_vp,
239 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
240 vd->vdev_devid_vp = NULL;
245 vdev_disk_get_space(vdev_t *vd, uint64_t capacity, uint_t blksz)
247 ASSERT(vd->vdev_wholedisk);
249 vdev_disk_t *dvd = vd->vdev_tsd;
252 uint64_t avail_space = 0;
253 int efisize = EFI_LABEL_SIZE * 2;
255 dk_ioc.dki_data = kmem_alloc(efisize, KM_SLEEP);
257 dk_ioc.dki_length = efisize;
258 dk_ioc.dki_data_64 = (uint64_t)(uintptr_t)dk_ioc.dki_data;
259 efi = dk_ioc.dki_data;
261 if (ldi_ioctl(dvd->vd_lh, DKIOCGETEFI, (intptr_t)&dk_ioc,
262 FKIOCTL, kcred, NULL) == 0) {
263 uint64_t efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
265 zfs_dbgmsg("vdev %s, capacity %llu, altern lba %llu",
266 vd->vdev_path, capacity, efi_altern_lba);
267 if (capacity > efi_altern_lba)
268 avail_space = (capacity - efi_altern_lba) * blksz;
270 kmem_free(dk_ioc.dki_data, efisize);
271 return (avail_space);
275 * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when
276 * even a fallback to DKIOCGMEDIAINFO fails.
279 #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__)
281 #define VDEV_DEBUG(...) /* Nothing... */
285 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
288 spa_t *spa = vd->vdev_spa;
289 vdev_disk_t *dvd = vd->vdev_tsd;
290 ldi_ev_cookie_t ecookie;
291 vdev_disk_ldi_cb_t *lcb;
293 struct dk_minfo_ext ude;
296 struct dk_minfo_ext *dkmext = &dks.ude;
297 struct dk_minfo *dkm = &dks.ud;
301 boolean_t validate_devid = B_FALSE;
303 uint64_t capacity = 0, blksz = 0, pbsize;
306 * We must have a pathname, and it must be absolute.
308 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
309 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
310 return (SET_ERROR(EINVAL));
314 * Reopen the device if it's not currently open. Otherwise,
315 * just update the physical size of the device.
318 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
320 * If we are opening a device in its offline notify
321 * context, the LDI handle was just closed. Clean
322 * up the LDI event callbacks and free vd->vdev_tsd.
326 ASSERT(vd->vdev_reopening);
332 * Create vd->vdev_tsd.
338 * When opening a disk device, we want to preserve the user's original
339 * intent. We always want to open the device by the path the user gave
340 * us, even if it is one of multiple paths to the save device. But we
341 * also want to be able to survive disks being removed/recabled.
342 * Therefore the sequence of opening devices is:
344 * 1. Try opening the device by path. For legacy pools without the
345 * 'whole_disk' property, attempt to fix the path by appending 's0'.
347 * 2. If the devid of the device matches the stored value, return
350 * 3. Otherwise, the device may have moved. Try opening the device
351 * by the devid instead.
353 if (vd->vdev_devid != NULL) {
354 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
355 &dvd->vd_minor) != 0) {
356 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
357 return (SET_ERROR(EINVAL));
361 error = EINVAL; /* presume failure */
363 if (vd->vdev_path != NULL) {
365 if (vd->vdev_wholedisk == -1ULL) {
366 size_t len = strlen(vd->vdev_path) + 3;
367 char *buf = kmem_alloc(len, KM_SLEEP);
369 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
371 error = ldi_open_by_name(buf, spa_mode(spa), kcred,
372 &dvd->vd_lh, zfs_li);
374 spa_strfree(vd->vdev_path);
376 vd->vdev_wholedisk = 1ULL;
383 * If we have not yet opened the device, try to open it by the
387 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
388 kcred, &dvd->vd_lh, zfs_li);
392 * Compare the devid to the stored value.
394 if (error == 0 && vd->vdev_devid != NULL &&
395 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
396 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
397 error = SET_ERROR(EINVAL);
398 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
402 ddi_devid_free(devid);
406 * If we succeeded in opening the device, but 'vdev_wholedisk'
407 * is not yet set, then this must be a slice.
409 if (error == 0 && vd->vdev_wholedisk == -1ULL)
410 vd->vdev_wholedisk = 0;
414 * If we were unable to open by path, or the devid check fails, open by
417 if (error != 0 && vd->vdev_devid != NULL) {
418 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
419 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
423 * If all else fails, then try opening by physical path (if available)
424 * or the logical path (if we failed due to the devid check). While not
425 * as reliable as the devid, this will give us something, and the higher
426 * level vdev validation will prevent us from opening the wrong device.
429 if (vd->vdev_devid != NULL)
430 validate_devid = B_TRUE;
432 if (vd->vdev_physpath != NULL &&
433 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
434 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
435 kcred, &dvd->vd_lh, zfs_li);
438 * Note that we don't support the legacy auto-wholedisk support
439 * as above. This hasn't been used in a very long time and we
440 * don't need to propagate its oddities to this edge condition.
442 if (error && vd->vdev_path != NULL)
443 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
444 kcred, &dvd->vd_lh, zfs_li);
448 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
453 * Now that the device has been successfully opened, update the devid
456 if (validate_devid && spa_writeable(spa) &&
457 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
458 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
461 vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
462 zfs_dbgmsg("vdev %s: update devid from %s, "
463 "to %s", vd->vdev_path, vd->vdev_devid, vd_devid);
464 spa_strfree(vd->vdev_devid);
465 vd->vdev_devid = spa_strdup(vd_devid);
466 ddi_devid_str_free(vd_devid);
468 ddi_devid_free(devid);
472 * Once a device is opened, verify that the physical device path (if
473 * available) is up to date.
475 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
476 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
477 char *physpath, *minorname;
479 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
481 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
482 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
483 (vd->vdev_physpath == NULL ||
484 strcmp(vd->vdev_physpath, physpath) != 0)) {
485 if (vd->vdev_physpath)
486 spa_strfree(vd->vdev_physpath);
487 (void) strlcat(physpath, ":", MAXPATHLEN);
488 (void) strlcat(physpath, minorname, MAXPATHLEN);
489 vd->vdev_physpath = spa_strdup(physpath);
492 kmem_free(minorname, strlen(minorname) + 1);
493 kmem_free(physpath, MAXPATHLEN);
497 * Register callbacks for the LDI offline event.
499 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
501 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
502 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
503 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
504 &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
508 * Register callbacks for the LDI degrade event.
510 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
512 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
513 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
514 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
515 &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
519 * Determine the actual size of the device.
521 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
522 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
523 return (SET_ERROR(EINVAL));
529 * Determine the device's minimum transfer size.
530 * If the ioctl isn't supported, assume DEV_BSIZE.
532 if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
533 (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
534 capacity = dkmext->dki_capacity - 1;
535 blksz = dkmext->dki_lbsize;
536 pbsize = dkmext->dki_pbsize;
537 } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
538 (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
540 "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
542 capacity = dkm->dki_capacity - 1;
543 blksz = dkm->dki_lbsize;
546 VDEV_DEBUG("vdev_disk_open(\"%s\"): "
547 "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
548 vd->vdev_path, error);
552 *ashift = highbit(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
554 if (vd->vdev_wholedisk == 1) {
559 * If we have the capability to expand, we'd have
560 * found out via success from DKIOCGMEDIAINFO{,EXT}.
561 * Adjust max_psize upward accordingly since we know
562 * we own the whole disk now.
564 *max_psize += vdev_disk_get_space(vd, capacity, blksz);
565 zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
566 "max_psize %llu", vd->vdev_path, *psize,
571 * Since we own the whole disk, try to enable disk write
572 * caching. We ignore errors because it's OK if we can't do it.
574 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
575 FKIOCTL, kcred, NULL);
579 * Clear the nowritecache bit, so that on a vdev_reopen() we will
582 vd->vdev_nowritecache = B_FALSE;
588 vdev_disk_close(vdev_t *vd)
590 vdev_disk_t *dvd = vd->vdev_tsd;
592 if (vd->vdev_reopening || dvd == NULL)
595 if (dvd->vd_minor != NULL) {
596 ddi_devid_str_free(dvd->vd_minor);
597 dvd->vd_minor = NULL;
600 if (dvd->vd_devid != NULL) {
601 ddi_devid_free(dvd->vd_devid);
602 dvd->vd_devid = NULL;
605 if (dvd->vd_lh != NULL) {
606 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
610 vd->vdev_delayed_close = B_FALSE;
612 * If we closed the LDI handle due to an offline notify from LDI,
613 * don't free vd->vdev_tsd or unregister the callbacks here;
614 * the offline finalize callback or a reopen will take care of it.
616 if (dvd->vd_ldi_offline)
623 vdev_disk_physio(vdev_t *vd, caddr_t data,
624 size_t size, uint64_t offset, int flags, boolean_t isdump)
626 vdev_disk_t *dvd = vd->vdev_tsd;
629 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
630 * Nothing to be done here but return failure.
632 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
635 ASSERT(vd->vdev_ops == &vdev_disk_ops);
638 * If in the context of an active crash dump, use the ldi_dump(9F)
639 * call instead of ldi_strategy(9F) as usual.
642 ASSERT3P(dvd, !=, NULL);
643 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
647 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
651 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
652 size_t size, uint64_t offset, int flags)
658 return (SET_ERROR(EINVAL));
660 ASSERT(flags & B_READ || flags & B_WRITE);
662 bp = getrbuf(KM_SLEEP);
663 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
665 bp->b_un.b_addr = (void *)data;
666 bp->b_lblkno = lbtodb(offset);
667 bp->b_bufsize = size;
669 error = ldi_strategy(vd_lh, bp);
671 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
672 error = SET_ERROR(EIO);
679 vdev_disk_io_intr(buf_t *bp)
681 vdev_buf_t *vb = (vdev_buf_t *)bp;
682 zio_t *zio = vb->vb_io;
685 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
686 * Rather than teach the rest of the stack about other error
687 * possibilities (EFAULT, etc), we normalize the error value here.
689 zio->io_error = (geterror(bp) != 0 ? EIO : 0);
691 if (zio->io_error == 0 && bp->b_resid != 0)
692 zio->io_error = SET_ERROR(EIO);
694 kmem_free(vb, sizeof (vdev_buf_t));
700 vdev_disk_ioctl_free(zio_t *zio)
702 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
705 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
706 vdev_disk_ioctl_free,
707 zio_vsd_default_cksum_report
711 vdev_disk_ioctl_done(void *zio_arg, int error)
713 zio_t *zio = zio_arg;
715 zio->io_error = error;
721 vdev_disk_io_start(zio_t *zio)
723 vdev_t *vd = zio->io_vd;
724 vdev_disk_t *dvd = vd->vdev_tsd;
726 struct dk_callback *dkc;
731 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
732 * Nothing to be done here but return failure.
734 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
735 zio->io_error = ENXIO;
736 return (ZIO_PIPELINE_CONTINUE);
739 if (zio->io_type == ZIO_TYPE_IOCTL) {
741 if (!vdev_readable(vd)) {
742 zio->io_error = SET_ERROR(ENXIO);
743 return (ZIO_PIPELINE_CONTINUE);
746 switch (zio->io_cmd) {
748 case DKIOCFLUSHWRITECACHE:
750 if (zfs_nocacheflush)
753 if (vd->vdev_nowritecache) {
754 zio->io_error = SET_ERROR(ENOTSUP);
758 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
759 zio->io_vsd_ops = &vdev_disk_vsd_ops;
761 dkc->dkc_callback = vdev_disk_ioctl_done;
762 dkc->dkc_flag = FLUSH_VOLATILE;
763 dkc->dkc_cookie = zio;
765 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
766 (uintptr_t)dkc, FKIOCTL, kcred, NULL);
770 * The ioctl will be done asychronously,
771 * and will call vdev_disk_ioctl_done()
774 return (ZIO_PIPELINE_STOP);
777 if (error == ENOTSUP || error == ENOTTY) {
779 * If we get ENOTSUP or ENOTTY, we know that
780 * no future attempts will ever succeed.
781 * In this case we set a persistent bit so
782 * that we don't bother with the ioctl in the
785 vd->vdev_nowritecache = B_TRUE;
787 zio->io_error = error;
792 zio->io_error = SET_ERROR(ENOTSUP);
795 return (ZIO_PIPELINE_CONTINUE);
798 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
804 bp->b_flags = B_BUSY | B_NOCACHE |
805 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
806 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
807 bp->b_flags |= B_FAILFAST;
808 bp->b_bcount = zio->io_size;
809 bp->b_un.b_addr = zio->io_data;
810 bp->b_lblkno = lbtodb(zio->io_offset);
811 bp->b_bufsize = zio->io_size;
812 bp->b_iodone = (int (*)())vdev_disk_io_intr;
814 /* ldi_strategy() will return non-zero only on programming errors */
815 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
817 return (ZIO_PIPELINE_STOP);
821 vdev_disk_io_done(zio_t *zio)
823 vdev_t *vd = zio->io_vd;
826 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
827 * the device has been removed. If this is the case, then we trigger an
828 * asynchronous removal of the device. Otherwise, probe the device and
829 * make sure it's still accessible.
831 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
832 vdev_disk_t *dvd = vd->vdev_tsd;
833 int state = DKIO_NONE;
835 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
836 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
838 * We post the resource as soon as possible, instead of
839 * when the async removal actually happens, because the
840 * DE is using this information to discard previous I/O
843 zfs_post_remove(zio->io_spa, vd);
844 vd->vdev_remove_wanted = B_TRUE;
845 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
846 } else if (!vd->vdev_delayed_close) {
847 vd->vdev_delayed_close = B_TRUE;
852 vdev_ops_t vdev_disk_ops = {
861 VDEV_TYPE_DISK, /* name of this vdev type */
862 B_TRUE /* leaf vdev */
866 * Given the root disk device devid or pathname, read the label from
867 * the device, and construct a configuration nvlist.
870 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
876 ddi_devid_t tmpdevid;
881 * Read the device label and build the nvlist.
883 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
885 error = ldi_open_by_devid(tmpdevid, minor_name,
886 FREAD, kcred, &vd_lh, zfs_li);
887 ddi_devid_free(tmpdevid);
888 ddi_devid_str_free(minor_name);
891 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
895 if (ldi_get_size(vd_lh, &s)) {
896 (void) ldi_close(vd_lh, FREAD, kcred);
897 return (SET_ERROR(EIO));
900 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
901 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
904 for (l = 0; l < VDEV_LABELS; l++) {
905 uint64_t offset, state, txg = 0;
907 /* read vdev label */
908 offset = vdev_label_offset(size, l, 0);
909 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
910 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
913 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
914 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
919 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
920 &state) != 0 || state >= POOL_STATE_DESTROYED) {
921 nvlist_free(*config);
926 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
927 &txg) != 0 || txg == 0) {
928 nvlist_free(*config);
936 kmem_free(label, sizeof (vdev_label_t));
937 (void) ldi_close(vd_lh, FREAD, kcred);
939 error = SET_ERROR(EIDRM);