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