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