2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2003 Poul-Henning Kamp
5 * Copyright (c) 1995 Jason R. Thorpe.
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.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the NetBSD Project
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/linker.h>
41 #include <sys/module.h>
53 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
54 #define CCDF_MIRROR 0x04 /* use mirroring */
55 #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
56 #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
58 #include "pathnames.h"
60 static int lineno = 0;
61 static int verbose = 0;
62 static const char *ccdconf = _PATH_CCDCONF;
64 static struct flagval {
68 { "CCDF_UNIFORM", CCDF_UNIFORM },
69 { "uniform", CCDF_UNIFORM },
70 { "CCDF_MIRROR", CCDF_MIRROR },
71 { "mirror", CCDF_MIRROR },
72 { "CCDF_NO_OFFSET", CCDF_NO_OFFSET },
73 { "no_offset", CCDF_NO_OFFSET },
74 { "CCDF_LINUX", CCDF_LINUX },
75 { "linux", CCDF_LINUX },
80 #define CCD_CONFIG 0 /* configure a device */
81 #define CCD_CONFIGALL 1 /* configure all devices */
82 #define CCD_UNCONFIG 2 /* unconfigure a device */
83 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
84 #define CCD_DUMP 4 /* dump a ccd's configuration */
86 static int do_single(int, char **, int);
87 static int do_all(int);
88 static int dump_ccd(int, char **);
89 static int flags_to_val(char *);
90 static int resolve_ccdname(char *);
91 static void usage(void);
94 main(int argc, char *argv[])
96 int ch, options = 0, action = CCD_CONFIG;
98 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
106 action = CCD_CONFIGALL;
119 action = CCD_UNCONFIG;
124 action = CCD_UNCONFIGALL;
142 if (modfind("g_ccd") < 0) {
143 /* Not present in kernel, try loading it */
144 if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
145 warn("geom_ccd module not available!");
151 exit(do_single(argc, argv, action));
155 case CCD_UNCONFIGALL:
156 exit(do_all(action));
160 exit(dump_ccd(argc, argv));
168 do_single(int argc, char **argv, int action)
171 int ccd, noflags = 0, i, ileave, flags = 0;
172 struct gctl_req *grq;
178 * If unconfiguring, all arguments are treated as ccds.
180 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
183 cp = *argv++; --argc;
184 if ((ccd = resolve_ccdname(cp)) < 0) {
185 warnx("invalid ccd name: %s", cp);
188 grq = gctl_get_handle();
189 gctl_ro_param(grq, "verb", -1, "destroy geom");
190 gctl_ro_param(grq, "class", -1, "CCD");
191 sprintf(buf1, "ccd%d", ccd);
192 gctl_ro_param(grq, "geom", -1, buf1);
193 errstr = gctl_issue(grq);
194 if (errstr == NULL) {
196 printf("%s unconfigured\n", cp);
201 "%s\nor possibly kernel and ccdconfig out of sync",
208 /* Make sure there are enough arguments. */
211 /* Assume that no flags are specified. */
214 if (action == CCD_CONFIGALL) {
215 warnx("%s: bad line: %d", ccdconf, lineno);
222 /* First argument is the ccd to configure. */
223 cp = *argv++; --argc;
224 if ((ccd = resolve_ccdname(cp)) < 0) {
225 warnx("invalid ccd name: %s", cp);
229 /* Next argument is the interleave factor. */
230 cp = *argv++; --argc;
231 errno = 0; /* to check for ERANGE */
232 ileave = (int)strtol(cp, &cp2, 10);
233 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
234 warnx("invalid interleave factor: %s", cp);
239 /* Next argument is the ccd configuration flags. */
240 cp = *argv++; --argc;
241 if ((flags = flags_to_val(cp)) < 0) {
242 warnx("invalid flags argument: %s", cp);
246 grq = gctl_get_handle();
247 gctl_ro_param(grq, "verb", -1, "create geom");
248 gctl_ro_param(grq, "class", -1, "CCD");
249 gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
250 gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
251 if (flags & CCDF_UNIFORM)
252 gctl_ro_param(grq, "uniform", -1, "");
253 if (flags & CCDF_MIRROR)
254 gctl_ro_param(grq, "mirror", -1, "");
255 if (flags & CCDF_NO_OFFSET)
256 gctl_ro_param(grq, "no_offset", -1, "");
257 if (flags & CCDF_LINUX)
258 gctl_ro_param(grq, "linux", -1, "");
259 gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
260 for (i = 0; i < argc; i++) {
261 sprintf(buf1, "provider%d", i);
263 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
264 cp += strlen(_PATH_DEV);
265 gctl_ro_param(grq, buf1, -1, cp);
267 gctl_rw_param(grq, "output", sizeof(buf1), buf1);
268 errstr = gctl_issue(grq);
269 if (errstr == NULL) {
277 "%s\nor possibly kernel and ccdconfig out of sync",
286 char line[_POSIX2_LINE_MAX];
293 if (setegid(getgid()) != 0)
294 err(1, "setegid failed");
295 if ((f = fopen(ccdconf, "r")) == NULL) {
296 if (setegid(egid) != 0)
297 err(1, "setegid failed");
298 warn("fopen: %s", ccdconf);
301 if (setegid(egid) != 0)
302 err(1, "setegid failed");
304 while (fgets(line, sizeof(line), f) != NULL) {
308 if ((cp = strrchr(line, '\n')) != NULL)
311 /* Break up the line and pass it's contents to do_single(). */
314 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
317 if ((argv = realloc(argv,
318 sizeof(char *) * ++argc)) == NULL) {
319 warnx("no memory to configure ccds");
324 * If our action is to unconfigure all, then pass
325 * just the first token to do_single() and ignore
326 * the rest. Since this will be encountered on
327 * our first pass through the line, the Right
330 if (action == CCD_UNCONFIGALL) {
331 if (do_single(argc, argv, action))
337 if (do_single(argc, argv, action))
350 resolve_ccdname(char *name)
353 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
354 name += strlen(_PATH_DEV);
355 if (strncmp(name, "ccd", 3))
360 return (strtoul(name, NULL, 10));
367 struct gctl_req *grq;
372 grq = gctl_get_handle();
375 gctl_ro_param(grq, "verb", -1, "list");
376 gctl_ro_param(grq, "class", -1, "CCD");
377 gctl_ro_param(grq, "unit", sizeof(unit), &unit);
378 gctl_rw_param(grq, "output", ncp, cp);
379 errstr = gctl_issue(grq);
381 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
384 errx(1, "ccd%d not configured", unit);
386 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
395 dump_ccd(int argc, char **argv)
403 for (i = 0; error == 0 && i < argc; i++)
404 error = dumpout(resolve_ccdname(argv[i]));
410 flags_to_val(char *flags)
415 errno = 0; /* to check for ERANGE */
416 val = (int)strtol(flags, &cp, 0);
417 if ((errno != ERANGE) && (*cp == '\0')) {
418 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
423 /* Check for values represented by strings. */
424 if ((cp = strdup(flags)) == NULL)
425 err(1, "no memory to parse flags");
427 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
428 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
429 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
431 if (flagvaltab[i].fv_flag == NULL) {
435 tmp |= flagvaltab[i].fv_val;
438 /* If we get here, the string was ok. */
446 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
447 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
448 " ccdconfig -C [-v] [-f config_file]",
449 " ccdconfig -u [-v] ccd ...",
450 " ccdconfig -U [-v] [-f config_file]",
451 " ccdconfig -g [ccd ...]");