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/mountver/g_mountver.h>
50 SYSCTL_DECL(_kern_geom);
51 static SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW,
52 0, "GEOM_MOUNTVER stuff");
53 static u_int g_mountver_debug = 0;
54 static u_int g_mountver_check_ident = 1;
55 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, debug, CTLFLAG_RW,
56 &g_mountver_debug, 0, "Debug level");
57 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, check_ident, CTLFLAG_RW,
58 &g_mountver_check_ident, 0, "Check disk ident when reattaching");
60 static eventhandler_tag g_mountver_pre_sync = NULL;
62 static void g_mountver_queue(struct bio *bp);
63 static void g_mountver_orphan(struct g_consumer *cp);
64 static void g_mountver_resize(struct g_consumer *cp);
65 static int g_mountver_destroy(struct g_geom *gp, boolean_t force);
66 static g_taste_t g_mountver_taste;
67 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp,
69 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
71 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent,
72 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
73 static void g_mountver_init(struct g_class *mp);
74 static void g_mountver_fini(struct g_class *mp);
76 struct g_class g_mountver_class = {
77 .name = G_MOUNTVER_CLASS_NAME,
79 .ctlreq = g_mountver_config,
80 .taste = g_mountver_taste,
81 .destroy_geom = g_mountver_destroy_geom,
82 .init = g_mountver_init,
83 .fini = g_mountver_fini
87 g_mountver_done(struct bio *bp)
92 if (bp->bio_error != ENXIO) {
98 * When the device goes away, it's possible that few requests
99 * will be completed with ENXIO before g_mountver_orphan()
100 * gets called. To work around that, we have to queue requests
101 * that failed with ENXIO, in order to send them later.
103 gp = bp->bio_from->geom;
105 pbp = bp->bio_parent;
106 KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
107 ("parent request was for someone else"));
110 g_mountver_queue(pbp);
114 g_mountver_send(struct bio *bp)
119 gp = bp->bio_to->geom;
121 cbp = g_clone_bio(bp);
123 g_io_deliver(bp, ENOMEM);
127 cbp->bio_done = g_mountver_done;
128 g_io_request(cbp, LIST_FIRST(&gp->consumer));
132 g_mountver_queue(struct bio *bp)
134 struct g_mountver_softc *sc;
137 gp = bp->bio_to->geom;
140 mtx_lock(&sc->sc_mtx);
141 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
142 mtx_unlock(&sc->sc_mtx);
146 g_mountver_send_queued(struct g_geom *gp)
148 struct g_mountver_softc *sc;
153 mtx_lock(&sc->sc_mtx);
154 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
155 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
156 G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
159 mtx_unlock(&sc->sc_mtx);
163 g_mountver_discard_queued(struct g_geom *gp)
165 struct g_mountver_softc *sc;
170 mtx_lock(&sc->sc_mtx);
171 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
172 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
173 G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
174 g_io_deliver(bp, ENXIO);
176 mtx_unlock(&sc->sc_mtx);
180 g_mountver_start(struct bio *bp)
182 struct g_mountver_softc *sc;
185 gp = bp->bio_to->geom;
187 G_MOUNTVER_LOGREQ(bp, "Request received.");
190 * It is possible that some bios were returned with ENXIO, even though
191 * orphaning didn't happen yet. In that case, queue all subsequent
192 * requests in order to maintain ordering.
194 if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
195 if (sc->sc_shutting_down) {
196 G_MOUNTVER_LOGREQ(bp, "Discarding request due to shutdown.");
197 g_io_deliver(bp, ENXIO);
200 G_MOUNTVER_LOGREQ(bp, "Queueing request.");
201 g_mountver_queue(bp);
202 if (!sc->sc_orphaned)
203 g_mountver_send_queued(gp);
205 G_MOUNTVER_LOGREQ(bp, "Sending request.");
211 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
213 struct g_mountver_softc *sc;
215 struct g_consumer *cp;
220 cp = LIST_FIRST(&gp->consumer);
222 if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
224 KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
226 sc->sc_access_r += dr;
227 sc->sc_access_w += dw;
228 sc->sc_access_e += de;
233 return (g_access(cp, dr, dw, de));
237 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
239 struct g_mountver_softc *sc;
241 struct g_provider *newpp;
242 struct g_consumer *cp;
245 int identsize = DISK_IDENT_SIZE;
253 snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
254 LIST_FOREACH(gp, &mp->geom, geom) {
255 if (strcmp(gp->name, name) == 0) {
256 gctl_error(req, "Provider %s already exists.", name);
260 gp = g_new_geomf(mp, "%s", name);
261 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
262 mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF | MTX_RECURSE);
263 TAILQ_INIT(&sc->sc_queue);
264 sc->sc_provider_name = strdup(pp->name, M_GEOM);
266 gp->start = g_mountver_start;
267 gp->orphan = g_mountver_orphan;
268 gp->resize = g_mountver_resize;
269 gp->access = g_mountver_access;
270 gp->dumpconf = g_mountver_dumpconf;
272 newpp = g_new_providerf(gp, "%s", gp->name);
273 newpp->mediasize = pp->mediasize;
274 newpp->sectorsize = pp->sectorsize;
275 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
277 if ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0) {
278 G_MOUNTVER_DEBUG(0, "Unmapped supported for %s.", gp->name);
279 newpp->flags |= G_PF_ACCEPT_UNMAPPED;
281 G_MOUNTVER_DEBUG(0, "Unmapped unsupported for %s.", gp->name);
282 newpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
285 cp = g_new_consumer(gp);
286 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
287 error = g_attach(cp, pp);
289 gctl_error(req, "Cannot attach to provider %s.", pp->name);
292 error = g_access(cp, 1, 0, 0);
294 gctl_error(req, "Cannot access provider %s.", pp->name);
297 error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
298 g_access(cp, -1, 0, 0);
300 if (g_mountver_check_ident) {
301 gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
305 G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
306 sc->sc_ident[0] = '\0';
309 g_error_provider(newpp, 0);
310 G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
313 g_free(sc->sc_provider_name);
314 if (cp->provider != NULL)
316 g_destroy_consumer(cp);
317 g_destroy_provider(newpp);
324 g_mountver_destroy(struct g_geom *gp, boolean_t force)
326 struct g_mountver_softc *sc;
327 struct g_provider *pp;
330 if (gp->softc == NULL)
333 pp = LIST_FIRST(&gp->provider);
334 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
336 G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
337 "can't be definitely removed.", pp->name);
339 G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
340 pp->name, pp->acr, pp->acw, pp->ace);
344 G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
347 g_wither_provider(pp, ENXIO);
348 g_mountver_discard_queued(gp);
349 g_free(sc->sc_provider_name);
352 g_wither_geom(gp, ENXIO);
358 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
361 return (g_mountver_destroy(gp, 0));
365 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
367 struct g_provider *pp;
374 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
376 gctl_error(req, "No '%s' argument", "nargs");
380 gctl_error(req, "Missing device(s).");
383 for (i = 0; i < *nargs; i++) {
384 snprintf(param, sizeof(param), "arg%d", i);
385 name = gctl_get_asciiparam(req, param);
387 gctl_error(req, "No 'arg%d' argument", i);
390 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
391 name += strlen("/dev/");
392 pp = g_provider_by_name(name);
394 G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
395 gctl_error(req, "Provider %s is invalid.", name);
398 if (g_mountver_create(req, mp, pp) != 0)
403 static struct g_geom *
404 g_mountver_find_geom(struct g_class *mp, const char *name)
408 LIST_FOREACH(gp, &mp->geom, geom) {
409 if (strcmp(gp->name, name) == 0)
416 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
418 int *nargs, *force, error, i;
425 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
427 gctl_error(req, "No '%s' argument", "nargs");
431 gctl_error(req, "Missing device(s).");
434 force = gctl_get_paraml(req, "force", sizeof(*force));
436 gctl_error(req, "No 'force' argument");
440 for (i = 0; i < *nargs; i++) {
441 snprintf(param, sizeof(param), "arg%d", i);
442 name = gctl_get_asciiparam(req, param);
444 gctl_error(req, "No 'arg%d' argument", i);
447 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
448 name += strlen("/dev/");
449 gp = g_mountver_find_geom(mp, name);
451 G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
452 gctl_error(req, "Device %s is invalid.", name);
455 error = g_mountver_destroy(gp, *force);
457 gctl_error(req, "Cannot destroy device %s (error=%d).",
465 g_mountver_orphan(struct g_consumer *cp)
467 struct g_mountver_softc *sc;
471 sc = cp->geom->softc;
473 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
474 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
476 G_MOUNTVER_DEBUG(0, "%s is offline. Mount verification in progress.", sc->sc_provider_name);
480 g_mountver_resize(struct g_consumer *cp)
483 struct g_provider *pp;
487 LIST_FOREACH(pp, &gp->provider, provider)
488 g_resize_provider(pp, cp->provider->mediasize);
492 g_mountver_ident_matches(struct g_geom *gp)
494 struct g_consumer *cp;
495 struct g_mountver_softc *sc;
496 char ident[DISK_IDENT_SIZE];
497 int error, identsize = DISK_IDENT_SIZE;
500 cp = LIST_FIRST(&gp->consumer);
502 if (g_mountver_check_ident == 0)
505 error = g_access(cp, 1, 0, 0);
507 G_MOUNTVER_DEBUG(0, "Cannot access %s; "
508 "not attaching; error = %d.", gp->name, error);
511 error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
512 g_access(cp, -1, 0, 0);
514 G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
515 "not attaching; error = %d.", gp->name, error);
518 if (strcmp(ident, sc->sc_ident) != 0) {
519 G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
520 "from expected \"%s\", not attaching.", gp->name, ident,
528 static struct g_geom *
529 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
531 struct g_mountver_softc *sc;
532 struct g_consumer *cp;
537 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
538 G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
541 * Let's check if device already exists.
543 LIST_FOREACH(gp, &mp->geom, geom) {
548 /* Already attached? */
549 if (pp == LIST_FIRST(&gp->provider))
552 if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
558 cp = LIST_FIRST(&gp->consumer);
560 error = g_mountver_ident_matches(gp);
565 if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
566 error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
568 G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
573 g_mountver_send_queued(gp);
575 G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
581 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
587 version = gctl_get_paraml(req, "version", sizeof(*version));
588 if (version == NULL) {
589 gctl_error(req, "No '%s' argument.", "version");
592 if (*version != G_MOUNTVER_VERSION) {
593 gctl_error(req, "Userland and kernel parts are out of sync.");
597 if (strcmp(verb, "create") == 0) {
598 g_mountver_ctl_create(req, mp);
600 } else if (strcmp(verb, "destroy") == 0) {
601 g_mountver_ctl_destroy(req, mp);
605 gctl_error(req, "Unknown verb.");
609 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
610 struct g_consumer *cp, struct g_provider *pp)
612 struct g_mountver_softc *sc;
614 if (pp != NULL || cp != NULL)
618 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
619 sc->sc_orphaned ? "OFFLINE" : "ONLINE");
620 sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
621 sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
625 g_mountver_shutdown_pre_sync(void *arg, int howto)
627 struct g_mountver_softc *sc;
629 struct g_geom *gp, *gp2;
633 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
634 if (gp->softc == NULL)
637 sc->sc_shutting_down = 1;
639 g_mountver_destroy(gp, 1);
645 g_mountver_init(struct g_class *mp)
648 g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
649 g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
650 if (g_mountver_pre_sync == NULL)
651 G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
655 g_mountver_fini(struct g_class *mp)
658 if (g_mountver_pre_sync != NULL)
659 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
662 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);
663 MODULE_VERSION(geom_mountver, 0);