9963 Seperate tunable for disabling ZIL vdev flush
[unleashed.git] / usr / src / uts / common / fs / zfs / vdev_disk.c
blob65d7864a526f6464b10ac6434abbde4cf64f8045
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24 * Copyright 2016 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/abd.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/zio.h>
36 #include <sys/sunldi.h>
37 #include <sys/efi_partition.h>
38 #include <sys/fm/fs/zfs.h>
41 * Tunable parameter for debugging or performance analysis. Setting this
42 * will cause pool corruption on power loss if a volatile out-of-order
43 * write cache is enabled.
45 boolean_t zfs_nocacheflush = B_FALSE;
48 * Virtual device vector for disks.
51 extern ldi_ident_t zfs_li;
53 static void vdev_disk_close(vdev_t *);
55 typedef struct vdev_disk_ldi_cb {
56 list_node_t lcb_next;
57 ldi_callback_id_t lcb_id;
58 } vdev_disk_ldi_cb_t;
60 static void
61 vdev_disk_alloc(vdev_t *vd)
63 vdev_disk_t *dvd;
65 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
67 * Create the LDI event callback list.
69 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
70 offsetof(vdev_disk_ldi_cb_t, lcb_next));
73 static void
74 vdev_disk_free(vdev_t *vd)
76 vdev_disk_t *dvd = vd->vdev_tsd;
77 vdev_disk_ldi_cb_t *lcb;
79 if (dvd == NULL)
80 return;
83 * We have already closed the LDI handle. Clean up the LDI event
84 * callbacks and free vd->vdev_tsd.
86 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
87 list_remove(&dvd->vd_ldi_cbs, lcb);
88 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
89 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
91 list_destroy(&dvd->vd_ldi_cbs);
92 kmem_free(dvd, sizeof (vdev_disk_t));
93 vd->vdev_tsd = NULL;
96 /* ARGSUSED */
97 static int
98 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
99 void *ev_data)
101 vdev_t *vd = (vdev_t *)arg;
102 vdev_disk_t *dvd = vd->vdev_tsd;
105 * Ignore events other than offline.
107 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
108 return (LDI_EV_SUCCESS);
111 * All LDI handles must be closed for the state change to succeed, so
112 * call on vdev_disk_close() to do this.
114 * We inform vdev_disk_close that it is being called from offline
115 * notify context so it will defer cleanup of LDI event callbacks and
116 * freeing of vd->vdev_tsd to the offline finalize or a reopen.
118 dvd->vd_ldi_offline = B_TRUE;
119 vdev_disk_close(vd);
122 * Now that the device is closed, request that the spa_async_thread
123 * mark the device as REMOVED and notify FMA of the removal.
125 zfs_post_remove(vd->vdev_spa, vd);
126 vd->vdev_remove_wanted = B_TRUE;
127 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
129 return (LDI_EV_SUCCESS);
132 /* ARGSUSED */
133 static void
134 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
135 int ldi_result, void *arg, void *ev_data)
137 vdev_t *vd = (vdev_t *)arg;
140 * Ignore events other than offline.
142 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
143 return;
146 * We have already closed the LDI handle in notify.
147 * Clean up the LDI event callbacks and free vd->vdev_tsd.
149 vdev_disk_free(vd);
152 * Request that the vdev be reopened if the offline state change was
153 * unsuccessful.
155 if (ldi_result != LDI_EV_SUCCESS) {
156 vd->vdev_probe_wanted = B_TRUE;
157 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
161 static ldi_ev_callback_t vdev_disk_off_callb = {
162 .cb_vers = LDI_EV_CB_VERS,
163 .cb_notify = vdev_disk_off_notify,
164 .cb_finalize = vdev_disk_off_finalize
167 /* ARGSUSED */
168 static void
169 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
170 int ldi_result, void *arg, void *ev_data)
172 vdev_t *vd = (vdev_t *)arg;
175 * Ignore events other than degrade.
177 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
178 return;
181 * Degrade events always succeed. Mark the vdev as degraded.
182 * This status is purely informative for the user.
184 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
187 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
188 .cb_vers = LDI_EV_CB_VERS,
189 .cb_notify = NULL,
190 .cb_finalize = vdev_disk_dgrd_finalize
193 static void
194 vdev_disk_hold(vdev_t *vd)
196 ddi_devid_t devid;
197 char *minor;
199 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
202 * We must have a pathname, and it must be absolute.
204 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
205 return;
208 * Only prefetch path and devid info if the device has
209 * never been opened.
211 if (vd->vdev_tsd != NULL)
212 return;
214 if (vd->vdev_wholedisk == -1ULL) {
215 size_t len = strlen(vd->vdev_path) + 3;
216 char *buf = kmem_alloc(len, KM_SLEEP);
218 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
220 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
221 kmem_free(buf, len);
224 if (vd->vdev_name_vp == NULL)
225 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
227 if (vd->vdev_devid != NULL &&
228 ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
229 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
230 ddi_devid_str_free(minor);
231 ddi_devid_free(devid);
235 static void
236 vdev_disk_rele(vdev_t *vd)
238 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
240 if (vd->vdev_name_vp) {
241 VN_RELE_ASYNC(vd->vdev_name_vp,
242 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
243 vd->vdev_name_vp = NULL;
245 if (vd->vdev_devid_vp) {
246 VN_RELE_ASYNC(vd->vdev_devid_vp,
247 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
248 vd->vdev_devid_vp = NULL;
253 * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when
254 * even a fallback to DKIOCGMEDIAINFO fails.
256 #ifdef DEBUG
257 #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__)
258 #else
259 #define VDEV_DEBUG(...) /* Nothing... */
260 #endif
262 static int
263 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
264 uint64_t *ashift)
266 spa_t *spa = vd->vdev_spa;
267 vdev_disk_t *dvd = vd->vdev_tsd;
268 ldi_ev_cookie_t ecookie;
269 vdev_disk_ldi_cb_t *lcb;
270 union {
271 struct dk_minfo_ext ude;
272 struct dk_minfo ud;
273 } dks;
274 struct dk_minfo_ext *dkmext = &dks.ude;
275 struct dk_minfo *dkm = &dks.ud;
276 int error;
277 dev_t dev;
278 int otyp;
279 boolean_t validate_devid = B_FALSE;
280 ddi_devid_t devid;
281 uint64_t capacity = 0, blksz = 0, pbsize;
284 * We must have a pathname, and it must be absolute.
286 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
287 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
288 return (SET_ERROR(EINVAL));
292 * Reopen the device if it's not currently open. Otherwise,
293 * just update the physical size of the device.
295 if (dvd != NULL) {
296 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
298 * If we are opening a device in its offline notify
299 * context, the LDI handle was just closed. Clean
300 * up the LDI event callbacks and free vd->vdev_tsd.
302 vdev_disk_free(vd);
303 } else {
304 ASSERT(vd->vdev_reopening);
305 goto skip_open;
310 * Create vd->vdev_tsd.
312 vdev_disk_alloc(vd);
313 dvd = vd->vdev_tsd;
316 * When opening a disk device, we want to preserve the user's original
317 * intent. We always want to open the device by the path the user gave
318 * us, even if it is one of multiple paths to the same device. But we
319 * also want to be able to survive disks being removed/recabled.
320 * Therefore the sequence of opening devices is:
322 * 1. Try opening the device by path. For legacy pools without the
323 * 'whole_disk' property, attempt to fix the path by appending 's0'.
325 * 2. If the devid of the device matches the stored value, return
326 * success.
328 * 3. Otherwise, the device may have moved. Try opening the device
329 * by the devid instead.
331 if (vd->vdev_devid != NULL) {
332 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
333 &dvd->vd_minor) != 0) {
334 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
335 vdev_dbgmsg(vd, "vdev_disk_open: invalid "
336 "vdev_devid '%s'", vd->vdev_devid);
337 return (SET_ERROR(EINVAL));
341 error = EINVAL; /* presume failure */
343 if (vd->vdev_path != NULL) {
345 if (vd->vdev_wholedisk == -1ULL) {
346 size_t len = strlen(vd->vdev_path) + 3;
347 char *buf = kmem_alloc(len, KM_SLEEP);
349 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
351 error = ldi_open_by_name(buf, spa_mode(spa), kcred,
352 &dvd->vd_lh, zfs_li);
353 if (error == 0) {
354 spa_strfree(vd->vdev_path);
355 vd->vdev_path = buf;
356 vd->vdev_wholedisk = 1ULL;
357 } else {
358 kmem_free(buf, len);
363 * If we have not yet opened the device, try to open it by the
364 * specified path.
366 if (error != 0) {
367 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
368 kcred, &dvd->vd_lh, zfs_li);
372 * Compare the devid to the stored value.
374 if (error == 0 && vd->vdev_devid != NULL &&
375 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
376 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
377 error = SET_ERROR(EINVAL);
378 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
379 kcred);
380 dvd->vd_lh = NULL;
382 ddi_devid_free(devid);
386 * If we succeeded in opening the device, but 'vdev_wholedisk'
387 * is not yet set, then this must be a slice.
389 if (error == 0 && vd->vdev_wholedisk == -1ULL)
390 vd->vdev_wholedisk = 0;
394 * If we were unable to open by path, or the devid check fails, open by
395 * devid instead.
397 if (error != 0 && vd->vdev_devid != NULL) {
398 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
399 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
403 * If all else fails, then try opening by physical path (if available)
404 * or the logical path (if we failed due to the devid check). While not
405 * as reliable as the devid, this will give us something, and the higher
406 * level vdev validation will prevent us from opening the wrong device.
408 if (error) {
409 if (vd->vdev_devid != NULL)
410 validate_devid = B_TRUE;
412 if (vd->vdev_physpath != NULL &&
413 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
414 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
415 kcred, &dvd->vd_lh, zfs_li);
418 * Note that we don't support the legacy auto-wholedisk support
419 * as above. This hasn't been used in a very long time and we
420 * don't need to propagate its oddities to this edge condition.
422 if (error && vd->vdev_path != NULL)
423 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
424 kcred, &dvd->vd_lh, zfs_li);
427 if (error) {
428 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
429 vdev_dbgmsg(vd, "vdev_disk_open: failed to open [error=%d]",
430 error);
431 return (error);
435 * Now that the device has been successfully opened, update the devid
436 * if necessary.
438 if (validate_devid && spa_writeable(spa) &&
439 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
440 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
441 char *vd_devid;
443 vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
444 vdev_dbgmsg(vd, "vdev_disk_open: update devid from "
445 "'%s' to '%s'", vd->vdev_devid, vd_devid);
446 spa_strfree(vd->vdev_devid);
447 vd->vdev_devid = spa_strdup(vd_devid);
448 ddi_devid_str_free(vd_devid);
450 ddi_devid_free(devid);
454 * Once a device is opened, verify that the physical device path (if
455 * available) is up to date.
457 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
458 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
459 char *physpath, *minorname;
461 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
462 minorname = NULL;
463 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
464 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
465 (vd->vdev_physpath == NULL ||
466 strcmp(vd->vdev_physpath, physpath) != 0)) {
467 if (vd->vdev_physpath)
468 spa_strfree(vd->vdev_physpath);
469 (void) strlcat(physpath, ":", MAXPATHLEN);
470 (void) strlcat(physpath, minorname, MAXPATHLEN);
471 vd->vdev_physpath = spa_strdup(physpath);
473 if (minorname)
474 kmem_free(minorname, strlen(minorname) + 1);
475 kmem_free(physpath, MAXPATHLEN);
479 * Register callbacks for the LDI offline event.
481 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
482 LDI_EV_SUCCESS) {
483 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
484 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
485 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
486 &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
490 * Register callbacks for the LDI degrade event.
492 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
493 LDI_EV_SUCCESS) {
494 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
495 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
496 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
497 &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
499 skip_open:
501 * Determine the actual size of the device.
503 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
504 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
505 vdev_dbgmsg(vd, "vdev_disk_open: failed to get size");
506 return (SET_ERROR(EINVAL));
509 *max_psize = *psize;
512 * Determine the device's minimum transfer size.
513 * If the ioctl isn't supported, assume DEV_BSIZE.
515 if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
516 (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
517 capacity = dkmext->dki_capacity - 1;
518 blksz = dkmext->dki_lbsize;
519 pbsize = dkmext->dki_pbsize;
520 } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
521 (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
522 VDEV_DEBUG(
523 "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
524 vd->vdev_path);
525 capacity = dkm->dki_capacity - 1;
526 blksz = dkm->dki_lbsize;
527 pbsize = blksz;
528 } else {
529 VDEV_DEBUG("vdev_disk_open(\"%s\"): "
530 "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
531 vd->vdev_path, error);
532 pbsize = DEV_BSIZE;
535 *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
537 if (vd->vdev_wholedisk == 1) {
538 int wce = 1;
540 if (error == 0) {
542 * If we have the capability to expand, we'd have
543 * found out via success from DKIOCGMEDIAINFO{,EXT}.
544 * Adjust max_psize upward accordingly since we know
545 * we own the whole disk now.
547 *max_psize = capacity * blksz;
551 * Since we own the whole disk, try to enable disk write
552 * caching. We ignore errors because it's OK if we can't do it.
554 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
555 FKIOCTL, kcred, NULL);
559 * Clear the nowritecache bit, so that on a vdev_reopen() we will
560 * try again.
562 vd->vdev_nowritecache = B_FALSE;
564 return (0);
567 static void
568 vdev_disk_close(vdev_t *vd)
570 vdev_disk_t *dvd = vd->vdev_tsd;
572 if (vd->vdev_reopening || dvd == NULL)
573 return;
575 if (dvd->vd_minor != NULL) {
576 ddi_devid_str_free(dvd->vd_minor);
577 dvd->vd_minor = NULL;
580 if (dvd->vd_devid != NULL) {
581 ddi_devid_free(dvd->vd_devid);
582 dvd->vd_devid = NULL;
585 if (dvd->vd_lh != NULL) {
586 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
587 dvd->vd_lh = NULL;
590 vd->vdev_delayed_close = B_FALSE;
592 * If we closed the LDI handle due to an offline notify from LDI,
593 * don't free vd->vdev_tsd or unregister the callbacks here;
594 * the offline finalize callback or a reopen will take care of it.
596 if (dvd->vd_ldi_offline)
597 return;
599 vdev_disk_free(vd);
603 vdev_disk_physio(vdev_t *vd, caddr_t data,
604 size_t size, uint64_t offset, int flags, boolean_t isdump)
606 vdev_disk_t *dvd = vd->vdev_tsd;
609 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
610 * Nothing to be done here but return failure.
612 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
613 return (EIO);
615 ASSERT(vd->vdev_ops == &vdev_disk_ops);
618 * If in the context of an active crash dump, use the ldi_dump(9F)
619 * call instead of ldi_strategy(9F) as usual.
621 if (isdump) {
622 ASSERT3P(dvd, !=, NULL);
623 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
624 lbtodb(size)));
627 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
631 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
632 size_t size, uint64_t offset, int flags)
634 buf_t *bp;
635 int error = 0;
637 if (vd_lh == NULL)
638 return (SET_ERROR(EINVAL));
640 ASSERT(flags & B_READ || flags & B_WRITE);
642 bp = getrbuf(KM_SLEEP);
643 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
644 bp->b_bcount = size;
645 bp->b_un.b_addr = (void *)data;
646 bp->b_lblkno = lbtodb(offset);
647 bp->b_bufsize = size;
649 error = ldi_strategy(vd_lh, bp);
650 ASSERT(error == 0);
651 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
652 error = SET_ERROR(EIO);
653 freerbuf(bp);
655 return (error);
658 static void
659 vdev_disk_io_intr(buf_t *bp)
661 vdev_buf_t *vb = (vdev_buf_t *)bp;
662 zio_t *zio = vb->vb_io;
665 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
666 * Rather than teach the rest of the stack about other error
667 * possibilities (EFAULT, etc), we normalize the error value here.
669 zio->io_error = (geterror(bp) != 0 ? EIO : 0);
671 if (zio->io_error == 0 && bp->b_resid != 0)
672 zio->io_error = SET_ERROR(EIO);
674 if (zio->io_type == ZIO_TYPE_READ) {
675 abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
676 } else {
677 abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
680 kmem_free(vb, sizeof (vdev_buf_t));
682 zio_delay_interrupt(zio);
685 static void
686 vdev_disk_ioctl_free(zio_t *zio)
688 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
691 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
692 vdev_disk_ioctl_free,
693 zio_vsd_default_cksum_report
696 static void
697 vdev_disk_ioctl_done(void *zio_arg, int error)
699 zio_t *zio = zio_arg;
701 zio->io_error = error;
703 zio_interrupt(zio);
706 static void
707 vdev_disk_io_start(zio_t *zio)
709 vdev_t *vd = zio->io_vd;
710 vdev_disk_t *dvd = vd->vdev_tsd;
711 vdev_buf_t *vb;
712 struct dk_callback *dkc;
713 buf_t *bp;
714 int error;
717 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
718 * Nothing to be done here but return failure.
720 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
721 zio->io_error = ENXIO;
722 zio_interrupt(zio);
723 return;
726 if (zio->io_type == ZIO_TYPE_IOCTL) {
727 /* XXPOLICY */
728 if (!vdev_readable(vd)) {
729 zio->io_error = SET_ERROR(ENXIO);
730 zio_interrupt(zio);
731 return;
734 switch (zio->io_cmd) {
736 case DKIOCFLUSHWRITECACHE:
738 if (zfs_nocacheflush)
739 break;
741 if (vd->vdev_nowritecache) {
742 zio->io_error = SET_ERROR(ENOTSUP);
743 break;
746 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
747 zio->io_vsd_ops = &vdev_disk_vsd_ops;
749 dkc->dkc_callback = vdev_disk_ioctl_done;
750 dkc->dkc_flag = FLUSH_VOLATILE;
751 dkc->dkc_cookie = zio;
753 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
754 (uintptr_t)dkc, FKIOCTL, kcred, NULL);
756 if (error == 0) {
758 * The ioctl will be done asychronously,
759 * and will call vdev_disk_ioctl_done()
760 * upon completion.
762 return;
765 zio->io_error = error;
767 break;
769 default:
770 zio->io_error = SET_ERROR(ENOTSUP);
773 zio_execute(zio);
774 return;
777 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
778 zio->io_target_timestamp = zio_handle_io_delay(zio);
780 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
782 vb->vb_io = zio;
783 bp = &vb->vb_buf;
785 bioinit(bp);
786 bp->b_flags = B_BUSY | B_NOCACHE |
787 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
788 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
789 bp->b_flags |= B_FAILFAST;
790 bp->b_bcount = zio->io_size;
792 if (zio->io_type == ZIO_TYPE_READ) {
793 bp->b_un.b_addr =
794 abd_borrow_buf(zio->io_abd, zio->io_size);
795 } else {
796 bp->b_un.b_addr =
797 abd_borrow_buf_copy(zio->io_abd, zio->io_size);
800 bp->b_lblkno = lbtodb(zio->io_offset);
801 bp->b_bufsize = zio->io_size;
802 bp->b_iodone = (int (*)())vdev_disk_io_intr;
804 /* ldi_strategy() will return non-zero only on programming errors */
805 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
808 static void
809 vdev_disk_io_done(zio_t *zio)
811 vdev_t *vd = zio->io_vd;
814 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
815 * the device has been removed. If this is the case, then we trigger an
816 * asynchronous removal of the device. Otherwise, probe the device and
817 * make sure it's still accessible.
819 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
820 vdev_disk_t *dvd = vd->vdev_tsd;
821 int state = DKIO_NONE;
823 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
824 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
826 * We post the resource as soon as possible, instead of
827 * when the async removal actually happens, because the
828 * DE is using this information to discard previous I/O
829 * errors.
831 zfs_post_remove(zio->io_spa, vd);
832 vd->vdev_remove_wanted = B_TRUE;
833 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
834 } else if (!vd->vdev_delayed_close) {
835 vd->vdev_delayed_close = B_TRUE;
840 vdev_ops_t vdev_disk_ops = {
841 vdev_disk_open,
842 vdev_disk_close,
843 vdev_default_asize,
844 vdev_disk_io_start,
845 vdev_disk_io_done,
846 NULL,
847 vdev_disk_hold,
848 vdev_disk_rele,
849 NULL,
850 vdev_default_xlate,
851 VDEV_TYPE_DISK, /* name of this vdev type */
852 B_TRUE /* leaf vdev */
856 * Given the root disk device devid or pathname, read the label from
857 * the device, and construct a configuration nvlist.
860 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
862 ldi_handle_t vd_lh;
863 vdev_label_t *label;
864 uint64_t s, size;
865 int l;
866 ddi_devid_t tmpdevid;
867 int error = -1;
868 char *minor_name;
871 * Read the device label and build the nvlist.
873 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
874 &minor_name) == 0) {
875 error = ldi_open_by_devid(tmpdevid, minor_name,
876 FREAD, kcred, &vd_lh, zfs_li);
877 ddi_devid_free(tmpdevid);
878 ddi_devid_str_free(minor_name);
881 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
882 zfs_li)))
883 return (error);
885 if (ldi_get_size(vd_lh, &s)) {
886 (void) ldi_close(vd_lh, FREAD, kcred);
887 return (SET_ERROR(EIO));
890 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
891 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
893 *config = NULL;
894 for (l = 0; l < VDEV_LABELS; l++) {
895 uint64_t offset, state, txg = 0;
897 /* read vdev label */
898 offset = vdev_label_offset(size, l, 0);
899 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
900 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
901 continue;
903 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
904 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
905 *config = NULL;
906 continue;
909 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
910 &state) != 0 || state >= POOL_STATE_DESTROYED) {
911 nvlist_free(*config);
912 *config = NULL;
913 continue;
916 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
917 &txg) != 0 || txg == 0) {
918 nvlist_free(*config);
919 *config = NULL;
920 continue;
923 break;
926 kmem_free(label, sizeof (vdev_label_t));
927 (void) ldi_close(vd_lh, FREAD, kcred);
928 if (*config == NULL)
929 error = SET_ERROR(EIDRM);
931 return (error);