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