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