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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/zfs_context.h> 27 #include <sys/spa.h> 28 #include <sys/refcount.h> 29 #include <sys/vdev_disk.h> 30 #include <sys/vdev_impl.h> 31 #include <sys/fs/zfs.h> 32 #include <sys/zio.h> 33 #include <sys/sunldi.h> 34 #include <sys/fm/fs/zfs.h> 35 36 /* 37 * Virtual device vector for disks. 38 */ 39 40 extern ldi_ident_t zfs_li; 41 42 typedef struct vdev_disk_buf { 43 buf_t vdb_buf; 44 zio_t *vdb_io; 45 } vdev_disk_buf_t; 46 47 static int 48 vdev_disk_open_common(vdev_t *vd) 49 { 50 vdev_disk_t *dvd; 51 dev_t dev; 52 int error; 53 54 /* 55 * We must have a pathname, and it must be absolute. 56 */ 57 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 58 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 59 return (EINVAL); 60 } 61 62 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 63 64 /* 65 * When opening a disk device, we want to preserve the user's original 66 * intent. We always want to open the device by the path the user gave 67 * us, even if it is one of multiple paths to the save device. But we 68 * also want to be able to survive disks being removed/recabled. 69 * Therefore the sequence of opening devices is: 70 * 71 * 1. Try opening the device by path. For legacy pools without the 72 * 'whole_disk' property, attempt to fix the path by appending 's0'. 73 * 74 * 2. If the devid of the device matches the stored value, return 75 * success. 76 * 77 * 3. Otherwise, the device may have moved. Try opening the device 78 * by the devid instead. 79 * 80 * If the vdev is part of the root pool, we avoid opening it by path. 81 * We do this because there is no /dev path available early in boot, 82 * and if we try to open the device by path at a later point, we can 83 * deadlock when devfsadm attempts to open the underlying backing store 84 * file. 85 */ 86 if (vd->vdev_devid != NULL) { 87 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 88 &dvd->vd_minor) != 0) { 89 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 90 return (EINVAL); 91 } 92 } 93 94 error = EINVAL; /* presume failure */ 95 96 if (vd->vdev_path != NULL && !spa_is_root(vd->vdev_spa)) { 97 ddi_devid_t devid; 98 99 if (vd->vdev_wholedisk == -1ULL) { 100 size_t len = strlen(vd->vdev_path) + 3; 101 char *buf = kmem_alloc(len, KM_SLEEP); 102 ldi_handle_t lh; 103 104 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 105 106 if (ldi_open_by_name(buf, spa_mode, kcred, 107 &lh, zfs_li) == 0) { 108 spa_strfree(vd->vdev_path); 109 vd->vdev_path = buf; 110 vd->vdev_wholedisk = 1ULL; 111 (void) ldi_close(lh, spa_mode, kcred); 112 } else { 113 kmem_free(buf, len); 114 } 115 } 116 117 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 118 &dvd->vd_lh, zfs_li); 119 120 /* 121 * Compare the devid to the stored value. 122 */ 123 if (error == 0 && vd->vdev_devid != NULL && 124 ldi_get_devid(dvd->vd_lh, &devid) == 0) { 125 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 126 error = EINVAL; 127 (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 128 dvd->vd_lh = NULL; 129 } 130 ddi_devid_free(devid); 131 } 132 133 /* 134 * If we succeeded in opening the device, but 'vdev_wholedisk' 135 * is not yet set, then this must be a slice. 136 */ 137 if (error == 0 && vd->vdev_wholedisk == -1ULL) 138 vd->vdev_wholedisk = 0; 139 } 140 141 /* 142 * If we were unable to open by path, or the devid check fails, open by 143 * devid instead. 144 */ 145 if (error != 0 && vd->vdev_devid != NULL) 146 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 147 spa_mode, kcred, &dvd->vd_lh, zfs_li); 148 149 /* 150 * If all else fails, then try opening by physical path (if available) 151 * or the logical path (if we failed due to the devid check). While not 152 * as reliable as the devid, this will give us something, and the higher 153 * level vdev validation will prevent us from opening the wrong device. 154 */ 155 if (error) { 156 if (vd->vdev_physpath != NULL && 157 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV) 158 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode, 159 kcred, &dvd->vd_lh, zfs_li); 160 161 /* 162 * Note that we don't support the legacy auto-wholedisk support 163 * as above. This hasn't been used in a very long time and we 164 * don't need to propagate its oddities to this edge condition. 165 */ 166 if (error && vd->vdev_path != NULL && 167 !spa_is_root(vd->vdev_spa)) 168 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 169 &dvd->vd_lh, zfs_li); 170 } 171 172 if (error) 173 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 174 175 return (error); 176 } 177 178 static int 179 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 180 { 181 vdev_disk_t *dvd; 182 struct dk_minfo dkm; 183 int error; 184 dev_t dev; 185 int otyp; 186 187 error = vdev_disk_open_common(vd); 188 if (error) 189 return (error); 190 191 dvd = vd->vdev_tsd; 192 /* 193 * Once a device is opened, verify that the physical device path (if 194 * available) is up to date. 195 */ 196 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 197 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 198 char *physpath, *minorname; 199 200 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 201 minorname = NULL; 202 if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 203 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 204 (vd->vdev_physpath == NULL || 205 strcmp(vd->vdev_physpath, physpath) != 0)) { 206 if (vd->vdev_physpath) 207 spa_strfree(vd->vdev_physpath); 208 (void) strlcat(physpath, ":", MAXPATHLEN); 209 (void) strlcat(physpath, minorname, MAXPATHLEN); 210 vd->vdev_physpath = spa_strdup(physpath); 211 } 212 if (minorname) 213 kmem_free(minorname, strlen(minorname) + 1); 214 kmem_free(physpath, MAXPATHLEN); 215 } 216 217 /* 218 * Determine the actual size of the device. 219 */ 220 if (ldi_get_size(dvd->vd_lh, psize) != 0) { 221 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 222 return (EINVAL); 223 } 224 225 /* 226 * If we own the whole disk, try to enable disk write caching. 227 * We ignore errors because it's OK if we can't do it. 228 */ 229 if (vd->vdev_wholedisk == 1) { 230 int wce = 1; 231 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 232 FKIOCTL, kcred, NULL); 233 } 234 235 /* 236 * Determine the device's minimum transfer size. 237 * If the ioctl isn't supported, assume DEV_BSIZE. 238 */ 239 if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 240 FKIOCTL, kcred, NULL) != 0) 241 dkm.dki_lbsize = DEV_BSIZE; 242 243 *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 244 245 /* 246 * Clear the nowritecache bit, so that on a vdev_reopen() we will 247 * try again. 248 */ 249 vd->vdev_nowritecache = B_FALSE; 250 251 return (0); 252 } 253 254 static void 255 vdev_disk_close(vdev_t *vd) 256 { 257 vdev_disk_t *dvd = vd->vdev_tsd; 258 259 if (dvd == NULL) 260 return; 261 262 if (dvd->vd_minor != NULL) 263 ddi_devid_str_free(dvd->vd_minor); 264 265 if (dvd->vd_devid != NULL) 266 ddi_devid_free(dvd->vd_devid); 267 268 if (dvd->vd_lh != NULL) 269 (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 270 271 kmem_free(dvd, sizeof (vdev_disk_t)); 272 vd->vdev_tsd = NULL; 273 } 274 275 int 276 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 277 uint64_t offset, int flags) 278 { 279 buf_t *bp; 280 int error = 0; 281 282 if (vd_lh == NULL) 283 return (EINVAL); 284 285 ASSERT(flags & B_READ || flags & B_WRITE); 286 287 bp = getrbuf(KM_SLEEP); 288 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 289 bp->b_bcount = size; 290 bp->b_un.b_addr = (void *)data; 291 bp->b_lblkno = lbtodb(offset); 292 bp->b_bufsize = size; 293 294 error = ldi_strategy(vd_lh, bp); 295 ASSERT(error == 0); 296 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 297 error = EIO; 298 freerbuf(bp); 299 300 return (error); 301 } 302 303 static int 304 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset, 305 int flags) 306 { 307 int error = 0; 308 vdev_disk_t *dvd = vd ? vd->vdev_tsd : NULL; 309 310 if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL) 311 return (EINVAL); 312 313 error = vdev_disk_physio(dvd->vd_lh, data, size, offset, flags); 314 315 if (zio_injection_enabled && error == 0) 316 error = zio_handle_device_injection(vd, EIO); 317 318 return (error); 319 } 320 321 /* 322 * Determine if the underlying device is accessible by reading and writing 323 * to a known location. We must be able to do this during syncing context 324 * and thus we cannot set the vdev state directly. 325 */ 326 static int 327 vdev_disk_probe(vdev_t *vd) 328 { 329 uint64_t offset; 330 vdev_t *nvd; 331 int l, error = 0, retries = 0; 332 char *vl_pad; 333 334 if (vd == NULL) 335 return (EINVAL); 336 337 /* Hijack the current vdev */ 338 nvd = vd; 339 340 /* 341 * Pick a random label to rewrite. 342 */ 343 l = spa_get_random(VDEV_LABELS); 344 ASSERT(l < VDEV_LABELS); 345 346 offset = vdev_label_offset(vd->vdev_psize, l, 347 offsetof(vdev_label_t, vl_pad)); 348 349 vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP); 350 351 /* 352 * Try to read and write to a special location on the 353 * label. We use the existing vdev initially and only 354 * try to create and reopen it if we encounter a failure. 355 */ 356 while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE, 357 offset, B_READ)) != 0 && retries == 0) { 358 359 nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP); 360 if (vd->vdev_path) 361 nvd->vdev_path = spa_strdup(vd->vdev_path); 362 if (vd->vdev_physpath) 363 nvd->vdev_physpath = spa_strdup(vd->vdev_physpath); 364 if (vd->vdev_devid) 365 nvd->vdev_devid = spa_strdup(vd->vdev_devid); 366 nvd->vdev_wholedisk = vd->vdev_wholedisk; 367 nvd->vdev_guid = vd->vdev_guid; 368 nvd->vdev_spa = vd->vdev_spa; 369 retries++; 370 371 error = vdev_disk_open_common(nvd); 372 if (error) 373 break; 374 } 375 376 if (!error) { 377 error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE, 378 offset, B_WRITE); 379 } 380 381 /* Clean up if we allocated a new vdev */ 382 if (retries) { 383 vdev_disk_close(nvd); 384 if (nvd->vdev_path) 385 spa_strfree(nvd->vdev_path); 386 if (nvd->vdev_physpath) 387 spa_strfree(nvd->vdev_physpath); 388 if (nvd->vdev_devid) 389 spa_strfree(nvd->vdev_devid); 390 kmem_free(nvd, sizeof (vdev_t)); 391 } 392 kmem_free(vl_pad, VDEV_SKIP_SIZE); 393 394 /* Reset the failing flag */ 395 if (!error) 396 vd->vdev_is_failing = B_FALSE; 397 398 return (error); 399 } 400 401 static void 402 vdev_disk_io_intr(buf_t *bp) 403 { 404 vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 405 zio_t *zio = vdb->vdb_io; 406 407 /* 408 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 409 * Rather than teach the rest of the stack about other error 410 * possibilities (EFAULT, etc), we normalize the error value here. 411 */ 412 zio->io_error = (geterror(bp) != 0 ? EIO : 0); 413 414 if (zio->io_error == 0 && bp->b_resid != 0) 415 zio->io_error = EIO; 416 417 kmem_free(vdb, sizeof (vdev_disk_buf_t)); 418 419 zio_interrupt(zio); 420 } 421 422 static void 423 vdev_disk_ioctl_done(void *zio_arg, int error) 424 { 425 zio_t *zio = zio_arg; 426 427 zio->io_error = error; 428 429 zio_interrupt(zio); 430 } 431 432 static int 433 vdev_disk_io_start(zio_t *zio) 434 { 435 vdev_t *vd = zio->io_vd; 436 vdev_disk_t *dvd = vd->vdev_tsd; 437 vdev_disk_buf_t *vdb; 438 buf_t *bp; 439 int flags, error; 440 441 if (zio->io_type == ZIO_TYPE_IOCTL) { 442 zio_vdev_io_bypass(zio); 443 444 /* XXPOLICY */ 445 if (!vdev_readable(vd)) { 446 zio->io_error = ENXIO; 447 return (ZIO_PIPELINE_CONTINUE); 448 } 449 450 switch (zio->io_cmd) { 451 452 case DKIOCFLUSHWRITECACHE: 453 454 if (zfs_nocacheflush) 455 break; 456 457 if (vd->vdev_nowritecache) { 458 zio->io_error = ENOTSUP; 459 break; 460 } 461 462 zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done; 463 zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE; 464 zio->io_dk_callback.dkc_cookie = zio; 465 466 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 467 (uintptr_t)&zio->io_dk_callback, 468 FKIOCTL, kcred, NULL); 469 470 if (error == 0) { 471 /* 472 * The ioctl will be done asychronously, 473 * and will call vdev_disk_ioctl_done() 474 * upon completion. 475 */ 476 return (ZIO_PIPELINE_STOP); 477 } 478 479 if (error == ENOTSUP || error == ENOTTY) { 480 /* 481 * If we get ENOTSUP or ENOTTY, we know that 482 * no future attempts will ever succeed. 483 * In this case we set a persistent bit so 484 * that we don't bother with the ioctl in the 485 * future. 486 */ 487 vd->vdev_nowritecache = B_TRUE; 488 } 489 zio->io_error = error; 490 491 break; 492 493 default: 494 zio->io_error = ENOTSUP; 495 } 496 497 return (ZIO_PIPELINE_CONTINUE); 498 } 499 500 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0) 501 return (ZIO_PIPELINE_STOP); 502 503 if ((zio = vdev_queue_io(zio)) == NULL) 504 return (ZIO_PIPELINE_STOP); 505 506 if (zio->io_type == ZIO_TYPE_WRITE) 507 error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO; 508 else 509 error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO; 510 error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error; 511 512 if (error) { 513 zio->io_error = error; 514 zio_interrupt(zio); 515 return (ZIO_PIPELINE_STOP); 516 } 517 518 flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 519 flags |= B_BUSY | B_NOCACHE; 520 if (zio->io_flags & ZIO_FLAG_FAILFAST) 521 flags |= B_FAILFAST; 522 523 vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 524 525 vdb->vdb_io = zio; 526 bp = &vdb->vdb_buf; 527 528 bioinit(bp); 529 bp->b_flags = flags; 530 bp->b_bcount = zio->io_size; 531 bp->b_un.b_addr = zio->io_data; 532 bp->b_lblkno = lbtodb(zio->io_offset); 533 bp->b_bufsize = zio->io_size; 534 bp->b_iodone = (int (*)())vdev_disk_io_intr; 535 536 error = ldi_strategy(dvd->vd_lh, bp); 537 /* ldi_strategy() will return non-zero only on programming errors */ 538 ASSERT(error == 0); 539 540 return (ZIO_PIPELINE_STOP); 541 } 542 543 static int 544 vdev_disk_io_done(zio_t *zio) 545 { 546 vdev_queue_io_done(zio); 547 548 if (zio->io_type == ZIO_TYPE_WRITE) 549 vdev_cache_write(zio); 550 551 if (zio_injection_enabled && zio->io_error == 0) 552 zio->io_error = zio_handle_device_injection(zio->io_vd, EIO); 553 554 /* 555 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 556 * the device has been removed. If this is the case, then we trigger an 557 * asynchronous removal of the device. Otherwise, probe the device and 558 * make sure it's still accessible. 559 */ 560 if (zio->io_error == EIO) { 561 vdev_t *vd = zio->io_vd; 562 vdev_disk_t *dvd = vd->vdev_tsd; 563 int state; 564 565 state = DKIO_NONE; 566 if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 567 FKIOCTL, kcred, NULL) == 0 && 568 state != DKIO_INSERTED) { 569 vd->vdev_remove_wanted = B_TRUE; 570 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 571 } else if (vdev_probe(vd) != 0) { 572 ASSERT(vd->vdev_ops->vdev_op_leaf); 573 if (!vd->vdev_is_failing) { 574 vd->vdev_is_failing = B_TRUE; 575 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE, 576 vd->vdev_spa, vd, zio, 0, 0); 577 } 578 } 579 } 580 581 if (zio_injection_enabled && zio->io_error == 0) 582 zio->io_error = zio_handle_label_injection(zio, EIO); 583 584 return (ZIO_PIPELINE_CONTINUE); 585 } 586 587 vdev_ops_t vdev_disk_ops = { 588 vdev_disk_open, 589 vdev_disk_close, 590 vdev_disk_probe, 591 vdev_default_asize, 592 vdev_disk_io_start, 593 vdev_disk_io_done, 594 NULL, 595 VDEV_TYPE_DISK, /* name of this vdev type */ 596 B_TRUE /* leaf vdev */ 597 }; 598 599 /* 600 * Given the root disk device devid or pathname, read the label from 601 * the device, and construct a configuration nvlist. 602 */ 603 int 604 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 605 { 606 ldi_handle_t vd_lh; 607 vdev_label_t *label; 608 uint64_t s, size; 609 int l; 610 ddi_devid_t tmpdevid; 611 int error; 612 char *minor_name; 613 614 /* 615 * Read the device label and build the nvlist. 616 */ 617 error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, zfs_li); 618 619 if (error && devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 620 &minor_name) == 0) { 621 error = ldi_open_by_devid(tmpdevid, minor_name, 622 spa_mode, kcred, &vd_lh, zfs_li); 623 ddi_devid_free(tmpdevid); 624 ddi_devid_str_free(minor_name); 625 } 626 627 if (error) 628 return (error); 629 630 if (ldi_get_size(vd_lh, &s)) { 631 (void) ldi_close(vd_lh, FREAD, kcred); 632 return (EIO); 633 } 634 635 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 636 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 637 638 for (l = 0; l < VDEV_LABELS; l++) { 639 uint64_t offset, state, txg = 0; 640 641 /* read vdev label */ 642 offset = vdev_label_offset(size, l, 0); 643 if (vdev_disk_physio(vd_lh, (caddr_t)label, 644 VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE + 645 VDEV_PHYS_SIZE, offset, B_READ) != 0) 646 continue; 647 648 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 649 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 650 *config = NULL; 651 continue; 652 } 653 654 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 655 &state) != 0 || state >= POOL_STATE_DESTROYED) { 656 nvlist_free(*config); 657 *config = NULL; 658 continue; 659 } 660 661 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 662 &txg) != 0 || txg == 0) { 663 nvlist_free(*config); 664 *config = NULL; 665 continue; 666 } 667 668 break; 669 } 670 671 kmem_free(label, sizeof (vdev_label_t)); 672 (void) ldi_close(vd_lh, FREAD, kcred); 673 674 return (error); 675 } 676