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[] =
37 "$FreeBSD: src/sbin/ccdconfig/ccdconfig.c,v 1.16.2.2 2000/12/11 01:03:25 obrien Exp $";
40 #include <sys/param.h>
41 #include <sys/linker.h>
42 #include <sys/disklabel.h>
44 #include <sys/module.h>
57 #include <sys/devicestat.h>
58 #include <sys/ccdvar.h>
60 #include "pathnames.h"
62 static int lineno = 0;
63 static int verbose = 0;
64 static char *ccdconf = _PATH_CCDCONF;
66 static char *core = NULL;
67 static char *kernel = NULL;
73 { "CCDF_SWAP", CCDF_SWAP },
74 { "CCDF_UNIFORM", CCDF_UNIFORM },
75 { "CCDF_MIRROR", CCDF_MIRROR },
76 { "CCDF_PARITY", CCDF_PARITY },
80 static struct nlist nl[] = {
82 #define SYM_CCDSOFTC 0
88 #define CCD_CONFIG 0 /* configure a device */
89 #define CCD_CONFIGALL 1 /* configure all devices */
90 #define CCD_UNCONFIG 2 /* unconfigure a device */
91 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
92 #define CCD_DUMP 4 /* dump a ccd's configuration */
94 static int checkdev __P((char *));
95 static int do_io __P((char *, u_long, struct ccd_ioctl *));
96 static int do_single __P((int, char **, int));
97 static int do_all __P((int));
98 static int dump_ccd __P((int, char **));
99 static int getmaxpartitions __P((void));
100 static int getrawpartition __P((void));
101 static int flags_to_val __P((char *));
102 static void print_ccd_info __P((struct ccd_softc *, kvm_t *));
103 static char *resolve_ccdname __P((char *));
104 static void usage __P((void));
111 int ch, options = 0, action = CCD_CONFIG;
113 while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
121 action = CCD_CONFIGALL;
142 action = CCD_UNCONFIG;
147 action = CCD_UNCONFIGALL;
166 * Discard setgid privileges if not the running kernel so that bad
167 * guys can't print interesting stuff from kernel memory.
169 if (core != NULL || kernel != NULL || action != CCD_DUMP) {
174 if (modfind("ccd") < 0) {
175 /* Not present in kernel, try loading it */
176 if (kldload("ccd") < 0 || modfind("ccd") < 0)
177 warn("ccd module not available!");
183 exit(do_single(argc, argv, action));
187 case CCD_UNCONFIGALL:
188 exit(do_all(action));
192 exit(dump_ccd(argc, argv));
200 do_single(argc, argv, action)
205 struct ccd_ioctl ccio;
206 char *ccd, *cp, *cp2, **disks;
207 int noflags = 0, i, ileave, flags = 0, j;
209 bzero(&ccio, sizeof(ccio));
212 * If unconfiguring, all arguments are treated as ccds.
214 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
215 for (i = 0; argc != 0; ) {
216 cp = *argv++; --argc;
217 if ((ccd = resolve_ccdname(cp)) == NULL) {
218 warnx("invalid ccd name: %s", cp);
222 if (do_io(ccd, CCDIOCCLR, &ccio))
226 printf("%s unconfigured\n", cp);
231 /* Make sure there are enough arguments. */
234 /* Assume that no flags are specified. */
237 if (action == CCD_CONFIGALL) {
238 warnx("%s: bad line: %d", ccdconf, lineno);
245 /* First argument is the ccd to configure. */
246 cp = *argv++; --argc;
247 if ((ccd = resolve_ccdname(cp)) == NULL) {
248 warnx("invalid ccd name: %s", cp);
252 /* Next argument is the interleave factor. */
253 cp = *argv++; --argc;
254 errno = 0; /* to check for ERANGE */
255 ileave = (int)strtol(cp, &cp2, 10);
256 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
257 warnx("invalid interleave factor: %s", cp);
262 /* Next argument is the ccd configuration flags. */
263 cp = *argv++; --argc;
264 if ((flags = flags_to_val(cp)) < 0) {
265 warnx("invalid flags argument: %s", cp);
270 /* Next is the list of disks to make the ccd from. */
271 disks = malloc(argc * sizeof(char *));
273 warnx("no memory to configure ccd");
276 for (i = 0; argc != 0; ) {
277 cp = *argv++; --argc;
278 if ((j = checkdev(cp)) == 0)
281 warnx("%s: %s", cp, strerror(j));
286 /* Fill in the ccio. */
287 ccio.ccio_disks = disks;
288 ccio.ccio_ndisks = i;
289 ccio.ccio_ileave = ileave;
290 ccio.ccio_flags = flags;
292 if (do_io(ccd, CCDIOCSET, &ccio)) {
298 printf("ccd%d: %d components ", ccio.ccio_unit,
300 for (i = 0; i < ccio.ccio_ndisks; ++i) {
301 if ((cp2 = strrchr(disks[i], '/')) != NULL)
306 i == 0 ? '(' : ' ', cp2,
307 i == ccio.ccio_ndisks - 1 ? ')' : ',');
309 printf(", %lu blocks ", (u_long)ccio.ccio_size);
310 if (ccio.ccio_ileave != 0)
311 printf("interleaved at %d blocks\n", ccio.ccio_ileave);
313 printf("concatenated\n");
325 char line[_POSIX2_LINE_MAX];
333 if ((f = fopen(ccdconf, "r")) == NULL) {
335 warn("fopen: %s", ccdconf);
340 while (fgets(line, sizeof(line), f) != NULL) {
344 if ((cp = strrchr(line, '\n')) != NULL)
347 /* Break up the line and pass it's contents to do_single(). */
350 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
353 if ((argv = realloc(argv,
354 sizeof(char *) * ++argc)) == NULL) {
355 warnx("no memory to configure ccds");
360 * If our action is to unconfigure all, then pass
361 * just the first token to do_single() and ignore
362 * the rest. Since this will be encountered on
363 * our first pass through the line, the Right
366 if (action == CCD_UNCONFIGALL) {
367 if (do_single(argc, argv, action))
373 if (do_single(argc, argv, action))
391 if (stat(path, &st) != 0)
394 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
401 pathtounit(path, unitp)
408 if (stat(path, &st) != 0)
411 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
414 if ((maxpartitions = getmaxpartitions()) < 0)
417 *unitp = minor(st.st_rdev) / maxpartitions;
423 resolve_ccdname(name)
430 if (name[0] == '/' || name[0] == '.') {
431 /* Assume they gave the correct pathname. */
432 return (strdup(name));
439 if ((path = malloc(newlen)) == NULL)
444 if ((rawpart = getrawpartition()) < 0) {
448 (void)sprintf(path, "%s%s%c", _PATH_DEV, name, 'a' + rawpart);
450 (void)sprintf(path, "%s%s", _PATH_DEV, name);
456 do_io(path, cmd, cciop)
459 struct ccd_ioctl *cciop;
464 if ((fd = open(path, O_RDWR, 0640)) < 0) {
465 warn("open: %s", path);
469 if (ioctl(fd, cmd, cciop) < 0) {
482 warn("ioctl (%s): %s", cp, path);
489 #define KVM_ABORT(kd, str) { \
490 (void)kvm_close((kd)); \
491 warnx("%s", (str)); \
492 warnx("%s", kvm_geterr((kd))); \
501 char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
502 struct ccd_softc *cs, *kcs;
504 int i, error, numccd, numconfiged = 0;
507 bzero(errbuf, sizeof(errbuf));
509 if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
511 warnx("can't open kvm: %s", errbuf);
515 if (kvm_nlist(kd, nl))
516 KVM_ABORT(kd, "ccd-related symbols not available");
518 /* Check to see how many ccds are currently configured. */
519 if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
520 sizeof(numccd)) != sizeof(numccd))
521 KVM_ABORT(kd, "can't determine number of configured ccds");
524 printf("ccd driver in kernel, but is uninitialized\n");
528 /* Allocate space for the configuration data. */
529 readsize = numccd * sizeof(struct ccd_softc);
530 if ((cs = malloc(readsize)) == NULL) {
531 warnx("no memory for configuration data");
537 * Read the ccd configuration data from the kernel and dump
540 if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
541 sizeof(kcs)) != sizeof(kcs)) {
543 KVM_ABORT(kd, "can't find pointer to configuration data");
545 if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
547 KVM_ABORT(kd, "can't read configuration data");
551 for (i = 0; i < numccd; ++i)
552 if (cs[i].sc_flags & CCDF_INITED) {
554 print_ccd_info(&cs[i], kd);
557 if (numconfiged == 0)
558 printf("no concatenated disks configured\n");
561 cp = *argv++; --argc;
562 if ((ccd = resolve_ccdname(cp)) == NULL) {
563 warnx("invalid ccd name: %s", cp);
566 if ((error = pathtounit(ccd, &i)) != 0) {
567 warnx("%s: %s", ccd, strerror(error));
571 warnx("ccd%d not configured", i);
574 if (cs[i].sc_flags & CCDF_INITED)
575 print_ccd_info(&cs[i], kd);
577 printf("ccd%d not configured\n", i);
593 print_ccd_info(cs, kd)
594 struct ccd_softc *cs;
597 static int header_printed = 0;
598 struct ccdcinfo *cip;
600 char path[MAXPATHLEN];
603 if (header_printed == 0 && verbose) {
604 printf("# ccd\t\tileave\tflags\tcompnent devices\n");
608 readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
609 if ((cip = malloc(readsize)) == NULL) {
610 warn("ccd%d: can't allocate memory for component info",
614 bzero(cip, readsize);
616 /* Dump out softc information. */
617 printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
618 cs->sc_cflags & CCDF_USERMASK);
621 /* Read in the component info. */
622 if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
623 readsize) != readsize) {
625 warnx("can't read component info");
626 warnx("%s", kvm_geterr(kd));
630 /* Read component pathname and display component info. */
631 for (i = 0; i < cs->sc_nccdisks; ++i) {
632 if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
633 cip[i].ci_pathlen) != cip[i].ci_pathlen) {
635 warnx("can't read component pathname");
636 warnx("%s", kvm_geterr(kd));
639 printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
650 return (MAXPARTITIONS);
664 int i, tmp, val = ~CCDF_USERMASK;
668 * The most common case is that of NIL flags, so check for
671 if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
672 strcmp("0", flags) == 0)
675 flagslen = strlen(flags);
677 /* Check for values represented by strings. */
678 if ((cp = strdup(flags)) == NULL)
679 err(1, "no memory to parse flags");
681 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
682 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
683 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
685 if (flagvaltab[i].fv_flag == NULL) {
689 tmp |= flagvaltab[i].fv_val;
692 /* If we get here, the string was ok. */
699 /* Check for values represented in hex. */
700 if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
701 errno = 0; /* to check for ERANGE */
702 val = (int)strtol(&flags[2], &cp, 16);
703 if ((errno == ERANGE) || (*cp != '\0'))
708 /* Check for values represented in decimal. */
709 errno = 0; /* to check for ERANGE */
710 val = (int)strtol(flags, &cp, 10);
711 if ((errno == ERANGE) || (*cp != '\0'))
715 return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
721 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
722 "usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
723 " ccdconfig -C [-v] [-f config_file]",
724 " ccdconfig -u [-v] ccd [...]",
725 " ccdconfig -U [-v] [-f config_file]",
726 " ccdconfig -g [-M core] [-N system] [ccd [...]]");
730 /* Local Variables: */
731 /* c-argdecl-indent: 8 */
732 /* c-indent-level: 8 */