]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/gate/g_gate.c
Update OpenZFS to 2.0.0-rc3-gfc5966
[FreeBSD/FreeBSD.git] / sys / geom / gate / g_gate.c
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 | CTLFLAG_MPSAFE, 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 static struct g_gate_softc **g_gate_units;
88 static u_int g_gate_nunits;
89 static struct mtx g_gate_units_lock;
90
91 static void
92 g_gate_detach(void *arg, int flags __unused)
93 {
94         struct g_consumer *cp = arg;
95
96         g_topology_assert();
97         G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
98             cp->provider->name);
99         (void)g_access(cp, -1, 0, 0);
100         g_detach(cp);
101         g_destroy_consumer(cp);
102 }
103
104 static int
105 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
106 {
107         struct bio_queue_head queue;
108         struct g_provider *pp;
109         struct g_consumer *cp;
110         struct g_geom *gp;
111         struct bio *bp;
112
113         g_topology_assert();
114         mtx_assert(&g_gate_units_lock, MA_OWNED);
115         pp = sc->sc_provider;
116         if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
117                 mtx_unlock(&g_gate_units_lock);
118                 return (EBUSY);
119         }
120         mtx_unlock(&g_gate_units_lock);
121         mtx_lock(&sc->sc_queue_mtx);
122         if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
123                 sc->sc_flags |= G_GATE_FLAG_DESTROY;
124         wakeup(sc);
125         mtx_unlock(&sc->sc_queue_mtx);
126         gp = pp->geom;
127         g_wither_provider(pp, ENXIO);
128         callout_drain(&sc->sc_callout);
129         bioq_init(&queue);
130         mtx_lock(&sc->sc_queue_mtx);
131         while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) {
132                 sc->sc_queue_count--;
133                 bioq_insert_tail(&queue, bp);
134         }
135         while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) {
136                 sc->sc_queue_count--;
137                 bioq_insert_tail(&queue, bp);
138         }
139         mtx_unlock(&sc->sc_queue_mtx);
140         g_topology_unlock();
141         while ((bp = bioq_takefirst(&queue)) != NULL) {
142                 G_GATE_LOGREQ(1, bp, "Request canceled.");
143                 g_io_deliver(bp, ENXIO);
144         }
145         mtx_lock(&g_gate_units_lock);
146         /* One reference is ours. */
147         sc->sc_ref--;
148         while (sc->sc_ref > 0)
149                 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
150         g_gate_units[sc->sc_unit] = NULL;
151         KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
152         g_gate_nunits--;
153         mtx_unlock(&g_gate_units_lock);
154         mtx_destroy(&sc->sc_queue_mtx);
155         mtx_destroy(&sc->sc_read_mtx);
156         g_topology_lock();
157         if ((cp = sc->sc_readcons) != NULL) {
158                 sc->sc_readcons = NULL;
159                 (void)g_access(cp, -1, 0, 0);
160                 g_detach(cp);
161                 g_destroy_consumer(cp);
162         }
163         G_GATE_DEBUG(1, "Device %s destroyed.", gp->name);
164         gp->softc = NULL;
165         g_wither_geom(gp, ENXIO);
166         sc->sc_provider = NULL;
167         free(sc, M_GATE);
168         return (0);
169 }
170
171 static int
172 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
173 {
174         struct g_gate_softc *sc;
175
176         if (dr <= 0 && dw <= 0 && de <= 0)
177                 return (0);
178         sc = pp->geom->softc;
179         if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
180                 return (ENXIO);
181         /* XXX: Hack to allow read-only mounts. */
182 #if 0
183         if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
184                 return (EPERM);
185 #endif
186         if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
187                 return (EPERM);
188         return (0);
189 }
190
191 static void
192 g_gate_queue_io(struct bio *bp)
193 {
194         struct g_gate_softc *sc;
195
196         sc = bp->bio_to->geom->softc;
197         if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
198                 g_io_deliver(bp, ENXIO);
199                 return;
200         }
201
202         mtx_lock(&sc->sc_queue_mtx);
203
204         if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) {
205                 mtx_unlock(&sc->sc_queue_mtx);
206                 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
207                 g_io_deliver(bp, ENOMEM);
208                 return;
209         }
210
211         bp->bio_driver1 = (void *)sc->sc_seq;
212         sc->sc_seq++;
213         sc->sc_queue_count++;
214
215         bioq_insert_tail(&sc->sc_inqueue, bp);
216         wakeup(sc);
217
218         mtx_unlock(&sc->sc_queue_mtx);
219 }
220
221 static void
222 g_gate_done(struct bio *cbp)
223 {
224         struct g_gate_softc *sc;
225         struct bio *pbp;
226         struct g_consumer *cp;
227
228         cp = cbp->bio_from;
229         pbp = cbp->bio_parent;
230         if (cbp->bio_error == 0) {
231                 pbp->bio_completed = cbp->bio_completed;
232                 g_destroy_bio(cbp);
233                 pbp->bio_inbed++;
234                 g_io_deliver(pbp, 0);
235         } else {
236                 /* If direct read failed, pass it through userland daemon. */
237                 g_destroy_bio(cbp);
238                 pbp->bio_children--;
239                 g_gate_queue_io(pbp);
240         }
241
242         sc = cp->geom->softc;
243         mtx_lock(&sc->sc_read_mtx);
244         if (--cp->index == 0 && sc->sc_readcons != cp)
245                 g_post_event(g_gate_detach, cp, M_NOWAIT, NULL);
246         mtx_unlock(&sc->sc_read_mtx);
247 }
248
249 static void
250 g_gate_start(struct bio *pbp)
251 {
252         struct g_gate_softc *sc;
253         struct g_consumer *cp;
254         struct bio *cbp;
255
256         sc = pbp->bio_to->geom->softc;
257         if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
258                 g_io_deliver(pbp, ENXIO);
259                 return;
260         }
261         G_GATE_LOGREQ(2, pbp, "Request received.");
262         switch (pbp->bio_cmd) {
263         case BIO_READ:
264                 if (sc->sc_readcons == NULL)
265                         break;
266                 cbp = g_clone_bio(pbp);
267                 if (cbp == NULL) {
268                         g_io_deliver(pbp, ENOMEM);
269                         return;
270                 }
271                 mtx_lock(&sc->sc_read_mtx);
272                 if ((cp = sc->sc_readcons) == NULL) {
273                         mtx_unlock(&sc->sc_read_mtx);
274                         g_destroy_bio(cbp);
275                         pbp->bio_children--;
276                         break;
277                 }
278                 cp->index++;
279                 cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
280                 mtx_unlock(&sc->sc_read_mtx);
281                 cbp->bio_done = g_gate_done;
282                 g_io_request(cbp, cp);
283                 return;
284         case BIO_DELETE:
285         case BIO_WRITE:
286         case BIO_FLUSH:
287         case BIO_SPEEDUP:
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                 case BIO_SPEEDUP:
875                         break;
876                 case BIO_WRITE:
877                         error = copyout(bp->bio_data, ggio->gctl_data,
878                             bp->bio_length);
879                         if (error != 0) {
880                                 mtx_lock(&sc->sc_queue_mtx);
881                                 bioq_remove(&sc->sc_outqueue, bp);
882                                 bioq_insert_head(&sc->sc_inqueue, bp);
883                                 mtx_unlock(&sc->sc_queue_mtx);
884                                 goto start_end;
885                         }
886                         break;
887                 }
888 start_end:
889                 g_gate_release(sc);
890                 return (error);
891             }
892         case G_GATE_CMD_DONE:
893             {
894                 struct g_gate_ctl_io *ggio = (void *)addr;
895
896                 G_GATE_CHECK_VERSION(ggio);
897                 sc = g_gate_hold(ggio->gctl_unit, NULL);
898                 if (sc == NULL)
899                         return (ENOENT);
900                 error = 0;
901                 mtx_lock(&sc->sc_queue_mtx);
902                 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
903                         if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
904                                 break;
905                 }
906                 if (bp != NULL) {
907                         bioq_remove(&sc->sc_outqueue, bp);
908                         sc->sc_queue_count--;
909                 }
910                 mtx_unlock(&sc->sc_queue_mtx);
911                 if (bp == NULL) {
912                         /*
913                          * Request was probably canceled.
914                          */
915                         goto done_end;
916                 }
917                 if (ggio->gctl_error == EAGAIN) {
918                         bp->bio_error = 0;
919                         G_GATE_LOGREQ(1, bp, "Request desisted.");
920                         mtx_lock(&sc->sc_queue_mtx);
921                         sc->sc_queue_count++;
922                         bioq_insert_head(&sc->sc_inqueue, bp);
923                         wakeup(sc);
924                         mtx_unlock(&sc->sc_queue_mtx);
925                 } else {
926                         bp->bio_error = ggio->gctl_error;
927                         if (bp->bio_error == 0) {
928                                 bp->bio_completed = bp->bio_length;
929                                 switch (bp->bio_cmd) {
930                                 case BIO_READ:
931                                         error = copyin(ggio->gctl_data,
932                                             bp->bio_data, bp->bio_length);
933                                         if (error != 0)
934                                                 bp->bio_error = error;
935                                         break;
936                                 case BIO_DELETE:
937                                 case BIO_WRITE:
938                                 case BIO_FLUSH:
939                                 case BIO_SPEEDUP:
940                                         break;
941                                 }
942                         }
943                         G_GATE_LOGREQ(2, bp, "Request done.");
944                         g_io_deliver(bp, bp->bio_error);
945                 }
946 done_end:
947                 g_gate_release(sc);
948                 return (error);
949             }
950         }
951         return (ENOIOCTL);
952 }
953
954 static void
955 g_gate_device(void)
956 {
957
958         status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
959             G_GATE_CTL_NAME);
960 }
961
962 static int
963 g_gate_modevent(module_t mod, int type, void *data)
964 {
965         int error = 0;
966
967         switch (type) {
968         case MOD_LOAD:
969                 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
970                 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
971                     M_GATE, M_WAITOK | M_ZERO);
972                 g_gate_nunits = 0;
973                 g_gate_device();
974                 break;
975         case MOD_UNLOAD:
976                 mtx_lock(&g_gate_units_lock);
977                 if (g_gate_nunits > 0) {
978                         mtx_unlock(&g_gate_units_lock);
979                         error = EBUSY;
980                         break;
981                 }
982                 mtx_unlock(&g_gate_units_lock);
983                 mtx_destroy(&g_gate_units_lock);
984                 if (status_dev != NULL)
985                         destroy_dev(status_dev);
986                 free(g_gate_units, M_GATE);
987                 break;
988         default:
989                 return (EOPNOTSUPP);
990                 break;
991         }
992
993         return (error);
994 }
995 static moduledata_t g_gate_module = {
996         G_GATE_MOD_NAME,
997         g_gate_modevent,
998         NULL
999 };
1000 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1001 DECLARE_GEOM_CLASS(g_gate_class, g_gate);
1002 MODULE_VERSION(geom_gate, 0);