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;
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) {
180 for (i = 0; argc != 0; ) {
181 cp = *argv++; --argc;
182 if ((ccd = resolve_ccdname(cp)) < 0) {
183 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];
293 if ((f = fopen(ccdconf, "r")) == NULL) {
295 warn("fopen: %s", ccdconf);
300 while (fgets(line, sizeof(line), f) != NULL) {
304 if ((cp = strrchr(line, '\n')) != NULL)
307 /* Break up the line and pass it's contents to do_single(). */
310 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
313 if ((argv = realloc(argv,
314 sizeof(char *) * ++argc)) == NULL) {
315 warnx("no memory to configure ccds");
320 * If our action is to unconfigure all, then pass
321 * just the first token to do_single() and ignore
322 * the rest. Since this will be encountered on
323 * our first pass through the line, the Right
326 if (action == CCD_UNCONFIGALL) {
327 if (do_single(argc, argv, action))
333 if (do_single(argc, argv, action))
346 resolve_ccdname(char *name)
349 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
350 name += strlen(_PATH_DEV);
351 if (strncmp(name, "ccd", 3))
356 return (strtoul(name, NULL, 10));
363 struct gctl_req *grq;
368 grq = gctl_get_handle();
371 gctl_ro_param(grq, "verb", -1, "list");
372 gctl_ro_param(grq, "class", -1, "CCD");
373 gctl_ro_param(grq, "unit", sizeof(unit), &unit);
374 gctl_rw_param(grq, "output", ncp, cp);
375 errstr = gctl_issue(grq);
377 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
380 errx(1, "ccd%d not configured", unit);
382 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
391 dump_ccd(int argc, char **argv)
399 for (i = 0; error == 0 && i < argc; i++)
400 error = dumpout(resolve_ccdname(argv[i]));
406 flags_to_val(char *flags)
411 errno = 0; /* to check for ERANGE */
412 val = (int)strtol(flags, &cp, 0);
413 if ((errno != ERANGE) && (*cp == '\0')) {
414 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
419 /* Check for values represented by strings. */
420 if ((cp = strdup(flags)) == NULL)
421 err(1, "no memory to parse flags");
423 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
424 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
425 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
427 if (flagvaltab[i].fv_flag == NULL) {
431 tmp |= flagvaltab[i].fv_val;
434 /* If we get here, the string was ok. */
442 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
443 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
444 " ccdconfig -C [-v] [-f config_file]",
445 " ccdconfig -u [-v] ccd ...",
446 " ccdconfig -U [-v] [-f config_file]",
447 " ccdconfig -g [ccd ...]");