1 /* $NetBSD: ccdconfig.c,v 1.6 1996/05/16 07:11:18 thorpej Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2003 Poul-Henning Kamp
7 * Copyright (c) 1996 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/linker.h>
47 #include <sys/module.h>
59 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
60 #define CCDF_MIRROR 0x04 /* use mirroring */
61 #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
62 #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
64 #include "pathnames.h"
66 static int lineno = 0;
67 static int verbose = 0;
68 static const char *ccdconf = _PATH_CCDCONF;
70 static struct flagval {
74 { "CCDF_UNIFORM", CCDF_UNIFORM },
75 { "uniform", CCDF_UNIFORM },
76 { "CCDF_MIRROR", CCDF_MIRROR },
77 { "mirror", CCDF_MIRROR },
78 { "CCDF_NO_OFFSET", CCDF_NO_OFFSET },
79 { "no_offset", CCDF_NO_OFFSET },
80 { "CCDF_LINUX", CCDF_LINUX },
81 { "linux", CCDF_LINUX },
86 #define CCD_CONFIG 0 /* configure a device */
87 #define CCD_CONFIGALL 1 /* configure all devices */
88 #define CCD_UNCONFIG 2 /* unconfigure a device */
89 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
90 #define CCD_DUMP 4 /* dump a ccd's configuration */
92 static int do_single(int, char **, int);
93 static int do_all(int);
94 static int dump_ccd(int, char **);
95 static int flags_to_val(char *);
96 static int resolve_ccdname(char *);
97 static void usage(void);
100 main(int argc, char *argv[])
102 int ch, options = 0, action = CCD_CONFIG;
104 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
112 action = CCD_CONFIGALL;
125 action = CCD_UNCONFIG;
130 action = CCD_UNCONFIGALL;
148 if (modfind("g_ccd") < 0) {
149 /* Not present in kernel, try loading it */
150 if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
151 warn("geom_ccd module not available!");
157 exit(do_single(argc, argv, action));
161 case CCD_UNCONFIGALL:
162 exit(do_all(action));
166 exit(dump_ccd(argc, argv));
174 do_single(int argc, char **argv, int action)
177 int ccd, noflags = 0, i, ileave, flags = 0;
178 struct gctl_req *grq;
184 * If unconfiguring, all arguments are treated as ccds.
186 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
189 cp = *argv++; --argc;
190 if ((ccd = resolve_ccdname(cp)) < 0) {
191 warnx("invalid ccd name: %s", cp);
194 grq = gctl_get_handle();
195 gctl_ro_param(grq, "verb", -1, "destroy geom");
196 gctl_ro_param(grq, "class", -1, "CCD");
197 sprintf(buf1, "ccd%d", ccd);
198 gctl_ro_param(grq, "geom", -1, buf1);
199 errstr = gctl_issue(grq);
200 if (errstr == NULL) {
202 printf("%s unconfigured\n", cp);
207 "%s\nor possibly kernel and ccdconfig out of sync",
214 /* Make sure there are enough arguments. */
217 /* Assume that no flags are specified. */
220 if (action == CCD_CONFIGALL) {
221 warnx("%s: bad line: %d", ccdconf, lineno);
228 /* First argument is the ccd to configure. */
229 cp = *argv++; --argc;
230 if ((ccd = resolve_ccdname(cp)) < 0) {
231 warnx("invalid ccd name: %s", cp);
235 /* Next argument is the interleave factor. */
236 cp = *argv++; --argc;
237 errno = 0; /* to check for ERANGE */
238 ileave = (int)strtol(cp, &cp2, 10);
239 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
240 warnx("invalid interleave factor: %s", cp);
245 /* Next argument is the ccd configuration flags. */
246 cp = *argv++; --argc;
247 if ((flags = flags_to_val(cp)) < 0) {
248 warnx("invalid flags argument: %s", cp);
252 grq = gctl_get_handle();
253 gctl_ro_param(grq, "verb", -1, "create geom");
254 gctl_ro_param(grq, "class", -1, "CCD");
255 gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
256 gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
257 if (flags & CCDF_UNIFORM)
258 gctl_ro_param(grq, "uniform", -1, "");
259 if (flags & CCDF_MIRROR)
260 gctl_ro_param(grq, "mirror", -1, "");
261 if (flags & CCDF_NO_OFFSET)
262 gctl_ro_param(grq, "no_offset", -1, "");
263 if (flags & CCDF_LINUX)
264 gctl_ro_param(grq, "linux", -1, "");
265 gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
266 for (i = 0; i < argc; i++) {
267 sprintf(buf1, "provider%d", i);
269 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
270 cp += strlen(_PATH_DEV);
271 gctl_ro_param(grq, buf1, -1, cp);
273 gctl_rw_param(grq, "output", sizeof(buf1), buf1);
274 errstr = gctl_issue(grq);
275 if (errstr == NULL) {
283 "%s\nor possibly kernel and ccdconfig out of sync",
292 char line[_POSIX2_LINE_MAX];
299 if (setegid(getgid()) != 0)
300 err(1, "setegid failed");
301 if ((f = fopen(ccdconf, "r")) == NULL) {
302 if (setegid(egid) != 0)
303 err(1, "setegid failed");
304 warn("fopen: %s", ccdconf);
307 if (setegid(egid) != 0)
308 err(1, "setegid failed");
310 while (fgets(line, sizeof(line), f) != NULL) {
314 if ((cp = strrchr(line, '\n')) != NULL)
317 /* Break up the line and pass it's contents to do_single(). */
320 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
323 if ((argv = realloc(argv,
324 sizeof(char *) * ++argc)) == NULL) {
325 warnx("no memory to configure ccds");
330 * If our action is to unconfigure all, then pass
331 * just the first token to do_single() and ignore
332 * the rest. Since this will be encountered on
333 * our first pass through the line, the Right
336 if (action == CCD_UNCONFIGALL) {
337 if (do_single(argc, argv, action))
343 if (do_single(argc, argv, action))
356 resolve_ccdname(char *name)
359 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
360 name += strlen(_PATH_DEV);
361 if (strncmp(name, "ccd", 3))
366 return (strtoul(name, NULL, 10));
373 struct gctl_req *grq;
378 grq = gctl_get_handle();
381 gctl_ro_param(grq, "verb", -1, "list");
382 gctl_ro_param(grq, "class", -1, "CCD");
383 gctl_ro_param(grq, "unit", sizeof(unit), &unit);
384 gctl_rw_param(grq, "output", ncp, cp);
385 errstr = gctl_issue(grq);
387 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
390 errx(1, "ccd%d not configured", unit);
392 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
401 dump_ccd(int argc, char **argv)
409 for (i = 0; error == 0 && i < argc; i++)
410 error = dumpout(resolve_ccdname(argv[i]));
416 flags_to_val(char *flags)
421 errno = 0; /* to check for ERANGE */
422 val = (int)strtol(flags, &cp, 0);
423 if ((errno != ERANGE) && (*cp == '\0')) {
424 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
429 /* Check for values represented by strings. */
430 if ((cp = strdup(flags)) == NULL)
431 err(1, "no memory to parse flags");
433 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
434 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
435 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
437 if (flagvaltab[i].fv_flag == NULL) {
441 tmp |= flagvaltab[i].fv_val;
444 /* If we get here, the string was ok. */
452 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
453 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
454 " ccdconfig -C [-v] [-f config_file]",
455 " ccdconfig -u [-v] ccd ...",
456 " ccdconfig -U [-v] [-f config_file]",
457 " ccdconfig -g [ccd ...]");