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