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>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/eventhandler.h>
43 #include <geom/geom.h>
44 #include <geom/mountver/g_mountver.h>
47 SYSCTL_DECL(_kern_geom);
48 SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW,
49 0, "GEOM_MOUNTVER stuff");
50 static u_int g_mountver_debug = 0;
51 static u_int g_mountver_check_ident = 1;
52 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, debug, CTLFLAG_RW,
53 &g_mountver_debug, 0, "Debug level");
54 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, check_ident, CTLFLAG_RW,
55 &g_mountver_check_ident, 0, "Check disk ident when reattaching");
57 static eventhandler_tag g_mountver_pre_sync = NULL;
59 static void g_mountver_queue(struct bio *bp);
60 static void g_mountver_orphan(struct g_consumer *cp);
61 static int g_mountver_destroy(struct g_geom *gp, boolean_t force);
62 static g_taste_t g_mountver_taste;
63 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp,
65 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
67 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent,
68 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
69 static void g_mountver_init(struct g_class *mp);
70 static void g_mountver_fini(struct g_class *mp);
72 struct g_class g_mountver_class = {
73 .name = G_MOUNTVER_CLASS_NAME,
75 .ctlreq = g_mountver_config,
76 .taste = g_mountver_taste,
77 .destroy_geom = g_mountver_destroy_geom,
78 .init = g_mountver_init,
79 .fini = g_mountver_fini
83 g_mountver_done(struct bio *bp)
88 if (bp->bio_error != ENXIO) {
94 * When the device goes away, it's possible that few requests
95 * will be completed with ENXIO before g_mountver_orphan()
96 * gets called. To work around that, we have to queue requests
97 * that failed with ENXIO, in order to send them later.
99 gp = bp->bio_from->geom;
101 pbp = bp->bio_parent;
102 KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
103 ("parent request was for someone else"));
106 g_mountver_queue(pbp);
110 g_mountver_send(struct bio *bp)
115 gp = bp->bio_to->geom;
117 cbp = g_clone_bio(bp);
119 g_io_deliver(bp, ENOMEM);
123 cbp->bio_done = g_mountver_done;
124 g_io_request(cbp, LIST_FIRST(&gp->consumer));
128 g_mountver_queue(struct bio *bp)
130 struct g_mountver_softc *sc;
133 gp = bp->bio_to->geom;
136 mtx_lock(&sc->sc_mtx);
137 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
138 mtx_unlock(&sc->sc_mtx);
142 g_mountver_send_queued(struct g_geom *gp)
144 struct g_mountver_softc *sc;
149 mtx_lock(&sc->sc_mtx);
150 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
151 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
152 G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
155 mtx_unlock(&sc->sc_mtx);
159 g_mountver_discard_queued(struct g_geom *gp)
161 struct g_mountver_softc *sc;
166 mtx_lock(&sc->sc_mtx);
167 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
168 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
169 G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
170 g_io_deliver(bp, ENXIO);
172 mtx_unlock(&sc->sc_mtx);
176 g_mountver_start(struct bio *bp)
178 struct g_mountver_softc *sc;
181 gp = bp->bio_to->geom;
183 G_MOUNTVER_LOGREQ(bp, "Request received.");
186 * It is possible that some bios were returned with ENXIO, even though
187 * orphaning didn't happen yet. In that case, queue all subsequent
188 * requests in order to maintain ordering.
190 if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
191 G_MOUNTVER_LOGREQ(bp, "Queueing request.");
192 g_mountver_queue(bp);
193 if (!sc->sc_orphaned)
194 g_mountver_send_queued(gp);
196 G_MOUNTVER_LOGREQ(bp, "Sending request.");
202 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
204 struct g_mountver_softc *sc;
206 struct g_consumer *cp;
211 cp = LIST_FIRST(&gp->consumer);
213 if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
215 KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
217 sc->sc_access_r += dr;
218 sc->sc_access_w += dw;
219 sc->sc_access_e += de;
224 return (g_access(cp, dr, dw, de));
228 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
230 struct g_mountver_softc *sc;
232 struct g_provider *newpp;
233 struct g_consumer *cp;
236 int identsize = DISK_IDENT_SIZE;
244 snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
245 LIST_FOREACH(gp, &mp->geom, geom) {
246 if (strcmp(gp->name, name) == 0) {
247 gctl_error(req, "Provider %s already exists.", name);
251 gp = g_new_geomf(mp, name);
252 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
253 mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF);
254 TAILQ_INIT(&sc->sc_queue);
255 sc->sc_provider_name = strdup(pp->name, M_GEOM);
257 gp->start = g_mountver_start;
258 gp->orphan = g_mountver_orphan;
259 gp->access = g_mountver_access;
260 gp->dumpconf = g_mountver_dumpconf;
262 newpp = g_new_providerf(gp, gp->name);
263 newpp->mediasize = pp->mediasize;
264 newpp->sectorsize = pp->sectorsize;
266 cp = g_new_consumer(gp);
267 error = g_attach(cp, pp);
269 gctl_error(req, "Cannot attach to provider %s.", pp->name);
272 error = g_access(cp, 1, 0, 0);
274 gctl_error(req, "Cannot access provider %s.", pp->name);
277 error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
278 g_access(cp, -1, 0, 0);
280 if (g_mountver_check_ident) {
281 gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
285 G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
286 sc->sc_ident[0] = '\0';
289 g_error_provider(newpp, 0);
290 G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
293 g_free(sc->sc_provider_name);
294 if (cp->provider != NULL)
296 g_destroy_consumer(cp);
297 g_destroy_provider(newpp);
304 g_mountver_destroy(struct g_geom *gp, boolean_t force)
306 struct g_mountver_softc *sc;
307 struct g_provider *pp;
310 if (gp->softc == NULL)
313 pp = LIST_FIRST(&gp->provider);
314 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
316 G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
317 "can't be definitely removed.", pp->name);
319 G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
320 pp->name, pp->acr, pp->acw, pp->ace);
324 G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
327 g_orphan_provider(pp, ENXIO);
328 g_mountver_discard_queued(gp);
329 g_free(sc->sc_provider_name);
332 g_wither_geom(gp, ENXIO);
338 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
341 return (g_mountver_destroy(gp, 0));
345 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
347 struct g_provider *pp;
354 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
356 gctl_error(req, "No '%s' argument", "nargs");
360 gctl_error(req, "Missing device(s).");
363 for (i = 0; i < *nargs; i++) {
364 snprintf(param, sizeof(param), "arg%d", i);
365 name = gctl_get_asciiparam(req, param);
367 gctl_error(req, "No 'arg%d' argument", i);
370 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
371 name += strlen("/dev/");
372 pp = g_provider_by_name(name);
374 G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
375 gctl_error(req, "Provider %s is invalid.", name);
378 if (g_mountver_create(req, mp, pp) != 0)
383 static struct g_geom *
384 g_mountver_find_geom(struct g_class *mp, const char *name)
388 LIST_FOREACH(gp, &mp->geom, geom) {
389 if (strcmp(gp->name, name) == 0)
396 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
398 int *nargs, *force, error, i;
405 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
407 gctl_error(req, "No '%s' argument", "nargs");
411 gctl_error(req, "Missing device(s).");
414 force = gctl_get_paraml(req, "force", sizeof(*force));
416 gctl_error(req, "No 'force' argument");
420 for (i = 0; i < *nargs; i++) {
421 snprintf(param, sizeof(param), "arg%d", i);
422 name = gctl_get_asciiparam(req, param);
424 gctl_error(req, "No 'arg%d' argument", i);
427 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
428 name += strlen("/dev/");
429 gp = g_mountver_find_geom(mp, name);
431 G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
432 gctl_error(req, "Device %s is invalid.", name);
435 error = g_mountver_destroy(gp, *force);
437 gctl_error(req, "Cannot destroy device %s (error=%d).",
445 g_mountver_orphan(struct g_consumer *cp)
447 struct g_mountver_softc *sc;
451 sc = cp->geom->softc;
453 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
454 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
456 G_MOUNTVER_DEBUG(0, "%s is offline. Mount verification in progress.", sc->sc_provider_name);
460 g_mountver_ident_matches(struct g_geom *gp)
462 struct g_consumer *cp;
463 struct g_mountver_softc *sc;
464 char ident[DISK_IDENT_SIZE];
465 int error, identsize = DISK_IDENT_SIZE;
468 cp = LIST_FIRST(&gp->consumer);
470 if (g_mountver_check_ident == 0)
473 error = g_access(cp, 1, 0, 0);
475 G_MOUNTVER_DEBUG(0, "Cannot access %s; "
476 "not attaching; error = %d.", gp->name, error);
479 error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
480 g_access(cp, -1, 0, 0);
482 G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
483 "not attaching; error = %d.", gp->name, error);
486 if (strcmp(ident, sc->sc_ident) != 0) {
487 G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
488 "from expected \"%s\", not attaching.", gp->name, ident,
496 static struct g_geom *
497 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
499 struct g_mountver_softc *sc;
500 struct g_consumer *cp;
505 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
506 G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
509 * Let's check if device already exists.
511 LIST_FOREACH(gp, &mp->geom, geom) {
516 /* Already attached? */
517 if (pp == LIST_FIRST(&gp->provider))
520 if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
526 cp = LIST_FIRST(&gp->consumer);
528 error = g_mountver_ident_matches(gp);
533 if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
534 error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
536 G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
541 g_mountver_send_queued(gp);
543 G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
549 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
555 version = gctl_get_paraml(req, "version", sizeof(*version));
556 if (version == NULL) {
557 gctl_error(req, "No '%s' argument.", "version");
560 if (*version != G_MOUNTVER_VERSION) {
561 gctl_error(req, "Userland and kernel parts are out of sync.");
565 if (strcmp(verb, "create") == 0) {
566 g_mountver_ctl_create(req, mp);
568 } else if (strcmp(verb, "destroy") == 0) {
569 g_mountver_ctl_destroy(req, mp);
573 gctl_error(req, "Unknown verb.");
577 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
578 struct g_consumer *cp, struct g_provider *pp)
580 struct g_mountver_softc *sc;
582 if (pp != NULL || cp != NULL)
586 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
587 sc->sc_orphaned ? "OFFLINE" : "ONLINE");
588 sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
589 sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
593 g_mountver_shutdown_pre_sync(void *arg, int howto)
596 struct g_geom *gp, *gp2;
601 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2)
602 g_mountver_destroy(gp, 1);
608 g_mountver_init(struct g_class *mp)
611 g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
612 g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
613 if (g_mountver_pre_sync == NULL)
614 G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
618 g_mountver_fini(struct g_class *mp)
621 if (g_mountver_pre_sync != NULL)
622 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
625 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);