ccdconfig.c revision 45329
113044Sasami/*	$NetBSD: ccdconfig.c,v 1.2.2.1 1995/11/11 02:43:35 thorpej Exp $	*/
213044Sasami
313044Sasami/*
413044Sasami * Copyright (c) 1995 Jason R. Thorpe.
513044Sasami * All rights reserved.
613044Sasami *
713044Sasami * Redistribution and use in source and binary forms, with or without
813044Sasami * modification, are permitted provided that the following conditions
913044Sasami * are met:
1013044Sasami * 1. Redistributions of source code must retain the above copyright
1113044Sasami *    notice, this list of conditions and the following disclaimer.
1213044Sasami * 2. Redistributions in binary form must reproduce the above copyright
1313044Sasami *    notice, this list of conditions and the following disclaimer in the
1413044Sasami *    documentation and/or other materials provided with the distribution.
1513044Sasami * 3. All advertising materials mentioning features or use of this software
1613044Sasami *    must display the following acknowledgement:
1713044Sasami *	This product includes software developed for the NetBSD Project
1813044Sasami *	by Jason R. Thorpe.
1913044Sasami * 4. The name of the author may not be used to endorse or promote products
2013044Sasami *    derived from this software without specific prior written permission.
2113044Sasami *
2213044Sasami * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2313044Sasami * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2413044Sasami * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2513044Sasami * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2613044Sasami * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2713044Sasami * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2813044Sasami * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2913044Sasami * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3013044Sasami * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3113044Sasami * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3213044Sasami * SUCH DAMAGE.
3313044Sasami */
3413044Sasami
3536628Scharnier#ifndef lint
3636628Scharnierstatic const char rcsid[] =
3745329Speter	"$Id: ccdconfig.c,v 1.10 1998/09/15 08:15:22 gibbs Exp $";
3836628Scharnier#endif /* not lint */
3936628Scharnier
4013044Sasami#include <sys/param.h>
4113044Sasami#include <sys/disklabel.h>
4213044Sasami#include <sys/device.h>
4313044Sasami#include <sys/disk.h>
4413044Sasami#include <sys/stat.h>
4545329Speter#include <sys/module.h>
4613044Sasami#include <ctype.h>
4713044Sasami#include <err.h>
4813044Sasami#include <errno.h>
4913044Sasami#include <fcntl.h>
5013044Sasami#include <kvm.h>
5113044Sasami#include <limits.h>
5213044Sasami#include <stdio.h>
5313044Sasami#include <stdlib.h>
5413044Sasami#include <string.h>
5513044Sasami#include <unistd.h>
5613044Sasami
5739228Sgibbs#include <sys/devicestat.h>
5813052Sasami#include <sys/ccdvar.h>
5913044Sasami
6013044Sasami#include "pathnames.h"
6113044Sasami
6213044Sasamistatic	int lineno = 0;
6313044Sasamistatic	int verbose = 0;
6413044Sasamistatic	char *ccdconf = _PATH_CCDCONF;
6513044Sasami
6613044Sasamistatic	char *core = NULL;
6713044Sasamistatic	char *kernel = NULL;
6813044Sasami
6913044Sasamistruct	flagval {
7013044Sasami	char	*fv_flag;
7113044Sasami	int	fv_val;
7213044Sasami} flagvaltab[] = {
7313044Sasami	{ "CCDF_SWAP",		CCDF_SWAP },
7413044Sasami	{ "CCDF_UNIFORM",	CCDF_UNIFORM },
7513762Sasami	{ "CCDF_MIRROR",	CCDF_MIRROR },
7613762Sasami	{ "CCDF_PARITY",	CCDF_PARITY },
7713044Sasami	{ NULL,			0 },
7813044Sasami};
7913044Sasami
8013044Sasamistatic	struct nlist nl[] = {
8113044Sasami	{ "_ccd_softc" },
8213044Sasami#define SYM_CCDSOFTC		0
8313044Sasami	{ "_numccd" },
8413044Sasami#define SYM_NUMCCD		1
8513044Sasami	{ NULL },
8613044Sasami};
8713044Sasami
8813044Sasami#define CCD_CONFIG		0	/* configure a device */
8913044Sasami#define CCD_CONFIGALL		1	/* configure all devices */
9013044Sasami#define CCD_UNCONFIG		2	/* unconfigure a device */
9113044Sasami#define CCD_UNCONFIGALL		3	/* unconfigure all devices */
9213044Sasami#define CCD_DUMP		4	/* dump a ccd's configuration */
9313044Sasami
9413044Sasamistatic	int checkdev __P((char *));
9513044Sasamistatic	int do_io __P((char *, u_long, struct ccd_ioctl *));
9613044Sasamistatic	int do_single __P((int, char **, int));
9713044Sasamistatic	int do_all __P((int));
9813044Sasamistatic	int dump_ccd __P((int, char **));
9913044Sasamistatic	int getmaxpartitions __P((void));
10013044Sasamistatic	int getrawpartition __P((void));
10113044Sasamistatic	int flags_to_val __P((char *));
10213044Sasamistatic	void print_ccd_info __P((struct ccd_softc *, kvm_t *));
10313044Sasamistatic	char *resolve_ccdname __P((char *));
10413044Sasamistatic	void usage __P((void));
10513044Sasami
10613044Sasamiint
10713044Sasamimain(argc, argv)
10813044Sasami	int argc;
10913044Sasami	char **argv;
11013044Sasami{
11113044Sasami	int ch, options = 0, action = CCD_CONFIG;
11213044Sasami
11313044Sasami	while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
11413044Sasami		switch (ch) {
11513044Sasami		case 'c':
11613044Sasami			action = CCD_CONFIG;
11713044Sasami			++options;
11813044Sasami			break;
11913044Sasami
12013044Sasami		case 'C':
12113044Sasami			action = CCD_CONFIGALL;
12213044Sasami			++options;
12313044Sasami			break;
12413044Sasami
12513044Sasami		case 'f':
12613044Sasami			ccdconf = optarg;
12713044Sasami			break;
12813044Sasami
12913044Sasami		case 'g':
13013044Sasami			action = CCD_DUMP;
13113044Sasami			break;
13213044Sasami
13313044Sasami		case 'M':
13413044Sasami			core = optarg;
13513044Sasami			break;
13613044Sasami
13713044Sasami		case 'N':
13813044Sasami			kernel = optarg;
13913044Sasami			break;
14013044Sasami
14113044Sasami		case 'u':
14213044Sasami			action = CCD_UNCONFIG;
14313044Sasami			++options;
14413044Sasami			break;
14513044Sasami
14613044Sasami		case 'U':
14713044Sasami			action = CCD_UNCONFIGALL;
14813044Sasami			++options;
14913044Sasami			break;
15013044Sasami
15113044Sasami		case 'v':
15213044Sasami			verbose = 1;
15313044Sasami			break;
15413044Sasami
15513044Sasami		default:
15613044Sasami			usage();
15713044Sasami		}
15813044Sasami	}
15913044Sasami	argc -= optind;
16013044Sasami	argv += optind;
16113044Sasami
16213044Sasami	if (options > 1)
16313044Sasami		usage();
16413044Sasami
16532116Simp	/*
16632116Simp	 * Discard setgid privileges if not the running kernel so that bad
16732116Simp	 * guys can't print interesting stuff from kernel memory.
16832116Simp	 */
16932116Simp	if (core != NULL || kernel != NULL || action != CCD_DUMP) {
17032116Simp		setegid(getgid());
17132116Simp		setgid(getgid());
17232116Simp	}
17332116Simp
17445329Speter	if (modfind("ccd") < 0) {
17545329Speter		/* Not present in kernel, try loading it */
17645329Speter		if (kldload("ccd") < 0 || modfind("ccd") < 0)
17745329Speter			warn("ccd module not available!");
17845329Speter	}
17945329Speter
18013044Sasami	switch (action) {
18113044Sasami		case CCD_CONFIG:
18213044Sasami		case CCD_UNCONFIG:
18313044Sasami			exit(do_single(argc, argv, action));
18413044Sasami			/* NOTREACHED */
18513044Sasami
18613044Sasami		case CCD_CONFIGALL:
18713044Sasami		case CCD_UNCONFIGALL:
18813044Sasami			exit(do_all(action));
18913044Sasami			/* NOTREACHED */
19013044Sasami
19113044Sasami		case CCD_DUMP:
19213044Sasami			exit(dump_ccd(argc, argv));
19313044Sasami			/* NOTREACHED */
19413044Sasami	}
19513044Sasami	/* NOTREACHED */
19636628Scharnier	return (0);
19713044Sasami}
19813044Sasami
19913044Sasamistatic int
20013044Sasamido_single(argc, argv, action)
20113044Sasami	int argc;
20213044Sasami	char **argv;
20313044Sasami	int action;
20413044Sasami{
20513044Sasami	struct ccd_ioctl ccio;
20613044Sasami	char *ccd, *cp, *cp2, **disks;
20736628Scharnier	int noflags = 0, i, ileave, flags, j;
20813044Sasami
20913044Sasami	bzero(&ccio, sizeof(ccio));
21013044Sasami
21113044Sasami	/*
21213044Sasami	 * If unconfiguring, all arguments are treated as ccds.
21313044Sasami	 */
21413044Sasami	if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
21513044Sasami		for (i = 0; argc != 0; ) {
21613044Sasami			cp = *argv++; --argc;
21713044Sasami			if ((ccd = resolve_ccdname(cp)) == NULL) {
21813044Sasami				warnx("invalid ccd name: %s", cp);
21913044Sasami				i = 1;
22013044Sasami				continue;
22113044Sasami			}
22213044Sasami			if (do_io(ccd, CCDIOCCLR, &ccio))
22313044Sasami				i = 1;
22413044Sasami			else
22513044Sasami				if (verbose)
22613044Sasami					printf("%s unconfigured\n", cp);
22713044Sasami		}
22813044Sasami		return (i);
22913044Sasami	}
23013044Sasami
23113044Sasami	/* Make sure there are enough arguments. */
23213044Sasami	if (argc < 4)
23313044Sasami		if (argc == 3) {
23413044Sasami			/* Assume that no flags are specified. */
23513044Sasami			noflags = 1;
23613044Sasami		} else {
23713044Sasami			if (action == CCD_CONFIGALL) {
23813044Sasami				warnx("%s: bad line: %d", ccdconf, lineno);
23913044Sasami				return (1);
24013044Sasami			} else
24113044Sasami				usage();
24213044Sasami		}
24313044Sasami
24413044Sasami	/* First argument is the ccd to configure. */
24513044Sasami	cp = *argv++; --argc;
24613044Sasami	if ((ccd = resolve_ccdname(cp)) == NULL) {
24713044Sasami		warnx("invalid ccd name: %s", cp);
24813044Sasami		return (1);
24913044Sasami	}
25013044Sasami
25113044Sasami	/* Next argument is the interleave factor. */
25213044Sasami	cp = *argv++; --argc;
25313044Sasami	errno = 0;	/* to check for ERANGE */
25413044Sasami	ileave = (int)strtol(cp, &cp2, 10);
25513044Sasami	if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
25613044Sasami		warnx("invalid interleave factor: %s", cp);
25713044Sasami		return (1);
25813044Sasami	}
25913044Sasami
26013044Sasami	if (noflags == 0) {
26113044Sasami		/* Next argument is the ccd configuration flags. */
26213044Sasami		cp = *argv++; --argc;
26313044Sasami		if ((flags = flags_to_val(cp)) < 0) {
26413044Sasami			warnx("invalid flags argument: %s", cp);
26513044Sasami			return (1);
26613044Sasami		}
26713044Sasami	}
26813044Sasami
26913044Sasami	/* Next is the list of disks to make the ccd from. */
27013044Sasami	disks = malloc(argc * sizeof(char *));
27113044Sasami	if (disks == NULL) {
27213044Sasami		warnx("no memory to configure ccd");
27313044Sasami		return (1);
27413044Sasami	}
27513044Sasami	for (i = 0; argc != 0; ) {
27613044Sasami		cp = *argv++; --argc;
27713044Sasami		if ((j = checkdev(cp)) == 0)
27813044Sasami			disks[i++] = cp;
27913044Sasami		else {
28013044Sasami			warnx("%s: %s", cp, strerror(j));
28113044Sasami			return (1);
28213044Sasami		}
28313044Sasami	}
28413044Sasami
28513044Sasami	/* Fill in the ccio. */
28613044Sasami	ccio.ccio_disks = disks;
28713044Sasami	ccio.ccio_ndisks = i;
28813044Sasami	ccio.ccio_ileave = ileave;
28913044Sasami	ccio.ccio_flags = flags;
29013044Sasami
29113044Sasami	if (do_io(ccd, CCDIOCSET, &ccio)) {
29213044Sasami		free(disks);
29313044Sasami		return (1);
29413044Sasami	}
29513044Sasami
29613044Sasami	if (verbose) {
29713044Sasami		printf("ccd%d: %d components ", ccio.ccio_unit,
29813044Sasami		    ccio.ccio_ndisks);
29913044Sasami		for (i = 0; i < ccio.ccio_ndisks; ++i) {
30013044Sasami			if ((cp2 = strrchr(disks[i], '/')) != NULL)
30113044Sasami				++cp2;
30213044Sasami			else
30313044Sasami				cp2 = disks[i];
30413044Sasami			printf("%c%s%c",
30513044Sasami			    i == 0 ? '(' : ' ', cp2,
30613044Sasami			    i == ccio.ccio_ndisks - 1 ? ')' : ',');
30713044Sasami		}
30813044Sasami		printf(", %d blocks ", ccio.ccio_size);
30913044Sasami		if (ccio.ccio_ileave != 0)
31013044Sasami			printf("interleaved at %d blocks\n", ccio.ccio_ileave);
31113044Sasami		else
31213044Sasami			printf("concatenated\n");
31313044Sasami	}
31413044Sasami
31513044Sasami	free(disks);
31613044Sasami	return (0);
31713044Sasami}
31813044Sasami
31913044Sasamistatic int
32013044Sasamido_all(action)
32113044Sasami	int action;
32213044Sasami{
32313044Sasami	FILE *f;
32413044Sasami	char line[_POSIX2_LINE_MAX];
32513044Sasami	char *cp, **argv;
32613044Sasami	int argc, rval;
32732116Simp	gid_t egid;
32813044Sasami
32932116Simp	egid = getegid();
33032116Simp	setegid(getgid());
33113044Sasami	if ((f = fopen(ccdconf, "r")) == NULL) {
33232116Simp		setegid(egid);
33313044Sasami		warn("fopen: %s", ccdconf);
33413044Sasami		return (1);
33513044Sasami	}
33632116Simp	setegid(egid);
33713044Sasami
33813044Sasami	while (fgets(line, sizeof(line), f) != NULL) {
33913044Sasami		argc = 0;
34013044Sasami		argv = NULL;
34113044Sasami		++lineno;
34213044Sasami		if ((cp = strrchr(line, '\n')) != NULL)
34313044Sasami			*cp = '\0';
34413044Sasami
34513044Sasami		/* Break up the line and pass it's contents to do_single(). */
34613044Sasami		if (line[0] == '\0')
34713044Sasami			goto end_of_line;
34813044Sasami		for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
34913044Sasami			if (*cp == '#')
35013044Sasami				break;
35113044Sasami			if ((argv = realloc(argv,
35213044Sasami			    sizeof(char *) * ++argc)) == NULL) {
35313044Sasami				warnx("no memory to configure ccds");
35413044Sasami				return (1);
35513044Sasami			}
35613044Sasami			argv[argc - 1] = cp;
35713044Sasami			/*
35813044Sasami			 * If our action is to unconfigure all, then pass
35913044Sasami			 * just the first token to do_single() and ignore
36013044Sasami			 * the rest.  Since this will be encountered on
36113044Sasami			 * our first pass through the line, the Right
36213044Sasami			 * Thing will happen.
36313044Sasami			 */
36413044Sasami			if (action == CCD_UNCONFIGALL) {
36513044Sasami				if (do_single(argc, argv, action))
36613044Sasami					rval = 1;
36713044Sasami				goto end_of_line;
36813044Sasami			}
36913044Sasami		}
37013044Sasami		if (argc != 0)
37113044Sasami			if (do_single(argc, argv, action))
37213044Sasami				rval = 1;
37313044Sasami
37413044Sasami end_of_line:
37513044Sasami		if (argv != NULL)
37613044Sasami			free(argv);
37713044Sasami	}
37813044Sasami
37913044Sasami	(void)fclose(f);
38013044Sasami	return (rval);
38113044Sasami}
38213044Sasami
38313044Sasamistatic int
38413044Sasamicheckdev(path)
38513044Sasami	char *path;
38613044Sasami{
38713044Sasami	struct stat st;
38813044Sasami
38913044Sasami	if (stat(path, &st) != 0)
39013044Sasami		return (errno);
39113044Sasami
39213044Sasami	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
39313044Sasami		return (EINVAL);
39413044Sasami
39513044Sasami	return (0);
39613044Sasami}
39713044Sasami
39813044Sasamistatic int
39913044Sasamipathtounit(path, unitp)
40013044Sasami	char *path;
40113044Sasami	int *unitp;
40213044Sasami{
40313044Sasami	struct stat st;
40413044Sasami	int maxpartitions;
40513044Sasami
40613044Sasami	if (stat(path, &st) != 0)
40713044Sasami		return (errno);
40813044Sasami
40913044Sasami	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
41013044Sasami		return (EINVAL);
41113044Sasami
41213044Sasami	if ((maxpartitions = getmaxpartitions()) < 0)
41313044Sasami		return (errno);
41413044Sasami
41513044Sasami	*unitp = minor(st.st_rdev) / maxpartitions;
41613044Sasami
41713044Sasami	return (0);
41813044Sasami}
41913044Sasami
42013044Sasamistatic char *
42113044Sasamiresolve_ccdname(name)
42213044Sasami	char *name;
42313044Sasami{
42436628Scharnier	char c, *path;
42513044Sasami	size_t len, newlen;
42613044Sasami	int rawpart;
42713044Sasami
42813044Sasami	if (name[0] == '/' || name[0] == '.') {
42913044Sasami		/* Assume they gave the correct pathname. */
43013044Sasami		return (strdup(name));
43113044Sasami	}
43213044Sasami
43313044Sasami	len = strlen(name);
43413044Sasami	c = name[len - 1];
43513044Sasami
43613044Sasami	newlen = len + 8;
43713044Sasami	if ((path = malloc(newlen)) == NULL)
43813044Sasami		return (NULL);
43913044Sasami	bzero(path, newlen);
44013044Sasami
44113044Sasami	if (isdigit(c)) {
44213044Sasami		if ((rawpart = getrawpartition()) < 0) {
44313044Sasami			free(path);
44413044Sasami			return (NULL);
44513044Sasami		}
44613044Sasami		(void)sprintf(path, "/dev/%s%c", name, 'a' + rawpart);
44713044Sasami	} else
44813044Sasami		(void)sprintf(path, "/dev/%s", name);
44913044Sasami
45013044Sasami	return (path);
45113044Sasami}
45213044Sasami
45313044Sasamistatic int
45413044Sasamido_io(path, cmd, cciop)
45513044Sasami	char *path;
45613044Sasami	u_long cmd;
45713044Sasami	struct ccd_ioctl *cciop;
45813044Sasami{
45913044Sasami	int fd;
46013044Sasami	char *cp;
46113044Sasami
46213044Sasami	if ((fd = open(path, O_RDWR, 0640)) < 0) {
46313044Sasami		warn("open: %s", path);
46413044Sasami		return (1);
46513044Sasami	}
46613044Sasami
46713044Sasami	if (ioctl(fd, cmd, cciop) < 0) {
46813044Sasami		switch (cmd) {
46913044Sasami		case CCDIOCSET:
47013044Sasami			cp = "CCDIOCSET";
47113044Sasami			break;
47213044Sasami
47313044Sasami		case CCDIOCCLR:
47413044Sasami			cp = "CCDIOCCLR";
47513044Sasami			break;
47613044Sasami
47713044Sasami		default:
47813044Sasami			cp = "unknown";
47913044Sasami		}
48013044Sasami		warn("ioctl (%s): %s", cp, path);
48113044Sasami		return (1);
48213044Sasami	}
48313044Sasami
48413044Sasami	return (0);
48513044Sasami}
48613044Sasami
48713044Sasami#define KVM_ABORT(kd, str) {						\
48813044Sasami	(void)kvm_close((kd));						\
48913044Sasami	warnx((str));							\
49013044Sasami	warnx(kvm_geterr((kd)));					\
49113044Sasami	return (1);							\
49213044Sasami}
49313044Sasami
49413044Sasamistatic int
49513044Sasamidump_ccd(argc, argv)
49613044Sasami	int argc;
49713044Sasami	char **argv;
49813044Sasami{
49913044Sasami	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
50013044Sasami	struct ccd_softc *cs, *kcs;
50113044Sasami	size_t readsize;
50213044Sasami	int i, error, numccd, numconfiged = 0;
50313044Sasami	kvm_t *kd;
50413044Sasami
50513044Sasami	bzero(errbuf, sizeof(errbuf));
50613044Sasami
50713044Sasami	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
50813044Sasami	    errbuf)) == NULL) {
50913044Sasami		warnx("can't open kvm: %s", errbuf);
51013044Sasami		return (1);
51113044Sasami	}
51213044Sasami
51313044Sasami	if (kvm_nlist(kd, nl))
51413044Sasami		KVM_ABORT(kd, "ccd-related symbols not available");
51513044Sasami
51613044Sasami	/* Check to see how many ccds are currently configured. */
51713044Sasami	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
51813044Sasami	    sizeof(numccd)) != sizeof(numccd))
51913044Sasami		KVM_ABORT(kd, "can't determine number of configured ccds");
52013044Sasami
52113044Sasami	if (numccd == 0) {
52213044Sasami		printf("ccd driver in kernel, but is uninitialized\n");
52313044Sasami		goto done;
52413044Sasami	}
52513044Sasami
52613044Sasami	/* Allocate space for the configuration data. */
52713044Sasami	readsize = numccd * sizeof(struct ccd_softc);
52813044Sasami	if ((cs = malloc(readsize)) == NULL) {
52913044Sasami		warnx("no memory for configuration data");
53013044Sasami		goto bad;
53113044Sasami	}
53213044Sasami	bzero(cs, readsize);
53313044Sasami
53413044Sasami	/*
53513044Sasami	 * Read the ccd configuration data from the kernel and dump
53613044Sasami	 * it to stdout.
53713044Sasami	 */
53813044Sasami	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
53913044Sasami	    sizeof(kcs)) != sizeof(kcs)) {
54013044Sasami		free(cs);
54113044Sasami		KVM_ABORT(kd, "can't find pointer to configuration data");
54213044Sasami	}
54313044Sasami	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
54413044Sasami		free(cs);
54513044Sasami		KVM_ABORT(kd, "can't read configuration data");
54613044Sasami	}
54713044Sasami
54813044Sasami	if (argc == 0) {
54913044Sasami		for (i = 0; i < numccd; ++i)
55013044Sasami			if (cs[i].sc_flags & CCDF_INITED) {
55113044Sasami				++numconfiged;
55213044Sasami				print_ccd_info(&cs[i], kd);
55313044Sasami			}
55413044Sasami
55513044Sasami		if (numconfiged == 0)
55613044Sasami			printf("no concatenated disks configured\n");
55713044Sasami	} else {
55813044Sasami		while (argc) {
55913044Sasami			cp = *argv++; --argc;
56013044Sasami			if ((ccd = resolve_ccdname(cp)) == NULL) {
56113044Sasami				warnx("invalid ccd name: %s", cp);
56213044Sasami				continue;
56313044Sasami			}
56413044Sasami			if ((error = pathtounit(ccd, &i)) != 0) {
56513044Sasami				warnx("%s: %s", ccd, strerror(error));
56613044Sasami				continue;
56713044Sasami			}
56813044Sasami			if (i >= numccd) {
56913044Sasami				warnx("ccd%d not configured", i);
57013044Sasami				continue;
57113044Sasami			}
57213044Sasami			if (cs[i].sc_flags & CCDF_INITED)
57313044Sasami				print_ccd_info(&cs[i], kd);
57413044Sasami			else
57513044Sasami				printf("ccd%d not configured\n", i);
57613044Sasami		}
57713044Sasami	}
57813044Sasami
57913044Sasami	free(cs);
58013044Sasami
58113044Sasami done:
58213044Sasami	(void)kvm_close(kd);
58313044Sasami	return (0);
58413044Sasami
58513044Sasami bad:
58613044Sasami	(void)kvm_close(kd);
58713044Sasami	return (1);
58813044Sasami}
58913044Sasami
59013044Sasamistatic void
59113044Sasamiprint_ccd_info(cs, kd)
59213044Sasami	struct ccd_softc *cs;
59313044Sasami	kvm_t *kd;
59413044Sasami{
59513044Sasami	static int header_printed = 0;
59613044Sasami	struct ccdcinfo *cip;
59713044Sasami	size_t readsize;
59813044Sasami	char path[MAXPATHLEN];
59913044Sasami	int i;
60013044Sasami
60113044Sasami	if (header_printed == 0 && verbose) {
60213044Sasami		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
60313044Sasami		header_printed = 1;
60413044Sasami	}
60513044Sasami
60613044Sasami	readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
60713044Sasami	if ((cip = malloc(readsize)) == NULL) {
60813044Sasami		warn("ccd%d: can't allocate memory for component info",
60913044Sasami		    cs->sc_unit);
61013044Sasami		return;
61113044Sasami	}
61213044Sasami	bzero(cip, readsize);
61313044Sasami
61413044Sasami	/* Dump out softc information. */
61513044Sasami	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
61613044Sasami	    cs->sc_cflags & CCDF_USERMASK);
61713044Sasami	fflush(stdout);
61813044Sasami
61913044Sasami	/* Read in the component info. */
62013044Sasami	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
62113044Sasami	    readsize) != readsize) {
62213044Sasami		printf("\n");
62313044Sasami		warnx("can't read component info");
62413044Sasami		warnx(kvm_geterr(kd));
62513044Sasami		goto done;
62613044Sasami	}
62713044Sasami
62813044Sasami	/* Read component pathname and display component info. */
62913044Sasami	for (i = 0; i < cs->sc_nccdisks; ++i) {
63013044Sasami		if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
63113044Sasami		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
63213044Sasami			printf("\n");
63313044Sasami			warnx("can't read component pathname");
63413044Sasami			warnx(kvm_geterr(kd));
63513044Sasami			goto done;
63613044Sasami		}
63713044Sasami		printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
63813044Sasami		fflush(stdout);
63913044Sasami	}
64013044Sasami
64113044Sasami done:
64213044Sasami	free(cip);
64313044Sasami}
64413044Sasami
64513044Sasamistatic int
64613044Sasamigetmaxpartitions()
64713044Sasami{
64813052Sasami    return (MAXPARTITIONS);
64913044Sasami}
65013044Sasami
65113044Sasamistatic int
65213044Sasamigetrawpartition()
65313044Sasami{
65413052Sasami	return (RAW_PART);
65513044Sasami}
65613044Sasami
65713044Sasamistatic int
65813044Sasamiflags_to_val(flags)
65913044Sasami	char *flags;
66013044Sasami{
66113044Sasami	char *cp, *tok;
66213044Sasami	int i, tmp, val = ~CCDF_USERMASK;
66313044Sasami	size_t flagslen;
66413044Sasami
66513044Sasami	/*
66613044Sasami	 * The most common case is that of NIL flags, so check for
66713044Sasami	 * those first.
66813044Sasami	 */
66913044Sasami	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
67013044Sasami	    strcmp("0", flags) == 0)
67113044Sasami		return (0);
67213044Sasami
67313044Sasami	flagslen = strlen(flags);
67413044Sasami
67513044Sasami	/* Check for values represented by strings. */
67613044Sasami	if ((cp = strdup(flags)) == NULL)
67713044Sasami		err(1, "no memory to parse flags");
67813044Sasami	tmp = 0;
67913044Sasami	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
68013044Sasami		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
68113044Sasami			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
68213044Sasami				break;
68313044Sasami		if (flagvaltab[i].fv_flag == NULL) {
68413044Sasami			free(cp);
68513044Sasami			goto bad_string;
68613044Sasami		}
68713044Sasami		tmp |= flagvaltab[i].fv_val;
68813044Sasami	}
68913044Sasami
69013044Sasami	/* If we get here, the string was ok. */
69113044Sasami	free(cp);
69213044Sasami	val = tmp;
69313044Sasami	goto out;
69413044Sasami
69513044Sasami bad_string:
69613044Sasami
69713044Sasami	/* Check for values represented in hex. */
69813044Sasami	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
69913044Sasami		errno = 0;	/* to check for ERANGE */
70013044Sasami		val = (int)strtol(&flags[2], &cp, 16);
70113044Sasami		if ((errno == ERANGE) || (*cp != '\0'))
70213044Sasami			return (-1);
70313044Sasami		goto out;
70413044Sasami	}
70513044Sasami
70613044Sasami	/* Check for values represented in decimal. */
70713044Sasami	errno = 0;	/* to check for ERANGE */
70813044Sasami	val = (int)strtol(flags, &cp, 10);
70913044Sasami	if ((errno == ERANGE) || (*cp != '\0'))
71013044Sasami		return (-1);
71113044Sasami
71213044Sasami out:
71313044Sasami	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
71413044Sasami}
71513044Sasami
71613044Sasamistatic void
71713044Sasamiusage()
71813044Sasami{
71926541Scharnier	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
72026541Scharnier		"usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
72126541Scharnier		"       ccdconfig -C [-v] [-f config_file]",
72226541Scharnier		"       ccdconfig -u [-v] ccd [...]",
72326541Scharnier		"       ccdconfig -U [-v] [-f config_file]",
72426541Scharnier		"       ccdconfig -g [-M core] [-N system] [ccd [...]]");
72513044Sasami	exit(1);
72613044Sasami}
72726541Scharnier
72813052Sasami/* Local Variables: */
72913052Sasami/* c-argdecl-indent: 8 */
73013052Sasami/* c-indent-level: 8 */
73113052Sasami/* End: */
732