ccdconfig.c revision 1.7 1 /* $NetBSD: ccdconfig.c,v 1.7 1996/06/06 23:34:39 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 #include <util.h>
58
59 #include <dev/ccdvar.h>
60
61 #include "pathnames.h"
62
63 extern char *__progname;
64
65 static int lineno = 0;
66 static int verbose = 0;
67 static char *ccdconf = _PATH_CCDCONF;
68
69 static char *core = NULL;
70 static char *kernel = NULL;
71
72 struct flagval {
73 char *fv_flag;
74 int fv_val;
75 } flagvaltab[] = {
76 { "CCDF_SWAP", CCDF_SWAP },
77 { "CCDF_UNIFORM", CCDF_UNIFORM },
78 { "CCDF_MIRROR", CCDF_MIRROR },
79 { NULL, 0 },
80 };
81
82 static struct nlist nl[] = {
83 { "_ccd_softc" },
84 #define SYM_CCDSOFTC 0
85 { "_numccd" },
86 #define SYM_NUMCCD 1
87 { NULL },
88 };
89
90 #define CCD_CONFIG 0 /* configure a device */
91 #define CCD_CONFIGALL 1 /* configure all devices */
92 #define CCD_UNCONFIG 2 /* unconfigure a device */
93 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
94 #define CCD_DUMP 4 /* dump a ccd's configuration */
95
96 static int checkdev __P((char *));
97 static int do_io __P((char *, u_long, struct ccd_ioctl *));
98 static int do_single __P((int, char **, int));
99 static int do_all __P((int));
100 static int dump_ccd __P((int, char **));
101 static int flags_to_val __P((char *));
102 static int pathtodevt __P((char *, dev_t *));
103 static void print_ccd_info __P((struct ccd_softc *, kvm_t *));
104 static char *resolve_ccdname __P((char *));
105 static void usage __P((void));
106
107 int
108 main(argc, argv)
109 int argc;
110 char **argv;
111 {
112 int ch, options = 0, action = CCD_CONFIG;
113
114 while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
115 switch (ch) {
116 case 'c':
117 action = CCD_CONFIG;
118 ++options;
119 break;
120
121 case 'C':
122 action = CCD_CONFIGALL;
123 ++options;
124 break;
125
126 case 'f':
127 ccdconf = optarg;
128 break;
129
130 case 'g':
131 action = CCD_DUMP;
132 break;
133
134 case 'M':
135 core = optarg;
136 break;
137
138 case 'N':
139 kernel = optarg;
140 break;
141
142 case 'u':
143 action = CCD_UNCONFIG;
144 ++options;
145 break;
146
147 case 'U':
148 action = CCD_UNCONFIGALL;
149 ++options;
150 break;
151
152 case 'v':
153 verbose = 1;
154 break;
155
156 default:
157 usage();
158 }
159 }
160 argc -= optind;
161 argv += optind;
162
163 if (options > 1)
164 usage();
165
166 /*
167 * Discard setgid privileges if not the running kernel so that bad
168 * guys can't print interesting stuff from kernel memory.
169 */
170 if (core != NULL || kernel != NULL)
171 setgid(getgid());
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
191 static int
192 do_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
311 static int
312 do_all(action)
313 int action;
314 {
315 FILE *f;
316 char line[_POSIX2_LINE_MAX];
317 char *cp, **argv;
318 int argc, rval;
319
320 if ((f = fopen(ccdconf, "r")) == NULL) {
321 warn("fopen: %s", ccdconf);
322 return (1);
323 }
324
325 while (fgets(line, sizeof(line), f) != NULL) {
326 argc = 0;
327 argv = NULL;
328 ++lineno;
329 if ((cp = strrchr(line, '\n')) != NULL)
330 *cp = '\0';
331
332 /* Break up the line and pass it's contents to do_single(). */
333 if (line[0] == '\0')
334 goto end_of_line;
335 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
336 if (*cp == '#')
337 break;
338 if ((argv = realloc(argv,
339 sizeof(char *) * ++argc)) == NULL) {
340 warnx("no memory to configure ccds");
341 return (1);
342 }
343 argv[argc - 1] = cp;
344 /*
345 * If our action is to unconfigure all, then pass
346 * just the first token to do_single() and ignore
347 * the rest. Since this will be encountered on
348 * our first pass through the line, the Right
349 * Thing will happen.
350 */
351 if (action == CCD_UNCONFIGALL) {
352 if (do_single(argc, argv, action))
353 rval = 1;
354 goto end_of_line;
355 }
356 }
357 if (argc != 0)
358 if (do_single(argc, argv, action))
359 rval = 1;
360
361 end_of_line:
362 if (argv != NULL)
363 free(argv);
364 }
365
366 (void)fclose(f);
367 return (rval);
368 }
369
370 static int
371 checkdev(path)
372 char *path;
373 {
374 struct stat st;
375
376 if (stat(path, &st) != 0)
377 return (errno);
378
379 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
380 return (EINVAL);
381
382 return (0);
383 }
384
385 static int
386 pathtounit(path, unitp)
387 char *path;
388 int *unitp;
389 {
390 struct stat st;
391 dev_t dev;
392 int maxpartitions;
393
394 if (stat(path, &st) != 0)
395 return (errno);
396
397 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
398 return (EINVAL);
399
400 if ((maxpartitions = getmaxpartitions()) < 0)
401 return (errno);
402
403 *unitp = minor(st.st_rdev) / maxpartitions;
404
405 return (0);
406 }
407
408 static char *
409 resolve_ccdname(name)
410 char *name;
411 {
412 char c, *cp, *path;
413 size_t len, newlen;
414 int rawpart;
415
416 if (name[0] == '/' || name[0] == '.') {
417 /* Assume they gave the correct pathname. */
418 return (strdup(name));
419 }
420
421 len = strlen(name);
422 c = name[len - 1];
423
424 newlen = len + 8;
425 if ((path = malloc(newlen)) == NULL)
426 return (NULL);
427 bzero(path, newlen);
428
429 if (isdigit(c)) {
430 if ((rawpart = getrawpartition()) < 0) {
431 free(path);
432 return (NULL);
433 }
434 (void)sprintf(path, "/dev/%s%c", name, 'a' + rawpart);
435 } else
436 (void)sprintf(path, "/dev/%s", name);
437
438 return (path);
439 }
440
441 static int
442 do_io(path, cmd, cciop)
443 char *path;
444 u_long cmd;
445 struct ccd_ioctl *cciop;
446 {
447 int fd;
448 char *cp;
449
450 if ((fd = open(path, O_RDWR, 0640)) < 0) {
451 warn("open: %s", path);
452 return (1);
453 }
454
455 if (ioctl(fd, cmd, cciop) < 0) {
456 switch (cmd) {
457 case CCDIOCSET:
458 cp = "CCDIOCSET";
459 break;
460
461 case CCDIOCCLR:
462 cp = "CCDIOCCLR";
463 break;
464
465 default:
466 cp = "unknown";
467 }
468 warn("ioctl (%s): %s", cp, path);
469 return (1);
470 }
471
472 return (0);
473 }
474
475 #define KVM_ABORT(kd, str) { \
476 (void)kvm_close((kd)); \
477 warnx((str)); \
478 warnx(kvm_geterr((kd))); \
479 return (1); \
480 }
481
482 static int
483 dump_ccd(argc, argv)
484 int argc;
485 char **argv;
486 {
487 char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
488 struct ccd_softc *cs, *kcs;
489 size_t readsize;
490 int i, error, numccd, numconfiged = 0;
491 kvm_t *kd;
492
493 bzero(errbuf, sizeof(errbuf));
494
495 if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
496 errbuf)) == NULL) {
497 warnx("can't open kvm: %s", errbuf);
498 return (1);
499 }
500
501 if (kvm_nlist(kd, nl))
502 KVM_ABORT(kd, "ccd-related symbols not available");
503
504 /* Check to see how many ccds are currently configured. */
505 if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
506 sizeof(numccd)) != sizeof(numccd))
507 KVM_ABORT(kd, "can't determine number of configured ccds");
508
509 if (numccd == 0) {
510 printf("ccd driver in kernel, but is uninitialized\n");
511 goto done;
512 }
513
514 /* Allocate space for the configuration data. */
515 readsize = numccd * sizeof(struct ccd_softc);
516 if ((cs = malloc(readsize)) == NULL) {
517 warnx("no memory for configuration data");
518 goto bad;
519 }
520 bzero(cs, readsize);
521
522 /*
523 * Read the ccd configuration data from the kernel and dump
524 * it to stdout.
525 */
526 if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
527 sizeof(kcs)) != sizeof(kcs)) {
528 free(cs);
529 KVM_ABORT(kd, "can't find pointer to configuration data");
530 }
531 if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
532 free(cs);
533 KVM_ABORT(kd, "can't read configuration data");
534 }
535
536 if (argc == 0) {
537 for (i = 0; i < numccd; ++i)
538 if (cs[i].sc_flags & CCDF_INITED) {
539 ++numconfiged;
540 print_ccd_info(&cs[i], kd);
541 }
542
543 if (numconfiged == 0)
544 printf("no concatenated disks configured\n");
545 } else {
546 while (argc) {
547 cp = *argv++; --argc;
548 if ((ccd = resolve_ccdname(cp)) == NULL) {
549 warnx("invalid ccd name: %s", cp);
550 continue;
551 }
552 if ((error = pathtounit(ccd, &i)) != 0) {
553 warnx("%s: %s", ccd, strerror(error));
554 continue;
555 }
556 if (i >= numccd) {
557 warnx("ccd%d not configured", i);
558 continue;
559 }
560 if (cs[i].sc_flags & CCDF_INITED)
561 print_ccd_info(&cs[i], kd);
562 else
563 printf("ccd%d not configured\n", i);
564 }
565 }
566
567 free(cs);
568
569 done:
570 (void)kvm_close(kd);
571 return (0);
572
573 bad:
574 (void)kvm_close(kd);
575 return (1);
576 }
577
578 static void
579 print_ccd_info(cs, kd)
580 struct ccd_softc *cs;
581 kvm_t *kd;
582 {
583 static int header_printed = 0;
584 struct ccdcinfo *cip;
585 size_t readsize;
586 char path[MAXPATHLEN];
587 int i;
588
589 if (header_printed == 0 && verbose) {
590 printf("# ccd\t\tileave\tflags\tcompnent devices\n");
591 header_printed = 1;
592 }
593
594 readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
595 if ((cip = malloc(readsize)) == NULL) {
596 warn("ccd%d: can't allocate memory for component info",
597 cs->sc_unit);
598 return;
599 }
600 bzero(cip, readsize);
601
602 /* Dump out softc information. */
603 printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
604 cs->sc_cflags & CCDF_USERMASK);
605 fflush(stdout);
606
607 /* Read in the component info. */
608 if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
609 readsize) != readsize) {
610 printf("\n");
611 warnx("can't read component info");
612 warnx(kvm_geterr(kd));
613 goto done;
614 }
615
616 /* Read component pathname and display component info. */
617 for (i = 0; i < cs->sc_nccdisks; ++i) {
618 if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
619 cip[i].ci_pathlen) != cip[i].ci_pathlen) {
620 printf("\n");
621 warnx("can't read component pathname");
622 warnx(kvm_geterr(kd));
623 goto done;
624 }
625 printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
626 fflush(stdout);
627 }
628
629 done:
630 free(cip);
631 }
632
633 static int
634 flags_to_val(flags)
635 char *flags;
636 {
637 char *cp, *tok;
638 int i, tmp, val = ~CCDF_USERMASK;
639 size_t flagslen;
640
641 /*
642 * The most common case is that of NIL flags, so check for
643 * those first.
644 */
645 if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
646 strcmp("0", flags) == 0)
647 return (0);
648
649 flagslen = strlen(flags);
650
651 /* Check for values represented by strings. */
652 if ((cp = strdup(flags)) == NULL)
653 err(1, "no memory to parse flags");
654 tmp = 0;
655 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
656 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
657 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
658 break;
659 if (flagvaltab[i].fv_flag == NULL) {
660 free(cp);
661 goto bad_string;
662 }
663 tmp |= flagvaltab[i].fv_val;
664 }
665
666 /* If we get here, the string was ok. */
667 free(cp);
668 val = tmp;
669 goto out;
670
671 bad_string:
672
673 /* Check for values represented in hex. */
674 if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
675 errno = 0; /* to check for ERANGE */
676 val = (int)strtol(&flags[2], &cp, 16);
677 if ((errno == ERANGE) || (*cp != '\0'))
678 return (-1);
679 goto out;
680 }
681
682 /* Check for values represented in decimal. */
683 errno = 0; /* to check for ERANGE */
684 val = (int)strtol(flags, &cp, 10);
685 if ((errno == ERANGE) || (*cp != '\0'))
686 return (-1);
687
688 out:
689 return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
690 }
691
692 static void
693 usage()
694 {
695
696 fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] %s\n", __progname,
697 "dev [...]");
698 fprintf(stderr, " %s -C [-v] [-f config_file]\n", __progname);
699 fprintf(stderr, " %s -u [-v] ccd [...]\n", __progname);
700 fprintf(stderr, " %s -U [-v] [-f config_file]\n", __progname);
701 fprintf(stderr, " %s -g [-M core] [-N system] %s\n", __progname,
702 "[ccd [...]]");
703 exit(1);
704 }
705