2 * Copyright (c) 2003 Poul-Henning Kamp
3 * Copyright (c) 1995 Jason R. Thorpe.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed for the NetBSD Project
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
51 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
52 #define CCDF_MIRROR 0x04 /* use mirroring */
53 #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
54 #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
56 #include "pathnames.h"
58 static int lineno = 0;
59 static int verbose = 0;
60 static const char *ccdconf = _PATH_CCDCONF;
62 static struct flagval {
66 { "CCDF_UNIFORM", CCDF_UNIFORM },
67 { "uniform", CCDF_UNIFORM },
68 { "CCDF_MIRROR", CCDF_MIRROR },
69 { "mirror", CCDF_MIRROR },
70 { "CCDF_NO_OFFSET", CCDF_NO_OFFSET },
71 { "no_offset", CCDF_NO_OFFSET },
72 { "CCDF_LINUX", CCDF_LINUX },
73 { "linux", CCDF_LINUX },
78 #define CCD_CONFIG 0 /* configure a device */
79 #define CCD_CONFIGALL 1 /* configure all devices */
80 #define CCD_UNCONFIG 2 /* unconfigure a device */
81 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
82 #define CCD_DUMP 4 /* dump a ccd's configuration */
84 static int do_single(int, char **, int);
85 static int do_all(int);
86 static int dump_ccd(int, char **);
87 static int flags_to_val(char *);
88 static int resolve_ccdname(char *);
89 static void usage(void);
92 main(int argc, char *argv[])
94 int ch, options = 0, action = CCD_CONFIG;
96 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
104 action = CCD_CONFIGALL;
117 action = CCD_UNCONFIG;
122 action = CCD_UNCONFIGALL;
140 if (modfind("g_ccd") < 0) {
141 /* Not present in kernel, try loading it */
142 if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
143 warn("geom_ccd module not available!");
149 exit(do_single(argc, argv, action));
153 case CCD_UNCONFIGALL:
154 exit(do_all(action));
158 exit(dump_ccd(argc, argv));
166 do_single(int argc, char **argv, int action)
169 int ccd, noflags = 0, i, ileave, flags = 0;
170 struct gctl_req *grq;
176 * If unconfiguring, all arguments are treated as ccds.
178 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
181 cp = *argv++; --argc;
182 if ((ccd = resolve_ccdname(cp)) < 0) {
183 warnx("invalid ccd name: %s", cp);
186 grq = gctl_get_handle();
187 gctl_ro_param(grq, "verb", -1, "destroy geom");
188 gctl_ro_param(grq, "class", -1, "CCD");
189 sprintf(buf1, "ccd%d", ccd);
190 gctl_ro_param(grq, "geom", -1, buf1);
191 errstr = gctl_issue(grq);
192 if (errstr == NULL) {
194 printf("%s unconfigured\n", cp);
199 "%s\nor possibly kernel and ccdconfig out of sync",
206 /* Make sure there are enough arguments. */
209 /* Assume that no flags are specified. */
212 if (action == CCD_CONFIGALL) {
213 warnx("%s: bad line: %d", ccdconf, lineno);
220 /* First argument is the ccd to configure. */
221 cp = *argv++; --argc;
222 if ((ccd = resolve_ccdname(cp)) < 0) {
223 warnx("invalid ccd name: %s", cp);
227 /* Next argument is the interleave factor. */
228 cp = *argv++; --argc;
229 errno = 0; /* to check for ERANGE */
230 ileave = (int)strtol(cp, &cp2, 10);
231 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
232 warnx("invalid interleave factor: %s", cp);
237 /* Next argument is the ccd configuration flags. */
238 cp = *argv++; --argc;
239 if ((flags = flags_to_val(cp)) < 0) {
240 warnx("invalid flags argument: %s", cp);
244 grq = gctl_get_handle();
245 gctl_ro_param(grq, "verb", -1, "create geom");
246 gctl_ro_param(grq, "class", -1, "CCD");
247 gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
248 gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
249 if (flags & CCDF_UNIFORM)
250 gctl_ro_param(grq, "uniform", -1, "");
251 if (flags & CCDF_MIRROR)
252 gctl_ro_param(grq, "mirror", -1, "");
253 if (flags & CCDF_NO_OFFSET)
254 gctl_ro_param(grq, "no_offset", -1, "");
255 if (flags & CCDF_LINUX)
256 gctl_ro_param(grq, "linux", -1, "");
257 gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
258 for (i = 0; i < argc; i++) {
259 sprintf(buf1, "provider%d", i);
261 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
262 cp += strlen(_PATH_DEV);
263 gctl_ro_param(grq, buf1, -1, cp);
265 gctl_rw_param(grq, "output", sizeof(buf1), buf1);
266 errstr = gctl_issue(grq);
267 if (errstr == NULL) {
275 "%s\nor possibly kernel and ccdconfig out of sync",
284 char line[_POSIX2_LINE_MAX];
291 if (setegid(getgid()) != 0)
292 err(1, "setegid failed");
293 if ((f = fopen(ccdconf, "r")) == NULL) {
294 if (setegid(egid) != 0)
295 err(1, "setegid failed");
296 warn("fopen: %s", ccdconf);
299 if (setegid(egid) != 0)
300 err(1, "setegid failed");
302 while (fgets(line, sizeof(line), f) != NULL) {
306 if ((cp = strrchr(line, '\n')) != NULL)
309 /* Break up the line and pass it's contents to do_single(). */
312 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
315 if ((argv = realloc(argv,
316 sizeof(char *) * ++argc)) == NULL) {
317 warnx("no memory to configure ccds");
322 * If our action is to unconfigure all, then pass
323 * just the first token to do_single() and ignore
324 * the rest. Since this will be encountered on
325 * our first pass through the line, the Right
328 if (action == CCD_UNCONFIGALL) {
329 if (do_single(argc, argv, action))
335 if (do_single(argc, argv, action))
348 resolve_ccdname(char *name)
351 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
352 name += strlen(_PATH_DEV);
353 if (strncmp(name, "ccd", 3))
358 return (strtoul(name, NULL, 10));
365 struct gctl_req *grq;
370 grq = gctl_get_handle();
373 gctl_ro_param(grq, "verb", -1, "list");
374 gctl_ro_param(grq, "class", -1, "CCD");
375 gctl_ro_param(grq, "unit", sizeof(unit), &unit);
376 gctl_rw_param(grq, "output", ncp, cp);
377 errstr = gctl_issue(grq);
379 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
382 errx(1, "ccd%d not configured", unit);
384 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
393 dump_ccd(int argc, char **argv)
401 for (i = 0; error == 0 && i < argc; i++)
402 error = dumpout(resolve_ccdname(argv[i]));
408 flags_to_val(char *flags)
413 errno = 0; /* to check for ERANGE */
414 val = (int)strtol(flags, &cp, 0);
415 if ((errno != ERANGE) && (*cp == '\0')) {
416 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
421 /* Check for values represented by strings. */
422 if ((cp = strdup(flags)) == NULL)
423 err(1, "no memory to parse flags");
425 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
426 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
427 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
429 if (flagvaltab[i].fv_flag == NULL) {
433 tmp |= flagvaltab[i].fv_val;
436 /* If we get here, the string was ok. */
444 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
445 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
446 " ccdconfig -C [-v] [-f config_file]",
447 " ccdconfig -u [-v] ccd ...",
448 " ccdconfig -U [-v] [-f config_file]",
449 " ccdconfig -g [ccd ...]");