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