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