xref: /freebsd/sys/geom/shsec/g_shsec.c (revision 4b50c451720d8b427757a6da1dd2bb4c52cd9e35)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bio.h>
39 #include <sys/sbuf.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <vm/uma.h>
43 #include <geom/geom.h>
44 #include <geom/geom_dbg.h>
45 #include <geom/shsec/g_shsec.h>
46 
47 FEATURE(geom_shsec, "GEOM shared secret device support");
48 
49 static MALLOC_DEFINE(M_SHSEC, "shsec_data", "GEOM_SHSEC Data");
50 
51 static uma_zone_t g_shsec_zone;
52 
53 static int g_shsec_destroy(struct g_shsec_softc *sc, boolean_t force);
54 static int g_shsec_destroy_geom(struct gctl_req *req, struct g_class *mp,
55     struct g_geom *gp);
56 
57 static g_taste_t g_shsec_taste;
58 static g_ctl_req_t g_shsec_config;
59 static g_dumpconf_t g_shsec_dumpconf;
60 static g_init_t g_shsec_init;
61 static g_fini_t g_shsec_fini;
62 
63 struct g_class g_shsec_class = {
64 	.name = G_SHSEC_CLASS_NAME,
65 	.version = G_VERSION,
66 	.ctlreq = g_shsec_config,
67 	.taste = g_shsec_taste,
68 	.destroy_geom = g_shsec_destroy_geom,
69 	.init = g_shsec_init,
70 	.fini = g_shsec_fini
71 };
72 
73 SYSCTL_DECL(_kern_geom);
74 static SYSCTL_NODE(_kern_geom, OID_AUTO, shsec, CTLFLAG_RW, 0,
75     "GEOM_SHSEC stuff");
76 static u_int g_shsec_debug = 0;
77 SYSCTL_UINT(_kern_geom_shsec, OID_AUTO, debug, CTLFLAG_RWTUN, &g_shsec_debug, 0,
78     "Debug level");
79 static u_int g_shsec_maxmem = MAXPHYS * 100;
80 SYSCTL_UINT(_kern_geom_shsec, OID_AUTO, maxmem, CTLFLAG_RDTUN, &g_shsec_maxmem,
81     0, "Maximum memory that can be allocated for I/O (in bytes)");
82 static u_int g_shsec_alloc_failed = 0;
83 SYSCTL_UINT(_kern_geom_shsec, OID_AUTO, alloc_failed, CTLFLAG_RD,
84     &g_shsec_alloc_failed, 0, "How many times I/O allocation failed");
85 
86 /*
87  * Greatest Common Divisor.
88  */
89 static u_int
90 gcd(u_int a, u_int b)
91 {
92 	u_int c;
93 
94 	while (b != 0) {
95 		c = a;
96 		a = b;
97 		b = (c % b);
98 	}
99 	return (a);
100 }
101 
102 /*
103  * Least Common Multiple.
104  */
105 static u_int
106 lcm(u_int a, u_int b)
107 {
108 
109 	return ((a * b) / gcd(a, b));
110 }
111 
112 static void
113 g_shsec_init(struct g_class *mp __unused)
114 {
115 
116 	g_shsec_zone = uma_zcreate("g_shsec_zone", MAXPHYS, NULL, NULL, NULL,
117 	    NULL, 0, 0);
118 	g_shsec_maxmem -= g_shsec_maxmem % MAXPHYS;
119 	uma_zone_set_max(g_shsec_zone, g_shsec_maxmem / MAXPHYS);
120 }
121 
122 static void
123 g_shsec_fini(struct g_class *mp __unused)
124 {
125 
126 	uma_zdestroy(g_shsec_zone);
127 }
128 
129 /*
130  * Return the number of valid disks.
131  */
132 static u_int
133 g_shsec_nvalid(struct g_shsec_softc *sc)
134 {
135 	u_int i, no;
136 
137 	no = 0;
138 	for (i = 0; i < sc->sc_ndisks; i++) {
139 		if (sc->sc_disks[i] != NULL)
140 			no++;
141 	}
142 
143 	return (no);
144 }
145 
146 static void
147 g_shsec_remove_disk(struct g_consumer *cp)
148 {
149 	struct g_shsec_softc *sc;
150 	u_int no;
151 
152 	KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
153 	sc = (struct g_shsec_softc *)cp->private;
154 	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
155 	no = cp->index;
156 
157 	G_SHSEC_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
158 	    sc->sc_name);
159 
160 	sc->sc_disks[no] = NULL;
161 	if (sc->sc_provider != NULL) {
162 		g_wither_provider(sc->sc_provider, ENXIO);
163 		sc->sc_provider = NULL;
164 		G_SHSEC_DEBUG(0, "Device %s removed.", sc->sc_name);
165 	}
166 
167 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
168 		return;
169 	g_detach(cp);
170 	g_destroy_consumer(cp);
171 }
172 
173 static void
174 g_shsec_orphan(struct g_consumer *cp)
175 {
176 	struct g_shsec_softc *sc;
177 	struct g_geom *gp;
178 
179 	g_topology_assert();
180 	gp = cp->geom;
181 	sc = gp->softc;
182 	if (sc == NULL)
183 		return;
184 
185 	g_shsec_remove_disk(cp);
186 	/* If there are no valid disks anymore, remove device. */
187 	if (LIST_EMPTY(&gp->consumer))
188 		g_shsec_destroy(sc, 1);
189 }
190 
191 static int
192 g_shsec_access(struct g_provider *pp, int dr, int dw, int de)
193 {
194 	struct g_consumer *cp1, *cp2, *tmp;
195 	struct g_shsec_softc *sc;
196 	struct g_geom *gp;
197 	int error;
198 
199 	gp = pp->geom;
200 	sc = gp->softc;
201 
202 	/* On first open, grab an extra "exclusive" bit */
203 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
204 		de++;
205 	/* ... and let go of it on last close */
206 	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
207 		de--;
208 
209 	error = ENXIO;
210 	LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
211 		error = g_access(cp1, dr, dw, de);
212 		if (error != 0)
213 			goto fail;
214 		if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
215 		    cp1->flags & G_CF_ORPHAN) {
216 			g_detach(cp1);
217 			g_destroy_consumer(cp1);
218 		}
219 	}
220 
221 	/* If there are no valid disks anymore, remove device. */
222 	if (LIST_EMPTY(&gp->consumer))
223 		g_shsec_destroy(sc, 1);
224 
225 	return (error);
226 
227 fail:
228 	/* If we fail here, backout all previous changes. */
229 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
230 		if (cp1 == cp2)
231 			break;
232 		g_access(cp2, -dr, -dw, -de);
233 	}
234 	return (error);
235 }
236 
237 static void
238 g_shsec_xor1(uint32_t *src, uint32_t *dst, ssize_t len)
239 {
240 
241 	for (; len > 0; len -= sizeof(uint32_t), dst++)
242 		*dst = *dst ^ *src++;
243 	KASSERT(len == 0, ("len != 0 (len=%zd)", len));
244 }
245 
246 static void
247 g_shsec_done(struct bio *bp)
248 {
249 	struct g_shsec_softc *sc;
250 	struct bio *pbp;
251 
252 	pbp = bp->bio_parent;
253 	sc = pbp->bio_to->geom->softc;
254 	if (bp->bio_error == 0)
255 		G_SHSEC_LOGREQ(2, bp, "Request done.");
256 	else {
257 		G_SHSEC_LOGREQ(0, bp, "Request failed (error=%d).",
258 		    bp->bio_error);
259 		if (pbp->bio_error == 0)
260 			pbp->bio_error = bp->bio_error;
261 	}
262 	if (pbp->bio_cmd == BIO_READ) {
263 		if ((pbp->bio_pflags & G_SHSEC_BFLAG_FIRST) != 0) {
264 			bcopy(bp->bio_data, pbp->bio_data, pbp->bio_length);
265 			pbp->bio_pflags = 0;
266 		} else {
267 			g_shsec_xor1((uint32_t *)bp->bio_data,
268 			    (uint32_t *)pbp->bio_data,
269 			    (ssize_t)pbp->bio_length);
270 		}
271 	}
272 	bzero(bp->bio_data, bp->bio_length);
273 	uma_zfree(g_shsec_zone, bp->bio_data);
274 	g_destroy_bio(bp);
275 	pbp->bio_inbed++;
276 	if (pbp->bio_children == pbp->bio_inbed) {
277 		pbp->bio_completed = pbp->bio_length;
278 		g_io_deliver(pbp, pbp->bio_error);
279 	}
280 }
281 
282 static void
283 g_shsec_xor2(uint32_t *rand, uint32_t *dst, ssize_t len)
284 {
285 
286 	for (; len > 0; len -= sizeof(uint32_t), dst++) {
287 		*rand = arc4random();
288 		*dst = *dst ^ *rand++;
289 	}
290 	KASSERT(len == 0, ("len != 0 (len=%zd)", len));
291 }
292 
293 static void
294 g_shsec_start(struct bio *bp)
295 {
296 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
297 	struct g_shsec_softc *sc;
298 	struct bio *cbp;
299 	uint32_t *dst;
300 	ssize_t len;
301 	u_int no;
302 	int error;
303 
304 	sc = bp->bio_to->geom->softc;
305 	/*
306 	 * If sc == NULL, provider's error should be set and g_shsec_start()
307 	 * should not be called at all.
308 	 */
309 	KASSERT(sc != NULL,
310 	    ("Provider's error should be set (error=%d)(device=%s).",
311 	    bp->bio_to->error, bp->bio_to->name));
312 
313 	G_SHSEC_LOGREQ(2, bp, "Request received.");
314 
315 	switch (bp->bio_cmd) {
316 	case BIO_READ:
317 	case BIO_WRITE:
318 	case BIO_FLUSH:
319 		/*
320 		 * Only those requests are supported.
321 		 */
322 		break;
323 	case BIO_DELETE:
324 	case BIO_GETATTR:
325 		/* To which provider it should be delivered? */
326 	default:
327 		g_io_deliver(bp, EOPNOTSUPP);
328 		return;
329 	}
330 
331 	/*
332 	 * Allocate all bios first and calculate XOR.
333 	 */
334 	dst = NULL;
335 	len = bp->bio_length;
336 	if (bp->bio_cmd == BIO_READ)
337 		bp->bio_pflags = G_SHSEC_BFLAG_FIRST;
338 	for (no = 0; no < sc->sc_ndisks; no++) {
339 		cbp = g_clone_bio(bp);
340 		if (cbp == NULL) {
341 			error = ENOMEM;
342 			goto failure;
343 		}
344 		TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
345 
346 		/*
347 		 * Fill in the component buf structure.
348 		 */
349 		cbp->bio_done = g_shsec_done;
350 		cbp->bio_data = uma_zalloc(g_shsec_zone, M_NOWAIT);
351 		if (cbp->bio_data == NULL) {
352 			g_shsec_alloc_failed++;
353 			error = ENOMEM;
354 			goto failure;
355 		}
356 		cbp->bio_caller2 = sc->sc_disks[no];
357 		if (bp->bio_cmd == BIO_WRITE) {
358 			if (no == 0) {
359 				dst = (uint32_t *)cbp->bio_data;
360 				bcopy(bp->bio_data, dst, len);
361 			} else {
362 				g_shsec_xor2((uint32_t *)cbp->bio_data, dst,
363 				    len);
364 			}
365 		}
366 	}
367 	/*
368 	 * Fire off all allocated requests!
369 	 */
370 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
371 		struct g_consumer *cp;
372 
373 		TAILQ_REMOVE(&queue, cbp, bio_queue);
374 		cp = cbp->bio_caller2;
375 		cbp->bio_caller2 = NULL;
376 		cbp->bio_to = cp->provider;
377 		G_SHSEC_LOGREQ(2, cbp, "Sending request.");
378 		g_io_request(cbp, cp);
379 	}
380 	return;
381 failure:
382 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
383 		TAILQ_REMOVE(&queue, cbp, bio_queue);
384 		bp->bio_children--;
385 		if (cbp->bio_data != NULL) {
386 			bzero(cbp->bio_data, cbp->bio_length);
387 			uma_zfree(g_shsec_zone, cbp->bio_data);
388 		}
389 		g_destroy_bio(cbp);
390 	}
391 	if (bp->bio_error == 0)
392 		bp->bio_error = error;
393 	g_io_deliver(bp, bp->bio_error);
394 }
395 
396 static void
397 g_shsec_check_and_run(struct g_shsec_softc *sc)
398 {
399 	off_t mediasize, ms;
400 	u_int no, sectorsize = 0;
401 
402 	if (g_shsec_nvalid(sc) != sc->sc_ndisks)
403 		return;
404 
405 	sc->sc_provider = g_new_providerf(sc->sc_geom, "shsec/%s", sc->sc_name);
406 	/*
407 	 * Find the smallest disk.
408 	 */
409 	mediasize = sc->sc_disks[0]->provider->mediasize;
410 	mediasize -= sc->sc_disks[0]->provider->sectorsize;
411 	sectorsize = sc->sc_disks[0]->provider->sectorsize;
412 	for (no = 1; no < sc->sc_ndisks; no++) {
413 		ms = sc->sc_disks[no]->provider->mediasize;
414 		ms -= sc->sc_disks[no]->provider->sectorsize;
415 		if (ms < mediasize)
416 			mediasize = ms;
417 		sectorsize = lcm(sectorsize,
418 		    sc->sc_disks[no]->provider->sectorsize);
419 	}
420 	sc->sc_provider->sectorsize = sectorsize;
421 	sc->sc_provider->mediasize = mediasize;
422 	g_error_provider(sc->sc_provider, 0);
423 
424 	G_SHSEC_DEBUG(0, "Device %s activated.", sc->sc_name);
425 }
426 
427 static int
428 g_shsec_read_metadata(struct g_consumer *cp, struct g_shsec_metadata *md)
429 {
430 	struct g_provider *pp;
431 	u_char *buf;
432 	int error;
433 
434 	g_topology_assert();
435 
436 	error = g_access(cp, 1, 0, 0);
437 	if (error != 0)
438 		return (error);
439 	pp = cp->provider;
440 	g_topology_unlock();
441 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
442 	    &error);
443 	g_topology_lock();
444 	g_access(cp, -1, 0, 0);
445 	if (buf == NULL)
446 		return (error);
447 
448 	/* Decode metadata. */
449 	shsec_metadata_decode(buf, md);
450 	g_free(buf);
451 
452 	return (0);
453 }
454 
455 /*
456  * Add disk to given device.
457  */
458 static int
459 g_shsec_add_disk(struct g_shsec_softc *sc, struct g_provider *pp, u_int no)
460 {
461 	struct g_consumer *cp, *fcp;
462 	struct g_geom *gp;
463 	struct g_shsec_metadata md;
464 	int error;
465 
466 	/* Metadata corrupted? */
467 	if (no >= sc->sc_ndisks)
468 		return (EINVAL);
469 
470 	/* Check if disk is not already attached. */
471 	if (sc->sc_disks[no] != NULL)
472 		return (EEXIST);
473 
474 	gp = sc->sc_geom;
475 	fcp = LIST_FIRST(&gp->consumer);
476 
477 	cp = g_new_consumer(gp);
478 	error = g_attach(cp, pp);
479 	if (error != 0) {
480 		g_destroy_consumer(cp);
481 		return (error);
482 	}
483 
484 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
485 		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
486 		if (error != 0) {
487 			g_detach(cp);
488 			g_destroy_consumer(cp);
489 			return (error);
490 		}
491 	}
492 
493 	/* Reread metadata. */
494 	error = g_shsec_read_metadata(cp, &md);
495 	if (error != 0)
496 		goto fail;
497 
498 	if (strcmp(md.md_magic, G_SHSEC_MAGIC) != 0 ||
499 	    strcmp(md.md_name, sc->sc_name) != 0 || md.md_id != sc->sc_id) {
500 		G_SHSEC_DEBUG(0, "Metadata on %s changed.", pp->name);
501 		goto fail;
502 	}
503 
504 	cp->private = sc;
505 	cp->index = no;
506 	sc->sc_disks[no] = cp;
507 
508 	G_SHSEC_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
509 
510 	g_shsec_check_and_run(sc);
511 
512 	return (0);
513 fail:
514 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
515 		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
516 	g_detach(cp);
517 	g_destroy_consumer(cp);
518 	return (error);
519 }
520 
521 static struct g_geom *
522 g_shsec_create(struct g_class *mp, const struct g_shsec_metadata *md)
523 {
524 	struct g_shsec_softc *sc;
525 	struct g_geom *gp;
526 	u_int no;
527 
528 	G_SHSEC_DEBUG(1, "Creating device %s (id=%u).", md->md_name, md->md_id);
529 
530 	/* Two disks is minimum. */
531 	if (md->md_all < 2) {
532 		G_SHSEC_DEBUG(0, "Too few disks defined for %s.", md->md_name);
533 		return (NULL);
534 	}
535 
536 	/* Check for duplicate unit */
537 	LIST_FOREACH(gp, &mp->geom, geom) {
538 		sc = gp->softc;
539 		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
540 			G_SHSEC_DEBUG(0, "Device %s already configured.",
541 			    sc->sc_name);
542 			return (NULL);
543 		}
544 	}
545 	gp = g_new_geomf(mp, "%s", md->md_name);
546 	sc = malloc(sizeof(*sc), M_SHSEC, M_WAITOK | M_ZERO);
547 	gp->start = g_shsec_start;
548 	gp->spoiled = g_shsec_orphan;
549 	gp->orphan = g_shsec_orphan;
550 	gp->access = g_shsec_access;
551 	gp->dumpconf = g_shsec_dumpconf;
552 
553 	sc->sc_id = md->md_id;
554 	sc->sc_ndisks = md->md_all;
555 	sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
556 	    M_SHSEC, M_WAITOK | M_ZERO);
557 	for (no = 0; no < sc->sc_ndisks; no++)
558 		sc->sc_disks[no] = NULL;
559 
560 	gp->softc = sc;
561 	sc->sc_geom = gp;
562 	sc->sc_provider = NULL;
563 
564 	G_SHSEC_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
565 
566 	return (gp);
567 }
568 
569 static int
570 g_shsec_destroy(struct g_shsec_softc *sc, boolean_t force)
571 {
572 	struct g_provider *pp;
573 	struct g_geom *gp;
574 	u_int no;
575 
576 	g_topology_assert();
577 
578 	if (sc == NULL)
579 		return (ENXIO);
580 
581 	pp = sc->sc_provider;
582 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
583 		if (force) {
584 			G_SHSEC_DEBUG(0, "Device %s is still open, so it "
585 			    "can't be definitely removed.", pp->name);
586 		} else {
587 			G_SHSEC_DEBUG(1,
588 			    "Device %s is still open (r%dw%de%d).", pp->name,
589 			    pp->acr, pp->acw, pp->ace);
590 			return (EBUSY);
591 		}
592 	}
593 
594 	for (no = 0; no < sc->sc_ndisks; no++) {
595 		if (sc->sc_disks[no] != NULL)
596 			g_shsec_remove_disk(sc->sc_disks[no]);
597 	}
598 
599 	gp = sc->sc_geom;
600 	gp->softc = NULL;
601 	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
602 	    gp->name));
603 	free(sc->sc_disks, M_SHSEC);
604 	free(sc, M_SHSEC);
605 
606 	pp = LIST_FIRST(&gp->provider);
607 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
608 		G_SHSEC_DEBUG(0, "Device %s destroyed.", gp->name);
609 
610 	g_wither_geom(gp, ENXIO);
611 
612 	return (0);
613 }
614 
615 static int
616 g_shsec_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
617     struct g_geom *gp)
618 {
619 	struct g_shsec_softc *sc;
620 
621 	sc = gp->softc;
622 	return (g_shsec_destroy(sc, 0));
623 }
624 
625 static struct g_geom *
626 g_shsec_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
627 {
628 	struct g_shsec_metadata md;
629 	struct g_shsec_softc *sc;
630 	struct g_consumer *cp;
631 	struct g_geom *gp;
632 	int error;
633 
634 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
635 	g_topology_assert();
636 
637 	/* Skip providers that are already open for writing. */
638 	if (pp->acw > 0)
639 		return (NULL);
640 
641 	G_SHSEC_DEBUG(3, "Tasting %s.", pp->name);
642 
643 	gp = g_new_geomf(mp, "shsec:taste");
644 	gp->start = g_shsec_start;
645 	gp->access = g_shsec_access;
646 	gp->orphan = g_shsec_orphan;
647 	cp = g_new_consumer(gp);
648 	g_attach(cp, pp);
649 	error = g_shsec_read_metadata(cp, &md);
650 	g_detach(cp);
651 	g_destroy_consumer(cp);
652 	g_destroy_geom(gp);
653 	if (error != 0)
654 		return (NULL);
655 	gp = NULL;
656 
657 	if (strcmp(md.md_magic, G_SHSEC_MAGIC) != 0)
658 		return (NULL);
659 	if (md.md_version > G_SHSEC_VERSION) {
660 		G_SHSEC_DEBUG(0, "Kernel module is too old to handle %s.\n",
661 		    pp->name);
662 		return (NULL);
663 	}
664 	/*
665 	 * Backward compatibility:
666 	 */
667 	/* There was no md_provsize field in earlier versions of metadata. */
668 	if (md.md_version < 1)
669 		md.md_provsize = pp->mediasize;
670 
671 	if (md.md_provider[0] != '\0' &&
672 	    !g_compare_names(md.md_provider, pp->name))
673 		return (NULL);
674 	if (md.md_provsize != pp->mediasize)
675 		return (NULL);
676 
677 	/*
678 	 * Let's check if device already exists.
679 	 */
680 	sc = NULL;
681 	LIST_FOREACH(gp, &mp->geom, geom) {
682 		sc = gp->softc;
683 		if (sc == NULL)
684 			continue;
685 		if (strcmp(md.md_name, sc->sc_name) != 0)
686 			continue;
687 		if (md.md_id != sc->sc_id)
688 			continue;
689 		break;
690 	}
691 	if (gp != NULL) {
692 		G_SHSEC_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
693 		error = g_shsec_add_disk(sc, pp, md.md_no);
694 		if (error != 0) {
695 			G_SHSEC_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
696 			    pp->name, gp->name, error);
697 			return (NULL);
698 		}
699 	} else {
700 		gp = g_shsec_create(mp, &md);
701 		if (gp == NULL) {
702 			G_SHSEC_DEBUG(0, "Cannot create device %s.", md.md_name);
703 			return (NULL);
704 		}
705 		sc = gp->softc;
706 		G_SHSEC_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
707 		error = g_shsec_add_disk(sc, pp, md.md_no);
708 		if (error != 0) {
709 			G_SHSEC_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
710 			    pp->name, gp->name, error);
711 			g_shsec_destroy(sc, 1);
712 			return (NULL);
713 		}
714 	}
715 	return (gp);
716 }
717 
718 static struct g_shsec_softc *
719 g_shsec_find_device(struct g_class *mp, const char *name)
720 {
721 	struct g_shsec_softc *sc;
722 	struct g_geom *gp;
723 
724 	LIST_FOREACH(gp, &mp->geom, geom) {
725 		sc = gp->softc;
726 		if (sc == NULL)
727 			continue;
728 		if (strcmp(sc->sc_name, name) == 0)
729 			return (sc);
730 	}
731 	return (NULL);
732 }
733 
734 static void
735 g_shsec_ctl_destroy(struct gctl_req *req, struct g_class *mp)
736 {
737 	struct g_shsec_softc *sc;
738 	int *force, *nargs, error;
739 	const char *name;
740 	char param[16];
741 	u_int i;
742 
743 	g_topology_assert();
744 
745 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
746 	if (nargs == NULL) {
747 		gctl_error(req, "No '%s' argument.", "nargs");
748 		return;
749 	}
750 	if (*nargs <= 0) {
751 		gctl_error(req, "Missing device(s).");
752 		return;
753 	}
754 	force = gctl_get_paraml(req, "force", sizeof(*force));
755 	if (force == NULL) {
756 		gctl_error(req, "No '%s' argument.", "force");
757 		return;
758 	}
759 
760 	for (i = 0; i < (u_int)*nargs; i++) {
761 		snprintf(param, sizeof(param), "arg%u", i);
762 		name = gctl_get_asciiparam(req, param);
763 		if (name == NULL) {
764 			gctl_error(req, "No 'arg%u' argument.", i);
765 			return;
766 		}
767 		sc = g_shsec_find_device(mp, name);
768 		if (sc == NULL) {
769 			gctl_error(req, "No such device: %s.", name);
770 			return;
771 		}
772 		error = g_shsec_destroy(sc, *force);
773 		if (error != 0) {
774 			gctl_error(req, "Cannot destroy device %s (error=%d).",
775 			    sc->sc_name, error);
776 			return;
777 		}
778 	}
779 }
780 
781 static void
782 g_shsec_config(struct gctl_req *req, struct g_class *mp, const char *verb)
783 {
784 	uint32_t *version;
785 
786 	g_topology_assert();
787 
788 	version = gctl_get_paraml(req, "version", sizeof(*version));
789 	if (version == NULL) {
790 		gctl_error(req, "No '%s' argument.", "version");
791 		return;
792 	}
793 	if (*version != G_SHSEC_VERSION) {
794 		gctl_error(req, "Userland and kernel parts are out of sync.");
795 		return;
796 	}
797 
798 	if (strcmp(verb, "stop") == 0) {
799 		g_shsec_ctl_destroy(req, mp);
800 		return;
801 	}
802 
803 	gctl_error(req, "Unknown verb.");
804 }
805 
806 static void
807 g_shsec_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
808     struct g_consumer *cp, struct g_provider *pp)
809 {
810 	struct g_shsec_softc *sc;
811 
812 	sc = gp->softc;
813 	if (sc == NULL)
814 		return;
815 	if (pp != NULL) {
816 		/* Nothing here. */
817 	} else if (cp != NULL) {
818 		sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
819 		    (u_int)cp->index);
820 	} else {
821 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
822 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
823 		    indent, sc->sc_ndisks, g_shsec_nvalid(sc));
824 		sbuf_printf(sb, "%s<State>", indent);
825 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
826 			sbuf_printf(sb, "UP");
827 		else
828 			sbuf_printf(sb, "DOWN");
829 		sbuf_printf(sb, "</State>\n");
830 	}
831 }
832 
833 DECLARE_GEOM_CLASS(g_shsec_class, g_shsec);
834 MODULE_VERSION(geom_shsec, 0);
835