2 * Copyright (c) 2010 Edward Tomasz Napierala <trasz@FreeBSD.org>
3 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/eventhandler.h>
44 #include <geom/geom.h>
45 #include <geom/mountver/g_mountver.h>
48 SYSCTL_DECL(_kern_geom);
49 static SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW,
50 0, "GEOM_MOUNTVER stuff");
51 static u_int g_mountver_debug = 0;
52 static u_int g_mountver_check_ident = 1;
53 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, debug, CTLFLAG_RW,
54 &g_mountver_debug, 0, "Debug level");
55 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, check_ident, CTLFLAG_RW,
56 &g_mountver_check_ident, 0, "Check disk ident when reattaching");
58 static eventhandler_tag g_mountver_pre_sync = NULL;
60 static void g_mountver_queue(struct bio *bp);
61 static void g_mountver_orphan(struct g_consumer *cp);
62 static void g_mountver_resize(struct g_consumer *cp);
63 static int g_mountver_destroy(struct g_geom *gp, boolean_t force);
64 static g_taste_t g_mountver_taste;
65 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp,
67 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
69 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent,
70 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
71 static void g_mountver_init(struct g_class *mp);
72 static void g_mountver_fini(struct g_class *mp);
74 struct g_class g_mountver_class = {
75 .name = G_MOUNTVER_CLASS_NAME,
77 .ctlreq = g_mountver_config,
78 .taste = g_mountver_taste,
79 .destroy_geom = g_mountver_destroy_geom,
80 .init = g_mountver_init,
81 .fini = g_mountver_fini
85 g_mountver_done(struct bio *bp)
90 if (bp->bio_error != ENXIO) {
96 * When the device goes away, it's possible that few requests
97 * will be completed with ENXIO before g_mountver_orphan()
98 * gets called. To work around that, we have to queue requests
99 * that failed with ENXIO, in order to send them later.
101 gp = bp->bio_from->geom;
103 pbp = bp->bio_parent;
104 KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
105 ("parent request was for someone else"));
108 g_mountver_queue(pbp);
112 g_mountver_send(struct bio *bp)
117 gp = bp->bio_to->geom;
119 cbp = g_clone_bio(bp);
121 g_io_deliver(bp, ENOMEM);
125 cbp->bio_done = g_mountver_done;
126 g_io_request(cbp, LIST_FIRST(&gp->consumer));
130 g_mountver_queue(struct bio *bp)
132 struct g_mountver_softc *sc;
135 gp = bp->bio_to->geom;
138 mtx_lock(&sc->sc_mtx);
139 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
140 mtx_unlock(&sc->sc_mtx);
144 g_mountver_send_queued(struct g_geom *gp)
146 struct g_mountver_softc *sc;
151 mtx_lock(&sc->sc_mtx);
152 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
153 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
154 G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
157 mtx_unlock(&sc->sc_mtx);
161 g_mountver_discard_queued(struct g_geom *gp)
163 struct g_mountver_softc *sc;
168 mtx_lock(&sc->sc_mtx);
169 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
170 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
171 G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
172 g_io_deliver(bp, ENXIO);
174 mtx_unlock(&sc->sc_mtx);
178 g_mountver_start(struct bio *bp)
180 struct g_mountver_softc *sc;
183 gp = bp->bio_to->geom;
185 G_MOUNTVER_LOGREQ(bp, "Request received.");
188 * It is possible that some bios were returned with ENXIO, even though
189 * orphaning didn't happen yet. In that case, queue all subsequent
190 * requests in order to maintain ordering.
192 if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
193 G_MOUNTVER_LOGREQ(bp, "Queueing request.");
194 g_mountver_queue(bp);
195 if (!sc->sc_orphaned)
196 g_mountver_send_queued(gp);
198 G_MOUNTVER_LOGREQ(bp, "Sending request.");
204 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
206 struct g_mountver_softc *sc;
208 struct g_consumer *cp;
213 cp = LIST_FIRST(&gp->consumer);
215 if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
217 KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
219 sc->sc_access_r += dr;
220 sc->sc_access_w += dw;
221 sc->sc_access_e += de;
226 return (g_access(cp, dr, dw, de));
230 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
232 struct g_mountver_softc *sc;
234 struct g_provider *newpp;
235 struct g_consumer *cp;
238 int identsize = DISK_IDENT_SIZE;
246 snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
247 LIST_FOREACH(gp, &mp->geom, geom) {
248 if (strcmp(gp->name, name) == 0) {
249 gctl_error(req, "Provider %s already exists.", name);
253 gp = g_new_geomf(mp, "%s", name);
254 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
255 mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF);
256 TAILQ_INIT(&sc->sc_queue);
257 sc->sc_provider_name = strdup(pp->name, M_GEOM);
259 gp->start = g_mountver_start;
260 gp->orphan = g_mountver_orphan;
261 gp->resize = g_mountver_resize;
262 gp->access = g_mountver_access;
263 gp->dumpconf = g_mountver_dumpconf;
265 newpp = g_new_providerf(gp, "%s", gp->name);
266 newpp->mediasize = pp->mediasize;
267 newpp->sectorsize = pp->sectorsize;
269 cp = g_new_consumer(gp);
270 error = g_attach(cp, pp);
272 gctl_error(req, "Cannot attach to provider %s.", pp->name);
275 error = g_access(cp, 1, 0, 0);
277 gctl_error(req, "Cannot access provider %s.", pp->name);
280 error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
281 g_access(cp, -1, 0, 0);
283 if (g_mountver_check_ident) {
284 gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
288 G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
289 sc->sc_ident[0] = '\0';
292 g_error_provider(newpp, 0);
293 G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
296 g_free(sc->sc_provider_name);
297 if (cp->provider != NULL)
299 g_destroy_consumer(cp);
300 g_destroy_provider(newpp);
307 g_mountver_destroy(struct g_geom *gp, boolean_t force)
309 struct g_mountver_softc *sc;
310 struct g_provider *pp;
313 if (gp->softc == NULL)
316 pp = LIST_FIRST(&gp->provider);
317 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
319 G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
320 "can't be definitely removed.", pp->name);
322 G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
323 pp->name, pp->acr, pp->acw, pp->ace);
327 G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
330 g_orphan_provider(pp, ENXIO);
331 g_mountver_discard_queued(gp);
332 g_free(sc->sc_provider_name);
335 g_wither_geom(gp, ENXIO);
341 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
344 return (g_mountver_destroy(gp, 0));
348 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
350 struct g_provider *pp;
357 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
359 gctl_error(req, "No '%s' argument", "nargs");
363 gctl_error(req, "Missing device(s).");
366 for (i = 0; i < *nargs; i++) {
367 snprintf(param, sizeof(param), "arg%d", i);
368 name = gctl_get_asciiparam(req, param);
370 gctl_error(req, "No 'arg%d' argument", i);
373 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
374 name += strlen("/dev/");
375 pp = g_provider_by_name(name);
377 G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
378 gctl_error(req, "Provider %s is invalid.", name);
381 if (g_mountver_create(req, mp, pp) != 0)
386 static struct g_geom *
387 g_mountver_find_geom(struct g_class *mp, const char *name)
391 LIST_FOREACH(gp, &mp->geom, geom) {
392 if (strcmp(gp->name, name) == 0)
399 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
401 int *nargs, *force, error, i;
408 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
410 gctl_error(req, "No '%s' argument", "nargs");
414 gctl_error(req, "Missing device(s).");
417 force = gctl_get_paraml(req, "force", sizeof(*force));
419 gctl_error(req, "No 'force' argument");
423 for (i = 0; i < *nargs; i++) {
424 snprintf(param, sizeof(param), "arg%d", i);
425 name = gctl_get_asciiparam(req, param);
427 gctl_error(req, "No 'arg%d' argument", i);
430 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
431 name += strlen("/dev/");
432 gp = g_mountver_find_geom(mp, name);
434 G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
435 gctl_error(req, "Device %s is invalid.", name);
438 error = g_mountver_destroy(gp, *force);
440 gctl_error(req, "Cannot destroy device %s (error=%d).",
448 g_mountver_orphan(struct g_consumer *cp)
450 struct g_mountver_softc *sc;
454 sc = cp->geom->softc;
456 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
457 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
459 G_MOUNTVER_DEBUG(0, "%s is offline. Mount verification in progress.", sc->sc_provider_name);
463 g_mountver_resize(struct g_consumer *cp)
466 struct g_provider *pp;
470 LIST_FOREACH(pp, &gp->provider, provider)
471 g_resize_provider(pp, cp->provider->mediasize);
475 g_mountver_ident_matches(struct g_geom *gp)
477 struct g_consumer *cp;
478 struct g_mountver_softc *sc;
479 char ident[DISK_IDENT_SIZE];
480 int error, identsize = DISK_IDENT_SIZE;
483 cp = LIST_FIRST(&gp->consumer);
485 if (g_mountver_check_ident == 0)
488 error = g_access(cp, 1, 0, 0);
490 G_MOUNTVER_DEBUG(0, "Cannot access %s; "
491 "not attaching; error = %d.", gp->name, error);
494 error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
495 g_access(cp, -1, 0, 0);
497 G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
498 "not attaching; error = %d.", gp->name, error);
501 if (strcmp(ident, sc->sc_ident) != 0) {
502 G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
503 "from expected \"%s\", not attaching.", gp->name, ident,
511 static struct g_geom *
512 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
514 struct g_mountver_softc *sc;
515 struct g_consumer *cp;
520 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
521 G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
524 * Let's check if device already exists.
526 LIST_FOREACH(gp, &mp->geom, geom) {
531 /* Already attached? */
532 if (pp == LIST_FIRST(&gp->provider))
535 if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
541 cp = LIST_FIRST(&gp->consumer);
543 error = g_mountver_ident_matches(gp);
548 if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
549 error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
551 G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
556 g_mountver_send_queued(gp);
558 G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
564 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
570 version = gctl_get_paraml(req, "version", sizeof(*version));
571 if (version == NULL) {
572 gctl_error(req, "No '%s' argument.", "version");
575 if (*version != G_MOUNTVER_VERSION) {
576 gctl_error(req, "Userland and kernel parts are out of sync.");
580 if (strcmp(verb, "create") == 0) {
581 g_mountver_ctl_create(req, mp);
583 } else if (strcmp(verb, "destroy") == 0) {
584 g_mountver_ctl_destroy(req, mp);
588 gctl_error(req, "Unknown verb.");
592 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
593 struct g_consumer *cp, struct g_provider *pp)
595 struct g_mountver_softc *sc;
597 if (pp != NULL || cp != NULL)
601 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
602 sc->sc_orphaned ? "OFFLINE" : "ONLINE");
603 sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
604 sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
608 g_mountver_shutdown_pre_sync(void *arg, int howto)
611 struct g_geom *gp, *gp2;
616 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2)
617 g_mountver_destroy(gp, 1);
623 g_mountver_init(struct g_class *mp)
626 g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
627 g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
628 if (g_mountver_pre_sync == NULL)
629 G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
633 g_mountver_fini(struct g_class *mp)
636 if (g_mountver_pre_sync != NULL)
637 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
640 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);