2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2009-2010 The FreeBSD Foundation
6 * Portions of this software were developed by Pawel Jakub Dawidek
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/fcntl.h>
41 #include <sys/linker.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/limits.h>
47 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 #include <sys/signalvar.h>
52 #include <machine/atomic.h>
54 #include <geom/geom.h>
55 #include <geom/gate/g_gate.h>
57 FEATURE(geom_gate, "GEOM Gate module");
59 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
61 SYSCTL_DECL(_kern_geom);
62 SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, "GEOM_GATE stuff");
63 static int g_gate_debug = 0;
64 TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug);
65 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0,
67 static u_int g_gate_maxunits = 256;
68 TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits);
69 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
70 &g_gate_maxunits, 0, "Maximum number of ggate devices");
72 struct g_class g_gate_class = {
73 .name = G_GATE_CLASS_NAME,
77 static struct cdev *status_dev;
78 static d_ioctl_t g_gate_ioctl;
79 static struct cdevsw g_gate_cdevsw = {
80 .d_version = D_VERSION,
81 .d_ioctl = g_gate_ioctl,
82 .d_name = G_GATE_CTL_NAME
86 static struct g_gate_softc **g_gate_units;
87 static u_int g_gate_nunits;
88 static struct mtx g_gate_units_lock;
91 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
93 struct g_provider *pp;
98 mtx_assert(&g_gate_units_lock, MA_OWNED);
100 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
101 mtx_unlock(&g_gate_units_lock);
104 mtx_unlock(&g_gate_units_lock);
105 mtx_lock(&sc->sc_queue_mtx);
106 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
107 sc->sc_flags |= G_GATE_FLAG_DESTROY;
109 mtx_unlock(&sc->sc_queue_mtx);
111 pp->flags |= G_PF_WITHER;
112 g_orphan_provider(pp, ENXIO);
113 callout_drain(&sc->sc_callout);
114 mtx_lock(&sc->sc_queue_mtx);
115 while ((bp = bioq_first(&sc->sc_inqueue)) != NULL) {
116 bioq_remove(&sc->sc_inqueue, bp);
117 sc->sc_queue_count--;
118 G_GATE_LOGREQ(1, bp, "Request canceled.");
119 g_io_deliver(bp, ENXIO);
121 while ((bp = bioq_first(&sc->sc_outqueue)) != NULL) {
122 bioq_remove(&sc->sc_outqueue, bp);
123 sc->sc_queue_count--;
124 G_GATE_LOGREQ(1, bp, "Request canceled.");
125 g_io_deliver(bp, ENXIO);
127 mtx_unlock(&sc->sc_queue_mtx);
129 mtx_lock(&g_gate_units_lock);
130 /* One reference is ours. */
132 while (sc->sc_ref > 0)
133 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
134 g_gate_units[sc->sc_unit] = NULL;
135 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
137 mtx_unlock(&g_gate_units_lock);
138 mtx_destroy(&sc->sc_queue_mtx);
140 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name);
142 g_wither_geom(gp, ENXIO);
143 sc->sc_provider = NULL;
149 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
151 struct g_gate_softc *sc;
153 if (dr <= 0 && dw <= 0 && de <= 0)
155 sc = pp->geom->softc;
156 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
158 /* XXX: Hack to allow read-only mounts. */
160 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
163 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
169 g_gate_start(struct bio *bp)
171 struct g_gate_softc *sc;
173 sc = bp->bio_to->geom->softc;
174 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
175 g_io_deliver(bp, ENXIO);
178 G_GATE_LOGREQ(2, bp, "Request received.");
179 switch (bp->bio_cmd) {
185 /* XXX: Hack to allow read-only mounts. */
186 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
187 g_io_deliver(bp, EPERM);
193 G_GATE_LOGREQ(2, bp, "Ignoring request.");
194 g_io_deliver(bp, EOPNOTSUPP);
198 mtx_lock(&sc->sc_queue_mtx);
199 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) {
200 mtx_unlock(&sc->sc_queue_mtx);
201 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
202 g_io_deliver(bp, ENOMEM);
206 bp->bio_driver1 = (void *)sc->sc_seq;
208 sc->sc_queue_count++;
210 bioq_insert_tail(&sc->sc_inqueue, bp);
213 mtx_unlock(&sc->sc_queue_mtx);
216 static struct g_gate_softc *
217 g_gate_hold(int unit, const char *name)
219 struct g_gate_softc *sc = NULL;
221 mtx_lock(&g_gate_units_lock);
222 if (unit >= 0 && unit < g_gate_maxunits)
223 sc = g_gate_units[unit];
224 else if (unit == G_GATE_NAME_GIVEN) {
225 KASSERT(name != NULL, ("name is NULL"));
226 for (unit = 0; unit < g_gate_maxunits; unit++) {
227 if (g_gate_units[unit] == NULL)
230 g_gate_units[unit]->sc_provider->name) != 0) {
233 sc = g_gate_units[unit];
239 mtx_unlock(&g_gate_units_lock);
244 g_gate_release(struct g_gate_softc *sc)
247 g_topology_assert_not();
248 mtx_lock(&g_gate_units_lock);
250 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
251 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
253 mtx_unlock(&g_gate_units_lock);
257 g_gate_getunit(int unit, int *errorp)
260 mtx_assert(&g_gate_units_lock, MA_OWNED);
262 if (unit >= g_gate_maxunits)
264 else if (g_gate_units[unit] == NULL)
269 for (unit = 0; unit < g_gate_maxunits; unit++) {
270 if (g_gate_units[unit] == NULL)
279 g_gate_guard(void *arg)
281 struct g_gate_softc *sc;
282 struct bintime curtime;
283 struct bio *bp, *bp2;
287 g_gate_hold(sc->sc_unit, NULL);
288 mtx_lock(&sc->sc_queue_mtx);
289 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
290 if (curtime.sec - bp->bio_t0.sec < 5)
292 bioq_remove(&sc->sc_inqueue, bp);
293 sc->sc_queue_count--;
294 G_GATE_LOGREQ(1, bp, "Request timeout.");
295 g_io_deliver(bp, EIO);
297 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
298 if (curtime.sec - bp->bio_t0.sec < 5)
300 bioq_remove(&sc->sc_outqueue, bp);
301 sc->sc_queue_count--;
302 G_GATE_LOGREQ(1, bp, "Request timeout.");
303 g_io_deliver(bp, EIO);
305 mtx_unlock(&sc->sc_queue_mtx);
306 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
307 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
314 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
315 struct g_consumer *cp, struct g_provider *pp)
317 struct g_gate_softc *sc;
320 if (sc == NULL || pp != NULL || cp != NULL)
322 g_gate_hold(sc->sc_unit, NULL);
323 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
324 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
325 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
326 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
329 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
332 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
333 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
334 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
336 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
338 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
339 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
346 g_gate_create(struct g_gate_ctl_create *ggio)
348 struct g_gate_softc *sc;
350 struct g_provider *pp;
354 if (ggio->gctl_mediasize == 0) {
355 G_GATE_DEBUG(1, "Invalid media size.");
358 if (ggio->gctl_sectorsize > 0 && !powerof2(ggio->gctl_sectorsize)) {
359 G_GATE_DEBUG(1, "Invalid sector size.");
362 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
363 G_GATE_DEBUG(1, "Invalid media size.");
366 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
367 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
368 G_GATE_DEBUG(1, "Invalid flags.");
371 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
372 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
373 ggio->gctl_unit < 0) {
374 G_GATE_DEBUG(1, "Invalid unit number.");
377 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
378 ggio->gctl_name[0] == '\0') {
379 G_GATE_DEBUG(1, "No device name.");
383 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
384 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
385 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
387 bioq_init(&sc->sc_inqueue);
388 bioq_init(&sc->sc_outqueue);
389 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
390 sc->sc_queue_count = 0;
391 sc->sc_queue_size = ggio->gctl_maxcount;
392 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
393 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
394 sc->sc_timeout = ggio->gctl_timeout;
395 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
396 mtx_lock(&g_gate_units_lock);
397 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
398 if (sc->sc_unit < 0) {
399 mtx_unlock(&g_gate_units_lock);
400 mtx_destroy(&sc->sc_queue_mtx);
404 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
405 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
407 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
410 /* Check for name collision. */
411 for (unit = 0; unit < g_gate_maxunits; unit++) {
412 if (g_gate_units[unit] == NULL)
414 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
416 mtx_unlock(&g_gate_units_lock);
417 mtx_destroy(&sc->sc_queue_mtx);
422 g_gate_units[sc->sc_unit] = sc;
424 mtx_unlock(&g_gate_units_lock);
426 ggio->gctl_unit = sc->sc_unit;
429 gp = g_new_geomf(&g_gate_class, "%s", name);
430 gp->start = g_gate_start;
431 gp->access = g_gate_access;
432 gp->dumpconf = g_gate_dumpconf;
434 pp = g_new_providerf(gp, "%s", name);
435 pp->mediasize = ggio->gctl_mediasize;
436 pp->sectorsize = ggio->gctl_sectorsize;
437 sc->sc_provider = pp;
438 g_error_provider(pp, 0);
440 mtx_lock(&g_gate_units_lock);
441 sc->sc_name = sc->sc_provider->name;
442 mtx_unlock(&g_gate_units_lock);
443 G_GATE_DEBUG(1, "Device %s created.", gp->name);
445 if (sc->sc_timeout > 0) {
446 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
452 #define G_GATE_CHECK_VERSION(ggio) do { \
453 if ((ggio)->gctl_version != G_GATE_VERSION) { \
454 printf("Version mismatch %d != %d.\n", \
455 ggio->gctl_version, G_GATE_VERSION); \
460 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
462 struct g_gate_softc *sc;
466 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
470 case G_GATE_CMD_CREATE:
472 struct g_gate_ctl_create *ggio = (void *)addr;
474 G_GATE_CHECK_VERSION(ggio);
475 error = g_gate_create(ggio);
477 * Reset TDP_GEOM flag.
478 * There are pending events for sure, because we just created
479 * new provider and other classes want to taste it, but we
480 * cannot answer on I/O requests until we're here.
482 td->td_pflags &= ~TDP_GEOM;
485 case G_GATE_CMD_DESTROY:
487 struct g_gate_ctl_destroy *ggio = (void *)addr;
489 G_GATE_CHECK_VERSION(ggio);
490 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
494 mtx_lock(&g_gate_units_lock);
495 error = g_gate_destroy(sc, ggio->gctl_force);
501 case G_GATE_CMD_CANCEL:
503 struct g_gate_ctl_cancel *ggio = (void *)addr;
504 struct bio *tbp, *lbp;
506 G_GATE_CHECK_VERSION(ggio);
507 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
511 mtx_lock(&sc->sc_queue_mtx);
512 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
513 if (ggio->gctl_seq == 0 ||
514 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
515 G_GATE_LOGREQ(1, bp, "Request canceled.");
516 bioq_remove(&sc->sc_outqueue, bp);
518 * Be sure to put requests back onto incoming
519 * queue in the proper order.
522 bioq_insert_head(&sc->sc_inqueue, bp);
524 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
529 * If only one request was canceled, leave now.
531 if (ggio->gctl_seq != 0)
535 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
536 ggio->gctl_unit = sc->sc_unit;
537 mtx_unlock(&sc->sc_queue_mtx);
541 case G_GATE_CMD_START:
543 struct g_gate_ctl_io *ggio = (void *)addr;
545 G_GATE_CHECK_VERSION(ggio);
546 sc = g_gate_hold(ggio->gctl_unit, NULL);
551 mtx_lock(&sc->sc_queue_mtx);
552 bp = bioq_first(&sc->sc_inqueue);
555 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
556 ggio->gctl_error = ECANCELED;
557 mtx_unlock(&sc->sc_queue_mtx);
560 if (msleep(sc, &sc->sc_queue_mtx,
561 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
562 ggio->gctl_error = ECANCELED;
566 ggio->gctl_cmd = bp->bio_cmd;
567 if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) &&
568 bp->bio_length > ggio->gctl_length) {
569 mtx_unlock(&sc->sc_queue_mtx);
570 ggio->gctl_length = bp->bio_length;
571 ggio->gctl_error = ENOMEM;
574 bioq_remove(&sc->sc_inqueue, bp);
575 bioq_insert_tail(&sc->sc_outqueue, bp);
576 mtx_unlock(&sc->sc_queue_mtx);
578 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
579 ggio->gctl_offset = bp->bio_offset;
580 ggio->gctl_length = bp->bio_length;
582 switch (bp->bio_cmd) {
588 error = copyout(bp->bio_data, ggio->gctl_data,
591 mtx_lock(&sc->sc_queue_mtx);
592 bioq_remove(&sc->sc_outqueue, bp);
593 bioq_insert_head(&sc->sc_inqueue, bp);
594 mtx_unlock(&sc->sc_queue_mtx);
603 case G_GATE_CMD_DONE:
605 struct g_gate_ctl_io *ggio = (void *)addr;
607 G_GATE_CHECK_VERSION(ggio);
608 sc = g_gate_hold(ggio->gctl_unit, NULL);
612 mtx_lock(&sc->sc_queue_mtx);
613 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
614 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
618 bioq_remove(&sc->sc_outqueue, bp);
619 sc->sc_queue_count--;
621 mtx_unlock(&sc->sc_queue_mtx);
624 * Request was probably canceled.
628 if (ggio->gctl_error == EAGAIN) {
630 G_GATE_LOGREQ(1, bp, "Request desisted.");
631 mtx_lock(&sc->sc_queue_mtx);
632 sc->sc_queue_count++;
633 bioq_insert_head(&sc->sc_inqueue, bp);
635 mtx_unlock(&sc->sc_queue_mtx);
637 bp->bio_error = ggio->gctl_error;
638 if (bp->bio_error == 0) {
639 bp->bio_completed = bp->bio_length;
640 switch (bp->bio_cmd) {
642 error = copyin(ggio->gctl_data,
643 bp->bio_data, bp->bio_length);
645 bp->bio_error = error;
653 G_GATE_LOGREQ(2, bp, "Request done.");
654 g_io_deliver(bp, bp->bio_error);
668 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
673 g_gate_modevent(module_t mod, int type, void *data)
679 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
680 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
681 M_GATE, M_WAITOK | M_ZERO);
686 mtx_lock(&g_gate_units_lock);
687 if (g_gate_nunits > 0) {
688 mtx_unlock(&g_gate_units_lock);
692 mtx_unlock(&g_gate_units_lock);
693 mtx_destroy(&g_gate_units_lock);
695 destroy_dev(status_dev);
696 free(g_gate_units, M_GATE);
705 static moduledata_t g_gate_module = {
710 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
711 DECLARE_GEOM_CLASS(g_gate_class, g_gate);