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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <geom/geom.h>
44 #include <geom/ventoy/g_ventoy.h>
46 FEATURE(geom_ventoy
, "GEOM ventoy support");
48 static MALLOC_DEFINE(M_VENTOY
, "ventoy_data", "GEOM_VENTOY Data");
50 SYSCTL_DECL(_kern_geom
);
51 static SYSCTL_NODE(_kern_geom
, OID_AUTO
, ventoy
, CTLFLAG_RW
, 0,
53 static u_int g_ventoy_debug
= 0;
54 SYSCTL_UINT(_kern_geom_ventoy
, OID_AUTO
, debug
, CTLFLAG_RWTUN
, &g_ventoy_debug
, 0,
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
);
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
,
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
;
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;
74 struct g_class g_ventoy_class
= {
75 .name
= G_VENTOY_CLASS_NAME
,
77 .ctlreq
= g_ventoy_config
,
78 .taste
= g_ventoy_taste
,
79 .destroy_geom
= g_ventoy_destroy_geom
84 * Greatest Common Divisor.
100 * Least Common Multiple.
103 lcm(u_int a
, u_int b
)
106 return ((a
* b
) / gcd(a
, b
));
110 * Return the number of valid disks.
113 g_ventoy_nvalid(struct g_ventoy_softc
*sc
)
118 for (i
= 0; i
< sc
->sc_ndisks
; i
++) {
119 if (sc
->sc_disks
[i
].d_consumer
!= NULL
)
127 g_ventoy_remove_disk(struct g_ventoy_disk
*disk
)
129 struct g_consumer
*cp
;
130 struct g_ventoy_softc
*sc
;
133 KASSERT(disk
->d_consumer
!= NULL
, ("Non-valid disk in %s.", __func__
));
135 cp
= disk
->d_consumer
;
137 if (!disk
->d_removed
) {
138 G_VENTOY_DEBUG(0, "Disk %s removed from %s.",
139 cp
->provider
->name
, sc
->sc_name
);
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
;
150 if (cp
->acr
> 0 || cp
->acw
> 0 || cp
->ace
> 0)
152 disk
->d_consumer
= NULL
;
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);
161 g_ventoy_orphan(struct g_consumer
*cp
)
163 struct g_ventoy_softc
*sc
;
164 struct g_ventoy_disk
*disk
;
174 if (disk
== NULL
) /* Possible? */
176 g_ventoy_remove_disk(disk
);
180 g_ventoy_access(struct g_provider
*pp
, int dr
, int dw
, int de
)
182 struct g_consumer
*cp1
, *cp2
, *tmp
;
183 struct g_ventoy_disk
*disk
;
187 if (dw
> 0) /* readonly */
193 /* On first open, grab an extra "exclusive" bit */
194 if (pp
->acr
== 0 && pp
->acw
== 0 && pp
->ace
== 0)
196 /* ... and let go of it on last close */
197 if ((pp
->acr
+ dr
) == 0 && (pp
->acw
+ dw
) == 0 && (pp
->ace
+ de
) == 0)
200 LIST_FOREACH_SAFE(cp1
, &gp
->consumer
, consumer
, tmp
) {
201 error
= g_access(cp1
, dr
, dw
, de
);
205 if (cp1
->acr
== 0 && cp1
->acw
== 0 && cp1
->ace
== 0 &&
207 g_ventoy_remove_disk(disk
); /* May destroy geom. */
213 LIST_FOREACH(cp2
, &gp
->consumer
, consumer
) {
216 g_access(cp2
, -dr
, -dw
, -de
);
222 g_ventoy_kernel_dump(struct bio
*bp
)
224 struct g_ventoy_softc
*sc
;
225 struct g_ventoy_disk
*disk
;
227 struct g_kerneldump
*gkd
;
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
)
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
);
245 g_io_deliver(bp
, ENOMEM
);
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
);
255 g_ventoy_done(struct bio
*bp
)
257 struct g_ventoy_softc
*sc
;
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
;
267 if (pbp
->bio_children
== pbp
->bio_inbed
) {
268 mtx_unlock(&sc
->sc_lock
);
269 g_io_deliver(pbp
, pbp
->bio_error
);
271 mtx_unlock(&sc
->sc_lock
);
276 g_ventoy_flush(struct g_ventoy_softc
*sc
, struct bio
*bp
)
278 struct bio_queue_head queue
;
279 struct g_consumer
*cp
;
284 for (no
= 0; no
< sc
->sc_ndisks
; no
++) {
285 cbp
= g_clone_bio(bp
);
287 while ((cbp
= bioq_takefirst(&queue
)) != NULL
)
289 if (bp
->bio_error
== 0)
290 bp
->bio_error
= ENOMEM
;
291 g_io_deliver(bp
, bp
->bio_error
);
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
;
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
);
308 g_ventoy_start(struct bio
*bp
)
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
;
320 sc
= pp
->geom
->softc
;
322 * If sc == NULL, provider's error should be set and g_ventoy_start()
323 * should not be called at all.
326 ("Provider's error should be set (error=%d)(device=%s).",
327 bp
->bio_to
->error
, bp
->bio_to
->name
));
329 G_VENTOY_LOGREQ(bp
, "Request received.");
331 switch (bp
->bio_cmd
) {
337 g_ventoy_flush(sc
, bp
);
340 if (strcmp("GEOM::kerneldump", bp
->bio_attribute
) == 0) {
341 g_ventoy_kernel_dump(bp
);
344 /* To which provider it should be delivered? */
347 g_io_deliver(bp
, EOPNOTSUPP
);
351 offset
= bp
->bio_offset
;
352 length
= bp
->bio_length
;
353 if ((bp
->bio_flags
& BIO_UNMAPPED
) != 0)
357 end
= offset
+ length
;
360 for (no
= 0; no
< sc
->sc_ndisks
; no
++) {
361 disk
= &sc
->sc_disks
[no
];
362 if (disk
->d_end
<= offset
)
364 if (disk
->d_start
>= end
)
367 off
= offset
- disk
->d_start
;
368 len
= MIN(length
, disk
->d_end
- offset
);
372 cbp
= g_clone_bio(bp
);
374 while ((cbp
= bioq_takefirst(&queue
)) != NULL
)
376 if (bp
->bio_error
== 0)
377 bp
->bio_error
= ENOMEM
;
378 g_io_deliver(bp
, bp
->bio_error
);
381 bioq_insert_tail(&queue
, cbp
);
383 * Fill in the component buf structure.
385 if (len
== bp
->bio_length
)
386 cbp
->bio_done
= g_std_done
;
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
;
398 cbp
->bio_data
= addr
;
400 cbp
->bio_to
= disk
->d_consumer
->provider
;
401 cbp
->bio_caller1
= disk
;
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
);
418 g_ventoy_check_and_run(struct g_ventoy_softc
*sc
)
420 struct g_ventoy_disk
*disk
;
421 struct g_provider
*dp
, *pp
;
422 u_int no
, sectorsize
= 0;
426 if (g_ventoy_nvalid(sc
) != sc
->sc_ndisks
)
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
;
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
;
442 sectorsize
= dp
->sectorsize
;
444 sectorsize
= lcm(sectorsize
, dp
->sectorsize
);
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
;
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);
461 G_VENTOY_DEBUG(0, "Device %s activated.", sc
->sc_provider
->name
);
465 g_ventoy_read_metadata(struct g_consumer
*cp
, struct g_ventoy_metadata
*md
)
467 struct g_provider
*pp
;
473 error
= g_access(cp
, 1, 0, 0);
478 buf
= g_read_data(cp
, pp
->mediasize
- pp
->sectorsize
, pp
->sectorsize
,
481 g_access(cp
, -1, 0, 0);
485 /* Decode metadata. */
486 ventoy_metadata_decode(buf
, md
);
493 * Add disk to given device.
496 g_ventoy_add_disk(struct g_ventoy_softc
*sc
, struct g_provider
*pp
, u_int no
)
498 struct g_ventoy_disk
*disk
;
499 struct g_consumer
*cp
, *fcp
;
504 /* Metadata corrupted? */
505 if (no
>= sc
->sc_ndisks
)
508 disk
= &sc
->sc_disks
[no
];
509 /* Check if disk is not already attached. */
510 if (disk
->d_consumer
!= NULL
)
514 fcp
= LIST_FIRST(&gp
->consumer
);
516 cp
= g_new_consumer(gp
);
517 cp
->flags
|= G_CF_DIRECT_SEND
| G_CF_DIRECT_RECEIVE
;
518 error
= g_attach(cp
, pp
);
520 g_destroy_consumer(cp
);
524 if (fcp
!= NULL
&& (fcp
->acr
> 0 || fcp
->acw
> 0 || fcp
->ace
> 0)) {
525 error
= g_access(cp
, fcp
->acr
, fcp
->acw
, fcp
->ace
);
528 g_destroy_consumer(cp
);
532 if (sc
->sc_type
== G_VENTOY_TYPE_AUTOMATIC
) {
533 struct g_ventoy_metadata md
;
535 /* Re-read metadata. */
536 error
= g_ventoy_read_metadata(cp
, &md
);
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
);
549 disk
->d_consumer
= cp
;
551 disk
->d_start
= 0; /* not yet */
552 disk
->d_end
= 0; /* not yet */
555 disk
->d_map_start
= g_disk_map_start
;
556 disk
->d_map_end
= g_disk_map_end
;
558 G_VENTOY_DEBUG(0, "Disk %s attached to %s.", pp
->name
, sc
->sc_name
);
560 g_ventoy_check_and_run(sc
);
564 if (fcp
!= NULL
&& (fcp
->acr
> 0 || fcp
->acw
> 0 || fcp
->ace
> 0))
565 g_access(cp
, -fcp
->acr
, -fcp
->acw
, -fcp
->ace
);
567 g_destroy_consumer(cp
);
571 static struct g_geom
*
572 g_ventoy_create(struct g_class
*mp
, const struct g_ventoy_metadata
*md
,
575 struct g_ventoy_softc
*sc
;
579 G_VENTOY_DEBUG(1, "Creating device %s (id=%u).", md
->md_name
,
582 /* One disks is minimum. */
586 /* Check for duplicate unit */
587 LIST_FOREACH(gp
, &mp
->geom
, geom
) {
589 if (sc
!= NULL
&& strcmp(sc
->sc_name
, md
->md_name
) == 0) {
590 G_VENTOY_DEBUG(0, "Device %s already configured.",
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
;
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
;
610 mtx_init(&sc
->sc_lock
, "gventoy lock", NULL
, MTX_DEF
);
614 sc
->sc_provider
= NULL
;
616 G_VENTOY_DEBUG(0, "Device %s created (id=%u).", sc
->sc_name
, sc
->sc_id
);
622 g_ventoy_destroy(struct g_ventoy_softc
*sc
, boolean_t force
)
624 struct g_provider
*pp
;
625 struct g_consumer
*cp
, *cp1
;
633 pp
= sc
->sc_provider
;
634 if (pp
!= NULL
&& (pp
->acr
!= 0 || pp
->acw
!= 0 || pp
->ace
!= 0)) {
636 G_VENTOY_DEBUG(0, "Device %s is still open, so it "
637 "can't be definitely removed.", pp
->name
);
640 "Device %s is still open (r%dw%de%d).", pp
->name
,
641 pp
->acr
, pp
->acw
, pp
->ace
);
647 LIST_FOREACH_SAFE(cp
, &gp
->consumer
, consumer
, cp1
) {
648 g_ventoy_remove_disk(cp
->private);
650 return (0); /* Recursion happened. */
652 if (!LIST_EMPTY(&gp
->consumer
))
653 return (EINPROGRESS
);
656 KASSERT(sc
->sc_provider
== NULL
, ("Provider still exists? (device=%s)",
658 free(sc
->sc_disks
, M_VENTOY
);
659 mtx_destroy(&sc
->sc_lock
);
662 G_VENTOY_DEBUG(0, "Device %s destroyed.", gp
->name
);
663 g_wither_geom(gp
, ENXIO
);
668 g_ventoy_destroy_geom(struct gctl_req
*req __unused
,
669 struct g_class
*mp __unused
, struct g_geom
*gp
)
671 struct g_ventoy_softc
*sc
;
674 return (g_ventoy_destroy(sc
, 0));
677 static bool g_vtoy_check_disk(struct g_class
*mp
, struct g_provider
*pp
)
683 struct g_consumer
*cp
;
686 if (g_ventoy_disk_size
== 0)
688 if (resource_string_value("ventoy", 0, "disksize", &value
) == 0)
690 G_DEBUG("ventoy.disksize: %s\n", value
);
691 g_ventoy_disk_size
= strtouq(value
, NULL
, 0);
694 if (resource_string_value("ventoy", 0, "diskuuid", &g_ventoy_disk_uuid
) == 0)
696 G_DEBUG("ventoy.diskuuid: <%s>\n", g_ventoy_disk_uuid
);
700 if (g_ventoy_disk_size
!= pp
->mediasize
)
705 if (strncmp(pp
->name
, "cd", 2) == 0 || strchr(pp
->name
, '/'))
710 /* read UUID from disk */
711 gp
= g_new_geomf(mp
, "ventoy:taste");
713 gp
->access
= g_ventoy_access
;
714 gp
->orphan
= g_ventoy_orphan
;
715 cp
= g_new_consumer(gp
);
718 g_access(cp
, 1, 0, 0);
720 buf
= g_read_data(cp
, 0, pp
->sectorsize
, NULL
);
722 g_access(cp
, -1, 0, 0);
725 g_destroy_consumer(cp
);
734 for (i
= 0; i
< 16; i
++)
736 sprintf(uuid
+ i
* 2, "%02x", buf
[0x180 + i
]);
740 if (strncmp(g_ventoy_disk_uuid
, uuid
, 32) == 0)
748 static struct g_geom
*
749 g_ventoy_taste(struct g_class
*mp
, struct g_provider
*pp
, int flags __unused
)
757 struct g_ventoy_metadata md
;
758 struct g_ventoy_softc
*sc
;
765 G_DEBUG("%s(%s, %s)\n", __func__
, mp
->name
, pp
->name
);
768 /* Skip providers that are already open for writing. */
772 if (!g_vtoy_check_disk(mp
, pp
))
777 g_ventoy_tasted
= true;
779 G_DEBUG("######### ventoy disk <%s> #############\n", pp
->name
);
781 resource_int_value("ventoy", 0, "segnum", &disknum
);
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();
788 md
.md_all
= (uint16_t)disknum
;
789 bzero(md
.md_provider
, sizeof(md
.md_provider
));
790 /* This field is not important here. */
793 gp
= g_ventoy_create(mp
, &md
, G_VENTOY_TYPE_MANUAL
);
795 G_VENTOY_DEBUG(0, "Cannot create device %s.",
801 for (i
= 0; i
< disknum
; i
++)
803 if (resource_string_value("ventoy", i
, "seg", &value
) == 0)
805 g_disk_map_start
= strtouq(value
, &endpos
, 0);
806 g_disk_map_end
= strtouq(endpos
+ 1, NULL
, 0);
810 printf("Failed to parse ventoy seg %d\n", i
);
814 G_DEBUG("ventoy segment%d: %s\n", i
, value
);
816 G_VENTOY_DEBUG(1, "Adding disk %s to %s.", pp
->name
, gp
->name
);
817 error
= g_ventoy_add_disk(sc
, pp
, i
);
820 "Cannot add disk %s to %s (error=%d).", pp
->name
,
822 g_ventoy_destroy(sc
, 1);
826 g_disk_map_start
= 0;
834 g_ventoy_ctl_create(struct gctl_req
*req
, struct g_class
*mp
)
837 struct g_ventoy_metadata md
;
838 struct g_provider
*pp
;
839 struct g_ventoy_softc
*sc
;
847 nargs
= gctl_get_paraml(req
, "nargs", sizeof(*nargs
));
849 gctl_error(req
, "No '%s' argument.", "nargs");
853 gctl_error(req
, "Too few arguments.");
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");
861 gctl_error(req
, "No 'arg%u' argument.", 0);
864 strlcpy(md
.md_name
, name
, sizeof(md
.md_name
));
865 md
.md_id
= arc4random();
867 md
.md_all
= *nargs
- 1;
868 bzero(md
.md_provider
, sizeof(md
.md_provider
));
869 /* This field is not important here. */
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
);
877 gctl_error(req
, "No 'arg%u' argument.", no
);
880 if (strncmp(name
, "/dev/", strlen("/dev/")) == 0)
881 name
+= strlen("/dev/");
882 pp
= g_provider_by_name(name
);
884 G_VENTOY_DEBUG(1, "Disk %s is invalid.", name
);
885 gctl_error(req
, "Disk %s is invalid.", name
);
890 gp
= g_ventoy_create(mp
, &md
, G_VENTOY_TYPE_MANUAL
);
892 gctl_error(req
, "Can't configure %s.", md
.md_name
);
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
);
903 gctl_error(req
, "No 'arg%d' argument.", no
);
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
);
919 if (md
.md_all
!= attached
) {
920 g_ventoy_destroy(gp
->softc
, 1);
921 gctl_error(req
, "%s", sbuf_data(sb
));
926 static struct g_ventoy_softc
*
927 g_ventoy_find_device(struct g_class
*mp
, const char *name
)
929 struct g_ventoy_softc
*sc
;
932 LIST_FOREACH(gp
, &mp
->geom
, geom
) {
936 if (strcmp(sc
->sc_name
, name
) == 0)
943 g_ventoy_ctl_destroy(struct gctl_req
*req
, struct g_class
*mp
)
945 struct g_ventoy_softc
*sc
;
946 int *force
, *nargs
, error
;
953 nargs
= gctl_get_paraml(req
, "nargs", sizeof(*nargs
));
955 gctl_error(req
, "No '%s' argument.", "nargs");
959 gctl_error(req
, "Missing device(s).");
962 force
= gctl_get_paraml(req
, "force", sizeof(*force
));
964 gctl_error(req
, "No '%s' argument.", "force");
968 for (i
= 0; i
< (u_int
)*nargs
; i
++) {
969 snprintf(param
, sizeof(param
), "arg%u", i
);
970 name
= gctl_get_asciiparam(req
, param
);
972 gctl_error(req
, "No 'arg%u' argument.", i
);
975 sc
= g_ventoy_find_device(mp
, name
);
977 gctl_error(req
, "No such device: %s.", name
);
980 error
= g_ventoy_destroy(sc
, *force
);
982 gctl_error(req
, "Cannot destroy device %s (error=%d).",
990 g_ventoy_config(struct gctl_req
*req
, struct g_class
*mp
, const char *verb
)
998 version
= gctl_get_paraml(req
, "version", sizeof(*version
));
999 if (version
== NULL
) {
1000 gctl_error(req
, "No '%s' argument.", "version");
1003 if (*version
!= G_VENTOY_VERSION
) {
1004 gctl_error(req
, "Userland and kernel parts are out of sync.");
1008 if (strcmp(verb
, "create") == 0) {
1009 g_ventoy_ctl_create(req
, mp
);
1011 } else if (strcmp(verb
, "destroy") == 0 ||
1012 strcmp(verb
, "stop") == 0) {
1013 g_ventoy_ctl_destroy(req
, mp
);
1016 gctl_error(req
, "Unknown verb.");
1020 g_ventoy_dumpconf(struct sbuf
*sb
, const char *indent
, struct g_geom
*gp
,
1021 struct g_consumer
*cp
, struct g_provider
*pp
)
1023 struct g_ventoy_softc
*sc
;
1025 g_topology_assert();
1031 } else if (cp
!= NULL
) {
1032 struct g_ventoy_disk
*disk
;
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
);
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");
1048 case G_VENTOY_TYPE_MANUAL
:
1049 sbuf_cat(sb
, "MANUAL");
1052 sbuf_cat(sb
, "UNKNOWN");
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)
1062 sbuf_cat(sb
, "DOWN");
1063 sbuf_cat(sb
, "</State>\n");
1067 DECLARE_GEOM_CLASS(g_ventoy_class
, g_ventoy
);
1068 //MODULE_VERSION(geom_ventoy, 0);