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 (c) 2013 Joyent, Inc. All rights reserved. 26 */ 27 28 #include <sys/zfs_context.h> 29 #include <sys/spa_impl.h> 30 #include <sys/refcount.h> 31 #include <sys/vdev_disk.h> 32 #include <sys/vdev_impl.h> 33 #include <sys/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 return (0); 610 } 611 612 static void 613 vdev_disk_close(vdev_t *vd) 614 { 615 vdev_disk_t *dvd = vd->vdev_tsd; 616 617 if (vd->vdev_reopening || dvd == NULL) 618 return; 619 620 if (dvd->vd_minor != NULL) { 621 ddi_devid_str_free(dvd->vd_minor); 622 dvd->vd_minor = NULL; 623 } 624 625 if (dvd->vd_devid != NULL) { 626 ddi_devid_free(dvd->vd_devid); 627 dvd->vd_devid = NULL; 628 } 629 630 if (dvd->vd_lh != NULL) { 631 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 632 dvd->vd_lh = NULL; 633 } 634 635 vd->vdev_delayed_close = B_FALSE; 636 /* 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. 640 */ 641 if (dvd->vd_ldi_offline) 642 return; 643 644 vdev_disk_free(vd); 645 } 646 647 int 648 vdev_disk_physio(vdev_t *vd, caddr_t data, 649 size_t size, uint64_t offset, int flags, boolean_t isdump) 650 { 651 vdev_disk_t *dvd = vd->vdev_tsd; 652 653 /* 654 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 655 * Nothing to be done here but return failure. 656 */ 657 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) 658 return (EIO); 659 660 ASSERT(vd->vdev_ops == &vdev_disk_ops); 661 662 /* 663 * If in the context of an active crash dump, use the ldi_dump(9F) 664 * call instead of ldi_strategy(9F) as usual. 665 */ 666 if (isdump) { 667 ASSERT3P(dvd, !=, NULL); 668 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset), 669 lbtodb(size))); 670 } 671 672 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags)); 673 } 674 675 int 676 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data, 677 size_t size, uint64_t offset, int flags) 678 { 679 buf_t *bp; 680 int error = 0; 681 682 if (vd_lh == NULL) 683 return (SET_ERROR(EINVAL)); 684 685 ASSERT(flags & B_READ || flags & B_WRITE); 686 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; 693 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); 699 700 return (error); 701 } 702 703 static void 704 vdev_disk_io_intr(buf_t *bp) 705 { 706 vdev_buf_t *vb = (vdev_buf_t *)bp; 707 zio_t *zio = vb->vb_io; 708 709 /* 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. 713 */ 714 zio->io_error = (geterror(bp) != 0 ? EIO : 0); 715 716 if (zio->io_error == 0 && bp->b_resid != 0) 717 zio->io_error = SET_ERROR(EIO); 718 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); 723 } 724 725 kmem_free(vb, sizeof (vdev_buf_t)); 726 727 zio_delay_interrupt(zio); 728 } 729 730 static void 731 vdev_disk_ioctl_free(zio_t *zio) 732 { 733 kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 734 } 735 736 static const zio_vsd_ops_t vdev_disk_vsd_ops = { 737 vdev_disk_ioctl_free, 738 zio_vsd_default_cksum_report 739 }; 740 741 static void 742 vdev_disk_ioctl_done(void *zio_arg, int error) 743 { 744 zio_t *zio = zio_arg; 745 746 zio->io_error = error; 747 748 zio_interrupt(zio); 749 } 750 751 static void 752 vdev_disk_io_start(zio_t *zio) 753 { 754 vdev_t *vd = zio->io_vd; 755 vdev_disk_t *dvd = vd->vdev_tsd; 756 vdev_buf_t *vb; 757 struct dk_callback *dkc; 758 buf_t *bp; 759 int error; 760 761 /* 762 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 763 * Nothing to be done here but return failure. 764 */ 765 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) { 766 zio->io_error = ENXIO; 767 zio_interrupt(zio); 768 return; 769 } 770 771 if (zio->io_type == ZIO_TYPE_IOCTL) { 772 /* XXPOLICY */ 773 if (!vdev_readable(vd)) { 774 zio->io_error = SET_ERROR(ENXIO); 775 zio_interrupt(zio); 776 return; 777 } 778 779 switch (zio->io_cmd) { 780 781 case DKIOCFLUSHWRITECACHE: 782 783 if (zfs_nocacheflush) 784 break; 785 786 if (vd->vdev_nowritecache) { 787 zio->io_error = SET_ERROR(ENOTSUP); 788 break; 789 } 790 791 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 792 zio->io_vsd_ops = &vdev_disk_vsd_ops; 793 794 dkc->dkc_callback = vdev_disk_ioctl_done; 795 dkc->dkc_flag = FLUSH_VOLATILE; 796 dkc->dkc_cookie = zio; 797 798 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 799 (uintptr_t)dkc, FKIOCTL, kcred, NULL); 800 801 if (error == 0) { 802 /* 803 * The ioctl will be done asychronously, 804 * and will call vdev_disk_ioctl_done() 805 * upon completion. 806 */ 807 return; 808 } 809 810 zio->io_error = error; 811 812 break; 813 814 default: 815 zio->io_error = SET_ERROR(ENOTSUP); 816 } 817 818 zio_execute(zio); 819 return; 820 } 821 822 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 823 zio->io_target_timestamp = zio_handle_io_delay(zio); 824 825 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP); 826 827 vb->vb_io = zio; 828 bp = &vb->vb_buf; 829 830 bioinit(bp); 831 bp->b_flags = B_BUSY | B_NOCACHE | 832 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 833 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 834 bp->b_flags |= B_FAILFAST; 835 bp->b_bcount = zio->io_size; 836 837 if (zio->io_type == ZIO_TYPE_READ) { 838 bp->b_un.b_addr = 839 abd_borrow_buf(zio->io_abd, zio->io_size); 840 } else { 841 bp->b_un.b_addr = 842 abd_borrow_buf_copy(zio->io_abd, zio->io_size); 843 } 844 845 bp->b_lblkno = lbtodb(zio->io_offset); 846 bp->b_bufsize = zio->io_size; 847 bp->b_iodone = (int (*)())vdev_disk_io_intr; 848 849 /* ldi_strategy() will return non-zero only on programming errors */ 850 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0); 851 } 852 853 static void 854 vdev_disk_io_done(zio_t *zio) 855 { 856 vdev_t *vd = zio->io_vd; 857 858 /* 859 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 860 * the device has been removed. If this is the case, then we trigger an 861 * asynchronous removal of the device. Otherwise, probe the device and 862 * make sure it's still accessible. 863 */ 864 if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 865 vdev_disk_t *dvd = vd->vdev_tsd; 866 int state = DKIO_NONE; 867 868 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 869 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 870 /* 871 * We post the resource as soon as possible, instead of 872 * when the async removal actually happens, because the 873 * DE is using this information to discard previous I/O 874 * errors. 875 */ 876 zfs_post_remove(zio->io_spa, vd); 877 vd->vdev_remove_wanted = B_TRUE; 878 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 879 } else if (!vd->vdev_delayed_close) { 880 vd->vdev_delayed_close = B_TRUE; 881 } 882 } 883 } 884 885 vdev_ops_t vdev_disk_ops = { 886 vdev_disk_open, 887 vdev_disk_close, 888 vdev_default_asize, 889 vdev_disk_io_start, 890 vdev_disk_io_done, 891 NULL, 892 vdev_disk_hold, 893 vdev_disk_rele, 894 NULL, 895 vdev_default_xlate, 896 VDEV_TYPE_DISK, /* name of this vdev type */ 897 B_TRUE /* leaf vdev */ 898 }; 899 900 /* 901 * Given the root disk device devid or pathname, read the label from 902 * the device, and construct a configuration nvlist. 903 */ 904 int 905 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 906 { 907 ldi_handle_t vd_lh; 908 vdev_label_t *label; 909 uint64_t s, size; 910 int l; 911 ddi_devid_t tmpdevid; 912 int error = -1; 913 char *minor_name; 914 915 /* 916 * Read the device label and build the nvlist. 917 */ 918 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 919 &minor_name) == 0) { 920 error = ldi_open_by_devid(tmpdevid, minor_name, 921 FREAD, kcred, &vd_lh, zfs_li); 922 ddi_devid_free(tmpdevid); 923 ddi_devid_str_free(minor_name); 924 } 925 926 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 927 zfs_li))) 928 return (error); 929 930 if (ldi_get_size(vd_lh, &s)) { 931 (void) ldi_close(vd_lh, FREAD, kcred); 932 return (SET_ERROR(EIO)); 933 } 934 935 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 936 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 937 938 *config = NULL; 939 for (l = 0; l < VDEV_LABELS; l++) { 940 uint64_t offset, state, txg = 0; 941 942 /* read vdev label */ 943 offset = vdev_label_offset(size, l, 0); 944 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label, 945 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 946 continue; 947 948 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 949 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 950 *config = NULL; 951 continue; 952 } 953 954 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 955 &state) != 0 || state >= POOL_STATE_DESTROYED) { 956 nvlist_free(*config); 957 *config = NULL; 958 continue; 959 } 960 961 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 962 &txg) != 0 || txg == 0) { 963 nvlist_free(*config); 964 *config = NULL; 965 continue; 966 } 967 968 break; 969 } 970 971 kmem_free(label, sizeof (vdev_label_t)); 972 (void) ldi_close(vd_lh, FREAD, kcred); 973 if (*config == NULL) 974 error = SET_ERROR(EIDRM); 975 976 return (error); 977 } 978