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, 2014 by Delphix. All rights reserved. 24 * Copyright (c) 2013 Joyent, Inc. All rights reserved. 25 */ 26 27#include <sys/zfs_context.h> 28#include <sys/spa_impl.h> 29#include <sys/refcount.h> 30#include <sys/vdev_disk.h> 31#include <sys/vdev_impl.h> 32#include <sys/fs/zfs.h> 33#include <sys/zio.h> 34#include <sys/sunldi.h> 35#include <sys/efi_partition.h> 36#include <sys/fm/fs/zfs.h> 37 38/* 39 * Virtual device vector for disks. 40 */ 41 42extern ldi_ident_t zfs_li; 43 44static void vdev_disk_close(vdev_t *); 45 46typedef struct vdev_disk_ldi_cb { 47 list_node_t lcb_next; 48 ldi_callback_id_t lcb_id; 49} vdev_disk_ldi_cb_t; 50 51static void 52vdev_disk_alloc(vdev_t *vd) 53{ 54 vdev_disk_t *dvd; 55 56 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 57 /* 58 * Create the LDI event callback list. 59 */ 60 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t), 61 offsetof(vdev_disk_ldi_cb_t, lcb_next)); 62} 63 64static void 65vdev_disk_free(vdev_t *vd) 66{ 67 vdev_disk_t *dvd = vd->vdev_tsd; 68 vdev_disk_ldi_cb_t *lcb; 69 70 if (dvd == NULL) 71 return; 72 73 /* 74 * We have already closed the LDI handle. Clean up the LDI event 75 * callbacks and free vd->vdev_tsd. 76 */ 77 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) { 78 list_remove(&dvd->vd_ldi_cbs, lcb); 79 (void) ldi_ev_remove_callbacks(lcb->lcb_id); 80 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t)); 81 } 82 list_destroy(&dvd->vd_ldi_cbs); 83 kmem_free(dvd, sizeof (vdev_disk_t)); 84 vd->vdev_tsd = NULL; 85} 86 87/* ARGSUSED */ 88static int 89vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg, 90 void *ev_data) 91{ 92 vdev_t *vd = (vdev_t *)arg; 93 vdev_disk_t *dvd = vd->vdev_tsd; 94 95 /* 96 * Ignore events other than offline. 97 */ 98 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0) 99 return (LDI_EV_SUCCESS); 100 101 /* 102 * All LDI handles must be closed for the state change to succeed, so 103 * call on vdev_disk_close() to do this. 104 * 105 * We inform vdev_disk_close that it is being called from offline 106 * notify context so it will defer cleanup of LDI event callbacks and 107 * freeing of vd->vdev_tsd to the offline finalize or a reopen. 108 */ 109 dvd->vd_ldi_offline = B_TRUE; 110 vdev_disk_close(vd); 111 112 /* 113 * Now that the device is closed, request that the spa_async_thread 114 * mark the device as REMOVED and notify FMA of the removal. 115 */ 116 zfs_post_remove(vd->vdev_spa, vd); 117 vd->vdev_remove_wanted = B_TRUE; 118 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE); 119 120 return (LDI_EV_SUCCESS); 121} 122 123/* ARGSUSED */ 124static void 125vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie, 126 int ldi_result, void *arg, void *ev_data) 127{ 128 vdev_t *vd = (vdev_t *)arg; 129 130 /* 131 * Ignore events other than offline. 132 */ 133 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0) 134 return; 135 136 /* 137 * We have already closed the LDI handle in notify. 138 * Clean up the LDI event callbacks and free vd->vdev_tsd. 139 */ 140 vdev_disk_free(vd); 141 142 /* 143 * Request that the vdev be reopened if the offline state change was 144 * unsuccessful. 145 */ 146 if (ldi_result != LDI_EV_SUCCESS) { 147 vd->vdev_probe_wanted = B_TRUE; 148 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE); 149 } 150} 151 152static ldi_ev_callback_t vdev_disk_off_callb = { 153 .cb_vers = LDI_EV_CB_VERS, 154 .cb_notify = vdev_disk_off_notify, 155 .cb_finalize = vdev_disk_off_finalize 156}; 157 158/* ARGSUSED */ 159static void 160vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie, 161 int ldi_result, void *arg, void *ev_data) 162{ 163 vdev_t *vd = (vdev_t *)arg; 164 165 /* 166 * Ignore events other than degrade. 167 */ 168 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0) 169 return; 170 171 /* 172 * Degrade events always succeed. Mark the vdev as degraded. 173 * This status is purely informative for the user. 174 */ 175 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0); 176} 177 178static ldi_ev_callback_t vdev_disk_dgrd_callb = { 179 .cb_vers = LDI_EV_CB_VERS, 180 .cb_notify = NULL, 181 .cb_finalize = vdev_disk_dgrd_finalize 182}; 183 184static void 185vdev_disk_hold(vdev_t *vd) 186{ 187 ddi_devid_t devid; 188 char *minor; 189 190 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 191 192 /* 193 * We must have a pathname, and it must be absolute. 194 */ 195 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') 196 return; 197 198 /* 199 * Only prefetch path and devid info if the device has 200 * never been opened. 201 */ 202 if (vd->vdev_tsd != NULL) 203 return; 204 205 if (vd->vdev_wholedisk == -1ULL) { 206 size_t len = strlen(vd->vdev_path) + 3; 207 char *buf = kmem_alloc(len, KM_SLEEP); 208 209 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 210 211 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp); 212 kmem_free(buf, len); 213 } 214 215 if (vd->vdev_name_vp == NULL) 216 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp); 217 218 if (vd->vdev_devid != NULL && 219 ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) { 220 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp); 221 ddi_devid_str_free(minor); 222 ddi_devid_free(devid); 223 } 224} 225 226static void 227vdev_disk_rele(vdev_t *vd) 228{ 229 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 230 231 if (vd->vdev_name_vp) { 232 VN_RELE_ASYNC(vd->vdev_name_vp, 233 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool)); 234 vd->vdev_name_vp = NULL; 235 } 236 if (vd->vdev_devid_vp) { 237 VN_RELE_ASYNC(vd->vdev_devid_vp, 238 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool)); 239 vd->vdev_devid_vp = NULL; 240 } 241} 242 243static uint64_t 244vdev_disk_get_space(vdev_t *vd, uint64_t capacity, uint_t blksz) 245{ 246 ASSERT(vd->vdev_wholedisk); 247 248 vdev_disk_t *dvd = vd->vdev_tsd; 249 dk_efi_t dk_ioc; 250 efi_gpt_t *efi; 251 uint64_t avail_space = 0; 252 int efisize = EFI_LABEL_SIZE * 2; 253 254 dk_ioc.dki_data = kmem_alloc(efisize, KM_SLEEP); 255 dk_ioc.dki_lba = 1; 256 dk_ioc.dki_length = efisize; 257 dk_ioc.dki_data_64 = (uint64_t)(uintptr_t)dk_ioc.dki_data; 258 efi = dk_ioc.dki_data; 259 260 if (ldi_ioctl(dvd->vd_lh, DKIOCGETEFI, (intptr_t)&dk_ioc, 261 FKIOCTL, kcred, NULL) == 0) { 262 uint64_t efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA); 263 264 if (capacity > efi_altern_lba) 265 avail_space = (capacity - efi_altern_lba) * blksz; 266 } 267 kmem_free(dk_ioc.dki_data, efisize); 268 return (avail_space); 269} 270 271static int 272vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize, 273 uint64_t *ashift) 274{ 275 spa_t *spa = vd->vdev_spa; 276 vdev_disk_t *dvd = vd->vdev_tsd; 277 ldi_ev_cookie_t ecookie; 278 vdev_disk_ldi_cb_t *lcb; 279 struct dk_minfo_ext dkmext; 280 int error; 281 dev_t dev; 282 int otyp; 283 boolean_t validate_devid = B_FALSE; 284 ddi_devid_t devid; 285 286 /* 287 * We must have a pathname, and it must be absolute. 288 */ 289 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 290 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 291 return (SET_ERROR(EINVAL)); 292 } 293 294 /* 295 * Reopen the device if it's not currently open. Otherwise, 296 * just update the physical size of the device. 297 */ 298 if (dvd != NULL) { 299 if (dvd->vd_ldi_offline && dvd->vd_lh == NULL) { 300 /* 301 * If we are opening a device in its offline notify 302 * context, the LDI handle was just closed. Clean 303 * up the LDI event callbacks and free vd->vdev_tsd. 304 */ 305 vdev_disk_free(vd); 306 } else { 307 ASSERT(vd->vdev_reopening); 308 goto skip_open; 309 } 310 } 311 312 /* 313 * Create vd->vdev_tsd. 314 */ 315 vdev_disk_alloc(vd); 316 dvd = vd->vdev_tsd; 317 318 /* 319 * When opening a disk device, we want to preserve the user's original 320 * intent. We always want to open the device by the path the user gave 321 * us, even if it is one of multiple paths to the save device. But we 322 * also want to be able to survive disks being removed/recabled. 323 * Therefore the sequence of opening devices is: 324 * 325 * 1. Try opening the device by path. For legacy pools without the 326 * 'whole_disk' property, attempt to fix the path by appending 's0'. 327 * 328 * 2. If the devid of the device matches the stored value, return 329 * success. 330 * 331 * 3. Otherwise, the device may have moved. Try opening the device 332 * by the devid instead. 333 */ 334 if (vd->vdev_devid != NULL) { 335 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 336 &dvd->vd_minor) != 0) { 337 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 338 return (SET_ERROR(EINVAL)); 339 } 340 } 341 342 error = EINVAL; /* presume failure */ 343 344 if (vd->vdev_path != NULL) { 345 346 if (vd->vdev_wholedisk == -1ULL) { 347 size_t len = strlen(vd->vdev_path) + 3; 348 char *buf = kmem_alloc(len, KM_SLEEP); 349 350 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 351 352 error = ldi_open_by_name(buf, spa_mode(spa), kcred, 353 &dvd->vd_lh, zfs_li); 354 if (error == 0) { 355 spa_strfree(vd->vdev_path); 356 vd->vdev_path = buf; 357 vd->vdev_wholedisk = 1ULL; 358 } else { 359 kmem_free(buf, len); 360 } 361 } 362 363 /* 364 * If we have not yet opened the device, try to open it by the 365 * specified path. 366 */ 367 if (error != 0) { 368 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 369 kcred, &dvd->vd_lh, zfs_li); 370 } 371 372 /* 373 * Compare the devid to the stored value. 374 */ 375 if (error == 0 && vd->vdev_devid != NULL && 376 ldi_get_devid(dvd->vd_lh, &devid) == 0) { 377 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 378 error = SET_ERROR(EINVAL); 379 (void) ldi_close(dvd->vd_lh, spa_mode(spa), 380 kcred); 381 dvd->vd_lh = NULL; 382 } 383 ddi_devid_free(devid); 384 } 385 386 /* 387 * If we succeeded in opening the device, but 'vdev_wholedisk' 388 * is not yet set, then this must be a slice. 389 */ 390 if (error == 0 && vd->vdev_wholedisk == -1ULL) 391 vd->vdev_wholedisk = 0; 392 } 393 394 /* 395 * If we were unable to open by path, or the devid check fails, open by 396 * devid instead. 397 */ 398 if (error != 0 && vd->vdev_devid != NULL) { 399 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 400 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li); 401 } 402 403 /* 404 * If all else fails, then try opening by physical path (if available) 405 * or the logical path (if we failed due to the devid check). While not 406 * as reliable as the devid, this will give us something, and the higher 407 * level vdev validation will prevent us from opening the wrong device. 408 */ 409 if (error) { 410 if (vd->vdev_devid != NULL) 411 validate_devid = B_TRUE; 412 413 if (vd->vdev_physpath != NULL && 414 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV) 415 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa), 416 kcred, &dvd->vd_lh, zfs_li); 417 418 /* 419 * Note that we don't support the legacy auto-wholedisk support 420 * as above. This hasn't been used in a very long time and we 421 * don't need to propagate its oddities to this edge condition. 422 */ 423 if (error && vd->vdev_path != NULL) 424 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 425 kcred, &dvd->vd_lh, zfs_li); 426 } 427 428 if (error) { 429 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 430 return (error); 431 } 432 433 /* 434 * Now that the device has been successfully opened, update the devid 435 * if necessary. 436 */ 437 if (validate_devid && spa_writeable(spa) && 438 ldi_get_devid(dvd->vd_lh, &devid) == 0) { 439 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 440 char *vd_devid; 441 442 vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor); 443 zfs_dbgmsg("vdev %s: update devid from %s, " 444 "to %s", vd->vdev_path, vd->vdev_devid, vd_devid); 445 spa_strfree(vd->vdev_devid); 446 vd->vdev_devid = spa_strdup(vd_devid); 447 ddi_devid_str_free(vd_devid); 448 } 449 ddi_devid_free(devid); 450 } 451 452 /* 453 * Once a device is opened, verify that the physical device path (if 454 * available) is up to date. 455 */ 456 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 457 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 458 char *physpath, *minorname; 459 460 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 461 minorname = NULL; 462 if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 463 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 464 (vd->vdev_physpath == NULL || 465 strcmp(vd->vdev_physpath, physpath) != 0)) { 466 if (vd->vdev_physpath) 467 spa_strfree(vd->vdev_physpath); 468 (void) strlcat(physpath, ":", MAXPATHLEN); 469 (void) strlcat(physpath, minorname, MAXPATHLEN); 470 vd->vdev_physpath = spa_strdup(physpath); 471 } 472 if (minorname) 473 kmem_free(minorname, strlen(minorname) + 1); 474 kmem_free(physpath, MAXPATHLEN); 475 } 476 477 /* 478 * Register callbacks for the LDI offline event. 479 */ 480 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) == 481 LDI_EV_SUCCESS) { 482 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP); 483 list_insert_tail(&dvd->vd_ldi_cbs, lcb); 484 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie, 485 &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id); 486 } 487 488 /* 489 * Register callbacks for the LDI degrade event. 490 */ 491 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) == 492 LDI_EV_SUCCESS) { 493 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP); 494 list_insert_tail(&dvd->vd_ldi_cbs, lcb); 495 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie, 496 &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id); 497 } 498skip_open: 499 /* 500 * Determine the actual size of the device. 501 */ 502 if (ldi_get_size(dvd->vd_lh, psize) != 0) { 503 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 504 return (SET_ERROR(EINVAL)); 505 } 506 507 /* 508 * Determine the device's minimum transfer size. 509 * If the ioctl isn't supported, assume DEV_BSIZE. 510 */ 511 if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext, 512 FKIOCTL, kcred, NULL) != 0) 513 dkmext.dki_pbsize = DEV_BSIZE; 514 515 *ashift = highbit64(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1; 516 517 if (vd->vdev_wholedisk == 1) { 518 uint64_t capacity = dkmext.dki_capacity - 1; 519 uint64_t blksz = dkmext.dki_lbsize; 520 int wce = 1; 521 522 /* 523 * If we own the whole disk, try to enable disk write caching. 524 * We ignore errors because it's OK if we can't do it. 525 */ 526 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 527 FKIOCTL, kcred, NULL); 528 529 *max_psize = *psize + vdev_disk_get_space(vd, capacity, blksz); 530 zfs_dbgmsg("capacity change: vdev %s, psize %llu, " 531 "max_psize %llu", vd->vdev_path, *psize, *max_psize); 532 } else { 533 *max_psize = *psize; 534 } 535 536 /* 537 * Clear the nowritecache bit, so that on a vdev_reopen() we will 538 * try again. 539 */ 540 vd->vdev_nowritecache = B_FALSE; 541 542 return (0); 543} 544 545static void 546vdev_disk_close(vdev_t *vd) 547{ 548 vdev_disk_t *dvd = vd->vdev_tsd; 549 550 if (vd->vdev_reopening || dvd == NULL) 551 return; 552 553 if (dvd->vd_minor != NULL) { 554 ddi_devid_str_free(dvd->vd_minor); 555 dvd->vd_minor = NULL; 556 } 557 558 if (dvd->vd_devid != NULL) { 559 ddi_devid_free(dvd->vd_devid); 560 dvd->vd_devid = NULL; 561 } 562 563 if (dvd->vd_lh != NULL) { 564 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 565 dvd->vd_lh = NULL; 566 } 567 568 vd->vdev_delayed_close = B_FALSE; 569 /* 570 * If we closed the LDI handle due to an offline notify from LDI, 571 * don't free vd->vdev_tsd or unregister the callbacks here; 572 * the offline finalize callback or a reopen will take care of it. 573 */ 574 if (dvd->vd_ldi_offline) 575 return; 576 577 vdev_disk_free(vd); 578} 579 580int 581vdev_disk_physio(vdev_t *vd, caddr_t data, 582 size_t size, uint64_t offset, int flags, boolean_t isdump) 583{ 584 vdev_disk_t *dvd = vd->vdev_tsd; 585 586 /* 587 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 588 * Nothing to be done here but return failure. 589 */ 590 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) 591 return (EIO); 592 593 ASSERT(vd->vdev_ops == &vdev_disk_ops); 594 595 /* 596 * If in the context of an active crash dump, use the ldi_dump(9F) 597 * call instead of ldi_strategy(9F) as usual. 598 */ 599 if (isdump) { 600 ASSERT3P(dvd, !=, NULL); 601 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset), 602 lbtodb(size))); 603 } 604 605 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags)); 606} 607 608int 609vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data, 610 size_t size, uint64_t offset, int flags) 611{ 612 buf_t *bp; 613 int error = 0; 614 615 if (vd_lh == NULL) 616 return (SET_ERROR(EINVAL)); 617 618 ASSERT(flags & B_READ || flags & B_WRITE); 619 620 bp = getrbuf(KM_SLEEP); 621 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 622 bp->b_bcount = size; 623 bp->b_un.b_addr = (void *)data; 624 bp->b_lblkno = lbtodb(offset); 625 bp->b_bufsize = size; 626 627 error = ldi_strategy(vd_lh, bp); 628 ASSERT(error == 0); 629 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 630 error = SET_ERROR(EIO); 631 freerbuf(bp); 632 633 return (error); 634} 635 636static void 637vdev_disk_io_intr(buf_t *bp) 638{ 639 vdev_buf_t *vb = (vdev_buf_t *)bp; 640 zio_t *zio = vb->vb_io; 641 642 /* 643 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 644 * Rather than teach the rest of the stack about other error 645 * possibilities (EFAULT, etc), we normalize the error value here. 646 */ 647 zio->io_error = (geterror(bp) != 0 ? EIO : 0); 648 649 if (zio->io_error == 0 && bp->b_resid != 0) 650 zio->io_error = SET_ERROR(EIO); 651 652 kmem_free(vb, sizeof (vdev_buf_t)); 653 654 zio_interrupt(zio); 655} 656 657static void 658vdev_disk_ioctl_free(zio_t *zio) 659{ 660 kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 661} 662 663static const zio_vsd_ops_t vdev_disk_vsd_ops = { 664 vdev_disk_ioctl_free, 665 zio_vsd_default_cksum_report 666}; 667 668static void 669vdev_disk_ioctl_done(void *zio_arg, int error) 670{ 671 zio_t *zio = zio_arg; 672 673 zio->io_error = error; 674 675 zio_interrupt(zio); 676} 677 678static int 679vdev_disk_io_start(zio_t *zio) 680{ 681 vdev_t *vd = zio->io_vd; 682 vdev_disk_t *dvd = vd->vdev_tsd; 683 vdev_buf_t *vb; 684 struct dk_callback *dkc; 685 buf_t *bp; 686 int error; 687 688 /* 689 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 690 * Nothing to be done here but return failure. 691 */ 692 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) { 693 zio->io_error = ENXIO; 694 return (ZIO_PIPELINE_CONTINUE); 695 } 696 697 if (zio->io_type == ZIO_TYPE_IOCTL) { 698 /* XXPOLICY */ 699 if (!vdev_readable(vd)) { 700 zio->io_error = SET_ERROR(ENXIO); 701 return (ZIO_PIPELINE_CONTINUE); 702 } 703 704 switch (zio->io_cmd) { 705 706 case DKIOCFLUSHWRITECACHE: 707 708 if (zfs_nocacheflush) 709 break; 710 711 if (vd->vdev_nowritecache) { 712 zio->io_error = SET_ERROR(ENOTSUP); 713 break; 714 } 715 716 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 717 zio->io_vsd_ops = &vdev_disk_vsd_ops; 718 719 dkc->dkc_callback = vdev_disk_ioctl_done; 720 dkc->dkc_flag = FLUSH_VOLATILE; 721 dkc->dkc_cookie = zio; 722 723 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 724 (uintptr_t)dkc, FKIOCTL, kcred, NULL); 725 726 if (error == 0) { 727 /* 728 * The ioctl will be done asychronously, 729 * and will call vdev_disk_ioctl_done() 730 * upon completion. 731 */ 732 return (ZIO_PIPELINE_STOP); 733 } 734 735 if (error == ENOTSUP || error == ENOTTY) { 736 /* 737 * If we get ENOTSUP or ENOTTY, we know that 738 * no future attempts will ever succeed. 739 * In this case we set a persistent bit so 740 * that we don't bother with the ioctl in the 741 * future. 742 */ 743 vd->vdev_nowritecache = B_TRUE; 744 } 745 zio->io_error = error; 746 747 break; 748 749 default: 750 zio->io_error = SET_ERROR(ENOTSUP); 751 } 752 753 return (ZIO_PIPELINE_CONTINUE); 754 } 755 756 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP); 757 758 vb->vb_io = zio; 759 bp = &vb->vb_buf; 760 761 bioinit(bp); 762 bp->b_flags = B_BUSY | B_NOCACHE | 763 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 764 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 765 bp->b_flags |= B_FAILFAST; 766 bp->b_bcount = zio->io_size; 767 bp->b_un.b_addr = zio->io_data; 768 bp->b_lblkno = lbtodb(zio->io_offset); 769 bp->b_bufsize = zio->io_size; 770 bp->b_iodone = (int (*)())vdev_disk_io_intr; 771 772 /* ldi_strategy() will return non-zero only on programming errors */ 773 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0); 774 775 return (ZIO_PIPELINE_STOP); 776} 777 778static void 779vdev_disk_io_done(zio_t *zio) 780{ 781 vdev_t *vd = zio->io_vd; 782 783 /* 784 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 785 * the device has been removed. If this is the case, then we trigger an 786 * asynchronous removal of the device. Otherwise, probe the device and 787 * make sure it's still accessible. 788 */ 789 if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 790 vdev_disk_t *dvd = vd->vdev_tsd; 791 int state = DKIO_NONE; 792 793 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 794 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 795 /* 796 * We post the resource as soon as possible, instead of 797 * when the async removal actually happens, because the 798 * DE is using this information to discard previous I/O 799 * errors. 800 */ 801 zfs_post_remove(zio->io_spa, vd); 802 vd->vdev_remove_wanted = B_TRUE; 803 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 804 } else if (!vd->vdev_delayed_close) { 805 vd->vdev_delayed_close = B_TRUE; 806 } 807 } 808} 809 810vdev_ops_t vdev_disk_ops = { 811 vdev_disk_open, 812 vdev_disk_close, 813 vdev_default_asize, 814 vdev_disk_io_start, 815 vdev_disk_io_done, 816 NULL, 817 vdev_disk_hold, 818 vdev_disk_rele, 819 VDEV_TYPE_DISK, /* name of this vdev type */ 820 B_TRUE /* leaf vdev */ 821}; 822 823/* 824 * Given the root disk device devid or pathname, read the label from 825 * the device, and construct a configuration nvlist. 826 */ 827int 828vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 829{ 830 ldi_handle_t vd_lh; 831 vdev_label_t *label; 832 uint64_t s, size; 833 int l; 834 ddi_devid_t tmpdevid; 835 int error = -1; 836 char *minor_name; 837 838 /* 839 * Read the device label and build the nvlist. 840 */ 841 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 842 &minor_name) == 0) { 843 error = ldi_open_by_devid(tmpdevid, minor_name, 844 FREAD, kcred, &vd_lh, zfs_li); 845 ddi_devid_free(tmpdevid); 846 ddi_devid_str_free(minor_name); 847 } 848 849 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 850 zfs_li))) 851 return (error); 852 853 if (ldi_get_size(vd_lh, &s)) { 854 (void) ldi_close(vd_lh, FREAD, kcred); 855 return (SET_ERROR(EIO)); 856 } 857 858 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 859 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 860 861 *config = NULL; 862 for (l = 0; l < VDEV_LABELS; l++) { 863 uint64_t offset, state, txg = 0; 864 865 /* read vdev label */ 866 offset = vdev_label_offset(size, l, 0); 867 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label, 868 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 869 continue; 870 871 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 872 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 873 *config = NULL; 874 continue; 875 } 876 877 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 878 &state) != 0 || state >= POOL_STATE_DESTROYED) { 879 nvlist_free(*config); 880 *config = NULL; 881 continue; 882 } 883 884 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 885 &txg) != 0 || txg == 0) { 886 nvlist_free(*config); 887 *config = NULL; 888 continue; 889 } 890 891 break; 892 } 893 894 kmem_free(label, sizeof (vdev_label_t)); 895 (void) ldi_close(vd_lh, FREAD, kcred); 896 if (*config == NULL) 897 error = SET_ERROR(EIDRM); 898 899 return (error); 900} 901