1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2002 Poul-Henning Kamp 5 * Copyright (c) 2002 Networks Associates Technology, Inc. 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 9 * and NAI Labs, the Security Research Division of Network Associates, Inc. 10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 11 * DARPA CHATS research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. The names of the authors may not be used to endorse or promote 22 * products derived from this software without specific prior written 23 * permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_geom.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/sysctl.h> 47 #include <sys/bio.h> 48 #include <sys/ctype.h> 49 #include <sys/devctl.h> 50 #include <sys/fcntl.h> 51 #include <sys/malloc.h> 52 #include <sys/sbuf.h> 53 #include <sys/devicestat.h> 54 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <geom/geom.h> 58 #include <geom/geom_disk.h> 59 #include <geom/geom_int.h> 60 61 #include <dev/led/led.h> 62 63 #include <machine/bus.h> 64 65 struct g_disk_softc { 66 struct disk *dp; 67 struct devstat *d_devstat; 68 struct sysctl_ctx_list sysctl_ctx; 69 struct sysctl_oid *sysctl_tree; 70 char led[64]; 71 uint32_t state; 72 struct mtx done_mtx; 73 }; 74 75 static g_access_t g_disk_access; 76 static g_start_t g_disk_start; 77 static g_ioctl_t g_disk_ioctl; 78 static g_dumpconf_t g_disk_dumpconf; 79 static g_provgone_t g_disk_providergone; 80 81 static int g_disk_sysctl_flags(SYSCTL_HANDLER_ARGS); 82 83 static struct g_class g_disk_class = { 84 .name = G_DISK_CLASS_NAME, 85 .version = G_VERSION, 86 .start = g_disk_start, 87 .access = g_disk_access, 88 .ioctl = g_disk_ioctl, 89 .providergone = g_disk_providergone, 90 .dumpconf = g_disk_dumpconf, 91 }; 92 93 SYSCTL_DECL(_kern_geom); 94 static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 95 "GEOM_DISK stuff"); 96 97 DECLARE_GEOM_CLASS(g_disk_class, g_disk); 98 99 static int 100 g_disk_access(struct g_provider *pp, int r, int w, int e) 101 { 102 struct disk *dp; 103 struct g_disk_softc *sc; 104 int error; 105 106 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 107 pp->name, r, w, e); 108 g_topology_assert(); 109 sc = pp->private; 110 if ((dp = sc->dp) == NULL || dp->d_destroyed) { 111 /* 112 * Allow decreasing access count even if disk is not 113 * available anymore. 114 */ 115 if (r <= 0 && w <= 0 && e <= 0) 116 return (0); 117 return (ENXIO); 118 } 119 r += pp->acr; 120 w += pp->acw; 121 e += pp->ace; 122 error = 0; 123 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 124 /* 125 * It would be better to defer this decision to d_open if 126 * it was able to take flags. 127 */ 128 if (w > 0 && (dp->d_flags & DISKFLAG_WRITE_PROTECT) != 0) 129 error = EROFS; 130 if (error == 0 && dp->d_open != NULL) 131 error = dp->d_open(dp); 132 if (bootverbose && error != 0) 133 printf("Opened disk %s -> %d\n", pp->name, error); 134 if (error != 0) 135 return (error); 136 pp->sectorsize = dp->d_sectorsize; 137 if (dp->d_maxsize == 0) { 138 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 139 dp->d_name, dp->d_unit); 140 dp->d_maxsize = DFLTPHYS; 141 } 142 if (dp->d_delmaxsize == 0) { 143 if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) { 144 printf("WARNING: Disk drive %s%d has no " 145 "d_delmaxsize\n", dp->d_name, dp->d_unit); 146 } 147 dp->d_delmaxsize = dp->d_maxsize; 148 } 149 pp->stripeoffset = dp->d_stripeoffset; 150 pp->stripesize = dp->d_stripesize; 151 dp->d_flags |= DISKFLAG_OPEN; 152 /* 153 * Do not invoke resize event when initial size was zero. 154 * Some disks report its size only after first opening. 155 */ 156 if (pp->mediasize == 0) 157 pp->mediasize = dp->d_mediasize; 158 else 159 g_resize_provider(pp, dp->d_mediasize); 160 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 161 if (dp->d_close != NULL) { 162 error = dp->d_close(dp); 163 if (error != 0) 164 printf("Closed disk %s -> %d\n", 165 pp->name, error); 166 } 167 sc->state = G_STATE_ACTIVE; 168 if (sc->led[0] != 0) 169 led_set(sc->led, "0"); 170 dp->d_flags &= ~DISKFLAG_OPEN; 171 } 172 return (error); 173 } 174 175 static void 176 g_disk_kerneldump(struct bio *bp, struct disk *dp) 177 { 178 struct g_kerneldump *gkd; 179 struct g_geom *gp; 180 181 gkd = (struct g_kerneldump*)bp->bio_data; 182 gp = bp->bio_to->geom; 183 g_trace(G_T_TOPOLOGY, "g_disk_kerneldump(%s, %jd, %jd)", 184 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 185 if (dp->d_dump == NULL) { 186 g_io_deliver(bp, ENODEV); 187 return; 188 } 189 gkd->di.dumper = dp->d_dump; 190 gkd->di.priv = dp; 191 gkd->di.blocksize = dp->d_sectorsize; 192 gkd->di.maxiosize = dp->d_maxsize; 193 gkd->di.mediaoffset = gkd->offset; 194 if ((gkd->offset + gkd->length) > dp->d_mediasize) 195 gkd->length = dp->d_mediasize - gkd->offset; 196 gkd->di.mediasize = gkd->length; 197 g_io_deliver(bp, 0); 198 } 199 200 static void 201 g_disk_setstate(struct bio *bp, struct g_disk_softc *sc) 202 { 203 const char *cmd; 204 205 memcpy(&sc->state, bp->bio_data, sizeof(sc->state)); 206 if (sc->led[0] != 0) { 207 switch (sc->state) { 208 case G_STATE_FAILED: 209 cmd = "1"; 210 break; 211 case G_STATE_REBUILD: 212 cmd = "f5"; 213 break; 214 case G_STATE_RESYNC: 215 cmd = "f1"; 216 break; 217 default: 218 cmd = "0"; 219 break; 220 } 221 led_set(sc->led, cmd); 222 } 223 g_io_deliver(bp, 0); 224 } 225 226 static void 227 g_disk_done(struct bio *bp) 228 { 229 struct bintime now; 230 struct bio *bp2; 231 struct g_disk_softc *sc; 232 233 /* See "notes" for why we need a mutex here */ 234 sc = bp->bio_caller1; 235 bp2 = bp->bio_parent; 236 binuptime(&now); 237 mtx_lock(&sc->done_mtx); 238 if (bp2->bio_error == 0) 239 bp2->bio_error = bp->bio_error; 240 bp2->bio_completed += bp->bio_length - bp->bio_resid; 241 242 switch (bp->bio_cmd) { 243 case BIO_ZONE: 244 bcopy(&bp->bio_zone, &bp2->bio_zone, sizeof(bp->bio_zone)); 245 /*FALLTHROUGH*/ 246 case BIO_READ: 247 case BIO_WRITE: 248 case BIO_DELETE: 249 case BIO_FLUSH: 250 devstat_end_transaction_bio_bt(sc->d_devstat, bp, &now); 251 break; 252 default: 253 break; 254 } 255 bp2->bio_inbed++; 256 if (bp2->bio_children == bp2->bio_inbed) { 257 mtx_unlock(&sc->done_mtx); 258 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 259 g_io_deliver(bp2, bp2->bio_error); 260 } else 261 mtx_unlock(&sc->done_mtx); 262 g_destroy_bio(bp); 263 } 264 265 static int 266 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 267 { 268 struct disk *dp; 269 struct g_disk_softc *sc; 270 271 sc = pp->private; 272 dp = sc->dp; 273 KASSERT(dp != NULL && !dp->d_destroyed, 274 ("g_disk_ioctl(%lx) on destroyed disk %s", cmd, pp->name)); 275 276 if (dp->d_ioctl == NULL) 277 return (ENOIOCTL); 278 return (dp->d_ioctl(dp, cmd, data, fflag, td)); 279 } 280 281 static off_t 282 g_disk_maxsize(struct disk *dp, struct bio *bp) 283 { 284 if (bp->bio_cmd == BIO_DELETE) 285 return (dp->d_delmaxsize); 286 return (dp->d_maxsize); 287 } 288 289 static int 290 g_disk_maxsegs(struct disk *dp, struct bio *bp) 291 { 292 return ((g_disk_maxsize(dp, bp) / PAGE_SIZE) + 1); 293 } 294 295 static void 296 g_disk_advance(struct disk *dp, struct bio *bp, off_t off) 297 { 298 299 bp->bio_offset += off; 300 bp->bio_length -= off; 301 302 if ((bp->bio_flags & BIO_VLIST) != 0) { 303 bus_dma_segment_t *seg, *end; 304 305 seg = (bus_dma_segment_t *)bp->bio_data; 306 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n; 307 off += bp->bio_ma_offset; 308 while (off >= seg->ds_len) { 309 KASSERT((seg != end), 310 ("vlist request runs off the end")); 311 off -= seg->ds_len; 312 seg++; 313 } 314 bp->bio_ma_offset = off; 315 bp->bio_ma_n = end - seg; 316 bp->bio_data = (void *)seg; 317 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 318 bp->bio_ma += off / PAGE_SIZE; 319 bp->bio_ma_offset += off; 320 bp->bio_ma_offset %= PAGE_SIZE; 321 bp->bio_ma_n -= off / PAGE_SIZE; 322 } else { 323 bp->bio_data += off; 324 } 325 } 326 327 static void 328 g_disk_seg_limit(bus_dma_segment_t *seg, off_t *poffset, 329 off_t *plength, int *ppages) 330 { 331 uintptr_t seg_page_base; 332 uintptr_t seg_page_end; 333 off_t offset; 334 off_t length; 335 int seg_pages; 336 337 offset = *poffset; 338 length = *plength; 339 340 if (length > seg->ds_len - offset) 341 length = seg->ds_len - offset; 342 343 seg_page_base = trunc_page(seg->ds_addr + offset); 344 seg_page_end = round_page(seg->ds_addr + offset + length); 345 seg_pages = (seg_page_end - seg_page_base) >> PAGE_SHIFT; 346 347 if (seg_pages > *ppages) { 348 seg_pages = *ppages; 349 length = (seg_page_base + (seg_pages << PAGE_SHIFT)) - 350 (seg->ds_addr + offset); 351 } 352 353 *poffset = 0; 354 *plength -= length; 355 *ppages -= seg_pages; 356 } 357 358 static off_t 359 g_disk_vlist_limit(struct disk *dp, struct bio *bp, bus_dma_segment_t **pendseg) 360 { 361 bus_dma_segment_t *seg, *end; 362 off_t residual; 363 off_t offset; 364 int pages; 365 366 seg = (bus_dma_segment_t *)bp->bio_data; 367 end = (bus_dma_segment_t *)bp->bio_data + bp->bio_ma_n; 368 residual = bp->bio_length; 369 offset = bp->bio_ma_offset; 370 pages = g_disk_maxsegs(dp, bp); 371 while (residual != 0 && pages != 0) { 372 KASSERT((seg != end), 373 ("vlist limit runs off the end")); 374 g_disk_seg_limit(seg, &offset, &residual, &pages); 375 seg++; 376 } 377 if (pendseg != NULL) 378 *pendseg = seg; 379 return (residual); 380 } 381 382 static bool 383 g_disk_limit(struct disk *dp, struct bio *bp) 384 { 385 bool limited = false; 386 off_t maxsz; 387 388 maxsz = g_disk_maxsize(dp, bp); 389 390 /* 391 * XXX: If we have a stripesize we should really use it here. 392 * Care should be taken in the delete case if this is done 393 * as deletes can be very sensitive to size given how they 394 * are processed. 395 */ 396 if (bp->bio_length > maxsz) { 397 bp->bio_length = maxsz; 398 limited = true; 399 } 400 401 if ((bp->bio_flags & BIO_VLIST) != 0) { 402 bus_dma_segment_t *firstseg, *endseg; 403 off_t residual; 404 405 firstseg = (bus_dma_segment_t*)bp->bio_data; 406 residual = g_disk_vlist_limit(dp, bp, &endseg); 407 if (residual != 0) { 408 bp->bio_ma_n = endseg - firstseg; 409 bp->bio_length -= residual; 410 limited = true; 411 } 412 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 413 bp->bio_ma_n = 414 howmany(bp->bio_ma_offset + bp->bio_length, PAGE_SIZE); 415 } 416 417 return (limited); 418 } 419 420 static void 421 g_disk_start(struct bio *bp) 422 { 423 struct bio *bp2, *bp3; 424 struct disk *dp; 425 struct g_disk_softc *sc; 426 int error; 427 off_t off; 428 429 biotrack(bp, __func__); 430 431 sc = bp->bio_to->private; 432 dp = sc->dp; 433 KASSERT(dp != NULL && !dp->d_destroyed, 434 ("g_disk_start(%p) on destroyed disk %s", bp, bp->bio_to->name)); 435 error = EJUSTRETURN; 436 switch(bp->bio_cmd) { 437 case BIO_DELETE: 438 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 439 error = EOPNOTSUPP; 440 break; 441 } 442 /* fall-through */ 443 case BIO_READ: 444 case BIO_WRITE: 445 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0 || 446 (bp->bio_flags & BIO_UNMAPPED) == 0, 447 ("unmapped bio not supported by disk %s", dp->d_name)); 448 off = 0; 449 bp3 = NULL; 450 bp2 = g_clone_bio(bp); 451 if (bp2 == NULL) { 452 error = ENOMEM; 453 break; 454 } 455 for (;;) { 456 if (g_disk_limit(dp, bp2)) { 457 off += bp2->bio_length; 458 459 /* 460 * To avoid a race, we need to grab the next bio 461 * before we schedule this one. See "notes". 462 */ 463 bp3 = g_clone_bio(bp); 464 if (bp3 == NULL) 465 bp->bio_error = ENOMEM; 466 } 467 bp2->bio_done = g_disk_done; 468 bp2->bio_caller1 = sc; 469 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 470 bp2->bio_bcount = bp2->bio_length; 471 bp2->bio_disk = dp; 472 devstat_start_transaction_bio(dp->d_devstat, bp2); 473 dp->d_strategy(bp2); 474 475 if (bp3 == NULL) 476 break; 477 478 bp2 = bp3; 479 bp3 = NULL; 480 g_disk_advance(dp, bp2, off); 481 } 482 break; 483 case BIO_GETATTR: 484 /* Give the driver a chance to override */ 485 if (dp->d_getattr != NULL) { 486 if (bp->bio_disk == NULL) 487 bp->bio_disk = dp; 488 error = dp->d_getattr(bp); 489 if (error != -1) 490 break; 491 error = EJUSTRETURN; 492 } 493 if (g_handleattr_int(bp, "GEOM::candelete", 494 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 495 break; 496 else if (g_handleattr_int(bp, "GEOM::fwsectors", 497 dp->d_fwsectors)) 498 break; 499 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 500 break; 501 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 502 break; 503 else if (g_handleattr_str(bp, "GEOM::descr", dp->d_descr)) 504 break; 505 else if (g_handleattr_uint16_t(bp, "GEOM::hba_vendor", 506 dp->d_hba_vendor)) 507 break; 508 else if (g_handleattr_uint16_t(bp, "GEOM::hba_device", 509 dp->d_hba_device)) 510 break; 511 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subvendor", 512 dp->d_hba_subvendor)) 513 break; 514 else if (g_handleattr_uint16_t(bp, "GEOM::hba_subdevice", 515 dp->d_hba_subdevice)) 516 break; 517 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 518 g_disk_kerneldump(bp, dp); 519 else if (!strcmp(bp->bio_attribute, "GEOM::setstate")) 520 g_disk_setstate(bp, sc); 521 else if (g_handleattr_uint16_t(bp, "GEOM::rotation_rate", 522 dp->d_rotation_rate)) 523 break; 524 else if (g_handleattr_str(bp, "GEOM::attachment", 525 dp->d_attachment)) 526 break; 527 else 528 error = ENOIOCTL; 529 break; 530 case BIO_FLUSH: 531 g_trace(G_T_BIO, "g_disk_flushcache(%s)", 532 bp->bio_to->name); 533 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 534 error = EOPNOTSUPP; 535 break; 536 } 537 /*FALLTHROUGH*/ 538 case BIO_ZONE: 539 if (bp->bio_cmd == BIO_ZONE) { 540 if (!(dp->d_flags & DISKFLAG_CANZONE)) { 541 error = EOPNOTSUPP; 542 break; 543 } 544 g_trace(G_T_BIO, "g_disk_zone(%s)", 545 bp->bio_to->name); 546 } 547 bp2 = g_clone_bio(bp); 548 if (bp2 == NULL) { 549 g_io_deliver(bp, ENOMEM); 550 return; 551 } 552 bp2->bio_done = g_disk_done; 553 bp2->bio_caller1 = sc; 554 bp2->bio_disk = dp; 555 devstat_start_transaction_bio(dp->d_devstat, bp2); 556 dp->d_strategy(bp2); 557 break; 558 case BIO_SPEEDUP: 559 bp2 = g_clone_bio(bp); 560 if (bp2 == NULL) { 561 g_io_deliver(bp, ENOMEM); 562 return; 563 } 564 bp2->bio_done = g_disk_done; 565 bp2->bio_caller1 = sc; 566 bp2->bio_disk = dp; 567 dp->d_strategy(bp2); 568 break; 569 default: 570 error = EOPNOTSUPP; 571 break; 572 } 573 if (error != EJUSTRETURN) 574 g_io_deliver(bp, error); 575 return; 576 } 577 578 static void 579 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 580 { 581 struct bio *bp; 582 struct disk *dp; 583 struct g_disk_softc *sc; 584 char *buf; 585 int res = 0; 586 587 sc = gp->softc; 588 if (sc == NULL || (dp = sc->dp) == NULL) 589 return; 590 if (indent == NULL) { 591 sbuf_printf(sb, " hd %u", dp->d_fwheads); 592 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 593 return; 594 } 595 if (pp != NULL) { 596 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 597 indent, dp->d_fwheads); 598 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 599 indent, dp->d_fwsectors); 600 601 /* 602 * "rotationrate" is a little complicated, because the value 603 * returned by the drive might not be the RPM; 0 and 1 are 604 * special cases, and there's also a valid range. 605 */ 606 sbuf_printf(sb, "%s<rotationrate>", indent); 607 if (dp->d_rotation_rate == DISK_RR_UNKNOWN) /* Old drives */ 608 sbuf_cat(sb, "unknown"); /* don't report RPM. */ 609 else if (dp->d_rotation_rate == DISK_RR_NON_ROTATING) 610 sbuf_cat(sb, "0"); 611 else if ((dp->d_rotation_rate >= DISK_RR_MIN) && 612 (dp->d_rotation_rate <= DISK_RR_MAX)) 613 sbuf_printf(sb, "%u", dp->d_rotation_rate); 614 else 615 sbuf_cat(sb, "invalid"); 616 sbuf_cat(sb, "</rotationrate>\n"); 617 if (dp->d_getattr != NULL) { 618 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK); 619 bp = g_alloc_bio(); 620 bp->bio_disk = dp; 621 bp->bio_attribute = "GEOM::ident"; 622 bp->bio_length = DISK_IDENT_SIZE; 623 bp->bio_data = buf; 624 res = dp->d_getattr(bp); 625 sbuf_printf(sb, "%s<ident>", indent); 626 g_conf_cat_escaped(sb, res == 0 ? buf : dp->d_ident); 627 sbuf_cat(sb, "</ident>\n"); 628 bp->bio_attribute = "GEOM::lunid"; 629 bp->bio_length = DISK_IDENT_SIZE; 630 bp->bio_data = buf; 631 if (dp->d_getattr(bp) == 0) { 632 sbuf_printf(sb, "%s<lunid>", indent); 633 g_conf_cat_escaped(sb, buf); 634 sbuf_cat(sb, "</lunid>\n"); 635 } 636 bp->bio_attribute = "GEOM::lunname"; 637 bp->bio_length = DISK_IDENT_SIZE; 638 bp->bio_data = buf; 639 if (dp->d_getattr(bp) == 0) { 640 sbuf_printf(sb, "%s<lunname>", indent); 641 g_conf_cat_escaped(sb, buf); 642 sbuf_cat(sb, "</lunname>\n"); 643 } 644 g_destroy_bio(bp); 645 g_free(buf); 646 } else { 647 sbuf_printf(sb, "%s<ident>", indent); 648 g_conf_cat_escaped(sb, dp->d_ident); 649 sbuf_cat(sb, "</ident>\n"); 650 } 651 sbuf_printf(sb, "%s<descr>", indent); 652 g_conf_cat_escaped(sb, dp->d_descr); 653 sbuf_cat(sb, "</descr>\n"); 654 } 655 } 656 657 static void 658 g_disk_resize(void *ptr, int flag) 659 { 660 struct disk *dp; 661 struct g_geom *gp; 662 struct g_provider *pp; 663 664 if (flag == EV_CANCEL) 665 return; 666 g_topology_assert(); 667 668 dp = ptr; 669 gp = dp->d_geom; 670 671 if (dp->d_destroyed || gp == NULL) 672 return; 673 674 LIST_FOREACH(pp, &gp->provider, provider) { 675 if (pp->sectorsize != 0 && 676 pp->sectorsize != dp->d_sectorsize) 677 g_wither_provider(pp, ENXIO); 678 else 679 g_resize_provider(pp, dp->d_mediasize); 680 } 681 } 682 683 static void 684 g_disk_create(void *arg, int flag) 685 { 686 struct g_geom *gp; 687 struct g_provider *pp; 688 struct disk *dp; 689 struct g_disk_softc *sc; 690 struct disk_alias *dap; 691 char tmpstr[80]; 692 693 if (flag == EV_CANCEL) 694 return; 695 g_topology_assert(); 696 dp = arg; 697 698 mtx_pool_lock(mtxpool_sleep, dp); 699 dp->d_init_level = DISK_INIT_START; 700 701 /* 702 * If the disk has already gone away, we can just stop here and 703 * call the user's callback to tell him we've cleaned things up. 704 */ 705 if (dp->d_goneflag != 0) { 706 mtx_pool_unlock(mtxpool_sleep, dp); 707 if (dp->d_gone != NULL) 708 dp->d_gone(dp); 709 return; 710 } 711 mtx_pool_unlock(mtxpool_sleep, dp); 712 713 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 714 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF); 715 sc->dp = dp; 716 sc->d_devstat = dp->d_devstat; 717 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 718 gp->softc = sc; 719 pp = g_new_providerf(gp, "%s", gp->name); 720 LIST_FOREACH(dap, &dp->d_aliases, da_next) 721 g_provider_add_alias(pp, "%s%d", dap->da_alias, dp->d_unit); 722 devstat_remove_entry(pp->stat); 723 pp->stat = NULL; 724 dp->d_devstat->id = pp; 725 pp->mediasize = dp->d_mediasize; 726 pp->sectorsize = dp->d_sectorsize; 727 pp->stripeoffset = dp->d_stripeoffset; 728 pp->stripesize = dp->d_stripesize; 729 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0) 730 pp->flags |= G_PF_ACCEPT_UNMAPPED; 731 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0) 732 pp->flags |= G_PF_DIRECT_SEND; 733 pp->flags |= G_PF_DIRECT_RECEIVE; 734 if (bootverbose) 735 printf("GEOM: new disk %s\n", gp->name); 736 sysctl_ctx_init(&sc->sysctl_ctx); 737 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name); 738 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 739 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name, 740 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr); 741 if (sc->sysctl_tree != NULL) { 742 SYSCTL_ADD_STRING(&sc->sysctl_ctx, 743 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led", 744 CTLFLAG_RWTUN, sc->led, sizeof(sc->led), 745 "LED name"); 746 SYSCTL_ADD_PROC(&sc->sysctl_ctx, 747 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "flags", 748 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, dp, 0, 749 g_disk_sysctl_flags, "A", "Report disk flags"); 750 } 751 pp->private = sc; 752 dp->d_geom = gp; 753 g_error_provider(pp, 0); 754 755 mtx_pool_lock(mtxpool_sleep, dp); 756 dp->d_init_level = DISK_INIT_DONE; 757 758 /* 759 * If the disk has gone away at this stage, start the withering 760 * process for it. 761 */ 762 if (dp->d_goneflag != 0) { 763 mtx_pool_unlock(mtxpool_sleep, dp); 764 g_wither_provider(pp, ENXIO); 765 return; 766 } 767 mtx_pool_unlock(mtxpool_sleep, dp); 768 769 } 770 771 /* 772 * We get this callback after all of the consumers have gone away, and just 773 * before the provider is freed. If the disk driver provided a d_gone 774 * callback, let them know that it is okay to free resources -- they won't 775 * be getting any more accesses from GEOM. 776 */ 777 static void 778 g_disk_providergone(struct g_provider *pp) 779 { 780 struct disk *dp; 781 struct g_disk_softc *sc; 782 783 sc = (struct g_disk_softc *)pp->private; 784 dp = sc->dp; 785 if (dp != NULL && dp->d_gone != NULL) 786 dp->d_gone(dp); 787 if (sc->sysctl_tree != NULL) { 788 sysctl_ctx_free(&sc->sysctl_ctx); 789 sc->sysctl_tree = NULL; 790 } 791 if (sc->led[0] != 0) { 792 led_set(sc->led, "0"); 793 sc->led[0] = 0; 794 } 795 pp->private = NULL; 796 pp->geom->softc = NULL; 797 mtx_destroy(&sc->done_mtx); 798 g_free(sc); 799 } 800 801 static void 802 g_disk_destroy(void *ptr, int flag) 803 { 804 struct disk *dp; 805 struct g_geom *gp; 806 struct g_disk_softc *sc; 807 struct disk_alias *dap, *daptmp; 808 809 g_topology_assert(); 810 dp = ptr; 811 gp = dp->d_geom; 812 if (gp != NULL) { 813 sc = gp->softc; 814 if (sc != NULL) 815 sc->dp = NULL; 816 dp->d_geom = NULL; 817 g_wither_geom(gp, ENXIO); 818 } 819 LIST_FOREACH_SAFE(dap, &dp->d_aliases, da_next, daptmp) 820 g_free(dap); 821 822 g_free(dp); 823 } 824 825 /* 826 * We only allow printable characters in disk ident, 827 * the rest is converted to 'x<HH>'. 828 */ 829 static void 830 g_disk_ident_adjust(char *ident, size_t size) 831 { 832 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 833 834 newid[0] = '\0'; 835 for (p = ident; *p != '\0'; p++) { 836 if (isprint(*p)) { 837 tmp[0] = *p; 838 tmp[1] = '\0'; 839 } else { 840 snprintf(tmp, sizeof(tmp), "x%02hhx", 841 *(unsigned char *)p); 842 } 843 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 844 break; 845 } 846 bzero(ident, size); 847 strlcpy(ident, newid, size); 848 } 849 850 struct disk * 851 disk_alloc(void) 852 { 853 struct disk *dp; 854 855 dp = g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO); 856 LIST_INIT(&dp->d_aliases); 857 return (dp); 858 } 859 860 void 861 disk_create(struct disk *dp, int version) 862 { 863 864 if (version != DISK_VERSION) { 865 printf("WARNING: Attempt to add disk %s%d %s", 866 dp->d_name, dp->d_unit, 867 " using incompatible ABI version of disk(9)\n"); 868 printf("WARNING: Ignoring disk %s%d\n", 869 dp->d_name, dp->d_unit); 870 return; 871 } 872 if (dp->d_flags & DISKFLAG_RESERVED) { 873 printf("WARNING: Attempt to add non-MPSAFE disk %s%d\n", 874 dp->d_name, dp->d_unit); 875 printf("WARNING: Ignoring disk %s%d\n", 876 dp->d_name, dp->d_unit); 877 return; 878 } 879 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 880 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 881 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 882 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 883 if (dp->d_devstat == NULL) 884 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 885 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 886 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 887 dp->d_geom = NULL; 888 dp->d_event = g_alloc_event(M_WAITOK); 889 890 dp->d_init_level = DISK_INIT_NONE; 891 892 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 893 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 894 } 895 896 void 897 disk_destroy(struct disk *dp) 898 { 899 900 KASSERT(dp->d_event != NULL, 901 ("Disk destroy for %p with event NULL", dp)); 902 disk_gone(dp); 903 dp->d_destroyed = 1; 904 g_cancel_event(dp); 905 if (dp->d_devstat != NULL) 906 devstat_remove_entry(dp->d_devstat); 907 g_post_event_ep(g_disk_destroy, dp, dp->d_event, NULL); 908 } 909 910 void 911 disk_add_alias(struct disk *dp, const char *name) 912 { 913 struct disk_alias *dap; 914 915 dap = (struct disk_alias *)g_malloc( 916 sizeof(struct disk_alias) + strlen(name) + 1, M_WAITOK); 917 strcpy((char *)(dap + 1), name); 918 dap->da_alias = (const char *)(dap + 1); 919 LIST_INSERT_HEAD(&dp->d_aliases, dap, da_next); 920 } 921 922 void 923 disk_gone(struct disk *dp) 924 { 925 struct g_geom *gp; 926 struct g_provider *pp; 927 928 mtx_pool_lock(mtxpool_sleep, dp); 929 930 /* 931 * Second wither call makes no sense, plus we can not access the list 932 * of providers without topology lock after calling wither once. 933 */ 934 if (dp->d_goneflag != 0) { 935 mtx_pool_unlock(mtxpool_sleep, dp); 936 return; 937 } 938 939 dp->d_goneflag = 1; 940 941 /* 942 * If we're still in the process of creating this disk (the 943 * g_disk_create() function is still queued, or is in 944 * progress), the init level will not yet be DISK_INIT_DONE. 945 * 946 * If that is the case, g_disk_create() will see d_goneflag 947 * and take care of cleaning things up. 948 * 949 * If the disk has already been created, we default to 950 * withering the provider as usual below. 951 * 952 * If the caller has not set a d_gone() callback, he will 953 * not be any worse off by returning here, because the geom 954 * has not been fully setup in any case. 955 */ 956 if (dp->d_init_level < DISK_INIT_DONE) { 957 mtx_pool_unlock(mtxpool_sleep, dp); 958 return; 959 } 960 mtx_pool_unlock(mtxpool_sleep, dp); 961 962 gp = dp->d_geom; 963 pp = LIST_FIRST(&gp->provider); 964 if (pp != NULL) { 965 KASSERT(LIST_NEXT(pp, provider) == NULL, 966 ("geom %p has more than one provider", gp)); 967 g_wither_provider(pp, ENXIO); 968 } 969 } 970 971 void 972 disk_attr_changed(struct disk *dp, const char *attr, int flag) 973 { 974 struct g_geom *gp; 975 struct g_provider *pp; 976 char devnamebuf[128]; 977 978 gp = dp->d_geom; 979 if (gp != NULL) 980 LIST_FOREACH(pp, &gp->provider, provider) 981 (void)g_attr_changed(pp, attr, flag); 982 snprintf(devnamebuf, sizeof(devnamebuf), "devname=%s%d", dp->d_name, 983 dp->d_unit); 984 devctl_notify("GEOM", "disk", attr, devnamebuf); 985 } 986 987 void 988 disk_media_changed(struct disk *dp, int flag) 989 { 990 struct g_geom *gp; 991 struct g_provider *pp; 992 993 gp = dp->d_geom; 994 if (gp != NULL) { 995 pp = LIST_FIRST(&gp->provider); 996 if (pp != NULL) { 997 KASSERT(LIST_NEXT(pp, provider) == NULL, 998 ("geom %p has more than one provider", gp)); 999 g_media_changed(pp, flag); 1000 } 1001 } 1002 } 1003 1004 void 1005 disk_media_gone(struct disk *dp, int flag) 1006 { 1007 struct g_geom *gp; 1008 struct g_provider *pp; 1009 1010 gp = dp->d_geom; 1011 if (gp != NULL) { 1012 pp = LIST_FIRST(&gp->provider); 1013 if (pp != NULL) { 1014 KASSERT(LIST_NEXT(pp, provider) == NULL, 1015 ("geom %p has more than one provider", gp)); 1016 g_media_gone(pp, flag); 1017 } 1018 } 1019 } 1020 1021 int 1022 disk_resize(struct disk *dp, int flag) 1023 { 1024 1025 if (dp->d_destroyed || dp->d_geom == NULL) 1026 return (0); 1027 1028 return (g_post_event(g_disk_resize, dp, flag, NULL)); 1029 } 1030 1031 static void 1032 g_kern_disks(void *p, int flag __unused) 1033 { 1034 struct sbuf *sb; 1035 struct g_geom *gp; 1036 char *sp; 1037 1038 sb = p; 1039 sp = ""; 1040 g_topology_assert(); 1041 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 1042 sbuf_printf(sb, "%s%s", sp, gp->name); 1043 sp = " "; 1044 } 1045 sbuf_finish(sb); 1046 } 1047 1048 static int 1049 g_disk_sysctl_flags(SYSCTL_HANDLER_ARGS) 1050 { 1051 struct disk *dp; 1052 struct sbuf *sb; 1053 int error; 1054 1055 sb = sbuf_new_auto(); 1056 dp = (struct disk *)arg1; 1057 sbuf_printf(sb, "%b", dp->d_flags, 1058 "\20" 1059 "\2OPEN" 1060 "\3CANDELETE" 1061 "\4CANFLUSHCACHE" 1062 "\5UNMAPPEDBIO" 1063 "\6DIRECTCOMPLETION" 1064 "\10CANZONE" 1065 "\11WRITEPROTECT"); 1066 1067 sbuf_finish(sb); 1068 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 1069 sbuf_delete(sb); 1070 return (error); 1071 } 1072 1073 static int 1074 sysctl_disks(SYSCTL_HANDLER_ARGS) 1075 { 1076 int error; 1077 struct sbuf *sb; 1078 1079 sb = sbuf_new_auto(); 1080 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 1081 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 1082 sbuf_delete(sb); 1083 return error; 1084 } 1085 1086 SYSCTL_PROC(_kern, OID_AUTO, disks, 1087 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 1088 sysctl_disks, "A", "names of available disks"); 1089