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