1 /* $NetBSD: ccdconfig.c,v 1.2.2.1 1995/11/11 02:43:35 thorpej Exp $ */
4 * Copyright (c) 1995 Jason R. Thorpe.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char rcsid[] =
40 #include <sys/param.h>
41 #include <sys/linker.h>
42 #include <sys/disklabel.h>
44 #include <sys/module.h>
56 #include <sys/devicestat.h>
57 #include <sys/ccdvar.h>
59 #include "pathnames.h"
61 static int lineno = 0;
62 static int verbose = 0;
63 static char *ccdconf = _PATH_CCDCONF;
69 { "CCDF_SWAP", CCDF_SWAP },
70 { "CCDF_UNIFORM", CCDF_UNIFORM },
71 { "CCDF_MIRROR", CCDF_MIRROR },
72 { "CCDF_PARITY", CCDF_PARITY },
76 #define CCD_CONFIG 0 /* configure a device */
77 #define CCD_CONFIGALL 1 /* configure all devices */
78 #define CCD_UNCONFIG 2 /* unconfigure a device */
79 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
80 #define CCD_DUMP 4 /* dump a ccd's configuration */
82 static int checkdev(char *);
83 static int do_io(char *, u_long, struct ccd_ioctl *);
84 static int do_single(int, char **, int);
85 static int do_all(int);
86 static int dump_ccd(int, char **);
87 static int getmaxpartitions(void);
88 static int getrawpartition(void);
89 static int flags_to_val(char *);
90 static void print_ccd_info(struct ccd_s *);
91 static char *resolve_ccdname(char *);
92 static void usage(void);
95 main(int argc, char *argv[])
97 int ch, options = 0, action = CCD_CONFIG;
99 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
107 action = CCD_CONFIGALL;
120 action = CCD_UNCONFIG;
125 action = CCD_UNCONFIGALL;
143 if (modfind("ccd") < 0) {
144 /* Not present in kernel, try loading it */
145 if (kldload("ccd") < 0 || modfind("ccd") < 0)
146 warn("ccd module not available!");
152 exit(do_single(argc, argv, action));
156 case CCD_UNCONFIGALL:
157 exit(do_all(action));
161 exit(dump_ccd(argc, argv));
169 do_single(int argc, char **argv, int action)
171 struct ccd_ioctl ccio;
172 char *ccd, *cp, *cp2, **disks;
173 int noflags = 0, i, ileave, flags = 0, j;
175 bzero(&ccio, sizeof(ccio));
178 * If unconfiguring, all arguments are treated as ccds.
180 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
181 for (i = 0; argc != 0; ) {
182 cp = *argv++; --argc;
183 if ((ccd = resolve_ccdname(cp)) == NULL) {
184 warnx("invalid ccd name: %s", cp);
188 if (do_io(ccd, CCDIOCCLR, &ccio))
192 printf("%s unconfigured\n", cp);
197 /* Make sure there are enough arguments. */
200 /* Assume that no flags are specified. */
203 if (action == CCD_CONFIGALL) {
204 warnx("%s: bad line: %d", ccdconf, lineno);
211 /* First argument is the ccd to configure. */
212 cp = *argv++; --argc;
213 if ((ccd = resolve_ccdname(cp)) == NULL) {
214 warnx("invalid ccd name: %s", cp);
218 /* Next argument is the interleave factor. */
219 cp = *argv++; --argc;
220 errno = 0; /* to check for ERANGE */
221 ileave = (int)strtol(cp, &cp2, 10);
222 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
223 warnx("invalid interleave factor: %s", cp);
228 /* Next argument is the ccd configuration flags. */
229 cp = *argv++; --argc;
230 if ((flags = flags_to_val(cp)) < 0) {
231 warnx("invalid flags argument: %s", cp);
236 /* Next is the list of disks to make the ccd from. */
237 disks = malloc(argc * sizeof(char *));
239 warnx("no memory to configure ccd");
242 for (i = 0; argc != 0; ) {
243 cp = *argv++; --argc;
244 if ((j = checkdev(cp)) == 0)
247 warnx("%s: %s", cp, strerror(j));
252 /* Fill in the ccio. */
253 ccio.ccio_disks = disks;
254 ccio.ccio_ndisks = i;
255 ccio.ccio_ileave = ileave;
256 ccio.ccio_flags = flags;
258 if (do_io(ccd, CCDIOCSET, &ccio)) {
264 printf("ccd%d: %d components ", ccio.ccio_unit,
266 for (i = 0; i < ccio.ccio_ndisks; ++i) {
267 if ((cp2 = strrchr(disks[i], '/')) != NULL)
272 i == 0 ? '(' : ' ', cp2,
273 i == ccio.ccio_ndisks - 1 ? ')' : ',');
275 printf(", %lu blocks ", (u_long)ccio.ccio_size);
276 if (ccio.ccio_ileave != 0)
277 printf("interleaved at %d blocks\n", ccio.ccio_ileave);
279 printf("concatenated\n");
290 char line[_POSIX2_LINE_MAX];
298 if ((f = fopen(ccdconf, "r")) == NULL) {
300 warn("fopen: %s", ccdconf);
305 while (fgets(line, sizeof(line), f) != NULL) {
309 if ((cp = strrchr(line, '\n')) != NULL)
312 /* Break up the line and pass it's contents to do_single(). */
315 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
318 if ((argv = realloc(argv,
319 sizeof(char *) * ++argc)) == NULL) {
320 warnx("no memory to configure ccds");
325 * If our action is to unconfigure all, then pass
326 * just the first token to do_single() and ignore
327 * the rest. Since this will be encountered on
328 * our first pass through the line, the Right
331 if (action == CCD_UNCONFIGALL) {
332 if (do_single(argc, argv, action))
338 if (do_single(argc, argv, action))
355 if (stat(path, &st) != 0)
358 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
365 pathtounit(char *path, int *unitp)
370 if (stat(path, &st) != 0)
373 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
376 if ((maxpartitions = getmaxpartitions()) < 0)
379 *unitp = minor(st.st_rdev) / maxpartitions;
385 resolve_ccdname(char *name)
391 if (name[0] == '/' || name[0] == '.') {
392 /* Assume they gave the correct pathname. */
393 return (strdup(name));
400 if ((path = malloc(newlen)) == NULL)
405 if ((rawpart = getrawpartition()) < 0) {
409 (void)sprintf(path, "%s%s%c", _PATH_DEV, name, 'a' + rawpart);
411 (void)sprintf(path, "%s%s", _PATH_DEV, name);
417 do_io(char *path, u_long cmd, struct ccd_ioctl *cciop)
422 if ((fd = open(path, O_RDWR, 0640)) < 0) {
423 warn("open: %s", path);
427 if (ioctl(fd, cmd, cciop) < 0) {
448 warn("ioctl (%s): %s", cp, path);
456 dump_ccd(int argc, char **argv)
459 int i, error, numccd, numconfiged = 0;
463 * Read the ccd configuration data from the kernel and dump
466 if ((ccd = resolve_ccdname("ccd0")) == NULL) { /* XXX */
467 warnx("invalid ccd name: %s", cp);
471 if (do_io(ccd, CCDCONFINFO, (struct ccd_ioctl *) &conf))
473 if (conf.size == 0) {
474 printf("no concatenated disks configured\n");
477 /* Allocate space for the configuration data. */
478 conf.buffer = alloca(conf.size);
479 if (conf.buffer == NULL) {
480 warnx("no memory for configuration data");
483 if (do_io(ccd, CCDCONFINFO, (struct ccd_ioctl *) &conf))
486 numconfiged = conf.size / sizeof(struct ccd_s);
489 for (i = 0; i < numconfiged; i++)
490 print_ccd_info(&(conf.buffer[i]));
493 cp = *argv++; --argc;
494 if ((ccd = resolve_ccdname(cp)) == NULL) {
495 warnx("invalid ccd name: %s", cp);
498 if ((error = pathtounit(ccd, &numccd)) != 0) {
499 warnx("%s: %s", ccd, strerror(error));
503 for (i = 0; i < numconfiged; i++) {
504 if (conf.buffer[i].sc_unit == numccd) {
505 print_ccd_info(&(conf.buffer[i]));
511 warnx("ccd%d not configured", numccd);
521 print_ccd_info(struct ccd_s *cs)
524 static int header_printed = 0;
527 /* Print out header if necessary*/
528 if (header_printed == 0 && verbose) {
529 printf("# ccd\t\tileave\tflags\tcompnent devices\n");
533 /* Dump out softc information. */
534 printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
535 cs->sc_cflags & CCDF_USERMASK);
538 /* Read in the component info. */
539 asprintf(&cp, "%s%d", cs->device_stats.device_name,
540 cs->device_stats.unit_number);
543 warn("ccd%d: can't allocate memory",
548 if ((ccd = resolve_ccdname(cp)) == NULL) {
550 warnx("can't read component info: invalid ccd name: %s", cp);
554 if (do_io(ccd, CCDCPPINFO, (struct ccd_ioctl *) &cpps)) {
556 warnx("can't read component info");
559 cpps.buffer = alloca(cpps.size);
560 if (cpps.buffer == NULL) {
562 warn("ccd%d: can't allocate memory for component info",
566 if (do_io(ccd, CCDCPPINFO, (struct ccd_ioctl *) &cpps)) {
568 warnx("can't read component info");
572 /* Display component info. */
573 for (cp = cpps.buffer; cp - cpps.buffer < cpps.size; cp += strlen(cp) + 1) {
574 printf((cp + strlen(cp) + 1) < (cpps.buffer + cpps.size) ?
582 getmaxpartitions(void)
584 return (MAXPARTITIONS);
588 getrawpartition(void)
594 flags_to_val(char *flags)
597 int i, tmp, val = ~CCDF_USERMASK;
601 * The most common case is that of NIL flags, so check for
604 if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
605 strcmp("0", flags) == 0)
608 flagslen = strlen(flags);
610 /* Check for values represented by strings. */
611 if ((cp = strdup(flags)) == NULL)
612 err(1, "no memory to parse flags");
614 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
615 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
616 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
618 if (flagvaltab[i].fv_flag == NULL) {
622 tmp |= flagvaltab[i].fv_val;
625 /* If we get here, the string was ok. */
632 /* Check for values represented in hex. */
633 if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
634 errno = 0; /* to check for ERANGE */
635 val = (int)strtol(&flags[2], &cp, 16);
636 if ((errno == ERANGE) || (*cp != '\0'))
641 /* Check for values represented in decimal. */
642 errno = 0; /* to check for ERANGE */
643 val = (int)strtol(flags, &cp, 10);
644 if ((errno == ERANGE) || (*cp != '\0'))
648 return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
654 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
655 "usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
656 " ccdconfig -C [-v] [-f config_file]",
657 " ccdconfig -u [-v] ccd [...]",
658 " ccdconfig -U [-v] [-f config_file]",
659 " ccdconfig -g [ccd [...]]");
663 /* Local Variables: */
664 /* c-argdecl-indent: 8 */
665 /* c-indent-level: 8 */