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