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 <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/malloc.h> 44 #include <sys/kernel.h> 45 #include <sys/conf.h> 46 #include <sys/ctype.h> 47 #include <sys/bio.h> 48 #include <sys/devctl.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/proc.h> 52 #include <sys/errno.h> 53 #include <sys/time.h> 54 #include <sys/disk.h> 55 #include <sys/fcntl.h> 56 #include <sys/limits.h> 57 #include <sys/sysctl.h> 58 #include <geom/geom.h> 59 #include <geom/geom_int.h> 60 #include <machine/stdarg.h> 61 62 struct g_dev_softc { 63 struct mtx sc_mtx; 64 struct cdev *sc_dev; 65 struct cdev *sc_alias; 66 int sc_open; 67 u_int sc_active; 68 #define SC_A_DESTROY (1 << 31) 69 #define SC_A_OPEN (1 << 30) 70 #define SC_A_ACTIVE (SC_A_OPEN - 1) 71 }; 72 73 static d_open_t g_dev_open; 74 static d_close_t g_dev_close; 75 static d_strategy_t g_dev_strategy; 76 static d_ioctl_t g_dev_ioctl; 77 78 static struct cdevsw g_dev_cdevsw = { 79 .d_version = D_VERSION, 80 .d_open = g_dev_open, 81 .d_close = g_dev_close, 82 .d_read = physread, 83 .d_write = physwrite, 84 .d_ioctl = g_dev_ioctl, 85 .d_strategy = g_dev_strategy, 86 .d_name = "g_dev", 87 .d_flags = D_DISK | D_TRACKCLOSE, 88 }; 89 90 static g_init_t g_dev_init; 91 static g_fini_t g_dev_fini; 92 static g_taste_t g_dev_taste; 93 static g_orphan_t g_dev_orphan; 94 static g_attrchanged_t g_dev_attrchanged; 95 static g_resize_t g_dev_resize; 96 97 static struct g_class g_dev_class = { 98 .name = "DEV", 99 .version = G_VERSION, 100 .init = g_dev_init, 101 .fini = g_dev_fini, 102 .taste = g_dev_taste, 103 .orphan = g_dev_orphan, 104 .attrchanged = g_dev_attrchanged, 105 .resize = g_dev_resize 106 }; 107 108 /* 109 * We target 262144 (8 x 32768) sectors by default as this significantly 110 * increases the throughput on commonly used SSD's with a marginal 111 * increase in non-interruptible request latency. 112 */ 113 static uint64_t g_dev_del_max_sectors = 262144; 114 SYSCTL_DECL(_kern_geom); 115 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 116 "GEOM_DEV stuff"); 117 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW, 118 &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single " 119 "delete request sent to the provider. Larger requests are chunked " 120 "so they can be interrupted. (0 = disable chunking)"); 121 122 static char *dumpdev = NULL; 123 static void 124 g_dev_init(struct g_class *mp) 125 { 126 127 dumpdev = kern_getenv("dumpdev"); 128 } 129 130 static void 131 g_dev_fini(struct g_class *mp) 132 { 133 134 freeenv(dumpdev); 135 dumpdev = NULL; 136 } 137 138 static int 139 g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda) 140 { 141 struct g_kerneldump kd; 142 struct g_consumer *cp; 143 int error, len; 144 145 MPASS(dev != NULL && kda != NULL); 146 MPASS(kda->kda_index != KDA_REMOVE); 147 148 cp = dev->si_drv2; 149 len = sizeof(kd); 150 memset(&kd, 0, len); 151 kd.offset = 0; 152 kd.length = OFF_MAX; 153 error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd); 154 if (error != 0) 155 return (error); 156 157 error = dumper_insert(&kd.di, devtoname(dev), kda); 158 if (error == 0) 159 dev->si_flags |= SI_DUMPDEV; 160 161 return (error); 162 } 163 164 static int 165 init_dumpdev(struct cdev *dev) 166 { 167 struct diocskerneldump_arg kda; 168 struct g_consumer *cp; 169 const char *devprefix = _PATH_DEV, *devname; 170 int error; 171 size_t len; 172 173 bzero(&kda, sizeof(kda)); 174 kda.kda_index = KDA_APPEND; 175 176 if (dumpdev == NULL) 177 return (0); 178 179 len = strlen(devprefix); 180 devname = devtoname(dev); 181 if (strcmp(devname, dumpdev) != 0 && 182 (strncmp(dumpdev, devprefix, len) != 0 || 183 strcmp(devname, dumpdev + len) != 0)) 184 return (0); 185 186 cp = (struct g_consumer *)dev->si_drv2; 187 error = g_access(cp, 1, 0, 0); 188 if (error != 0) 189 return (error); 190 191 error = g_dev_setdumpdev(dev, &kda); 192 if (error == 0) { 193 freeenv(dumpdev); 194 dumpdev = NULL; 195 } 196 197 (void)g_access(cp, -1, 0, 0); 198 199 return (error); 200 } 201 202 static void 203 g_dev_destroy(void *arg, int flags __unused) 204 { 205 struct g_consumer *cp; 206 struct g_geom *gp; 207 struct g_dev_softc *sc; 208 char buf[SPECNAMELEN + 6]; 209 210 g_topology_assert(); 211 cp = arg; 212 gp = cp->geom; 213 sc = cp->private; 214 g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name); 215 snprintf(buf, sizeof(buf), "cdev=%s", gp->name); 216 devctl_notify("GEOM", "DEV", "DESTROY", buf); 217 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 218 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 219 g_detach(cp); 220 g_destroy_consumer(cp); 221 g_destroy_geom(gp); 222 mtx_destroy(&sc->sc_mtx); 223 g_free(sc); 224 } 225 226 void 227 g_dev_print(void) 228 { 229 struct g_geom *gp; 230 char const *p = ""; 231 232 LIST_FOREACH(gp, &g_dev_class.geom, geom) { 233 printf("%s%s", p, gp->name); 234 p = " "; 235 } 236 printf("\n"); 237 } 238 239 static void 240 g_dev_set_physpath(struct g_consumer *cp) 241 { 242 struct g_dev_softc *sc; 243 char *physpath; 244 int error, physpath_len; 245 246 if (g_access(cp, 1, 0, 0) != 0) 247 return; 248 249 sc = cp->private; 250 physpath_len = MAXPATHLEN; 251 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO); 252 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath); 253 g_access(cp, -1, 0, 0); 254 if (error == 0 && strlen(physpath) != 0) { 255 struct cdev *dev, *old_alias_dev; 256 struct cdev **alias_devp; 257 258 dev = sc->sc_dev; 259 old_alias_dev = sc->sc_alias; 260 alias_devp = (struct cdev **)&sc->sc_alias; 261 make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev, 262 old_alias_dev, physpath); 263 } else if (sc->sc_alias) { 264 destroy_dev((struct cdev *)sc->sc_alias); 265 sc->sc_alias = NULL; 266 } 267 g_free(physpath); 268 } 269 270 static void 271 g_dev_set_media(struct g_consumer *cp) 272 { 273 struct g_dev_softc *sc; 274 struct cdev *dev; 275 char buf[SPECNAMELEN + 6]; 276 277 sc = cp->private; 278 dev = sc->sc_dev; 279 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name); 280 devctl_notify("DEVFS", "CDEV", "MEDIACHANGE", buf); 281 devctl_notify("GEOM", "DEV", "MEDIACHANGE", buf); 282 dev = sc->sc_alias; 283 if (dev != NULL) { 284 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name); 285 devctl_notify("DEVFS", "CDEV", "MEDIACHANGE", buf); 286 devctl_notify("GEOM", "DEV", "MEDIACHANGE", buf); 287 } 288 } 289 290 static void 291 g_dev_attrchanged(struct g_consumer *cp, const char *attr) 292 { 293 294 if (strcmp(attr, "GEOM::media") == 0) { 295 g_dev_set_media(cp); 296 return; 297 } 298 299 if (strcmp(attr, "GEOM::physpath") == 0) { 300 g_dev_set_physpath(cp); 301 return; 302 } 303 } 304 305 static void 306 g_dev_resize(struct g_consumer *cp) 307 { 308 char buf[SPECNAMELEN + 6]; 309 310 snprintf(buf, sizeof(buf), "cdev=%s", cp->provider->name); 311 devctl_notify("GEOM", "DEV", "SIZECHANGE", buf); 312 } 313 314 struct g_provider * 315 g_dev_getprovider(struct cdev *dev) 316 { 317 struct g_consumer *cp; 318 319 g_topology_assert(); 320 if (dev == NULL) 321 return (NULL); 322 if (dev->si_devsw != &g_dev_cdevsw) 323 return (NULL); 324 cp = dev->si_drv2; 325 return (cp->provider); 326 } 327 328 static struct g_geom * 329 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 330 { 331 struct g_geom *gp; 332 struct g_geom_alias *gap; 333 struct g_consumer *cp; 334 struct g_dev_softc *sc; 335 int error; 336 struct cdev *dev, *adev; 337 char buf[SPECNAMELEN + 6]; 338 struct make_dev_args args; 339 340 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 341 g_topology_assert(); 342 gp = g_new_geomf(mp, "%s", pp->name); 343 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 344 mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF); 345 cp = g_new_consumer(gp); 346 cp->private = sc; 347 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 348 error = g_attach(cp, pp); 349 if (error != 0) { 350 printf("%s: g_dev_taste(%s) failed to g_attach, error=%d\n", 351 __func__, pp->name, error); 352 g_destroy_consumer(cp); 353 g_destroy_geom(gp); 354 mtx_destroy(&sc->sc_mtx); 355 g_free(sc); 356 return (NULL); 357 } 358 make_dev_args_init(&args); 359 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 360 args.mda_devsw = &g_dev_cdevsw; 361 args.mda_cr = NULL; 362 args.mda_uid = UID_ROOT; 363 args.mda_gid = GID_OPERATOR; 364 args.mda_mode = 0640; 365 args.mda_si_drv1 = sc; 366 args.mda_si_drv2 = cp; 367 error = make_dev_s(&args, &sc->sc_dev, "%s", gp->name); 368 if (error != 0) { 369 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n", 370 __func__, gp->name, error); 371 g_detach(cp); 372 g_destroy_consumer(cp); 373 g_destroy_geom(gp); 374 mtx_destroy(&sc->sc_mtx); 375 g_free(sc); 376 return (NULL); 377 } 378 dev = sc->sc_dev; 379 dev->si_flags |= SI_UNMAPPED; 380 dev->si_iosize_max = MAXPHYS; 381 error = init_dumpdev(dev); 382 if (error != 0) 383 printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n", 384 __func__, gp->name, error); 385 386 g_dev_attrchanged(cp, "GEOM::physpath"); 387 snprintf(buf, sizeof(buf), "cdev=%s", gp->name); 388 devctl_notify("GEOM", "DEV", "CREATE", buf); 389 /* 390 * Now add all the aliases for this drive 391 */ 392 LIST_FOREACH(gap, &pp->aliases, ga_next) { 393 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev, 394 "%s", gap->ga_alias); 395 if (error) { 396 printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n", 397 __func__, gap->ga_alias, error); 398 continue; 399 } 400 snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias); 401 devctl_notify("GEOM", "DEV", "CREATE", buf); 402 } 403 404 return (gp); 405 } 406 407 static int 408 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 409 { 410 struct g_consumer *cp; 411 struct g_dev_softc *sc; 412 int error, r, w, e; 413 414 cp = dev->si_drv2; 415 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 416 cp->geom->name, flags, fmt, td); 417 418 r = flags & FREAD ? 1 : 0; 419 w = flags & FWRITE ? 1 : 0; 420 #ifdef notyet 421 e = flags & O_EXCL ? 1 : 0; 422 #else 423 e = 0; 424 #endif 425 426 /* 427 * This happens on attempt to open a device node with O_EXEC. 428 */ 429 if (r + w + e == 0) 430 return (EINVAL); 431 432 if (w) { 433 /* 434 * When running in very secure mode, do not allow 435 * opens for writing of any disks. 436 */ 437 error = securelevel_ge(td->td_ucred, 2); 438 if (error) 439 return (error); 440 } 441 g_topology_lock(); 442 error = g_access(cp, r, w, e); 443 g_topology_unlock(); 444 if (error == 0) { 445 sc = dev->si_drv1; 446 mtx_lock(&sc->sc_mtx); 447 if (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0) 448 wakeup(&sc->sc_active); 449 sc->sc_open += r + w + e; 450 if (sc->sc_open == 0) 451 atomic_clear_int(&sc->sc_active, SC_A_OPEN); 452 else 453 atomic_set_int(&sc->sc_active, SC_A_OPEN); 454 mtx_unlock(&sc->sc_mtx); 455 } 456 return (error); 457 } 458 459 static int 460 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 461 { 462 struct g_consumer *cp; 463 struct g_dev_softc *sc; 464 int error, r, w, e; 465 466 cp = dev->si_drv2; 467 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 468 cp->geom->name, flags, fmt, td); 469 470 r = flags & FREAD ? -1 : 0; 471 w = flags & FWRITE ? -1 : 0; 472 #ifdef notyet 473 e = flags & O_EXCL ? -1 : 0; 474 #else 475 e = 0; 476 #endif 477 478 /* 479 * The vgonel(9) - caused by eg. forced unmount of devfs - calls 480 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags, 481 * which would result in zero deltas, which in turn would cause 482 * panic in g_access(9). 483 * 484 * Note that we cannot zero the counters (ie. do "r = cp->acr" 485 * etc) instead, because the consumer might be opened in another 486 * devfs instance. 487 */ 488 if (r + w + e == 0) 489 return (EINVAL); 490 491 sc = dev->si_drv1; 492 mtx_lock(&sc->sc_mtx); 493 sc->sc_open += r + w + e; 494 if (sc->sc_open == 0) 495 atomic_clear_int(&sc->sc_active, SC_A_OPEN); 496 else 497 atomic_set_int(&sc->sc_active, SC_A_OPEN); 498 while (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0) 499 msleep(&sc->sc_active, &sc->sc_mtx, 0, "g_dev_close", hz / 10); 500 mtx_unlock(&sc->sc_mtx); 501 g_topology_lock(); 502 error = g_access(cp, r, w, e); 503 g_topology_unlock(); 504 return (error); 505 } 506 507 static int 508 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 509 { 510 struct g_consumer *cp; 511 struct g_provider *pp; 512 off_t offset, length, chunk, odd; 513 int i, error; 514 #ifdef COMPAT_FREEBSD12 515 struct diocskerneldump_arg kda_copy; 516 #endif 517 518 cp = dev->si_drv2; 519 pp = cp->provider; 520 521 /* If consumer or provider is dying, don't disturb. */ 522 if (cp->flags & G_CF_ORPHAN) 523 return (ENXIO); 524 if (pp->error) 525 return (pp->error); 526 527 error = 0; 528 KASSERT(cp->acr || cp->acw, 529 ("Consumer with zero access count in g_dev_ioctl")); 530 531 i = IOCPARM_LEN(cmd); 532 switch (cmd) { 533 case DIOCGSECTORSIZE: 534 *(u_int *)data = pp->sectorsize; 535 if (*(u_int *)data == 0) 536 error = ENOENT; 537 break; 538 case DIOCGMEDIASIZE: 539 *(off_t *)data = pp->mediasize; 540 if (*(off_t *)data == 0) 541 error = ENOENT; 542 break; 543 case DIOCGFWSECTORS: 544 error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 545 if (error == 0 && *(u_int *)data == 0) 546 error = ENOENT; 547 break; 548 case DIOCGFWHEADS: 549 error = g_io_getattr("GEOM::fwheads", cp, &i, data); 550 if (error == 0 && *(u_int *)data == 0) 551 error = ENOENT; 552 break; 553 case DIOCGFRONTSTUFF: 554 error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 555 break; 556 #ifdef COMPAT_FREEBSD11 557 case DIOCSKERNELDUMP_FREEBSD11: 558 { 559 struct diocskerneldump_arg kda; 560 561 gone_in(13, "FreeBSD 11.x ABI compat"); 562 563 bzero(&kda, sizeof(kda)); 564 kda.kda_encryption = KERNELDUMP_ENC_NONE; 565 kda.kda_index = (*(u_int *)data ? 0 : KDA_REMOVE_ALL); 566 if (kda.kda_index == KDA_REMOVE_ALL) 567 error = dumper_remove(devtoname(dev), &kda); 568 else 569 error = g_dev_setdumpdev(dev, &kda); 570 break; 571 } 572 #endif 573 #ifdef COMPAT_FREEBSD12 574 case DIOCSKERNELDUMP_FREEBSD12: 575 { 576 struct diocskerneldump_arg_freebsd12 *kda12; 577 578 gone_in(14, "FreeBSD 12.x ABI compat"); 579 580 kda12 = (void *)data; 581 memcpy(&kda_copy, kda12, sizeof(kda_copy)); 582 kda_copy.kda_index = (kda12->kda12_enable ? 583 0 : KDA_REMOVE_ALL); 584 585 explicit_bzero(kda12, sizeof(*kda12)); 586 /* Kludge to pass kda_copy to kda in fallthrough. */ 587 data = (void *)&kda_copy; 588 } 589 /* FALLTHROUGH */ 590 #endif 591 case DIOCSKERNELDUMP: 592 { 593 struct diocskerneldump_arg *kda; 594 uint8_t *encryptedkey; 595 596 kda = (struct diocskerneldump_arg *)data; 597 if (kda->kda_index == KDA_REMOVE_ALL || 598 kda->kda_index == KDA_REMOVE_DEV || 599 kda->kda_index == KDA_REMOVE) { 600 error = dumper_remove(devtoname(dev), kda); 601 explicit_bzero(kda, sizeof(*kda)); 602 break; 603 } 604 605 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) { 606 if (kda->kda_encryptedkeysize == 0 || 607 kda->kda_encryptedkeysize > 608 KERNELDUMP_ENCKEY_MAX_SIZE) { 609 explicit_bzero(kda, sizeof(*kda)); 610 return (EINVAL); 611 } 612 encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP, 613 M_WAITOK); 614 error = copyin(kda->kda_encryptedkey, encryptedkey, 615 kda->kda_encryptedkeysize); 616 } else { 617 encryptedkey = NULL; 618 } 619 if (error == 0) { 620 kda->kda_encryptedkey = encryptedkey; 621 error = g_dev_setdumpdev(dev, kda); 622 } 623 zfree(encryptedkey, M_TEMP); 624 explicit_bzero(kda, sizeof(*kda)); 625 break; 626 } 627 case DIOCGFLUSH: 628 error = g_io_flush(cp); 629 break; 630 case DIOCGDELETE: 631 offset = ((off_t *)data)[0]; 632 length = ((off_t *)data)[1]; 633 if ((offset % pp->sectorsize) != 0 || 634 (length % pp->sectorsize) != 0 || length <= 0) { 635 printf("%s: offset=%jd length=%jd\n", __func__, offset, 636 length); 637 error = EINVAL; 638 break; 639 } 640 if ((pp->mediasize > 0) && (offset >= pp->mediasize)) { 641 /* 642 * Catch out-of-bounds requests here. The problem is 643 * that due to historical GEOM I/O implementation 644 * peculatities, g_delete_data() would always return 645 * success for requests starting just the next byte 646 * after providers media boundary. Condition check on 647 * non-zero media size, since that condition would 648 * (most likely) cause ENXIO instead. 649 */ 650 error = EIO; 651 break; 652 } 653 while (length > 0) { 654 chunk = length; 655 if (g_dev_del_max_sectors != 0 && 656 chunk > g_dev_del_max_sectors * pp->sectorsize) { 657 chunk = g_dev_del_max_sectors * pp->sectorsize; 658 if (pp->stripesize > 0) { 659 odd = (offset + chunk + 660 pp->stripeoffset) % pp->stripesize; 661 if (chunk > odd) 662 chunk -= odd; 663 } 664 } 665 error = g_delete_data(cp, offset, chunk); 666 length -= chunk; 667 offset += chunk; 668 if (error) 669 break; 670 /* 671 * Since the request size can be large, the service 672 * time can be is likewise. We make this ioctl 673 * interruptible by checking for signals for each bio. 674 */ 675 if (SIGPENDING(td)) 676 break; 677 } 678 break; 679 case DIOCGIDENT: 680 error = g_io_getattr("GEOM::ident", cp, &i, data); 681 break; 682 case DIOCGPROVIDERNAME: 683 strlcpy(data, pp->name, i); 684 break; 685 case DIOCGSTRIPESIZE: 686 *(off_t *)data = pp->stripesize; 687 break; 688 case DIOCGSTRIPEOFFSET: 689 *(off_t *)data = pp->stripeoffset; 690 break; 691 case DIOCGPHYSPATH: 692 error = g_io_getattr("GEOM::physpath", cp, &i, data); 693 if (error == 0 && *(char *)data == '\0') 694 error = ENOENT; 695 break; 696 case DIOCGATTR: { 697 struct diocgattr_arg *arg = (struct diocgattr_arg *)data; 698 699 if (arg->len > sizeof(arg->value)) { 700 error = EINVAL; 701 break; 702 } 703 error = g_io_getattr(arg->name, cp, &arg->len, &arg->value); 704 break; 705 } 706 case DIOCZONECMD: { 707 struct disk_zone_args *zone_args =(struct disk_zone_args *)data; 708 struct disk_zone_rep_entry *new_entries, *old_entries; 709 struct disk_zone_report *rep; 710 size_t alloc_size; 711 712 old_entries = NULL; 713 new_entries = NULL; 714 rep = NULL; 715 alloc_size = 0; 716 717 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) { 718 rep = &zone_args->zone_params.report; 719 #define MAXENTRIES (MAXPHYS / sizeof(struct disk_zone_rep_entry)) 720 if (rep->entries_allocated > MAXENTRIES) 721 rep->entries_allocated = MAXENTRIES; 722 alloc_size = rep->entries_allocated * 723 sizeof(struct disk_zone_rep_entry); 724 if (alloc_size != 0) 725 new_entries = g_malloc(alloc_size, 726 M_WAITOK| M_ZERO); 727 old_entries = rep->entries; 728 rep->entries = new_entries; 729 } 730 error = g_io_zonecmd(zone_args, cp); 731 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES && 732 alloc_size != 0 && error == 0) 733 error = copyout(new_entries, old_entries, alloc_size); 734 if (old_entries != NULL && rep != NULL) 735 rep->entries = old_entries; 736 if (new_entries != NULL) 737 g_free(new_entries); 738 break; 739 } 740 default: 741 if (pp->geom->ioctl != NULL) { 742 error = pp->geom->ioctl(pp, cmd, data, fflag, td); 743 } else { 744 error = ENOIOCTL; 745 } 746 } 747 748 return (error); 749 } 750 751 static void 752 g_dev_done(struct bio *bp2) 753 { 754 struct g_consumer *cp; 755 struct g_dev_softc *sc; 756 struct bio *bp; 757 int active; 758 759 cp = bp2->bio_from; 760 sc = cp->private; 761 bp = bp2->bio_parent; 762 bp->bio_error = bp2->bio_error; 763 bp->bio_completed = bp2->bio_completed; 764 bp->bio_resid = bp->bio_length - bp2->bio_completed; 765 if (bp2->bio_cmd == BIO_ZONE) 766 bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone)); 767 768 if (bp2->bio_error != 0) { 769 g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 770 bp2, bp2->bio_error); 771 bp->bio_flags |= BIO_ERROR; 772 } else { 773 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 774 bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed); 775 } 776 g_destroy_bio(bp2); 777 active = atomic_fetchadd_int(&sc->sc_active, -1) - 1; 778 if ((active & SC_A_ACTIVE) == 0) { 779 if ((active & SC_A_OPEN) == 0) 780 wakeup(&sc->sc_active); 781 if (active & SC_A_DESTROY) 782 g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL); 783 } 784 biodone(bp); 785 } 786 787 static void 788 g_dev_strategy(struct bio *bp) 789 { 790 struct g_consumer *cp; 791 struct bio *bp2; 792 struct cdev *dev; 793 struct g_dev_softc *sc; 794 795 KASSERT(bp->bio_cmd == BIO_READ || 796 bp->bio_cmd == BIO_WRITE || 797 bp->bio_cmd == BIO_DELETE || 798 bp->bio_cmd == BIO_FLUSH || 799 bp->bio_cmd == BIO_ZONE, 800 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 801 dev = bp->bio_dev; 802 cp = dev->si_drv2; 803 KASSERT(cp->acr || cp->acw, 804 ("Consumer with zero access count in g_dev_strategy")); 805 biotrack(bp, __func__); 806 #ifdef INVARIANTS 807 if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 808 (bp->bio_bcount % cp->provider->sectorsize) != 0) { 809 bp->bio_resid = bp->bio_bcount; 810 biofinish(bp, NULL, EINVAL); 811 return; 812 } 813 #endif 814 sc = dev->si_drv1; 815 KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy")); 816 atomic_add_int(&sc->sc_active, 1); 817 818 for (;;) { 819 /* 820 * XXX: This is not an ideal solution, but I believe it to 821 * XXX: deadlock safely, all things considered. 822 */ 823 bp2 = g_clone_bio(bp); 824 if (bp2 != NULL) 825 break; 826 pause("gdstrat", hz / 10); 827 } 828 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 829 bp2->bio_done = g_dev_done; 830 g_trace(G_T_BIO, 831 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 832 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 833 bp2->bio_data, bp2->bio_cmd); 834 g_io_request(bp2, cp); 835 KASSERT(cp->acr || cp->acw, 836 ("g_dev_strategy raced with g_dev_close and lost")); 837 838 } 839 840 /* 841 * g_dev_callback() 842 * 843 * Called by devfs when asynchronous device destruction is completed. 844 * - Mark that we have no attached device any more. 845 * - If there are no outstanding requests, schedule geom destruction. 846 * Otherwise destruction will be scheduled later by g_dev_done(). 847 */ 848 849 static void 850 g_dev_callback(void *arg) 851 { 852 struct g_consumer *cp; 853 struct g_dev_softc *sc; 854 int active; 855 856 cp = arg; 857 sc = cp->private; 858 g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name); 859 860 sc->sc_dev = NULL; 861 sc->sc_alias = NULL; 862 active = atomic_fetchadd_int(&sc->sc_active, SC_A_DESTROY); 863 if ((active & SC_A_ACTIVE) == 0) 864 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL); 865 } 866 867 /* 868 * g_dev_orphan() 869 * 870 * Called from below when the provider orphaned us. 871 * - Clear any dump settings. 872 * - Request asynchronous device destruction to prevent any more requests 873 * from coming in. The provider is already marked with an error, so 874 * anything which comes in the interim will be returned immediately. 875 */ 876 877 static void 878 g_dev_orphan(struct g_consumer *cp) 879 { 880 struct cdev *dev; 881 struct g_dev_softc *sc; 882 883 g_topology_assert(); 884 sc = cp->private; 885 dev = sc->sc_dev; 886 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name); 887 888 /* Reset any dump-area set on this device */ 889 if (dev->si_flags & SI_DUMPDEV) { 890 struct diocskerneldump_arg kda; 891 892 bzero(&kda, sizeof(kda)); 893 kda.kda_index = KDA_REMOVE_DEV; 894 (void)dumper_remove(devtoname(dev), &kda); 895 } 896 897 /* Destroy the struct cdev *so we get no more requests */ 898 delist_dev(dev); 899 destroy_dev_sched_cb(dev, g_dev_callback, cp); 900 } 901 902 DECLARE_GEOM_CLASS(g_dev_class, g_dev); 903