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>
48 #include <sys/sysctl.h>
49 #include <sys/signalvar.h>
51 #include <machine/atomic.h>
53 #include <geom/geom.h>
54 #include <geom/gate/g_gate.h>
56 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
58 SYSCTL_DECL(_kern_geom);
59 SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, "GEOM_GATE stuff");
60 static int g_gate_debug = 0;
61 TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug);
62 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0,
64 static u_int g_gate_maxunits = 256;
65 TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits);
66 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
67 &g_gate_maxunits, 0, "Maximum number of ggate devices");
69 struct g_class g_gate_class = {
70 .name = G_GATE_CLASS_NAME,
74 static struct cdev *status_dev;
75 static d_ioctl_t g_gate_ioctl;
76 static struct cdevsw g_gate_cdevsw = {
77 .d_version = D_VERSION,
78 .d_ioctl = g_gate_ioctl,
79 .d_name = G_GATE_CTL_NAME
83 static struct g_gate_softc **g_gate_units;
84 static u_int g_gate_nunits;
85 static struct mtx g_gate_units_lock;
88 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
90 struct g_provider *pp;
95 mtx_assert(&g_gate_units_lock, MA_OWNED);
97 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
98 mtx_unlock(&g_gate_units_lock);
101 mtx_unlock(&g_gate_units_lock);
102 mtx_lock(&sc->sc_queue_mtx);
103 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
104 sc->sc_flags |= G_GATE_FLAG_DESTROY;
106 mtx_unlock(&sc->sc_queue_mtx);
108 pp->flags |= G_PF_WITHER;
109 g_orphan_provider(pp, ENXIO);
110 callout_drain(&sc->sc_callout);
111 mtx_lock(&sc->sc_queue_mtx);
112 while ((bp = bioq_first(&sc->sc_inqueue)) != NULL) {
113 bioq_remove(&sc->sc_inqueue, bp);
114 sc->sc_queue_count--;
115 G_GATE_LOGREQ(1, bp, "Request canceled.");
116 g_io_deliver(bp, ENXIO);
118 while ((bp = bioq_first(&sc->sc_outqueue)) != NULL) {
119 bioq_remove(&sc->sc_outqueue, bp);
120 sc->sc_queue_count--;
121 G_GATE_LOGREQ(1, bp, "Request canceled.");
122 g_io_deliver(bp, ENXIO);
124 mtx_unlock(&sc->sc_queue_mtx);
126 mtx_lock(&g_gate_units_lock);
127 /* One reference is ours. */
129 while (sc->sc_ref > 0)
130 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
131 g_gate_units[sc->sc_unit] = NULL;
132 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
134 mtx_unlock(&g_gate_units_lock);
135 mtx_destroy(&sc->sc_queue_mtx);
137 G_GATE_DEBUG(0, "Device %s destroyed.", gp->name);
139 g_wither_geom(gp, ENXIO);
140 sc->sc_provider = NULL;
146 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
148 struct g_gate_softc *sc;
150 if (dr <= 0 && dw <= 0 && de <= 0)
152 sc = pp->geom->softc;
153 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
155 /* XXX: Hack to allow read-only mounts. */
157 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
160 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
166 g_gate_start(struct bio *bp)
168 struct g_gate_softc *sc;
170 sc = bp->bio_to->geom->softc;
171 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
172 g_io_deliver(bp, ENXIO);
175 G_GATE_LOGREQ(2, bp, "Request received.");
176 switch (bp->bio_cmd) {
181 /* XXX: Hack to allow read-only mounts. */
182 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
183 g_io_deliver(bp, EPERM);
189 G_GATE_LOGREQ(2, bp, "Ignoring request.");
190 g_io_deliver(bp, EOPNOTSUPP);
194 mtx_lock(&sc->sc_queue_mtx);
195 if (sc->sc_queue_count > sc->sc_queue_size) {
196 mtx_unlock(&sc->sc_queue_mtx);
197 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
198 g_io_deliver(bp, ENOMEM);
202 bp->bio_driver1 = (void *)sc->sc_seq;
204 sc->sc_queue_count++;
206 bioq_insert_tail(&sc->sc_inqueue, bp);
209 mtx_unlock(&sc->sc_queue_mtx);
212 static struct g_gate_softc *
213 g_gate_hold(int unit, const char *name)
215 struct g_gate_softc *sc = NULL;
217 mtx_lock(&g_gate_units_lock);
218 if (unit >= 0 && unit < g_gate_maxunits)
219 sc = g_gate_units[unit];
220 else if (unit == G_GATE_NAME_GIVEN) {
221 KASSERT(name != NULL, ("name is NULL"));
222 for (unit = 0; unit < g_gate_maxunits; unit++) {
223 if (g_gate_units[unit] == NULL)
226 g_gate_units[unit]->sc_provider->name) != 0) {
229 sc = g_gate_units[unit];
235 mtx_unlock(&g_gate_units_lock);
240 g_gate_release(struct g_gate_softc *sc)
243 g_topology_assert_not();
244 mtx_lock(&g_gate_units_lock);
246 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
247 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
249 mtx_unlock(&g_gate_units_lock);
253 g_gate_getunit(int unit, int *errorp)
256 mtx_assert(&g_gate_units_lock, MA_OWNED);
258 if (unit >= g_gate_maxunits)
260 else if (g_gate_units[unit] == NULL)
265 for (unit = 0; unit < g_gate_maxunits; unit++) {
266 if (g_gate_units[unit] == NULL)
275 g_gate_guard(void *arg)
277 struct g_gate_softc *sc;
278 struct bintime curtime;
279 struct bio *bp, *bp2;
283 g_gate_hold(sc->sc_unit, NULL);
284 mtx_lock(&sc->sc_queue_mtx);
285 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
286 if (curtime.sec - bp->bio_t0.sec < 5)
288 bioq_remove(&sc->sc_inqueue, bp);
289 sc->sc_queue_count--;
290 G_GATE_LOGREQ(1, bp, "Request timeout.");
291 g_io_deliver(bp, EIO);
293 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
294 if (curtime.sec - bp->bio_t0.sec < 5)
296 bioq_remove(&sc->sc_outqueue, bp);
297 sc->sc_queue_count--;
298 G_GATE_LOGREQ(1, bp, "Request timeout.");
299 g_io_deliver(bp, EIO);
301 mtx_unlock(&sc->sc_queue_mtx);
302 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
303 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
310 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
311 struct g_consumer *cp, struct g_provider *pp)
313 struct g_gate_softc *sc;
316 if (sc == NULL || pp != NULL || cp != NULL)
318 g_gate_hold(sc->sc_unit, NULL);
319 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
320 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
321 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
322 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
325 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
328 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
329 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
330 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
332 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
334 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
335 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
342 g_gate_create(struct g_gate_ctl_create *ggio)
344 struct g_gate_softc *sc;
346 struct g_provider *pp;
350 if (ggio->gctl_mediasize == 0) {
351 G_GATE_DEBUG(1, "Invalid media size.");
354 if (ggio->gctl_sectorsize > 0 && !powerof2(ggio->gctl_sectorsize)) {
355 G_GATE_DEBUG(1, "Invalid sector size.");
358 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
359 G_GATE_DEBUG(1, "Invalid media size.");
362 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
363 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
364 G_GATE_DEBUG(1, "Invalid flags.");
367 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
368 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
369 ggio->gctl_unit < 0) {
370 G_GATE_DEBUG(1, "Invalid unit number.");
373 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
374 ggio->gctl_name[0] == '\0') {
375 G_GATE_DEBUG(1, "No device name.");
379 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
380 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
381 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
383 bioq_init(&sc->sc_inqueue);
384 bioq_init(&sc->sc_outqueue);
385 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
386 sc->sc_queue_count = 0;
387 sc->sc_queue_size = ggio->gctl_maxcount;
388 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
389 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
390 sc->sc_timeout = ggio->gctl_timeout;
391 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
392 mtx_lock(&g_gate_units_lock);
393 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
394 if (sc->sc_unit < 0) {
395 mtx_unlock(&g_gate_units_lock);
396 mtx_destroy(&sc->sc_queue_mtx);
400 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
401 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
403 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
406 /* Check for name collision. */
407 for (unit = 0; unit < g_gate_maxunits; unit++) {
408 if (g_gate_units[unit] == NULL)
410 if (strcmp(name, g_gate_units[unit]->sc_provider->name) != 0)
412 mtx_unlock(&g_gate_units_lock);
413 mtx_destroy(&sc->sc_queue_mtx);
417 g_gate_units[sc->sc_unit] = sc;
419 mtx_unlock(&g_gate_units_lock);
421 ggio->gctl_unit = sc->sc_unit;
424 gp = g_new_geomf(&g_gate_class, "%s", name);
425 gp->start = g_gate_start;
426 gp->access = g_gate_access;
427 gp->dumpconf = g_gate_dumpconf;
429 pp = g_new_providerf(gp, "%s", name);
430 pp->mediasize = ggio->gctl_mediasize;
431 pp->sectorsize = ggio->gctl_sectorsize;
432 sc->sc_provider = pp;
433 g_error_provider(pp, 0);
436 if (sc->sc_timeout > 0) {
437 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
443 #define G_GATE_CHECK_VERSION(ggio) do { \
444 if ((ggio)->gctl_version != G_GATE_VERSION) { \
445 printf("Version mismatch %d != %d.\n", \
446 ggio->gctl_version, G_GATE_VERSION); \
451 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
453 struct g_gate_softc *sc;
457 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
461 case G_GATE_CMD_CREATE:
463 struct g_gate_ctl_create *ggio = (void *)addr;
465 G_GATE_CHECK_VERSION(ggio);
466 error = g_gate_create(ggio);
468 * Reset TDP_GEOM flag.
469 * There are pending events for sure, because we just created
470 * new provider and other classes want to taste it, but we
471 * cannot answer on I/O requests until we're here.
473 td->td_pflags &= ~TDP_GEOM;
476 case G_GATE_CMD_DESTROY:
478 struct g_gate_ctl_destroy *ggio = (void *)addr;
480 G_GATE_CHECK_VERSION(ggio);
481 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
485 mtx_lock(&g_gate_units_lock);
486 error = g_gate_destroy(sc, ggio->gctl_force);
492 case G_GATE_CMD_CANCEL:
494 struct g_gate_ctl_cancel *ggio = (void *)addr;
495 struct bio *tbp, *lbp;
497 G_GATE_CHECK_VERSION(ggio);
498 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
502 mtx_lock(&sc->sc_queue_mtx);
503 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
504 if (ggio->gctl_seq == 0 ||
505 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
506 G_GATE_LOGREQ(1, bp, "Request canceled.");
507 bioq_remove(&sc->sc_outqueue, bp);
509 * Be sure to put requests back onto incoming
510 * queue in the proper order.
513 bioq_insert_head(&sc->sc_inqueue, bp);
515 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
520 * If only one request was canceled, leave now.
522 if (ggio->gctl_seq != 0)
526 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
527 ggio->gctl_unit = sc->sc_unit;
528 mtx_unlock(&sc->sc_queue_mtx);
532 case G_GATE_CMD_START:
534 struct g_gate_ctl_io *ggio = (void *)addr;
536 G_GATE_CHECK_VERSION(ggio);
537 sc = g_gate_hold(ggio->gctl_unit, NULL);
542 mtx_lock(&sc->sc_queue_mtx);
543 bp = bioq_first(&sc->sc_inqueue);
546 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
547 ggio->gctl_error = ECANCELED;
548 mtx_unlock(&sc->sc_queue_mtx);
551 if (msleep(sc, &sc->sc_queue_mtx,
552 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
553 ggio->gctl_error = ECANCELED;
557 ggio->gctl_cmd = bp->bio_cmd;
558 if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) &&
559 bp->bio_length > ggio->gctl_length) {
560 mtx_unlock(&sc->sc_queue_mtx);
561 ggio->gctl_length = bp->bio_length;
562 ggio->gctl_error = ENOMEM;
565 bioq_remove(&sc->sc_inqueue, bp);
566 bioq_insert_tail(&sc->sc_outqueue, bp);
567 mtx_unlock(&sc->sc_queue_mtx);
569 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
570 ggio->gctl_offset = bp->bio_offset;
571 ggio->gctl_length = bp->bio_length;
573 switch (bp->bio_cmd) {
578 error = copyout(bp->bio_data, ggio->gctl_data,
581 mtx_lock(&sc->sc_queue_mtx);
582 bioq_remove(&sc->sc_outqueue, bp);
583 bioq_insert_head(&sc->sc_inqueue, bp);
584 mtx_unlock(&sc->sc_queue_mtx);
593 case G_GATE_CMD_DONE:
595 struct g_gate_ctl_io *ggio = (void *)addr;
597 G_GATE_CHECK_VERSION(ggio);
598 sc = g_gate_hold(ggio->gctl_unit, NULL);
602 mtx_lock(&sc->sc_queue_mtx);
603 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
604 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
608 bioq_remove(&sc->sc_outqueue, bp);
609 sc->sc_queue_count--;
611 mtx_unlock(&sc->sc_queue_mtx);
614 * Request was probably canceled.
618 if (ggio->gctl_error == EAGAIN) {
620 G_GATE_LOGREQ(1, bp, "Request desisted.");
621 mtx_lock(&sc->sc_queue_mtx);
622 sc->sc_queue_count++;
623 bioq_insert_head(&sc->sc_inqueue, bp);
625 mtx_unlock(&sc->sc_queue_mtx);
627 bp->bio_error = ggio->gctl_error;
628 if (bp->bio_error == 0) {
629 bp->bio_completed = bp->bio_length;
630 switch (bp->bio_cmd) {
632 error = copyin(ggio->gctl_data,
633 bp->bio_data, bp->bio_length);
635 bp->bio_error = error;
642 G_GATE_LOGREQ(2, bp, "Request done.");
643 g_io_deliver(bp, bp->bio_error);
657 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
662 g_gate_modevent(module_t mod, int type, void *data)
668 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
669 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
670 M_GATE, M_WAITOK | M_ZERO);
675 mtx_lock(&g_gate_units_lock);
676 if (g_gate_nunits > 0) {
677 mtx_unlock(&g_gate_units_lock);
681 mtx_unlock(&g_gate_units_lock);
682 mtx_destroy(&g_gate_units_lock);
684 destroy_dev(status_dev);
685 free(g_gate_units, M_GATE);
694 static moduledata_t g_gate_module = {
699 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
700 DECLARE_GEOM_CLASS(g_gate_class, g_gate);