1 /*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_geom.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 #include <sys/bio.h> 46 #include <sys/ctype.h> 47 #include <sys/fcntl.h> 48 #include <sys/malloc.h> 49 #include <sys/sbuf.h> 50 #include <sys/sysctl.h> 51 #include <sys/devicestat.h> 52 #include <machine/md_var.h> 53 54 #include <sys/lock.h> 55 #include <sys/mutex.h> 56 #include <geom/geom.h> 57 #include <geom/geom_disk.h> 58 #include <geom/geom_int.h> 59 60 #include <dev/led/led.h> 61 62 struct g_disk_softc { 63 struct disk *dp; 64 struct sysctl_ctx_list sysctl_ctx; 65 struct sysctl_oid *sysctl_tree; 66 char led[64]; 67 uint32_t state; 68 }; 69 70 static struct mtx g_disk_done_mtx; 71 72 static g_access_t g_disk_access; 73 static g_init_t g_disk_init; 74 static g_fini_t g_disk_fini; 75 static g_start_t g_disk_start; 76 static g_ioctl_t g_disk_ioctl; 77 static g_dumpconf_t g_disk_dumpconf; 78 79 static struct g_class g_disk_class = { 80 .name = "DISK", 81 .version = G_VERSION, 82 .init = g_disk_init, 83 .fini = g_disk_fini, 84 .start = g_disk_start, 85 .access = g_disk_access, 86 .ioctl = g_disk_ioctl, 87 .dumpconf = g_disk_dumpconf, 88 }; 89 90 SYSCTL_DECL(_kern_geom); 91 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0, 92 "GEOM_DISK stuff"); 93 94 static void 95 g_disk_init(struct g_class *mp __unused) 96 { 97 98 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 99 } 100 101 static void 102 g_disk_fini(struct g_class *mp __unused) 103 { 104 105 mtx_destroy(&g_disk_done_mtx); 106 } 107 108 DECLARE_GEOM_CLASS(g_disk_class, g_disk); 109 110 static void __inline 111 g_disk_lock_giant(struct disk *dp) 112 { 113 114 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 115 mtx_lock(&Giant); 116 } 117 118 static void __inline 119 g_disk_unlock_giant(struct disk *dp) 120 { 121 122 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 123 mtx_unlock(&Giant); 124 } 125 126 static int 127 g_disk_access(struct g_provider *pp, int r, int w, int e) 128 { 129 struct disk *dp; 130 struct g_disk_softc *sc; 131 int error; 132 133 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 134 pp->name, r, w, e); 135 g_topology_assert(); 136 sc = pp->geom->softc; 137 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { 138 /* 139 * Allow decreasing access count even if disk is not 140 * avaliable anymore. 141 */ 142 if (r <= 0 && w <= 0 && e <= 0) 143 return (0); 144 return (ENXIO); 145 } 146 r += pp->acr; 147 w += pp->acw; 148 e += pp->ace; 149 error = 0; 150 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 151 if (dp->d_open != NULL) { 152 g_disk_lock_giant(dp); 153 error = dp->d_open(dp); 154 if (bootverbose && error != 0) 155 printf("Opened disk %s -> %d\n", 156 pp->name, error); 157 g_disk_unlock_giant(dp); 158 } 159 pp->mediasize = dp->d_mediasize; 160 pp->sectorsize = dp->d_sectorsize; 161 if (dp->d_flags & DISKFLAG_CANDELETE) 162 pp->flags |= G_PF_CANDELETE; 163 else 164 pp->flags &= ~G_PF_CANDELETE; 165 pp->stripeoffset = dp->d_stripeoffset; 166 pp->stripesize = dp->d_stripesize; 167 dp->d_flags |= DISKFLAG_OPEN; 168 if (dp->d_maxsize == 0) { 169 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 170 dp->d_name, dp->d_unit); 171 dp->d_maxsize = DFLTPHYS; 172 } 173 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 174 if (dp->d_close != NULL) { 175 g_disk_lock_giant(dp); 176 error = dp->d_close(dp); 177 if (error != 0) 178 printf("Closed disk %s -> %d\n", 179 pp->name, error); 180 g_disk_unlock_giant(dp); 181 } 182 sc->state = G_STATE_ACTIVE; 183 if (sc->led[0] != 0) 184 led_set(sc->led, "0"); 185 dp->d_flags &= ~DISKFLAG_OPEN; 186 } 187 return (error); 188 } 189 190 static void 191 g_disk_kerneldump(struct bio *bp, struct disk *dp) 192 { 193 struct g_kerneldump *gkd; 194 struct g_geom *gp; 195 196 gkd = (struct g_kerneldump*)bp->bio_data; 197 gp = bp->bio_to->geom; 198 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 199 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 200 if (dp->d_dump == NULL) { 201 g_io_deliver(bp, ENODEV); 202 return; 203 } 204 gkd->di.dumper = dp->d_dump; 205 gkd->di.priv = dp; 206 gkd->di.blocksize = dp->d_sectorsize; 207 gkd->di.maxiosize = dp->d_maxsize; 208 gkd->di.mediaoffset = gkd->offset; 209 if ((gkd->offset + gkd->length) > dp->d_mediasize) 210 gkd->length = dp->d_mediasize - gkd->offset; 211 gkd->di.mediasize = gkd->length; 212 g_io_deliver(bp, 0); 213 } 214 215 static void 216 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc) 217 { 218 const char *cmd; 219 220 memcpy(&sc->state, bp->bio_data, sizeof(sc->state)); 221 if (sc->led[0] != 0) { 222 switch (sc->state) { 223 case G_STATE_FAILED: 224 cmd = "1"; 225 break; 226 case G_STATE_REBUILD: 227 cmd = "f5"; 228 break; 229 case G_STATE_RESYNC: 230 cmd = "f1"; 231 break; 232 default: 233 cmd = "0"; 234 break; 235 } 236 led_set(sc->led, cmd); 237 } 238 g_io_deliver(bp, 0); 239 } 240 241 static void 242 g_disk_done(struct bio *bp) 243 { 244 struct bio *bp2; 245 struct disk *dp; 246 struct g_disk_softc *sc; 247 248 /* See "notes" for why we need a mutex here */ 249 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 250 mtx_lock(&g_disk_done_mtx); 251 bp->bio_completed = bp->bio_length - bp->bio_resid; 252 253 bp2 = bp->bio_parent; 254 if (bp2->bio_error == 0) 255 bp2->bio_error = bp->bio_error; 256 bp2->bio_completed += bp->bio_completed; 257 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0 && 258 (sc = bp2->bio_to->geom->softc) != NULL && 259 (dp = sc->dp) != NULL) { 260 devstat_end_transaction_bio(dp->d_devstat, bp); 261 } 262 g_destroy_bio(bp); 263 bp2->bio_inbed++; 264 if (bp2->bio_children == bp2->bio_inbed) { 265 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 266 g_io_deliver(bp2, bp2->bio_error); 267 } 268 mtx_unlock(&g_disk_done_mtx); 269 } 270 271 static int 272 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 273 { 274 struct g_geom *gp; 275 struct disk *dp; 276 struct g_disk_softc *sc; 277 int error; 278 279 gp = pp->geom; 280 sc = gp->softc; 281 dp = sc->dp; 282 283 if (dp->d_ioctl == NULL) 284 return (ENOIOCTL); 285 g_disk_lock_giant(dp); 286 error = dp->d_ioctl(dp, cmd, data, fflag, td); 287 g_disk_unlock_giant(dp); 288 return (error); 289 } 290 291 static void 292 g_disk_start(struct bio *bp) 293 { 294 struct bio *bp2, *bp3; 295 struct disk *dp; 296 struct g_disk_softc *sc; 297 int error; 298 off_t off; 299 300 sc = bp->bio_to->geom->softc; 301 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { 302 g_io_deliver(bp, ENXIO); 303 return; 304 } 305 error = EJUSTRETURN; 306 switch(bp->bio_cmd) { 307 case BIO_DELETE: 308 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 309 error = EOPNOTSUPP; 310 break; 311 } 312 /* fall-through */ 313 case BIO_READ: 314 case BIO_WRITE: 315 off = 0; 316 bp3 = NULL; 317 bp2 = g_clone_bio(bp); 318 if (bp2 == NULL) { 319 error = ENOMEM; 320 break; 321 } 322 do { 323 bp2->bio_offset += off; 324 bp2->bio_length -= off; 325 bp2->bio_data += off; 326 if (bp2->bio_length > dp->d_maxsize) { 327 /* 328 * XXX: If we have a stripesize we should really 329 * use it here. 330 */ 331 bp2->bio_length = dp->d_maxsize; 332 off += dp->d_maxsize; 333 /* 334 * To avoid a race, we need to grab the next bio 335 * before we schedule this one. See "notes". 336 */ 337 bp3 = g_clone_bio(bp); 338 if (bp3 == NULL) 339 bp->bio_error = ENOMEM; 340 } 341 bp2->bio_done = g_disk_done; 342 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 343 bp2->bio_bcount = bp2->bio_length; 344 bp2->bio_disk = dp; 345 devstat_start_transaction_bio(dp->d_devstat, bp2); 346 g_disk_lock_giant(dp); 347 dp->d_strategy(bp2); 348 g_disk_unlock_giant(dp); 349 bp2 = bp3; 350 bp3 = NULL; 351 } while (bp2 != NULL); 352 break; 353 case BIO_GETATTR: 354 /* Give the driver a chance to override */ 355 if (dp->d_getattr != NULL) { 356 if (bp->bio_disk == NULL) 357 bp->bio_disk = dp; 358 error = dp->d_getattr(bp); 359 if (error != -1) 360 break; 361 error = EJUSTRETURN; 362 } 363 if (g_handleattr_int(bp, "GEOM::candelete", 364 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 365 break; 366 else if (g_handleattr_int(bp, "GEOM::fwsectors", 367 dp->d_fwsectors)) 368 break; 369 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 370 break; 371 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 372 break; 373 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 374 break; 375 else if (g_handleattr(bp, "GEOM::hba_vendor", 376 &dp->d_hba_vendor, 2)) 377 break; 378 else if (g_handleattr(bp, "GEOM::hba_device", 379 &dp->d_hba_device, 2)) 380 break; 381 else if (g_handleattr(bp, "GEOM::hba_subvendor", 382 &dp->d_hba_subvendor, 2)) 383 break; 384 else if (g_handleattr(bp, "GEOM::hba_subdevice", 385 &dp->d_hba_subdevice, 2)) 386 break; 387 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 388 g_disk_kerneldump(bp, dp); 389 else if (!strcmp(bp->bio_attribute, "GEOM::setstate")) 390 g_disk_setstate(bp, sc); 391 else 392 error = ENOIOCTL; 393 break; 394 case BIO_FLUSH: 395 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 396 bp->bio_to->name); 397 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 398 error = EOPNOTSUPP; 399 break; 400 } 401 bp2 = g_clone_bio(bp); 402 if (bp2 == NULL) { 403 g_io_deliver(bp, ENOMEM); 404 return; 405 } 406 bp2->bio_done = g_disk_done; 407 bp2->bio_disk = dp; 408 g_disk_lock_giant(dp); 409 dp->d_strategy(bp2); 410 g_disk_unlock_giant(dp); 411 break; 412 default: 413 error = EOPNOTSUPP; 414 break; 415 } 416 if (error != EJUSTRETURN) 417 g_io_deliver(bp, error); 418 return; 419 } 420 421 static void 422 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 423 { 424 struct disk *dp; 425 struct g_disk_softc *sc; 426 427 sc = gp->softc; 428 if (sc == NULL || (dp = sc->dp) == NULL) 429 return; 430 if (indent == NULL) { 431 sbuf_printf(sb, " hd %u", dp->d_fwheads); 432 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 433 return; 434 } 435 if (pp != NULL) { 436 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 437 indent, dp->d_fwheads); 438 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 439 indent, dp->d_fwsectors); 440 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident); 441 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr); 442 } 443 } 444 445 static void 446 g_disk_create(void *arg, int flag) 447 { 448 struct g_geom *gp; 449 struct g_provider *pp; 450 struct disk *dp; 451 struct g_disk_softc *sc; 452 char tmpstr[80]; 453 454 if (flag == EV_CANCEL) 455 return; 456 g_topology_assert(); 457 dp = arg; 458 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 459 sc->dp = dp; 460 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 461 gp->softc = sc; 462 pp = g_new_providerf(gp, "%s", gp->name); 463 pp->mediasize = dp->d_mediasize; 464 pp->sectorsize = dp->d_sectorsize; 465 if (dp->d_flags & DISKFLAG_CANDELETE) 466 pp->flags |= G_PF_CANDELETE; 467 pp->stripeoffset = dp->d_stripeoffset; 468 pp->stripesize = dp->d_stripesize; 469 if (bootverbose) 470 printf("GEOM: new disk %s\n", gp->name); 471 sysctl_ctx_init(&sc->sysctl_ctx); 472 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name); 473 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 474 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name, 475 CTLFLAG_RD, 0, tmpstr); 476 if (sc->sysctl_tree != NULL) { 477 snprintf(tmpstr, sizeof(tmpstr), 478 "kern.geom.disk.%s.led", gp->name); 479 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led)); 480 SYSCTL_ADD_STRING(&sc->sysctl_ctx, 481 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led", 482 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led), 483 "LED name"); 484 } 485 pp->private = sc; 486 dp->d_geom = gp; 487 g_error_provider(pp, 0); 488 } 489 490 static void 491 g_disk_destroy(void *ptr, int flag) 492 { 493 struct disk *dp; 494 struct g_geom *gp; 495 struct g_disk_softc *sc; 496 497 g_topology_assert(); 498 dp = ptr; 499 gp = dp->d_geom; 500 if (gp != NULL) { 501 sc = gp->softc; 502 if (sc->sysctl_tree != NULL) { 503 sysctl_ctx_free(&sc->sysctl_ctx); 504 sc->sysctl_tree = NULL; 505 } 506 if (sc->led[0] != 0) { 507 led_set(sc->led, "0"); 508 sc->led[0] = 0; 509 } 510 g_free(sc); 511 gp->softc = NULL; 512 g_wither_geom(gp, ENXIO); 513 } 514 g_free(dp); 515 } 516 517 /* 518 * We only allow printable characters in disk ident, 519 * the rest is converted to 'x<HH>'. 520 */ 521 static void 522 g_disk_ident_adjust(char *ident, size_t size) 523 { 524 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 525 526 newid[0] = '\0'; 527 for (p = ident; *p != '\0'; p++) { 528 if (isprint(*p)) { 529 tmp[0] = *p; 530 tmp[1] = '\0'; 531 } else { 532 snprintf(tmp, sizeof(tmp), "x%02hhx", 533 *(unsigned char *)p); 534 } 535 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 536 break; 537 } 538 bzero(ident, size); 539 strlcpy(ident, newid, size); 540 } 541 542 struct disk * 543 disk_alloc(void) 544 { 545 546 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO)); 547 } 548 549 void 550 disk_create(struct disk *dp, int version) 551 { 552 553 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) { 554 printf("WARNING: Attempt to add disk %s%d %s", 555 dp->d_name, dp->d_unit, 556 " using incompatible ABI version of disk(9)\n"); 557 printf("WARNING: Ignoring disk %s%d\n", 558 dp->d_name, dp->d_unit); 559 return; 560 } 561 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 562 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 563 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 564 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 565 if (dp->d_devstat == NULL) 566 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 567 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 568 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 569 dp->d_geom = NULL; 570 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 571 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 572 } 573 574 void 575 disk_destroy(struct disk *dp) 576 { 577 578 g_cancel_event(dp); 579 dp->d_destroyed = 1; 580 if (dp->d_devstat != NULL) 581 devstat_remove_entry(dp->d_devstat); 582 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 583 } 584 585 void 586 disk_gone(struct disk *dp) 587 { 588 struct g_geom *gp; 589 struct g_provider *pp; 590 591 gp = dp->d_geom; 592 if (gp != NULL) 593 LIST_FOREACH(pp, &gp->provider, provider) 594 g_wither_provider(pp, ENXIO); 595 } 596 597 void 598 disk_attr_changed(struct disk *dp, const char *attr, int flag) 599 { 600 struct g_geom *gp; 601 struct g_provider *pp; 602 603 gp = dp->d_geom; 604 if (gp != NULL) 605 LIST_FOREACH(pp, &gp->provider, provider) 606 (void)g_attr_changed(pp, attr, flag); 607 } 608 609 static void 610 g_kern_disks(void *p, int flag __unused) 611 { 612 struct sbuf *sb; 613 struct g_geom *gp; 614 char *sp; 615 616 sb = p; 617 sp = ""; 618 g_topology_assert(); 619 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 620 sbuf_printf(sb, "%s%s", sp, gp->name); 621 sp = " "; 622 } 623 sbuf_finish(sb); 624 } 625 626 static int 627 sysctl_disks(SYSCTL_HANDLER_ARGS) 628 { 629 int error; 630 struct sbuf *sb; 631 632 sb = sbuf_new_auto(); 633 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 634 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 635 sbuf_delete(sb); 636 return error; 637 } 638 639 SYSCTL_PROC(_kern, OID_AUTO, disks, 640 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 641 sysctl_disks, "A", "names of available disks"); 642