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(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 49 { 50 vdev_disk_t *dvd; 51 struct dk_minfo dkm; 52 int error; 53 dev_t dev; 54 int otyp; 55 56 /* 57 * We must have a pathname, and it must be absolute. 58 */ 59 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 60 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 61 return (EINVAL); 62 } 63 64 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 65 66 /* 67 * When opening a disk device, we want to preserve the user's original 68 * intent. We always want to open the device by the path the user gave 69 * us, even if it is one of multiple paths to the save device. But we 70 * also want to be able to survive disks being removed/recabled. 71 * Therefore the sequence of opening devices is: 72 * 73 * 1. Try opening the device by path. For legacy pools without the 74 * 'whole_disk' property, attempt to fix the path by appending 's0'. 75 * 76 * 2. If the devid of the device matches the stored value, return 77 * success. 78 * 79 * 3. Otherwise, the device may have moved. Try opening the device 80 * by the devid instead. 81 * 82 * If the vdev is part of the root pool, we avoid opening it by path. 83 * We do this because there is no /dev path available early in boot, 84 * and if we try to open the device by path at a later point, we can 85 * deadlock when devfsadm attempts to open the underlying backing store 86 * file. 87 */ 88 if (vd->vdev_devid != NULL) { 89 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 90 &dvd->vd_minor) != 0) { 91 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 92 return (EINVAL); 93 } 94 } 95 96 error = EINVAL; /* presume failure */ 97 98 if (vd->vdev_path != NULL && !spa_is_root(vd->vdev_spa)) { 99 ddi_devid_t devid; 100 101 if (vd->vdev_wholedisk == -1ULL) { 102 size_t len = strlen(vd->vdev_path) + 3; 103 char *buf = kmem_alloc(len, KM_SLEEP); 104 ldi_handle_t lh; 105 106 (void) snprintf(buf, len, "%ss0", vd->vdev_path); 107 108 if (ldi_open_by_name(buf, spa_mode, kcred, 109 &lh, zfs_li) == 0) { 110 spa_strfree(vd->vdev_path); 111 vd->vdev_path = buf; 112 vd->vdev_wholedisk = 1ULL; 113 (void) ldi_close(lh, spa_mode, kcred); 114 } else { 115 kmem_free(buf, len); 116 } 117 } 118 119 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 120 &dvd->vd_lh, zfs_li); 121 122 /* 123 * Compare the devid to the stored value. 124 */ 125 if (error == 0 && vd->vdev_devid != NULL && 126 ldi_get_devid(dvd->vd_lh, &devid) == 0) { 127 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 128 error = EINVAL; 129 (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 130 dvd->vd_lh = NULL; 131 } 132 ddi_devid_free(devid); 133 } 134 135 /* 136 * If we succeeded in opening the device, but 'vdev_wholedisk' 137 * is not yet set, then this must be a slice. 138 */ 139 if (error == 0 && vd->vdev_wholedisk == -1ULL) 140 vd->vdev_wholedisk = 0; 141 } 142 143 /* 144 * If we were unable to open by path, or the devid check fails, open by 145 * devid instead. 146 */ 147 if (error != 0 && vd->vdev_devid != NULL) 148 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 149 spa_mode, kcred, &dvd->vd_lh, zfs_li); 150 151 /* 152 * If all else fails, then try opening by physical path (if available) 153 * or the logical path (if we failed due to the devid check). While not 154 * as reliable as the devid, this will give us something, and the higher 155 * level vdev validation will prevent us from opening the wrong device. 156 */ 157 if (error) { 158 if (vd->vdev_physpath != NULL && 159 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV) 160 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode, 161 kcred, &dvd->vd_lh, zfs_li); 162 163 /* 164 * Note that we don't support the legacy auto-wholedisk support 165 * as above. This hasn't been used in a very long time and we 166 * don't need to propagate its oddities to this edge condition. 167 */ 168 if (error && vd->vdev_path != NULL && 169 !spa_is_root(vd->vdev_spa)) 170 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 171 &dvd->vd_lh, zfs_li); 172 } 173 174 if (error) { 175 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 176 return (error); 177 } 178 179 /* 180 * Once a device is opened, verify that the physical device path (if 181 * available) is up to date. 182 */ 183 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 184 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 185 char *physpath, *minorname; 186 187 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 188 minorname = NULL; 189 if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 190 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 191 (vd->vdev_physpath == NULL || 192 strcmp(vd->vdev_physpath, physpath) != 0)) { 193 if (vd->vdev_physpath) 194 spa_strfree(vd->vdev_physpath); 195 (void) strlcat(physpath, ":", MAXPATHLEN); 196 (void) strlcat(physpath, minorname, MAXPATHLEN); 197 vd->vdev_physpath = spa_strdup(physpath); 198 } 199 if (minorname) 200 kmem_free(minorname, strlen(minorname) + 1); 201 kmem_free(physpath, MAXPATHLEN); 202 } 203 204 /* 205 * Determine the actual size of the device. 206 */ 207 if (ldi_get_size(dvd->vd_lh, psize) != 0) { 208 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 209 return (EINVAL); 210 } 211 212 /* 213 * If we own the whole disk, try to enable disk write caching. 214 * We ignore errors because it's OK if we can't do it. 215 */ 216 if (vd->vdev_wholedisk == 1) { 217 int wce = 1; 218 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 219 FKIOCTL, kcred, NULL); 220 } 221 222 /* 223 * Determine the device's minimum transfer size. 224 * If the ioctl isn't supported, assume DEV_BSIZE. 225 */ 226 if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 227 FKIOCTL, kcred, NULL) != 0) 228 dkm.dki_lbsize = DEV_BSIZE; 229 230 *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 231 232 /* 233 * Clear the nowritecache bit, so that on a vdev_reopen() we will 234 * try again. 235 */ 236 vd->vdev_nowritecache = B_FALSE; 237 238 return (0); 239 } 240 241 static void 242 vdev_disk_close(vdev_t *vd) 243 { 244 vdev_disk_t *dvd = vd->vdev_tsd; 245 246 if (dvd == NULL) 247 return; 248 249 if (dvd->vd_minor != NULL) 250 ddi_devid_str_free(dvd->vd_minor); 251 252 if (dvd->vd_devid != NULL) 253 ddi_devid_free(dvd->vd_devid); 254 255 if (dvd->vd_lh != NULL) 256 (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 257 258 kmem_free(dvd, sizeof (vdev_disk_t)); 259 vd->vdev_tsd = NULL; 260 } 261 262 int 263 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 264 uint64_t offset, int flags) 265 { 266 buf_t *bp; 267 int error = 0; 268 269 if (vd_lh == NULL) 270 return (EINVAL); 271 272 ASSERT(flags & B_READ || flags & B_WRITE); 273 274 bp = getrbuf(KM_SLEEP); 275 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 276 bp->b_bcount = size; 277 bp->b_un.b_addr = (void *)data; 278 bp->b_lblkno = lbtodb(offset); 279 bp->b_bufsize = size; 280 281 error = ldi_strategy(vd_lh, bp); 282 ASSERT(error == 0); 283 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 284 error = EIO; 285 freerbuf(bp); 286 287 return (error); 288 } 289 290 static void 291 vdev_disk_io_intr(buf_t *bp) 292 { 293 vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 294 zio_t *zio = vdb->vdb_io; 295 296 /* 297 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 298 * Rather than teach the rest of the stack about other error 299 * possibilities (EFAULT, etc), we normalize the error value here. 300 */ 301 zio->io_error = (geterror(bp) != 0 ? EIO : 0); 302 303 if (zio->io_error == 0 && bp->b_resid != 0) 304 zio->io_error = EIO; 305 306 kmem_free(vdb, sizeof (vdev_disk_buf_t)); 307 308 zio_interrupt(zio); 309 } 310 311 static void 312 vdev_disk_ioctl_done(void *zio_arg, int error) 313 { 314 zio_t *zio = zio_arg; 315 struct dk_callback *dkc = zio->io_vsd; 316 317 kmem_free(dkc, sizeof (*dkc)); 318 319 zio->io_vsd = NULL; 320 zio->io_error = error; 321 322 zio_interrupt(zio); 323 } 324 325 static int 326 vdev_disk_io_start(zio_t *zio) 327 { 328 vdev_t *vd = zio->io_vd; 329 vdev_disk_t *dvd = vd->vdev_tsd; 330 vdev_disk_buf_t *vdb; 331 struct dk_callback *dkc; 332 buf_t *bp; 333 int error; 334 335 if (zio->io_type == ZIO_TYPE_IOCTL) { 336 /* XXPOLICY */ 337 if (!vdev_readable(vd)) { 338 zio->io_error = ENXIO; 339 return (ZIO_PIPELINE_CONTINUE); 340 } 341 342 switch (zio->io_cmd) { 343 344 case DKIOCFLUSHWRITECACHE: 345 346 if (zfs_nocacheflush) 347 break; 348 349 if (vd->vdev_nowritecache) { 350 zio->io_error = ENOTSUP; 351 break; 352 } 353 354 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 355 356 dkc->dkc_callback = vdev_disk_ioctl_done; 357 dkc->dkc_flag = FLUSH_VOLATILE; 358 dkc->dkc_cookie = zio; 359 360 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 361 (uintptr_t)dkc, FKIOCTL, kcred, NULL); 362 363 if (error == 0) { 364 /* 365 * The ioctl will be done asychronously, 366 * and will call vdev_disk_ioctl_done() 367 * upon completion. 368 */ 369 return (ZIO_PIPELINE_STOP); 370 } 371 372 if (error == ENOTSUP || error == ENOTTY) { 373 /* 374 * If we get ENOTSUP or ENOTTY, we know that 375 * no future attempts will ever succeed. 376 * In this case we set a persistent bit so 377 * that we don't bother with the ioctl in the 378 * future. 379 */ 380 vd->vdev_nowritecache = B_TRUE; 381 } 382 zio->io_error = error; 383 384 break; 385 386 default: 387 zio->io_error = ENOTSUP; 388 } 389 390 return (ZIO_PIPELINE_CONTINUE); 391 } 392 393 vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 394 395 vdb->vdb_io = zio; 396 bp = &vdb->vdb_buf; 397 398 bioinit(bp); 399 bp->b_flags = B_BUSY | B_NOCACHE | 400 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE) | 401 ((zio->io_flags & ZIO_FLAG_IO_RETRY) ? 0 : B_FAILFAST); 402 bp->b_bcount = zio->io_size; 403 bp->b_un.b_addr = zio->io_data; 404 bp->b_lblkno = lbtodb(zio->io_offset); 405 bp->b_bufsize = zio->io_size; 406 bp->b_iodone = (int (*)())vdev_disk_io_intr; 407 408 /* ldi_strategy() will return non-zero only on programming errors */ 409 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0); 410 411 return (ZIO_PIPELINE_STOP); 412 } 413 414 static void 415 vdev_disk_io_done(zio_t *zio) 416 { 417 vdev_t *vd = zio->io_vd; 418 419 /* 420 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 421 * the device has been removed. If this is the case, then we trigger an 422 * asynchronous removal of the device. Otherwise, probe the device and 423 * make sure it's still accessible. 424 */ 425 if (zio->io_error == EIO) { 426 vdev_disk_t *dvd = vd->vdev_tsd; 427 int state = DKIO_NONE; 428 429 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 430 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 431 vd->vdev_remove_wanted = B_TRUE; 432 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 433 } 434 } 435 } 436 437 vdev_ops_t vdev_disk_ops = { 438 vdev_disk_open, 439 vdev_disk_close, 440 vdev_default_asize, 441 vdev_disk_io_start, 442 vdev_disk_io_done, 443 NULL, 444 VDEV_TYPE_DISK, /* name of this vdev type */ 445 B_TRUE /* leaf vdev */ 446 }; 447 448 /* 449 * Given the root disk device devid or pathname, read the label from 450 * the device, and construct a configuration nvlist. 451 */ 452 int 453 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 454 { 455 ldi_handle_t vd_lh; 456 vdev_label_t *label; 457 uint64_t s, size; 458 int l; 459 ddi_devid_t tmpdevid; 460 int error = -1; 461 char *minor_name; 462 463 /* 464 * Read the device label and build the nvlist. 465 */ 466 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 467 &minor_name) == 0) { 468 error = ldi_open_by_devid(tmpdevid, minor_name, 469 spa_mode, kcred, &vd_lh, zfs_li); 470 ddi_devid_free(tmpdevid); 471 ddi_devid_str_free(minor_name); 472 } 473 474 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 475 zfs_li))) 476 return (error); 477 478 if (ldi_get_size(vd_lh, &s)) { 479 (void) ldi_close(vd_lh, FREAD, kcred); 480 return (EIO); 481 } 482 483 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 484 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 485 486 for (l = 0; l < VDEV_LABELS; l++) { 487 uint64_t offset, state, txg = 0; 488 489 /* read vdev label */ 490 offset = vdev_label_offset(size, l, 0); 491 if (vdev_disk_physio(vd_lh, (caddr_t)label, 492 VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE + 493 VDEV_PHYS_SIZE, offset, B_READ) != 0) 494 continue; 495 496 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 497 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 498 *config = NULL; 499 continue; 500 } 501 502 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 503 &state) != 0 || state >= POOL_STATE_DESTROYED) { 504 nvlist_free(*config); 505 *config = NULL; 506 continue; 507 } 508 509 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 510 &txg) != 0 || txg == 0) { 511 nvlist_free(*config); 512 *config = NULL; 513 continue; 514 } 515 516 break; 517 } 518 519 kmem_free(label, sizeof (vdev_label_t)); 520 (void) ldi_close(vd_lh, FREAD, kcred); 521 522 return (error); 523 } 524