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