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