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