xref: /freebsd/sys/geom/mountver/g_mountver.c (revision 0bf48626aaa33768078f5872b922b1487b3a9296)
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,
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_done(struct bio *bp)
89 {
90 	struct g_geom *gp;
91 	struct bio *pbp;
92 
93 	if (bp->bio_error != ENXIO) {
94 		g_std_done(bp);
95 		return;
96 	}
97 
98 	/*
99 	 * When the device goes away, it's possible that few requests
100 	 * will be completed with ENXIO before g_mountver_orphan()
101 	 * gets called.  To work around that, we have to queue requests
102 	 * that failed with ENXIO, in order to send them later.
103 	 */
104 	gp = bp->bio_from->geom;
105 
106 	pbp = bp->bio_parent;
107 	KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
108 	    ("parent request was for someone else"));
109 	g_destroy_bio(bp);
110 	pbp->bio_inbed++;
111 	g_mountver_queue(pbp);
112 }
113 
114 static void
115 g_mountver_send(struct bio *bp)
116 {
117 	struct g_geom *gp;
118 	struct bio *cbp;
119 
120 	gp = bp->bio_to->geom;
121 
122 	cbp = g_clone_bio(bp);
123 	if (cbp == NULL) {
124 		g_io_deliver(bp, ENOMEM);
125 		return;
126 	}
127 
128 	cbp->bio_done = g_mountver_done;
129 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
130 }
131 
132 static void
133 g_mountver_queue(struct bio *bp)
134 {
135 	struct g_mountver_softc *sc;
136 	struct g_geom *gp;
137 
138 	gp = bp->bio_to->geom;
139 	sc = gp->softc;
140 
141 	mtx_lock(&sc->sc_mtx);
142 	TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
143 	mtx_unlock(&sc->sc_mtx);
144 }
145 
146 static void
147 g_mountver_send_queued(struct g_geom *gp)
148 {
149 	struct g_mountver_softc *sc;
150 	struct bio *bp;
151 
152 	sc = gp->softc;
153 
154 	mtx_lock(&sc->sc_mtx);
155 	while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
156 		TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
157 		G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
158 		g_mountver_send(bp);
159 	}
160 	mtx_unlock(&sc->sc_mtx);
161 }
162 
163 static void
164 g_mountver_discard_queued(struct g_geom *gp)
165 {
166 	struct g_mountver_softc *sc;
167 	struct bio *bp;
168 
169 	sc = gp->softc;
170 
171 	mtx_lock(&sc->sc_mtx);
172 	while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
173 		TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
174 		G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
175 		g_io_deliver(bp, ENXIO);
176 	}
177 	mtx_unlock(&sc->sc_mtx);
178 }
179 
180 static void
181 g_mountver_start(struct bio *bp)
182 {
183 	struct g_mountver_softc *sc;
184 	struct g_geom *gp;
185 
186 	gp = bp->bio_to->geom;
187 	sc = gp->softc;
188 	G_MOUNTVER_LOGREQ(bp, "Request received.");
189 
190 	/*
191 	 * It is possible that some bios were returned with ENXIO, even though
192 	 * orphaning didn't happen yet.  In that case, queue all subsequent
193 	 * requests in order to maintain ordering.
194 	 */
195 	if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
196 		if (sc->sc_shutting_down) {
197 			G_MOUNTVER_LOGREQ(bp, "Discarding request due to shutdown.");
198 			g_io_deliver(bp, ENXIO);
199 			return;
200 		}
201 		G_MOUNTVER_LOGREQ(bp, "Queueing request.");
202 		g_mountver_queue(bp);
203 		if (!sc->sc_orphaned)
204 			g_mountver_send_queued(gp);
205 	} else {
206 		G_MOUNTVER_LOGREQ(bp, "Sending request.");
207 		g_mountver_send(bp);
208 	}
209 }
210 
211 static int
212 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
213 {
214 	struct g_mountver_softc *sc;
215 	struct g_geom *gp;
216 	struct g_consumer *cp;
217 
218 	g_topology_assert();
219 
220 	gp = pp->geom;
221 	cp = LIST_FIRST(&gp->consumer);
222 	sc = gp->softc;
223 	if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
224 		return (0);
225 	KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
226 
227 	sc->sc_access_r += dr;
228 	sc->sc_access_w += dw;
229 	sc->sc_access_e += de;
230 
231 	if (sc->sc_orphaned)
232 		return (0);
233 
234 	return (g_access(cp, dr, dw, de));
235 }
236 
237 static int
238 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
239 {
240 	struct g_mountver_softc *sc;
241 	struct g_geom *gp;
242 	struct g_provider *newpp;
243 	struct g_consumer *cp;
244 	char name[64];
245 	int error;
246 	int identsize = DISK_IDENT_SIZE;
247 
248 	g_topology_assert();
249 
250 	gp = NULL;
251 	newpp = NULL;
252 	cp = NULL;
253 
254 	snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
255 	LIST_FOREACH(gp, &mp->geom, geom) {
256 		if (strcmp(gp->name, name) == 0) {
257 			gctl_error(req, "Provider %s already exists.", name);
258 			return (EEXIST);
259 		}
260 	}
261 	gp = g_new_geomf(mp, "%s", name);
262 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
263 	mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF | MTX_RECURSE);
264 	TAILQ_INIT(&sc->sc_queue);
265 	sc->sc_provider_name = strdup(pp->name, M_GEOM);
266 	gp->softc = sc;
267 	gp->start = g_mountver_start;
268 	gp->orphan = g_mountver_orphan;
269 	gp->resize = g_mountver_resize;
270 	gp->access = g_mountver_access;
271 	gp->dumpconf = g_mountver_dumpconf;
272 
273 	newpp = g_new_providerf(gp, "%s", gp->name);
274 	newpp->mediasize = pp->mediasize;
275 	newpp->sectorsize = pp->sectorsize;
276 	newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
277 
278 	if ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0) {
279 		G_MOUNTVER_DEBUG(0, "Unmapped supported for %s.", gp->name);
280 		newpp->flags |= G_PF_ACCEPT_UNMAPPED;
281 	} else {
282 		G_MOUNTVER_DEBUG(0, "Unmapped unsupported for %s.", gp->name);
283 		newpp->flags &= ~G_PF_ACCEPT_UNMAPPED;
284 	}
285 
286 	cp = g_new_consumer(gp);
287 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
288 	error = g_attach(cp, pp);
289 	if (error != 0) {
290 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
291 		goto fail;
292 	}
293 	error = g_access(cp, 1, 0, 0);
294 	if (error != 0) {
295 		gctl_error(req, "Cannot access provider %s.", pp->name);
296 		goto fail;
297 	}
298 	error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
299 	g_access(cp, -1, 0, 0);
300 	if (error != 0) {
301 		if (g_mountver_check_ident) {
302 			gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
303 			goto fail;
304 		}
305 
306 		G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
307 		sc->sc_ident[0] = '\0';
308 	}
309 
310 	g_error_provider(newpp, 0);
311 	G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
312 	return (0);
313 fail:
314 	g_free(sc->sc_provider_name);
315 	if (cp->provider != NULL)
316 		g_detach(cp);
317 	g_destroy_consumer(cp);
318 	g_destroy_provider(newpp);
319 	g_free(gp->softc);
320 	g_destroy_geom(gp);
321 	return (error);
322 }
323 
324 static int
325 g_mountver_destroy(struct g_geom *gp, boolean_t force)
326 {
327 	struct g_mountver_softc *sc;
328 	struct g_provider *pp;
329 
330 	g_topology_assert();
331 	if (gp->softc == NULL)
332 		return (ENXIO);
333 	sc = gp->softc;
334 	pp = LIST_FIRST(&gp->provider);
335 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
336 		if (force) {
337 			G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
338 			    "can't be definitely removed.", pp->name);
339 		} else {
340 			G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
341 			    pp->name, pp->acr, pp->acw, pp->ace);
342 			return (EBUSY);
343 		}
344 	} else {
345 		G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
346 	}
347 	if (pp != NULL)
348 		g_wither_provider(pp, ENXIO);
349 	g_mountver_discard_queued(gp);
350 	g_free(sc->sc_provider_name);
351 	g_free(gp->softc);
352 	gp->softc = NULL;
353 	g_wither_geom(gp, ENXIO);
354 
355 	return (0);
356 }
357 
358 static int
359 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
360 {
361 
362 	return (g_mountver_destroy(gp, 0));
363 }
364 
365 static void
366 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
367 {
368 	struct g_provider *pp;
369 	const char *name;
370 	char param[16];
371 	int i, *nargs;
372 
373 	g_topology_assert();
374 
375 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
376 	if (nargs == NULL) {
377 		gctl_error(req, "No '%s' argument", "nargs");
378 		return;
379 	}
380 	if (*nargs <= 0) {
381 		gctl_error(req, "Missing device(s).");
382 		return;
383 	}
384 	for (i = 0; i < *nargs; i++) {
385 		snprintf(param, sizeof(param), "arg%d", i);
386 		name = gctl_get_asciiparam(req, param);
387 		if (name == NULL) {
388 			gctl_error(req, "No 'arg%d' argument", i);
389 			return;
390 		}
391 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
392 			name += strlen("/dev/");
393 		pp = g_provider_by_name(name);
394 		if (pp == NULL) {
395 			G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
396 			gctl_error(req, "Provider %s is invalid.", name);
397 			return;
398 		}
399 		if (g_mountver_create(req, mp, pp) != 0)
400 			return;
401 	}
402 }
403 
404 static struct g_geom *
405 g_mountver_find_geom(struct g_class *mp, const char *name)
406 {
407 	struct g_geom *gp;
408 
409 	LIST_FOREACH(gp, &mp->geom, geom) {
410 		if (strcmp(gp->name, name) == 0)
411 			return (gp);
412 	}
413 	return (NULL);
414 }
415 
416 static void
417 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
418 {
419 	int *nargs, *force, error, i;
420 	struct g_geom *gp;
421 	const char *name;
422 	char param[16];
423 
424 	g_topology_assert();
425 
426 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
427 	if (nargs == NULL) {
428 		gctl_error(req, "No '%s' argument", "nargs");
429 		return;
430 	}
431 	if (*nargs <= 0) {
432 		gctl_error(req, "Missing device(s).");
433 		return;
434 	}
435 	force = gctl_get_paraml(req, "force", sizeof(*force));
436 	if (force == NULL) {
437 		gctl_error(req, "No 'force' argument");
438 		return;
439 	}
440 
441 	for (i = 0; i < *nargs; i++) {
442 		snprintf(param, sizeof(param), "arg%d", i);
443 		name = gctl_get_asciiparam(req, param);
444 		if (name == NULL) {
445 			gctl_error(req, "No 'arg%d' argument", i);
446 			return;
447 		}
448 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
449 			name += strlen("/dev/");
450 		gp = g_mountver_find_geom(mp, name);
451 		if (gp == NULL) {
452 			G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
453 			gctl_error(req, "Device %s is invalid.", name);
454 			return;
455 		}
456 		error = g_mountver_destroy(gp, *force);
457 		if (error != 0) {
458 			gctl_error(req, "Cannot destroy device %s (error=%d).",
459 			    gp->name, error);
460 			return;
461 		}
462 	}
463 }
464 
465 static void
466 g_mountver_orphan(struct g_consumer *cp)
467 {
468 	struct g_mountver_softc *sc;
469 
470 	g_topology_assert();
471 
472 	sc = cp->geom->softc;
473 	sc->sc_orphaned = 1;
474 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
475 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
476 	g_detach(cp);
477 	G_MOUNTVER_DEBUG(0, "%s is offline.  Mount verification in progress.", sc->sc_provider_name);
478 }
479 
480 static void
481 g_mountver_resize(struct g_consumer *cp)
482 {
483 	struct g_geom *gp;
484 	struct g_provider *pp;
485 
486 	gp = cp->geom;
487 
488 	LIST_FOREACH(pp, &gp->provider, provider)
489 		g_resize_provider(pp, cp->provider->mediasize);
490 }
491 
492 static int
493 g_mountver_ident_matches(struct g_geom *gp)
494 {
495 	struct g_consumer *cp;
496 	struct g_mountver_softc *sc;
497 	char ident[DISK_IDENT_SIZE];
498 	int error, identsize = DISK_IDENT_SIZE;
499 
500 	sc = gp->softc;
501 	cp = LIST_FIRST(&gp->consumer);
502 
503 	if (g_mountver_check_ident == 0)
504 		return (0);
505 
506 	error = g_access(cp, 1, 0, 0);
507 	if (error != 0) {
508 		G_MOUNTVER_DEBUG(0, "Cannot access %s; "
509 		    "not attaching; error = %d.", gp->name, error);
510 		return (1);
511 	}
512 	error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
513 	g_access(cp, -1, 0, 0);
514 	if (error != 0) {
515 		G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
516 		    "not attaching; error = %d.", gp->name, error);
517 		return (1);
518 	}
519 	if (strcmp(ident, sc->sc_ident) != 0) {
520 		G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
521 		    "from expected \"%s\", not attaching.", gp->name, ident,
522 		    sc->sc_ident);
523 		return (1);
524 	}
525 
526 	return (0);
527 }
528 
529 static struct g_geom *
530 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
531 {
532 	struct g_mountver_softc *sc;
533 	struct g_consumer *cp;
534 	struct g_geom *gp;
535 	int error;
536 
537 	g_topology_assert();
538 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
539 	G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
540 
541 	/*
542 	 * Let's check if device already exists.
543 	 */
544 	LIST_FOREACH(gp, &mp->geom, geom) {
545 		sc = gp->softc;
546 		if (sc == NULL)
547 			continue;
548 
549 		/* Already attached? */
550 		if (pp == LIST_FIRST(&gp->provider))
551 			return (NULL);
552 
553 		if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
554 			break;
555 	}
556 	if (gp == NULL)
557 		return (NULL);
558 
559 	cp = LIST_FIRST(&gp->consumer);
560 	g_attach(cp, pp);
561 	error = g_mountver_ident_matches(gp);
562 	if (error != 0) {
563 		g_detach(cp);
564 		return (NULL);
565 	}
566 	if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
567 		error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
568 		if (error != 0) {
569 			G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
570 			g_detach(cp);
571 			return (NULL);
572 		}
573 	}
574 	g_mountver_send_queued(gp);
575 	sc->sc_orphaned = 0;
576 	G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
577 
578 	return (gp);
579 }
580 
581 static void
582 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
583 {
584 	uint32_t *version;
585 
586 	g_topology_assert();
587 
588 	version = gctl_get_paraml(req, "version", sizeof(*version));
589 	if (version == NULL) {
590 		gctl_error(req, "No '%s' argument.", "version");
591 		return;
592 	}
593 	if (*version != G_MOUNTVER_VERSION) {
594 		gctl_error(req, "Userland and kernel parts are out of sync.");
595 		return;
596 	}
597 
598 	if (strcmp(verb, "create") == 0) {
599 		g_mountver_ctl_create(req, mp);
600 		return;
601 	} else if (strcmp(verb, "destroy") == 0) {
602 		g_mountver_ctl_destroy(req, mp);
603 		return;
604 	}
605 
606 	gctl_error(req, "Unknown verb.");
607 }
608 
609 static void
610 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
611     struct g_consumer *cp, struct g_provider *pp)
612 {
613 	struct g_mountver_softc *sc;
614 
615 	if (pp != NULL || cp != NULL)
616 		return;
617 
618 	sc = gp->softc;
619 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
620 	    sc->sc_orphaned ? "OFFLINE" : "ONLINE");
621 	sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
622 	sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
623 }
624 
625 static void
626 g_mountver_shutdown_pre_sync(void *arg, int howto)
627 {
628 	struct g_mountver_softc *sc;
629 	struct g_class *mp;
630 	struct g_geom *gp, *gp2;
631 
632 	mp = arg;
633 	g_topology_lock();
634 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
635 		if (gp->softc == NULL)
636 			continue;
637 		sc = gp->softc;
638 		sc->sc_shutting_down = 1;
639 		if (sc->sc_orphaned)
640 			g_mountver_destroy(gp, 1);
641 	}
642 	g_topology_unlock();
643 }
644 
645 static void
646 g_mountver_init(struct g_class *mp)
647 {
648 
649 	g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
650 	    g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
651 	if (g_mountver_pre_sync == NULL)
652 		G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
653 }
654 
655 static void
656 g_mountver_fini(struct g_class *mp)
657 {
658 
659 	if (g_mountver_pre_sync != NULL)
660 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
661 }
662 
663 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);
664 MODULE_VERSION(geom_mountver, 0);
665