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