1 /*- 2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * Copyright (c) 2009-2010 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Pawel Jakub Dawidek 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bio.h> 37 #include <sys/conf.h> 38 #include <sys/kernel.h> 39 #include <sys/kthread.h> 40 #include <sys/fcntl.h> 41 #include <sys/linker.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mutex.h> 45 #include <sys/proc.h> 46 #include <sys/limits.h> 47 #include <sys/queue.h> 48 #include <sys/sysctl.h> 49 #include <sys/signalvar.h> 50 #include <sys/time.h> 51 #include <machine/atomic.h> 52 53 #include <geom/geom.h> 54 #include <geom/gate/g_gate.h> 55 56 FEATURE(geom_gate, "GEOM Gate module"); 57 58 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data"); 59 60 SYSCTL_DECL(_kern_geom); 61 SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, "GEOM_GATE stuff"); 62 static int g_gate_debug = 0; 63 TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug); 64 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0, 65 "Debug level"); 66 static u_int g_gate_maxunits = 256; 67 TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits); 68 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN, 69 &g_gate_maxunits, 0, "Maximum number of ggate devices"); 70 71 struct g_class g_gate_class = { 72 .name = G_GATE_CLASS_NAME, 73 .version = G_VERSION, 74 }; 75 76 static struct cdev *status_dev; 77 static d_ioctl_t g_gate_ioctl; 78 static struct cdevsw g_gate_cdevsw = { 79 .d_version = D_VERSION, 80 .d_ioctl = g_gate_ioctl, 81 .d_name = G_GATE_CTL_NAME 82 }; 83 84 85 static struct g_gate_softc **g_gate_units; 86 static u_int g_gate_nunits; 87 static struct mtx g_gate_units_lock; 88 89 static int 90 g_gate_destroy(struct g_gate_softc *sc, boolean_t force) 91 { 92 struct g_provider *pp; 93 struct g_geom *gp; 94 struct bio *bp; 95 96 g_topology_assert(); 97 mtx_assert(&g_gate_units_lock, MA_OWNED); 98 pp = sc->sc_provider; 99 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 100 mtx_unlock(&g_gate_units_lock); 101 return (EBUSY); 102 } 103 mtx_unlock(&g_gate_units_lock); 104 mtx_lock(&sc->sc_queue_mtx); 105 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) 106 sc->sc_flags |= G_GATE_FLAG_DESTROY; 107 wakeup(sc); 108 mtx_unlock(&sc->sc_queue_mtx); 109 gp = pp->geom; 110 pp->flags |= G_PF_WITHER; 111 g_orphan_provider(pp, ENXIO); 112 callout_drain(&sc->sc_callout); 113 mtx_lock(&sc->sc_queue_mtx); 114 while ((bp = bioq_first(&sc->sc_inqueue)) != NULL) { 115 bioq_remove(&sc->sc_inqueue, bp); 116 sc->sc_queue_count--; 117 G_GATE_LOGREQ(1, bp, "Request canceled."); 118 g_io_deliver(bp, ENXIO); 119 } 120 while ((bp = bioq_first(&sc->sc_outqueue)) != NULL) { 121 bioq_remove(&sc->sc_outqueue, bp); 122 sc->sc_queue_count--; 123 G_GATE_LOGREQ(1, bp, "Request canceled."); 124 g_io_deliver(bp, ENXIO); 125 } 126 mtx_unlock(&sc->sc_queue_mtx); 127 g_topology_unlock(); 128 mtx_lock(&g_gate_units_lock); 129 /* One reference is ours. */ 130 sc->sc_ref--; 131 while (sc->sc_ref > 0) 132 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0); 133 g_gate_units[sc->sc_unit] = NULL; 134 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 135 g_gate_nunits--; 136 mtx_unlock(&g_gate_units_lock); 137 mtx_destroy(&sc->sc_queue_mtx); 138 g_topology_lock(); 139 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name); 140 gp->softc = NULL; 141 g_wither_geom(gp, ENXIO); 142 sc->sc_provider = NULL; 143 free(sc, M_GATE); 144 return (0); 145 } 146 147 static int 148 g_gate_access(struct g_provider *pp, int dr, int dw, int de) 149 { 150 struct g_gate_softc *sc; 151 152 if (dr <= 0 && dw <= 0 && de <= 0) 153 return (0); 154 sc = pp->geom->softc; 155 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 156 return (ENXIO); 157 /* XXX: Hack to allow read-only mounts. */ 158 #if 0 159 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0) 160 return (EPERM); 161 #endif 162 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0) 163 return (EPERM); 164 return (0); 165 } 166 167 static void 168 g_gate_start(struct bio *bp) 169 { 170 struct g_gate_softc *sc; 171 172 sc = bp->bio_to->geom->softc; 173 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 174 g_io_deliver(bp, ENXIO); 175 return; 176 } 177 G_GATE_LOGREQ(2, bp, "Request received."); 178 switch (bp->bio_cmd) { 179 case BIO_READ: 180 break; 181 case BIO_DELETE: 182 case BIO_WRITE: 183 case BIO_FLUSH: 184 /* XXX: Hack to allow read-only mounts. */ 185 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 186 g_io_deliver(bp, EPERM); 187 return; 188 } 189 break; 190 case BIO_GETATTR: 191 default: 192 G_GATE_LOGREQ(2, bp, "Ignoring request."); 193 g_io_deliver(bp, EOPNOTSUPP); 194 return; 195 } 196 197 mtx_lock(&sc->sc_queue_mtx); 198 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) { 199 mtx_unlock(&sc->sc_queue_mtx); 200 G_GATE_LOGREQ(1, bp, "Queue full, request canceled."); 201 g_io_deliver(bp, ENOMEM); 202 return; 203 } 204 205 bp->bio_driver1 = (void *)sc->sc_seq; 206 sc->sc_seq++; 207 sc->sc_queue_count++; 208 209 bioq_insert_tail(&sc->sc_inqueue, bp); 210 wakeup(sc); 211 212 mtx_unlock(&sc->sc_queue_mtx); 213 } 214 215 static struct g_gate_softc * 216 g_gate_hold(int unit, const char *name) 217 { 218 struct g_gate_softc *sc = NULL; 219 220 mtx_lock(&g_gate_units_lock); 221 if (unit >= 0 && unit < g_gate_maxunits) 222 sc = g_gate_units[unit]; 223 else if (unit == G_GATE_NAME_GIVEN) { 224 KASSERT(name != NULL, ("name is NULL")); 225 for (unit = 0; unit < g_gate_maxunits; unit++) { 226 if (g_gate_units[unit] == NULL) 227 continue; 228 if (strcmp(name, 229 g_gate_units[unit]->sc_provider->name) != 0) { 230 continue; 231 } 232 sc = g_gate_units[unit]; 233 break; 234 } 235 } 236 if (sc != NULL) 237 sc->sc_ref++; 238 mtx_unlock(&g_gate_units_lock); 239 return (sc); 240 } 241 242 static void 243 g_gate_release(struct g_gate_softc *sc) 244 { 245 246 g_topology_assert_not(); 247 mtx_lock(&g_gate_units_lock); 248 sc->sc_ref--; 249 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name)); 250 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 251 wakeup(&sc->sc_ref); 252 mtx_unlock(&g_gate_units_lock); 253 } 254 255 static int 256 g_gate_getunit(int unit, int *errorp) 257 { 258 259 mtx_assert(&g_gate_units_lock, MA_OWNED); 260 if (unit >= 0) { 261 if (unit >= g_gate_maxunits) 262 *errorp = EINVAL; 263 else if (g_gate_units[unit] == NULL) 264 return (unit); 265 else 266 *errorp = EEXIST; 267 } else { 268 for (unit = 0; unit < g_gate_maxunits; unit++) { 269 if (g_gate_units[unit] == NULL) 270 return (unit); 271 } 272 *errorp = ENFILE; 273 } 274 return (-1); 275 } 276 277 static void 278 g_gate_guard(void *arg) 279 { 280 struct g_gate_softc *sc; 281 struct bintime curtime; 282 struct bio *bp, *bp2; 283 284 sc = arg; 285 binuptime(&curtime); 286 g_gate_hold(sc->sc_unit, NULL); 287 mtx_lock(&sc->sc_queue_mtx); 288 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) { 289 if (curtime.sec - bp->bio_t0.sec < 5) 290 continue; 291 bioq_remove(&sc->sc_inqueue, bp); 292 sc->sc_queue_count--; 293 G_GATE_LOGREQ(1, bp, "Request timeout."); 294 g_io_deliver(bp, EIO); 295 } 296 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) { 297 if (curtime.sec - bp->bio_t0.sec < 5) 298 continue; 299 bioq_remove(&sc->sc_outqueue, bp); 300 sc->sc_queue_count--; 301 G_GATE_LOGREQ(1, bp, "Request timeout."); 302 g_io_deliver(bp, EIO); 303 } 304 mtx_unlock(&sc->sc_queue_mtx); 305 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) { 306 callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 307 g_gate_guard, sc); 308 } 309 g_gate_release(sc); 310 } 311 312 static void 313 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 314 struct g_consumer *cp, struct g_provider *pp) 315 { 316 struct g_gate_softc *sc; 317 318 sc = gp->softc; 319 if (sc == NULL || pp != NULL || cp != NULL) 320 return; 321 g_gate_hold(sc->sc_unit, NULL); 322 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 323 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only"); 324 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) { 325 sbuf_printf(sb, "%s<access>%s</access>\n", indent, 326 "write-only"); 327 } else { 328 sbuf_printf(sb, "%s<access>%s</access>\n", indent, 329 "read-write"); 330 } 331 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout); 332 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info); 333 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent, 334 sc->sc_queue_count); 335 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent, 336 sc->sc_queue_size); 337 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref); 338 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit); 339 g_topology_unlock(); 340 g_gate_release(sc); 341 g_topology_lock(); 342 } 343 344 static int 345 g_gate_create(struct g_gate_ctl_create *ggio) 346 { 347 struct g_gate_softc *sc; 348 struct g_geom *gp; 349 struct g_provider *pp; 350 char name[NAME_MAX]; 351 int error = 0, unit; 352 353 if (ggio->gctl_mediasize == 0) { 354 G_GATE_DEBUG(1, "Invalid media size."); 355 return (EINVAL); 356 } 357 if (ggio->gctl_sectorsize > 0 && !powerof2(ggio->gctl_sectorsize)) { 358 G_GATE_DEBUG(1, "Invalid sector size."); 359 return (EINVAL); 360 } 361 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) { 362 G_GATE_DEBUG(1, "Invalid media size."); 363 return (EINVAL); 364 } 365 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 && 366 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) { 367 G_GATE_DEBUG(1, "Invalid flags."); 368 return (EINVAL); 369 } 370 if (ggio->gctl_unit != G_GATE_UNIT_AUTO && 371 ggio->gctl_unit != G_GATE_NAME_GIVEN && 372 ggio->gctl_unit < 0) { 373 G_GATE_DEBUG(1, "Invalid unit number."); 374 return (EINVAL); 375 } 376 if (ggio->gctl_unit == G_GATE_NAME_GIVEN && 377 ggio->gctl_name[0] == '\0') { 378 G_GATE_DEBUG(1, "No device name."); 379 return (EINVAL); 380 } 381 382 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO); 383 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS); 384 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 385 sc->sc_seq = 1; 386 bioq_init(&sc->sc_inqueue); 387 bioq_init(&sc->sc_outqueue); 388 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF); 389 sc->sc_queue_count = 0; 390 sc->sc_queue_size = ggio->gctl_maxcount; 391 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE) 392 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE; 393 sc->sc_timeout = ggio->gctl_timeout; 394 callout_init(&sc->sc_callout, CALLOUT_MPSAFE); 395 mtx_lock(&g_gate_units_lock); 396 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error); 397 if (sc->sc_unit < 0) { 398 mtx_unlock(&g_gate_units_lock); 399 mtx_destroy(&sc->sc_queue_mtx); 400 free(sc, M_GATE); 401 return (error); 402 } 403 if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 404 snprintf(name, sizeof(name), "%s", ggio->gctl_name); 405 else { 406 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME, 407 sc->sc_unit); 408 } 409 /* Check for name collision. */ 410 for (unit = 0; unit < g_gate_maxunits; unit++) { 411 if (g_gate_units[unit] == NULL) 412 continue; 413 if (strcmp(name, g_gate_units[unit]->sc_name) != 0) 414 continue; 415 mtx_unlock(&g_gate_units_lock); 416 mtx_destroy(&sc->sc_queue_mtx); 417 free(sc, M_GATE); 418 return (EEXIST); 419 } 420 sc->sc_name = name; 421 g_gate_units[sc->sc_unit] = sc; 422 g_gate_nunits++; 423 mtx_unlock(&g_gate_units_lock); 424 425 ggio->gctl_unit = sc->sc_unit; 426 427 g_topology_lock(); 428 gp = g_new_geomf(&g_gate_class, "%s", name); 429 gp->start = g_gate_start; 430 gp->access = g_gate_access; 431 gp->dumpconf = g_gate_dumpconf; 432 gp->softc = sc; 433 pp = g_new_providerf(gp, "%s", name); 434 pp->mediasize = ggio->gctl_mediasize; 435 pp->sectorsize = ggio->gctl_sectorsize; 436 sc->sc_provider = pp; 437 g_error_provider(pp, 0); 438 g_topology_unlock(); 439 mtx_lock(&g_gate_units_lock); 440 sc->sc_name = sc->sc_provider->name; 441 mtx_unlock(&g_gate_units_lock); 442 G_GATE_DEBUG(1, "Device %s created.", gp->name); 443 444 if (sc->sc_timeout > 0) { 445 callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 446 g_gate_guard, sc); 447 } 448 return (0); 449 } 450 451 #define G_GATE_CHECK_VERSION(ggio) do { \ 452 if ((ggio)->gctl_version != G_GATE_VERSION) { \ 453 printf("Version mismatch %d != %d.\n", \ 454 ggio->gctl_version, G_GATE_VERSION); \ 455 return (EINVAL); \ 456 } \ 457 } while (0) 458 static int 459 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 460 { 461 struct g_gate_softc *sc; 462 struct bio *bp; 463 int error = 0; 464 465 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr, 466 flags, td); 467 468 switch (cmd) { 469 case G_GATE_CMD_CREATE: 470 { 471 struct g_gate_ctl_create *ggio = (void *)addr; 472 473 G_GATE_CHECK_VERSION(ggio); 474 error = g_gate_create(ggio); 475 /* 476 * Reset TDP_GEOM flag. 477 * There are pending events for sure, because we just created 478 * new provider and other classes want to taste it, but we 479 * cannot answer on I/O requests until we're here. 480 */ 481 td->td_pflags &= ~TDP_GEOM; 482 return (error); 483 } 484 case G_GATE_CMD_DESTROY: 485 { 486 struct g_gate_ctl_destroy *ggio = (void *)addr; 487 488 G_GATE_CHECK_VERSION(ggio); 489 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 490 if (sc == NULL) 491 return (ENXIO); 492 g_topology_lock(); 493 mtx_lock(&g_gate_units_lock); 494 error = g_gate_destroy(sc, ggio->gctl_force); 495 g_topology_unlock(); 496 if (error != 0) 497 g_gate_release(sc); 498 return (error); 499 } 500 case G_GATE_CMD_CANCEL: 501 { 502 struct g_gate_ctl_cancel *ggio = (void *)addr; 503 struct bio *tbp, *lbp; 504 505 G_GATE_CHECK_VERSION(ggio); 506 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 507 if (sc == NULL) 508 return (ENXIO); 509 lbp = NULL; 510 mtx_lock(&sc->sc_queue_mtx); 511 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) { 512 if (ggio->gctl_seq == 0 || 513 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) { 514 G_GATE_LOGREQ(1, bp, "Request canceled."); 515 bioq_remove(&sc->sc_outqueue, bp); 516 /* 517 * Be sure to put requests back onto incoming 518 * queue in the proper order. 519 */ 520 if (lbp == NULL) 521 bioq_insert_head(&sc->sc_inqueue, bp); 522 else { 523 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue, 524 lbp, bp, bio_queue); 525 } 526 lbp = bp; 527 /* 528 * If only one request was canceled, leave now. 529 */ 530 if (ggio->gctl_seq != 0) 531 break; 532 } 533 } 534 if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 535 ggio->gctl_unit = sc->sc_unit; 536 mtx_unlock(&sc->sc_queue_mtx); 537 g_gate_release(sc); 538 return (error); 539 } 540 case G_GATE_CMD_START: 541 { 542 struct g_gate_ctl_io *ggio = (void *)addr; 543 544 G_GATE_CHECK_VERSION(ggio); 545 sc = g_gate_hold(ggio->gctl_unit, NULL); 546 if (sc == NULL) 547 return (ENXIO); 548 error = 0; 549 for (;;) { 550 mtx_lock(&sc->sc_queue_mtx); 551 bp = bioq_first(&sc->sc_inqueue); 552 if (bp != NULL) 553 break; 554 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 555 ggio->gctl_error = ECANCELED; 556 mtx_unlock(&sc->sc_queue_mtx); 557 goto start_end; 558 } 559 if (msleep(sc, &sc->sc_queue_mtx, 560 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) { 561 ggio->gctl_error = ECANCELED; 562 goto start_end; 563 } 564 } 565 ggio->gctl_cmd = bp->bio_cmd; 566 if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) && 567 bp->bio_length > ggio->gctl_length) { 568 mtx_unlock(&sc->sc_queue_mtx); 569 ggio->gctl_length = bp->bio_length; 570 ggio->gctl_error = ENOMEM; 571 goto start_end; 572 } 573 bioq_remove(&sc->sc_inqueue, bp); 574 bioq_insert_tail(&sc->sc_outqueue, bp); 575 mtx_unlock(&sc->sc_queue_mtx); 576 577 ggio->gctl_seq = (uintptr_t)bp->bio_driver1; 578 ggio->gctl_offset = bp->bio_offset; 579 ggio->gctl_length = bp->bio_length; 580 581 switch (bp->bio_cmd) { 582 case BIO_READ: 583 case BIO_DELETE: 584 case BIO_FLUSH: 585 break; 586 case BIO_WRITE: 587 error = copyout(bp->bio_data, ggio->gctl_data, 588 bp->bio_length); 589 if (error != 0) { 590 mtx_lock(&sc->sc_queue_mtx); 591 bioq_remove(&sc->sc_outqueue, bp); 592 bioq_insert_head(&sc->sc_inqueue, bp); 593 mtx_unlock(&sc->sc_queue_mtx); 594 goto start_end; 595 } 596 break; 597 } 598 start_end: 599 g_gate_release(sc); 600 return (error); 601 } 602 case G_GATE_CMD_DONE: 603 { 604 struct g_gate_ctl_io *ggio = (void *)addr; 605 606 G_GATE_CHECK_VERSION(ggio); 607 sc = g_gate_hold(ggio->gctl_unit, NULL); 608 if (sc == NULL) 609 return (ENOENT); 610 error = 0; 611 mtx_lock(&sc->sc_queue_mtx); 612 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) { 613 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1) 614 break; 615 } 616 if (bp != NULL) { 617 bioq_remove(&sc->sc_outqueue, bp); 618 sc->sc_queue_count--; 619 } 620 mtx_unlock(&sc->sc_queue_mtx); 621 if (bp == NULL) { 622 /* 623 * Request was probably canceled. 624 */ 625 goto done_end; 626 } 627 if (ggio->gctl_error == EAGAIN) { 628 bp->bio_error = 0; 629 G_GATE_LOGREQ(1, bp, "Request desisted."); 630 mtx_lock(&sc->sc_queue_mtx); 631 sc->sc_queue_count++; 632 bioq_insert_head(&sc->sc_inqueue, bp); 633 wakeup(sc); 634 mtx_unlock(&sc->sc_queue_mtx); 635 } else { 636 bp->bio_error = ggio->gctl_error; 637 if (bp->bio_error == 0) { 638 bp->bio_completed = bp->bio_length; 639 switch (bp->bio_cmd) { 640 case BIO_READ: 641 error = copyin(ggio->gctl_data, 642 bp->bio_data, bp->bio_length); 643 if (error != 0) 644 bp->bio_error = error; 645 break; 646 case BIO_DELETE: 647 case BIO_WRITE: 648 case BIO_FLUSH: 649 break; 650 } 651 } 652 G_GATE_LOGREQ(2, bp, "Request done."); 653 g_io_deliver(bp, bp->bio_error); 654 } 655 done_end: 656 g_gate_release(sc); 657 return (error); 658 } 659 } 660 return (ENOIOCTL); 661 } 662 663 static void 664 g_gate_device(void) 665 { 666 667 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600, 668 G_GATE_CTL_NAME); 669 } 670 671 static int 672 g_gate_modevent(module_t mod, int type, void *data) 673 { 674 int error = 0; 675 676 switch (type) { 677 case MOD_LOAD: 678 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF); 679 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]), 680 M_GATE, M_WAITOK | M_ZERO); 681 g_gate_nunits = 0; 682 g_gate_device(); 683 break; 684 case MOD_UNLOAD: 685 mtx_lock(&g_gate_units_lock); 686 if (g_gate_nunits > 0) { 687 mtx_unlock(&g_gate_units_lock); 688 error = EBUSY; 689 break; 690 } 691 mtx_unlock(&g_gate_units_lock); 692 mtx_destroy(&g_gate_units_lock); 693 if (status_dev != 0) 694 destroy_dev(status_dev); 695 free(g_gate_units, M_GATE); 696 break; 697 default: 698 return (EOPNOTSUPP); 699 break; 700 } 701 702 return (error); 703 } 704 static moduledata_t g_gate_module = { 705 G_GATE_MOD_NAME, 706 g_gate_modevent, 707 NULL 708 }; 709 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 710 DECLARE_GEOM_CLASS(g_gate_class, g_gate); 711