ccdconfig.c revision 36628
1/*	$NetBSD: ccdconfig.c,v 1.2.2.1 1995/11/11 02:43:35 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 1995 Jason R. Thorpe.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed for the NetBSD Project
18 *	by Jason R. Thorpe.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifndef lint
36static const char rcsid[] =
37	"$Id$";
38#endif /* not lint */
39
40#include <sys/param.h>
41#include <sys/disklabel.h>
42#include <sys/device.h>
43#include <sys/disk.h>
44#include <sys/stat.h>
45#include <ctype.h>
46#include <err.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <kvm.h>
50#include <limits.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	void print_ccd_info __P((struct ccd_softc *, kvm_t *));
101static	char *resolve_ccdname __P((char *));
102static	void usage __P((void));
103
104int
105main(argc, argv)
106	int argc;
107	char **argv;
108{
109	int ch, options = 0, action = CCD_CONFIG;
110
111	while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
112		switch (ch) {
113		case 'c':
114			action = CCD_CONFIG;
115			++options;
116			break;
117
118		case 'C':
119			action = CCD_CONFIGALL;
120			++options;
121			break;
122
123		case 'f':
124			ccdconf = optarg;
125			break;
126
127		case 'g':
128			action = CCD_DUMP;
129			break;
130
131		case 'M':
132			core = optarg;
133			break;
134
135		case 'N':
136			kernel = optarg;
137			break;
138
139		case 'u':
140			action = CCD_UNCONFIG;
141			++options;
142			break;
143
144		case 'U':
145			action = CCD_UNCONFIGALL;
146			++options;
147			break;
148
149		case 'v':
150			verbose = 1;
151			break;
152
153		default:
154			usage();
155		}
156	}
157	argc -= optind;
158	argv += optind;
159
160	if (options > 1)
161		usage();
162
163	/*
164	 * Discard setgid privileges if not the running kernel so that bad
165	 * guys can't print interesting stuff from kernel memory.
166	 */
167	if (core != NULL || kernel != NULL || action != CCD_DUMP) {
168		setegid(getgid());
169		setgid(getgid());
170	}
171
172	switch (action) {
173		case CCD_CONFIG:
174		case CCD_UNCONFIG:
175			exit(do_single(argc, argv, action));
176			/* NOTREACHED */
177
178		case CCD_CONFIGALL:
179		case CCD_UNCONFIGALL:
180			exit(do_all(action));
181			/* NOTREACHED */
182
183		case CCD_DUMP:
184			exit(dump_ccd(argc, argv));
185			/* NOTREACHED */
186	}
187	/* NOTREACHED */
188	return (0);
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;
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	int maxpartitions;
397
398	if (stat(path, &st) != 0)
399		return (errno);
400
401	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
402		return (EINVAL);
403
404	if ((maxpartitions = getmaxpartitions()) < 0)
405		return (errno);
406
407	*unitp = minor(st.st_rdev) / maxpartitions;
408
409	return (0);
410}
411
412static char *
413resolve_ccdname(name)
414	char *name;
415{
416	char c, *path;
417	size_t len, newlen;
418	int rawpart;
419
420	if (name[0] == '/' || name[0] == '.') {
421		/* Assume they gave the correct pathname. */
422		return (strdup(name));
423	}
424
425	len = strlen(name);
426	c = name[len - 1];
427
428	newlen = len + 8;
429	if ((path = malloc(newlen)) == NULL)
430		return (NULL);
431	bzero(path, newlen);
432
433	if (isdigit(c)) {
434		if ((rawpart = getrawpartition()) < 0) {
435			free(path);
436			return (NULL);
437		}
438		(void)sprintf(path, "/dev/%s%c", name, 'a' + rawpart);
439	} else
440		(void)sprintf(path, "/dev/%s", name);
441
442	return (path);
443}
444
445static int
446do_io(path, cmd, cciop)
447	char *path;
448	u_long cmd;
449	struct ccd_ioctl *cciop;
450{
451	int fd;
452	char *cp;
453
454	if ((fd = open(path, O_RDWR, 0640)) < 0) {
455		warn("open: %s", path);
456		return (1);
457	}
458
459	if (ioctl(fd, cmd, cciop) < 0) {
460		switch (cmd) {
461		case CCDIOCSET:
462			cp = "CCDIOCSET";
463			break;
464
465		case CCDIOCCLR:
466			cp = "CCDIOCCLR";
467			break;
468
469		default:
470			cp = "unknown";
471		}
472		warn("ioctl (%s): %s", cp, path);
473		return (1);
474	}
475
476	return (0);
477}
478
479#define KVM_ABORT(kd, str) {						\
480	(void)kvm_close((kd));						\
481	warnx((str));							\
482	warnx(kvm_geterr((kd)));					\
483	return (1);							\
484}
485
486static int
487dump_ccd(argc, argv)
488	int argc;
489	char **argv;
490{
491	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
492	struct ccd_softc *cs, *kcs;
493	size_t readsize;
494	int i, error, numccd, numconfiged = 0;
495	kvm_t *kd;
496
497	bzero(errbuf, sizeof(errbuf));
498
499	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
500	    errbuf)) == NULL) {
501		warnx("can't open kvm: %s", errbuf);
502		return (1);
503	}
504
505	if (kvm_nlist(kd, nl))
506		KVM_ABORT(kd, "ccd-related symbols not available");
507
508	/* Check to see how many ccds are currently configured. */
509	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
510	    sizeof(numccd)) != sizeof(numccd))
511		KVM_ABORT(kd, "can't determine number of configured ccds");
512
513	if (numccd == 0) {
514		printf("ccd driver in kernel, but is uninitialized\n");
515		goto done;
516	}
517
518	/* Allocate space for the configuration data. */
519	readsize = numccd * sizeof(struct ccd_softc);
520	if ((cs = malloc(readsize)) == NULL) {
521		warnx("no memory for configuration data");
522		goto bad;
523	}
524	bzero(cs, readsize);
525
526	/*
527	 * Read the ccd configuration data from the kernel and dump
528	 * it to stdout.
529	 */
530	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
531	    sizeof(kcs)) != sizeof(kcs)) {
532		free(cs);
533		KVM_ABORT(kd, "can't find pointer to configuration data");
534	}
535	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
536		free(cs);
537		KVM_ABORT(kd, "can't read configuration data");
538	}
539
540	if (argc == 0) {
541		for (i = 0; i < numccd; ++i)
542			if (cs[i].sc_flags & CCDF_INITED) {
543				++numconfiged;
544				print_ccd_info(&cs[i], kd);
545			}
546
547		if (numconfiged == 0)
548			printf("no concatenated disks configured\n");
549	} else {
550		while (argc) {
551			cp = *argv++; --argc;
552			if ((ccd = resolve_ccdname(cp)) == NULL) {
553				warnx("invalid ccd name: %s", cp);
554				continue;
555			}
556			if ((error = pathtounit(ccd, &i)) != 0) {
557				warnx("%s: %s", ccd, strerror(error));
558				continue;
559			}
560			if (i >= numccd) {
561				warnx("ccd%d not configured", i);
562				continue;
563			}
564			if (cs[i].sc_flags & CCDF_INITED)
565				print_ccd_info(&cs[i], kd);
566			else
567				printf("ccd%d not configured\n", i);
568		}
569	}
570
571	free(cs);
572
573 done:
574	(void)kvm_close(kd);
575	return (0);
576
577 bad:
578	(void)kvm_close(kd);
579	return (1);
580}
581
582static void
583print_ccd_info(cs, kd)
584	struct ccd_softc *cs;
585	kvm_t *kd;
586{
587	static int header_printed = 0;
588	struct ccdcinfo *cip;
589	size_t readsize;
590	char path[MAXPATHLEN];
591	int i;
592
593	if (header_printed == 0 && verbose) {
594		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
595		header_printed = 1;
596	}
597
598	readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
599	if ((cip = malloc(readsize)) == NULL) {
600		warn("ccd%d: can't allocate memory for component info",
601		    cs->sc_unit);
602		return;
603	}
604	bzero(cip, readsize);
605
606	/* Dump out softc information. */
607	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
608	    cs->sc_cflags & CCDF_USERMASK);
609	fflush(stdout);
610
611	/* Read in the component info. */
612	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
613	    readsize) != readsize) {
614		printf("\n");
615		warnx("can't read component info");
616		warnx(kvm_geterr(kd));
617		goto done;
618	}
619
620	/* Read component pathname and display component info. */
621	for (i = 0; i < cs->sc_nccdisks; ++i) {
622		if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
623		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
624			printf("\n");
625			warnx("can't read component pathname");
626			warnx(kvm_geterr(kd));
627			goto done;
628		}
629		printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
630		fflush(stdout);
631	}
632
633 done:
634	free(cip);
635}
636
637static int
638getmaxpartitions()
639{
640    return (MAXPARTITIONS);
641}
642
643static int
644getrawpartition()
645{
646	return (RAW_PART);
647}
648
649static int
650flags_to_val(flags)
651	char *flags;
652{
653	char *cp, *tok;
654	int i, tmp, val = ~CCDF_USERMASK;
655	size_t flagslen;
656
657	/*
658	 * The most common case is that of NIL flags, so check for
659	 * those first.
660	 */
661	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
662	    strcmp("0", flags) == 0)
663		return (0);
664
665	flagslen = strlen(flags);
666
667	/* Check for values represented by strings. */
668	if ((cp = strdup(flags)) == NULL)
669		err(1, "no memory to parse flags");
670	tmp = 0;
671	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
672		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
673			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
674				break;
675		if (flagvaltab[i].fv_flag == NULL) {
676			free(cp);
677			goto bad_string;
678		}
679		tmp |= flagvaltab[i].fv_val;
680	}
681
682	/* If we get here, the string was ok. */
683	free(cp);
684	val = tmp;
685	goto out;
686
687 bad_string:
688
689	/* Check for values represented in hex. */
690	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
691		errno = 0;	/* to check for ERANGE */
692		val = (int)strtol(&flags[2], &cp, 16);
693		if ((errno == ERANGE) || (*cp != '\0'))
694			return (-1);
695		goto out;
696	}
697
698	/* Check for values represented in decimal. */
699	errno = 0;	/* to check for ERANGE */
700	val = (int)strtol(flags, &cp, 10);
701	if ((errno == ERANGE) || (*cp != '\0'))
702		return (-1);
703
704 out:
705	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
706}
707
708static void
709usage()
710{
711	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
712		"usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
713		"       ccdconfig -C [-v] [-f config_file]",
714		"       ccdconfig -u [-v] ccd [...]",
715		"       ccdconfig -U [-v] [-f config_file]",
716		"       ccdconfig -g [-M core] [-N system] [ccd [...]]");
717	exit(1);
718}
719
720/* Local Variables: */
721/* c-argdecl-indent: 8 */
722/* c-indent-level: 8 */
723/* End: */
724