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) 2012, 2018 by Delphix. All rights reserved. 24 * Copyright 2016 Nexenta Systems, Inc. All rights reserved. 25 * Copyright 2019 Joyent, Inc. 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/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> 39 40 /* 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. 44 */ 45 boolean_t zfs_nocacheflush = B_FALSE; 46 47 /* 48 * Virtual device vector for disks. 49 */ 50 51 extern ldi_ident_t zfs_li; 52 53 static void vdev_disk_close(vdev_t *); 54 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; 59 60 /* 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. 65 * 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. 72 */ 73 boolean_t vdev_disk_bypass_devid = B_FALSE; 74 75 static void 76 vdev_disk_alloc(vdev_t *vd) 77 { 78 vdev_disk_t *dvd; 79 80 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 81 /* 82 * Create the LDI event callback list. 83 */ 84 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t), 85 offsetof(vdev_disk_ldi_cb_t, lcb_next)); 86 } 87 88 static void 89 vdev_disk_free(vdev_t *vd) 90 { 91 vdev_disk_t *dvd = vd->vdev_tsd; 92 vdev_disk_ldi_cb_t *lcb; 93 94 if (dvd == NULL) 95 return; 96 97 /* 98 * We have already closed the LDI handle. Clean up the LDI event 99 * callbacks and free vd->vdev_tsd. 100 */ 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)); 105 } 106 list_destroy(&dvd->vd_ldi_cbs); 107 kmem_free(dvd, sizeof (vdev_disk_t)); 108 vd->vdev_tsd = NULL; 109 } 110 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) 115 { 116 vdev_t *vd = (vdev_t *)arg; 117 vdev_disk_t *dvd = vd->vdev_tsd; 118 119 /* 120 * Ignore events other than offline. 121 */ 122 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0) 123 return (LDI_EV_SUCCESS); 124 125 /* 126 * All LDI handles must be closed for the state change to succeed, so 127 * call on vdev_disk_close() to do this. 128 * 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. 132 */ 133 dvd->vd_ldi_offline = B_TRUE; 134 vdev_disk_close(vd); 135 136 /* 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. 139 */ 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); 143 144 return (LDI_EV_SUCCESS); 145 } 146 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) 151 { 152 vdev_t *vd = (vdev_t *)arg; 153 154 /* 155 * Ignore events other than offline. 156 */ 157 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0) 158 return; 159 160 /* 161 * We have already closed the LDI handle in notify. 162 * Clean up the LDI event callbacks and free vd->vdev_tsd. 163 */ 164 vdev_disk_free(vd); 165 166 /* 167 * Request that the vdev be reopened if the offline state change was 168 * unsuccessful. 169 */ 170 if (ldi_result != LDI_EV_SUCCESS) { 171 vd->vdev_probe_wanted = B_TRUE; 172 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE); 173 } 174 } 175 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 180 }; 181 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) 186 { 187 vdev_t *vd = (vdev_t *)arg; 188 189 /* 190 * Ignore events other than degrade. 191 */ 192 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0) 193 return; 194 195 /* 196 * Degrade events always succeed. Mark the vdev as degraded. 197 * This status is purely informative for the user. 198 */ 199 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0); 200 } 201 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 206 }; 207 208 static void 209 vdev_disk_hold(vdev_t *vd) 210 { 211 ddi_devid_t devid; 212 char *minor; 213 214 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 215 216 /* 217 * We must have a pathname, and it must be absolute. 218 */ 219 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') 220 return; 221 222 /* 223 * Only prefetch path and devid info if the device has 224 * never been opened. 225 */ 226 if (vd->vdev_tsd != NULL) 227 return; 228 229 if (vd->vdev_wholedisk == -1ULL) { 230 size_t len = strlen(vd->vdev_path) + 3; 231 char *buf = kmem_alloc(len, KM_SLEEP); 232 233 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 234 235 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp); 236 kmem_free(buf, len); 237 } 238 239 if (vd->vdev_name_vp == NULL) 240 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp); 241 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); 247 } 248 } 249 250 static void 251 vdev_disk_rele(vdev_t *vd) 252 { 253 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 254 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; 259 } 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; 264 } 265 } 266 267 /* 268 * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when 269 * even a fallback to DKIOCGMEDIAINFO fails. 270 */ 271 #ifdef DEBUG 272 #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__) 273 #else 274 #define VDEV_DEBUG(...) /* Nothing... */ 275 #endif 276 277 static int 278 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize, 279 uint64_t *ashift) 280 { 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; 297 298 /* 299 * We must have a pathname, and it must be absolute. 300 */ 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)); 304 } 305 306 /* 307 * Reopen the device if it's not currently open. Otherwise, 308 * just update the physical size of the device. 309 */ 310 if (dvd != NULL) { 311 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) { 312 /* 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. 316 */ 317 vdev_disk_free(vd); 318 } else { 319 ASSERT(vd->vdev_reopening); 320 goto skip_open; 321 } 322 } 323 324 /* 325 * Create vd->vdev_tsd. 326 */ 327 vdev_disk_alloc(vd); 328 dvd = vd->vdev_tsd; 329 330 /* 331 * Allow bypassing the devid. 332 */ 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; 338 } 339 340 /* 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: 346 * 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'. 349 * 350 * 2. If the devid of the device matches the stored value, return 351 * success. 352 * 353 * 3. Otherwise, the device may have moved. Try opening the device 354 * by the devid instead. 355 */ 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)); 363 } 364 } 365 366 error = EINVAL; /* presume failure */ 367 368 if (vd->vdev_path != NULL) { 369 370 if (vd->vdev_wholedisk == -1ULL) { 371 size_t len = strlen(vd->vdev_path) + 3; 372 char *buf = kmem_alloc(len, KM_SLEEP); 373 374 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 375 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); 384 } 385 } 386 387 /* 388 * If we have not yet opened the device, try to open it by the 389 * specified path. 390 */ 391 if (error != 0) { 392 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 393 kcred, &dvd->vd_lh, zfs_li); 394 } 395 396 /* 397 * Compare the devid to the stored value. 398 */ 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) { 402 /* 403 * A mismatch here is unexpected, log it. 404 */ 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); 414 415 error = SET_ERROR(EINVAL); 416 (void) ldi_close(dvd->vd_lh, spa_mode(spa), 417 kcred); 418 dvd->vd_lh = NULL; 419 } 420 ddi_devid_free(devid); 421 } 422 423 /* 424 * If we succeeded in opening the device, but 'vdev_wholedisk' 425 * is not yet set, then this must be a slice. 426 */ 427 if (error == 0 && vd->vdev_wholedisk == -1ULL) 428 vd->vdev_wholedisk = 0; 429 } 430 431 /* 432 * If we were unable to open by path, or the devid check fails, open by 433 * devid instead. 434 */ 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); 441 } 442 } 443 444 /* 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. 449 */ 450 if (error) { 451 if (vd->vdev_devid != NULL) 452 validate_devid = B_TRUE; 453 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); 458 459 /* 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. 463 */ 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); 467 } 468 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); 474 } 475 476 /* 477 * Now that the device has been successfully opened, update the devid 478 * if necessary. 479 */ 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; 484 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); 494 } 495 ddi_devid_free(devid); 496 } 497 498 /* 499 * Once a device is opened, verify that the physical device path (if 500 * available) is up to date. 501 */ 502 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 503 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 504 char *physpath, *minorname; 505 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); 517 } 518 if (minorname) 519 kmem_free(minorname, strlen(minorname) + 1); 520 kmem_free(physpath, MAXPATHLEN); 521 } 522 523 /* 524 * Register callbacks for the LDI offline event. 525 */ 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); 532 } 533 534 /* 535 * Register callbacks for the LDI degrade event. 536 */ 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); 543 } 544 skip_open: 545 /* 546 * Determine the actual size of the device. 547 */ 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)); 552 } 553 554 *max_psize = *psize; 555 556 /* 557 * Determine the device's minimum transfer size. 558 * If the ioctl isn't supported, assume DEV_BSIZE. 559 */ 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; 578 } 579 580 *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1; 581 582 if (vd->vdev_wholedisk == 1) { 583 int wce = 1; 584 585 if (error == 0) { 586 /* 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. 591 */ 592 *max_psize = capacity * blksz; 593 } 594 595 /* 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. 598 */ 599 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 600 FKIOCTL, kcred, NULL); 601 } 602 603 /* 604 * Clear the nowritecache bit, so that on a vdev_reopen() we will 605 * try again. 606 */ 607 vd->vdev_nowritecache = B_FALSE; 608 609 /* Inform the ZIO pipeline that we are non-rotational */ 610 vd->vdev_nonrot = B_FALSE; 611 if (ldi_prop_exists(dvd->vd_lh, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 612 "device-solid-state")) { 613 if (ldi_prop_get_int(dvd->vd_lh, 614 LDI_DEV_T_ANY | DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 615 "device-solid-state", B_FALSE) != 0) 616 vd->vdev_nonrot = B_TRUE; 617 } 618 619 return (0); 620 } 621 622 static void 623 vdev_disk_close(vdev_t *vd) 624 { 625 vdev_disk_t *dvd = vd->vdev_tsd; 626 627 if (vd->vdev_reopening || dvd == NULL) 628 return; 629 630 if (dvd->vd_minor != NULL) { 631 ddi_devid_str_free(dvd->vd_minor); 632 dvd->vd_minor = NULL; 633 } 634 635 if (dvd->vd_devid != NULL) { 636 ddi_devid_free(dvd->vd_devid); 637 dvd->vd_devid = NULL; 638 } 639 640 if (dvd->vd_lh != NULL) { 641 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 642 dvd->vd_lh = NULL; 643 } 644 645 vd->vdev_delayed_close = B_FALSE; 646 /* 647 * If we closed the LDI handle due to an offline notify from LDI, 648 * don't free vd->vdev_tsd or unregister the callbacks here; 649 * the offline finalize callback or a reopen will take care of it. 650 */ 651 if (dvd->vd_ldi_offline) 652 return; 653 654 vdev_disk_free(vd); 655 } 656 657 int 658 vdev_disk_physio(vdev_t *vd, caddr_t data, 659 size_t size, uint64_t offset, int flags, boolean_t isdump) 660 { 661 vdev_disk_t *dvd = vd->vdev_tsd; 662 663 /* 664 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 665 * Nothing to be done here but return failure. 666 */ 667 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) 668 return (EIO); 669 670 ASSERT(vd->vdev_ops == &vdev_disk_ops); 671 672 /* 673 * If in the context of an active crash dump, use the ldi_dump(9F) 674 * call instead of ldi_strategy(9F) as usual. 675 */ 676 if (isdump) { 677 ASSERT3P(dvd, !=, NULL); 678 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset), 679 lbtodb(size))); 680 } 681 682 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags)); 683 } 684 685 int 686 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data, 687 size_t size, uint64_t offset, int flags) 688 { 689 buf_t *bp; 690 int error = 0; 691 692 if (vd_lh == NULL) 693 return (SET_ERROR(EINVAL)); 694 695 ASSERT(flags & B_READ || flags & B_WRITE); 696 697 bp = getrbuf(KM_SLEEP); 698 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 699 bp->b_bcount = size; 700 bp->b_un.b_addr = (void *)data; 701 bp->b_lblkno = lbtodb(offset); 702 bp->b_bufsize = size; 703 704 error = ldi_strategy(vd_lh, bp); 705 ASSERT(error == 0); 706 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 707 error = SET_ERROR(EIO); 708 freerbuf(bp); 709 710 return (error); 711 } 712 713 static int 714 vdev_disk_io_intr(buf_t *bp) 715 { 716 vdev_buf_t *vb = (vdev_buf_t *)bp; 717 zio_t *zio = vb->vb_io; 718 719 /* 720 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 721 * Rather than teach the rest of the stack about other error 722 * possibilities (EFAULT, etc), we normalize the error value here. 723 */ 724 zio->io_error = (geterror(bp) != 0 ? EIO : 0); 725 726 if (zio->io_error == 0 && bp->b_resid != 0) 727 zio->io_error = SET_ERROR(EIO); 728 729 if (zio->io_type == ZIO_TYPE_READ) { 730 abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size); 731 } else { 732 abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size); 733 } 734 735 kmem_free(vb, sizeof (vdev_buf_t)); 736 737 zio_delay_interrupt(zio); 738 return (0); 739 } 740 741 static void 742 vdev_disk_ioctl_free(zio_t *zio) 743 { 744 kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 745 } 746 747 static const zio_vsd_ops_t vdev_disk_vsd_ops = { 748 vdev_disk_ioctl_free, 749 zio_vsd_default_cksum_report 750 }; 751 752 static void 753 vdev_disk_ioctl_done(void *zio_arg, int error) 754 { 755 zio_t *zio = zio_arg; 756 757 zio->io_error = error; 758 759 zio_interrupt(zio); 760 } 761 762 static void 763 vdev_disk_io_start(zio_t *zio) 764 { 765 vdev_t *vd = zio->io_vd; 766 vdev_disk_t *dvd = vd->vdev_tsd; 767 vdev_buf_t *vb; 768 struct dk_callback *dkc; 769 buf_t *bp; 770 int error; 771 772 /* 773 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 774 * Nothing to be done here but return failure. 775 */ 776 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) { 777 zio->io_error = ENXIO; 778 zio_interrupt(zio); 779 return; 780 } 781 782 if (zio->io_type == ZIO_TYPE_IOCTL) { 783 /* XXPOLICY */ 784 if (!vdev_readable(vd)) { 785 zio->io_error = SET_ERROR(ENXIO); 786 zio_interrupt(zio); 787 return; 788 } 789 790 switch (zio->io_cmd) { 791 792 case DKIOCFLUSHWRITECACHE: 793 794 if (zfs_nocacheflush) 795 break; 796 797 if (vd->vdev_nowritecache) { 798 zio->io_error = SET_ERROR(ENOTSUP); 799 break; 800 } 801 802 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 803 zio->io_vsd_ops = &vdev_disk_vsd_ops; 804 805 dkc->dkc_callback = vdev_disk_ioctl_done; 806 dkc->dkc_flag = FLUSH_VOLATILE; 807 dkc->dkc_cookie = zio; 808 809 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 810 (uintptr_t)dkc, FKIOCTL, kcred, NULL); 811 812 if (error == 0) { 813 /* 814 * The ioctl will be done asychronously, 815 * and will call vdev_disk_ioctl_done() 816 * upon completion. 817 */ 818 return; 819 } 820 821 zio->io_error = error; 822 823 break; 824 825 default: 826 zio->io_error = SET_ERROR(ENOTSUP); 827 } 828 829 zio_execute(zio); 830 return; 831 } 832 833 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 834 zio->io_target_timestamp = zio_handle_io_delay(zio); 835 836 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP); 837 838 vb->vb_io = zio; 839 bp = &vb->vb_buf; 840 841 bioinit(bp); 842 bp->b_flags = B_BUSY | B_NOCACHE | 843 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 844 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 845 bp->b_flags |= B_FAILFAST; 846 bp->b_bcount = zio->io_size; 847 848 if (zio->io_type == ZIO_TYPE_READ) { 849 bp->b_un.b_addr = 850 abd_borrow_buf(zio->io_abd, zio->io_size); 851 } else { 852 bp->b_un.b_addr = 853 abd_borrow_buf_copy(zio->io_abd, zio->io_size); 854 } 855 856 bp->b_lblkno = lbtodb(zio->io_offset); 857 bp->b_bufsize = zio->io_size; 858 bp->b_iodone = vdev_disk_io_intr; 859 860 /* 861 * In general we would expect ldi_strategy() to return non-zero only 862 * because of programming errors, but we've also seen this fail shortly 863 * after a disk dies. 864 */ 865 if (ldi_strategy(dvd->vd_lh, bp) != 0) { 866 zio->io_error = ENXIO; 867 zio_interrupt(zio); 868 } 869 } 870 871 static void 872 vdev_disk_io_done(zio_t *zio) 873 { 874 vdev_t *vd = zio->io_vd; 875 876 /* 877 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 878 * the device has been removed. If this is the case, then we trigger an 879 * asynchronous removal of the device. Otherwise, probe the device and 880 * make sure it's still accessible. 881 */ 882 if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 883 vdev_disk_t *dvd = vd->vdev_tsd; 884 int state = DKIO_NONE; 885 886 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 887 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 888 /* 889 * We post the resource as soon as possible, instead of 890 * when the async removal actually happens, because the 891 * DE is using this information to discard previous I/O 892 * errors. 893 */ 894 zfs_post_remove(zio->io_spa, vd); 895 vd->vdev_remove_wanted = B_TRUE; 896 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 897 } else if (!vd->vdev_delayed_close) { 898 vd->vdev_delayed_close = B_TRUE; 899 } 900 } 901 } 902 903 vdev_ops_t vdev_disk_ops = { 904 .vdev_op_open = vdev_disk_open, 905 .vdev_op_close = vdev_disk_close, 906 .vdev_op_asize = vdev_default_asize, 907 .vdev_op_io_start = vdev_disk_io_start, 908 .vdev_op_io_done = vdev_disk_io_done, 909 .vdev_op_state_change = NULL, 910 .vdev_op_need_resilver = NULL, 911 .vdev_op_hold = vdev_disk_hold, 912 .vdev_op_rele = vdev_disk_rele, 913 .vdev_op_remap = NULL, 914 .vdev_op_xlate = vdev_default_xlate, 915 .vdev_op_type = VDEV_TYPE_DISK, /* name of this vdev type */ 916 .vdev_op_leaf = B_TRUE /* leaf vdev */ 917 }; 918 919 /* 920 * Given the root disk device devid or pathname, read the label from 921 * the device, and construct a configuration nvlist. 922 */ 923 int 924 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 925 { 926 ldi_handle_t vd_lh; 927 vdev_label_t *label; 928 uint64_t s, size; 929 int l; 930 ddi_devid_t tmpdevid; 931 int error = -1; 932 char *minor_name; 933 934 /* 935 * Read the device label and build the nvlist. 936 */ 937 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 938 &minor_name) == 0) { 939 error = ldi_open_by_devid(tmpdevid, minor_name, 940 FREAD, kcred, &vd_lh, zfs_li); 941 ddi_devid_free(tmpdevid); 942 ddi_devid_str_free(minor_name); 943 } 944 945 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 946 zfs_li))) 947 return (error); 948 949 if (ldi_get_size(vd_lh, &s)) { 950 (void) ldi_close(vd_lh, FREAD, kcred); 951 return (SET_ERROR(EIO)); 952 } 953 954 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 955 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 956 957 *config = NULL; 958 for (l = 0; l < VDEV_LABELS; l++) { 959 uint64_t offset, state, txg = 0; 960 961 /* read vdev label */ 962 offset = vdev_label_offset(size, l, 0); 963 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label, 964 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 965 continue; 966 967 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 968 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 969 *config = NULL; 970 continue; 971 } 972 973 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 974 &state) != 0 || state >= POOL_STATE_DESTROYED) { 975 nvlist_free(*config); 976 *config = NULL; 977 continue; 978 } 979 980 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 981 &txg) != 0 || txg == 0) { 982 nvlist_free(*config); 983 *config = NULL; 984 continue; 985 } 986 987 break; 988 } 989 990 kmem_free(label, sizeof (vdev_label_t)); 991 (void) ldi_close(vd_lh, FREAD, kcred); 992 if (*config == NULL) 993 error = SET_ERROR(EIDRM); 994 995 return (error); 996 } 997