Merge illumos-gate
[unleashed/lotheac.git] / kernel / fs / zfs / vdev_disk.c
blob2d431373ce1d6276ae39b661880164c7befe8dc5
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, 2018 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;
61 * Bypass the devid when opening a disk vdev.
62 * There have been issues where the devids of several devices were shuffled,
63 * causing pool open failures. Note, that this flag is intended to be used
64 * for pool recovery only.
66 * Note that if a pool is imported with the devids bypassed, all its vdevs will
67 * cease storing devid information permanently. In practice, the devid is rarely
68 * useful as vdev paths do not tend to change unless the hardware is
69 * reconfigured. That said, if the paths do change and a pool fails to open
70 * automatically at boot, a simple zpool import should re-scan the paths and fix
71 * the issue.
73 boolean_t vdev_disk_bypass_devid = B_FALSE;
75 static void
76 vdev_disk_alloc(vdev_t *vd)
78 vdev_disk_t *dvd;
80 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
82 * Create the LDI event callback list.
84 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
85 offsetof(vdev_disk_ldi_cb_t, lcb_next));
88 static void
89 vdev_disk_free(vdev_t *vd)
91 vdev_disk_t *dvd = vd->vdev_tsd;
92 vdev_disk_ldi_cb_t *lcb;
94 if (dvd == NULL)
95 return;
98 * We have already closed the LDI handle. Clean up the LDI event
99 * callbacks and free vd->vdev_tsd.
101 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
102 list_remove(&dvd->vd_ldi_cbs, lcb);
103 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
104 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
106 list_destroy(&dvd->vd_ldi_cbs);
107 kmem_free(dvd, sizeof (vdev_disk_t));
108 vd->vdev_tsd = NULL;
111 /* ARGSUSED */
112 static int
113 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
114 void *ev_data)
116 vdev_t *vd = (vdev_t *)arg;
117 vdev_disk_t *dvd = vd->vdev_tsd;
120 * Ignore events other than offline.
122 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
123 return (LDI_EV_SUCCESS);
126 * All LDI handles must be closed for the state change to succeed, so
127 * call on vdev_disk_close() to do this.
129 * We inform vdev_disk_close that it is being called from offline
130 * notify context so it will defer cleanup of LDI event callbacks and
131 * freeing of vd->vdev_tsd to the offline finalize or a reopen.
133 dvd->vd_ldi_offline = B_TRUE;
134 vdev_disk_close(vd);
137 * Now that the device is closed, request that the spa_async_thread
138 * mark the device as REMOVED and notify FMA of the removal.
140 zfs_post_remove(vd->vdev_spa, vd);
141 vd->vdev_remove_wanted = B_TRUE;
142 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
144 return (LDI_EV_SUCCESS);
147 /* ARGSUSED */
148 static void
149 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
150 int ldi_result, void *arg, void *ev_data)
152 vdev_t *vd = (vdev_t *)arg;
155 * Ignore events other than offline.
157 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
158 return;
161 * We have already closed the LDI handle in notify.
162 * Clean up the LDI event callbacks and free vd->vdev_tsd.
164 vdev_disk_free(vd);
167 * Request that the vdev be reopened if the offline state change was
168 * unsuccessful.
170 if (ldi_result != LDI_EV_SUCCESS) {
171 vd->vdev_probe_wanted = B_TRUE;
172 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
176 static ldi_ev_callback_t vdev_disk_off_callb = {
177 .cb_vers = LDI_EV_CB_VERS,
178 .cb_notify = vdev_disk_off_notify,
179 .cb_finalize = vdev_disk_off_finalize
182 /* ARGSUSED */
183 static void
184 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
185 int ldi_result, void *arg, void *ev_data)
187 vdev_t *vd = (vdev_t *)arg;
190 * Ignore events other than degrade.
192 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
193 return;
196 * Degrade events always succeed. Mark the vdev as degraded.
197 * This status is purely informative for the user.
199 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
202 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
203 .cb_vers = LDI_EV_CB_VERS,
204 .cb_notify = NULL,
205 .cb_finalize = vdev_disk_dgrd_finalize
208 static void
209 vdev_disk_hold(vdev_t *vd)
211 ddi_devid_t devid;
212 char *minor;
214 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
217 * We must have a pathname, and it must be absolute.
219 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
220 return;
223 * Only prefetch path and devid info if the device has
224 * never been opened.
226 if (vd->vdev_tsd != NULL)
227 return;
229 if (vd->vdev_wholedisk == -1ULL) {
230 size_t len = strlen(vd->vdev_path) + 3;
231 char *buf = kmem_alloc(len, KM_SLEEP);
233 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
235 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
236 kmem_free(buf, len);
239 if (vd->vdev_name_vp == NULL)
240 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
242 if (vd->vdev_devid != NULL &&
243 ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
244 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
245 ddi_devid_str_free(minor);
246 ddi_devid_free(devid);
250 static void
251 vdev_disk_rele(vdev_t *vd)
253 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
255 if (vd->vdev_name_vp) {
256 VN_RELE_ASYNC(vd->vdev_name_vp,
257 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
258 vd->vdev_name_vp = NULL;
260 if (vd->vdev_devid_vp) {
261 VN_RELE_ASYNC(vd->vdev_devid_vp,
262 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
263 vd->vdev_devid_vp = NULL;
268 * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when
269 * even a fallback to DKIOCGMEDIAINFO fails.
271 #ifdef DEBUG
272 #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__)
273 #else
274 #define VDEV_DEBUG(...) /* Nothing... */
275 #endif
277 static int
278 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
279 uint64_t *ashift)
281 spa_t *spa = vd->vdev_spa;
282 vdev_disk_t *dvd = vd->vdev_tsd;
283 ldi_ev_cookie_t ecookie;
284 vdev_disk_ldi_cb_t *lcb;
285 union {
286 struct dk_minfo_ext ude;
287 struct dk_minfo ud;
288 } dks;
289 struct dk_minfo_ext *dkmext = &dks.ude;
290 struct dk_minfo *dkm = &dks.ud;
291 int error;
292 dev_t dev;
293 int otyp;
294 boolean_t validate_devid = B_FALSE;
295 ddi_devid_t devid;
296 uint64_t capacity = 0, blksz = 0, pbsize;
299 * We must have a pathname, and it must be absolute.
301 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
302 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
303 return (SET_ERROR(EINVAL));
307 * Reopen the device if it's not currently open. Otherwise,
308 * just update the physical size of the device.
310 if (dvd != NULL) {
311 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) {
313 * If we are opening a device in its offline notify
314 * context, the LDI handle was just closed. Clean
315 * up the LDI event callbacks and free vd->vdev_tsd.
317 vdev_disk_free(vd);
318 } else {
319 ASSERT(vd->vdev_reopening);
320 goto skip_open;
325 * Create vd->vdev_tsd.
327 vdev_disk_alloc(vd);
328 dvd = vd->vdev_tsd;
331 * Allow bypassing the devid.
333 if (vd->vdev_devid != NULL && vdev_disk_bypass_devid) {
334 vdev_dbgmsg(vd, "vdev_disk_open, devid %s bypassed",
335 vd->vdev_devid);
336 spa_strfree(vd->vdev_devid);
337 vd->vdev_devid = NULL;
341 * When opening a disk device, we want to preserve the user's original
342 * intent. We always want to open the device by the path the user gave
343 * us, even if it is one of multiple paths to the same device. But we
344 * also want to be able to survive disks being removed/recabled.
345 * Therefore the sequence of opening devices is:
347 * 1. Try opening the device by path. For legacy pools without the
348 * 'whole_disk' property, attempt to fix the path by appending 's0'.
350 * 2. If the devid of the device matches the stored value, return
351 * success.
353 * 3. Otherwise, the device may have moved. Try opening the device
354 * by the devid instead.
356 if (vd->vdev_devid != NULL) {
357 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
358 &dvd->vd_minor) != 0) {
359 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
360 vdev_dbgmsg(vd, "vdev_disk_open: invalid "
361 "vdev_devid '%s'", vd->vdev_devid);
362 return (SET_ERROR(EINVAL));
366 error = EINVAL; /* presume failure */
368 if (vd->vdev_path != NULL) {
370 if (vd->vdev_wholedisk == -1ULL) {
371 size_t len = strlen(vd->vdev_path) + 3;
372 char *buf = kmem_alloc(len, KM_SLEEP);
374 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
376 error = ldi_open_by_name(buf, spa_mode(spa), kcred,
377 &dvd->vd_lh, zfs_li);
378 if (error == 0) {
379 spa_strfree(vd->vdev_path);
380 vd->vdev_path = buf;
381 vd->vdev_wholedisk = 1ULL;
382 } else {
383 kmem_free(buf, len);
388 * If we have not yet opened the device, try to open it by the
389 * specified path.
391 if (error != 0) {
392 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
393 kcred, &dvd->vd_lh, zfs_li);
397 * Compare the devid to the stored value.
399 if (error == 0 && vd->vdev_devid != NULL &&
400 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
401 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
403 * A mismatch here is unexpected, log it.
405 char *devid_str = ddi_devid_str_encode(devid,
406 dvd->vd_minor);
407 vdev_dbgmsg(vd, "vdev_disk_open: devid "
408 "mismatch: %s != %s", vd->vdev_devid,
409 devid_str);
410 cmn_err(CE_NOTE, "vdev_disk_open %s: devid "
411 "mismatch: %s != %s", vd->vdev_path,
412 vd->vdev_devid, devid_str);
413 ddi_devid_str_free(devid_str);
415 error = SET_ERROR(EINVAL);
416 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
417 kcred);
418 dvd->vd_lh = NULL;
420 ddi_devid_free(devid);
424 * If we succeeded in opening the device, but 'vdev_wholedisk'
425 * is not yet set, then this must be a slice.
427 if (error == 0 && vd->vdev_wholedisk == -1ULL)
428 vd->vdev_wholedisk = 0;
432 * If we were unable to open by path, or the devid check fails, open by
433 * devid instead.
435 if (error != 0 && vd->vdev_devid != NULL) {
436 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
437 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
438 if (error != 0) {
439 vdev_dbgmsg(vd, "Failed to open by devid (%s)",
440 vd->vdev_devid);
445 * If all else fails, then try opening by physical path (if available)
446 * or the logical path (if we failed due to the devid check). While not
447 * as reliable as the devid, this will give us something, and the higher
448 * level vdev validation will prevent us from opening the wrong device.
450 if (error) {
451 if (vd->vdev_devid != NULL)
452 validate_devid = B_TRUE;
454 if (vd->vdev_physpath != NULL &&
455 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
456 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
457 kcred, &dvd->vd_lh, zfs_li);
460 * Note that we don't support the legacy auto-wholedisk support
461 * as above. This hasn't been used in a very long time and we
462 * don't need to propagate its oddities to this edge condition.
464 if (error && vd->vdev_path != NULL)
465 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
466 kcred, &dvd->vd_lh, zfs_li);
469 if (error) {
470 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
471 vdev_dbgmsg(vd, "vdev_disk_open: failed to open [error=%d]",
472 error);
473 return (error);
477 * Now that the device has been successfully opened, update the devid
478 * if necessary.
480 if (validate_devid && spa_writeable(spa) &&
481 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
482 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
483 char *vd_devid;
485 vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
486 vdev_dbgmsg(vd, "vdev_disk_open: update devid from "
487 "'%s' to '%s'", vd->vdev_devid, vd_devid);
488 cmn_err(CE_NOTE, "vdev_disk_open %s: update devid "
489 "from '%s' to '%s'", vd->vdev_path != NULL ?
490 vd->vdev_path : "?", vd->vdev_devid, vd_devid);
491 spa_strfree(vd->vdev_devid);
492 vd->vdev_devid = spa_strdup(vd_devid);
493 ddi_devid_str_free(vd_devid);
495 ddi_devid_free(devid);
499 * Once a device is opened, verify that the physical device path (if
500 * available) is up to date.
502 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
503 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
504 char *physpath, *minorname;
506 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
507 minorname = NULL;
508 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
509 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
510 (vd->vdev_physpath == NULL ||
511 strcmp(vd->vdev_physpath, physpath) != 0)) {
512 if (vd->vdev_physpath)
513 spa_strfree(vd->vdev_physpath);
514 (void) strlcat(physpath, ":", MAXPATHLEN);
515 (void) strlcat(physpath, minorname, MAXPATHLEN);
516 vd->vdev_physpath = spa_strdup(physpath);
518 if (minorname)
519 kmem_free(minorname, strlen(minorname) + 1);
520 kmem_free(physpath, MAXPATHLEN);
524 * Register callbacks for the LDI offline event.
526 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
527 LDI_EV_SUCCESS) {
528 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
529 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
530 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
531 &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
535 * Register callbacks for the LDI degrade event.
537 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
538 LDI_EV_SUCCESS) {
539 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
540 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
541 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
542 &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
544 skip_open:
546 * Determine the actual size of the device.
548 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
549 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
550 vdev_dbgmsg(vd, "vdev_disk_open: failed to get size");
551 return (SET_ERROR(EINVAL));
554 *max_psize = *psize;
557 * Determine the device's minimum transfer size.
558 * If the ioctl isn't supported, assume DEV_BSIZE.
560 if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
561 (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
562 capacity = dkmext->dki_capacity - 1;
563 blksz = dkmext->dki_lbsize;
564 pbsize = dkmext->dki_pbsize;
565 } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
566 (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
567 VDEV_DEBUG(
568 "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n",
569 vd->vdev_path);
570 capacity = dkm->dki_capacity - 1;
571 blksz = dkm->dki_lbsize;
572 pbsize = blksz;
573 } else {
574 VDEV_DEBUG("vdev_disk_open(\"%s\"): "
575 "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n",
576 vd->vdev_path, error);
577 pbsize = DEV_BSIZE;
580 *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
582 if (vd->vdev_wholedisk == 1) {
583 int wce = 1;
585 if (error == 0) {
587 * If we have the capability to expand, we'd have
588 * found out via success from DKIOCGMEDIAINFO{,EXT}.
589 * Adjust max_psize upward accordingly since we know
590 * we own the whole disk now.
592 *max_psize = capacity * blksz;
596 * Since we own the whole disk, try to enable disk write
597 * caching. We ignore errors because it's OK if we can't do it.
599 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
600 FKIOCTL, kcred, NULL);
604 * Clear the nowritecache bit, so that on a vdev_reopen() we will
605 * try again.
607 vd->vdev_nowritecache = B_FALSE;
609 return (0);
612 static void
613 vdev_disk_close(vdev_t *vd)
615 vdev_disk_t *dvd = vd->vdev_tsd;
617 if (vd->vdev_reopening || dvd == NULL)
618 return;
620 if (dvd->vd_minor != NULL) {
621 ddi_devid_str_free(dvd->vd_minor);
622 dvd->vd_minor = NULL;
625 if (dvd->vd_devid != NULL) {
626 ddi_devid_free(dvd->vd_devid);
627 dvd->vd_devid = NULL;
630 if (dvd->vd_lh != NULL) {
631 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
632 dvd->vd_lh = NULL;
635 vd->vdev_delayed_close = B_FALSE;
637 * If we closed the LDI handle due to an offline notify from LDI,
638 * don't free vd->vdev_tsd or unregister the callbacks here;
639 * the offline finalize callback or a reopen will take care of it.
641 if (dvd->vd_ldi_offline)
642 return;
644 vdev_disk_free(vd);
648 vdev_disk_physio(vdev_t *vd, caddr_t data,
649 size_t size, uint64_t offset, int flags, boolean_t isdump)
651 vdev_disk_t *dvd = vd->vdev_tsd;
654 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
655 * Nothing to be done here but return failure.
657 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
658 return (EIO);
660 ASSERT(vd->vdev_ops == &vdev_disk_ops);
663 * If in the context of an active crash dump, use the ldi_dump(9F)
664 * call instead of ldi_strategy(9F) as usual.
666 if (isdump) {
667 ASSERT3P(dvd, !=, NULL);
668 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
669 lbtodb(size)));
672 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
676 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
677 size_t size, uint64_t offset, int flags)
679 buf_t *bp;
680 int error = 0;
682 if (vd_lh == NULL)
683 return (SET_ERROR(EINVAL));
685 ASSERT(flags & B_READ || flags & B_WRITE);
687 bp = getrbuf(KM_SLEEP);
688 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
689 bp->b_bcount = size;
690 bp->b_un.b_addr = (void *)data;
691 bp->b_lblkno = lbtodb(offset);
692 bp->b_bufsize = size;
694 error = ldi_strategy(vd_lh, bp);
695 ASSERT(error == 0);
696 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
697 error = SET_ERROR(EIO);
698 freerbuf(bp);
700 return (error);
703 static int
704 vdev_disk_io_intr(buf_t *bp)
706 vdev_buf_t *vb = (vdev_buf_t *)bp;
707 zio_t *zio = vb->vb_io;
710 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
711 * Rather than teach the rest of the stack about other error
712 * possibilities (EFAULT, etc), we normalize the error value here.
714 zio->io_error = (geterror(bp) != 0 ? EIO : 0);
716 if (zio->io_error == 0 && bp->b_resid != 0)
717 zio->io_error = SET_ERROR(EIO);
719 if (zio->io_type == ZIO_TYPE_READ) {
720 abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
721 } else {
722 abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
725 kmem_free(vb, sizeof (vdev_buf_t));
727 zio_delay_interrupt(zio);
728 return (0);
731 static void
732 vdev_disk_ioctl_free(zio_t *zio)
734 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
737 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
738 vdev_disk_ioctl_free,
739 zio_vsd_default_cksum_report
742 static void
743 vdev_disk_ioctl_done(void *zio_arg, int error)
745 zio_t *zio = zio_arg;
747 zio->io_error = error;
749 zio_interrupt(zio);
752 static void
753 vdev_disk_io_start(zio_t *zio)
755 vdev_t *vd = zio->io_vd;
756 vdev_disk_t *dvd = vd->vdev_tsd;
757 vdev_buf_t *vb;
758 struct dk_callback *dkc;
759 buf_t *bp;
760 int error;
763 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
764 * Nothing to be done here but return failure.
766 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
767 zio->io_error = ENXIO;
768 zio_interrupt(zio);
769 return;
772 if (zio->io_type == ZIO_TYPE_IOCTL) {
773 /* XXPOLICY */
774 if (!vdev_readable(vd)) {
775 zio->io_error = SET_ERROR(ENXIO);
776 zio_interrupt(zio);
777 return;
780 switch (zio->io_cmd) {
782 case DKIOCFLUSHWRITECACHE:
784 if (zfs_nocacheflush)
785 break;
787 if (vd->vdev_nowritecache) {
788 zio->io_error = SET_ERROR(ENOTSUP);
789 break;
792 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
793 zio->io_vsd_ops = &vdev_disk_vsd_ops;
795 dkc->dkc_callback = vdev_disk_ioctl_done;
796 dkc->dkc_flag = FLUSH_VOLATILE;
797 dkc->dkc_cookie = zio;
799 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
800 (uintptr_t)dkc, FKIOCTL, kcred, NULL);
802 if (error == 0) {
804 * The ioctl will be done asychronously,
805 * and will call vdev_disk_ioctl_done()
806 * upon completion.
808 return;
811 zio->io_error = error;
813 break;
815 default:
816 zio->io_error = SET_ERROR(ENOTSUP);
819 zio_execute(zio);
820 return;
823 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
824 zio->io_target_timestamp = zio_handle_io_delay(zio);
826 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
828 vb->vb_io = zio;
829 bp = &vb->vb_buf;
831 bioinit(bp);
832 bp->b_flags = B_BUSY | B_NOCACHE |
833 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
834 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
835 bp->b_flags |= B_FAILFAST;
836 bp->b_bcount = zio->io_size;
838 if (zio->io_type == ZIO_TYPE_READ) {
839 bp->b_un.b_addr =
840 abd_borrow_buf(zio->io_abd, zio->io_size);
841 } else {
842 bp->b_un.b_addr =
843 abd_borrow_buf_copy(zio->io_abd, zio->io_size);
846 bp->b_lblkno = lbtodb(zio->io_offset);
847 bp->b_bufsize = zio->io_size;
848 bp->b_iodone = vdev_disk_io_intr;
850 /* ldi_strategy() will return non-zero only on programming errors */
851 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
854 static void
855 vdev_disk_io_done(zio_t *zio)
857 vdev_t *vd = zio->io_vd;
860 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
861 * the device has been removed. If this is the case, then we trigger an
862 * asynchronous removal of the device. Otherwise, probe the device and
863 * make sure it's still accessible.
865 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
866 vdev_disk_t *dvd = vd->vdev_tsd;
867 int state = DKIO_NONE;
869 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
870 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
872 * We post the resource as soon as possible, instead of
873 * when the async removal actually happens, because the
874 * DE is using this information to discard previous I/O
875 * errors.
877 zfs_post_remove(zio->io_spa, vd);
878 vd->vdev_remove_wanted = B_TRUE;
879 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
880 } else if (!vd->vdev_delayed_close) {
881 vd->vdev_delayed_close = B_TRUE;
886 vdev_ops_t vdev_disk_ops = {
887 vdev_disk_open,
888 vdev_disk_close,
889 vdev_default_asize,
890 vdev_disk_io_start,
891 vdev_disk_io_done,
892 NULL,
893 vdev_disk_hold,
894 vdev_disk_rele,
895 NULL,
896 vdev_default_xlate,
897 VDEV_TYPE_DISK, /* name of this vdev type */
898 B_TRUE /* leaf vdev */
902 * Given the root disk device devid or pathname, read the label from
903 * the device, and construct a configuration nvlist.
906 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
908 ldi_handle_t vd_lh;
909 vdev_label_t *label;
910 uint64_t s, size;
911 int l;
912 ddi_devid_t tmpdevid;
913 int error = -1;
914 char *minor_name;
917 * Read the device label and build the nvlist.
919 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
920 &minor_name) == 0) {
921 error = ldi_open_by_devid(tmpdevid, minor_name,
922 FREAD, kcred, &vd_lh, zfs_li);
923 ddi_devid_free(tmpdevid);
924 ddi_devid_str_free(minor_name);
927 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
928 zfs_li)))
929 return (error);
931 if (ldi_get_size(vd_lh, &s)) {
932 (void) ldi_close(vd_lh, FREAD, kcred);
933 return (SET_ERROR(EIO));
936 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
937 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
939 *config = NULL;
940 for (l = 0; l < VDEV_LABELS; l++) {
941 uint64_t offset, state, txg = 0;
943 /* read vdev label */
944 offset = vdev_label_offset(size, l, 0);
945 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
946 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
947 continue;
949 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
950 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
951 *config = NULL;
952 continue;
955 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
956 &state) != 0 || state >= POOL_STATE_DESTROYED) {
957 nvlist_free(*config);
958 *config = NULL;
959 continue;
962 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
963 &txg) != 0 || txg == 0) {
964 nvlist_free(*config);
965 *config = NULL;
966 continue;
969 break;
972 kmem_free(label, sizeof (vdev_label_t));
973 (void) ldi_close(vd_lh, FREAD, kcred);
974 if (*config == NULL)
975 error = SET_ERROR(EIDRM);
977 return (error);