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