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