geom_disk.c revision 260385
1193326Sed/*- 2193326Sed * Copyright (c) 2002 Poul-Henning Kamp 3193326Sed * Copyright (c) 2002 Networks Associates Technology, Inc. 4193326Sed * All rights reserved. 5193326Sed * 6193326Sed * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7193326Sed * and NAI Labs, the Security Research Division of Network Associates, Inc. 8193326Sed * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9193326Sed * DARPA CHATS research program. 10193326Sed * 11193326Sed * Redistribution and use in source and binary forms, with or without 12193326Sed * modification, are permitted provided that the following conditions 13193326Sed * are met: 14193326Sed * 1. Redistributions of source code must retain the above copyright 15193326Sed * notice, this list of conditions and the following disclaimer. 16193326Sed * 2. Redistributions in binary form must reproduce the above copyright 17193326Sed * notice, this list of conditions and the following disclaimer in the 18193326Sed * documentation and/or other materials provided with the distribution. 19193326Sed * 3. The names of the authors may not be used to endorse or promote 20193326Sed * products derived from this software without specific prior written 21193326Sed * permission. 22193326Sed * 23193326Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24193326Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25193326Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26193326Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27193326Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29193326Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32193326Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33193326Sed * SUCH DAMAGE. 34193326Sed */ 35193326Sed 36193326Sed#include <sys/cdefs.h> 37193326Sed__FBSDID("$FreeBSD: stable/10/sys/geom/geom_disk.c 260385 2014-01-07 01:32:23Z scottl $"); 38193326Sed 39193326Sed#include "opt_geom.h" 40193326Sed 41193326Sed#include <sys/param.h> 42193326Sed#include <sys/systm.h> 43193326Sed#include <sys/kernel.h> 44193326Sed#include <sys/sysctl.h> 45193326Sed#include <sys/bio.h> 46193326Sed#include <sys/ctype.h> 47193326Sed#include <sys/fcntl.h> 48193326Sed#include <sys/malloc.h> 49193326Sed#include <sys/sbuf.h> 50193326Sed#include <sys/sysctl.h> 51193326Sed#include <sys/devicestat.h> 52193326Sed#include <machine/md_var.h> 53193326Sed 54193326Sed#include <sys/lock.h> 55193326Sed#include <sys/mutex.h> 56193326Sed#include <geom/geom.h> 57193326Sed#include <geom/geom_disk.h> 58193326Sed#include <geom/geom_int.h> 59193326Sed 60193326Sed#include <dev/led/led.h> 61193326Sed 62193326Sedstruct g_disk_softc { 63193326Sed struct mtx done_mtx; 64193326Sed struct disk *dp; 65193326Sed struct sysctl_ctx_list sysctl_ctx; 66193326Sed struct sysctl_oid *sysctl_tree; 67193326Sed char led[64]; 68193326Sed uint32_t state; 69193326Sed struct mtx start_mtx; 70193326Sed}; 71193326Sed 72193326Sedstatic g_access_t g_disk_access; 73193326Sedstatic g_start_t g_disk_start; 74193326Sedstatic g_ioctl_t g_disk_ioctl; 75193326Sedstatic g_dumpconf_t g_disk_dumpconf; 76193326Sedstatic g_provgone_t g_disk_providergone; 77193326Sed 78193326Sedstatic struct g_class g_disk_class = { 79193326Sed .name = G_DISK_CLASS_NAME, 80193326Sed .version = G_VERSION, 81193326Sed .start = g_disk_start, 82193326Sed .access = g_disk_access, 83193326Sed .ioctl = g_disk_ioctl, 84193326Sed .providergone = g_disk_providergone, 85193326Sed .dumpconf = g_disk_dumpconf, 86193326Sed}; 87193326Sed 88193326SedSYSCTL_DECL(_kern_geom); 89193326Sedstatic SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0, 90193326Sed "GEOM_DISK stuff"); 91193326Sed 92193326SedDECLARE_GEOM_CLASS(g_disk_class, g_disk); 93193326Sed 94193326Sedstatic void __inline 95193326Sedg_disk_lock_giant(struct disk *dp) 96193326Sed{ 97193326Sed 98193326Sed if (dp->d_flags & DISKFLAG_NEEDSGIANT) 99193326Sed mtx_lock(&Giant); 100193326Sed} 101193326Sed 102193326Sedstatic void __inline 103193326Sedg_disk_unlock_giant(struct disk *dp) 104193326Sed{ 105193326Sed 106193326Sed if (dp->d_flags & DISKFLAG_NEEDSGIANT) 107193326Sed mtx_unlock(&Giant); 108193326Sed} 109193326Sed 110193326Sedstatic int 111193326Sedg_disk_access(struct g_provider *pp, int r, int w, int e) 112193326Sed{ 113193326Sed struct disk *dp; 114193326Sed struct g_disk_softc *sc; 115193326Sed int error; 116193326Sed 117193326Sed g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 118193326Sed pp->name, r, w, e); 119193326Sed g_topology_assert(); 120193326Sed sc = pp->private; 121193326Sed if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { 122193326Sed /* 123193326Sed * Allow decreasing access count even if disk is not 124193326Sed * avaliable anymore. 125193326Sed */ 126193326Sed if (r <= 0 && w <= 0 && e <= 0) 127193326Sed return (0); 128193326Sed return (ENXIO); 129193326Sed } 130193326Sed r += pp->acr; 131193326Sed w += pp->acw; 132193326Sed e += pp->ace; 133193326Sed error = 0; 134193326Sed if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 135193326Sed if (dp->d_open != NULL) { 136193326Sed g_disk_lock_giant(dp); 137193326Sed error = dp->d_open(dp); 138193326Sed if (bootverbose && error != 0) 139193326Sed printf("Opened disk %s -> %d\n", 140193326Sed pp->name, error); 141193326Sed g_disk_unlock_giant(dp); 142193326Sed if (error != 0) 143193326Sed return (error); 144193326Sed } 145193326Sed pp->mediasize = dp->d_mediasize; 146193326Sed pp->sectorsize = dp->d_sectorsize; 147193326Sed if (dp->d_maxsize == 0) { 148193326Sed printf("WARNING: Disk drive %s%d has no d_maxsize\n", 149193326Sed dp->d_name, dp->d_unit); 150193326Sed dp->d_maxsize = DFLTPHYS; 151193326Sed } 152193326Sed if (dp->d_delmaxsize == 0) { 153193326Sed if (bootverbose && dp->d_flags & DISKFLAG_CANDELETE) { 154193326Sed printf("WARNING: Disk drive %s%d has no " 155193326Sed "d_delmaxsize\n", dp->d_name, dp->d_unit); 156193326Sed } 157193326Sed dp->d_delmaxsize = dp->d_maxsize; 158193326Sed } 159193326Sed pp->stripeoffset = dp->d_stripeoffset; 160193326Sed pp->stripesize = dp->d_stripesize; 161193326Sed dp->d_flags |= DISKFLAG_OPEN; 162193326Sed } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 163193326Sed if (dp->d_close != NULL) { 164193326Sed g_disk_lock_giant(dp); 165193326Sed error = dp->d_close(dp); 166193326Sed if (error != 0) 167193326Sed printf("Closed disk %s -> %d\n", 168193326Sed pp->name, error); 169193326Sed g_disk_unlock_giant(dp); 170193326Sed } 171193326Sed sc->state = G_STATE_ACTIVE; 172193326Sed if (sc->led[0] != 0) 173193326Sed led_set(sc->led, "0"); 174193326Sed dp->d_flags &= ~DISKFLAG_OPEN; 175193326Sed } 176193326Sed return (error); 177193326Sed} 178193326Sed 179193326Sedstatic void 180193326Sedg_disk_kerneldump(struct bio *bp, struct disk *dp) 181193326Sed{ 182193326Sed struct g_kerneldump *gkd; 183 struct g_geom *gp; 184 185 gkd = (struct g_kerneldump*)bp->bio_data; 186 gp = bp->bio_to->geom; 187 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 188 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 189 if (dp->d_dump == NULL) { 190 g_io_deliver(bp, ENODEV); 191 return; 192 } 193 gkd->di.dumper = dp->d_dump; 194 gkd->di.priv = dp; 195 gkd->di.blocksize = dp->d_sectorsize; 196 gkd->di.maxiosize = dp->d_maxsize; 197 gkd->di.mediaoffset = gkd->offset; 198 if ((gkd->offset + gkd->length) > dp->d_mediasize) 199 gkd->length = dp->d_mediasize - gkd->offset; 200 gkd->di.mediasize = gkd->length; 201 g_io_deliver(bp, 0); 202} 203 204static void 205g_disk_setstate(struct bio *bp, struct g_disk_softc *sc) 206{ 207 const char *cmd; 208 209 memcpy(&sc->state, bp->bio_data, sizeof(sc->state)); 210 if (sc->led[0] != 0) { 211 switch (sc->state) { 212 case G_STATE_FAILED: 213 cmd = "1"; 214 break; 215 case G_STATE_REBUILD: 216 cmd = "f5"; 217 break; 218 case G_STATE_RESYNC: 219 cmd = "f1"; 220 break; 221 default: 222 cmd = "0"; 223 break; 224 } 225 led_set(sc->led, cmd); 226 } 227 g_io_deliver(bp, 0); 228} 229 230static void 231g_disk_done(struct bio *bp) 232{ 233 struct bintime now; 234 struct bio *bp2; 235 struct g_disk_softc *sc; 236 237 /* See "notes" for why we need a mutex here */ 238 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 239 bp2 = bp->bio_parent; 240 sc = bp2->bio_to->private; 241 bp->bio_completed = bp->bio_length - bp->bio_resid; 242 binuptime(&now); 243 mtx_lock(&sc->done_mtx); 244 if (bp2->bio_error == 0) 245 bp2->bio_error = bp->bio_error; 246 bp2->bio_completed += bp->bio_completed; 247 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0) 248 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now); 249 bp2->bio_inbed++; 250 if (bp2->bio_children == bp2->bio_inbed) { 251 mtx_unlock(&sc->done_mtx); 252 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 253 g_io_deliver(bp2, bp2->bio_error); 254 } else 255 mtx_unlock(&sc->done_mtx); 256 g_destroy_bio(bp); 257} 258 259static void 260g_disk_done_single(struct bio *bp) 261{ 262 struct bintime now; 263 struct g_disk_softc *sc; 264 265 bp->bio_completed = bp->bio_length - bp->bio_resid; 266 bp->bio_done = (void *)bp->bio_to; 267 bp->bio_to = LIST_FIRST(&bp->bio_disk->d_geom->provider); 268 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0) { 269 binuptime(&now); 270 sc = bp->bio_to->private; 271 mtx_lock(&sc->done_mtx); 272 devstat_end_transaction_bio_bt(sc->dp->d_devstat, bp, &now); 273 mtx_unlock(&sc->done_mtx); 274 } 275 g_io_deliver(bp, bp->bio_error); 276} 277 278static int 279g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 280{ 281 struct disk *dp; 282 struct g_disk_softc *sc; 283 int error; 284 285 sc = pp->private; 286 dp = sc->dp; 287 288 if (dp->d_ioctl == NULL) 289 return (ENOIOCTL); 290 g_disk_lock_giant(dp); 291 error = dp->d_ioctl(dp, cmd, data, fflag, td); 292 g_disk_unlock_giant(dp); 293 return (error); 294} 295 296static void 297g_disk_start(struct bio *bp) 298{ 299 struct bio *bp2, *bp3; 300 struct disk *dp; 301 struct g_disk_softc *sc; 302 int error; 303 off_t d_maxsize, off; 304 305 sc = bp->bio_to->private; 306 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { 307 g_io_deliver(bp, ENXIO); 308 return; 309 } 310 error = EJUSTRETURN; 311 switch(bp->bio_cmd) { 312 case BIO_DELETE: 313 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 314 error = EOPNOTSUPP; 315 break; 316 } 317 /* fall-through */ 318 case BIO_READ: 319 case BIO_WRITE: 320 d_maxsize = (bp->bio_cmd == BIO_DELETE) ? 321 dp->d_delmaxsize : dp->d_maxsize; 322 if (bp->bio_length <= d_maxsize) { 323 bp->bio_disk = dp; 324 bp->bio_to = (void *)bp->bio_done; 325 bp->bio_done = g_disk_done_single; 326 bp->bio_pblkno = bp->bio_offset / dp->d_sectorsize; 327 bp->bio_bcount = bp->bio_length; 328 mtx_lock(&sc->start_mtx); 329 devstat_start_transaction_bio(dp->d_devstat, bp); 330 mtx_unlock(&sc->start_mtx); 331 g_disk_lock_giant(dp); 332 dp->d_strategy(bp); 333 g_disk_unlock_giant(dp); 334 break; 335 } 336 off = 0; 337 bp3 = NULL; 338 bp2 = g_clone_bio(bp); 339 if (bp2 == NULL) { 340 error = ENOMEM; 341 break; 342 } 343 do { 344 bp2->bio_offset += off; 345 bp2->bio_length -= off; 346 if ((bp->bio_flags & BIO_UNMAPPED) == 0) { 347 bp2->bio_data += off; 348 } else { 349 KASSERT((dp->d_flags & DISKFLAG_UNMAPPED_BIO) 350 != 0, 351 ("unmapped bio not supported by disk %s", 352 dp->d_name)); 353 bp2->bio_ma += off / PAGE_SIZE; 354 bp2->bio_ma_offset += off; 355 bp2->bio_ma_offset %= PAGE_SIZE; 356 bp2->bio_ma_n -= off / PAGE_SIZE; 357 } 358 if (bp2->bio_length > d_maxsize) { 359 /* 360 * XXX: If we have a stripesize we should really 361 * use it here. Care should be taken in the delete 362 * case if this is done as deletes can be very 363 * sensitive to size given how they are processed. 364 */ 365 bp2->bio_length = d_maxsize; 366 if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 367 bp2->bio_ma_n = howmany( 368 bp2->bio_ma_offset + 369 bp2->bio_length, PAGE_SIZE); 370 } 371 off += d_maxsize; 372 /* 373 * To avoid a race, we need to grab the next bio 374 * before we schedule this one. See "notes". 375 */ 376 bp3 = g_clone_bio(bp); 377 if (bp3 == NULL) 378 bp->bio_error = ENOMEM; 379 } 380 bp2->bio_done = g_disk_done; 381 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 382 bp2->bio_bcount = bp2->bio_length; 383 bp2->bio_disk = dp; 384 mtx_lock(&sc->start_mtx); 385 devstat_start_transaction_bio(dp->d_devstat, bp2); 386 mtx_unlock(&sc->start_mtx); 387 g_disk_lock_giant(dp); 388 dp->d_strategy(bp2); 389 g_disk_unlock_giant(dp); 390 bp2 = bp3; 391 bp3 = NULL; 392 } while (bp2 != NULL); 393 break; 394 case BIO_GETATTR: 395 /* Give the driver a chance to override */ 396 if (dp->d_getattr != NULL) { 397 if (bp->bio_disk == NULL) 398 bp->bio_disk = dp; 399 error = dp->d_getattr(bp); 400 if (error != -1) 401 break; 402 error = EJUSTRETURN; 403 } 404 if (g_handleattr_int(bp, "GEOM::candelete", 405 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 406 break; 407 else if (g_handleattr_int(bp, "GEOM::fwsectors", 408 dp->d_fwsectors)) 409 break; 410 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 411 break; 412 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 413 break; 414 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 415 break; 416 else if (g_handleattr(bp, "GEOM::hba_vendor", 417 &dp->d_hba_vendor, 2)) 418 break; 419 else if (g_handleattr(bp, "GEOM::hba_device", 420 &dp->d_hba_device, 2)) 421 break; 422 else if (g_handleattr(bp, "GEOM::hba_subvendor", 423 &dp->d_hba_subvendor, 2)) 424 break; 425 else if (g_handleattr(bp, "GEOM::hba_subdevice", 426 &dp->d_hba_subdevice, 2)) 427 break; 428 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 429 g_disk_kerneldump(bp, dp); 430 else if (!strcmp(bp->bio_attribute, "GEOM::setstate")) 431 g_disk_setstate(bp, sc); 432 else 433 error = ENOIOCTL; 434 break; 435 case BIO_FLUSH: 436 g_trace(G_T_BIO, "g_disk_flushcache(%s)", 437 bp->bio_to->name); 438 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 439 error = EOPNOTSUPP; 440 break; 441 } 442 bp->bio_disk = dp; 443 bp->bio_to = (void *)bp->bio_done; 444 bp->bio_done = g_disk_done_single; 445 g_disk_lock_giant(dp); 446 dp->d_strategy(bp); 447 g_disk_unlock_giant(dp); 448 break; 449 default: 450 error = EOPNOTSUPP; 451 break; 452 } 453 if (error != EJUSTRETURN) 454 g_io_deliver(bp, error); 455 return; 456} 457 458static void 459g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 460{ 461 struct bio *bp; 462 struct disk *dp; 463 struct g_disk_softc *sc; 464 char *buf; 465 int res = 0; 466 467 sc = gp->softc; 468 if (sc == NULL || (dp = sc->dp) == NULL) 469 return; 470 if (indent == NULL) { 471 sbuf_printf(sb, " hd %u", dp->d_fwheads); 472 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 473 return; 474 } 475 if (pp != NULL) { 476 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 477 indent, dp->d_fwheads); 478 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 479 indent, dp->d_fwsectors); 480 if (dp->d_getattr != NULL) { 481 buf = g_malloc(DISK_IDENT_SIZE, M_WAITOK); 482 bp = g_alloc_bio(); 483 bp->bio_disk = dp; 484 bp->bio_attribute = "GEOM::ident"; 485 bp->bio_length = DISK_IDENT_SIZE; 486 bp->bio_data = buf; 487 res = dp->d_getattr(bp); 488 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, 489 res == 0 ? buf: dp->d_ident); 490 bp->bio_attribute = "GEOM::lunid"; 491 bp->bio_length = DISK_IDENT_SIZE; 492 bp->bio_data = buf; 493 if (dp->d_getattr(bp) == 0) 494 sbuf_printf(sb, "%s<lunid>%s</lunid>\n", 495 indent, buf); 496 bp->bio_attribute = "GEOM::lunname"; 497 bp->bio_length = DISK_IDENT_SIZE; 498 bp->bio_data = buf; 499 if (dp->d_getattr(bp) == 0) 500 sbuf_printf(sb, "%s<lunname>%s</lunname>\n", 501 indent, buf); 502 g_destroy_bio(bp); 503 g_free(buf); 504 } else 505 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, 506 dp->d_ident); 507 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr); 508 } 509} 510 511static void 512g_disk_resize(void *ptr, int flag) 513{ 514 struct disk *dp; 515 struct g_geom *gp; 516 struct g_provider *pp; 517 518 if (flag == EV_CANCEL) 519 return; 520 g_topology_assert(); 521 522 dp = ptr; 523 gp = dp->d_geom; 524 525 if (dp->d_destroyed || gp == NULL) 526 return; 527 528 LIST_FOREACH(pp, &gp->provider, provider) { 529 if (pp->sectorsize != 0 && 530 pp->sectorsize != dp->d_sectorsize) 531 g_wither_provider(pp, ENXIO); 532 else 533 g_resize_provider(pp, dp->d_mediasize); 534 } 535} 536 537static void 538g_disk_create(void *arg, int flag) 539{ 540 struct g_geom *gp; 541 struct g_provider *pp; 542 struct disk *dp; 543 struct g_disk_softc *sc; 544 char tmpstr[80]; 545 546 if (flag == EV_CANCEL) 547 return; 548 g_topology_assert(); 549 dp = arg; 550 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 551 mtx_init(&sc->start_mtx, "g_disk_start", NULL, MTX_DEF); 552 mtx_init(&sc->done_mtx, "g_disk_done", NULL, MTX_DEF); 553 sc->dp = dp; 554 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 555 gp->softc = sc; 556 pp = g_new_providerf(gp, "%s", gp->name); 557 devstat_remove_entry(pp->stat); 558 pp->stat = NULL; 559 dp->d_devstat->id = pp; 560 pp->mediasize = dp->d_mediasize; 561 pp->sectorsize = dp->d_sectorsize; 562 pp->stripeoffset = dp->d_stripeoffset; 563 pp->stripesize = dp->d_stripesize; 564 if ((dp->d_flags & DISKFLAG_UNMAPPED_BIO) != 0) 565 pp->flags |= G_PF_ACCEPT_UNMAPPED; 566 if ((dp->d_flags & DISKFLAG_DIRECT_COMPLETION) != 0) 567 pp->flags |= G_PF_DIRECT_SEND; 568 pp->flags |= G_PF_DIRECT_RECEIVE; 569 if (bootverbose) 570 printf("GEOM: new disk %s\n", gp->name); 571 sysctl_ctx_init(&sc->sysctl_ctx); 572 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name); 573 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 574 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name, 575 CTLFLAG_RD, 0, tmpstr); 576 if (sc->sysctl_tree != NULL) { 577 snprintf(tmpstr, sizeof(tmpstr), 578 "kern.geom.disk.%s.led", gp->name); 579 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led)); 580 SYSCTL_ADD_STRING(&sc->sysctl_ctx, 581 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led", 582 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led), 583 "LED name"); 584 } 585 pp->private = sc; 586 dp->d_geom = gp; 587 g_error_provider(pp, 0); 588} 589 590/* 591 * We get this callback after all of the consumers have gone away, and just 592 * before the provider is freed. If the disk driver provided a d_gone 593 * callback, let them know that it is okay to free resources -- they won't 594 * be getting any more accesses from GEOM. 595 */ 596static void 597g_disk_providergone(struct g_provider *pp) 598{ 599 struct disk *dp; 600 struct g_disk_softc *sc; 601 602 sc = (struct g_disk_softc *)pp->private; 603 dp = sc->dp; 604 if (dp != NULL && dp->d_gone != NULL) 605 dp->d_gone(dp); 606 if (sc->sysctl_tree != NULL) { 607 sysctl_ctx_free(&sc->sysctl_ctx); 608 sc->sysctl_tree = NULL; 609 } 610 if (sc->led[0] != 0) { 611 led_set(sc->led, "0"); 612 sc->led[0] = 0; 613 } 614 pp->private = NULL; 615 pp->geom->softc = NULL; 616 mtx_destroy(&sc->done_mtx); 617 mtx_destroy(&sc->start_mtx); 618 g_free(sc); 619} 620 621static void 622g_disk_destroy(void *ptr, int flag) 623{ 624 struct disk *dp; 625 struct g_geom *gp; 626 struct g_disk_softc *sc; 627 628 g_topology_assert(); 629 dp = ptr; 630 gp = dp->d_geom; 631 if (gp != NULL) { 632 sc = gp->softc; 633 if (sc != NULL) 634 sc->dp = NULL; 635 dp->d_geom = NULL; 636 g_wither_geom(gp, ENXIO); 637 } 638 g_free(dp); 639} 640 641/* 642 * We only allow printable characters in disk ident, 643 * the rest is converted to 'x<HH>'. 644 */ 645static void 646g_disk_ident_adjust(char *ident, size_t size) 647{ 648 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 649 650 newid[0] = '\0'; 651 for (p = ident; *p != '\0'; p++) { 652 if (isprint(*p)) { 653 tmp[0] = *p; 654 tmp[1] = '\0'; 655 } else { 656 snprintf(tmp, sizeof(tmp), "x%02hhx", 657 *(unsigned char *)p); 658 } 659 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 660 break; 661 } 662 bzero(ident, size); 663 strlcpy(ident, newid, size); 664} 665 666struct disk * 667disk_alloc(void) 668{ 669 670 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO)); 671} 672 673void 674disk_create(struct disk *dp, int version) 675{ 676 677 if (version != DISK_VERSION) { 678 printf("WARNING: Attempt to add disk %s%d %s", 679 dp->d_name, dp->d_unit, 680 " using incompatible ABI version of disk(9)\n"); 681 printf("WARNING: Ignoring disk %s%d\n", 682 dp->d_name, dp->d_unit); 683 return; 684 } 685 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 686 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 687 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 688 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 689 if (dp->d_devstat == NULL) 690 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 691 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 692 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 693 dp->d_geom = NULL; 694 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 695 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 696} 697 698void 699disk_destroy(struct disk *dp) 700{ 701 702 g_cancel_event(dp); 703 dp->d_destroyed = 1; 704 if (dp->d_devstat != NULL) 705 devstat_remove_entry(dp->d_devstat); 706 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 707} 708 709void 710disk_gone(struct disk *dp) 711{ 712 struct g_geom *gp; 713 struct g_provider *pp; 714 715 gp = dp->d_geom; 716 if (gp != NULL) { 717 pp = LIST_FIRST(&gp->provider); 718 if (pp != NULL) { 719 KASSERT(LIST_NEXT(pp, provider) == NULL, 720 ("geom %p has more than one provider", gp)); 721 g_wither_provider(pp, ENXIO); 722 } 723 } 724} 725 726void 727disk_attr_changed(struct disk *dp, const char *attr, int flag) 728{ 729 struct g_geom *gp; 730 struct g_provider *pp; 731 732 gp = dp->d_geom; 733 if (gp != NULL) 734 LIST_FOREACH(pp, &gp->provider, provider) 735 (void)g_attr_changed(pp, attr, flag); 736} 737 738void 739disk_media_changed(struct disk *dp, int flag) 740{ 741 struct g_geom *gp; 742 struct g_provider *pp; 743 744 gp = dp->d_geom; 745 if (gp != NULL) { 746 pp = LIST_FIRST(&gp->provider); 747 if (pp != NULL) { 748 KASSERT(LIST_NEXT(pp, provider) == NULL, 749 ("geom %p has more than one provider", gp)); 750 g_media_changed(pp, flag); 751 } 752 } 753} 754 755void 756disk_media_gone(struct disk *dp, int flag) 757{ 758 struct g_geom *gp; 759 struct g_provider *pp; 760 761 gp = dp->d_geom; 762 if (gp != NULL) { 763 pp = LIST_FIRST(&gp->provider); 764 if (pp != NULL) { 765 KASSERT(LIST_NEXT(pp, provider) == NULL, 766 ("geom %p has more than one provider", gp)); 767 g_media_gone(pp, flag); 768 } 769 } 770} 771 772int 773disk_resize(struct disk *dp, int flag) 774{ 775 776 if (dp->d_destroyed || dp->d_geom == NULL) 777 return (0); 778 779 return (g_post_event(g_disk_resize, dp, flag, NULL)); 780} 781 782static void 783g_kern_disks(void *p, int flag __unused) 784{ 785 struct sbuf *sb; 786 struct g_geom *gp; 787 char *sp; 788 789 sb = p; 790 sp = ""; 791 g_topology_assert(); 792 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 793 sbuf_printf(sb, "%s%s", sp, gp->name); 794 sp = " "; 795 } 796 sbuf_finish(sb); 797} 798 799static int 800sysctl_disks(SYSCTL_HANDLER_ARGS) 801{ 802 int error; 803 struct sbuf *sb; 804 805 sb = sbuf_new_auto(); 806 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 807 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 808 sbuf_delete(sb); 809 return error; 810} 811 812SYSCTL_PROC(_kern, OID_AUTO, disks, 813 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 814 sysctl_disks, "A", "names of available disks"); 815