2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 longpanda <admin@ventoy.net>
5 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * This file is just copied from g_concat.h and replace strings
33 * "concat" ==> "ventoy"
34 * "CONCAT" ==> "VENTOY"
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
51 #include <geom/geom.h>
52 #include <geom/ventoy/g_ventoy.h>
54 FEATURE(geom_ventoy
, "GEOM ventoy support");
56 static MALLOC_DEFINE(M_VENTOY
, "ventoy_data", "GEOM_VENTOY Data");
58 SYSCTL_DECL(_kern_geom
);
59 static SYSCTL_NODE(_kern_geom
, OID_AUTO
, ventoy
, CTLFLAG_RW
, 0,
61 static u_int g_ventoy_debug
= 0;
62 SYSCTL_UINT(_kern_geom_ventoy
, OID_AUTO
, debug
, CTLFLAG_RWTUN
, &g_ventoy_debug
, 0,
65 extern int resource_string_value(const char *name
, int unit
, const char *resname
, const char **result
);
66 extern int resource_int_value(const char *name
, int unit
, const char *resname
, int *result
);
68 static int g_ventoy_destroy(struct g_ventoy_softc
*sc
, boolean_t force
);
69 static int g_ventoy_destroy_geom(struct gctl_req
*req
, struct g_class
*mp
,
72 static g_taste_t g_ventoy_taste
;
73 static g_ctl_req_t g_ventoy_config
;
74 static g_dumpconf_t g_ventoy_dumpconf
;
76 static const char *g_ventoy_disk_uuid
= NULL
;
77 static bool g_ventoy_tasted
= false;
78 static off_t g_ventoy_disk_size
= 0;
79 static off_t g_disk_map_start
= 0;
80 static off_t g_disk_map_end
= 0;
82 struct g_class g_ventoy_class
= {
83 .name
= G_VENTOY_CLASS_NAME
,
85 .ctlreq
= g_ventoy_config
,
86 .taste
= g_ventoy_taste
,
87 .destroy_geom
= g_ventoy_destroy_geom
92 * Greatest Common Divisor.
108 * Least Common Multiple.
111 lcm(u_int a
, u_int b
)
114 return ((a
* b
) / gcd(a
, b
));
118 * Return the number of valid disks.
121 g_ventoy_nvalid(struct g_ventoy_softc
*sc
)
126 for (i
= 0; i
< sc
->sc_ndisks
; i
++) {
127 if (sc
->sc_disks
[i
].d_consumer
!= NULL
)
135 g_ventoy_remove_disk(struct g_ventoy_disk
*disk
)
137 struct g_consumer
*cp
;
138 struct g_ventoy_softc
*sc
;
141 KASSERT(disk
->d_consumer
!= NULL
, ("Non-valid disk in %s.", __func__
));
143 cp
= disk
->d_consumer
;
145 if (!disk
->d_removed
) {
146 G_VENTOY_DEBUG(0, "Disk %s removed from %s.",
147 cp
->provider
->name
, sc
->sc_name
);
151 if (sc
->sc_provider
!= NULL
) {
152 G_VENTOY_DEBUG(0, "Device %s deactivated.",
153 sc
->sc_provider
->name
);
154 g_wither_provider(sc
->sc_provider
, ENXIO
);
155 sc
->sc_provider
= NULL
;
158 if (cp
->acr
> 0 || cp
->acw
> 0 || cp
->ace
> 0)
160 disk
->d_consumer
= NULL
;
162 g_destroy_consumer(cp
);
163 /* If there are no valid disks anymore, remove device. */
164 if (LIST_EMPTY(&sc
->sc_geom
->consumer
))
165 g_ventoy_destroy(sc
, 1);
169 g_ventoy_orphan(struct g_consumer
*cp
)
171 struct g_ventoy_softc
*sc
;
172 struct g_ventoy_disk
*disk
;
182 if (disk
== NULL
) /* Possible? */
184 g_ventoy_remove_disk(disk
);
188 g_ventoy_access(struct g_provider
*pp
, int dr
, int dw
, int de
)
190 struct g_consumer
*cp1
, *cp2
, *tmp
;
191 struct g_ventoy_disk
*disk
;
195 if (dw
> 0) /* readonly */
201 /* On first open, grab an extra "exclusive" bit */
202 if (pp
->acr
== 0 && pp
->acw
== 0 && pp
->ace
== 0)
204 /* ... and let go of it on last close */
205 if ((pp
->acr
+ dr
) == 0 && (pp
->acw
+ dw
) == 0 && (pp
->ace
+ de
) == 0)
208 LIST_FOREACH_SAFE(cp1
, &gp
->consumer
, consumer
, tmp
) {
209 error
= g_access(cp1
, dr
, dw
, de
);
213 if (cp1
->acr
== 0 && cp1
->acw
== 0 && cp1
->ace
== 0 &&
215 g_ventoy_remove_disk(disk
); /* May destroy geom. */
221 LIST_FOREACH(cp2
, &gp
->consumer
, consumer
) {
224 g_access(cp2
, -dr
, -dw
, -de
);
230 g_ventoy_kernel_dump(struct bio
*bp
)
232 struct g_ventoy_softc
*sc
;
233 struct g_ventoy_disk
*disk
;
235 struct g_kerneldump
*gkd
;
238 sc
= bp
->bio_to
->geom
->softc
;
239 gkd
= (struct g_kerneldump
*)bp
->bio_data
;
240 for (i
= 0; i
< sc
->sc_ndisks
; i
++) {
241 if (sc
->sc_disks
[i
].d_start
<= gkd
->offset
&&
242 sc
->sc_disks
[i
].d_end
> gkd
->offset
)
245 if (i
== sc
->sc_ndisks
)
246 g_io_deliver(bp
, EOPNOTSUPP
);
247 disk
= &sc
->sc_disks
[i
];
248 gkd
->offset
-= disk
->d_start
;
249 if (gkd
->length
> disk
->d_end
- disk
->d_start
- gkd
->offset
)
250 gkd
->length
= disk
->d_end
- disk
->d_start
- gkd
->offset
;
251 cbp
= g_clone_bio(bp
);
253 g_io_deliver(bp
, ENOMEM
);
256 cbp
->bio_done
= g_std_done
;
257 g_io_request(cbp
, disk
->d_consumer
);
258 G_VENTOY_DEBUG(1, "Kernel dump will go to %s.",
259 disk
->d_consumer
->provider
->name
);
263 g_ventoy_done(struct bio
*bp
)
265 struct g_ventoy_softc
*sc
;
268 pbp
= bp
->bio_parent
;
269 sc
= pbp
->bio_to
->geom
->softc
;
270 mtx_lock(&sc
->sc_lock
);
271 if (pbp
->bio_error
== 0)
272 pbp
->bio_error
= bp
->bio_error
;
273 pbp
->bio_completed
+= bp
->bio_completed
;
275 if (pbp
->bio_children
== pbp
->bio_inbed
) {
276 mtx_unlock(&sc
->sc_lock
);
277 g_io_deliver(pbp
, pbp
->bio_error
);
279 mtx_unlock(&sc
->sc_lock
);
284 g_ventoy_flush(struct g_ventoy_softc
*sc
, struct bio
*bp
)
286 struct bio_queue_head queue
;
287 struct g_consumer
*cp
;
292 for (no
= 0; no
< sc
->sc_ndisks
; no
++) {
293 cbp
= g_clone_bio(bp
);
295 while ((cbp
= bioq_takefirst(&queue
)) != NULL
)
297 if (bp
->bio_error
== 0)
298 bp
->bio_error
= ENOMEM
;
299 g_io_deliver(bp
, bp
->bio_error
);
302 bioq_insert_tail(&queue
, cbp
);
303 cbp
->bio_done
= g_ventoy_done
;
304 cbp
->bio_caller1
= sc
->sc_disks
[no
].d_consumer
;
305 cbp
->bio_to
= sc
->sc_disks
[no
].d_consumer
->provider
;
307 while ((cbp
= bioq_takefirst(&queue
)) != NULL
) {
308 G_VENTOY_LOGREQ(cbp
, "Sending request.");
309 cp
= cbp
->bio_caller1
;
310 cbp
->bio_caller1
= NULL
;
311 g_io_request(cbp
, cp
);
316 g_ventoy_start(struct bio
*bp
)
318 struct bio_queue_head queue
;
319 struct g_ventoy_softc
*sc
;
320 struct g_ventoy_disk
*disk
;
321 struct g_provider
*pp
;
322 off_t offset
, end
, length
, off
, len
;
328 sc
= pp
->geom
->softc
;
330 * If sc == NULL, provider's error should be set and g_ventoy_start()
331 * should not be called at all.
334 ("Provider's error should be set (error=%d)(device=%s).",
335 bp
->bio_to
->error
, bp
->bio_to
->name
));
337 G_VENTOY_LOGREQ(bp
, "Request received.");
339 switch (bp
->bio_cmd
) {
345 g_ventoy_flush(sc
, bp
);
348 if (strcmp("GEOM::kerneldump", bp
->bio_attribute
) == 0) {
349 g_ventoy_kernel_dump(bp
);
352 /* To which provider it should be delivered? */
355 g_io_deliver(bp
, EOPNOTSUPP
);
359 offset
= bp
->bio_offset
;
360 length
= bp
->bio_length
;
361 if ((bp
->bio_flags
& BIO_UNMAPPED
) != 0)
365 end
= offset
+ length
;
368 for (no
= 0; no
< sc
->sc_ndisks
; no
++) {
369 disk
= &sc
->sc_disks
[no
];
370 if (disk
->d_end
<= offset
)
372 if (disk
->d_start
>= end
)
375 off
= offset
- disk
->d_start
;
376 len
= MIN(length
, disk
->d_end
- offset
);
380 cbp
= g_clone_bio(bp
);
382 while ((cbp
= bioq_takefirst(&queue
)) != NULL
)
384 if (bp
->bio_error
== 0)
385 bp
->bio_error
= ENOMEM
;
386 g_io_deliver(bp
, bp
->bio_error
);
389 bioq_insert_tail(&queue
, cbp
);
391 * Fill in the component buf structure.
393 if (len
== bp
->bio_length
)
394 cbp
->bio_done
= g_std_done
;
396 cbp
->bio_done
= g_ventoy_done
;
397 cbp
->bio_offset
= off
+ disk
->d_map_start
;
398 cbp
->bio_length
= len
;
399 if ((bp
->bio_flags
& BIO_UNMAPPED
) != 0) {
400 cbp
->bio_ma_offset
+= (uintptr_t)addr
;
401 cbp
->bio_ma
+= cbp
->bio_ma_offset
/ PAGE_SIZE
;
402 cbp
->bio_ma_offset
%= PAGE_SIZE
;
403 cbp
->bio_ma_n
= round_page(cbp
->bio_ma_offset
+
404 cbp
->bio_length
) / PAGE_SIZE
;
406 cbp
->bio_data
= addr
;
408 cbp
->bio_to
= disk
->d_consumer
->provider
;
409 cbp
->bio_caller1
= disk
;
415 ("Length is still greater than 0 (class=%s, name=%s).",
416 bp
->bio_to
->geom
->class->name
, bp
->bio_to
->geom
->name
));
417 while ((cbp
= bioq_takefirst(&queue
)) != NULL
) {
418 G_VENTOY_LOGREQ(cbp
, "Sending request.");
419 disk
= cbp
->bio_caller1
;
420 cbp
->bio_caller1
= NULL
;
421 g_io_request(cbp
, disk
->d_consumer
);
426 g_ventoy_check_and_run(struct g_ventoy_softc
*sc
)
428 struct g_ventoy_disk
*disk
;
429 struct g_provider
*dp
, *pp
;
430 u_int no
, sectorsize
= 0;
434 if (g_ventoy_nvalid(sc
) != sc
->sc_ndisks
)
437 pp
= g_new_providerf(sc
->sc_geom
, "ventoy/%s", sc
->sc_name
);
438 pp
->flags
|= G_PF_DIRECT_SEND
| G_PF_DIRECT_RECEIVE
|
439 G_PF_ACCEPT_UNMAPPED
;
441 for (no
= 0; no
< sc
->sc_ndisks
; no
++) {
442 disk
= &sc
->sc_disks
[no
];
443 dp
= disk
->d_consumer
->provider
;
444 disk
->d_start
= start
;
445 disk
->d_end
= disk
->d_start
+ (disk
->d_map_end
- disk
->d_map_start
);
446 if (sc
->sc_type
== G_VENTOY_TYPE_AUTOMATIC
)
447 disk
->d_end
-= dp
->sectorsize
;
450 sectorsize
= dp
->sectorsize
;
452 sectorsize
= lcm(sectorsize
, dp
->sectorsize
);
454 /* A provider underneath us doesn't support unmapped */
455 if ((dp
->flags
& G_PF_ACCEPT_UNMAPPED
) == 0) {
456 G_VENTOY_DEBUG(1, "Cancelling unmapped "
457 "because of %s.", dp
->name
);
458 pp
->flags
&= ~G_PF_ACCEPT_UNMAPPED
;
461 pp
->sectorsize
= sectorsize
;
462 /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
463 pp
->mediasize
= start
;
464 pp
->stripesize
= sc
->sc_disks
[0].d_consumer
->provider
->stripesize
;
465 pp
->stripeoffset
= sc
->sc_disks
[0].d_consumer
->provider
->stripeoffset
;
466 sc
->sc_provider
= pp
;
467 g_error_provider(pp
, 0);
469 G_VENTOY_DEBUG(0, "Device %s activated.", sc
->sc_provider
->name
);
473 g_ventoy_read_metadata(struct g_consumer
*cp
, struct g_ventoy_metadata
*md
)
475 struct g_provider
*pp
;
481 error
= g_access(cp
, 1, 0, 0);
486 buf
= g_read_data(cp
, pp
->mediasize
- pp
->sectorsize
, pp
->sectorsize
,
489 g_access(cp
, -1, 0, 0);
493 /* Decode metadata. */
494 ventoy_metadata_decode(buf
, md
);
501 * Add disk to given device.
504 g_ventoy_add_disk(struct g_ventoy_softc
*sc
, struct g_provider
*pp
, u_int no
)
506 struct g_ventoy_disk
*disk
;
507 struct g_consumer
*cp
, *fcp
;
512 /* Metadata corrupted? */
513 if (no
>= sc
->sc_ndisks
)
516 disk
= &sc
->sc_disks
[no
];
517 /* Check if disk is not already attached. */
518 if (disk
->d_consumer
!= NULL
)
522 fcp
= LIST_FIRST(&gp
->consumer
);
524 cp
= g_new_consumer(gp
);
525 cp
->flags
|= G_CF_DIRECT_SEND
| G_CF_DIRECT_RECEIVE
;
526 error
= g_attach(cp
, pp
);
528 g_destroy_consumer(cp
);
532 if (fcp
!= NULL
&& (fcp
->acr
> 0 || fcp
->acw
> 0 || fcp
->ace
> 0)) {
533 error
= g_access(cp
, fcp
->acr
, fcp
->acw
, fcp
->ace
);
536 g_destroy_consumer(cp
);
540 if (sc
->sc_type
== G_VENTOY_TYPE_AUTOMATIC
) {
541 struct g_ventoy_metadata md
;
543 /* Re-read metadata. */
544 error
= g_ventoy_read_metadata(cp
, &md
);
548 if (strcmp(md
.md_magic
, G_VENTOY_MAGIC
) != 0 ||
549 strcmp(md
.md_name
, sc
->sc_name
) != 0 ||
550 md
.md_id
!= sc
->sc_id
) {
551 G_VENTOY_DEBUG(0, "Metadata on %s changed.", pp
->name
);
557 disk
->d_consumer
= cp
;
559 disk
->d_start
= 0; /* not yet */
560 disk
->d_end
= 0; /* not yet */
563 disk
->d_map_start
= g_disk_map_start
;
564 disk
->d_map_end
= g_disk_map_end
;
566 G_VENTOY_DEBUG(0, "Disk %s attached to %s.", pp
->name
, sc
->sc_name
);
568 g_ventoy_check_and_run(sc
);
572 if (fcp
!= NULL
&& (fcp
->acr
> 0 || fcp
->acw
> 0 || fcp
->ace
> 0))
573 g_access(cp
, -fcp
->acr
, -fcp
->acw
, -fcp
->ace
);
575 g_destroy_consumer(cp
);
579 static struct g_geom
*
580 g_ventoy_create(struct g_class
*mp
, const struct g_ventoy_metadata
*md
,
583 struct g_ventoy_softc
*sc
;
587 G_VENTOY_DEBUG(1, "Creating device %s (id=%u).", md
->md_name
,
590 /* One disks is minimum. */
594 /* Check for duplicate unit */
595 LIST_FOREACH(gp
, &mp
->geom
, geom
) {
597 if (sc
!= NULL
&& strcmp(sc
->sc_name
, md
->md_name
) == 0) {
598 G_VENTOY_DEBUG(0, "Device %s already configured.",
603 gp
= g_new_geomf(mp
, "%s", md
->md_name
);
604 sc
= malloc(sizeof(*sc
), M_VENTOY
, M_WAITOK
| M_ZERO
);
605 gp
->start
= g_ventoy_start
;
606 gp
->spoiled
= g_ventoy_orphan
;
607 gp
->orphan
= g_ventoy_orphan
;
608 gp
->access
= g_ventoy_access
;
609 gp
->dumpconf
= g_ventoy_dumpconf
;
611 sc
->sc_id
= md
->md_id
;
612 sc
->sc_ndisks
= md
->md_all
;
613 sc
->sc_disks
= malloc(sizeof(struct g_ventoy_disk
) * sc
->sc_ndisks
,
614 M_VENTOY
, M_WAITOK
| M_ZERO
);
615 for (no
= 0; no
< sc
->sc_ndisks
; no
++)
616 sc
->sc_disks
[no
].d_consumer
= NULL
;
618 mtx_init(&sc
->sc_lock
, "gventoy lock", NULL
, MTX_DEF
);
622 sc
->sc_provider
= NULL
;
624 G_VENTOY_DEBUG(0, "Device %s created (id=%u).", sc
->sc_name
, sc
->sc_id
);
630 g_ventoy_destroy(struct g_ventoy_softc
*sc
, boolean_t force
)
632 struct g_provider
*pp
;
633 struct g_consumer
*cp
, *cp1
;
641 pp
= sc
->sc_provider
;
642 if (pp
!= NULL
&& (pp
->acr
!= 0 || pp
->acw
!= 0 || pp
->ace
!= 0)) {
644 G_VENTOY_DEBUG(0, "Device %s is still open, so it "
645 "can't be definitely removed.", pp
->name
);
648 "Device %s is still open (r%dw%de%d).", pp
->name
,
649 pp
->acr
, pp
->acw
, pp
->ace
);
655 LIST_FOREACH_SAFE(cp
, &gp
->consumer
, consumer
, cp1
) {
656 g_ventoy_remove_disk(cp
->private);
658 return (0); /* Recursion happened. */
660 if (!LIST_EMPTY(&gp
->consumer
))
661 return (EINPROGRESS
);
664 KASSERT(sc
->sc_provider
== NULL
, ("Provider still exists? (device=%s)",
666 free(sc
->sc_disks
, M_VENTOY
);
667 mtx_destroy(&sc
->sc_lock
);
670 G_VENTOY_DEBUG(0, "Device %s destroyed.", gp
->name
);
671 g_wither_geom(gp
, ENXIO
);
676 g_ventoy_destroy_geom(struct gctl_req
*req __unused
,
677 struct g_class
*mp __unused
, struct g_geom
*gp
)
679 struct g_ventoy_softc
*sc
;
682 return (g_ventoy_destroy(sc
, 0));
685 static bool g_vtoy_check_disk(struct g_class
*mp
, struct g_provider
*pp
)
691 struct g_consumer
*cp
;
694 if (g_ventoy_disk_size
== 0)
696 if (resource_string_value("ventoy", 0, "disksize", &value
) == 0)
698 G_DEBUG("ventoy.disksize: %s\n", value
);
699 g_ventoy_disk_size
= strtouq(value
, NULL
, 0);
702 if (resource_string_value("ventoy", 0, "diskuuid", &g_ventoy_disk_uuid
) == 0)
704 G_DEBUG("ventoy.diskuuid: <%s>\n", g_ventoy_disk_uuid
);
708 if (g_ventoy_disk_size
!= pp
->mediasize
)
713 if (strncmp(pp
->name
, "cd", 2) == 0 || strchr(pp
->name
, '/'))
718 /* read UUID from disk */
719 gp
= g_new_geomf(mp
, "ventoy:taste");
721 gp
->access
= g_ventoy_access
;
722 gp
->orphan
= g_ventoy_orphan
;
723 cp
= g_new_consumer(gp
);
726 g_access(cp
, 1, 0, 0);
728 buf
= g_read_data(cp
, 0, pp
->sectorsize
, NULL
);
730 g_access(cp
, -1, 0, 0);
733 g_destroy_consumer(cp
);
742 for (i
= 0; i
< 16; i
++)
744 sprintf(uuid
+ i
* 2, "%02x", buf
[0x180 + i
]);
748 if (strncmp(g_ventoy_disk_uuid
, uuid
, 32) == 0)
756 static struct g_geom
*
757 g_ventoy_taste(struct g_class
*mp
, struct g_provider
*pp
, int flags __unused
)
765 struct g_ventoy_metadata md
;
766 struct g_ventoy_softc
*sc
;
773 G_DEBUG("%s(%s, %s)\n", __func__
, mp
->name
, pp
->name
);
776 /* Skip providers that are already open for writing. */
780 if (!g_vtoy_check_disk(mp
, pp
))
785 g_ventoy_tasted
= true;
787 G_DEBUG("######### ventoy disk <%s> #############\n", pp
->name
);
789 resource_int_value("ventoy", 0, "segnum", &disknum
);
791 strlcpy(md
.md_magic
, G_VENTOY_MAGIC
, sizeof(md
.md_magic
));
792 md
.md_version
= G_VENTOY_VERSION
;
793 strlcpy(md
.md_name
, "IMAGE", sizeof(md
.md_name
));
794 md
.md_id
= arc4random();
796 md
.md_all
= (uint16_t)disknum
;
797 bzero(md
.md_provider
, sizeof(md
.md_provider
));
798 /* This field is not important here. */
801 gp
= g_ventoy_create(mp
, &md
, G_VENTOY_TYPE_MANUAL
);
803 G_VENTOY_DEBUG(0, "Cannot create device %s.",
809 for (i
= 0; i
< disknum
; i
++)
811 if (resource_string_value("ventoy", i
, "seg", &value
) == 0)
813 g_disk_map_start
= strtouq(value
, &endpos
, 0);
814 g_disk_map_end
= strtouq(endpos
+ 1, NULL
, 0);
818 printf("Failed to parse ventoy seg %d\n", i
);
822 G_DEBUG("ventoy segment%d: %s\n", i
, value
);
824 G_VENTOY_DEBUG(1, "Adding disk %s to %s.", pp
->name
, gp
->name
);
825 error
= g_ventoy_add_disk(sc
, pp
, i
);
828 "Cannot add disk %s to %s (error=%d).", pp
->name
,
830 g_ventoy_destroy(sc
, 1);
834 g_disk_map_start
= 0;
842 g_ventoy_ctl_create(struct gctl_req
*req
, struct g_class
*mp
)
845 struct g_ventoy_metadata md
;
846 struct g_provider
*pp
;
847 struct g_ventoy_softc
*sc
;
855 nargs
= gctl_get_paraml(req
, "nargs", sizeof(*nargs
));
857 gctl_error(req
, "No '%s' argument.", "nargs");
861 gctl_error(req
, "Too few arguments.");
865 strlcpy(md
.md_magic
, G_VENTOY_MAGIC
, sizeof(md
.md_magic
));
866 md
.md_version
= G_VENTOY_VERSION
;
867 name
= gctl_get_asciiparam(req
, "arg0");
869 gctl_error(req
, "No 'arg%u' argument.", 0);
872 strlcpy(md
.md_name
, name
, sizeof(md
.md_name
));
873 md
.md_id
= arc4random();
875 md
.md_all
= *nargs
- 1;
876 bzero(md
.md_provider
, sizeof(md
.md_provider
));
877 /* This field is not important here. */
880 /* Check all providers are valid */
881 for (no
= 1; no
< *nargs
; no
++) {
882 snprintf(param
, sizeof(param
), "arg%u", no
);
883 name
= gctl_get_asciiparam(req
, param
);
885 gctl_error(req
, "No 'arg%u' argument.", no
);
888 if (strncmp(name
, "/dev/", strlen("/dev/")) == 0)
889 name
+= strlen("/dev/");
890 pp
= g_provider_by_name(name
);
892 G_VENTOY_DEBUG(1, "Disk %s is invalid.", name
);
893 gctl_error(req
, "Disk %s is invalid.", name
);
898 gp
= g_ventoy_create(mp
, &md
, G_VENTOY_TYPE_MANUAL
);
900 gctl_error(req
, "Can't configure %s.", md
.md_name
);
905 sb
= sbuf_new_auto();
906 sbuf_printf(sb
, "Can't attach disk(s) to %s:", gp
->name
);
907 for (attached
= 0, no
= 1; no
< *nargs
; no
++) {
908 snprintf(param
, sizeof(param
), "arg%u", no
);
909 name
= gctl_get_asciiparam(req
, param
);
911 gctl_error(req
, "No 'arg%d' argument.", no
);
914 if (strncmp(name
, "/dev/", strlen("/dev/")) == 0)
915 name
+= strlen("/dev/");
916 pp
= g_provider_by_name(name
);
917 KASSERT(pp
!= NULL
, ("Provider %s disappear?!", name
));
918 if (g_ventoy_add_disk(sc
, pp
, no
- 1) != 0) {
919 G_VENTOY_DEBUG(1, "Disk %u (%s) not attached to %s.",
920 no
, pp
->name
, gp
->name
);
921 sbuf_printf(sb
, " %s", pp
->name
);
927 if (md
.md_all
!= attached
) {
928 g_ventoy_destroy(gp
->softc
, 1);
929 gctl_error(req
, "%s", sbuf_data(sb
));
934 static struct g_ventoy_softc
*
935 g_ventoy_find_device(struct g_class
*mp
, const char *name
)
937 struct g_ventoy_softc
*sc
;
940 LIST_FOREACH(gp
, &mp
->geom
, geom
) {
944 if (strcmp(sc
->sc_name
, name
) == 0)
951 g_ventoy_ctl_destroy(struct gctl_req
*req
, struct g_class
*mp
)
953 struct g_ventoy_softc
*sc
;
954 int *force
, *nargs
, error
;
961 nargs
= gctl_get_paraml(req
, "nargs", sizeof(*nargs
));
963 gctl_error(req
, "No '%s' argument.", "nargs");
967 gctl_error(req
, "Missing device(s).");
970 force
= gctl_get_paraml(req
, "force", sizeof(*force
));
972 gctl_error(req
, "No '%s' argument.", "force");
976 for (i
= 0; i
< (u_int
)*nargs
; i
++) {
977 snprintf(param
, sizeof(param
), "arg%u", i
);
978 name
= gctl_get_asciiparam(req
, param
);
980 gctl_error(req
, "No 'arg%u' argument.", i
);
983 sc
= g_ventoy_find_device(mp
, name
);
985 gctl_error(req
, "No such device: %s.", name
);
988 error
= g_ventoy_destroy(sc
, *force
);
990 gctl_error(req
, "Cannot destroy device %s (error=%d).",
998 g_ventoy_config(struct gctl_req
*req
, struct g_class
*mp
, const char *verb
)
1004 g_topology_assert();
1006 version
= gctl_get_paraml(req
, "version", sizeof(*version
));
1007 if (version
== NULL
) {
1008 gctl_error(req
, "No '%s' argument.", "version");
1011 if (*version
!= G_VENTOY_VERSION
) {
1012 gctl_error(req
, "Userland and kernel parts are out of sync.");
1016 if (strcmp(verb
, "create") == 0) {
1017 g_ventoy_ctl_create(req
, mp
);
1019 } else if (strcmp(verb
, "destroy") == 0 ||
1020 strcmp(verb
, "stop") == 0) {
1021 g_ventoy_ctl_destroy(req
, mp
);
1024 gctl_error(req
, "Unknown verb.");
1028 g_ventoy_dumpconf(struct sbuf
*sb
, const char *indent
, struct g_geom
*gp
,
1029 struct g_consumer
*cp
, struct g_provider
*pp
)
1031 struct g_ventoy_softc
*sc
;
1033 g_topology_assert();
1039 } else if (cp
!= NULL
) {
1040 struct g_ventoy_disk
*disk
;
1045 sbuf_printf(sb
, "%s<End>%jd</End>\n", indent
,
1046 (intmax_t)disk
->d_end
);
1047 sbuf_printf(sb
, "%s<Start>%jd</Start>\n", indent
,
1048 (intmax_t)disk
->d_start
);
1050 sbuf_printf(sb
, "%s<ID>%u</ID>\n", indent
, (u_int
)sc
->sc_id
);
1051 sbuf_printf(sb
, "%s<Type>", indent
);
1052 switch (sc
->sc_type
) {
1053 case G_VENTOY_TYPE_AUTOMATIC
:
1054 sbuf_cat(sb
, "AUTOMATIC");
1056 case G_VENTOY_TYPE_MANUAL
:
1057 sbuf_cat(sb
, "MANUAL");
1060 sbuf_cat(sb
, "UNKNOWN");
1063 sbuf_cat(sb
, "</Type>\n");
1064 sbuf_printf(sb
, "%s<Status>Total=%u, Online=%u</Status>\n",
1065 indent
, sc
->sc_ndisks
, g_ventoy_nvalid(sc
));
1066 sbuf_printf(sb
, "%s<State>", indent
);
1067 if (sc
->sc_provider
!= NULL
&& sc
->sc_provider
->error
== 0)
1070 sbuf_cat(sb
, "DOWN");
1071 sbuf_cat(sb
, "</State>\n");
1075 DECLARE_GEOM_CLASS(g_ventoy_class
, g_ventoy
);
1076 //MODULE_VERSION(geom_ventoy, 0);