2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Edward Tomasz Napierala <trasz@FreeBSD.org>
5 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/eventhandler.h>
46 #include <geom/geom.h>
47 #include <geom/geom_dbg.h>
48 #include <geom/mountver/g_mountver.h>
51 SYSCTL_DECL(_kern_geom);
52 static SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW,
53 0, "GEOM_MOUNTVER stuff");
54 static u_int g_mountver_debug = 0;
55 static u_int g_mountver_check_ident = 1;
56 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, debug, CTLFLAG_RW,
57 &g_mountver_debug, 0, "Debug level");
58 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, check_ident, CTLFLAG_RW,
59 &g_mountver_check_ident, 0, "Check disk ident when reattaching");
61 static eventhandler_tag g_mountver_pre_sync = NULL;
63 static void g_mountver_queue(struct bio *bp);
64 static void g_mountver_orphan(struct g_consumer *cp);
65 static void g_mountver_resize(struct g_consumer *cp);
66 static int g_mountver_destroy(struct g_geom *gp, boolean_t force);
67 static g_taste_t g_mountver_taste;
68 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp,
70 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
72 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent,
73 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
74 static void g_mountver_init(struct g_class *mp);
75 static void g_mountver_fini(struct g_class *mp);
77 struct g_class g_mountver_class = {
78 .name = G_MOUNTVER_CLASS_NAME,
80 .ctlreq = g_mountver_config,
81 .taste = g_mountver_taste,
82 .destroy_geom = g_mountver_destroy_geom,
83 .init = g_mountver_init,
84 .fini = g_mountver_fini
88 g_mountver_detach(void *arg, int flags __unused)
90 struct g_consumer *cp = arg;
93 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
94 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
99 g_mountver_done(struct bio *bp)
101 struct g_mountver_softc *sc;
103 struct g_consumer *cp;
108 if (bp->bio_error != ENXIO) {
114 * When the device goes away, it's possible that few requests
115 * will be completed with ENXIO before g_mountver_orphan()
116 * gets called. To work around that, we have to queue requests
117 * that failed with ENXIO, in order to send them later.
119 pbp = bp->bio_parent;
120 KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
121 ("parent request was for someone else"));
124 g_mountver_queue(pbp);
128 mtx_lock(&sc->sc_mtx);
129 if (--cp->index == 0 && sc->sc_orphaned)
130 g_post_event(g_mountver_detach, cp, M_NOWAIT, NULL);
131 mtx_unlock(&sc->sc_mtx);
135 * Send the BIO down. The function is called with sc_mtx held to cover
136 * the race with orphan, but drops it before external calls.
139 g_mountver_send(struct g_geom *gp, struct bio *bp)
141 struct g_mountver_softc *sc = gp->softc;
142 struct g_consumer *cp;
145 mtx_assert(&sc->sc_mtx, MA_OWNED);
146 cbp = g_clone_bio(bp);
148 mtx_unlock(&sc->sc_mtx);
149 g_io_deliver(bp, ENOMEM);
152 cp = LIST_FIRST(&gp->consumer);
154 mtx_unlock(&sc->sc_mtx);
156 cbp->bio_done = g_mountver_done;
157 g_io_request(cbp, cp);
161 g_mountver_queue(struct bio *bp)
163 struct g_mountver_softc *sc;
166 gp = bp->bio_to->geom;
169 mtx_lock(&sc->sc_mtx);
170 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
171 mtx_unlock(&sc->sc_mtx);
175 g_mountver_send_queued(struct g_geom *gp)
177 struct g_mountver_softc *sc;
182 mtx_lock(&sc->sc_mtx);
183 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL && !sc->sc_orphaned) {
184 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
185 G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
186 /* sc_mtx is dropped inside */
187 g_mountver_send(gp, bp);
188 mtx_lock(&sc->sc_mtx);
190 mtx_unlock(&sc->sc_mtx);
194 g_mountver_discard_queued(struct g_geom *gp)
196 struct g_mountver_softc *sc;
201 mtx_lock(&sc->sc_mtx);
202 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
203 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
204 mtx_unlock(&sc->sc_mtx);
205 G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
206 g_io_deliver(bp, ENXIO);
207 mtx_lock(&sc->sc_mtx);
209 mtx_unlock(&sc->sc_mtx);
213 g_mountver_start(struct bio *bp)
215 struct g_mountver_softc *sc;
218 gp = bp->bio_to->geom;
220 G_MOUNTVER_LOGREQ(bp, "Request received.");
223 * It is possible that some bios were returned with ENXIO, even though
224 * orphaning didn't happen yet. In that case, queue all subsequent
225 * requests in order to maintain ordering.
227 mtx_lock(&sc->sc_mtx);
228 if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
229 mtx_unlock(&sc->sc_mtx);
230 if (sc->sc_shutting_down) {
231 G_MOUNTVER_LOGREQ(bp, "Discarding request due to shutdown.");
232 g_io_deliver(bp, ENXIO);
235 G_MOUNTVER_LOGREQ(bp, "Queueing request.");
236 g_mountver_queue(bp);
237 if (!sc->sc_orphaned)
238 g_mountver_send_queued(gp);
240 G_MOUNTVER_LOGREQ(bp, "Sending request.");
241 /* sc_mtx is dropped inside */
242 g_mountver_send(gp, bp);
247 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
249 struct g_mountver_softc *sc;
251 struct g_consumer *cp;
256 cp = LIST_FIRST(&gp->consumer);
258 if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
260 KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
262 sc->sc_access_r += dr;
263 sc->sc_access_w += dw;
264 sc->sc_access_e += de;
269 return (g_access(cp, dr, dw, de));
273 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
275 struct g_mountver_softc *sc;
277 struct g_provider *newpp;
278 struct g_consumer *cp;
281 int identsize = DISK_IDENT_SIZE;
289 snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
290 LIST_FOREACH(gp, &mp->geom, geom) {
291 if (strcmp(gp->name, name) == 0) {
292 gctl_error(req, "Provider %s already exists.", name);
296 gp = g_new_geomf(mp, "%s", name);
297 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
298 mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF | MTX_RECURSE);
299 TAILQ_INIT(&sc->sc_queue);
300 sc->sc_provider_name = strdup(pp->name, M_GEOM);
302 gp->start = g_mountver_start;
303 gp->orphan = g_mountver_orphan;
304 gp->resize = g_mountver_resize;
305 gp->access = g_mountver_access;
306 gp->dumpconf = g_mountver_dumpconf;
308 newpp = g_new_providerf(gp, "%s", gp->name);
309 newpp->mediasize = pp->mediasize;
310 newpp->sectorsize = pp->sectorsize;
311 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
313 if ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0) {
314 G_MOUNTVER_DEBUG(0, "Unmapped supported for %s.", gp->name);
315 newpp->flags |= G_PF_ACCEPT_UNMAPPED;
317 G_MOUNTVER_DEBUG(0, "Unmapped unsupported for %s.", gp->name);
318 newpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
321 cp = g_new_consumer(gp);
322 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
323 error = g_attach(cp, pp);
325 gctl_error(req, "Cannot attach to provider %s.", pp->name);
328 error = g_access(cp, 1, 0, 0);
330 gctl_error(req, "Cannot access provider %s.", pp->name);
333 error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
334 g_access(cp, -1, 0, 0);
336 if (g_mountver_check_ident) {
337 gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
341 G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
342 sc->sc_ident[0] = '\0';
345 g_error_provider(newpp, 0);
346 G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
349 g_free(sc->sc_provider_name);
350 if (cp->provider != NULL)
352 g_destroy_consumer(cp);
353 g_destroy_provider(newpp);
360 g_mountver_destroy(struct g_geom *gp, boolean_t force)
362 struct g_mountver_softc *sc;
363 struct g_provider *pp;
366 if (gp->softc == NULL)
369 pp = LIST_FIRST(&gp->provider);
370 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
372 G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
373 "can't be definitely removed.", pp->name);
375 G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
376 pp->name, pp->acr, pp->acw, pp->ace);
380 G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
383 g_wither_provider(pp, ENXIO);
384 g_mountver_discard_queued(gp);
385 g_free(sc->sc_provider_name);
388 g_wither_geom(gp, ENXIO);
394 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
397 return (g_mountver_destroy(gp, 0));
401 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
403 struct g_provider *pp;
410 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
412 gctl_error(req, "No '%s' argument", "nargs");
416 gctl_error(req, "Missing device(s).");
419 for (i = 0; i < *nargs; i++) {
420 snprintf(param, sizeof(param), "arg%d", i);
421 name = gctl_get_asciiparam(req, param);
423 gctl_error(req, "No 'arg%d' argument", i);
426 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
427 name += strlen("/dev/");
428 pp = g_provider_by_name(name);
430 G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
431 gctl_error(req, "Provider %s is invalid.", name);
434 if (g_mountver_create(req, mp, pp) != 0)
439 static struct g_geom *
440 g_mountver_find_geom(struct g_class *mp, const char *name)
444 LIST_FOREACH(gp, &mp->geom, geom) {
445 if (strcmp(gp->name, name) == 0)
452 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
454 int *nargs, *force, error, i;
461 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
463 gctl_error(req, "No '%s' argument", "nargs");
467 gctl_error(req, "Missing device(s).");
470 force = gctl_get_paraml(req, "force", sizeof(*force));
472 gctl_error(req, "No 'force' argument");
476 for (i = 0; i < *nargs; i++) {
477 snprintf(param, sizeof(param), "arg%d", i);
478 name = gctl_get_asciiparam(req, param);
480 gctl_error(req, "No 'arg%d' argument", i);
483 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
484 name += strlen("/dev/");
485 gp = g_mountver_find_geom(mp, name);
487 G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
488 gctl_error(req, "Device %s is invalid.", name);
491 error = g_mountver_destroy(gp, *force);
493 gctl_error(req, "Cannot destroy device %s (error=%d).",
501 g_mountver_orphan(struct g_consumer *cp)
503 struct g_mountver_softc *sc;
508 sc = cp->geom->softc;
509 mtx_lock(&sc->sc_mtx);
511 done = (cp->index == 0);
512 mtx_unlock(&sc->sc_mtx);
514 g_mountver_detach(cp, 0);
515 G_MOUNTVER_DEBUG(0, "%s is offline. Mount verification in progress.", sc->sc_provider_name);
519 g_mountver_resize(struct g_consumer *cp)
522 struct g_provider *pp;
526 LIST_FOREACH(pp, &gp->provider, provider)
527 g_resize_provider(pp, cp->provider->mediasize);
531 g_mountver_ident_matches(struct g_geom *gp)
533 struct g_consumer *cp;
534 struct g_mountver_softc *sc;
535 char ident[DISK_IDENT_SIZE];
536 int error, identsize = DISK_IDENT_SIZE;
539 cp = LIST_FIRST(&gp->consumer);
541 if (g_mountver_check_ident == 0)
544 error = g_access(cp, 1, 0, 0);
546 G_MOUNTVER_DEBUG(0, "Cannot access %s; "
547 "not attaching; error = %d.", gp->name, error);
550 error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
551 g_access(cp, -1, 0, 0);
553 G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
554 "not attaching; error = %d.", gp->name, error);
557 if (strcmp(ident, sc->sc_ident) != 0) {
558 G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
559 "from expected \"%s\", not attaching.", gp->name, ident,
567 static struct g_geom *
568 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
570 struct g_mountver_softc *sc;
571 struct g_consumer *cp;
576 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
577 G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
580 * Let's check if device already exists.
582 LIST_FOREACH(gp, &mp->geom, geom) {
587 /* Already attached? */
588 if (pp == LIST_FIRST(&gp->provider))
591 if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
597 cp = LIST_FIRST(&gp->consumer);
599 error = g_mountver_ident_matches(gp);
604 if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
605 error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
607 G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
613 g_mountver_send_queued(gp);
614 G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
620 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
626 version = gctl_get_paraml(req, "version", sizeof(*version));
627 if (version == NULL) {
628 gctl_error(req, "No '%s' argument.", "version");
631 if (*version != G_MOUNTVER_VERSION) {
632 gctl_error(req, "Userland and kernel parts are out of sync.");
636 if (strcmp(verb, "create") == 0) {
637 g_mountver_ctl_create(req, mp);
639 } else if (strcmp(verb, "destroy") == 0) {
640 g_mountver_ctl_destroy(req, mp);
644 gctl_error(req, "Unknown verb.");
648 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
649 struct g_consumer *cp, struct g_provider *pp)
651 struct g_mountver_softc *sc;
653 if (pp != NULL || cp != NULL)
657 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
658 sc->sc_orphaned ? "OFFLINE" : "ONLINE");
659 sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
660 sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
664 g_mountver_shutdown_pre_sync(void *arg, int howto)
666 struct g_mountver_softc *sc;
668 struct g_geom *gp, *gp2;
672 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
673 if (gp->softc == NULL)
676 sc->sc_shutting_down = 1;
678 g_mountver_destroy(gp, 1);
684 g_mountver_init(struct g_class *mp)
687 g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
688 g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
689 if (g_mountver_pre_sync == NULL)
690 G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
694 g_mountver_fini(struct g_class *mp)
697 if (g_mountver_pre_sync != NULL)
698 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
701 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);
702 MODULE_VERSION(geom_mountver, 0);