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