xref: /freebsd/sys/geom/mountver/g_mountver.c (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Edward Tomasz Napierala <trasz@FreeBSD.org>
5  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/bio.h>
40 #include <sys/disk.h>
41 #include <sys/proc.h>
42 #include <sys/sbuf.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>
49 
50 
51 SYSCTL_DECL(_kern_geom);
52 static SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW | CTLFLAG_MPSAFE,
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");
60 
61 static eventhandler_tag g_mountver_pre_sync = NULL;
62 
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,
69     struct g_geom *gp);
70 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
71     const char *verb);
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);
76 
77 struct g_class g_mountver_class = {
78 	.name = G_MOUNTVER_CLASS_NAME,
79 	.version = G_VERSION,
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
85 };
86 
87 static void
88 g_mountver_detach(void *arg, int flags __unused)
89 {
90 	struct g_consumer *cp = arg;
91 
92 	g_topology_assert();
93 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
94 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
95 	g_detach(cp);
96 }
97 
98 static void
99 g_mountver_done(struct bio *bp)
100 {
101 	struct g_mountver_softc *sc;
102 	struct g_geom *gp;
103 	struct g_consumer *cp;
104 	struct bio *pbp;
105 
106 	cp = bp->bio_from;
107 	gp = cp->geom;
108 	if (bp->bio_error != ENXIO) {
109 		g_std_done(bp);
110 		goto done;
111 	}
112 
113 	/*
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.
118 	 */
119 	pbp = bp->bio_parent;
120 	KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
121 	    ("parent request was for someone else"));
122 	g_destroy_bio(bp);
123 	pbp->bio_inbed++;
124 	g_mountver_queue(pbp);
125 
126 done:
127 	sc = gp->softc;
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);
132 }
133 
134 /*
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.
137  */
138 static void
139 g_mountver_send(struct g_geom *gp, struct bio *bp)
140 {
141 	struct g_mountver_softc *sc = gp->softc;
142 	struct g_consumer *cp;
143 	struct bio *cbp;
144 
145 	mtx_assert(&sc->sc_mtx, MA_OWNED);
146 	cbp = g_clone_bio(bp);
147 	if (cbp == NULL) {
148 		mtx_unlock(&sc->sc_mtx);
149 		g_io_deliver(bp, ENOMEM);
150 		return;
151 	}
152 	cp = LIST_FIRST(&gp->consumer);
153 	cp->index++;
154 	mtx_unlock(&sc->sc_mtx);
155 
156 	cbp->bio_done = g_mountver_done;
157 	g_io_request(cbp, cp);
158 }
159 
160 static void
161 g_mountver_queue(struct bio *bp)
162 {
163 	struct g_mountver_softc *sc;
164 	struct g_geom *gp;
165 
166 	gp = bp->bio_to->geom;
167 	sc = gp->softc;
168 
169 	mtx_lock(&sc->sc_mtx);
170 	TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
171 	mtx_unlock(&sc->sc_mtx);
172 }
173 
174 static void
175 g_mountver_send_queued(struct g_geom *gp)
176 {
177 	struct g_mountver_softc *sc;
178 	struct bio *bp;
179 
180 	sc = gp->softc;
181 
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);
189 	}
190 	mtx_unlock(&sc->sc_mtx);
191 }
192 
193 static void
194 g_mountver_discard_queued(struct g_geom *gp)
195 {
196 	struct g_mountver_softc *sc;
197 	struct bio *bp;
198 
199 	sc = gp->softc;
200 
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);
208 	}
209 	mtx_unlock(&sc->sc_mtx);
210 }
211 
212 static void
213 g_mountver_start(struct bio *bp)
214 {
215 	struct g_mountver_softc *sc;
216 	struct g_geom *gp;
217 
218 	gp = bp->bio_to->geom;
219 	sc = gp->softc;
220 	G_MOUNTVER_LOGREQ(bp, "Request received.");
221 
222 	/*
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.
226 	 */
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);
233 			return;
234 		}
235 		G_MOUNTVER_LOGREQ(bp, "Queueing request.");
236 		g_mountver_queue(bp);
237 		if (!sc->sc_orphaned)
238 			g_mountver_send_queued(gp);
239 	} else {
240 		G_MOUNTVER_LOGREQ(bp, "Sending request.");
241 		/* sc_mtx is dropped inside */
242 		g_mountver_send(gp, bp);
243 	}
244 }
245 
246 static int
247 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
248 {
249 	struct g_mountver_softc *sc;
250 	struct g_geom *gp;
251 	struct g_consumer *cp;
252 
253 	g_topology_assert();
254 
255 	gp = pp->geom;
256 	cp = LIST_FIRST(&gp->consumer);
257 	sc = gp->softc;
258 	if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
259 		return (0);
260 	KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
261 
262 	sc->sc_access_r += dr;
263 	sc->sc_access_w += dw;
264 	sc->sc_access_e += de;
265 
266 	if (sc->sc_orphaned)
267 		return (0);
268 
269 	return (g_access(cp, dr, dw, de));
270 }
271 
272 static int
273 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
274 {
275 	struct g_mountver_softc *sc;
276 	struct g_geom *gp;
277 	struct g_provider *newpp;
278 	struct g_consumer *cp;
279 	struct g_geom_alias *gap;
280 	char name[64];
281 	int error;
282 	int identsize = DISK_IDENT_SIZE;
283 
284 	g_topology_assert();
285 
286 	gp = NULL;
287 	newpp = NULL;
288 	cp = NULL;
289 
290 	snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
291 	LIST_FOREACH(gp, &mp->geom, geom) {
292 		if (strcmp(gp->name, name) == 0) {
293 			gctl_error(req, "Provider %s already exists.", name);
294 			return (EEXIST);
295 		}
296 	}
297 	gp = g_new_geomf(mp, "%s", name);
298 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
299 	mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF | MTX_RECURSE);
300 	TAILQ_INIT(&sc->sc_queue);
301 	sc->sc_provider_name = strdup(pp->name, M_GEOM);
302 	gp->softc = sc;
303 	gp->start = g_mountver_start;
304 	gp->orphan = g_mountver_orphan;
305 	gp->resize = g_mountver_resize;
306 	gp->access = g_mountver_access;
307 	gp->dumpconf = g_mountver_dumpconf;
308 
309 	newpp = g_new_providerf(gp, "%s", gp->name);
310 	newpp->mediasize = pp->mediasize;
311 	newpp->sectorsize = pp->sectorsize;
312 	newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
313 	LIST_FOREACH(gap, &pp->aliases, ga_next)
314 		g_provider_add_alias(newpp, "%s%s", gap->ga_alias, G_MOUNTVER_SUFFIX);
315 
316 	if ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0) {
317 		G_MOUNTVER_DEBUG(0, "Unmapped supported for %s.", gp->name);
318 		newpp->flags |= G_PF_ACCEPT_UNMAPPED;
319 	} else {
320 		G_MOUNTVER_DEBUG(0, "Unmapped unsupported for %s.", gp->name);
321 		newpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
322 	}
323 
324 	cp = g_new_consumer(gp);
325 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
326 	error = g_attach(cp, pp);
327 	if (error != 0) {
328 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
329 		goto fail;
330 	}
331 	error = g_access(cp, 1, 0, 0);
332 	if (error != 0) {
333 		gctl_error(req, "Cannot access provider %s.", pp->name);
334 		goto fail;
335 	}
336 	error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
337 	g_access(cp, -1, 0, 0);
338 	if (error != 0) {
339 		if (g_mountver_check_ident) {
340 			gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
341 			goto fail;
342 		}
343 
344 		G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
345 		sc->sc_ident[0] = '\0';
346 	}
347 
348 	g_error_provider(newpp, 0);
349 	G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
350 	return (0);
351 fail:
352 	g_free(sc->sc_provider_name);
353 	if (cp->provider != NULL)
354 		g_detach(cp);
355 	g_destroy_consumer(cp);
356 	g_destroy_provider(newpp);
357 	g_free(gp->softc);
358 	g_destroy_geom(gp);
359 	return (error);
360 }
361 
362 static int
363 g_mountver_destroy(struct g_geom *gp, boolean_t force)
364 {
365 	struct g_mountver_softc *sc;
366 	struct g_provider *pp;
367 
368 	g_topology_assert();
369 	if (gp->softc == NULL)
370 		return (ENXIO);
371 	sc = gp->softc;
372 	pp = LIST_FIRST(&gp->provider);
373 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
374 		if (force) {
375 			G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
376 			    "can't be definitely removed.", pp->name);
377 		} else {
378 			G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
379 			    pp->name, pp->acr, pp->acw, pp->ace);
380 			return (EBUSY);
381 		}
382 	} else {
383 		G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
384 	}
385 	if (pp != NULL)
386 		g_wither_provider(pp, ENXIO);
387 	g_mountver_discard_queued(gp);
388 	g_free(sc->sc_provider_name);
389 	g_free(gp->softc);
390 	gp->softc = NULL;
391 	g_wither_geom(gp, ENXIO);
392 
393 	return (0);
394 }
395 
396 static int
397 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
398 {
399 
400 	return (g_mountver_destroy(gp, 0));
401 }
402 
403 static void
404 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
405 {
406 	struct g_provider *pp;
407 	const char *name;
408 	char param[16];
409 	int i, *nargs;
410 
411 	g_topology_assert();
412 
413 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
414 	if (nargs == NULL) {
415 		gctl_error(req, "No '%s' argument", "nargs");
416 		return;
417 	}
418 	if (*nargs <= 0) {
419 		gctl_error(req, "Missing device(s).");
420 		return;
421 	}
422 	for (i = 0; i < *nargs; i++) {
423 		snprintf(param, sizeof(param), "arg%d", i);
424 		name = gctl_get_asciiparam(req, param);
425 		if (name == NULL) {
426 			gctl_error(req, "No 'arg%d' argument", i);
427 			return;
428 		}
429 		if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
430 			name += strlen(_PATH_DEV);
431 		pp = g_provider_by_name(name);
432 		if (pp == NULL) {
433 			G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
434 			gctl_error(req, "Provider %s is invalid.", name);
435 			return;
436 		}
437 		if (g_mountver_create(req, mp, pp) != 0)
438 			return;
439 	}
440 }
441 
442 static struct g_geom *
443 g_mountver_find_geom(struct g_class *mp, const char *name)
444 {
445 	struct g_geom *gp;
446 
447 	LIST_FOREACH(gp, &mp->geom, geom) {
448 		if (strcmp(gp->name, name) == 0)
449 			return (gp);
450 	}
451 	return (NULL);
452 }
453 
454 static void
455 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
456 {
457 	int *nargs, *force, error, i;
458 	struct g_geom *gp;
459 	const char *name;
460 	char param[16];
461 
462 	g_topology_assert();
463 
464 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
465 	if (nargs == NULL) {
466 		gctl_error(req, "No '%s' argument", "nargs");
467 		return;
468 	}
469 	if (*nargs <= 0) {
470 		gctl_error(req, "Missing device(s).");
471 		return;
472 	}
473 	force = gctl_get_paraml(req, "force", sizeof(*force));
474 	if (force == NULL) {
475 		gctl_error(req, "No 'force' argument");
476 		return;
477 	}
478 
479 	for (i = 0; i < *nargs; i++) {
480 		snprintf(param, sizeof(param), "arg%d", i);
481 		name = gctl_get_asciiparam(req, param);
482 		if (name == NULL) {
483 			gctl_error(req, "No 'arg%d' argument", i);
484 			return;
485 		}
486 		if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
487 			name += strlen(_PATH_DEV);
488 		gp = g_mountver_find_geom(mp, name);
489 		if (gp == NULL) {
490 			G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
491 			gctl_error(req, "Device %s is invalid.", name);
492 			return;
493 		}
494 		error = g_mountver_destroy(gp, *force);
495 		if (error != 0) {
496 			gctl_error(req, "Cannot destroy device %s (error=%d).",
497 			    gp->name, error);
498 			return;
499 		}
500 	}
501 }
502 
503 static void
504 g_mountver_orphan(struct g_consumer *cp)
505 {
506 	struct g_mountver_softc *sc;
507 	int done;
508 
509 	g_topology_assert();
510 
511 	sc = cp->geom->softc;
512 	mtx_lock(&sc->sc_mtx);
513 	sc->sc_orphaned = 1;
514 	done = (cp->index == 0);
515 	mtx_unlock(&sc->sc_mtx);
516 	if (done)
517 		g_mountver_detach(cp, 0);
518 	G_MOUNTVER_DEBUG(0, "%s is offline.  Mount verification in progress.", sc->sc_provider_name);
519 }
520 
521 static void
522 g_mountver_resize(struct g_consumer *cp)
523 {
524 	struct g_geom *gp;
525 	struct g_provider *pp;
526 
527 	gp = cp->geom;
528 
529 	LIST_FOREACH(pp, &gp->provider, provider)
530 		g_resize_provider(pp, cp->provider->mediasize);
531 }
532 
533 static int
534 g_mountver_ident_matches(struct g_geom *gp)
535 {
536 	struct g_consumer *cp;
537 	struct g_mountver_softc *sc;
538 	char ident[DISK_IDENT_SIZE];
539 	int error, identsize = DISK_IDENT_SIZE;
540 
541 	sc = gp->softc;
542 	cp = LIST_FIRST(&gp->consumer);
543 
544 	if (g_mountver_check_ident == 0)
545 		return (0);
546 
547 	error = g_access(cp, 1, 0, 0);
548 	if (error != 0) {
549 		G_MOUNTVER_DEBUG(0, "Cannot access %s; "
550 		    "not attaching; error = %d.", gp->name, error);
551 		return (1);
552 	}
553 	error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
554 	g_access(cp, -1, 0, 0);
555 	if (error != 0) {
556 		G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
557 		    "not attaching; error = %d.", gp->name, error);
558 		return (1);
559 	}
560 	if (strcmp(ident, sc->sc_ident) != 0) {
561 		G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
562 		    "from expected \"%s\", not attaching.", gp->name, ident,
563 		    sc->sc_ident);
564 		return (1);
565 	}
566 
567 	return (0);
568 }
569 
570 static struct g_geom *
571 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
572 {
573 	struct g_mountver_softc *sc;
574 	struct g_consumer *cp;
575 	struct g_geom *gp;
576 	int error;
577 
578 	g_topology_assert();
579 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
580 	G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
581 
582 	/*
583 	 * Let's check if device already exists.
584 	 */
585 	LIST_FOREACH(gp, &mp->geom, geom) {
586 		sc = gp->softc;
587 		if (sc == NULL)
588 			continue;
589 
590 		/* Already attached? */
591 		if (pp == LIST_FIRST(&gp->provider))
592 			return (NULL);
593 
594 		if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
595 			break;
596 	}
597 	if (gp == NULL)
598 		return (NULL);
599 
600 	cp = LIST_FIRST(&gp->consumer);
601 	g_attach(cp, pp);
602 	error = g_mountver_ident_matches(gp);
603 	if (error != 0) {
604 		g_detach(cp);
605 		return (NULL);
606 	}
607 	if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
608 		error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
609 		if (error != 0) {
610 			G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
611 			g_detach(cp);
612 			return (NULL);
613 		}
614 	}
615 	sc->sc_orphaned = 0;
616 	g_mountver_send_queued(gp);
617 	G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
618 
619 	return (gp);
620 }
621 
622 static void
623 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
624 {
625 	uint32_t *version;
626 
627 	g_topology_assert();
628 
629 	version = gctl_get_paraml(req, "version", sizeof(*version));
630 	if (version == NULL) {
631 		gctl_error(req, "No '%s' argument.", "version");
632 		return;
633 	}
634 	if (*version != G_MOUNTVER_VERSION) {
635 		gctl_error(req, "Userland and kernel parts are out of sync.");
636 		return;
637 	}
638 
639 	if (strcmp(verb, "create") == 0) {
640 		g_mountver_ctl_create(req, mp);
641 		return;
642 	} else if (strcmp(verb, "destroy") == 0) {
643 		g_mountver_ctl_destroy(req, mp);
644 		return;
645 	}
646 
647 	gctl_error(req, "Unknown verb.");
648 }
649 
650 static void
651 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
652     struct g_consumer *cp, struct g_provider *pp)
653 {
654 	struct g_mountver_softc *sc;
655 
656 	if (pp != NULL || cp != NULL)
657 		return;
658 
659 	sc = gp->softc;
660 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
661 	    sc->sc_orphaned ? "OFFLINE" : "ONLINE");
662 	sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
663 	sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
664 }
665 
666 static void
667 g_mountver_shutdown_pre_sync(void *arg, int howto)
668 {
669 	struct g_mountver_softc *sc;
670 	struct g_class *mp;
671 	struct g_geom *gp, *gp2;
672 
673 	mp = arg;
674 	g_topology_lock();
675 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
676 		if (gp->softc == NULL)
677 			continue;
678 		sc = gp->softc;
679 		sc->sc_shutting_down = 1;
680 		if (sc->sc_orphaned)
681 			g_mountver_destroy(gp, 1);
682 	}
683 	g_topology_unlock();
684 }
685 
686 static void
687 g_mountver_init(struct g_class *mp)
688 {
689 
690 	g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
691 	    g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
692 	if (g_mountver_pre_sync == NULL)
693 		G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
694 }
695 
696 static void
697 g_mountver_fini(struct g_class *mp)
698 {
699 
700 	if (g_mountver_pre_sync != NULL)
701 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
702 }
703 
704 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);
705 MODULE_VERSION(geom_mountver, 0);
706