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 <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/kernel.h> 43 #include <sys/conf.h> 44 #include <sys/ctype.h> 45 #include <sys/bio.h> 46 #include <sys/bus.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/proc.h> 50 #include <sys/errno.h> 51 #include <sys/time.h> 52 #include <sys/disk.h> 53 #include <sys/fcntl.h> 54 #include <sys/limits.h> 55 #include <sys/sysctl.h> 56 #include <geom/geom.h> 57 #include <geom/geom_int.h> 58 #include <machine/stdarg.h> 59 60 struct g_dev_softc { 61 struct mtx sc_mtx; 62 struct cdev *sc_dev; 63 struct cdev *sc_alias; 64 int sc_open; 65 int sc_active; 66 }; 67 68 static d_open_t g_dev_open; 69 static d_close_t g_dev_close; 70 static d_strategy_t g_dev_strategy; 71 static d_ioctl_t g_dev_ioctl; 72 73 static struct cdevsw g_dev_cdevsw = { 74 .d_version = D_VERSION, 75 .d_open = g_dev_open, 76 .d_close = g_dev_close, 77 .d_read = physread, 78 .d_write = physwrite, 79 .d_ioctl = g_dev_ioctl, 80 .d_strategy = g_dev_strategy, 81 .d_name = "g_dev", 82 .d_flags = D_DISK | D_TRACKCLOSE | D_UNMAPPED_IO, 83 }; 84 85 static g_taste_t g_dev_taste; 86 static g_orphan_t g_dev_orphan; 87 static g_attrchanged_t g_dev_attrchanged; 88 89 static struct g_class g_dev_class = { 90 .name = "DEV", 91 .version = G_VERSION, 92 .taste = g_dev_taste, 93 .orphan = g_dev_orphan, 94 .attrchanged = g_dev_attrchanged 95 }; 96 97 /* 98 * We target 262144 (8 x 32768) sectors by default as this significantly 99 * increases the throughput on commonly used SSD's with a marginal 100 * increase in non-interruptible request latency. 101 */ 102 static uint64_t g_dev_del_max_sectors = 262144; 103 SYSCTL_DECL(_kern_geom); 104 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff"); 105 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW, 106 &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single " 107 "delete request sent to the provider. Larger requests are chunked " 108 "so they can be interrupted. (0 = disable chunking)"); 109 110 static void 111 g_dev_destroy(void *arg, int flags __unused) 112 { 113 struct g_consumer *cp; 114 struct g_geom *gp; 115 struct g_dev_softc *sc; 116 117 g_topology_assert(); 118 cp = arg; 119 gp = cp->geom; 120 sc = cp->private; 121 g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name); 122 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 123 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 124 g_detach(cp); 125 g_destroy_consumer(cp); 126 g_destroy_geom(gp); 127 mtx_destroy(&sc->sc_mtx); 128 g_free(sc); 129 } 130 131 void 132 g_dev_print(void) 133 { 134 struct g_geom *gp; 135 char const *p = ""; 136 137 LIST_FOREACH(gp, &g_dev_class.geom, geom) { 138 printf("%s%s", p, gp->name); 139 p = " "; 140 } 141 printf("\n"); 142 } 143 144 static void 145 g_dev_attrchanged(struct g_consumer *cp, const char *attr) 146 { 147 struct g_dev_softc *sc; 148 struct cdev *dev; 149 char buf[SPECNAMELEN + 6]; 150 151 sc = cp->private; 152 if (strcmp(attr, "GEOM::media") == 0) { 153 dev = sc->sc_dev; 154 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name); 155 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK); 156 dev = sc->sc_alias; 157 if (dev != NULL) { 158 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name); 159 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, 160 M_WAITOK); 161 } 162 return; 163 } 164 165 if (strcmp(attr, "GEOM::physpath") != 0) 166 return; 167 168 if (g_access(cp, 1, 0, 0) == 0) { 169 char *physpath; 170 int error, physpath_len; 171 172 physpath_len = MAXPATHLEN; 173 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO); 174 error = 175 g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath); 176 g_access(cp, -1, 0, 0); 177 if (error == 0 && strlen(physpath) != 0) { 178 struct cdev *old_alias_dev; 179 struct cdev **alias_devp; 180 181 dev = sc->sc_dev; 182 old_alias_dev = sc->sc_alias; 183 alias_devp = (struct cdev **)&sc->sc_alias; 184 make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, 185 dev, old_alias_dev, physpath); 186 } else if (sc->sc_alias) { 187 destroy_dev((struct cdev *)sc->sc_alias); 188 sc->sc_alias = NULL; 189 } 190 g_free(physpath); 191 } 192 } 193 194 struct g_provider * 195 g_dev_getprovider(struct cdev *dev) 196 { 197 struct g_consumer *cp; 198 199 g_topology_assert(); 200 if (dev == NULL) 201 return (NULL); 202 if (dev->si_devsw != &g_dev_cdevsw) 203 return (NULL); 204 cp = dev->si_drv2; 205 return (cp->provider); 206 } 207 208 static struct g_geom * 209 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 210 { 211 struct g_geom *gp; 212 struct g_consumer *cp; 213 struct g_dev_softc *sc; 214 int error, len; 215 struct cdev *dev, *adev; 216 char buf[64], *val; 217 218 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 219 g_topology_assert(); 220 gp = g_new_geomf(mp, "%s", pp->name); 221 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 222 mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF); 223 cp = g_new_consumer(gp); 224 cp->private = sc; 225 error = g_attach(cp, pp); 226 KASSERT(error == 0, 227 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 228 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev, 229 &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name); 230 if (error != 0) { 231 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n", 232 __func__, gp->name, error); 233 g_detach(cp); 234 g_destroy_consumer(cp); 235 g_destroy_geom(gp); 236 mtx_destroy(&sc->sc_mtx); 237 g_free(sc); 238 return (NULL); 239 } 240 sc->sc_dev = dev; 241 242 /* Search for device alias name and create it if found. */ 243 adev = NULL; 244 for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) { 245 snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name); 246 buf[14 + len] = 0; 247 val = getenv(buf); 248 if (val != NULL) { 249 snprintf(buf, sizeof(buf), "%s%s", 250 val, gp->name + len); 251 freeenv(val); 252 make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 253 &adev, dev, "%s", buf); 254 break; 255 } 256 } 257 258 dev->si_iosize_max = MAXPHYS; 259 dev->si_drv2 = cp; 260 if (adev != NULL) { 261 adev->si_iosize_max = MAXPHYS; 262 adev->si_drv2 = cp; 263 } 264 265 g_dev_attrchanged(cp, "GEOM::physpath"); 266 267 return (gp); 268 } 269 270 static int 271 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 272 { 273 struct g_consumer *cp; 274 struct g_dev_softc *sc; 275 int error, r, w, e; 276 277 cp = dev->si_drv2; 278 if (cp == NULL) 279 return(ENXIO); /* g_dev_taste() not done yet */ 280 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 281 cp->geom->name, flags, fmt, td); 282 283 r = flags & FREAD ? 1 : 0; 284 w = flags & FWRITE ? 1 : 0; 285 #ifdef notyet 286 e = flags & O_EXCL ? 1 : 0; 287 #else 288 e = 0; 289 #endif 290 if (w) { 291 /* 292 * When running in very secure mode, do not allow 293 * opens for writing of any disks. 294 */ 295 error = securelevel_ge(td->td_ucred, 2); 296 if (error) 297 return (error); 298 } 299 g_topology_lock(); 300 error = g_access(cp, r, w, e); 301 g_topology_unlock(); 302 if (error == 0) { 303 sc = cp->private; 304 mtx_lock(&sc->sc_mtx); 305 if (sc->sc_open == 0 && sc->sc_active != 0) 306 wakeup(&sc->sc_active); 307 sc->sc_open += r + w + e; 308 mtx_unlock(&sc->sc_mtx); 309 } 310 return(error); 311 } 312 313 static int 314 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 315 { 316 struct g_consumer *cp; 317 struct g_dev_softc *sc; 318 int error, r, w, e; 319 320 cp = dev->si_drv2; 321 if (cp == NULL) 322 return(ENXIO); 323 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 324 cp->geom->name, flags, fmt, td); 325 326 r = flags & FREAD ? -1 : 0; 327 w = flags & FWRITE ? -1 : 0; 328 #ifdef notyet 329 e = flags & O_EXCL ? -1 : 0; 330 #else 331 e = 0; 332 #endif 333 sc = cp->private; 334 mtx_lock(&sc->sc_mtx); 335 sc->sc_open += r + w + e; 336 while (sc->sc_open == 0 && sc->sc_active != 0) 337 msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0); 338 mtx_unlock(&sc->sc_mtx); 339 g_topology_lock(); 340 error = g_access(cp, r, w, e); 341 g_topology_unlock(); 342 return (error); 343 } 344 345 /* 346 * XXX: Until we have unmessed the ioctl situation, there is a race against 347 * XXX: a concurrent orphanization. We cannot close it by holding topology 348 * XXX: since that would prevent us from doing our job, and stalling events 349 * XXX: will break (actually: stall) the BSD disklabel hacks. 350 */ 351 static int 352 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 353 { 354 struct g_consumer *cp; 355 struct g_provider *pp; 356 struct g_kerneldump kd; 357 off_t offset, length, chunk; 358 int i, error; 359 u_int u; 360 361 cp = dev->si_drv2; 362 pp = cp->provider; 363 364 error = 0; 365 KASSERT(cp->acr || cp->acw, 366 ("Consumer with zero access count in g_dev_ioctl")); 367 368 i = IOCPARM_LEN(cmd); 369 switch (cmd) { 370 case DIOCGSECTORSIZE: 371 *(u_int *)data = cp->provider->sectorsize; 372 if (*(u_int *)data == 0) 373 error = ENOENT; 374 break; 375 case DIOCGMEDIASIZE: 376 *(off_t *)data = cp->provider->mediasize; 377 if (*(off_t *)data == 0) 378 error = ENOENT; 379 break; 380 case DIOCGFWSECTORS: 381 error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 382 if (error == 0 && *(u_int *)data == 0) 383 error = ENOENT; 384 break; 385 case DIOCGFWHEADS: 386 error = g_io_getattr("GEOM::fwheads", cp, &i, data); 387 if (error == 0 && *(u_int *)data == 0) 388 error = ENOENT; 389 break; 390 case DIOCGFRONTSTUFF: 391 error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 392 break; 393 case DIOCSKERNELDUMP: 394 u = *((u_int *)data); 395 if (!u) { 396 set_dumper(NULL, NULL); 397 error = 0; 398 break; 399 } 400 kd.offset = 0; 401 kd.length = OFF_MAX; 402 i = sizeof kd; 403 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 404 if (!error) { 405 error = set_dumper(&kd.di, devtoname(dev)); 406 if (!error) 407 dev->si_flags |= SI_DUMPDEV; 408 } 409 break; 410 case DIOCGFLUSH: 411 error = g_io_flush(cp); 412 break; 413 case DIOCGDELETE: 414 offset = ((off_t *)data)[0]; 415 length = ((off_t *)data)[1]; 416 if ((offset % cp->provider->sectorsize) != 0 || 417 (length % cp->provider->sectorsize) != 0 || length <= 0) { 418 printf("%s: offset=%jd length=%jd\n", __func__, offset, 419 length); 420 error = EINVAL; 421 break; 422 } 423 while (length > 0) { 424 chunk = length; 425 if (g_dev_del_max_sectors != 0 && chunk > 426 g_dev_del_max_sectors * cp->provider->sectorsize) { 427 chunk = g_dev_del_max_sectors * 428 cp->provider->sectorsize; 429 } 430 error = g_delete_data(cp, offset, chunk); 431 length -= chunk; 432 offset += chunk; 433 if (error) 434 break; 435 /* 436 * Since the request size can be large, the service 437 * time can be is likewise. We make this ioctl 438 * interruptible by checking for signals for each bio. 439 */ 440 if (SIGPENDING(td)) 441 break; 442 } 443 break; 444 case DIOCGIDENT: 445 error = g_io_getattr("GEOM::ident", cp, &i, data); 446 break; 447 case DIOCGPROVIDERNAME: 448 if (pp == NULL) 449 return (ENOENT); 450 strlcpy(data, pp->name, i); 451 break; 452 case DIOCGSTRIPESIZE: 453 *(off_t *)data = cp->provider->stripesize; 454 break; 455 case DIOCGSTRIPEOFFSET: 456 *(off_t *)data = cp->provider->stripeoffset; 457 break; 458 case DIOCGPHYSPATH: 459 error = g_io_getattr("GEOM::physpath", cp, &i, data); 460 if (error == 0 && *(char *)data == '\0') 461 error = ENOENT; 462 break; 463 default: 464 if (cp->provider->geom->ioctl != NULL) { 465 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 466 } else { 467 error = ENOIOCTL; 468 } 469 } 470 471 return (error); 472 } 473 474 static void 475 g_dev_done(struct bio *bp2) 476 { 477 struct g_consumer *cp; 478 struct g_dev_softc *sc; 479 struct bio *bp; 480 int destroy; 481 482 cp = bp2->bio_from; 483 sc = cp->private; 484 bp = bp2->bio_parent; 485 bp->bio_error = bp2->bio_error; 486 if (bp->bio_error != 0) { 487 g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 488 bp2, bp->bio_error); 489 bp->bio_flags |= BIO_ERROR; 490 } else { 491 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 492 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 493 } 494 bp->bio_resid = bp->bio_length - bp2->bio_completed; 495 bp->bio_completed = bp2->bio_completed; 496 g_destroy_bio(bp2); 497 destroy = 0; 498 mtx_lock(&sc->sc_mtx); 499 if ((--sc->sc_active) == 0) { 500 if (sc->sc_open == 0) 501 wakeup(&sc->sc_active); 502 if (sc->sc_dev == NULL) 503 destroy = 1; 504 } 505 mtx_unlock(&sc->sc_mtx); 506 if (destroy) 507 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 508 biodone(bp); 509 } 510 511 static void 512 g_dev_strategy(struct bio *bp) 513 { 514 struct g_consumer *cp; 515 struct bio *bp2; 516 struct cdev *dev; 517 struct g_dev_softc *sc; 518 519 KASSERT(bp->bio_cmd == BIO_READ || 520 bp->bio_cmd == BIO_WRITE || 521 bp->bio_cmd == BIO_DELETE || 522 bp->bio_cmd == BIO_FLUSH, 523 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 524 dev = bp->bio_dev; 525 cp = dev->si_drv2; 526 sc = cp->private; 527 KASSERT(cp->acr || cp->acw, 528 ("Consumer with zero access count in g_dev_strategy")); 529 #ifdef INVARIANTS 530 if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 531 (bp->bio_bcount % cp->provider->sectorsize) != 0) { 532 bp->bio_resid = bp->bio_bcount; 533 biofinish(bp, NULL, EINVAL); 534 return; 535 } 536 #endif 537 mtx_lock(&sc->sc_mtx); 538 KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy")); 539 sc->sc_active++; 540 mtx_unlock(&sc->sc_mtx); 541 542 for (;;) { 543 /* 544 * XXX: This is not an ideal solution, but I belive it to 545 * XXX: deadlock safe, all things considered. 546 */ 547 bp2 = g_clone_bio(bp); 548 if (bp2 != NULL) 549 break; 550 pause("gdstrat", hz / 10); 551 } 552 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 553 bp2->bio_done = g_dev_done; 554 g_trace(G_T_BIO, 555 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 556 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 557 bp2->bio_data, bp2->bio_cmd); 558 g_io_request(bp2, cp); 559 KASSERT(cp->acr || cp->acw, 560 ("g_dev_strategy raced with g_dev_close and lost")); 561 562 } 563 564 /* 565 * g_dev_callback() 566 * 567 * Called by devfs when asynchronous device destruction is completed. 568 * - Mark that we have no attached device any more. 569 * - If there are no outstanding requests, schedule geom destruction. 570 * Otherwise destruction will be scheduled later by g_dev_done(). 571 */ 572 573 static void 574 g_dev_callback(void *arg) 575 { 576 struct g_consumer *cp; 577 struct g_dev_softc *sc; 578 int destroy; 579 580 cp = arg; 581 sc = cp->private; 582 g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name); 583 584 mtx_lock(&sc->sc_mtx); 585 sc->sc_dev = NULL; 586 sc->sc_alias = NULL; 587 destroy = (sc->sc_active == 0); 588 mtx_unlock(&sc->sc_mtx); 589 if (destroy) 590 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 591 } 592 593 /* 594 * g_dev_orphan() 595 * 596 * Called from below when the provider orphaned us. 597 * - Clear any dump settings. 598 * - Request asynchronous device destruction to prevent any more requests 599 * from coming in. The provider is already marked with an error, so 600 * anything which comes in in the interrim will be returned immediately. 601 */ 602 603 static void 604 g_dev_orphan(struct g_consumer *cp) 605 { 606 struct cdev *dev; 607 struct g_dev_softc *sc; 608 609 g_topology_assert(); 610 sc = cp->private; 611 dev = sc->sc_dev; 612 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name); 613 614 /* Reset any dump-area set on this device */ 615 if (dev->si_flags & SI_DUMPDEV) 616 set_dumper(NULL, NULL); 617 618 /* Destroy the struct cdev *so we get no more requests */ 619 destroy_dev_sched_cb(dev, g_dev_callback, cp); 620 } 621 622 DECLARE_GEOM_CLASS(g_dev_class, g_dev); 623