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