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