Home | History | Annotate | Line # | Download | only in ccdconfig
ccdconfig.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: ccdconfig.c,v 1.2 1995/08/23 01:06:59 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1995 Jason R. Thorpe.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.1  thorpej  *    must display the following acknowledgement:
     17  1.1  thorpej  *	This product includes software developed for the NetBSD Project
     18  1.1  thorpej  *	by Jason R. Thorpe.
     19  1.1  thorpej  * 4. The name of the author may not be used to endorse or promote products
     20  1.1  thorpej  *    derived from this software without specific prior written permission.
     21  1.1  thorpej  *
     22  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  1.1  thorpej  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  1.1  thorpej  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  1.1  thorpej  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.1  thorpej  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  thorpej  * SUCH DAMAGE.
     33  1.1  thorpej  */
     34  1.1  thorpej 
     35  1.1  thorpej #include <sys/param.h>
     36  1.1  thorpej #include <sys/ioctl.h>
     37  1.1  thorpej #include <sys/disklabel.h>
     38  1.1  thorpej #include <sys/device.h>
     39  1.1  thorpej #include <sys/disk.h>
     40  1.1  thorpej #include <sys/stat.h>
     41  1.1  thorpej #include <sys/sysctl.h>
     42  1.1  thorpej #include <ctype.h>
     43  1.1  thorpej #include <err.h>
     44  1.1  thorpej #include <errno.h>
     45  1.1  thorpej #include <fcntl.h>
     46  1.1  thorpej #include <kvm.h>
     47  1.1  thorpej #include <limits.h>
     48  1.1  thorpej #include <nlist.h>
     49  1.1  thorpej #include <stdio.h>
     50  1.1  thorpej #include <stdlib.h>
     51  1.1  thorpej #include <string.h>
     52  1.1  thorpej #include <unistd.h>
     53  1.1  thorpej 
     54  1.1  thorpej #include <dev/ccdvar.h>
     55  1.1  thorpej 
     56  1.1  thorpej #include "pathnames.h"
     57  1.1  thorpej 
     58  1.1  thorpej extern	char *__progname;
     59  1.1  thorpej 
     60  1.1  thorpej static	int lineno = 0;
     61  1.1  thorpej static	int verbose = 0;
     62  1.1  thorpej static	char *ccdconf = _PATH_CCDCONF;
     63  1.1  thorpej 
     64  1.1  thorpej static	char *core = NULL;
     65  1.1  thorpej static	char *kernel = NULL;
     66  1.1  thorpej 
     67  1.1  thorpej struct	flagval {
     68  1.1  thorpej 	char	*fv_flag;
     69  1.1  thorpej 	int	fv_val;
     70  1.1  thorpej } flagvaltab[] = {
     71  1.1  thorpej 	{ "CCDF_SWAP",		CCDF_SWAP },
     72  1.1  thorpej 	{ "CCDF_UNIFORM",	CCDF_UNIFORM },
     73  1.1  thorpej 	{ NULL,			0 },
     74  1.1  thorpej };
     75  1.1  thorpej 
     76  1.1  thorpej static	struct nlist nl[] = {
     77  1.1  thorpej 	{ "_ccd_softc" },
     78  1.1  thorpej #define SYM_CCDSOFTC		0
     79  1.1  thorpej 	{ "_numccd" },
     80  1.1  thorpej #define SYM_NUMCCD		1
     81  1.1  thorpej 	{ NULL },
     82  1.1  thorpej };
     83  1.1  thorpej 
     84  1.1  thorpej #define CCD_CONFIG		0	/* configure a device */
     85  1.1  thorpej #define CCD_CONFIGALL		1	/* configure all devices */
     86  1.1  thorpej #define CCD_UNCONFIG		2	/* unconfigure a device */
     87  1.1  thorpej #define CCD_UNCONFIGALL		3	/* unconfigure all devices */
     88  1.1  thorpej #define CCD_DUMP		4	/* dump a ccd's configuration */
     89  1.1  thorpej 
     90  1.1  thorpej static	int checkdev __P((char *));
     91  1.1  thorpej static	int do_io __P((char *, u_long, struct ccd_ioctl *));
     92  1.1  thorpej static	int do_single __P((int, char **, int));
     93  1.1  thorpej static	int do_all __P((int));
     94  1.1  thorpej static	int dump_ccd __P((int, char **));
     95  1.1  thorpej static	int getmaxpartitions __P((void));
     96  1.1  thorpej static	int getrawpartition __P((void));
     97  1.1  thorpej static	int flags_to_val __P((char *));
     98  1.1  thorpej static	int pathtodevt __P((char *, dev_t *));
     99  1.1  thorpej static	void print_ccd_info __P((struct ccd_softc *, kvm_t *));
    100  1.1  thorpej static	char *resolve_ccdname __P((char *));
    101  1.1  thorpej static	void usage __P((void));
    102  1.1  thorpej 
    103  1.1  thorpej int
    104  1.1  thorpej main(argc, argv)
    105  1.1  thorpej 	int argc;
    106  1.1  thorpej 	char **argv;
    107  1.1  thorpej {
    108  1.1  thorpej 	int ch, options = 0, action = CCD_CONFIG;
    109  1.1  thorpej 
    110  1.1  thorpej 	while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
    111  1.1  thorpej 		switch (ch) {
    112  1.1  thorpej 		case 'c':
    113  1.1  thorpej 			action = CCD_CONFIG;
    114  1.1  thorpej 			++options;
    115  1.1  thorpej 			break;
    116  1.1  thorpej 
    117  1.1  thorpej 		case 'C':
    118  1.1  thorpej 			action = CCD_CONFIGALL;
    119  1.1  thorpej 			++options;
    120  1.1  thorpej 			break;
    121  1.1  thorpej 
    122  1.1  thorpej 		case 'f':
    123  1.1  thorpej 			ccdconf = optarg;
    124  1.1  thorpej 			break;
    125  1.1  thorpej 
    126  1.1  thorpej 		case 'g':
    127  1.1  thorpej 			action = CCD_DUMP;
    128  1.1  thorpej 			break;
    129  1.1  thorpej 
    130  1.1  thorpej 		case 'M':
    131  1.1  thorpej 			core = optarg;
    132  1.1  thorpej 			break;
    133  1.1  thorpej 
    134  1.1  thorpej 		case 'N':
    135  1.1  thorpej 			core = optarg;
    136  1.1  thorpej 			break;
    137  1.1  thorpej 
    138  1.1  thorpej 		case 'u':
    139  1.1  thorpej 			action = CCD_UNCONFIG;
    140  1.1  thorpej 			++options;
    141  1.1  thorpej 			break;
    142  1.1  thorpej 
    143  1.1  thorpej 		case 'U':
    144  1.1  thorpej 			action = CCD_UNCONFIGALL;
    145  1.1  thorpej 			++options;
    146  1.1  thorpej 			break;
    147  1.1  thorpej 
    148  1.1  thorpej 		case 'v':
    149  1.1  thorpej 			verbose = 1;
    150  1.1  thorpej 			break;
    151  1.1  thorpej 
    152  1.1  thorpej 		default:
    153  1.1  thorpej 			usage();
    154  1.1  thorpej 		}
    155  1.1  thorpej 	}
    156  1.1  thorpej 	argc -= optind;
    157  1.1  thorpej 	argv += optind;
    158  1.1  thorpej 
    159  1.1  thorpej 	if (options > 1)
    160  1.1  thorpej 		usage();
    161  1.1  thorpej 
    162  1.1  thorpej 	switch (action) {
    163  1.1  thorpej 		case CCD_CONFIG:
    164  1.1  thorpej 		case CCD_UNCONFIG:
    165  1.1  thorpej 			exit(do_single(argc, argv, action));
    166  1.1  thorpej 			/* NOTREACHED */
    167  1.1  thorpej 
    168  1.1  thorpej 		case CCD_CONFIGALL:
    169  1.1  thorpej 		case CCD_UNCONFIGALL:
    170  1.1  thorpej 			exit(do_all(action));
    171  1.1  thorpej 			/* NOTREACHED */
    172  1.1  thorpej 
    173  1.1  thorpej 		case CCD_DUMP:
    174  1.1  thorpej 			exit(dump_ccd(argc, argv));
    175  1.1  thorpej 			/* NOTREACHED */
    176  1.1  thorpej 	}
    177  1.1  thorpej 	/* NOTREACHED */
    178  1.1  thorpej }
    179  1.1  thorpej 
    180  1.1  thorpej static int
    181  1.1  thorpej do_single(argc, argv, action)
    182  1.1  thorpej 	int argc;
    183  1.1  thorpej 	char **argv;
    184  1.1  thorpej 	int action;
    185  1.1  thorpej {
    186  1.1  thorpej 	struct ccd_ioctl ccio;
    187  1.1  thorpej 	char *ccd, *cp, *cp2, **disks;
    188  1.1  thorpej 	int noflags = 0, i, ileave, flags, j, error;
    189  1.1  thorpej 
    190  1.1  thorpej 	bzero(&ccio, sizeof(ccio));
    191  1.1  thorpej 
    192  1.1  thorpej 	/*
    193  1.1  thorpej 	 * If unconfiguring, all arguments are treated as ccds.
    194  1.1  thorpej 	 */
    195  1.1  thorpej 	if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
    196  1.1  thorpej 		for (i = 0; argc != 0; ) {
    197  1.1  thorpej 			cp = *argv++; --argc;
    198  1.1  thorpej 			if ((ccd = resolve_ccdname(cp)) == NULL) {
    199  1.1  thorpej 				warnx("invalid ccd name: %s", cp);
    200  1.1  thorpej 				i = 1;
    201  1.1  thorpej 				continue;
    202  1.1  thorpej 			}
    203  1.1  thorpej 			if (do_io(ccd, CCDIOCCLR, &ccio))
    204  1.1  thorpej 				i = 1;
    205  1.1  thorpej 			else
    206  1.1  thorpej 				if (verbose)
    207  1.2  thorpej 					printf("%s unconfigured\n", cp);
    208  1.1  thorpej 		}
    209  1.1  thorpej 		return (i);
    210  1.1  thorpej 	}
    211  1.1  thorpej 
    212  1.1  thorpej 	/* Make sure there are enough arguments. */
    213  1.1  thorpej 	if (argc < 4)
    214  1.1  thorpej 		if (argc == 3) {
    215  1.1  thorpej 			/* Assume that no flags are specified. */
    216  1.1  thorpej 			noflags = 1;
    217  1.1  thorpej 		} else {
    218  1.1  thorpej 			if (action == CCD_CONFIGALL) {
    219  1.1  thorpej 				warnx("%s: bad line: %d", ccdconf, lineno);
    220  1.1  thorpej 				return (1);
    221  1.1  thorpej 			} else
    222  1.1  thorpej 				usage();
    223  1.1  thorpej 		}
    224  1.1  thorpej 
    225  1.1  thorpej 	/* First argument is the ccd to configure. */
    226  1.1  thorpej 	cp = *argv++; --argc;
    227  1.1  thorpej 	if ((ccd = resolve_ccdname(cp)) == NULL) {
    228  1.1  thorpej 		warnx("invalid ccd name: %s", cp);
    229  1.1  thorpej 		return (1);
    230  1.1  thorpej 	}
    231  1.1  thorpej 
    232  1.1  thorpej 	/* Next argument is the interleave factor. */
    233  1.1  thorpej 	cp = *argv++; --argc;
    234  1.1  thorpej 	errno = 0;	/* to check for ERANGE */
    235  1.1  thorpej 	ileave = (int)strtol(cp, &cp2, 10);
    236  1.1  thorpej 	if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
    237  1.1  thorpej 		warnx("invalid interleave factor: %s", cp);
    238  1.1  thorpej 		return (1);
    239  1.1  thorpej 	}
    240  1.1  thorpej 
    241  1.1  thorpej 	if (noflags == 0) {
    242  1.1  thorpej 		/* Next argument is the ccd configuration flags. */
    243  1.1  thorpej 		cp = *argv++; --argc;
    244  1.1  thorpej 		if ((flags = flags_to_val(cp)) < 0) {
    245  1.1  thorpej 			warnx("invalid flags argument: %s", cp);
    246  1.1  thorpej 			return (1);
    247  1.1  thorpej 		}
    248  1.1  thorpej 	}
    249  1.1  thorpej 
    250  1.1  thorpej 	/* Next is the list of disks to make the ccd from. */
    251  1.1  thorpej 	disks = malloc(argc * sizeof(char *));
    252  1.1  thorpej 	if (disks == NULL) {
    253  1.1  thorpej 		warnx("no memory to configure ccd");
    254  1.1  thorpej 		return (1);
    255  1.1  thorpej 	}
    256  1.1  thorpej 	for (i = 0; argc != 0; ) {
    257  1.1  thorpej 		cp = *argv++; --argc;
    258  1.1  thorpej 		if ((j = checkdev(cp)) == 0)
    259  1.1  thorpej 			disks[i++] = cp;
    260  1.1  thorpej 		else {
    261  1.1  thorpej 			warnx("%s: %s", cp, strerror(j));
    262  1.1  thorpej 			return (1);
    263  1.1  thorpej 		}
    264  1.1  thorpej 	}
    265  1.1  thorpej 
    266  1.1  thorpej 	/* Fill in the ccio. */
    267  1.1  thorpej 	ccio.ccio_disks = disks;
    268  1.1  thorpej 	ccio.ccio_ndisks = i;
    269  1.1  thorpej 	ccio.ccio_ileave = ileave;
    270  1.1  thorpej 	ccio.ccio_flags = flags;
    271  1.1  thorpej 
    272  1.1  thorpej 	if (do_io(ccd, CCDIOCSET, &ccio)) {
    273  1.1  thorpej 		free(disks);
    274  1.1  thorpej 		return (1);
    275  1.1  thorpej 	}
    276  1.1  thorpej 
    277  1.1  thorpej 	if (verbose) {
    278  1.1  thorpej 		printf("ccd%d: %d components ", ccio.ccio_unit,
    279  1.1  thorpej 		    ccio.ccio_ndisks);
    280  1.1  thorpej 		for (i = 0; i < ccio.ccio_ndisks; ++i) {
    281  1.1  thorpej 			if ((cp2 = strrchr(disks[i], '/')) != NULL)
    282  1.1  thorpej 				++cp2;
    283  1.1  thorpej 			else
    284  1.1  thorpej 				cp2 = disks[i];
    285  1.1  thorpej 			printf("%c%s%c",
    286  1.1  thorpej 			    i == 0 ? '(' : ' ', cp2,
    287  1.1  thorpej 			    i == ccio.ccio_ndisks - 1 ? ')' : ',');
    288  1.1  thorpej 		}
    289  1.1  thorpej 		printf(", %d blocks ", ccio.ccio_size);
    290  1.1  thorpej 		if (ccio.ccio_ileave != 0)
    291  1.1  thorpej 			printf("interleaved at %d blocks\n", ccio.ccio_ileave);
    292  1.1  thorpej 		else
    293  1.1  thorpej 			printf("concatenated\n");
    294  1.1  thorpej 	}
    295  1.1  thorpej 
    296  1.1  thorpej 	free(disks);
    297  1.1  thorpej 	return (0);
    298  1.1  thorpej }
    299  1.1  thorpej 
    300  1.1  thorpej static int
    301  1.1  thorpej do_all(action)
    302  1.1  thorpej 	int action;
    303  1.1  thorpej {
    304  1.1  thorpej 	FILE *f;
    305  1.1  thorpej 	char line[_POSIX2_LINE_MAX];
    306  1.1  thorpej 	char *cp, **argv;
    307  1.1  thorpej 	int argc, rval;
    308  1.1  thorpej 
    309  1.1  thorpej 	if ((f = fopen(ccdconf, "r")) == NULL) {
    310  1.1  thorpej 		warn("fopen: %s", ccdconf);
    311  1.1  thorpej 		return (1);
    312  1.1  thorpej 	}
    313  1.1  thorpej 
    314  1.1  thorpej 	while (fgets(line, sizeof(line), f) != NULL) {
    315  1.1  thorpej 		argc = 0;
    316  1.1  thorpej 		argv = NULL;
    317  1.1  thorpej 		++lineno;
    318  1.1  thorpej 		if ((cp = strrchr(line, '\n')) != NULL)
    319  1.1  thorpej 			*cp = '\0';
    320  1.1  thorpej 
    321  1.1  thorpej 		/* Break up the line and pass it's contents to do_single(). */
    322  1.1  thorpej 		if (line[0] == '\0')
    323  1.1  thorpej 			goto end_of_line;
    324  1.1  thorpej 		for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
    325  1.1  thorpej 			if (*cp == '#')
    326  1.1  thorpej 				break;
    327  1.1  thorpej 			if ((argv = realloc(argv,
    328  1.1  thorpej 			    sizeof(char *) * ++argc)) == NULL) {
    329  1.1  thorpej 				warnx("no memory to configure ccds");
    330  1.1  thorpej 				return (1);
    331  1.1  thorpej 			}
    332  1.1  thorpej 			argv[argc - 1] = cp;
    333  1.1  thorpej 			/*
    334  1.1  thorpej 			 * If our action is to unconfigure all, then pass
    335  1.1  thorpej 			 * just the first token to do_single() and ignore
    336  1.1  thorpej 			 * the rest.  Since this will be encountered on
    337  1.1  thorpej 			 * our first pass through the line, the Right
    338  1.1  thorpej 			 * Thing will happen.
    339  1.1  thorpej 			 */
    340  1.1  thorpej 			if (action == CCD_UNCONFIGALL) {
    341  1.1  thorpej 				if (do_single(argc, argv, action))
    342  1.1  thorpej 					rval = 1;
    343  1.1  thorpej 				goto end_of_line;
    344  1.1  thorpej 			}
    345  1.1  thorpej 		}
    346  1.1  thorpej 		if (argc != 0)
    347  1.1  thorpej 			if (do_single(argc, argv, action))
    348  1.1  thorpej 				rval = 1;
    349  1.1  thorpej 
    350  1.1  thorpej  end_of_line:
    351  1.1  thorpej 		if (argv != NULL)
    352  1.1  thorpej 			free(argv);
    353  1.1  thorpej 	}
    354  1.1  thorpej 
    355  1.1  thorpej 	(void)fclose(f);
    356  1.1  thorpej 	return (rval);
    357  1.1  thorpej }
    358  1.1  thorpej 
    359  1.1  thorpej static int
    360  1.1  thorpej checkdev(path)
    361  1.1  thorpej 	char *path;
    362  1.1  thorpej {
    363  1.1  thorpej 	struct stat st;
    364  1.1  thorpej 
    365  1.1  thorpej 	if (stat(path, &st) != 0)
    366  1.1  thorpej 		return (errno);
    367  1.1  thorpej 
    368  1.1  thorpej 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    369  1.1  thorpej 		return (EINVAL);
    370  1.1  thorpej 
    371  1.1  thorpej 	return (0);
    372  1.1  thorpej }
    373  1.1  thorpej 
    374  1.1  thorpej static int
    375  1.1  thorpej pathtounit(path, unitp)
    376  1.1  thorpej 	char *path;
    377  1.1  thorpej 	int *unitp;
    378  1.1  thorpej {
    379  1.1  thorpej 	struct stat st;
    380  1.1  thorpej 	dev_t dev;
    381  1.1  thorpej 	int maxpartitions;
    382  1.1  thorpej 
    383  1.1  thorpej 	if (stat(path, &st) != 0)
    384  1.1  thorpej 		return (errno);
    385  1.1  thorpej 
    386  1.1  thorpej 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    387  1.1  thorpej 		return (EINVAL);
    388  1.1  thorpej 
    389  1.1  thorpej 	if ((maxpartitions = getmaxpartitions()) < 0)
    390  1.1  thorpej 		return (errno);
    391  1.1  thorpej 
    392  1.1  thorpej 	*unitp = minor(st.st_rdev) / maxpartitions;
    393  1.1  thorpej 
    394  1.1  thorpej 	return (0);
    395  1.1  thorpej }
    396  1.1  thorpej 
    397  1.1  thorpej static char *
    398  1.1  thorpej resolve_ccdname(name)
    399  1.1  thorpej 	char *name;
    400  1.1  thorpej {
    401  1.1  thorpej 	char c, *cp, *path;
    402  1.1  thorpej 	size_t len, newlen;
    403  1.1  thorpej 	int rawpart;
    404  1.1  thorpej 
    405  1.1  thorpej 	if (name[0] == '/' || name[0] == '.') {
    406  1.1  thorpej 		/* Assume they gave the correct pathname. */
    407  1.1  thorpej 		return (strdup(name));
    408  1.1  thorpej 	}
    409  1.1  thorpej 
    410  1.1  thorpej 	len = strlen(name);
    411  1.1  thorpej 	c = name[len - 1];
    412  1.1  thorpej 
    413  1.1  thorpej 	newlen = len + 8;
    414  1.1  thorpej 	if ((path = malloc(newlen)) == NULL)
    415  1.1  thorpej 		return (NULL);
    416  1.1  thorpej 	bzero(path, newlen);
    417  1.1  thorpej 
    418  1.1  thorpej 	if (isdigit(c)) {
    419  1.1  thorpej 		if ((rawpart = getrawpartition()) < 0) {
    420  1.1  thorpej 			free(path);
    421  1.1  thorpej 			return (NULL);
    422  1.1  thorpej 		}
    423  1.1  thorpej 		(void)sprintf(path, "/dev/%s%c", name, 'a' + rawpart);
    424  1.1  thorpej 	} else
    425  1.1  thorpej 		(void)sprintf(path, "/dev/%s", name);
    426  1.1  thorpej 
    427  1.1  thorpej 	return (path);
    428  1.1  thorpej }
    429  1.1  thorpej 
    430  1.1  thorpej static int
    431  1.1  thorpej do_io(path, cmd, cciop)
    432  1.1  thorpej 	char *path;
    433  1.1  thorpej 	u_long cmd;
    434  1.1  thorpej 	struct ccd_ioctl *cciop;
    435  1.1  thorpej {
    436  1.1  thorpej 	int fd;
    437  1.1  thorpej 	char *cp;
    438  1.1  thorpej 
    439  1.1  thorpej 	if ((fd = open(path, O_RDWR, 0640)) < 0) {
    440  1.1  thorpej 		warn("open: %s", path);
    441  1.1  thorpej 		return (1);
    442  1.1  thorpej 	}
    443  1.1  thorpej 
    444  1.1  thorpej 	if (ioctl(fd, cmd, cciop) < 0) {
    445  1.1  thorpej 		switch (cmd) {
    446  1.1  thorpej 		case CCDIOCSET:
    447  1.1  thorpej 			cp = "CCDIOCSET";
    448  1.1  thorpej 			break;
    449  1.1  thorpej 
    450  1.1  thorpej 		case CCDIOCCLR:
    451  1.1  thorpej 			cp = "CCDIOCCLR";
    452  1.1  thorpej 			break;
    453  1.1  thorpej 
    454  1.1  thorpej 		default:
    455  1.1  thorpej 			cp = "unknown";
    456  1.1  thorpej 		}
    457  1.1  thorpej 		warn("ioctl (%s): %s", cp, path);
    458  1.1  thorpej 		return (1);
    459  1.1  thorpej 	}
    460  1.1  thorpej 
    461  1.1  thorpej 	return (0);
    462  1.1  thorpej }
    463  1.1  thorpej 
    464  1.1  thorpej #define KVM_ABORT(kd, str) {						\
    465  1.1  thorpej 	(void)kvm_close((kd));						\
    466  1.1  thorpej 	warnx((str));							\
    467  1.1  thorpej 	warnx(kvm_geterr((kd)));					\
    468  1.1  thorpej 	return (1);							\
    469  1.1  thorpej }
    470  1.1  thorpej 
    471  1.1  thorpej static int
    472  1.1  thorpej dump_ccd(argc, argv)
    473  1.1  thorpej 	int argc;
    474  1.1  thorpej 	char **argv;
    475  1.1  thorpej {
    476  1.1  thorpej 	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
    477  1.1  thorpej 	struct ccd_softc *cs, *kcs;
    478  1.1  thorpej 	size_t readsize;
    479  1.1  thorpej 	int i, error, numccd, numconfiged = 0;
    480  1.1  thorpej 	kvm_t *kd;
    481  1.1  thorpej 
    482  1.1  thorpej 	bzero(errbuf, sizeof(errbuf));
    483  1.1  thorpej 
    484  1.1  thorpej 	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
    485  1.1  thorpej 	    errbuf)) == NULL) {
    486  1.1  thorpej 		warnx("can't open kvm: %s", errbuf);
    487  1.1  thorpej 		return (1);
    488  1.1  thorpej 	}
    489  1.1  thorpej 
    490  1.1  thorpej 	if (kvm_nlist(kd, nl))
    491  1.1  thorpej 		KVM_ABORT(kd, "ccd-related symbols not available");
    492  1.1  thorpej 
    493  1.1  thorpej 	/* Check to see how many ccds are currently configured. */
    494  1.1  thorpej 	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
    495  1.1  thorpej 	    sizeof(numccd)) != sizeof(numccd))
    496  1.1  thorpej 		KVM_ABORT(kd, "can't determine number of configured ccds");
    497  1.1  thorpej 
    498  1.1  thorpej 	if (numccd == 0) {
    499  1.1  thorpej 		printf("ccd driver in kernel, but is uninitialized\n");
    500  1.1  thorpej 		goto done;
    501  1.1  thorpej 	}
    502  1.1  thorpej 
    503  1.1  thorpej 	/* Allocate space for the configuration data. */
    504  1.1  thorpej 	readsize = numccd * sizeof(struct ccd_softc);
    505  1.1  thorpej 	if ((cs = malloc(readsize)) == NULL) {
    506  1.1  thorpej 		warnx("no memory for configuration data");
    507  1.1  thorpej 		goto bad;
    508  1.1  thorpej 	}
    509  1.1  thorpej 	bzero(cs, readsize);
    510  1.1  thorpej 
    511  1.1  thorpej 	/*
    512  1.1  thorpej 	 * Read the ccd configuration data from the kernel and dump
    513  1.1  thorpej 	 * it to stdout.
    514  1.1  thorpej 	 */
    515  1.1  thorpej 	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
    516  1.1  thorpej 	    sizeof(kcs)) != sizeof(kcs)) {
    517  1.1  thorpej 		free(cs);
    518  1.1  thorpej 		KVM_ABORT(kd, "can't find pointer to configuration data");
    519  1.1  thorpej 	}
    520  1.1  thorpej 	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
    521  1.1  thorpej 		free(cs);
    522  1.1  thorpej 		KVM_ABORT(kd, "can't read configuration data");
    523  1.1  thorpej 	}
    524  1.1  thorpej 
    525  1.1  thorpej 	if (argc == 0) {
    526  1.1  thorpej 		for (i = 0; i < numccd; ++i)
    527  1.1  thorpej 			if (cs[i].sc_flags & CCDF_INITED) {
    528  1.1  thorpej 				++numconfiged;
    529  1.1  thorpej 				print_ccd_info(&cs[i], kd);
    530  1.1  thorpej 			}
    531  1.1  thorpej 
    532  1.1  thorpej 		if (numconfiged == 0)
    533  1.1  thorpej 			printf("no concatenated disks configured\n");
    534  1.1  thorpej 	} else {
    535  1.1  thorpej 		while (argc) {
    536  1.1  thorpej 			cp = *argv++; --argc;
    537  1.1  thorpej 			if ((ccd = resolve_ccdname(cp)) == NULL) {
    538  1.1  thorpej 				warnx("invalid ccd name: %s", cp);
    539  1.1  thorpej 				continue;
    540  1.1  thorpej 			}
    541  1.1  thorpej 			if ((error = pathtounit(ccd, &i)) != 0) {
    542  1.1  thorpej 				warnx("%s: %s", ccd, strerror(error));
    543  1.1  thorpej 				continue;
    544  1.1  thorpej 			}
    545  1.1  thorpej 			if (i >= numccd) {
    546  1.1  thorpej 				warnx("ccd%d not configured", i);
    547  1.1  thorpej 				continue;
    548  1.1  thorpej 			}
    549  1.1  thorpej 			if (cs[i].sc_flags & CCDF_INITED)
    550  1.1  thorpej 				print_ccd_info(&cs[i], kd);
    551  1.1  thorpej 			else
    552  1.1  thorpej 				printf("ccd%d not configured\n", i);
    553  1.1  thorpej 		}
    554  1.1  thorpej 	}
    555  1.1  thorpej 
    556  1.1  thorpej 	free(cs);
    557  1.1  thorpej 
    558  1.1  thorpej  done:
    559  1.1  thorpej 	(void)kvm_close(kd);
    560  1.1  thorpej 	return (0);
    561  1.1  thorpej 
    562  1.1  thorpej  bad:
    563  1.1  thorpej 	(void)kvm_close(kd);
    564  1.1  thorpej 	return (1);
    565  1.1  thorpej }
    566  1.1  thorpej 
    567  1.1  thorpej static void
    568  1.1  thorpej print_ccd_info(cs, kd)
    569  1.1  thorpej 	struct ccd_softc *cs;
    570  1.1  thorpej 	kvm_t *kd;
    571  1.1  thorpej {
    572  1.1  thorpej 	static int header_printed = 0;
    573  1.1  thorpej 	struct ccdcinfo *cip;
    574  1.1  thorpej 	size_t readsize;
    575  1.1  thorpej 	char path[MAXPATHLEN];
    576  1.1  thorpej 	int i;
    577  1.1  thorpej 
    578  1.1  thorpej 	if (header_printed == 0 && verbose) {
    579  1.1  thorpej 		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
    580  1.1  thorpej 		header_printed = 1;
    581  1.1  thorpej 	}
    582  1.1  thorpej 
    583  1.1  thorpej 	readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
    584  1.1  thorpej 	if ((cip = malloc(readsize)) == NULL) {
    585  1.1  thorpej 		warn("ccd%d: can't allocate memory for component info",
    586  1.1  thorpej 		    cs->sc_unit);
    587  1.1  thorpej 		return;
    588  1.1  thorpej 	}
    589  1.1  thorpej 	bzero(cip, readsize);
    590  1.1  thorpej 
    591  1.1  thorpej 	/* Dump out softc information. */
    592  1.1  thorpej 	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
    593  1.1  thorpej 	    cs->sc_cflags & CCDF_USERMASK);
    594  1.1  thorpej 	fflush(stdout);
    595  1.1  thorpej 
    596  1.1  thorpej 	/* Read in the component info. */
    597  1.1  thorpej 	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
    598  1.1  thorpej 	    readsize) != readsize) {
    599  1.1  thorpej 		printf("\n");
    600  1.1  thorpej 		warnx("can't read component info");
    601  1.1  thorpej 		warnx(kvm_geterr(kd));
    602  1.1  thorpej 		goto done;
    603  1.1  thorpej 	}
    604  1.1  thorpej 
    605  1.1  thorpej 	/* Read component pathname and display component info. */
    606  1.1  thorpej 	for (i = 0; i < cs->sc_nccdisks; ++i) {
    607  1.1  thorpej 		if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
    608  1.1  thorpej 		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
    609  1.1  thorpej 			printf("\n");
    610  1.1  thorpej 			warnx("can't read component pathname");
    611  1.1  thorpej 			warnx(kvm_geterr(kd));
    612  1.1  thorpej 			goto done;
    613  1.1  thorpej 		}
    614  1.1  thorpej 		printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
    615  1.1  thorpej 		fflush(stdout);
    616  1.1  thorpej 	}
    617  1.1  thorpej 
    618  1.1  thorpej  done:
    619  1.1  thorpej 	free(cip);
    620  1.1  thorpej }
    621  1.1  thorpej 
    622  1.1  thorpej static int
    623  1.1  thorpej getmaxpartitions()
    624  1.1  thorpej {
    625  1.1  thorpej 	int maxpart, mib[2];
    626  1.1  thorpej 	size_t varlen;
    627  1.1  thorpej 
    628  1.1  thorpej 	mib[0] = CTL_KERN;
    629  1.1  thorpej 	mib[1] = KERN_MAXPARTITIONS;
    630  1.1  thorpej 	varlen = sizeof(maxpart);
    631  1.1  thorpej 	if (sysctl(mib, 2, &maxpart, &varlen, NULL, 0) < 0)
    632  1.1  thorpej 		return (-1);
    633  1.1  thorpej 
    634  1.1  thorpej 	return (maxpart);
    635  1.1  thorpej }
    636  1.1  thorpej 
    637  1.1  thorpej static int
    638  1.1  thorpej getrawpartition()
    639  1.1  thorpej {
    640  1.1  thorpej 	int rawpart, mib[2];
    641  1.1  thorpej 	size_t varlen;
    642  1.1  thorpej 
    643  1.1  thorpej 	mib[0] = CTL_KERN;
    644  1.1  thorpej 	mib[1] = KERN_RAWPARTITION;
    645  1.1  thorpej 	varlen = sizeof(rawpart);
    646  1.1  thorpej 	if (sysctl(mib, 2, &rawpart, &varlen, NULL, 0) < 0)
    647  1.1  thorpej 		return (-1);
    648  1.1  thorpej 
    649  1.1  thorpej 	return (rawpart);
    650  1.1  thorpej }
    651  1.1  thorpej 
    652  1.1  thorpej static int
    653  1.1  thorpej flags_to_val(flags)
    654  1.1  thorpej 	char *flags;
    655  1.1  thorpej {
    656  1.1  thorpej 	char *cp, *tok;
    657  1.1  thorpej 	int i, tmp, val = ~CCDF_USERMASK;
    658  1.1  thorpej 	size_t flagslen;
    659  1.1  thorpej 
    660  1.1  thorpej 	/*
    661  1.1  thorpej 	 * The most common case is that of NIL flags, so check for
    662  1.1  thorpej 	 * those first.
    663  1.1  thorpej 	 */
    664  1.1  thorpej 	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
    665  1.1  thorpej 	    strcmp("0", flags) == 0)
    666  1.1  thorpej 		return (0);
    667  1.1  thorpej 
    668  1.1  thorpej 	flagslen = strlen(flags);
    669  1.1  thorpej 
    670  1.1  thorpej 	/* Check for values represented by strings. */
    671  1.1  thorpej 	if ((cp = strdup(flags)) == NULL)
    672  1.1  thorpej 		err(1, "no memory to parse flags");
    673  1.1  thorpej 	tmp = 0;
    674  1.1  thorpej 	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
    675  1.1  thorpej 		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
    676  1.1  thorpej 			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
    677  1.1  thorpej 				break;
    678  1.1  thorpej 		if (flagvaltab[i].fv_flag == NULL) {
    679  1.1  thorpej 			free(cp);
    680  1.1  thorpej 			goto bad_string;
    681  1.1  thorpej 		}
    682  1.1  thorpej 		tmp |= flagvaltab[i].fv_val;
    683  1.1  thorpej 	}
    684  1.1  thorpej 
    685  1.1  thorpej 	/* If we get here, the string was ok. */
    686  1.1  thorpej 	free(cp);
    687  1.1  thorpej 	val = tmp;
    688  1.1  thorpej 	goto out;
    689  1.1  thorpej 
    690  1.1  thorpej  bad_string:
    691  1.1  thorpej 
    692  1.1  thorpej 	/* Check for values represented in hex. */
    693  1.1  thorpej 	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
    694  1.1  thorpej 		errno = 0;	/* to check for ERANGE */
    695  1.1  thorpej 		val = (int)strtol(&flags[2], &cp, 16);
    696  1.1  thorpej 		if ((errno == ERANGE) || (*cp != '\0'))
    697  1.1  thorpej 			return (-1);
    698  1.1  thorpej 		goto out;
    699  1.1  thorpej 	}
    700  1.1  thorpej 
    701  1.1  thorpej 	/* Check for values represented in decimal. */
    702  1.1  thorpej 	errno = 0;	/* to check for ERANGE */
    703  1.1  thorpej 	val = (int)strtol(flags, &cp, 10);
    704  1.1  thorpej 	if ((errno == ERANGE) || (*cp != '\0'))
    705  1.1  thorpej 		return (-1);
    706  1.1  thorpej 
    707  1.1  thorpej  out:
    708  1.1  thorpej 	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
    709  1.1  thorpej }
    710  1.1  thorpej 
    711  1.1  thorpej static void
    712  1.1  thorpej usage()
    713  1.1  thorpej {
    714  1.1  thorpej 
    715  1.1  thorpej 	fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] %s\n", __progname,
    716  1.1  thorpej 	    "dev [...]");
    717  1.1  thorpej 	fprintf(stderr, "       %s -C [-v] [-f config_file]\n", __progname);
    718  1.1  thorpej 	fprintf(stderr, "       %s -u [-v] ccd [...]\n", __progname);
    719  1.1  thorpej 	fprintf(stderr, "       %s -U [-v] [-f config_file]\n", __progname);
    720  1.1  thorpej 	fprintf(stderr, "       %s -g [-M core] [-N system] %s\n", __progname,
    721  1.1  thorpej 	    "[ccd [...]]");
    722  1.1  thorpej 	exit(1);
    723  1.1  thorpej }
    724