]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sbin/ccdconfig/ccdconfig.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sbin / ccdconfig / ccdconfig.c
1 /*
2  * Copyright (c) 2003 Poul-Henning Kamp
3  * Copyright (c) 1995 Jason R. Thorpe.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed for the NetBSD Project
17  *      by Jason R. Thorpe.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <paths.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <libgeom.h>
50
51 #define CCDF_UNIFORM    0x02    /* use LCCD of sizes for uniform interleave */
52 #define CCDF_MIRROR     0x04    /* use mirroring */
53 #define CCDF_NO_OFFSET  0x08    /* do not leave space in front */
54 #define CCDF_LINUX      0x10    /* use Linux compatibility mode */
55
56 #include "pathnames.h"
57
58 static  int lineno = 0;
59 static  int verbose = 0;
60 static  const char *ccdconf = _PATH_CCDCONF;
61
62 struct  flagval {
63         const char      *fv_flag;
64         int             fv_val;
65 } flagvaltab[] = {
66         { "CCDF_UNIFORM",       CCDF_UNIFORM },
67         { "uniform",            CCDF_UNIFORM },
68         { "CCDF_MIRROR",        CCDF_MIRROR },
69         { "mirror",             CCDF_MIRROR },
70         { "CCDF_NO_OFFSET",     CCDF_NO_OFFSET },
71         { "no_offset",          CCDF_NO_OFFSET },
72         { "CCDF_LINUX",         CCDF_LINUX },
73         { "linux",              CCDF_LINUX },
74         { "none",               0 },
75         { NULL,                 0 },
76 };
77
78 #define CCD_CONFIG              0       /* configure a device */
79 #define CCD_CONFIGALL           1       /* configure all devices */
80 #define CCD_UNCONFIG            2       /* unconfigure a device */
81 #define CCD_UNCONFIGALL         3       /* unconfigure all devices */
82 #define CCD_DUMP                4       /* dump a ccd's configuration */
83
84 static  int do_single(int, char **, int);
85 static  int do_all(int);
86 static  int dump_ccd(int, char **);
87 static  int flags_to_val(char *);
88 static  int resolve_ccdname(char *);
89 static  void usage(void);
90
91 int
92 main(int argc, char *argv[])
93 {
94         int ch, options = 0, action = CCD_CONFIG;
95
96         while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
97                 switch (ch) {
98                 case 'c':
99                         action = CCD_CONFIG;
100                         ++options;
101                         break;
102
103                 case 'C':
104                         action = CCD_CONFIGALL;
105                         ++options;
106                         break;
107
108                 case 'f':
109                         ccdconf = optarg;
110                         break;
111
112                 case 'g':
113                         action = CCD_DUMP;
114                         break;
115
116                 case 'u':
117                         action = CCD_UNCONFIG;
118                         ++options;
119                         break;
120
121                 case 'U':
122                         action = CCD_UNCONFIGALL;
123                         ++options;
124                         break;
125
126                 case 'v':
127                         verbose = 1;
128                         break;
129
130                 default:
131                         usage();
132                 }
133         }
134         argc -= optind;
135         argv += optind;
136
137         if (options > 1)
138                 usage();
139
140         if (modfind("g_ccd") < 0) {
141                 /* Not present in kernel, try loading it */
142                 if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
143                         warn("geom_ccd module not available!");
144         }
145
146         switch (action) {
147                 case CCD_CONFIG:
148                 case CCD_UNCONFIG:
149                         exit(do_single(argc, argv, action));
150                         /* NOTREACHED */
151
152                 case CCD_CONFIGALL:
153                 case CCD_UNCONFIGALL:
154                         exit(do_all(action));
155                         /* NOTREACHED */
156
157                 case CCD_DUMP:
158                         exit(dump_ccd(argc, argv));
159                         /* NOTREACHED */
160         }
161         /* NOTREACHED */
162         return (0);
163 }
164
165 static int
166 do_single(int argc, char **argv, int action)
167 {
168         char *cp, *cp2;
169         int ccd, noflags = 0, i, ileave, flags = 0;
170         struct gctl_req *grq;
171         char const *errstr;
172         char buf1[BUFSIZ];
173         int ex;
174
175         /*
176          * If unconfiguring, all arguments are treated as ccds.
177          */
178         if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
179                 ex = 0;
180                 for (; argc != 0;) {
181                         cp = *argv++; --argc;
182                         if ((ccd = resolve_ccdname(cp)) < 0) {
183                                 warnx("invalid ccd name: %s", cp);
184                                 continue;
185                         }
186                         grq = gctl_get_handle();
187                         gctl_ro_param(grq, "verb", -1, "destroy geom");
188                         gctl_ro_param(grq, "class", -1, "CCD");
189                         sprintf(buf1, "ccd%d", ccd);
190                         gctl_ro_param(grq, "geom", -1, buf1);
191                         errstr = gctl_issue(grq);
192                         if (errstr == NULL) {           
193                                 if (verbose)
194                                         printf("%s unconfigured\n", cp);
195                                 gctl_free(grq);
196                                 continue;
197                         }
198                         warnx(
199                             "%s\nor possibly kernel and ccdconfig out of sync",
200                             errstr);
201                         ex = 1;
202                 }
203                 return (ex);
204         }
205
206         /* Make sure there are enough arguments. */
207         if (argc < 4) {
208                 if (argc == 3) {
209                         /* Assume that no flags are specified. */
210                         noflags = 1;
211                 } else {
212                         if (action == CCD_CONFIGALL) {
213                                 warnx("%s: bad line: %d", ccdconf, lineno);
214                                 return (1);
215                         } else
216                                 usage();
217                 }
218         }
219
220         /* First argument is the ccd to configure. */
221         cp = *argv++; --argc;
222         if ((ccd = resolve_ccdname(cp)) < 0) {
223                 warnx("invalid ccd name: %s", cp);
224                 return (1);
225         }
226
227         /* Next argument is the interleave factor. */
228         cp = *argv++; --argc;
229         errno = 0;      /* to check for ERANGE */
230         ileave = (int)strtol(cp, &cp2, 10);
231         if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
232                 warnx("invalid interleave factor: %s", cp);
233                 return (1);
234         }
235
236         if (noflags == 0) {
237                 /* Next argument is the ccd configuration flags. */
238                 cp = *argv++; --argc;
239                 if ((flags = flags_to_val(cp)) < 0) {
240                         warnx("invalid flags argument: %s", cp);
241                         return (1);
242                 }
243         }
244         grq = gctl_get_handle();
245         gctl_ro_param(grq, "verb", -1, "create geom");
246         gctl_ro_param(grq, "class", -1, "CCD");
247         gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
248         gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
249         if (flags & CCDF_UNIFORM)
250                 gctl_ro_param(grq, "uniform", -1, "");
251         if (flags & CCDF_MIRROR)
252                 gctl_ro_param(grq, "mirror", -1, "");
253         if (flags & CCDF_NO_OFFSET)
254                 gctl_ro_param(grq, "no_offset", -1, "");
255         if (flags & CCDF_LINUX)
256                 gctl_ro_param(grq, "linux", -1, "");
257         gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
258         for (i = 0; i < argc; i++) {
259                 sprintf(buf1, "provider%d", i);
260                 cp = argv[i];
261                 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
262                         cp += strlen(_PATH_DEV);
263                 gctl_ro_param(grq, buf1, -1, cp);
264         }
265         gctl_rw_param(grq, "output", sizeof(buf1), buf1);
266         errstr = gctl_issue(grq);
267         if (errstr == NULL) {           
268                 if (verbose) {
269                         printf("%s", buf1);
270                 }
271                 gctl_free(grq);
272                 return (0);
273         }
274         warnx(
275             "%s\nor possibly kernel and ccdconfig out of sync",
276             errstr);
277         return (1);
278 }
279
280 static int
281 do_all(int action)
282 {
283         FILE *f;
284         char line[_POSIX2_LINE_MAX];
285         char *cp, **argv;
286         int argc, rval;
287         gid_t egid;
288
289         rval = 0;
290         egid = getegid();
291         setegid(getgid());
292         if ((f = fopen(ccdconf, "r")) == NULL) {
293                 setegid(egid);
294                 warn("fopen: %s", ccdconf);
295                 return (1);
296         }
297         setegid(egid);
298
299         while (fgets(line, sizeof(line), f) != NULL) {
300                 argc = 0;
301                 argv = NULL;
302                 ++lineno;
303                 if ((cp = strrchr(line, '\n')) != NULL)
304                         *cp = '\0';
305
306                 /* Break up the line and pass it's contents to do_single(). */
307                 if (line[0] == '\0')
308                         goto end_of_line;
309                 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
310                         if (*cp == '#')
311                                 break;
312                         if ((argv = realloc(argv,
313                             sizeof(char *) * ++argc)) == NULL) {
314                                 warnx("no memory to configure ccds");
315                                 return (1);
316                         }
317                         argv[argc - 1] = cp;
318                         /*
319                          * If our action is to unconfigure all, then pass
320                          * just the first token to do_single() and ignore
321                          * the rest.  Since this will be encountered on
322                          * our first pass through the line, the Right
323                          * Thing will happen.
324                          */
325                         if (action == CCD_UNCONFIGALL) {
326                                 if (do_single(argc, argv, action))
327                                         rval = 1;
328                                 goto end_of_line;
329                         }
330                 }
331                 if (argc != 0)
332                         if (do_single(argc, argv, action))
333                                 rval = 1;
334
335  end_of_line:
336                 if (argv != NULL)
337                         free(argv);
338         }
339
340         (void)fclose(f);
341         return (rval);
342 }
343
344 static int
345 resolve_ccdname(char *name)
346 {
347
348         if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
349                 name += strlen(_PATH_DEV);
350         if (strncmp(name, "ccd", 3))
351                 return -1;
352         name += 3;
353         if (!isdigit(*name))
354                 return -1;
355         return (strtoul(name, NULL, 10));
356 }
357
358 static int
359 dumpout(int unit)
360 {
361         static int v;
362         struct gctl_req *grq;
363         int ncp;
364         char *cp;
365         char const *errstr;
366
367         grq = gctl_get_handle();
368         ncp = 65536;
369         cp = malloc(ncp);
370         gctl_ro_param(grq, "verb", -1, "list");
371         gctl_ro_param(grq, "class", -1, "CCD");
372         gctl_ro_param(grq, "unit", sizeof(unit), &unit);
373         gctl_rw_param(grq, "output", ncp, cp);
374         errstr = gctl_issue(grq);
375         if (errstr != NULL)
376                 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
377                         errstr);
378         if (strlen(cp) == 0)
379                 errx(1, "ccd%d not configured", unit);
380         if (verbose && !v) {
381                 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
382                 v = 1;
383         }
384         printf("%s", cp);
385         free(cp);
386         return (0);
387 }
388
389 static int
390 dump_ccd(int argc, char **argv)
391 {
392         int i, error;
393
394         if (argc == 0) {
395                 error = dumpout(-1);
396         } else {
397                 error = 0;
398                 for (i = 0; error == 0 && i < argc; i++)
399                         error = dumpout(resolve_ccdname(argv[i]));
400         }
401         return (error);
402 }
403
404 static int
405 flags_to_val(char *flags)
406 {
407         char *cp, *tok;
408         int i, tmp, val;
409
410         errno = 0;      /* to check for ERANGE */
411         val = (int)strtol(flags, &cp, 0);
412         if ((errno != ERANGE) && (*cp == '\0')) {
413                 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
414                         return (-1);
415                 return (val);
416         }
417
418         /* Check for values represented by strings. */
419         if ((cp = strdup(flags)) == NULL)
420                 err(1, "no memory to parse flags");
421         tmp = 0;
422         for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
423                 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
424                         if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
425                                 break;
426                 if (flagvaltab[i].fv_flag == NULL) {
427                         free(cp);
428                         return (-1);
429                 }
430                 tmp |= flagvaltab[i].fv_val;
431         }
432
433         /* If we get here, the string was ok. */
434         free(cp);
435         return (tmp);
436 }
437
438 static void
439 usage(void)
440 {
441         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
442                 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
443                 "       ccdconfig -C [-v] [-f config_file]",
444                 "       ccdconfig -u [-v] ccd ...",
445                 "       ccdconfig -U [-v] [-f config_file]",
446                 "       ccdconfig -g [ccd ...]");
447         exit(1);
448 }