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/vdev_trim.h> 34 #include <sys/abd.h> 35 #include <sys/fs/zfs.h> 36 #include <sys/zio.h> 37 #include <sys/sunldi.h> 38 #include <sys/efi_partition.h> 39 #include <sys/fm/fs/zfs.h> 40 41 /* 42 * Tunable to enable TRIM, which is temporarily disabled by default. 43 */ 44 uint_t zfs_no_trim = 1; 45 46 /* 47 * Tunable parameter for debugging or performance analysis. Setting this 48 * will cause pool corruption on power loss if a volatile out-of-order 49 * write cache is enabled. 50 */ 51 boolean_t zfs_nocacheflush = B_FALSE; 52 53 /* 54 * Virtual device vector for disks. 55 */ 56 57 extern ldi_ident_t zfs_li; 58 59 static void vdev_disk_close(vdev_t *); 60 61 typedef struct vdev_disk_ldi_cb { 62 list_node_t lcb_next; 63 ldi_callback_id_t lcb_id; 64 } vdev_disk_ldi_cb_t; 65 66 /* 67 * Bypass the devid when opening a disk vdev. 68 * There have been issues where the devids of several devices were shuffled, 69 * causing pool open failures. Note, that this flag is intended to be used 70 * for pool recovery only. 71 * 72 * Note that if a pool is imported with the devids bypassed, all its vdevs will 73 * cease storing devid information permanently. In practice, the devid is rarely 74 * useful as vdev paths do not tend to change unless the hardware is 75 * reconfigured. That said, if the paths do change and a pool fails to open 76 * automatically at boot, a simple zpool import should re-scan the paths and fix 77 * the issue. 78 */ 79 boolean_t vdev_disk_bypass_devid = B_FALSE; 80 81 static void 82 vdev_disk_alloc(vdev_t *vd) 83 { 84 vdev_disk_t *dvd; 85 86 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 87 /* 88 * Create the LDI event callback list. 89 */ 90 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t), 91 offsetof(vdev_disk_ldi_cb_t, lcb_next)); 92 } 93 94 static void 95 vdev_disk_free(vdev_t *vd) 96 { 97 vdev_disk_t *dvd = vd->vdev_tsd; 98 vdev_disk_ldi_cb_t *lcb; 99 100 if (dvd == NULL) 101 return; 102 103 /* 104 * We have already closed the LDI handle. Clean up the LDI event 105 * callbacks and free vd->vdev_tsd. 106 */ 107 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) { 108 list_remove(&dvd->vd_ldi_cbs, lcb); 109 (void) ldi_ev_remove_callbacks(lcb->lcb_id); 110 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t)); 111 } 112 list_destroy(&dvd->vd_ldi_cbs); 113 kmem_free(dvd, sizeof (vdev_disk_t)); 114 vd->vdev_tsd = NULL; 115 } 116 117 /* ARGSUSED */ 118 static int 119 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg, 120 void *ev_data) 121 { 122 vdev_t *vd = (vdev_t *)arg; 123 vdev_disk_t *dvd = vd->vdev_tsd; 124 125 /* 126 * Ignore events other than offline. 127 */ 128 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0) 129 return (LDI_EV_SUCCESS); 130 131 /* 132 * All LDI handles must be closed for the state change to succeed, so 133 * call on vdev_disk_close() to do this. 134 * 135 * We inform vdev_disk_close that it is being called from offline 136 * notify context so it will defer cleanup of LDI event callbacks and 137 * freeing of vd->vdev_tsd to the offline finalize or a reopen. 138 */ 139 dvd->vd_ldi_offline = B_TRUE; 140 vdev_disk_close(vd); 141 142 /* 143 * Now that the device is closed, request that the spa_async_thread 144 * mark the device as REMOVED and notify FMA of the removal. 145 */ 146 zfs_post_remove(vd->vdev_spa, vd); 147 vd->vdev_remove_wanted = B_TRUE; 148 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE); 149 150 return (LDI_EV_SUCCESS); 151 } 152 153 /* ARGSUSED */ 154 static void 155 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie, 156 int ldi_result, void *arg, void *ev_data) 157 { 158 vdev_t *vd = (vdev_t *)arg; 159 160 /* 161 * Ignore events other than offline. 162 */ 163 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0) 164 return; 165 166 /* 167 * We have already closed the LDI handle in notify. 168 * Clean up the LDI event callbacks and free vd->vdev_tsd. 169 */ 170 vdev_disk_free(vd); 171 172 /* 173 * Request that the vdev be reopened if the offline state change was 174 * unsuccessful. 175 */ 176 if (ldi_result != LDI_EV_SUCCESS) { 177 vd->vdev_probe_wanted = B_TRUE; 178 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE); 179 } 180 } 181 182 static ldi_ev_callback_t vdev_disk_off_callb = { 183 .cb_vers = LDI_EV_CB_VERS, 184 .cb_notify = vdev_disk_off_notify, 185 .cb_finalize = vdev_disk_off_finalize 186 }; 187 188 /* ARGSUSED */ 189 static void 190 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie, 191 int ldi_result, void *arg, void *ev_data) 192 { 193 vdev_t *vd = (vdev_t *)arg; 194 195 /* 196 * Ignore events other than degrade. 197 */ 198 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0) 199 return; 200 201 /* 202 * Degrade events always succeed. Mark the vdev as degraded. 203 * This status is purely informative for the user. 204 */ 205 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0); 206 } 207 208 static ldi_ev_callback_t vdev_disk_dgrd_callb = { 209 .cb_vers = LDI_EV_CB_VERS, 210 .cb_notify = NULL, 211 .cb_finalize = vdev_disk_dgrd_finalize 212 }; 213 214 static void 215 vdev_disk_hold(vdev_t *vd) 216 { 217 ddi_devid_t devid; 218 char *minor; 219 220 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 221 222 /* 223 * We must have a pathname, and it must be absolute. 224 */ 225 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') 226 return; 227 228 /* 229 * Only prefetch path and devid info if the device has 230 * never been opened. 231 */ 232 if (vd->vdev_tsd != NULL) 233 return; 234 235 if (vd->vdev_wholedisk == -1ULL) { 236 size_t len = strlen(vd->vdev_path) + 3; 237 char *buf = kmem_alloc(len, KM_SLEEP); 238 239 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 240 241 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp); 242 kmem_free(buf, len); 243 } 244 245 if (vd->vdev_name_vp == NULL) 246 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp); 247 248 if (vd->vdev_devid != NULL && 249 ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) { 250 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp); 251 ddi_devid_str_free(minor); 252 ddi_devid_free(devid); 253 } 254 } 255 256 static void 257 vdev_disk_rele(vdev_t *vd) 258 { 259 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 260 261 if (vd->vdev_name_vp) { 262 VN_RELE_ASYNC(vd->vdev_name_vp, 263 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool)); 264 vd->vdev_name_vp = NULL; 265 } 266 if (vd->vdev_devid_vp) { 267 VN_RELE_ASYNC(vd->vdev_devid_vp, 268 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool)); 269 vd->vdev_devid_vp = NULL; 270 } 271 } 272 273 /* 274 * We want to be loud in DEBUG kernels when DKIOCGMEDIAINFOEXT fails, or when 275 * even a fallback to DKIOCGMEDIAINFO fails. 276 */ 277 #ifdef DEBUG 278 #define VDEV_DEBUG(...) cmn_err(CE_NOTE, __VA_ARGS__) 279 #else 280 #define VDEV_DEBUG(...) /* Nothing... */ 281 #endif 282 283 static int 284 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize, 285 uint64_t *ashift) 286 { 287 spa_t *spa = vd->vdev_spa; 288 vdev_disk_t *dvd = vd->vdev_tsd; 289 ldi_ev_cookie_t ecookie; 290 vdev_disk_ldi_cb_t *lcb; 291 union { 292 struct dk_minfo_ext ude; 293 struct dk_minfo ud; 294 } dks; 295 struct dk_minfo_ext *dkmext = &dks.ude; 296 struct dk_minfo *dkm = &dks.ud; 297 int error, can_free; 298 dev_t dev; 299 int otyp; 300 boolean_t validate_devid = B_FALSE; 301 uint64_t capacity = 0, blksz = 0, pbsize; 302 303 /* 304 * We must have a pathname, and it must be absolute. 305 */ 306 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 307 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 308 return (SET_ERROR(EINVAL)); 309 } 310 311 /* 312 * Reopen the device if it's not currently open. Otherwise, 313 * just update the physical size of the device. 314 */ 315 if (dvd != NULL) { 316 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) { 317 /* 318 * If we are opening a device in its offline notify 319 * context, the LDI handle was just closed. Clean 320 * up the LDI event callbacks and free vd->vdev_tsd. 321 */ 322 vdev_disk_free(vd); 323 } else { 324 ASSERT(vd->vdev_reopening); 325 goto skip_open; 326 } 327 } 328 329 /* 330 * Create vd->vdev_tsd. 331 */ 332 vdev_disk_alloc(vd); 333 dvd = vd->vdev_tsd; 334 335 /* 336 * Allow bypassing the devid. 337 */ 338 if (vd->vdev_devid != NULL && vdev_disk_bypass_devid) { 339 vdev_dbgmsg(vd, "vdev_disk_open, devid %s bypassed", 340 vd->vdev_devid); 341 spa_strfree(vd->vdev_devid); 342 vd->vdev_devid = NULL; 343 } 344 345 /* 346 * When opening a disk device, we want to preserve the user's original 347 * intent. We always want to open the device by the path the user gave 348 * us, even if it is one of multiple paths to the same device. But we 349 * also want to be able to survive disks being removed/recabled. 350 * Therefore the sequence of opening devices is: 351 * 352 * 1. Try opening the device by path. For legacy pools without the 353 * 'whole_disk' property, attempt to fix the path by appending 's0'. 354 * 355 * 2. If the devid of the device matches the stored value, return 356 * success. 357 * 358 * 3. Otherwise, the device may have moved. Try opening the device 359 * by the devid instead. 360 */ 361 if (vd->vdev_devid != NULL) { 362 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 363 &dvd->vd_minor) != 0) { 364 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 365 vdev_dbgmsg(vd, "vdev_disk_open: invalid " 366 "vdev_devid '%s'", vd->vdev_devid); 367 return (SET_ERROR(EINVAL)); 368 } 369 } 370 371 error = EINVAL; /* presume failure */ 372 373 if (vd->vdev_path != NULL) { 374 375 if (vd->vdev_wholedisk == -1ULL) { 376 size_t len = strlen(vd->vdev_path) + 3; 377 char *buf = kmem_alloc(len, KM_SLEEP); 378 379 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 380 381 error = ldi_open_by_name(buf, spa_mode(spa), kcred, 382 &dvd->vd_lh, zfs_li); 383 if (error == 0) { 384 spa_strfree(vd->vdev_path); 385 vd->vdev_path = buf; 386 vd->vdev_wholedisk = 1ULL; 387 } else { 388 kmem_free(buf, len); 389 } 390 } 391 392 /* 393 * If we have not yet opened the device, try to open it by the 394 * specified path. 395 */ 396 if (error != 0) { 397 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 398 kcred, &dvd->vd_lh, zfs_li); 399 } 400 401 /* 402 * Compare the devid to the stored value. 403 */ 404 if (error == 0 && vd->vdev_devid != NULL) { 405 ddi_devid_t devid = NULL; 406 407 if (ldi_get_devid(dvd->vd_lh, &devid) != 0) { 408 /* 409 * We expected a devid on this device but it no 410 * longer appears to have one. The validation 411 * step may need to remove it from the 412 * configuration. 413 */ 414 validate_devid = B_TRUE; 415 416 } else if (ddi_devid_compare(devid, dvd->vd_devid) != 417 0) { 418 /* 419 * A mismatch here is unexpected, log it. 420 */ 421 char *devid_str = ddi_devid_str_encode(devid, 422 dvd->vd_minor); 423 vdev_dbgmsg(vd, "vdev_disk_open: devid " 424 "mismatch: %s != %s", vd->vdev_devid, 425 devid_str); 426 cmn_err(CE_NOTE, "vdev_disk_open %s: devid " 427 "mismatch: %s != %s", vd->vdev_path, 428 vd->vdev_devid, devid_str); 429 ddi_devid_str_free(devid_str); 430 431 error = SET_ERROR(EINVAL); 432 (void) ldi_close(dvd->vd_lh, spa_mode(spa), 433 kcred); 434 dvd->vd_lh = NULL; 435 } 436 437 if (devid != NULL) { 438 ddi_devid_free(devid); 439 } 440 } 441 442 /* 443 * If we succeeded in opening the device, but 'vdev_wholedisk' 444 * is not yet set, then this must be a slice. 445 */ 446 if (error == 0 && vd->vdev_wholedisk == -1ULL) 447 vd->vdev_wholedisk = 0; 448 } 449 450 /* 451 * If we were unable to open by path, or the devid check fails, open by 452 * devid instead. 453 */ 454 if (error != 0 && vd->vdev_devid != NULL) { 455 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 456 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li); 457 if (error != 0) { 458 vdev_dbgmsg(vd, "Failed to open by devid (%s)", 459 vd->vdev_devid); 460 } 461 } 462 463 /* 464 * If all else fails, then try opening by physical path (if available) 465 * or the logical path (if we failed due to the devid check). While not 466 * as reliable as the devid, this will give us something, and the higher 467 * level vdev validation will prevent us from opening the wrong device. 468 */ 469 if (error != 0) { 470 validate_devid = B_TRUE; 471 472 if (vd->vdev_physpath != NULL && 473 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV) { 474 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa), 475 kcred, &dvd->vd_lh, zfs_li); 476 } 477 478 /* 479 * Note that we don't support the legacy auto-wholedisk support 480 * as above. This hasn't been used in a very long time and we 481 * don't need to propagate its oddities to this edge condition. 482 */ 483 if (error != 0 && vd->vdev_path != NULL) { 484 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 485 kcred, &dvd->vd_lh, zfs_li); 486 } 487 } 488 489 if (error != 0) { 490 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 491 vdev_dbgmsg(vd, "vdev_disk_open: failed to open [error=%d]", 492 error); 493 return (error); 494 } 495 496 /* 497 * Now that the device has been successfully opened, update the devid 498 * if necessary. 499 */ 500 if (validate_devid) { 501 ddi_devid_t devid = NULL; 502 char *minorname = NULL; 503 char *vd_devid = NULL; 504 boolean_t remove = B_FALSE, update = B_FALSE; 505 506 /* 507 * Get the current devid and minor name for the device we 508 * opened. 509 */ 510 if (ldi_get_devid(dvd->vd_lh, &devid) != 0 || 511 ldi_get_minor_name(dvd->vd_lh, &minorname) != 0) { 512 /* 513 * If we are unable to get the devid or the minor name 514 * for the device, we need to remove them from the 515 * configuration to prevent potential inconsistencies. 516 */ 517 if (dvd->vd_minor != NULL || dvd->vd_devid != NULL || 518 vd->vdev_devid != NULL) { 519 /* 520 * We only need to remove the devid if one 521 * exists. 522 */ 523 remove = B_TRUE; 524 } 525 526 } else if (dvd->vd_devid == NULL || dvd->vd_minor == NULL) { 527 /* 528 * There was previously no devid at all so we need to 529 * add one. 530 */ 531 update = B_TRUE; 532 533 } else if (ddi_devid_compare(devid, dvd->vd_devid) != 0 || 534 strcmp(minorname, dvd->vd_minor) != 0) { 535 /* 536 * The devid or minor name on file does not match the 537 * one from the opened device. 538 */ 539 update = B_TRUE; 540 } 541 542 if (update) { 543 /* 544 * Render the new devid and minor name as a string for 545 * logging and to store in the vdev configuration. 546 */ 547 vd_devid = ddi_devid_str_encode(devid, minorname); 548 } 549 550 if (update || remove) { 551 vdev_dbgmsg(vd, "vdev_disk_open: update devid from " 552 "'%s' to '%s'", 553 vd->vdev_devid != NULL ? vd->vdev_devid : "<none>", 554 vd_devid != NULL ? vd_devid : "<none>"); 555 cmn_err(CE_NOTE, "vdev_disk_open %s: update devid " 556 "from '%s' to '%s'", 557 vd->vdev_path != NULL ? vd->vdev_path : "?", 558 vd->vdev_devid != NULL ? vd->vdev_devid : "<none>", 559 vd_devid != NULL ? vd_devid : "<none>"); 560 561 /* 562 * Remove and free any existing values. 563 */ 564 if (dvd->vd_minor != NULL) { 565 ddi_devid_str_free(dvd->vd_minor); 566 dvd->vd_minor = NULL; 567 } 568 if (dvd->vd_devid != NULL) { 569 ddi_devid_free(dvd->vd_devid); 570 dvd->vd_devid = NULL; 571 } 572 if (vd->vdev_devid != NULL) { 573 spa_strfree(vd->vdev_devid); 574 vd->vdev_devid = NULL; 575 } 576 } 577 578 if (update) { 579 /* 580 * Install the new values. 581 */ 582 vd->vdev_devid = vd_devid; 583 dvd->vd_minor = minorname; 584 dvd->vd_devid = devid; 585 586 } else { 587 if (devid != NULL) { 588 ddi_devid_free(devid); 589 } 590 if (minorname != NULL) { 591 kmem_free(minorname, strlen(minorname) + 1); 592 } 593 } 594 } 595 596 /* 597 * Once a device is opened, verify that the physical device path (if 598 * available) is up to date. 599 */ 600 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 601 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 602 char *physpath, *minorname; 603 604 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 605 minorname = NULL; 606 if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 607 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 608 (vd->vdev_physpath == NULL || 609 strcmp(vd->vdev_physpath, physpath) != 0)) { 610 if (vd->vdev_physpath) 611 spa_strfree(vd->vdev_physpath); 612 (void) strlcat(physpath, ":", MAXPATHLEN); 613 (void) strlcat(physpath, minorname, MAXPATHLEN); 614 vd->vdev_physpath = spa_strdup(physpath); 615 } 616 if (minorname) 617 kmem_free(minorname, strlen(minorname) + 1); 618 kmem_free(physpath, MAXPATHLEN); 619 } 620 621 /* 622 * Register callbacks for the LDI offline event. 623 */ 624 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) == 625 LDI_EV_SUCCESS) { 626 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP); 627 list_insert_tail(&dvd->vd_ldi_cbs, lcb); 628 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie, 629 &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id); 630 } 631 632 /* 633 * Register callbacks for the LDI degrade event. 634 */ 635 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) == 636 LDI_EV_SUCCESS) { 637 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP); 638 list_insert_tail(&dvd->vd_ldi_cbs, lcb); 639 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie, 640 &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id); 641 } 642 643 skip_open: 644 /* 645 * Determine the actual size of the device. 646 */ 647 if (ldi_get_size(dvd->vd_lh, psize) != 0) { 648 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 649 vdev_dbgmsg(vd, "vdev_disk_open: failed to get size"); 650 return (SET_ERROR(EINVAL)); 651 } 652 653 *max_psize = *psize; 654 655 /* 656 * Determine the device's minimum transfer size. 657 * If the ioctl isn't supported, assume DEV_BSIZE. 658 */ 659 if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, 660 (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) { 661 capacity = dkmext->dki_capacity - 1; 662 blksz = dkmext->dki_lbsize; 663 pbsize = dkmext->dki_pbsize; 664 } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, 665 (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) { 666 VDEV_DEBUG( 667 "vdev_disk_open(\"%s\"): fallback to DKIOCGMEDIAINFO\n", 668 vd->vdev_path); 669 capacity = dkm->dki_capacity - 1; 670 blksz = dkm->dki_lbsize; 671 pbsize = blksz; 672 } else { 673 VDEV_DEBUG("vdev_disk_open(\"%s\"): " 674 "both DKIOCGMEDIAINFO{,EXT} calls failed, %d\n", 675 vd->vdev_path, error); 676 pbsize = DEV_BSIZE; 677 } 678 679 *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1; 680 681 if (vd->vdev_wholedisk == 1) { 682 int wce = 1; 683 684 if (error == 0) { 685 /* 686 * If we have the capability to expand, we'd have 687 * found out via success from DKIOCGMEDIAINFO{,EXT}. 688 * Adjust max_psize upward accordingly since we know 689 * we own the whole disk now. 690 */ 691 *max_psize = capacity * blksz; 692 } 693 694 /* 695 * Since we own the whole disk, try to enable disk write 696 * caching. We ignore errors because it's OK if we can't do it. 697 */ 698 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 699 FKIOCTL, kcred, NULL); 700 } 701 702 /* 703 * Clear the nowritecache bit, so that on a vdev_reopen() we will 704 * try again. 705 */ 706 vd->vdev_nowritecache = B_FALSE; 707 708 if (ldi_ioctl(dvd->vd_lh, DKIOC_CANFREE, (intptr_t)&can_free, FKIOCTL, 709 kcred, NULL) == 0 && can_free == 1) { 710 vd->vdev_has_trim = B_TRUE; 711 } else { 712 vd->vdev_has_trim = B_FALSE; 713 } 714 715 if (zfs_no_trim == 1) 716 vd->vdev_has_trim = B_FALSE; 717 718 /* Currently only supported for ZoL. */ 719 vd->vdev_has_securetrim = B_FALSE; 720 721 /* Inform the ZIO pipeline that we are non-rotational */ 722 vd->vdev_nonrot = B_FALSE; 723 if (ldi_prop_exists(dvd->vd_lh, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 724 "device-solid-state")) { 725 if (ldi_prop_get_int(dvd->vd_lh, 726 LDI_DEV_T_ANY | DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 727 "device-solid-state", B_FALSE) != 0) 728 vd->vdev_nonrot = B_TRUE; 729 } 730 731 return (0); 732 } 733 734 static void 735 vdev_disk_close(vdev_t *vd) 736 { 737 vdev_disk_t *dvd = vd->vdev_tsd; 738 739 if (vd->vdev_reopening || dvd == NULL) 740 return; 741 742 if (dvd->vd_minor != NULL) { 743 ddi_devid_str_free(dvd->vd_minor); 744 dvd->vd_minor = NULL; 745 } 746 747 if (dvd->vd_devid != NULL) { 748 ddi_devid_free(dvd->vd_devid); 749 dvd->vd_devid = NULL; 750 } 751 752 if (dvd->vd_lh != NULL) { 753 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 754 dvd->vd_lh = NULL; 755 } 756 757 vd->vdev_delayed_close = B_FALSE; 758 /* 759 * If we closed the LDI handle due to an offline notify from LDI, 760 * don't free vd->vdev_tsd or unregister the callbacks here; 761 * the offline finalize callback or a reopen will take care of it. 762 */ 763 if (dvd->vd_ldi_offline) 764 return; 765 766 vdev_disk_free(vd); 767 } 768 769 int 770 vdev_disk_physio(vdev_t *vd, caddr_t data, 771 size_t size, uint64_t offset, int flags, boolean_t isdump) 772 { 773 vdev_disk_t *dvd = vd->vdev_tsd; 774 775 /* 776 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 777 * Nothing to be done here but return failure. 778 */ 779 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) 780 return (EIO); 781 782 ASSERT(vd->vdev_ops == &vdev_disk_ops); 783 784 /* 785 * If in the context of an active crash dump, use the ldi_dump(9F) 786 * call instead of ldi_strategy(9F) as usual. 787 */ 788 if (isdump) { 789 ASSERT3P(dvd, !=, NULL); 790 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset), 791 lbtodb(size))); 792 } 793 794 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags)); 795 } 796 797 int 798 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data, 799 size_t size, uint64_t offset, int flags) 800 { 801 buf_t *bp; 802 int error = 0; 803 804 if (vd_lh == NULL) 805 return (SET_ERROR(EINVAL)); 806 807 ASSERT(flags & B_READ || flags & B_WRITE); 808 809 bp = getrbuf(KM_SLEEP); 810 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 811 bp->b_bcount = size; 812 bp->b_un.b_addr = (void *)data; 813 bp->b_lblkno = lbtodb(offset); 814 bp->b_bufsize = size; 815 816 error = ldi_strategy(vd_lh, bp); 817 ASSERT(error == 0); 818 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 819 error = SET_ERROR(EIO); 820 freerbuf(bp); 821 822 return (error); 823 } 824 825 static int 826 vdev_disk_io_intr(buf_t *bp) 827 { 828 vdev_buf_t *vb = (vdev_buf_t *)bp; 829 zio_t *zio = vb->vb_io; 830 831 /* 832 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 833 * Rather than teach the rest of the stack about other error 834 * possibilities (EFAULT, etc), we normalize the error value here. 835 */ 836 zio->io_error = (geterror(bp) != 0 ? EIO : 0); 837 838 if (zio->io_error == 0 && bp->b_resid != 0) 839 zio->io_error = SET_ERROR(EIO); 840 841 if (zio->io_type == ZIO_TYPE_READ) { 842 abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size); 843 } else { 844 abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size); 845 } 846 847 kmem_free(vb, sizeof (vdev_buf_t)); 848 849 zio_delay_interrupt(zio); 850 return (0); 851 } 852 853 static void 854 vdev_disk_ioctl_free(zio_t *zio) 855 { 856 kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 857 } 858 859 static const zio_vsd_ops_t vdev_disk_vsd_ops = { 860 vdev_disk_ioctl_free, 861 zio_vsd_default_cksum_report 862 }; 863 864 static void 865 vdev_disk_ioctl_done(void *zio_arg, int error) 866 { 867 zio_t *zio = zio_arg; 868 869 zio->io_error = error; 870 871 zio_interrupt(zio); 872 } 873 874 static void 875 vdev_disk_io_start(zio_t *zio) 876 { 877 vdev_t *vd = zio->io_vd; 878 vdev_disk_t *dvd = vd->vdev_tsd; 879 unsigned long trim_flags = 0; 880 vdev_buf_t *vb; 881 struct dk_callback *dkc; 882 buf_t *bp; 883 int error; 884 885 /* 886 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 887 * Nothing to be done here but return failure. 888 */ 889 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) { 890 zio->io_error = ENXIO; 891 zio_interrupt(zio); 892 return; 893 } 894 895 switch (zio->io_type) { 896 case ZIO_TYPE_IOCTL: 897 /* XXPOLICY */ 898 if (!vdev_readable(vd)) { 899 zio->io_error = SET_ERROR(ENXIO); 900 zio_interrupt(zio); 901 return; 902 } 903 904 switch (zio->io_cmd) { 905 906 case DKIOCFLUSHWRITECACHE: 907 908 if (zfs_nocacheflush) 909 break; 910 911 if (vd->vdev_nowritecache) { 912 zio->io_error = SET_ERROR(ENOTSUP); 913 break; 914 } 915 916 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 917 zio->io_vsd_ops = &vdev_disk_vsd_ops; 918 919 dkc->dkc_callback = vdev_disk_ioctl_done; 920 dkc->dkc_flag = FLUSH_VOLATILE; 921 dkc->dkc_cookie = zio; 922 923 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 924 (uintptr_t)dkc, FKIOCTL, kcred, NULL); 925 926 if (error == 0) { 927 /* 928 * The ioctl will be done asychronously, 929 * and will call vdev_disk_ioctl_done() 930 * upon completion. 931 */ 932 return; 933 } 934 935 zio->io_error = error; 936 937 break; 938 939 default: 940 zio->io_error = SET_ERROR(ENOTSUP); 941 } 942 943 zio_execute(zio); 944 return; 945 946 case ZIO_TYPE_TRIM: 947 if (zfs_no_trim == 1 || !vd->vdev_has_trim) { 948 zio->io_error = SET_ERROR(ENOTSUP); 949 zio_execute(zio); 950 return; 951 } 952 /* Currently only supported on ZoL. */ 953 ASSERT0(zio->io_trim_flags & ZIO_TRIM_SECURE); 954 955 /* dkioc_free_list_t is already declared to hold one entry */ 956 dkioc_free_list_t dfl; 957 dfl.dfl_flags = 0; 958 dfl.dfl_num_exts = 1; 959 dfl.dfl_offset = 0; 960 dfl.dfl_exts[0].dfle_start = zio->io_offset; 961 dfl.dfl_exts[0].dfle_length = zio->io_size; 962 963 zio->io_error = ldi_ioctl(dvd->vd_lh, DKIOCFREE, 964 (uintptr_t)&dfl, FKIOCTL, kcred, NULL); 965 966 if (zio->io_error == ENOTSUP || zio->io_error == ENOTTY) { 967 /* 968 * The device must have changed and now TRIM is 969 * no longer supported. 970 */ 971 vd->vdev_has_trim = B_FALSE; 972 } 973 974 zio_interrupt(zio); 975 return; 976 } 977 978 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 979 zio->io_target_timestamp = zio_handle_io_delay(zio); 980 981 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP); 982 983 vb->vb_io = zio; 984 bp = &vb->vb_buf; 985 986 bioinit(bp); 987 bp->b_flags = B_BUSY | B_NOCACHE | 988 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 989 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 990 bp->b_flags |= B_FAILFAST; 991 bp->b_bcount = zio->io_size; 992 993 if (zio->io_type == ZIO_TYPE_READ) { 994 bp->b_un.b_addr = 995 abd_borrow_buf(zio->io_abd, zio->io_size); 996 } else { 997 bp->b_un.b_addr = 998 abd_borrow_buf_copy(zio->io_abd, zio->io_size); 999 } 1000 1001 bp->b_lblkno = lbtodb(zio->io_offset); 1002 bp->b_bufsize = zio->io_size; 1003 bp->b_iodone = vdev_disk_io_intr; 1004 1005 /* 1006 * In general we would expect ldi_strategy() to return non-zero only 1007 * because of programming errors, but we've also seen this fail shortly 1008 * after a disk dies. 1009 */ 1010 if (ldi_strategy(dvd->vd_lh, bp) != 0) { 1011 zio->io_error = ENXIO; 1012 zio_interrupt(zio); 1013 } 1014 } 1015 1016 static void 1017 vdev_disk_io_done(zio_t *zio) 1018 { 1019 vdev_t *vd = zio->io_vd; 1020 1021 /* 1022 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 1023 * the device has been removed. If this is the case, then we trigger an 1024 * asynchronous removal of the device. Otherwise, probe the device and 1025 * make sure it's still accessible. 1026 */ 1027 if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 1028 vdev_disk_t *dvd = vd->vdev_tsd; 1029 int state = DKIO_NONE; 1030 1031 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 1032 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 1033 /* 1034 * We post the resource as soon as possible, instead of 1035 * when the async removal actually happens, because the 1036 * DE is using this information to discard previous I/O 1037 * errors. 1038 */ 1039 zfs_post_remove(zio->io_spa, vd); 1040 vd->vdev_remove_wanted = B_TRUE; 1041 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 1042 } else if (!vd->vdev_delayed_close) { 1043 vd->vdev_delayed_close = B_TRUE; 1044 } 1045 } 1046 } 1047 1048 vdev_ops_t vdev_disk_ops = { 1049 .vdev_op_open = vdev_disk_open, 1050 .vdev_op_close = vdev_disk_close, 1051 .vdev_op_asize = vdev_default_asize, 1052 .vdev_op_io_start = vdev_disk_io_start, 1053 .vdev_op_io_done = vdev_disk_io_done, 1054 .vdev_op_state_change = NULL, 1055 .vdev_op_need_resilver = NULL, 1056 .vdev_op_hold = vdev_disk_hold, 1057 .vdev_op_rele = vdev_disk_rele, 1058 .vdev_op_remap = NULL, 1059 .vdev_op_xlate = vdev_default_xlate, 1060 .vdev_op_type = VDEV_TYPE_DISK, /* name of this vdev type */ 1061 .vdev_op_leaf = B_TRUE /* leaf vdev */ 1062 }; 1063 1064 /* 1065 * Given the root disk device devid or pathname, read the label from 1066 * the device, and construct a configuration nvlist. 1067 */ 1068 int 1069 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 1070 { 1071 ldi_handle_t vd_lh; 1072 vdev_label_t *label; 1073 uint64_t s, size; 1074 int l; 1075 ddi_devid_t tmpdevid; 1076 int error = -1; 1077 char *minor_name; 1078 1079 /* 1080 * Read the device label and build the nvlist. 1081 */ 1082 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 1083 &minor_name) == 0) { 1084 error = ldi_open_by_devid(tmpdevid, minor_name, 1085 FREAD, kcred, &vd_lh, zfs_li); 1086 ddi_devid_free(tmpdevid); 1087 ddi_devid_str_free(minor_name); 1088 } 1089 1090 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 1091 zfs_li))) 1092 return (error); 1093 1094 if (ldi_get_size(vd_lh, &s)) { 1095 (void) ldi_close(vd_lh, FREAD, kcred); 1096 return (SET_ERROR(EIO)); 1097 } 1098 1099 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 1100 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 1101 1102 *config = NULL; 1103 for (l = 0; l < VDEV_LABELS; l++) { 1104 uint64_t offset, state, txg = 0; 1105 1106 /* read vdev label */ 1107 offset = vdev_label_offset(size, l, 0); 1108 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label, 1109 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 1110 continue; 1111 1112 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 1113 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 1114 *config = NULL; 1115 continue; 1116 } 1117 1118 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 1119 &state) != 0 || state >= POOL_STATE_DESTROYED) { 1120 nvlist_free(*config); 1121 *config = NULL; 1122 continue; 1123 } 1124 1125 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 1126 &txg) != 0 || txg == 0) { 1127 nvlist_free(*config); 1128 *config = NULL; 1129 continue; 1130 } 1131 1132 break; 1133 } 1134 1135 kmem_free(label, sizeof (vdev_label_t)); 1136 (void) ldi_close(vd_lh, FREAD, kcred); 1137 if (*config == NULL) 1138 error = SET_ERROR(EIDRM); 1139 1140 return (error); 1141 } 1142