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