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
34 * $FreeBSD: src/sbin/ccdconfig/ccdconfig.c,v 1.16.2.2 2000/12/11 01:03:25 obrien Exp $
35 * $DragonFly: src/sbin/ccdconfig/ccdconfig.c,v 1.6 2005/01/14 07:14:15 joerg Exp $
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/disklabel.h>
42 #include <sys/module.h>
55 #include <sys/devicestat.h>
56 #include <sys/ccdvar.h>
58 #include "pathnames.h"
60 static int lineno = 0;
61 static int verbose = 0;
62 static char *ccdconf = _PATH_CCDCONF;
64 static char *core = NULL;
65 static char *kernel = NULL;
71 { "CCDF_SWAP", CCDF_SWAP },
72 { "CCDF_UNIFORM", CCDF_UNIFORM },
73 { "CCDF_MIRROR", CCDF_MIRROR },
74 { "CCDF_PARITY", CCDF_PARITY },
78 static struct nlist nl[] = {
80 #define SYM_CCDSOFTC 0
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 checkdev(char *);
93 static int do_io(char *, u_long, struct ccd_ioctl *);
94 static int do_single(int, char **, int);
95 static int do_all(int);
96 static int dump_ccd(int, char **);
97 static int getmaxpartitions(void);
98 static int getrawpartition(void);
99 static int flags_to_val(char *);
100 static void print_ccd_info(struct ccd_softc *, kvm_t *);
101 static char *resolve_ccdname(char *);
102 static void usage(void);
105 main(int argc, char **argv)
107 int ch, options = 0, action = CCD_CONFIG;
109 while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
117 action = CCD_CONFIGALL;
138 action = CCD_UNCONFIG;
143 action = CCD_UNCONFIGALL;
162 * Discard setgid privileges if not the running kernel so that bad
163 * guys can't print interesting stuff from kernel memory.
165 if (core != NULL || kernel != NULL || action != CCD_DUMP) {
170 if (modfind("ccd") < 0) {
171 /* Not present in kernel, try loading it */
172 if (kldload("ccd") < 0 || modfind("ccd") < 0)
173 warn("ccd module not available!");
179 exit(do_single(argc, argv, action));
183 case CCD_UNCONFIGALL:
184 exit(do_all(action));
188 exit(dump_ccd(argc, argv));
196 do_single(int argc, char **argv, int action)
198 struct ccd_ioctl ccio;
199 char *ccd, *cp, *cp2, **disks;
200 int noflags = 0, i, ileave, flags = 0, j;
202 bzero(&ccio, sizeof(ccio));
205 * If unconfiguring, all arguments are treated as ccds.
207 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
208 for (i = 0; argc != 0; ) {
209 cp = *argv++; --argc;
210 if ((ccd = resolve_ccdname(cp)) == NULL) {
211 warnx("invalid ccd name: %s", cp);
215 if (do_io(ccd, CCDIOCCLR, &ccio))
219 printf("%s unconfigured\n", cp);
224 /* Make sure there are enough arguments. */
227 /* Assume that no flags are specified. */
230 if (action == CCD_CONFIGALL) {
231 warnx("%s: bad line: %d", ccdconf, lineno);
238 /* First argument is the ccd to configure. */
239 cp = *argv++; --argc;
240 if ((ccd = resolve_ccdname(cp)) == NULL) {
241 warnx("invalid ccd name: %s", cp);
245 /* Next argument is the interleave factor. */
246 cp = *argv++; --argc;
247 errno = 0; /* to check for ERANGE */
248 ileave = (int)strtol(cp, &cp2, 10);
249 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
250 warnx("invalid interleave factor: %s", cp);
255 /* Next argument is the ccd configuration flags. */
256 cp = *argv++; --argc;
257 if ((flags = flags_to_val(cp)) < 0) {
258 warnx("invalid flags argument: %s", cp);
263 /* Next is the list of disks to make the ccd from. */
264 disks = malloc(argc * sizeof(char *));
266 warnx("no memory to configure ccd");
269 for (i = 0; argc != 0; ) {
270 cp = *argv++; --argc;
271 if ((j = checkdev(cp)) == 0)
274 warnx("%s: %s", cp, strerror(j));
279 /* Fill in the ccio. */
280 ccio.ccio_disks = disks;
281 ccio.ccio_ndisks = i;
282 ccio.ccio_ileave = ileave;
283 ccio.ccio_flags = flags;
285 if (do_io(ccd, CCDIOCSET, &ccio)) {
291 printf("ccd%d: %d components ", ccio.ccio_unit,
293 for (i = 0; i < ccio.ccio_ndisks; ++i) {
294 if ((cp2 = strrchr(disks[i], '/')) != NULL)
299 i == 0 ? '(' : ' ', cp2,
300 i == ccio.ccio_ndisks - 1 ? ')' : ',');
302 printf(", %lu blocks ", (u_long)ccio.ccio_size);
303 if (ccio.ccio_ileave != 0)
304 printf("interleaved at %d blocks\n", ccio.ccio_ileave);
306 printf("concatenated\n");
317 char line[_POSIX2_LINE_MAX];
325 if ((f = fopen(ccdconf, "r")) == NULL) {
327 warn("fopen: %s", ccdconf);
332 while (fgets(line, sizeof(line), f) != NULL) {
336 if ((cp = strrchr(line, '\n')) != NULL)
339 /* Break up the line and pass it's contents to do_single(). */
342 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
345 if ((argv = realloc(argv,
346 sizeof(char *) * ++argc)) == NULL) {
347 warnx("no memory to configure ccds");
352 * If our action is to unconfigure all, then pass
353 * just the first token to do_single() and ignore
354 * the rest. Since this will be encountered on
355 * our first pass through the line, the Right
358 if (action == CCD_UNCONFIGALL) {
359 if (do_single(argc, argv, action))
365 if (do_single(argc, argv, action))
382 if (stat(path, &st) != 0)
385 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
392 pathtounit(char *path, int *unitp)
397 if (stat(path, &st) != 0)
400 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
403 if ((maxpartitions = getmaxpartitions()) < 0)
406 *unitp = minor(st.st_rdev) / maxpartitions;
412 resolve_ccdname(char *name)
418 if (name[0] == '/' || name[0] == '.') {
419 /* Assume they gave the correct pathname. */
420 return (strdup(name));
430 if ((rawpart = getrawpartition()) < 0)
432 asprintf(&path, "%s%s%c", _PATH_DEV, name, 'a' + rawpart);
434 asprintf(&path, "%s%s", _PATH_DEV, name);
441 do_io(char *path, u_long cmd, struct ccd_ioctl *cciop)
446 if ((fd = open(path, O_RDWR, 0640)) < 0) {
447 warn("open: %s", path);
451 if (ioctl(fd, cmd, cciop) < 0) {
464 warn("ioctl (%s): %s", cp, path);
471 #define KVM_ABORT(kd, str) { \
473 warnx("%s", (str)); \
474 warnx("%s", kvm_geterr((kd))); \
479 dump_ccd(int argc, char **argv)
481 char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
482 struct ccd_softc *cs, *kcs;
484 int i, error, numccd, numconfiged = 0;
487 bzero(errbuf, sizeof(errbuf));
489 if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
491 warnx("can't open kvm: %s", errbuf);
495 if (kvm_nlist(kd, nl))
496 KVM_ABORT(kd, "ccd-related symbols not available");
498 /* Check to see how many ccds are currently configured. */
499 if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
500 sizeof(numccd)) != sizeof(numccd))
501 KVM_ABORT(kd, "can't determine number of configured ccds");
504 printf("ccd driver in kernel, but is uninitialized\n");
508 /* Allocate space for the configuration data. */
509 readsize = numccd * sizeof(struct ccd_softc);
510 if ((cs = malloc(readsize)) == NULL) {
511 warnx("no memory for configuration data");
517 * Read the ccd configuration data from the kernel and dump
520 if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
521 sizeof(kcs)) != sizeof(kcs)) {
523 KVM_ABORT(kd, "can't find pointer to configuration data");
525 if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
527 KVM_ABORT(kd, "can't read configuration data");
531 for (i = 0; i < numccd; ++i)
532 if (cs[i].sc_flags & CCDF_INITED) {
534 print_ccd_info(&cs[i], kd);
537 if (numconfiged == 0)
538 printf("no concatenated disks configured\n");
541 cp = *argv++; --argc;
542 if ((ccd = resolve_ccdname(cp)) == NULL) {
543 warnx("invalid ccd name: %s", cp);
546 if ((error = pathtounit(ccd, &i)) != 0) {
547 warnx("%s: %s", ccd, strerror(error));
551 warnx("ccd%d not configured", i);
554 if (cs[i].sc_flags & CCDF_INITED)
555 print_ccd_info(&cs[i], kd);
557 printf("ccd%d not configured\n", i);
573 print_ccd_info(struct ccd_softc *cs, kvm_t *kd)
575 static int header_printed = 0;
576 struct ccdcinfo *cip;
578 char path[MAXPATHLEN];
581 if (header_printed == 0 && verbose) {
582 printf("# ccd\t\tileave\tflags\tcompnent devices\n");
586 readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
587 if ((cip = malloc(readsize)) == NULL) {
588 warn("ccd%d: can't allocate memory for component info",
592 bzero(cip, readsize);
594 /* Dump out softc information. */
595 printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
596 cs->sc_cflags & CCDF_USERMASK);
599 /* Read in the component info. */
600 if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
601 readsize) != readsize) {
603 warnx("can't read component info");
604 warnx("%s", kvm_geterr(kd));
608 /* Read component pathname and display component info. */
609 for (i = 0; i < cs->sc_nccdisks; ++i) {
610 if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
611 cip[i].ci_pathlen) != cip[i].ci_pathlen) {
613 warnx("can't read component pathname");
614 warnx("%s", kvm_geterr(kd));
617 printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
626 getmaxpartitions(void)
628 return (MAXPARTITIONS);
632 getrawpartition(void)
638 flags_to_val(char *flags)
641 int i, tmp, val = ~CCDF_USERMASK;
645 * The most common case is that of NIL flags, so check for
648 if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
649 strcmp("0", flags) == 0)
652 flagslen = strlen(flags);
654 /* Check for values represented by strings. */
655 if ((cp = strdup(flags)) == NULL)
656 err(1, "no memory to parse flags");
658 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
659 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
660 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
662 if (flagvaltab[i].fv_flag == NULL) {
666 tmp |= flagvaltab[i].fv_val;
669 /* If we get here, the string was ok. */
676 /* Check for values represented in hex. */
677 if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
678 errno = 0; /* to check for ERANGE */
679 val = (int)strtol(&flags[2], &cp, 16);
680 if ((errno == ERANGE) || (*cp != '\0'))
685 /* Check for values represented in decimal. */
686 errno = 0; /* to check for ERANGE */
687 val = (int)strtol(flags, &cp, 10);
688 if ((errno == ERANGE) || (*cp != '\0'))
692 return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
698 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
699 "usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
700 " ccdconfig -C [-v] [-f config_file]",
701 " ccdconfig -u [-v] ccd [...]",
702 " ccdconfig -U [-v] [-f config_file]",
703 " ccdconfig -g [-M core] [-N system] [ccd [...]]");
707 /* Local Variables: */
708 /* c-argdecl-indent: 8 */
709 /* c-indent-level: 8 */