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