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