]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Unix/ventoy_unix_src/FreeBSD/geom_ventoy_src/12.x/sys/geom/ventoy/g_ventoy.c
29ccc70701a097103c5ec1c4e6761c9bac1267f9
[Ventoy.git] / Unix / ventoy_unix_src / FreeBSD / geom_ventoy_src / 12.x / sys / geom / ventoy / g_ventoy.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2020 longpanda <admin@ventoy.net>
5 * Copyright (c) 2004-2005 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
31 /*
32 * This file is just copied from g_concat.h and replace strings
33 * "concat" ==> "ventoy"
34 * "CONCAT" ==> "VENTOY"
35 */
36
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/bio.h>
48 #include <sys/sbuf.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
51 #include <geom/geom.h>
52 #include <geom/ventoy/g_ventoy.h>
53
54 FEATURE(geom_ventoy, "GEOM ventoy support");
55
56 static MALLOC_DEFINE(M_VENTOY, "ventoy_data", "GEOM_VENTOY Data");
57
58 SYSCTL_DECL(_kern_geom);
59 static SYSCTL_NODE(_kern_geom, OID_AUTO, ventoy, CTLFLAG_RW, 0,
60 "GEOM_VENTOY stuff");
61 static u_int g_ventoy_debug = 0;
62 SYSCTL_UINT(_kern_geom_ventoy, OID_AUTO, debug, CTLFLAG_RWTUN, &g_ventoy_debug, 0,
63 "Debug level");
64
65 extern int resource_string_value(const char *name, int unit, const char *resname, const char **result);
66 extern int resource_int_value(const char *name, int unit, const char *resname, int *result);
67
68 static int g_ventoy_destroy(struct g_ventoy_softc *sc, boolean_t force);
69 static int g_ventoy_destroy_geom(struct gctl_req *req, struct g_class *mp,
70 struct g_geom *gp);
71
72 static g_taste_t g_ventoy_taste;
73 static g_ctl_req_t g_ventoy_config;
74 static g_dumpconf_t g_ventoy_dumpconf;
75
76 static const char *g_ventoy_disk_uuid = NULL;
77 static bool g_ventoy_tasted = false;
78 static off_t g_ventoy_disk_size = 0;
79 static off_t g_disk_map_start = 0;
80 static off_t g_disk_map_end = 0;
81
82 struct g_class g_ventoy_class = {
83 .name = G_VENTOY_CLASS_NAME,
84 .version = G_VERSION,
85 .ctlreq = g_ventoy_config,
86 .taste = g_ventoy_taste,
87 .destroy_geom = g_ventoy_destroy_geom
88 };
89
90
91 /*
92 * Greatest Common Divisor.
93 */
94 static u_int
95 gcd(u_int a, u_int b)
96 {
97 u_int c;
98
99 while (b != 0) {
100 c = a;
101 a = b;
102 b = (c % b);
103 }
104 return (a);
105 }
106
107 /*
108 * Least Common Multiple.
109 */
110 static u_int
111 lcm(u_int a, u_int b)
112 {
113
114 return ((a * b) / gcd(a, b));
115 }
116
117 /*
118 * Return the number of valid disks.
119 */
120 static u_int
121 g_ventoy_nvalid(struct g_ventoy_softc *sc)
122 {
123 u_int i, no;
124
125 no = 0;
126 for (i = 0; i < sc->sc_ndisks; i++) {
127 if (sc->sc_disks[i].d_consumer != NULL)
128 no++;
129 }
130
131 return (no);
132 }
133
134 static void
135 g_ventoy_remove_disk(struct g_ventoy_disk *disk)
136 {
137 struct g_consumer *cp;
138 struct g_ventoy_softc *sc;
139
140 g_topology_assert();
141 KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
142 sc = disk->d_softc;
143 cp = disk->d_consumer;
144
145 if (!disk->d_removed) {
146 G_VENTOY_DEBUG(0, "Disk %s removed from %s.",
147 cp->provider->name, sc->sc_name);
148 disk->d_removed = 1;
149 }
150
151 if (sc->sc_provider != NULL) {
152 G_VENTOY_DEBUG(0, "Device %s deactivated.",
153 sc->sc_provider->name);
154 g_wither_provider(sc->sc_provider, ENXIO);
155 sc->sc_provider = NULL;
156 }
157
158 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
159 return;
160 disk->d_consumer = NULL;
161 g_detach(cp);
162 g_destroy_consumer(cp);
163 /* If there are no valid disks anymore, remove device. */
164 if (LIST_EMPTY(&sc->sc_geom->consumer))
165 g_ventoy_destroy(sc, 1);
166 }
167
168 static void
169 g_ventoy_orphan(struct g_consumer *cp)
170 {
171 struct g_ventoy_softc *sc;
172 struct g_ventoy_disk *disk;
173 struct g_geom *gp;
174
175 g_topology_assert();
176 gp = cp->geom;
177 sc = gp->softc;
178 if (sc == NULL)
179 return;
180
181 disk = cp->private;
182 if (disk == NULL) /* Possible? */
183 return;
184 g_ventoy_remove_disk(disk);
185 }
186
187 static int
188 g_ventoy_access(struct g_provider *pp, int dr, int dw, int de)
189 {
190 struct g_consumer *cp1, *cp2, *tmp;
191 struct g_ventoy_disk *disk;
192 struct g_geom *gp;
193 int error;
194
195 if (dw > 0) /* readonly */
196 return (EPERM);
197
198 g_topology_assert();
199 gp = pp->geom;
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 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
209 error = g_access(cp1, dr, dw, de);
210 if (error != 0)
211 goto fail;
212 disk = cp1->private;
213 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
214 disk->d_removed) {
215 g_ventoy_remove_disk(disk); /* May destroy geom. */
216 }
217 }
218 return (0);
219
220 fail:
221 LIST_FOREACH(cp2, &gp->consumer, consumer) {
222 if (cp1 == cp2)
223 break;
224 g_access(cp2, -dr, -dw, -de);
225 }
226 return (error);
227 }
228
229 static void
230 g_ventoy_kernel_dump(struct bio *bp)
231 {
232 struct g_ventoy_softc *sc;
233 struct g_ventoy_disk *disk;
234 struct bio *cbp;
235 struct g_kerneldump *gkd;
236 u_int i;
237
238 sc = bp->bio_to->geom->softc;
239 gkd = (struct g_kerneldump *)bp->bio_data;
240 for (i = 0; i < sc->sc_ndisks; i++) {
241 if (sc->sc_disks[i].d_start <= gkd->offset &&
242 sc->sc_disks[i].d_end > gkd->offset)
243 break;
244 }
245 if (i == sc->sc_ndisks)
246 g_io_deliver(bp, EOPNOTSUPP);
247 disk = &sc->sc_disks[i];
248 gkd->offset -= disk->d_start;
249 if (gkd->length > disk->d_end - disk->d_start - gkd->offset)
250 gkd->length = disk->d_end - disk->d_start - gkd->offset;
251 cbp = g_clone_bio(bp);
252 if (cbp == NULL) {
253 g_io_deliver(bp, ENOMEM);
254 return;
255 }
256 cbp->bio_done = g_std_done;
257 g_io_request(cbp, disk->d_consumer);
258 G_VENTOY_DEBUG(1, "Kernel dump will go to %s.",
259 disk->d_consumer->provider->name);
260 }
261
262 static void
263 g_ventoy_done(struct bio *bp)
264 {
265 struct g_ventoy_softc *sc;
266 struct bio *pbp;
267
268 pbp = bp->bio_parent;
269 sc = pbp->bio_to->geom->softc;
270 mtx_lock(&sc->sc_lock);
271 if (pbp->bio_error == 0)
272 pbp->bio_error = bp->bio_error;
273 pbp->bio_completed += bp->bio_completed;
274 pbp->bio_inbed++;
275 if (pbp->bio_children == pbp->bio_inbed) {
276 mtx_unlock(&sc->sc_lock);
277 g_io_deliver(pbp, pbp->bio_error);
278 } else
279 mtx_unlock(&sc->sc_lock);
280 g_destroy_bio(bp);
281 }
282
283 static void
284 g_ventoy_flush(struct g_ventoy_softc *sc, struct bio *bp)
285 {
286 struct bio_queue_head queue;
287 struct g_consumer *cp;
288 struct bio *cbp;
289 u_int no;
290
291 bioq_init(&queue);
292 for (no = 0; no < sc->sc_ndisks; no++) {
293 cbp = g_clone_bio(bp);
294 if (cbp == NULL) {
295 while ((cbp = bioq_takefirst(&queue)) != NULL)
296 g_destroy_bio(cbp);
297 if (bp->bio_error == 0)
298 bp->bio_error = ENOMEM;
299 g_io_deliver(bp, bp->bio_error);
300 return;
301 }
302 bioq_insert_tail(&queue, cbp);
303 cbp->bio_done = g_ventoy_done;
304 cbp->bio_caller1 = sc->sc_disks[no].d_consumer;
305 cbp->bio_to = sc->sc_disks[no].d_consumer->provider;
306 }
307 while ((cbp = bioq_takefirst(&queue)) != NULL) {
308 G_VENTOY_LOGREQ(cbp, "Sending request.");
309 cp = cbp->bio_caller1;
310 cbp->bio_caller1 = NULL;
311 g_io_request(cbp, cp);
312 }
313 }
314
315 static void
316 g_ventoy_start(struct bio *bp)
317 {
318 struct bio_queue_head queue;
319 struct g_ventoy_softc *sc;
320 struct g_ventoy_disk *disk;
321 struct g_provider *pp;
322 off_t offset, end, length, off, len;
323 struct bio *cbp;
324 char *addr;
325 u_int no;
326
327 pp = bp->bio_to;
328 sc = pp->geom->softc;
329 /*
330 * If sc == NULL, provider's error should be set and g_ventoy_start()
331 * should not be called at all.
332 */
333 KASSERT(sc != NULL,
334 ("Provider's error should be set (error=%d)(device=%s).",
335 bp->bio_to->error, bp->bio_to->name));
336
337 G_VENTOY_LOGREQ(bp, "Request received.");
338
339 switch (bp->bio_cmd) {
340 case BIO_READ:
341 case BIO_WRITE:
342 case BIO_DELETE:
343 break;
344 case BIO_FLUSH:
345 g_ventoy_flush(sc, bp);
346 return;
347 case BIO_GETATTR:
348 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
349 g_ventoy_kernel_dump(bp);
350 return;
351 }
352 /* To which provider it should be delivered? */
353 /* FALLTHROUGH */
354 default:
355 g_io_deliver(bp, EOPNOTSUPP);
356 return;
357 }
358
359 offset = bp->bio_offset;
360 length = bp->bio_length;
361 if ((bp->bio_flags & BIO_UNMAPPED) != 0)
362 addr = NULL;
363 else
364 addr = bp->bio_data;
365 end = offset + length;
366
367 bioq_init(&queue);
368 for (no = 0; no < sc->sc_ndisks; no++) {
369 disk = &sc->sc_disks[no];
370 if (disk->d_end <= offset)
371 continue;
372 if (disk->d_start >= end)
373 break;
374
375 off = offset - disk->d_start;
376 len = MIN(length, disk->d_end - offset);
377 length -= len;
378 offset += len;
379
380 cbp = g_clone_bio(bp);
381 if (cbp == NULL) {
382 while ((cbp = bioq_takefirst(&queue)) != NULL)
383 g_destroy_bio(cbp);
384 if (bp->bio_error == 0)
385 bp->bio_error = ENOMEM;
386 g_io_deliver(bp, bp->bio_error);
387 return;
388 }
389 bioq_insert_tail(&queue, cbp);
390 /*
391 * Fill in the component buf structure.
392 */
393 if (len == bp->bio_length)
394 cbp->bio_done = g_std_done;
395 else
396 cbp->bio_done = g_ventoy_done;
397 cbp->bio_offset = off + disk->d_map_start;
398 cbp->bio_length = len;
399 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
400 cbp->bio_ma_offset += (uintptr_t)addr;
401 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
402 cbp->bio_ma_offset %= PAGE_SIZE;
403 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
404 cbp->bio_length) / PAGE_SIZE;
405 } else
406 cbp->bio_data = addr;
407 addr += len;
408 cbp->bio_to = disk->d_consumer->provider;
409 cbp->bio_caller1 = disk;
410
411 if (length == 0)
412 break;
413 }
414 KASSERT(length == 0,
415 ("Length is still greater than 0 (class=%s, name=%s).",
416 bp->bio_to->geom->class->name, bp->bio_to->geom->name));
417 while ((cbp = bioq_takefirst(&queue)) != NULL) {
418 G_VENTOY_LOGREQ(cbp, "Sending request.");
419 disk = cbp->bio_caller1;
420 cbp->bio_caller1 = NULL;
421 g_io_request(cbp, disk->d_consumer);
422 }
423 }
424
425 static void
426 g_ventoy_check_and_run(struct g_ventoy_softc *sc)
427 {
428 struct g_ventoy_disk *disk;
429 struct g_provider *dp, *pp;
430 u_int no, sectorsize = 0;
431 off_t start;
432
433 g_topology_assert();
434 if (g_ventoy_nvalid(sc) != sc->sc_ndisks)
435 return;
436
437 pp = g_new_providerf(sc->sc_geom, "ventoy/%s", sc->sc_name);
438 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
439 G_PF_ACCEPT_UNMAPPED;
440 start = 0;
441 for (no = 0; no < sc->sc_ndisks; no++) {
442 disk = &sc->sc_disks[no];
443 dp = disk->d_consumer->provider;
444 disk->d_start = start;
445 disk->d_end = disk->d_start + (disk->d_map_end - disk->d_map_start);
446 if (sc->sc_type == G_VENTOY_TYPE_AUTOMATIC)
447 disk->d_end -= dp->sectorsize;
448 start = disk->d_end;
449 if (no == 0)
450 sectorsize = dp->sectorsize;
451 else
452 sectorsize = lcm(sectorsize, dp->sectorsize);
453
454 /* A provider underneath us doesn't support unmapped */
455 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
456 G_VENTOY_DEBUG(1, "Cancelling unmapped "
457 "because of %s.", dp->name);
458 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
459 }
460 }
461 pp->sectorsize = sectorsize;
462 /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
463 pp->mediasize = start;
464 pp->stripesize = sc->sc_disks[0].d_consumer->provider->stripesize;
465 pp->stripeoffset = sc->sc_disks[0].d_consumer->provider->stripeoffset;
466 sc->sc_provider = pp;
467 g_error_provider(pp, 0);
468
469 G_VENTOY_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
470 }
471
472 static int
473 g_ventoy_read_metadata(struct g_consumer *cp, struct g_ventoy_metadata *md)
474 {
475 struct g_provider *pp;
476 u_char *buf;
477 int error;
478
479 g_topology_assert();
480
481 error = g_access(cp, 1, 0, 0);
482 if (error != 0)
483 return (error);
484 pp = cp->provider;
485 g_topology_unlock();
486 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
487 &error);
488 g_topology_lock();
489 g_access(cp, -1, 0, 0);
490 if (buf == NULL)
491 return (error);
492
493 /* Decode metadata. */
494 ventoy_metadata_decode(buf, md);
495 g_free(buf);
496
497 return (0);
498 }
499
500 /*
501 * Add disk to given device.
502 */
503 static int
504 g_ventoy_add_disk(struct g_ventoy_softc *sc, struct g_provider *pp, u_int no)
505 {
506 struct g_ventoy_disk *disk;
507 struct g_consumer *cp, *fcp;
508 struct g_geom *gp;
509 int error;
510
511 g_topology_assert();
512 /* Metadata corrupted? */
513 if (no >= sc->sc_ndisks)
514 return (EINVAL);
515
516 disk = &sc->sc_disks[no];
517 /* Check if disk is not already attached. */
518 if (disk->d_consumer != NULL)
519 return (EEXIST);
520
521 gp = sc->sc_geom;
522 fcp = LIST_FIRST(&gp->consumer);
523
524 cp = g_new_consumer(gp);
525 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
526 error = g_attach(cp, pp);
527 if (error != 0) {
528 g_destroy_consumer(cp);
529 return (error);
530 }
531
532 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
533 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
534 if (error != 0) {
535 g_detach(cp);
536 g_destroy_consumer(cp);
537 return (error);
538 }
539 }
540 if (sc->sc_type == G_VENTOY_TYPE_AUTOMATIC) {
541 struct g_ventoy_metadata md;
542
543 /* Re-read metadata. */
544 error = g_ventoy_read_metadata(cp, &md);
545 if (error != 0)
546 goto fail;
547
548 if (strcmp(md.md_magic, G_VENTOY_MAGIC) != 0 ||
549 strcmp(md.md_name, sc->sc_name) != 0 ||
550 md.md_id != sc->sc_id) {
551 G_VENTOY_DEBUG(0, "Metadata on %s changed.", pp->name);
552 goto fail;
553 }
554 }
555
556 cp->private = disk;
557 disk->d_consumer = cp;
558 disk->d_softc = sc;
559 disk->d_start = 0; /* not yet */
560 disk->d_end = 0; /* not yet */
561 disk->d_removed = 0;
562
563 disk->d_map_start = g_disk_map_start;
564 disk->d_map_end = g_disk_map_end;
565
566 G_VENTOY_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
567
568 g_ventoy_check_and_run(sc);
569
570 return (0);
571 fail:
572 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
573 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
574 g_detach(cp);
575 g_destroy_consumer(cp);
576 return (error);
577 }
578
579 static struct g_geom *
580 g_ventoy_create(struct g_class *mp, const struct g_ventoy_metadata *md,
581 u_int type)
582 {
583 struct g_ventoy_softc *sc;
584 struct g_geom *gp;
585 u_int no;
586
587 G_VENTOY_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
588 md->md_id);
589
590 /* One disks is minimum. */
591 if (md->md_all < 1)
592 return (NULL);
593
594 /* Check for duplicate unit */
595 LIST_FOREACH(gp, &mp->geom, geom) {
596 sc = gp->softc;
597 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
598 G_VENTOY_DEBUG(0, "Device %s already configured.",
599 gp->name);
600 return (NULL);
601 }
602 }
603 gp = g_new_geomf(mp, "%s", md->md_name);
604 sc = malloc(sizeof(*sc), M_VENTOY, M_WAITOK | M_ZERO);
605 gp->start = g_ventoy_start;
606 gp->spoiled = g_ventoy_orphan;
607 gp->orphan = g_ventoy_orphan;
608 gp->access = g_ventoy_access;
609 gp->dumpconf = g_ventoy_dumpconf;
610
611 sc->sc_id = md->md_id;
612 sc->sc_ndisks = md->md_all;
613 sc->sc_disks = malloc(sizeof(struct g_ventoy_disk) * sc->sc_ndisks,
614 M_VENTOY, M_WAITOK | M_ZERO);
615 for (no = 0; no < sc->sc_ndisks; no++)
616 sc->sc_disks[no].d_consumer = NULL;
617 sc->sc_type = type;
618 mtx_init(&sc->sc_lock, "gventoy lock", NULL, MTX_DEF);
619
620 gp->softc = sc;
621 sc->sc_geom = gp;
622 sc->sc_provider = NULL;
623
624 G_VENTOY_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
625
626 return (gp);
627 }
628
629 static int
630 g_ventoy_destroy(struct g_ventoy_softc *sc, boolean_t force)
631 {
632 struct g_provider *pp;
633 struct g_consumer *cp, *cp1;
634 struct g_geom *gp;
635
636 g_topology_assert();
637
638 if (sc == NULL)
639 return (ENXIO);
640
641 pp = sc->sc_provider;
642 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
643 if (force) {
644 G_VENTOY_DEBUG(0, "Device %s is still open, so it "
645 "can't be definitely removed.", pp->name);
646 } else {
647 G_VENTOY_DEBUG(1,
648 "Device %s is still open (r%dw%de%d).", pp->name,
649 pp->acr, pp->acw, pp->ace);
650 return (EBUSY);
651 }
652 }
653
654 gp = sc->sc_geom;
655 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
656 g_ventoy_remove_disk(cp->private);
657 if (cp1 == NULL)
658 return (0); /* Recursion happened. */
659 }
660 if (!LIST_EMPTY(&gp->consumer))
661 return (EINPROGRESS);
662
663 gp->softc = NULL;
664 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
665 gp->name));
666 free(sc->sc_disks, M_VENTOY);
667 mtx_destroy(&sc->sc_lock);
668 free(sc, M_VENTOY);
669
670 G_VENTOY_DEBUG(0, "Device %s destroyed.", gp->name);
671 g_wither_geom(gp, ENXIO);
672 return (0);
673 }
674
675 static int
676 g_ventoy_destroy_geom(struct gctl_req *req __unused,
677 struct g_class *mp __unused, struct g_geom *gp)
678 {
679 struct g_ventoy_softc *sc;
680
681 sc = gp->softc;
682 return (g_ventoy_destroy(sc, 0));
683 }
684
685 static bool g_vtoy_check_disk(struct g_class *mp, struct g_provider *pp)
686 {
687 int i;
688 uint8_t *buf;
689 char uuid[64];
690 const char *value;
691 struct g_consumer *cp;
692 struct g_geom *gp;
693
694 if (g_ventoy_disk_size == 0)
695 {
696 if (resource_string_value("ventoy", 0, "disksize", &value) == 0)
697 {
698 G_DEBUG("ventoy.disksize: %s\n", value);
699 g_ventoy_disk_size = strtouq(value, NULL, 0);
700 }
701
702 if (resource_string_value("ventoy", 0, "diskuuid", &g_ventoy_disk_uuid) == 0)
703 {
704 G_DEBUG("ventoy.diskuuid: <%s>\n", g_ventoy_disk_uuid);
705 }
706 }
707
708 if (g_ventoy_disk_size != pp->mediasize)
709 {
710 return false;
711 }
712
713 if (strncmp(pp->name, "cd", 2) == 0 || strchr(pp->name, '/'))
714 {
715 return false;
716 }
717
718 /* read UUID from disk */
719 gp = g_new_geomf(mp, "ventoy:taste");
720 gp->start = NULL;
721 gp->access = g_ventoy_access;
722 gp->orphan = g_ventoy_orphan;
723 cp = g_new_consumer(gp);
724 g_attach(cp, pp);
725
726 g_access(cp, 1, 0, 0);
727 g_topology_unlock();
728 buf = g_read_data(cp, 0, pp->sectorsize, NULL);
729 g_topology_lock();
730 g_access(cp, -1, 0, 0);
731
732 g_detach(cp);
733 g_destroy_consumer(cp);
734 g_destroy_geom(gp);
735 gp = NULL;
736
737 if (!buf)
738 {
739 return false;
740 }
741
742 for (i = 0; i < 16; i++)
743 {
744 sprintf(uuid + i * 2, "%02x", buf[0x180 + i]);
745 }
746 g_free(buf);
747
748 if (strncmp(g_ventoy_disk_uuid, uuid, 32) == 0)
749 {
750 return true;
751 }
752
753 return false;
754 }
755
756 static struct g_geom *
757 g_ventoy_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
758 {
759 int i;
760 int error;
761 int disknum;
762 char *endpos;
763 const char *value;
764 struct g_geom *gp;
765 struct g_ventoy_metadata md;
766 struct g_ventoy_softc *sc;
767
768 if (g_ventoy_tasted)
769 {
770 return NULL;
771 }
772
773 G_DEBUG("%s(%s, %s)\n", __func__, mp->name, pp->name);
774 g_topology_assert();
775
776 /* Skip providers that are already open for writing. */
777 if (pp->acw > 0)
778 return (NULL);
779
780 if (!g_vtoy_check_disk(mp, pp))
781 {
782 return NULL;
783 }
784
785 g_ventoy_tasted = true;
786
787 G_DEBUG("######### ventoy disk <%s> #############\n", pp->name);
788
789 resource_int_value("ventoy", 0, "segnum", &disknum);
790
791 strlcpy(md.md_magic, G_VENTOY_MAGIC, sizeof(md.md_magic));
792 md.md_version = G_VENTOY_VERSION;
793 strlcpy(md.md_name, "IMAGE", sizeof(md.md_name));
794 md.md_id = arc4random();
795 md.md_no = 0;
796 md.md_all = (uint16_t)disknum;
797 bzero(md.md_provider, sizeof(md.md_provider));
798 /* This field is not important here. */
799 md.md_provsize = 0;
800
801 gp = g_ventoy_create(mp, &md, G_VENTOY_TYPE_MANUAL);
802 if (gp == NULL) {
803 G_VENTOY_DEBUG(0, "Cannot create device %s.",
804 md.md_name);
805 return (NULL);
806 }
807 sc = gp->softc;
808
809 for (i = 0; i < disknum; i ++)
810 {
811 if (resource_string_value("ventoy", i, "seg", &value) == 0)
812 {
813 g_disk_map_start = strtouq(value, &endpos, 0);
814 g_disk_map_end = strtouq(endpos + 1, NULL, 0);
815 }
816 else
817 {
818 printf("Failed to parse ventoy seg %d\n", i);
819 continue;
820 }
821
822 G_DEBUG("ventoy segment%d: %s\n", i, value);
823
824 G_VENTOY_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
825 error = g_ventoy_add_disk(sc, pp, i);
826 if (error != 0) {
827 G_VENTOY_DEBUG(0,
828 "Cannot add disk %s to %s (error=%d).", pp->name,
829 gp->name, error);
830 g_ventoy_destroy(sc, 1);
831 return (NULL);
832 }
833
834 g_disk_map_start = 0;
835 g_disk_map_end = 0;
836 }
837
838 return (gp);
839 }
840
841 static void
842 g_ventoy_ctl_create(struct gctl_req *req, struct g_class *mp)
843 {
844 u_int attached, no;
845 struct g_ventoy_metadata md;
846 struct g_provider *pp;
847 struct g_ventoy_softc *sc;
848 struct g_geom *gp;
849 struct sbuf *sb;
850 const char *name;
851 char param[16];
852 int *nargs;
853
854 g_topology_assert();
855 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
856 if (nargs == NULL) {
857 gctl_error(req, "No '%s' argument.", "nargs");
858 return;
859 }
860 if (*nargs < 2) {
861 gctl_error(req, "Too few arguments.");
862 return;
863 }
864
865 strlcpy(md.md_magic, G_VENTOY_MAGIC, sizeof(md.md_magic));
866 md.md_version = G_VENTOY_VERSION;
867 name = gctl_get_asciiparam(req, "arg0");
868 if (name == NULL) {
869 gctl_error(req, "No 'arg%u' argument.", 0);
870 return;
871 }
872 strlcpy(md.md_name, name, sizeof(md.md_name));
873 md.md_id = arc4random();
874 md.md_no = 0;
875 md.md_all = *nargs - 1;
876 bzero(md.md_provider, sizeof(md.md_provider));
877 /* This field is not important here. */
878 md.md_provsize = 0;
879
880 /* Check all providers are valid */
881 for (no = 1; no < *nargs; no++) {
882 snprintf(param, sizeof(param), "arg%u", no);
883 name = gctl_get_asciiparam(req, param);
884 if (name == NULL) {
885 gctl_error(req, "No 'arg%u' argument.", no);
886 return;
887 }
888 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
889 name += strlen("/dev/");
890 pp = g_provider_by_name(name);
891 if (pp == NULL) {
892 G_VENTOY_DEBUG(1, "Disk %s is invalid.", name);
893 gctl_error(req, "Disk %s is invalid.", name);
894 return;
895 }
896 }
897
898 gp = g_ventoy_create(mp, &md, G_VENTOY_TYPE_MANUAL);
899 if (gp == NULL) {
900 gctl_error(req, "Can't configure %s.", md.md_name);
901 return;
902 }
903
904 sc = gp->softc;
905 sb = sbuf_new_auto();
906 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
907 for (attached = 0, no = 1; no < *nargs; no++) {
908 snprintf(param, sizeof(param), "arg%u", no);
909 name = gctl_get_asciiparam(req, param);
910 if (name == NULL) {
911 gctl_error(req, "No 'arg%d' argument.", no);
912 return;
913 }
914 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
915 name += strlen("/dev/");
916 pp = g_provider_by_name(name);
917 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
918 if (g_ventoy_add_disk(sc, pp, no - 1) != 0) {
919 G_VENTOY_DEBUG(1, "Disk %u (%s) not attached to %s.",
920 no, pp->name, gp->name);
921 sbuf_printf(sb, " %s", pp->name);
922 continue;
923 }
924 attached++;
925 }
926 sbuf_finish(sb);
927 if (md.md_all != attached) {
928 g_ventoy_destroy(gp->softc, 1);
929 gctl_error(req, "%s", sbuf_data(sb));
930 }
931 sbuf_delete(sb);
932 }
933
934 static struct g_ventoy_softc *
935 g_ventoy_find_device(struct g_class *mp, const char *name)
936 {
937 struct g_ventoy_softc *sc;
938 struct g_geom *gp;
939
940 LIST_FOREACH(gp, &mp->geom, geom) {
941 sc = gp->softc;
942 if (sc == NULL)
943 continue;
944 if (strcmp(sc->sc_name, name) == 0)
945 return (sc);
946 }
947 return (NULL);
948 }
949
950 static void
951 g_ventoy_ctl_destroy(struct gctl_req *req, struct g_class *mp)
952 {
953 struct g_ventoy_softc *sc;
954 int *force, *nargs, error;
955 const char *name;
956 char param[16];
957 u_int i;
958
959 g_topology_assert();
960
961 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
962 if (nargs == NULL) {
963 gctl_error(req, "No '%s' argument.", "nargs");
964 return;
965 }
966 if (*nargs <= 0) {
967 gctl_error(req, "Missing device(s).");
968 return;
969 }
970 force = gctl_get_paraml(req, "force", sizeof(*force));
971 if (force == NULL) {
972 gctl_error(req, "No '%s' argument.", "force");
973 return;
974 }
975
976 for (i = 0; i < (u_int)*nargs; i++) {
977 snprintf(param, sizeof(param), "arg%u", i);
978 name = gctl_get_asciiparam(req, param);
979 if (name == NULL) {
980 gctl_error(req, "No 'arg%u' argument.", i);
981 return;
982 }
983 sc = g_ventoy_find_device(mp, name);
984 if (sc == NULL) {
985 gctl_error(req, "No such device: %s.", name);
986 return;
987 }
988 error = g_ventoy_destroy(sc, *force);
989 if (error != 0) {
990 gctl_error(req, "Cannot destroy device %s (error=%d).",
991 sc->sc_name, error);
992 return;
993 }
994 }
995 }
996
997 static void
998 g_ventoy_config(struct gctl_req *req, struct g_class *mp, const char *verb)
999 {
1000 uint32_t *version;
1001
1002 return;
1003
1004 g_topology_assert();
1005
1006 version = gctl_get_paraml(req, "version", sizeof(*version));
1007 if (version == NULL) {
1008 gctl_error(req, "No '%s' argument.", "version");
1009 return;
1010 }
1011 if (*version != G_VENTOY_VERSION) {
1012 gctl_error(req, "Userland and kernel parts are out of sync.");
1013 return;
1014 }
1015
1016 if (strcmp(verb, "create") == 0) {
1017 g_ventoy_ctl_create(req, mp);
1018 return;
1019 } else if (strcmp(verb, "destroy") == 0 ||
1020 strcmp(verb, "stop") == 0) {
1021 g_ventoy_ctl_destroy(req, mp);
1022 return;
1023 }
1024 gctl_error(req, "Unknown verb.");
1025 }
1026
1027 static void
1028 g_ventoy_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1029 struct g_consumer *cp, struct g_provider *pp)
1030 {
1031 struct g_ventoy_softc *sc;
1032
1033 g_topology_assert();
1034 sc = gp->softc;
1035 if (sc == NULL)
1036 return;
1037 if (pp != NULL) {
1038 /* Nothing here. */
1039 } else if (cp != NULL) {
1040 struct g_ventoy_disk *disk;
1041
1042 disk = cp->private;
1043 if (disk == NULL)
1044 return;
1045 sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
1046 (intmax_t)disk->d_end);
1047 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
1048 (intmax_t)disk->d_start);
1049 } else {
1050 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1051 sbuf_printf(sb, "%s<Type>", indent);
1052 switch (sc->sc_type) {
1053 case G_VENTOY_TYPE_AUTOMATIC:
1054 sbuf_cat(sb, "AUTOMATIC");
1055 break;
1056 case G_VENTOY_TYPE_MANUAL:
1057 sbuf_cat(sb, "MANUAL");
1058 break;
1059 default:
1060 sbuf_cat(sb, "UNKNOWN");
1061 break;
1062 }
1063 sbuf_cat(sb, "</Type>\n");
1064 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1065 indent, sc->sc_ndisks, g_ventoy_nvalid(sc));
1066 sbuf_printf(sb, "%s<State>", indent);
1067 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1068 sbuf_cat(sb, "UP");
1069 else
1070 sbuf_cat(sb, "DOWN");
1071 sbuf_cat(sb, "</State>\n");
1072 }
1073 }
1074
1075 DECLARE_GEOM_CLASS(g_ventoy_class, g_ventoy);
1076 //MODULE_VERSION(geom_ventoy, 0);