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 int g_mountver_destroy(struct g_geom *gp, boolean_t force);
63 static g_taste_t g_mountver_taste;
64 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp,
66 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
68 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent,
69 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
70 static void g_mountver_init(struct g_class *mp);
71 static void g_mountver_fini(struct g_class *mp);
73 struct g_class g_mountver_class = {
74 .name = G_MOUNTVER_CLASS_NAME,
76 .ctlreq = g_mountver_config,
77 .taste = g_mountver_taste,
78 .destroy_geom = g_mountver_destroy_geom,
79 .init = g_mountver_init,
80 .fini = g_mountver_fini
84 g_mountver_done(struct bio *bp)
89 if (bp->bio_error != ENXIO) {
95 * When the device goes away, it's possible that few requests
96 * will be completed with ENXIO before g_mountver_orphan()
97 * gets called. To work around that, we have to queue requests
98 * that failed with ENXIO, in order to send them later.
100 gp = bp->bio_from->geom;
102 pbp = bp->bio_parent;
103 KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
104 ("parent request was for someone else"));
107 g_mountver_queue(pbp);
111 g_mountver_send(struct bio *bp)
116 gp = bp->bio_to->geom;
118 cbp = g_clone_bio(bp);
120 g_io_deliver(bp, ENOMEM);
124 cbp->bio_done = g_mountver_done;
125 g_io_request(cbp, LIST_FIRST(&gp->consumer));
129 g_mountver_queue(struct bio *bp)
131 struct g_mountver_softc *sc;
134 gp = bp->bio_to->geom;
137 mtx_lock(&sc->sc_mtx);
138 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
139 mtx_unlock(&sc->sc_mtx);
143 g_mountver_send_queued(struct g_geom *gp)
145 struct g_mountver_softc *sc;
150 mtx_lock(&sc->sc_mtx);
151 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
152 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
153 G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
156 mtx_unlock(&sc->sc_mtx);
160 g_mountver_discard_queued(struct g_geom *gp)
162 struct g_mountver_softc *sc;
167 mtx_lock(&sc->sc_mtx);
168 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
169 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
170 G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
171 g_io_deliver(bp, ENXIO);
173 mtx_unlock(&sc->sc_mtx);
177 g_mountver_start(struct bio *bp)
179 struct g_mountver_softc *sc;
182 gp = bp->bio_to->geom;
184 G_MOUNTVER_LOGREQ(bp, "Request received.");
187 * It is possible that some bios were returned with ENXIO, even though
188 * orphaning didn't happen yet. In that case, queue all subsequent
189 * requests in order to maintain ordering.
191 if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
192 G_MOUNTVER_LOGREQ(bp, "Queueing request.");
193 g_mountver_queue(bp);
194 if (!sc->sc_orphaned)
195 g_mountver_send_queued(gp);
197 G_MOUNTVER_LOGREQ(bp, "Sending request.");
203 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
205 struct g_mountver_softc *sc;
207 struct g_consumer *cp;
212 cp = LIST_FIRST(&gp->consumer);
214 if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
216 KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
218 sc->sc_access_r += dr;
219 sc->sc_access_w += dw;
220 sc->sc_access_e += de;
225 return (g_access(cp, dr, dw, de));
229 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
231 struct g_mountver_softc *sc;
233 struct g_provider *newpp;
234 struct g_consumer *cp;
237 int identsize = DISK_IDENT_SIZE;
245 snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
246 LIST_FOREACH(gp, &mp->geom, geom) {
247 if (strcmp(gp->name, name) == 0) {
248 gctl_error(req, "Provider %s already exists.", name);
252 gp = g_new_geomf(mp, "%s", name);
253 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
254 mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF);
255 TAILQ_INIT(&sc->sc_queue);
256 sc->sc_provider_name = strdup(pp->name, M_GEOM);
258 gp->start = g_mountver_start;
259 gp->orphan = g_mountver_orphan;
260 gp->access = g_mountver_access;
261 gp->dumpconf = g_mountver_dumpconf;
263 newpp = g_new_providerf(gp, "%s", gp->name);
264 newpp->mediasize = pp->mediasize;
265 newpp->sectorsize = pp->sectorsize;
267 cp = g_new_consumer(gp);
268 error = g_attach(cp, pp);
270 gctl_error(req, "Cannot attach to provider %s.", pp->name);
273 error = g_access(cp, 1, 0, 0);
275 gctl_error(req, "Cannot access provider %s.", pp->name);
278 error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
279 g_access(cp, -1, 0, 0);
281 if (g_mountver_check_ident) {
282 gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
286 G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
287 sc->sc_ident[0] = '\0';
290 g_error_provider(newpp, 0);
291 G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
294 g_free(sc->sc_provider_name);
295 if (cp->provider != NULL)
297 g_destroy_consumer(cp);
298 g_destroy_provider(newpp);
305 g_mountver_destroy(struct g_geom *gp, boolean_t force)
307 struct g_mountver_softc *sc;
308 struct g_provider *pp;
311 if (gp->softc == NULL)
314 pp = LIST_FIRST(&gp->provider);
315 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
317 G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
318 "can't be definitely removed.", pp->name);
320 G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
321 pp->name, pp->acr, pp->acw, pp->ace);
325 G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
328 g_orphan_provider(pp, ENXIO);
329 g_mountver_discard_queued(gp);
330 g_free(sc->sc_provider_name);
333 g_wither_geom(gp, ENXIO);
339 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
342 return (g_mountver_destroy(gp, 0));
346 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
348 struct g_provider *pp;
355 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
357 gctl_error(req, "No '%s' argument", "nargs");
361 gctl_error(req, "Missing device(s).");
364 for (i = 0; i < *nargs; i++) {
365 snprintf(param, sizeof(param), "arg%d", i);
366 name = gctl_get_asciiparam(req, param);
368 gctl_error(req, "No 'arg%d' argument", i);
371 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
372 name += strlen("/dev/");
373 pp = g_provider_by_name(name);
375 G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
376 gctl_error(req, "Provider %s is invalid.", name);
379 if (g_mountver_create(req, mp, pp) != 0)
384 static struct g_geom *
385 g_mountver_find_geom(struct g_class *mp, const char *name)
389 LIST_FOREACH(gp, &mp->geom, geom) {
390 if (strcmp(gp->name, name) == 0)
397 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
399 int *nargs, *force, error, i;
406 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
408 gctl_error(req, "No '%s' argument", "nargs");
412 gctl_error(req, "Missing device(s).");
415 force = gctl_get_paraml(req, "force", sizeof(*force));
417 gctl_error(req, "No 'force' argument");
421 for (i = 0; i < *nargs; i++) {
422 snprintf(param, sizeof(param), "arg%d", i);
423 name = gctl_get_asciiparam(req, param);
425 gctl_error(req, "No 'arg%d' argument", i);
428 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
429 name += strlen("/dev/");
430 gp = g_mountver_find_geom(mp, name);
432 G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
433 gctl_error(req, "Device %s is invalid.", name);
436 error = g_mountver_destroy(gp, *force);
438 gctl_error(req, "Cannot destroy device %s (error=%d).",
446 g_mountver_orphan(struct g_consumer *cp)
448 struct g_mountver_softc *sc;
452 sc = cp->geom->softc;
454 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
455 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
457 G_MOUNTVER_DEBUG(0, "%s is offline. Mount verification in progress.", sc->sc_provider_name);
461 g_mountver_ident_matches(struct g_geom *gp)
463 struct g_consumer *cp;
464 struct g_mountver_softc *sc;
465 char ident[DISK_IDENT_SIZE];
466 int error, identsize = DISK_IDENT_SIZE;
469 cp = LIST_FIRST(&gp->consumer);
471 if (g_mountver_check_ident == 0)
474 error = g_access(cp, 1, 0, 0);
476 G_MOUNTVER_DEBUG(0, "Cannot access %s; "
477 "not attaching; error = %d.", gp->name, error);
480 error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
481 g_access(cp, -1, 0, 0);
483 G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
484 "not attaching; error = %d.", gp->name, error);
487 if (strcmp(ident, sc->sc_ident) != 0) {
488 G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
489 "from expected \"%s\", not attaching.", gp->name, ident,
497 static struct g_geom *
498 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
500 struct g_mountver_softc *sc;
501 struct g_consumer *cp;
506 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
507 G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
510 * Let's check if device already exists.
512 LIST_FOREACH(gp, &mp->geom, geom) {
517 /* Already attached? */
518 if (pp == LIST_FIRST(&gp->provider))
521 if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
527 cp = LIST_FIRST(&gp->consumer);
529 error = g_mountver_ident_matches(gp);
534 if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
535 error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
537 G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
542 g_mountver_send_queued(gp);
544 G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
550 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
556 version = gctl_get_paraml(req, "version", sizeof(*version));
557 if (version == NULL) {
558 gctl_error(req, "No '%s' argument.", "version");
561 if (*version != G_MOUNTVER_VERSION) {
562 gctl_error(req, "Userland and kernel parts are out of sync.");
566 if (strcmp(verb, "create") == 0) {
567 g_mountver_ctl_create(req, mp);
569 } else if (strcmp(verb, "destroy") == 0) {
570 g_mountver_ctl_destroy(req, mp);
574 gctl_error(req, "Unknown verb.");
578 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
579 struct g_consumer *cp, struct g_provider *pp)
581 struct g_mountver_softc *sc;
583 if (pp != NULL || cp != NULL)
587 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
588 sc->sc_orphaned ? "OFFLINE" : "ONLINE");
589 sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
590 sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
594 g_mountver_shutdown_pre_sync(void *arg, int howto)
597 struct g_geom *gp, *gp2;
602 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2)
603 g_mountver_destroy(gp, 1);
609 g_mountver_init(struct g_class *mp)
612 g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
613 g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
614 if (g_mountver_pre_sync == NULL)
615 G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
619 g_mountver_fini(struct g_class *mp)
622 if (g_mountver_pre_sync != NULL)
623 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
626 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);