1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * Copyright (c) 2009-2010 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Pawel Jakub Dawidek 9 * under sponsorship from the FreeBSD Foundation. 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/bio.h> 39 #include <sys/conf.h> 40 #include <sys/kernel.h> 41 #include <sys/kthread.h> 42 #include <sys/fcntl.h> 43 #include <sys/linker.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/limits.h> 49 #include <sys/queue.h> 50 #include <sys/sbuf.h> 51 #include <sys/sysctl.h> 52 #include <sys/signalvar.h> 53 #include <sys/time.h> 54 #include <machine/atomic.h> 55 56 #include <geom/geom.h> 57 #include <geom/geom_dbg.h> 58 #include <geom/gate/g_gate.h> 59 60 FEATURE(geom_gate, "GEOM Gate module"); 61 62 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data"); 63 64 SYSCTL_DECL(_kern_geom); 65 static SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, 66 "GEOM_GATE configuration"); 67 static int g_gate_debug = 0; 68 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RWTUN, &g_gate_debug, 0, 69 "Debug level"); 70 static u_int g_gate_maxunits = 256; 71 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN, 72 &g_gate_maxunits, 0, "Maximum number of ggate devices"); 73 74 struct g_class g_gate_class = { 75 .name = G_GATE_CLASS_NAME, 76 .version = G_VERSION, 77 }; 78 79 static struct cdev *status_dev; 80 static d_ioctl_t g_gate_ioctl; 81 static struct cdevsw g_gate_cdevsw = { 82 .d_version = D_VERSION, 83 .d_ioctl = g_gate_ioctl, 84 .d_name = G_GATE_CTL_NAME 85 }; 86 87 88 static struct g_gate_softc **g_gate_units; 89 static u_int g_gate_nunits; 90 static struct mtx g_gate_units_lock; 91 92 static void 93 g_gate_detach(void *arg, int flags __unused) 94 { 95 struct g_consumer *cp = arg; 96 97 g_topology_assert(); 98 G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.", 99 cp->provider->name); 100 (void)g_access(cp, -1, 0, 0); 101 g_detach(cp); 102 g_destroy_consumer(cp); 103 } 104 105 static int 106 g_gate_destroy(struct g_gate_softc *sc, boolean_t force) 107 { 108 struct bio_queue_head queue; 109 struct g_provider *pp; 110 struct g_consumer *cp; 111 struct g_geom *gp; 112 struct bio *bp; 113 114 g_topology_assert(); 115 mtx_assert(&g_gate_units_lock, MA_OWNED); 116 pp = sc->sc_provider; 117 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 118 mtx_unlock(&g_gate_units_lock); 119 return (EBUSY); 120 } 121 mtx_unlock(&g_gate_units_lock); 122 mtx_lock(&sc->sc_queue_mtx); 123 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) 124 sc->sc_flags |= G_GATE_FLAG_DESTROY; 125 wakeup(sc); 126 mtx_unlock(&sc->sc_queue_mtx); 127 gp = pp->geom; 128 g_wither_provider(pp, ENXIO); 129 callout_drain(&sc->sc_callout); 130 bioq_init(&queue); 131 mtx_lock(&sc->sc_queue_mtx); 132 while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) { 133 sc->sc_queue_count--; 134 bioq_insert_tail(&queue, bp); 135 } 136 while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) { 137 sc->sc_queue_count--; 138 bioq_insert_tail(&queue, bp); 139 } 140 mtx_unlock(&sc->sc_queue_mtx); 141 g_topology_unlock(); 142 while ((bp = bioq_takefirst(&queue)) != NULL) { 143 G_GATE_LOGREQ(1, bp, "Request canceled."); 144 g_io_deliver(bp, ENXIO); 145 } 146 mtx_lock(&g_gate_units_lock); 147 /* One reference is ours. */ 148 sc->sc_ref--; 149 while (sc->sc_ref > 0) 150 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0); 151 g_gate_units[sc->sc_unit] = NULL; 152 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 153 g_gate_nunits--; 154 mtx_unlock(&g_gate_units_lock); 155 mtx_destroy(&sc->sc_queue_mtx); 156 mtx_destroy(&sc->sc_read_mtx); 157 g_topology_lock(); 158 if ((cp = sc->sc_readcons) != NULL) { 159 sc->sc_readcons = NULL; 160 (void)g_access(cp, -1, 0, 0); 161 g_detach(cp); 162 g_destroy_consumer(cp); 163 } 164 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name); 165 gp->softc = NULL; 166 g_wither_geom(gp, ENXIO); 167 sc->sc_provider = NULL; 168 free(sc, M_GATE); 169 return (0); 170 } 171 172 static int 173 g_gate_access(struct g_provider *pp, int dr, int dw, int de) 174 { 175 struct g_gate_softc *sc; 176 177 if (dr <= 0 && dw <= 0 && de <= 0) 178 return (0); 179 sc = pp->geom->softc; 180 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 181 return (ENXIO); 182 /* XXX: Hack to allow read-only mounts. */ 183 #if 0 184 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0) 185 return (EPERM); 186 #endif 187 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0) 188 return (EPERM); 189 return (0); 190 } 191 192 static void 193 g_gate_queue_io(struct bio *bp) 194 { 195 struct g_gate_softc *sc; 196 197 sc = bp->bio_to->geom->softc; 198 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 199 g_io_deliver(bp, ENXIO); 200 return; 201 } 202 203 mtx_lock(&sc->sc_queue_mtx); 204 205 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) { 206 mtx_unlock(&sc->sc_queue_mtx); 207 G_GATE_LOGREQ(1, bp, "Queue full, request canceled."); 208 g_io_deliver(bp, ENOMEM); 209 return; 210 } 211 212 bp->bio_driver1 = (void *)sc->sc_seq; 213 sc->sc_seq++; 214 sc->sc_queue_count++; 215 216 bioq_insert_tail(&sc->sc_inqueue, bp); 217 wakeup(sc); 218 219 mtx_unlock(&sc->sc_queue_mtx); 220 } 221 222 static void 223 g_gate_done(struct bio *cbp) 224 { 225 struct g_gate_softc *sc; 226 struct bio *pbp; 227 struct g_consumer *cp; 228 229 cp = cbp->bio_from; 230 pbp = cbp->bio_parent; 231 if (cbp->bio_error == 0) { 232 pbp->bio_completed = cbp->bio_completed; 233 g_destroy_bio(cbp); 234 pbp->bio_inbed++; 235 g_io_deliver(pbp, 0); 236 } else { 237 /* If direct read failed, pass it through userland daemon. */ 238 g_destroy_bio(cbp); 239 pbp->bio_children--; 240 g_gate_queue_io(pbp); 241 } 242 243 sc = cp->geom->softc; 244 mtx_lock(&sc->sc_read_mtx); 245 if (--cp->index == 0 && sc->sc_readcons != cp) 246 g_post_event(g_gate_detach, cp, M_NOWAIT, NULL); 247 mtx_unlock(&sc->sc_read_mtx); 248 } 249 250 static void 251 g_gate_start(struct bio *pbp) 252 { 253 struct g_gate_softc *sc; 254 struct g_consumer *cp; 255 struct bio *cbp; 256 257 sc = pbp->bio_to->geom->softc; 258 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 259 g_io_deliver(pbp, ENXIO); 260 return; 261 } 262 G_GATE_LOGREQ(2, pbp, "Request received."); 263 switch (pbp->bio_cmd) { 264 case BIO_READ: 265 if (sc->sc_readcons == NULL) 266 break; 267 cbp = g_clone_bio(pbp); 268 if (cbp == NULL) { 269 g_io_deliver(pbp, ENOMEM); 270 return; 271 } 272 mtx_lock(&sc->sc_read_mtx); 273 if ((cp = sc->sc_readcons) == NULL) { 274 mtx_unlock(&sc->sc_read_mtx); 275 g_destroy_bio(cbp); 276 pbp->bio_children--; 277 break; 278 } 279 cp->index++; 280 cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset; 281 mtx_unlock(&sc->sc_read_mtx); 282 cbp->bio_done = g_gate_done; 283 g_io_request(cbp, cp); 284 return; 285 case BIO_DELETE: 286 case BIO_WRITE: 287 case BIO_FLUSH: 288 /* XXX: Hack to allow read-only mounts. */ 289 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 290 g_io_deliver(pbp, EPERM); 291 return; 292 } 293 break; 294 case BIO_GETATTR: 295 default: 296 G_GATE_LOGREQ(2, pbp, "Ignoring request."); 297 g_io_deliver(pbp, EOPNOTSUPP); 298 return; 299 } 300 301 g_gate_queue_io(pbp); 302 } 303 304 static struct g_gate_softc * 305 g_gate_hold(int unit, const char *name) 306 { 307 struct g_gate_softc *sc = NULL; 308 309 mtx_lock(&g_gate_units_lock); 310 if (unit >= 0 && unit < g_gate_maxunits) 311 sc = g_gate_units[unit]; 312 else if (unit == G_GATE_NAME_GIVEN) { 313 KASSERT(name != NULL, ("name is NULL")); 314 for (unit = 0; unit < g_gate_maxunits; unit++) { 315 if (g_gate_units[unit] == NULL) 316 continue; 317 if (strcmp(name, 318 g_gate_units[unit]->sc_provider->name) != 0) { 319 continue; 320 } 321 sc = g_gate_units[unit]; 322 break; 323 } 324 } 325 if (sc != NULL) 326 sc->sc_ref++; 327 mtx_unlock(&g_gate_units_lock); 328 return (sc); 329 } 330 331 static void 332 g_gate_release(struct g_gate_softc *sc) 333 { 334 335 g_topology_assert_not(); 336 mtx_lock(&g_gate_units_lock); 337 sc->sc_ref--; 338 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name)); 339 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 340 wakeup(&sc->sc_ref); 341 mtx_unlock(&g_gate_units_lock); 342 } 343 344 static int 345 g_gate_getunit(int unit, int *errorp) 346 { 347 348 mtx_assert(&g_gate_units_lock, MA_OWNED); 349 if (unit >= 0) { 350 if (unit >= g_gate_maxunits) 351 *errorp = EINVAL; 352 else if (g_gate_units[unit] == NULL) 353 return (unit); 354 else 355 *errorp = EEXIST; 356 } else { 357 for (unit = 0; unit < g_gate_maxunits; unit++) { 358 if (g_gate_units[unit] == NULL) 359 return (unit); 360 } 361 *errorp = ENFILE; 362 } 363 return (-1); 364 } 365 366 static void 367 g_gate_guard(void *arg) 368 { 369 struct bio_queue_head queue; 370 struct g_gate_softc *sc; 371 struct bintime curtime; 372 struct bio *bp, *bp2; 373 374 sc = arg; 375 binuptime(&curtime); 376 g_gate_hold(sc->sc_unit, NULL); 377 bioq_init(&queue); 378 mtx_lock(&sc->sc_queue_mtx); 379 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) { 380 if (curtime.sec - bp->bio_t0.sec < 5) 381 continue; 382 bioq_remove(&sc->sc_inqueue, bp); 383 sc->sc_queue_count--; 384 bioq_insert_tail(&queue, bp); 385 } 386 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) { 387 if (curtime.sec - bp->bio_t0.sec < 5) 388 continue; 389 bioq_remove(&sc->sc_outqueue, bp); 390 sc->sc_queue_count--; 391 bioq_insert_tail(&queue, bp); 392 } 393 mtx_unlock(&sc->sc_queue_mtx); 394 while ((bp = bioq_takefirst(&queue)) != NULL) { 395 G_GATE_LOGREQ(1, bp, "Request timeout."); 396 g_io_deliver(bp, EIO); 397 } 398 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) { 399 callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 400 g_gate_guard, sc); 401 } 402 g_gate_release(sc); 403 } 404 405 static void 406 g_gate_orphan(struct g_consumer *cp) 407 { 408 struct g_gate_softc *sc; 409 struct g_geom *gp; 410 int done; 411 412 g_topology_assert(); 413 gp = cp->geom; 414 sc = gp->softc; 415 mtx_lock(&sc->sc_read_mtx); 416 if (sc->sc_readcons == cp) 417 sc->sc_readcons = NULL; 418 done = (cp->index == 0); 419 mtx_unlock(&sc->sc_read_mtx); 420 if (done) 421 g_gate_detach(cp, 0); 422 } 423 424 static void 425 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 426 struct g_consumer *cp, struct g_provider *pp) 427 { 428 struct g_gate_softc *sc; 429 430 sc = gp->softc; 431 if (sc == NULL || pp != NULL || cp != NULL) 432 return; 433 sc = g_gate_hold(sc->sc_unit, NULL); 434 if (sc == NULL) 435 return; 436 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 437 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only"); 438 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) { 439 sbuf_printf(sb, "%s<access>%s</access>\n", indent, 440 "write-only"); 441 } else { 442 sbuf_printf(sb, "%s<access>%s</access>\n", indent, 443 "read-write"); 444 } 445 if (sc->sc_readcons != NULL) { 446 sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n", 447 indent, (intmax_t)sc->sc_readoffset); 448 sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n", 449 indent, sc->sc_readcons->provider->name); 450 } 451 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout); 452 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info); 453 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent, 454 sc->sc_queue_count); 455 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent, 456 sc->sc_queue_size); 457 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref); 458 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit); 459 g_topology_unlock(); 460 g_gate_release(sc); 461 g_topology_lock(); 462 } 463 464 static int 465 g_gate_create(struct g_gate_ctl_create *ggio) 466 { 467 struct g_gate_softc *sc; 468 struct g_geom *gp; 469 struct g_provider *pp, *ropp; 470 struct g_consumer *cp; 471 char name[NAME_MAX]; 472 int error = 0, unit; 473 474 if (ggio->gctl_mediasize <= 0) { 475 G_GATE_DEBUG(1, "Invalid media size."); 476 return (EINVAL); 477 } 478 if (ggio->gctl_sectorsize <= 0) { 479 G_GATE_DEBUG(1, "Invalid sector size."); 480 return (EINVAL); 481 } 482 if (!powerof2(ggio->gctl_sectorsize)) { 483 G_GATE_DEBUG(1, "Invalid sector size."); 484 return (EINVAL); 485 } 486 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) { 487 G_GATE_DEBUG(1, "Invalid media size."); 488 return (EINVAL); 489 } 490 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 && 491 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) { 492 G_GATE_DEBUG(1, "Invalid flags."); 493 return (EINVAL); 494 } 495 if (ggio->gctl_unit != G_GATE_UNIT_AUTO && 496 ggio->gctl_unit != G_GATE_NAME_GIVEN && 497 ggio->gctl_unit < 0) { 498 G_GATE_DEBUG(1, "Invalid unit number."); 499 return (EINVAL); 500 } 501 if (ggio->gctl_unit == G_GATE_NAME_GIVEN && 502 ggio->gctl_name[0] == '\0') { 503 G_GATE_DEBUG(1, "No device name."); 504 return (EINVAL); 505 } 506 507 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO); 508 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS); 509 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 510 sc->sc_seq = 1; 511 bioq_init(&sc->sc_inqueue); 512 bioq_init(&sc->sc_outqueue); 513 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF); 514 mtx_init(&sc->sc_read_mtx, "gg:read", NULL, MTX_DEF); 515 sc->sc_queue_count = 0; 516 sc->sc_queue_size = ggio->gctl_maxcount; 517 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE) 518 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE; 519 sc->sc_timeout = ggio->gctl_timeout; 520 callout_init(&sc->sc_callout, 1); 521 522 mtx_lock(&g_gate_units_lock); 523 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error); 524 if (sc->sc_unit < 0) 525 goto fail1; 526 if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 527 snprintf(name, sizeof(name), "%s", ggio->gctl_name); 528 else { 529 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME, 530 sc->sc_unit); 531 } 532 /* Check for name collision. */ 533 for (unit = 0; unit < g_gate_maxunits; unit++) { 534 if (g_gate_units[unit] == NULL) 535 continue; 536 if (strcmp(name, g_gate_units[unit]->sc_name) != 0) 537 continue; 538 error = EEXIST; 539 goto fail1; 540 } 541 sc->sc_name = name; 542 g_gate_units[sc->sc_unit] = sc; 543 g_gate_nunits++; 544 mtx_unlock(&g_gate_units_lock); 545 546 g_topology_lock(); 547 548 if (ggio->gctl_readprov[0] == '\0') { 549 ropp = NULL; 550 } else { 551 ropp = g_provider_by_name(ggio->gctl_readprov); 552 if (ropp == NULL) { 553 G_GATE_DEBUG(1, "Provider %s doesn't exist.", 554 ggio->gctl_readprov); 555 error = EINVAL; 556 goto fail2; 557 } 558 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) { 559 G_GATE_DEBUG(1, "Invalid read offset."); 560 error = EINVAL; 561 goto fail2; 562 } 563 if (ggio->gctl_mediasize + ggio->gctl_readoffset > 564 ropp->mediasize) { 565 G_GATE_DEBUG(1, "Invalid read offset or media size."); 566 error = EINVAL; 567 goto fail2; 568 } 569 } 570 571 gp = g_new_geomf(&g_gate_class, "%s", name); 572 gp->start = g_gate_start; 573 gp->access = g_gate_access; 574 gp->orphan = g_gate_orphan; 575 gp->dumpconf = g_gate_dumpconf; 576 gp->softc = sc; 577 578 if (ropp != NULL) { 579 cp = g_new_consumer(gp); 580 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 581 error = g_attach(cp, ropp); 582 if (error != 0) { 583 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name); 584 goto fail3; 585 } 586 error = g_access(cp, 1, 0, 0); 587 if (error != 0) { 588 G_GATE_DEBUG(1, "Unable to access %s.", ropp->name); 589 g_detach(cp); 590 goto fail3; 591 } 592 sc->sc_readcons = cp; 593 sc->sc_readoffset = ggio->gctl_readoffset; 594 } 595 596 ggio->gctl_unit = sc->sc_unit; 597 598 pp = g_new_providerf(gp, "%s", name); 599 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 600 pp->mediasize = ggio->gctl_mediasize; 601 pp->sectorsize = ggio->gctl_sectorsize; 602 sc->sc_provider = pp; 603 g_error_provider(pp, 0); 604 605 g_topology_unlock(); 606 mtx_lock(&g_gate_units_lock); 607 sc->sc_name = sc->sc_provider->name; 608 mtx_unlock(&g_gate_units_lock); 609 G_GATE_DEBUG(1, "Device %s created.", gp->name); 610 611 if (sc->sc_timeout > 0) { 612 callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 613 g_gate_guard, sc); 614 } 615 return (0); 616 fail3: 617 g_destroy_consumer(cp); 618 g_destroy_geom(gp); 619 fail2: 620 g_topology_unlock(); 621 mtx_lock(&g_gate_units_lock); 622 g_gate_units[sc->sc_unit] = NULL; 623 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 624 g_gate_nunits--; 625 fail1: 626 mtx_unlock(&g_gate_units_lock); 627 mtx_destroy(&sc->sc_queue_mtx); 628 mtx_destroy(&sc->sc_read_mtx); 629 free(sc, M_GATE); 630 return (error); 631 } 632 633 static int 634 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio) 635 { 636 struct g_provider *pp; 637 struct g_consumer *cp; 638 int done, error; 639 640 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) { 641 if (ggio->gctl_mediasize <= 0) { 642 G_GATE_DEBUG(1, "Invalid media size."); 643 return (EINVAL); 644 } 645 pp = sc->sc_provider; 646 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) { 647 G_GATE_DEBUG(1, "Invalid media size."); 648 return (EINVAL); 649 } 650 g_resize_provider(pp, ggio->gctl_mediasize); 651 return (0); 652 } 653 654 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0) 655 (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 656 657 cp = NULL; 658 659 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 660 g_topology_lock(); 661 mtx_lock(&sc->sc_read_mtx); 662 if ((cp = sc->sc_readcons) != NULL) { 663 sc->sc_readcons = NULL; 664 done = (cp->index == 0); 665 mtx_unlock(&sc->sc_read_mtx); 666 if (done) 667 g_gate_detach(cp, 0); 668 } else 669 mtx_unlock(&sc->sc_read_mtx); 670 if (ggio->gctl_readprov[0] != '\0') { 671 pp = g_provider_by_name(ggio->gctl_readprov); 672 if (pp == NULL) { 673 g_topology_unlock(); 674 G_GATE_DEBUG(1, "Provider %s doesn't exist.", 675 ggio->gctl_readprov); 676 return (EINVAL); 677 } 678 cp = g_new_consumer(sc->sc_provider->geom); 679 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 680 error = g_attach(cp, pp); 681 if (error != 0) { 682 G_GATE_DEBUG(1, "Unable to attach to %s.", 683 pp->name); 684 } else { 685 error = g_access(cp, 1, 0, 0); 686 if (error != 0) { 687 G_GATE_DEBUG(1, "Unable to access %s.", 688 pp->name); 689 g_detach(cp); 690 } 691 } 692 if (error != 0) { 693 g_destroy_consumer(cp); 694 g_topology_unlock(); 695 return (error); 696 } 697 } 698 } else { 699 cp = sc->sc_readcons; 700 } 701 702 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) { 703 if (cp == NULL) { 704 G_GATE_DEBUG(1, "No read provider."); 705 return (EINVAL); 706 } 707 pp = sc->sc_provider; 708 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) { 709 G_GATE_DEBUG(1, "Invalid read offset."); 710 return (EINVAL); 711 } 712 if (pp->mediasize + ggio->gctl_readoffset > 713 cp->provider->mediasize) { 714 G_GATE_DEBUG(1, "Invalid read offset or media size."); 715 return (EINVAL); 716 } 717 sc->sc_readoffset = ggio->gctl_readoffset; 718 } 719 720 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 721 sc->sc_readcons = cp; 722 g_topology_unlock(); 723 } 724 725 return (0); 726 } 727 728 #define G_GATE_CHECK_VERSION(ggio) do { \ 729 if ((ggio)->gctl_version != G_GATE_VERSION) { \ 730 printf("Version mismatch %d != %d.\n", \ 731 ggio->gctl_version, G_GATE_VERSION); \ 732 return (EINVAL); \ 733 } \ 734 } while (0) 735 static int 736 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 737 { 738 struct g_gate_softc *sc; 739 struct bio *bp; 740 int error = 0; 741 742 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr, 743 flags, td); 744 745 switch (cmd) { 746 case G_GATE_CMD_CREATE: 747 { 748 struct g_gate_ctl_create *ggio = (void *)addr; 749 750 G_GATE_CHECK_VERSION(ggio); 751 error = g_gate_create(ggio); 752 /* 753 * Reset TDP_GEOM flag. 754 * There are pending events for sure, because we just created 755 * new provider and other classes want to taste it, but we 756 * cannot answer on I/O requests until we're here. 757 */ 758 td->td_pflags &= ~TDP_GEOM; 759 return (error); 760 } 761 case G_GATE_CMD_MODIFY: 762 { 763 struct g_gate_ctl_modify *ggio = (void *)addr; 764 765 G_GATE_CHECK_VERSION(ggio); 766 sc = g_gate_hold(ggio->gctl_unit, NULL); 767 if (sc == NULL) 768 return (ENXIO); 769 error = g_gate_modify(sc, ggio); 770 g_gate_release(sc); 771 return (error); 772 } 773 case G_GATE_CMD_DESTROY: 774 { 775 struct g_gate_ctl_destroy *ggio = (void *)addr; 776 777 G_GATE_CHECK_VERSION(ggio); 778 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 779 if (sc == NULL) 780 return (ENXIO); 781 g_topology_lock(); 782 mtx_lock(&g_gate_units_lock); 783 error = g_gate_destroy(sc, ggio->gctl_force); 784 g_topology_unlock(); 785 if (error != 0) 786 g_gate_release(sc); 787 return (error); 788 } 789 case G_GATE_CMD_CANCEL: 790 { 791 struct g_gate_ctl_cancel *ggio = (void *)addr; 792 struct bio *tbp, *lbp; 793 794 G_GATE_CHECK_VERSION(ggio); 795 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 796 if (sc == NULL) 797 return (ENXIO); 798 lbp = NULL; 799 mtx_lock(&sc->sc_queue_mtx); 800 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) { 801 if (ggio->gctl_seq == 0 || 802 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) { 803 G_GATE_LOGREQ(1, bp, "Request canceled."); 804 bioq_remove(&sc->sc_outqueue, bp); 805 /* 806 * Be sure to put requests back onto incoming 807 * queue in the proper order. 808 */ 809 if (lbp == NULL) 810 bioq_insert_head(&sc->sc_inqueue, bp); 811 else { 812 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue, 813 lbp, bp, bio_queue); 814 } 815 lbp = bp; 816 /* 817 * If only one request was canceled, leave now. 818 */ 819 if (ggio->gctl_seq != 0) 820 break; 821 } 822 } 823 if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 824 ggio->gctl_unit = sc->sc_unit; 825 mtx_unlock(&sc->sc_queue_mtx); 826 g_gate_release(sc); 827 return (error); 828 } 829 case G_GATE_CMD_START: 830 { 831 struct g_gate_ctl_io *ggio = (void *)addr; 832 833 G_GATE_CHECK_VERSION(ggio); 834 sc = g_gate_hold(ggio->gctl_unit, NULL); 835 if (sc == NULL) 836 return (ENXIO); 837 error = 0; 838 for (;;) { 839 mtx_lock(&sc->sc_queue_mtx); 840 bp = bioq_first(&sc->sc_inqueue); 841 if (bp != NULL) 842 break; 843 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 844 ggio->gctl_error = ECANCELED; 845 mtx_unlock(&sc->sc_queue_mtx); 846 goto start_end; 847 } 848 if (msleep(sc, &sc->sc_queue_mtx, 849 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) { 850 ggio->gctl_error = ECANCELED; 851 goto start_end; 852 } 853 } 854 ggio->gctl_cmd = bp->bio_cmd; 855 if (bp->bio_cmd == BIO_WRITE && 856 bp->bio_length > ggio->gctl_length) { 857 mtx_unlock(&sc->sc_queue_mtx); 858 ggio->gctl_length = bp->bio_length; 859 ggio->gctl_error = ENOMEM; 860 goto start_end; 861 } 862 bioq_remove(&sc->sc_inqueue, bp); 863 bioq_insert_tail(&sc->sc_outqueue, bp); 864 mtx_unlock(&sc->sc_queue_mtx); 865 866 ggio->gctl_seq = (uintptr_t)bp->bio_driver1; 867 ggio->gctl_offset = bp->bio_offset; 868 ggio->gctl_length = bp->bio_length; 869 870 switch (bp->bio_cmd) { 871 case BIO_READ: 872 case BIO_DELETE: 873 case BIO_FLUSH: 874 break; 875 case BIO_WRITE: 876 error = copyout(bp->bio_data, ggio->gctl_data, 877 bp->bio_length); 878 if (error != 0) { 879 mtx_lock(&sc->sc_queue_mtx); 880 bioq_remove(&sc->sc_outqueue, bp); 881 bioq_insert_head(&sc->sc_inqueue, bp); 882 mtx_unlock(&sc->sc_queue_mtx); 883 goto start_end; 884 } 885 break; 886 } 887 start_end: 888 g_gate_release(sc); 889 return (error); 890 } 891 case G_GATE_CMD_DONE: 892 { 893 struct g_gate_ctl_io *ggio = (void *)addr; 894 895 G_GATE_CHECK_VERSION(ggio); 896 sc = g_gate_hold(ggio->gctl_unit, NULL); 897 if (sc == NULL) 898 return (ENOENT); 899 error = 0; 900 mtx_lock(&sc->sc_queue_mtx); 901 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) { 902 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1) 903 break; 904 } 905 if (bp != NULL) { 906 bioq_remove(&sc->sc_outqueue, bp); 907 sc->sc_queue_count--; 908 } 909 mtx_unlock(&sc->sc_queue_mtx); 910 if (bp == NULL) { 911 /* 912 * Request was probably canceled. 913 */ 914 goto done_end; 915 } 916 if (ggio->gctl_error == EAGAIN) { 917 bp->bio_error = 0; 918 G_GATE_LOGREQ(1, bp, "Request desisted."); 919 mtx_lock(&sc->sc_queue_mtx); 920 sc->sc_queue_count++; 921 bioq_insert_head(&sc->sc_inqueue, bp); 922 wakeup(sc); 923 mtx_unlock(&sc->sc_queue_mtx); 924 } else { 925 bp->bio_error = ggio->gctl_error; 926 if (bp->bio_error == 0) { 927 bp->bio_completed = bp->bio_length; 928 switch (bp->bio_cmd) { 929 case BIO_READ: 930 error = copyin(ggio->gctl_data, 931 bp->bio_data, bp->bio_length); 932 if (error != 0) 933 bp->bio_error = error; 934 break; 935 case BIO_DELETE: 936 case BIO_WRITE: 937 case BIO_FLUSH: 938 break; 939 } 940 } 941 G_GATE_LOGREQ(2, bp, "Request done."); 942 g_io_deliver(bp, bp->bio_error); 943 } 944 done_end: 945 g_gate_release(sc); 946 return (error); 947 } 948 } 949 return (ENOIOCTL); 950 } 951 952 static void 953 g_gate_device(void) 954 { 955 956 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600, 957 G_GATE_CTL_NAME); 958 } 959 960 static int 961 g_gate_modevent(module_t mod, int type, void *data) 962 { 963 int error = 0; 964 965 switch (type) { 966 case MOD_LOAD: 967 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF); 968 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]), 969 M_GATE, M_WAITOK | M_ZERO); 970 g_gate_nunits = 0; 971 g_gate_device(); 972 break; 973 case MOD_UNLOAD: 974 mtx_lock(&g_gate_units_lock); 975 if (g_gate_nunits > 0) { 976 mtx_unlock(&g_gate_units_lock); 977 error = EBUSY; 978 break; 979 } 980 mtx_unlock(&g_gate_units_lock); 981 mtx_destroy(&g_gate_units_lock); 982 if (status_dev != NULL) 983 destroy_dev(status_dev); 984 free(g_gate_units, M_GATE); 985 break; 986 default: 987 return (EOPNOTSUPP); 988 break; 989 } 990 991 return (error); 992 } 993 static moduledata_t g_gate_module = { 994 G_GATE_MOD_NAME, 995 g_gate_modevent, 996 NULL 997 }; 998 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 999 DECLARE_GEOM_CLASS(g_gate_class, g_gate); 1000 MODULE_VERSION(geom_gate, 0); 1001