2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 Poul-Henning Kamp
5 * Copyright (c) 1996 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * NetBSD: ccdconfig.c,v 1.6 1996/05/16 07:11:18 thorpej Exp $
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/module.h>
52 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
53 #define CCDF_MIRROR 0x04 /* use mirroring */
54 #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
55 #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
57 #include "pathnames.h"
59 static int lineno = 0;
60 static int verbose = 0;
61 static const char *ccdconf = _PATH_CCDCONF;
63 static struct flagval {
67 { "CCDF_UNIFORM", CCDF_UNIFORM },
68 { "uniform", CCDF_UNIFORM },
69 { "CCDF_MIRROR", CCDF_MIRROR },
70 { "mirror", CCDF_MIRROR },
71 { "CCDF_NO_OFFSET", CCDF_NO_OFFSET },
72 { "no_offset", CCDF_NO_OFFSET },
73 { "CCDF_LINUX", CCDF_LINUX },
74 { "linux", CCDF_LINUX },
79 #define CCD_CONFIG 0 /* configure a device */
80 #define CCD_CONFIGALL 1 /* configure all devices */
81 #define CCD_UNCONFIG 2 /* unconfigure a device */
82 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
83 #define CCD_DUMP 4 /* dump a ccd's configuration */
85 static int do_single(int, char **, int);
86 static int do_all(int);
87 static int dump_ccd(int, char **);
88 static int flags_to_val(char *);
89 static int resolve_ccdname(char *);
90 static void usage(void);
93 main(int argc, char *argv[])
95 int ch, options = 0, action = CCD_CONFIG;
97 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
105 action = CCD_CONFIGALL;
118 action = CCD_UNCONFIG;
123 action = CCD_UNCONFIGALL;
141 if (modfind("g_ccd") < 0) {
142 /* Not present in kernel, try loading it */
143 if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
144 warn("geom_ccd module not available!");
150 exit(do_single(argc, argv, action));
154 case CCD_UNCONFIGALL:
155 exit(do_all(action));
159 exit(dump_ccd(argc, argv));
167 do_single(int argc, char **argv, int action)
170 int ccd, noflags = 0, i, ileave, flags = 0;
171 struct gctl_req *grq;
177 * If unconfiguring, all arguments are treated as ccds.
179 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
182 cp = *argv++; --argc;
183 if ((ccd = resolve_ccdname(cp)) < 0) {
184 warnx("invalid ccd name: %s", cp);
187 grq = gctl_get_handle();
188 gctl_ro_param(grq, "verb", -1, "destroy geom");
189 gctl_ro_param(grq, "class", -1, "CCD");
190 sprintf(buf1, "ccd%d", ccd);
191 gctl_ro_param(grq, "geom", -1, buf1);
192 errstr = gctl_issue(grq);
193 if (errstr == NULL) {
195 printf("%s unconfigured\n", cp);
200 "%s\nor possibly kernel and ccdconfig out of sync",
207 /* Make sure there are enough arguments. */
210 /* Assume that no flags are specified. */
213 if (action == CCD_CONFIGALL) {
214 warnx("%s: bad line: %d", ccdconf, lineno);
221 /* First argument is the ccd to configure. */
222 cp = *argv++; --argc;
223 if ((ccd = resolve_ccdname(cp)) < 0) {
224 warnx("invalid ccd name: %s", cp);
228 /* Next argument is the interleave factor. */
229 cp = *argv++; --argc;
230 errno = 0; /* to check for ERANGE */
231 ileave = (int)strtol(cp, &cp2, 10);
232 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
233 warnx("invalid interleave factor: %s", cp);
238 /* Next argument is the ccd configuration flags. */
239 cp = *argv++; --argc;
240 if ((flags = flags_to_val(cp)) < 0) {
241 warnx("invalid flags argument: %s", cp);
245 grq = gctl_get_handle();
246 gctl_ro_param(grq, "verb", -1, "create geom");
247 gctl_ro_param(grq, "class", -1, "CCD");
248 gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
249 gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
250 if (flags & CCDF_UNIFORM)
251 gctl_ro_param(grq, "uniform", -1, "");
252 if (flags & CCDF_MIRROR)
253 gctl_ro_param(grq, "mirror", -1, "");
254 if (flags & CCDF_NO_OFFSET)
255 gctl_ro_param(grq, "no_offset", -1, "");
256 if (flags & CCDF_LINUX)
257 gctl_ro_param(grq, "linux", -1, "");
258 gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
259 for (i = 0; i < argc; i++) {
260 sprintf(buf1, "provider%d", i);
262 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
263 cp += strlen(_PATH_DEV);
264 gctl_ro_param(grq, buf1, -1, cp);
266 gctl_rw_param(grq, "output", sizeof(buf1), buf1);
267 errstr = gctl_issue(grq);
268 if (errstr == NULL) {
276 "%s\nor possibly kernel and ccdconfig out of sync",
285 char line[_POSIX2_LINE_MAX];
292 if (setegid(getgid()) != 0)
293 err(1, "setegid failed");
294 if ((f = fopen(ccdconf, "r")) == NULL) {
295 if (setegid(egid) != 0)
296 err(1, "setegid failed");
297 warn("fopen: %s", ccdconf);
300 if (setegid(egid) != 0)
301 err(1, "setegid failed");
303 while (fgets(line, sizeof(line), f) != NULL) {
307 if ((cp = strrchr(line, '\n')) != NULL)
310 /* Break up the line and pass it's contents to do_single(). */
313 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
316 if ((argv = realloc(argv,
317 sizeof(char *) * ++argc)) == NULL) {
318 warnx("no memory to configure ccds");
323 * If our action is to unconfigure all, then pass
324 * just the first token to do_single() and ignore
325 * the rest. Since this will be encountered on
326 * our first pass through the line, the Right
329 if (action == CCD_UNCONFIGALL) {
330 if (do_single(argc, argv, action))
336 if (do_single(argc, argv, action))
349 resolve_ccdname(char *name)
352 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
353 name += strlen(_PATH_DEV);
354 if (strncmp(name, "ccd", 3))
359 return (strtoul(name, NULL, 10));
366 struct gctl_req *grq;
371 grq = gctl_get_handle();
374 gctl_ro_param(grq, "verb", -1, "list");
375 gctl_ro_param(grq, "class", -1, "CCD");
376 gctl_ro_param(grq, "unit", sizeof(unit), &unit);
377 gctl_rw_param(grq, "output", ncp, cp);
378 errstr = gctl_issue(grq);
380 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
383 errx(1, "ccd%d not configured", unit);
385 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
394 dump_ccd(int argc, char **argv)
402 for (i = 0; error == 0 && i < argc; i++)
403 error = dumpout(resolve_ccdname(argv[i]));
409 flags_to_val(char *flags)
414 errno = 0; /* to check for ERANGE */
415 val = (int)strtol(flags, &cp, 0);
416 if ((errno != ERANGE) && (*cp == '\0')) {
417 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
422 /* Check for values represented by strings. */
423 if ((cp = strdup(flags)) == NULL)
424 err(1, "no memory to parse flags");
426 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
427 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
428 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
430 if (flagvaltab[i].fv_flag == NULL) {
434 tmp |= flagvaltab[i].fv_val;
437 /* If we get here, the string was ok. */
445 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
446 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
447 " ccdconfig -C [-v] [-f config_file]",
448 " ccdconfig -u [-v] ccd ...",
449 " ccdconfig -U [-v] [-f config_file]",
450 " ccdconfig -g [ccd ...]");