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/devicestat.h> 51 #include <machine/md_var.h> 52 53 #include <sys/lock.h> 54 #include <sys/mutex.h> 55 #include <geom/geom.h> 56 #include <geom/geom_disk.h> 57 #include <geom/geom_int.h> 58 59 #include <dev/led/led.h> 60 61 struct g_disk_softc { 62 struct mtx done_mtx; 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 struct mtx start_mtx; 69 }; 70 71 static g_access_t g_disk_access; 72 static g_start_t g_disk_start; 73 static g_ioctl_t g_disk_ioctl; 74 static g_dumpconf_t g_disk_dumpconf; 75 static g_provgone_t g_disk_providergone; 76 77 static struct g_class g_disk_class = { 78 .name = G_DISK_CLASS_NAME, 79 .version = G_VERSION, 80 .start = g_disk_start, 81 .access = g_disk_access, 82 .ioctl = g_disk_ioctl, 83 .providergone = g_disk_providergone, 84 .dumpconf = g_disk_dumpconf, 85 }; 86 87 SYSCTL_DECL(_kern_geom); 88 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0, 89 "GEOM_DISK stuff"); 90 91 DECLARE_GEOM_CLASS(g_disk_class, g_disk); 92 93 static int 94 g_disk_access(struct g_provider *pp, int r, int w, int e) 95 { 96 struct disk *dp; 97 struct g_disk_softc *sc; 98 int error; 99 100 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 101 pp->name, r, w, e); 102 g_topology_assert(); 103 sc = pp->private; 104 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { 105 /* 106 * Allow decreasing access count even if disk is not 107 * avaliable anymore. 108 */ 109 if (r <= 0 && w <= 0 && e <= 0) 110 return (0); 111 return (ENXIO); 112 } 113 r += pp->acr; 114 w += pp->acw; 115 e += pp->ace; 116 error = 0; 117 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 118 if (dp->d_open != NULL) { 119 error = dp->d_open(dp); 120 if (bootverbose && error != 0) 121 printf("Opened disk %s -> %d\n", 122 pp->name, error); 123 if (error != 0) 124 return (error); 125 } 126 pp->mediasize = dp->d_mediasize; 127 pp->sectorsize = dp->d_sectorsize; 128 if (dp->d_maxsize == 0) { 129 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 130 dp->d_name, dp->d_unit); 131 dp->d_maxsize = DFLTPHYS; 132 } 133 if (dp->d_delmaxsize == 0) { 134 if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) { 135 printf("WARNING: Disk drive %s%d has no " 136 "d_delmaxsize\n", dp->d_name, dp->d_unit); 137 } 138 dp->d_delmaxsize = dp->d_maxsize; 139 } 140 pp->stripeoffset = dp->d_stripeoffset; 141 pp->stripesize = dp->d_stripesize; 142 dp->d_flags |= DISKFLAG_OPEN; 143 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 144 if (dp->d_close != NULL) { 145 error = dp->d_close(dp); 146 if (error != 0) 147 printf("Closed disk %s -> %d\n", 148 pp->name, error); 149 } 150 sc->state = G_STATE_ACTIVE; 151 if (sc->led[0] != 0) 152 led_set(sc->led, "0"); 153 dp->d_flags &= ~DISKFLAG_OPEN; 154 } 155 return (error); 156 } 157 158 static void 159 g_disk_kerneldump(struct bio *bp, struct disk *dp) 160 { 161 struct g_kerneldump *gkd; 162 struct g_geom *gp; 163 164 gkd = (struct g_kerneldump*)bp->bio_data; 165 gp = bp->bio_to->geom; 166 g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)", 167 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 168 if (dp->d_dump == NULL) { 169 g_io_deliver(bp, ENODEV); 170 return; 171 } 172 gkd->di.dumper = dp->d_dump; 173 gkd->di.priv = dp; 174 gkd->di.blocksize = dp->d_sectorsize; 175 gkd->di.maxiosize = dp->d_maxsize; 176 gkd->di.mediaoffset = gkd->offset; 177 if ((gkd->offset + gkd->length) > dp->d_mediasize) 178 gkd->length = dp->d_mediasize - gkd->offset; 179 gkd->di.mediasize = gkd->length; 180 g_io_deliver(bp, 0); 181 } 182 183 static void 184 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc) 185 { 186 const char *cmd; 187 188 memcpy(&sc->state, bp->bio_data, sizeof(sc->state)); 189 if (sc->led[0] != 0) { 190 switch (sc->state) { 191 case G_STATE_FAILED: 192 cmd = "1"; 193 break; 194 case G_STATE_REBUILD: 195 cmd = "f5"; 196 break; 197 case G_STATE_RESYNC: 198 cmd = "f1"; 199 break; 200 default: 201 cmd = "0"; 202 break; 203 } 204 led_set(sc->led, cmd); 205 } 206 g_io_deliver(bp, 0); 207 } 208 209 static void 210 g_disk_done(struct bio *bp) 211 { 212 struct bintime now; 213 struct bio *bp2; 214 struct g_disk_softc *sc; 215 216 /* See "notes" for why we need a mutex here */ 217 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 218 bp2 = bp->bio_parent; 219 sc = bp2->bio_to->private; 220 bp->bio_completed = bp->bio_length - bp->bio_resid; 221 binuptime(&now); 222 mtx_lock(&sc->done_mtx); 223 if (bp2->bio_error == 0) 224 bp2->bio_error = bp->bio_error; 225 bp2->bio_completed += bp->bio_completed; 226 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE|BIO_FLUSH)) != 0) 227 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now); 228 bp2->bio_inbed++; 229 if (bp2->bio_children == bp2->bio_inbed) { 230 mtx_unlock(&sc->done_mtx); 231 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 232 g_io_deliver(bp2, bp2->bio_error); 233 } else 234 mtx_unlock(&sc->done_mtx); 235 g_destroy_bio(bp); 236 } 237 238 static int 239 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 240 { 241 struct disk *dp; 242 struct g_disk_softc *sc; 243 int error; 244 245 sc = pp->private; 246 dp = sc->dp; 247 248 if (dp->d_ioctl == NULL) 249 return (ENOIOCTL); 250 error = dp->d_ioctl(dp, cmd, data, fflag, td); 251 return (error); 252 } 253 254 static void 255 g_disk_start(struct bio *bp) 256 { 257 struct bio *bp2, *bp3; 258 struct disk *dp; 259 struct g_disk_softc *sc; 260 int error; 261 off_t off; 262 263 sc = bp->bio_to->private; 264 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { 265 g_io_deliver(bp, ENXIO); 266 return; 267 } 268 error = EJUSTRETURN; 269 switch(bp->bio_cmd) { 270 case BIO_DELETE: 271 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 272 error = EOPNOTSUPP; 273 break; 274 } 275 /* fall-through */ 276 case BIO_READ: 277 case BIO_WRITE: 278 off = 0; 279 bp3 = NULL; 280 bp2 = g_clone_bio(bp); 281 if (bp2 == NULL) { 282 error = ENOMEM; 283 break; 284 } 285 do { 286 off_t d_maxsize; 287 288 d_maxsize = (bp->bio_cmd == BIO_DELETE) ? 289 dp->d_delmaxsize : dp->d_maxsize; 290 bp2->bio_offset += off; 291 bp2->bio_length -= off; 292 if ((bp->bio_flags & BIO_UNMAPPED) == 0) { 293 bp2->bio_data += off; 294 } else { 295 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) 296 != 0, 297 ("unmapped bio not supported by disk %s", 298 dp->d_name)); 299 bp2->bio_ma += off / PAGE_SIZE; 300 bp2->bio_ma_offset += off; 301 bp2->bio_ma_offset %= PAGE_SIZE; 302 bp2->bio_ma_n -= off / PAGE_SIZE; 303 } 304 if (bp2->bio_length > d_maxsize) { 305 /* 306 * XXX: If we have a stripesize we should really 307 * use it here. Care should be taken in the delete 308 * case if this is done as deletes can be very 309 * sensitive to size given how they are processed. 310 */ 311 bp2->bio_length = d_maxsize; 312 if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 313 bp2->bio_ma_n = howmany( 314 bp2->bio_ma_offset + 315 bp2->bio_length, PAGE_SIZE); 316 } 317 off += d_maxsize; 318 /* 319 * To avoid a race, we need to grab the next bio 320 * before we schedule this one. See "notes". 321 */ 322 bp3 = g_clone_bio(bp); 323 if (bp3 == NULL) 324 bp->bio_error = ENOMEM; 325 } 326 bp2->bio_done = g_disk_done; 327 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 328 bp2->bio_bcount = bp2->bio_length; 329 bp2->bio_disk = dp; 330 mtx_lock(&sc->start_mtx); 331 devstat_start_transaction_bio(dp->d_devstat, bp2); 332 mtx_unlock(&sc->start_mtx); 333 dp->d_strategy(bp2); 334 bp2 = bp3; 335 bp3 = NULL; 336 } while (bp2 != NULL); 337 break; 338 case BIO_GETATTR: 339 /* Give the driver a chance to override */ 340 if (dp->d_getattr != NULL) { 341 if (bp->bio_disk == NULL) 342 bp->bio_disk = dp; 343 error = dp->d_getattr(bp); 344 if (error != -1) 345 break; 346 error = EJUSTRETURN; 347 } 348 if (g_handleattr_int(bp, "GEOM::candelete", 349 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 350 break; 351 else if (g_handleattr_int(bp, "GEOM::fwsectors", 352 dp->d_fwsectors)) 353 break; 354 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 355 break; 356 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 357 break; 358 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 359 break; 360 else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor", 361 dp->d_hba_vendor)) 362 break; 363 else if (g_handleattr_uint16_t(bp, "GEOM::hba_device", 364 dp->d_hba_device)) 365 break; 366 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor", 367 dp->d_hba_subvendor)) 368 break; 369 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice", 370 dp->d_hba_subdevice)) 371 break; 372 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 373 g_disk_kerneldump(bp, dp); 374 else if (!strcmp(bp->bio_attribute, "GEOM::setstate")) 375 g_disk_setstate(bp, sc); 376 else if (g_handleattr_uint16_t(bp, "GEOM::rotation_rate", 377 dp->d_rotation_rate)) 378 break; 379 else 380 error = ENOIOCTL; 381 break; 382 case BIO_FLUSH: 383 g_trace(G_T_BIO, "g_disk_flushcache(%s)", 384 bp->bio_to->name); 385 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 386 error = EOPNOTSUPP; 387 break; 388 } 389 bp2 = g_clone_bio(bp); 390 if (bp2 == NULL) { 391 g_io_deliver(bp, ENOMEM); 392 return; 393 } 394 bp2->bio_done = g_disk_done; 395 bp2->bio_disk = dp; 396 mtx_lock(&sc->start_mtx); 397 devstat_start_transaction_bio(dp->d_devstat, bp2); 398 mtx_unlock(&sc->start_mtx); 399 dp->d_strategy(bp2); 400 break; 401 default: 402 error = EOPNOTSUPP; 403 break; 404 } 405 if (error != EJUSTRETURN) 406 g_io_deliver(bp, error); 407 return; 408 } 409 410 static void 411 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 412 { 413 struct bio *bp; 414 struct disk *dp; 415 struct g_disk_softc *sc; 416 char *buf; 417 int res = 0; 418 419 sc = gp->softc; 420 if (sc == NULL || (dp = sc->dp) == NULL) 421 return; 422 if (indent == NULL) { 423 sbuf_printf(sb, " hd %u", dp->d_fwheads); 424 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 425 return; 426 } 427 if (pp != NULL) { 428 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 429 indent, dp->d_fwheads); 430 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 431 indent, dp->d_fwsectors); 432 if (dp->d_getattr != NULL) { 433 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK); 434 bp = g_alloc_bio(); 435 bp->bio_disk = dp; 436 bp->bio_attribute = "GEOM::ident"; 437 bp->bio_length = DISK_IDENT_SIZE; 438 bp->bio_data = buf; 439 res = dp->d_getattr(bp); 440 sbuf_printf(sb, "%s<ident>", indent); 441 g_conf_printf_escaped(sb, "%s", 442 res == 0 ? buf: dp->d_ident); 443 sbuf_printf(sb, "</ident>\n"); 444 bp->bio_attribute = "GEOM::lunid"; 445 bp->bio_length = DISK_IDENT_SIZE; 446 bp->bio_data = buf; 447 if (dp->d_getattr(bp) == 0) { 448 sbuf_printf(sb, "%s<lunid>", indent); 449 g_conf_printf_escaped(sb, "%s", buf); 450 sbuf_printf(sb, "</lunid>\n"); 451 } 452 bp->bio_attribute = "GEOM::lunname"; 453 bp->bio_length = DISK_IDENT_SIZE; 454 bp->bio_data = buf; 455 if (dp->d_getattr(bp) == 0) { 456 sbuf_printf(sb, "%s<lunname>", indent); 457 g_conf_printf_escaped(sb, "%s", buf); 458 sbuf_printf(sb, "</lunname>\n"); 459 } 460 g_destroy_bio(bp); 461 g_free(buf); 462 } else { 463 sbuf_printf(sb, "%s<ident>", indent); 464 g_conf_printf_escaped(sb, "%s", dp->d_ident); 465 sbuf_printf(sb, "</ident>\n"); 466 } 467 sbuf_printf(sb, "%s<descr>", indent); 468 g_conf_printf_escaped(sb, "%s", dp->d_descr); 469 sbuf_printf(sb, "</descr>\n"); 470 } 471 } 472 473 static void 474 g_disk_resize(void *ptr, int flag) 475 { 476 struct disk *dp; 477 struct g_geom *gp; 478 struct g_provider *pp; 479 480 if (flag == EV_CANCEL) 481 return; 482 g_topology_assert(); 483 484 dp = ptr; 485 gp = dp->d_geom; 486 487 if (dp->d_destroyed || gp == NULL) 488 return; 489 490 LIST_FOREACH(pp, &gp->provider, provider) { 491 if (pp->sectorsize != 0 && 492 pp->sectorsize != dp->d_sectorsize) 493 g_wither_provider(pp, ENXIO); 494 else 495 g_resize_provider(pp, dp->d_mediasize); 496 } 497 } 498 499 static void 500 g_disk_create(void *arg, int flag) 501 { 502 struct g_geom *gp; 503 struct g_provider *pp; 504 struct disk *dp; 505 struct g_disk_softc *sc; 506 char tmpstr[80]; 507 508 if (flag == EV_CANCEL) 509 return; 510 g_topology_assert(); 511 dp = arg; 512 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 513 mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF); 514 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF); 515 sc->dp = dp; 516 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 517 gp->softc = sc; 518 pp = g_new_providerf(gp, "%s", gp->name); 519 devstat_remove_entry(pp->stat); 520 pp->stat = NULL; 521 dp->d_devstat->id = pp; 522 pp->mediasize = dp->d_mediasize; 523 pp->sectorsize = dp->d_sectorsize; 524 pp->stripeoffset = dp->d_stripeoffset; 525 pp->stripesize = dp->d_stripesize; 526 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0) 527 pp->flags |= G_PF_ACCEPT_UNMAPPED; 528 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0) 529 pp->flags |= G_PF_DIRECT_SEND; 530 pp->flags |= G_PF_DIRECT_RECEIVE; 531 if (bootverbose) 532 printf("GEOM: new disk %s\n", gp->name); 533 sysctl_ctx_init(&sc->sysctl_ctx); 534 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name); 535 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 536 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name, 537 CTLFLAG_RD, 0, tmpstr); 538 if (sc->sysctl_tree != NULL) { 539 SYSCTL_ADD_STRING(&sc->sysctl_ctx, 540 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led", 541 CTLFLAG_RWTUN, sc->led, sizeof(sc->led), 542 "LED name"); 543 } 544 pp->private = sc; 545 dp->d_geom = gp; 546 g_error_provider(pp, 0); 547 } 548 549 /* 550 * We get this callback after all of the consumers have gone away, and just 551 * before the provider is freed. If the disk driver provided a d_gone 552 * callback, let them know that it is okay to free resources -- they won't 553 * be getting any more accesses from GEOM. 554 */ 555 static void 556 g_disk_providergone(struct g_provider *pp) 557 { 558 struct disk *dp; 559 struct g_disk_softc *sc; 560 561 sc = (struct g_disk_softc *)pp->private; 562 dp = sc->dp; 563 if (dp != NULL && dp->d_gone != NULL) 564 dp->d_gone(dp); 565 if (sc->sysctl_tree != NULL) { 566 sysctl_ctx_free(&sc->sysctl_ctx); 567 sc->sysctl_tree = NULL; 568 } 569 if (sc->led[0] != 0) { 570 led_set(sc->led, "0"); 571 sc->led[0] = 0; 572 } 573 pp->private = NULL; 574 pp->geom->softc = NULL; 575 mtx_destroy(&sc->done_mtx); 576 mtx_destroy(&sc->start_mtx); 577 g_free(sc); 578 } 579 580 static void 581 g_disk_destroy(void *ptr, int flag) 582 { 583 struct disk *dp; 584 struct g_geom *gp; 585 struct g_disk_softc *sc; 586 587 g_topology_assert(); 588 dp = ptr; 589 gp = dp->d_geom; 590 if (gp != NULL) { 591 sc = gp->softc; 592 if (sc != NULL) 593 sc->dp = NULL; 594 dp->d_geom = NULL; 595 g_wither_geom(gp, ENXIO); 596 } 597 g_free(dp); 598 } 599 600 /* 601 * We only allow printable characters in disk ident, 602 * the rest is converted to 'x<HH>'. 603 */ 604 static void 605 g_disk_ident_adjust(char *ident, size_t size) 606 { 607 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 608 609 newid[0] = '\0'; 610 for (p = ident; *p != '\0'; p++) { 611 if (isprint(*p)) { 612 tmp[0] = *p; 613 tmp[1] = '\0'; 614 } else { 615 snprintf(tmp, sizeof(tmp), "x%02hhx", 616 *(unsigned char *)p); 617 } 618 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 619 break; 620 } 621 bzero(ident, size); 622 strlcpy(ident, newid, size); 623 } 624 625 struct disk * 626 disk_alloc(void) 627 { 628 629 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO)); 630 } 631 632 void 633 disk_create(struct disk *dp, int version) 634 { 635 636 if (version != DISK_VERSION) { 637 printf("WARNING: Attempt to add disk %s%d %s", 638 dp->d_name, dp->d_unit, 639 " using incompatible ABI version of disk(9)\n"); 640 printf("WARNING: Ignoring disk %s%d\n", 641 dp->d_name, dp->d_unit); 642 return; 643 } 644 if (dp->d_flags & DISKFLAG_RESERVED) { 645 printf("WARNING: Attempt to add non-MPSAFE disk %s%d\n", 646 dp->d_name, dp->d_unit); 647 printf("WARNING: Ignoring disk %s%d\n", 648 dp->d_name, dp->d_unit); 649 return; 650 } 651 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 652 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 653 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 654 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 655 if (dp->d_devstat == NULL) 656 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 657 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 658 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 659 dp->d_geom = NULL; 660 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 661 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 662 } 663 664 void 665 disk_destroy(struct disk *dp) 666 { 667 668 g_cancel_event(dp); 669 dp->d_destroyed = 1; 670 if (dp->d_devstat != NULL) 671 devstat_remove_entry(dp->d_devstat); 672 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 673 } 674 675 void 676 disk_gone(struct disk *dp) 677 { 678 struct g_geom *gp; 679 struct g_provider *pp; 680 681 gp = dp->d_geom; 682 if (gp != NULL) { 683 pp = LIST_FIRST(&gp->provider); 684 if (pp != NULL) { 685 KASSERT(LIST_NEXT(pp, provider) == NULL, 686 ("geom %p has more than one provider", gp)); 687 g_wither_provider(pp, ENXIO); 688 } 689 } 690 } 691 692 void 693 disk_attr_changed(struct disk *dp, const char *attr, int flag) 694 { 695 struct g_geom *gp; 696 struct g_provider *pp; 697 698 gp = dp->d_geom; 699 if (gp != NULL) 700 LIST_FOREACH(pp, &gp->provider, provider) 701 (void)g_attr_changed(pp, attr, flag); 702 } 703 704 void 705 disk_media_changed(struct disk *dp, int flag) 706 { 707 struct g_geom *gp; 708 struct g_provider *pp; 709 710 gp = dp->d_geom; 711 if (gp != NULL) { 712 pp = LIST_FIRST(&gp->provider); 713 if (pp != NULL) { 714 KASSERT(LIST_NEXT(pp, provider) == NULL, 715 ("geom %p has more than one provider", gp)); 716 g_media_changed(pp, flag); 717 } 718 } 719 } 720 721 void 722 disk_media_gone(struct disk *dp, int flag) 723 { 724 struct g_geom *gp; 725 struct g_provider *pp; 726 727 gp = dp->d_geom; 728 if (gp != NULL) { 729 pp = LIST_FIRST(&gp->provider); 730 if (pp != NULL) { 731 KASSERT(LIST_NEXT(pp, provider) == NULL, 732 ("geom %p has more than one provider", gp)); 733 g_media_gone(pp, flag); 734 } 735 } 736 } 737 738 int 739 disk_resize(struct disk *dp, int flag) 740 { 741 742 if (dp->d_destroyed || dp->d_geom == NULL) 743 return (0); 744 745 return (g_post_event(g_disk_resize, dp, flag, NULL)); 746 } 747 748 static void 749 g_kern_disks(void *p, int flag __unused) 750 { 751 struct sbuf *sb; 752 struct g_geom *gp; 753 char *sp; 754 755 sb = p; 756 sp = ""; 757 g_topology_assert(); 758 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 759 sbuf_printf(sb, "%s%s", sp, gp->name); 760 sp = " "; 761 } 762 sbuf_finish(sb); 763 } 764 765 static int 766 sysctl_disks(SYSCTL_HANDLER_ARGS) 767 { 768 int error; 769 struct sbuf *sb; 770 771 sb = sbuf_new_auto(); 772 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 773 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 774 sbuf_delete(sb); 775 return error; 776 } 777 778 SYSCTL_PROC(_kern, OID_AUTO, disks, 779 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 780 sysctl_disks, "A", "names of available disks"); 781