ccdconfig.c revision 1.20 1 1.20 mrg /* $NetBSD: ccdconfig.c,v 1.20 1998/07/06 07:50:19 mrg Exp $ */
2 1.1 thorpej
3 1.5 thorpej /*-
4 1.8 thorpej * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.5 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.5 thorpej * by Jason R. Thorpe.
9 1.5 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.5 thorpej * This product includes software developed by the NetBSD
21 1.5 thorpej * Foundation, Inc. and its contributors.
22 1.5 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.5 thorpej * contributors may be used to endorse or promote products derived
24 1.5 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.5 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.5 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.5 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.13 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.13 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.5 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.5 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.5 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.5 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.5 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.5 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.10 thorpej #include <sys/cdefs.h>
40 1.10 thorpej #ifndef lint
41 1.10 thorpej __COPYRIGHT(
42 1.12 thorpej "@(#) Copyright (c) 1996, 1997\
43 1.12 thorpej The NetBSD Foundation, Inc. All rights reserved.");
44 1.20 mrg __RCSID("$NetBSD: ccdconfig.c,v 1.20 1998/07/06 07:50:19 mrg Exp $");
45 1.10 thorpej #endif
46 1.10 thorpej
47 1.1 thorpej #include <sys/param.h>
48 1.1 thorpej #include <sys/ioctl.h>
49 1.1 thorpej #include <sys/disklabel.h>
50 1.1 thorpej #include <sys/device.h>
51 1.1 thorpej #include <sys/disk.h>
52 1.1 thorpej #include <sys/stat.h>
53 1.1 thorpej #include <sys/sysctl.h>
54 1.1 thorpej #include <ctype.h>
55 1.1 thorpej #include <err.h>
56 1.1 thorpej #include <errno.h>
57 1.1 thorpej #include <fcntl.h>
58 1.1 thorpej #include <kvm.h>
59 1.1 thorpej #include <limits.h>
60 1.1 thorpej #include <nlist.h>
61 1.1 thorpej #include <stdio.h>
62 1.1 thorpej #include <stdlib.h>
63 1.1 thorpej #include <string.h>
64 1.1 thorpej #include <unistd.h>
65 1.6 thorpej #include <util.h>
66 1.1 thorpej
67 1.1 thorpej #include <dev/ccdvar.h>
68 1.1 thorpej
69 1.1 thorpej #include "pathnames.h"
70 1.1 thorpej
71 1.1 thorpej extern char *__progname;
72 1.1 thorpej
73 1.20 mrg
74 1.16 thorpej static size_t lineno;
75 1.20 mrg static gid_t egid;
76 1.16 thorpej static int verbose;
77 1.1 thorpej static char *ccdconf = _PATH_CCDCONF;
78 1.1 thorpej
79 1.16 thorpej static char *core;
80 1.16 thorpej static char *kernel;
81 1.1 thorpej
82 1.1 thorpej struct flagval {
83 1.1 thorpej char *fv_flag;
84 1.1 thorpej int fv_val;
85 1.1 thorpej } flagvaltab[] = {
86 1.1 thorpej { "CCDF_SWAP", CCDF_SWAP },
87 1.1 thorpej { "CCDF_UNIFORM", CCDF_UNIFORM },
88 1.4 thorpej { "CCDF_MIRROR", CCDF_MIRROR },
89 1.1 thorpej { NULL, 0 },
90 1.1 thorpej };
91 1.1 thorpej
92 1.1 thorpej static struct nlist nl[] = {
93 1.1 thorpej { "_ccd_softc" },
94 1.1 thorpej #define SYM_CCDSOFTC 0
95 1.1 thorpej { "_numccd" },
96 1.1 thorpej #define SYM_NUMCCD 1
97 1.1 thorpej { NULL },
98 1.1 thorpej };
99 1.1 thorpej
100 1.1 thorpej #define CCD_CONFIG 0 /* configure a device */
101 1.1 thorpej #define CCD_CONFIGALL 1 /* configure all devices */
102 1.1 thorpej #define CCD_UNCONFIG 2 /* unconfigure a device */
103 1.1 thorpej #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
104 1.1 thorpej #define CCD_DUMP 4 /* dump a ccd's configuration */
105 1.8 thorpej #define CCD_STATS 5 /* show a ccd's stats */
106 1.1 thorpej
107 1.10 thorpej int main __P((int, char *[]));
108 1.1 thorpej static int checkdev __P((char *));
109 1.1 thorpej static int do_io __P((char *, u_long, struct ccd_ioctl *));
110 1.1 thorpej static int do_single __P((int, char **, int));
111 1.1 thorpej static int do_all __P((int));
112 1.8 thorpej static int dump_ccd __P((int, char **, int));
113 1.1 thorpej static int flags_to_val __P((char *));
114 1.10 thorpej static int pathtounit __P((char *, int *));
115 1.1 thorpej static void print_ccd_info __P((struct ccd_softc *, kvm_t *));
116 1.8 thorpej static void print_ccd_stats __P((struct ccd_softc *));
117 1.1 thorpej static char *resolve_ccdname __P((char *));
118 1.1 thorpej static void usage __P((void));
119 1.1 thorpej
120 1.1 thorpej int
121 1.1 thorpej main(argc, argv)
122 1.1 thorpej int argc;
123 1.10 thorpej char *argv[];
124 1.1 thorpej {
125 1.1 thorpej int ch, options = 0, action = CCD_CONFIG;
126 1.1 thorpej
127 1.20 mrg egid = getegid();
128 1.20 mrg setegid(getgid());
129 1.8 thorpej while ((ch = getopt(argc, argv, "cCf:gM:N:suUv")) != -1) {
130 1.1 thorpej switch (ch) {
131 1.1 thorpej case 'c':
132 1.1 thorpej action = CCD_CONFIG;
133 1.1 thorpej ++options;
134 1.1 thorpej break;
135 1.1 thorpej
136 1.1 thorpej case 'C':
137 1.1 thorpej action = CCD_CONFIGALL;
138 1.1 thorpej ++options;
139 1.1 thorpej break;
140 1.1 thorpej
141 1.1 thorpej case 'f':
142 1.1 thorpej ccdconf = optarg;
143 1.1 thorpej break;
144 1.1 thorpej
145 1.1 thorpej case 'g':
146 1.1 thorpej action = CCD_DUMP;
147 1.1 thorpej break;
148 1.1 thorpej
149 1.1 thorpej case 'M':
150 1.1 thorpej core = optarg;
151 1.1 thorpej break;
152 1.1 thorpej
153 1.1 thorpej case 'N':
154 1.3 thorpej kernel = optarg;
155 1.1 thorpej break;
156 1.1 thorpej
157 1.8 thorpej case 's':
158 1.8 thorpej action = CCD_STATS;
159 1.8 thorpej break;
160 1.8 thorpej
161 1.1 thorpej case 'u':
162 1.1 thorpej action = CCD_UNCONFIG;
163 1.1 thorpej ++options;
164 1.1 thorpej break;
165 1.1 thorpej
166 1.1 thorpej case 'U':
167 1.1 thorpej action = CCD_UNCONFIGALL;
168 1.1 thorpej ++options;
169 1.1 thorpej break;
170 1.1 thorpej
171 1.1 thorpej case 'v':
172 1.1 thorpej verbose = 1;
173 1.1 thorpej break;
174 1.1 thorpej
175 1.1 thorpej default:
176 1.1 thorpej usage();
177 1.1 thorpej }
178 1.1 thorpej }
179 1.1 thorpej argc -= optind;
180 1.1 thorpej argv += optind;
181 1.1 thorpej
182 1.1 thorpej if (options > 1)
183 1.1 thorpej usage();
184 1.7 thorpej
185 1.7 thorpej /*
186 1.20 mrg * Discard setgid privileges. If not the running kernel, we toss
187 1.20 mrg * them away totally so that bad guys can't print interesting stuff
188 1.20 mrg * from kernel memory, otherwise switch back to kmem for the
189 1.20 mrg * duration of the kvm_openfiles() call.
190 1.20 mrg *
191 1.20 mrg * We also do this if we aren't just looking...
192 1.7 thorpej */
193 1.20 mrg if (core != NULL || kernel != NULL ||
194 1.20 mrg (action != CCD_DUMP && action != CCD_STATS))
195 1.7 thorpej setgid(getgid());
196 1.1 thorpej
197 1.1 thorpej switch (action) {
198 1.1 thorpej case CCD_CONFIG:
199 1.1 thorpej case CCD_UNCONFIG:
200 1.1 thorpej exit(do_single(argc, argv, action));
201 1.1 thorpej /* NOTREACHED */
202 1.1 thorpej
203 1.1 thorpej case CCD_CONFIGALL:
204 1.1 thorpej case CCD_UNCONFIGALL:
205 1.1 thorpej exit(do_all(action));
206 1.1 thorpej /* NOTREACHED */
207 1.1 thorpej
208 1.1 thorpej case CCD_DUMP:
209 1.8 thorpej case CCD_STATS:
210 1.10 thorpej default:
211 1.8 thorpej exit(dump_ccd(argc, argv, action));
212 1.1 thorpej /* NOTREACHED */
213 1.1 thorpej }
214 1.1 thorpej /* NOTREACHED */
215 1.1 thorpej }
216 1.1 thorpej
217 1.1 thorpej static int
218 1.1 thorpej do_single(argc, argv, action)
219 1.1 thorpej int argc;
220 1.1 thorpej char **argv;
221 1.1 thorpej int action;
222 1.1 thorpej {
223 1.1 thorpej struct ccd_ioctl ccio;
224 1.1 thorpej char *ccd, *cp, *cp2, **disks;
225 1.10 thorpej int noflags = 0, i, ileave, flags, j;
226 1.1 thorpej
227 1.10 thorpej flags = 0;
228 1.14 lukem memset(&ccio, 0, sizeof(ccio));
229 1.1 thorpej
230 1.1 thorpej /*
231 1.1 thorpej * If unconfiguring, all arguments are treated as ccds.
232 1.1 thorpej */
233 1.1 thorpej if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
234 1.1 thorpej for (i = 0; argc != 0; ) {
235 1.1 thorpej cp = *argv++; --argc;
236 1.1 thorpej if ((ccd = resolve_ccdname(cp)) == NULL) {
237 1.1 thorpej warnx("invalid ccd name: %s", cp);
238 1.1 thorpej i = 1;
239 1.1 thorpej continue;
240 1.1 thorpej }
241 1.1 thorpej if (do_io(ccd, CCDIOCCLR, &ccio))
242 1.1 thorpej i = 1;
243 1.1 thorpej else
244 1.1 thorpej if (verbose)
245 1.2 thorpej printf("%s unconfigured\n", cp);
246 1.1 thorpej }
247 1.1 thorpej return (i);
248 1.1 thorpej }
249 1.1 thorpej
250 1.1 thorpej /* Make sure there are enough arguments. */
251 1.1 thorpej if (argc < 4)
252 1.1 thorpej if (argc == 3) {
253 1.1 thorpej /* Assume that no flags are specified. */
254 1.1 thorpej noflags = 1;
255 1.1 thorpej } else {
256 1.1 thorpej if (action == CCD_CONFIGALL) {
257 1.16 thorpej warnx("%s: bad line: %lu", ccdconf,
258 1.16 thorpej (u_long)lineno);
259 1.1 thorpej return (1);
260 1.1 thorpej } else
261 1.1 thorpej usage();
262 1.1 thorpej }
263 1.1 thorpej
264 1.1 thorpej /* First argument is the ccd to configure. */
265 1.1 thorpej cp = *argv++; --argc;
266 1.1 thorpej if ((ccd = resolve_ccdname(cp)) == NULL) {
267 1.1 thorpej warnx("invalid ccd name: %s", cp);
268 1.1 thorpej return (1);
269 1.1 thorpej }
270 1.1 thorpej
271 1.1 thorpej /* Next argument is the interleave factor. */
272 1.1 thorpej cp = *argv++; --argc;
273 1.1 thorpej errno = 0; /* to check for ERANGE */
274 1.1 thorpej ileave = (int)strtol(cp, &cp2, 10);
275 1.1 thorpej if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
276 1.1 thorpej warnx("invalid interleave factor: %s", cp);
277 1.1 thorpej return (1);
278 1.1 thorpej }
279 1.1 thorpej
280 1.1 thorpej if (noflags == 0) {
281 1.1 thorpej /* Next argument is the ccd configuration flags. */
282 1.1 thorpej cp = *argv++; --argc;
283 1.1 thorpej if ((flags = flags_to_val(cp)) < 0) {
284 1.1 thorpej warnx("invalid flags argument: %s", cp);
285 1.1 thorpej return (1);
286 1.1 thorpej }
287 1.1 thorpej }
288 1.1 thorpej
289 1.1 thorpej /* Next is the list of disks to make the ccd from. */
290 1.1 thorpej disks = malloc(argc * sizeof(char *));
291 1.1 thorpej if (disks == NULL) {
292 1.1 thorpej warnx("no memory to configure ccd");
293 1.1 thorpej return (1);
294 1.1 thorpej }
295 1.1 thorpej for (i = 0; argc != 0; ) {
296 1.1 thorpej cp = *argv++; --argc;
297 1.1 thorpej if ((j = checkdev(cp)) == 0)
298 1.1 thorpej disks[i++] = cp;
299 1.1 thorpej else {
300 1.1 thorpej warnx("%s: %s", cp, strerror(j));
301 1.1 thorpej return (1);
302 1.1 thorpej }
303 1.1 thorpej }
304 1.1 thorpej
305 1.1 thorpej /* Fill in the ccio. */
306 1.1 thorpej ccio.ccio_disks = disks;
307 1.1 thorpej ccio.ccio_ndisks = i;
308 1.1 thorpej ccio.ccio_ileave = ileave;
309 1.1 thorpej ccio.ccio_flags = flags;
310 1.1 thorpej
311 1.1 thorpej if (do_io(ccd, CCDIOCSET, &ccio)) {
312 1.1 thorpej free(disks);
313 1.1 thorpej return (1);
314 1.1 thorpej }
315 1.1 thorpej
316 1.1 thorpej if (verbose) {
317 1.1 thorpej printf("ccd%d: %d components ", ccio.ccio_unit,
318 1.1 thorpej ccio.ccio_ndisks);
319 1.1 thorpej for (i = 0; i < ccio.ccio_ndisks; ++i) {
320 1.1 thorpej if ((cp2 = strrchr(disks[i], '/')) != NULL)
321 1.1 thorpej ++cp2;
322 1.1 thorpej else
323 1.1 thorpej cp2 = disks[i];
324 1.1 thorpej printf("%c%s%c",
325 1.1 thorpej i == 0 ? '(' : ' ', cp2,
326 1.1 thorpej i == ccio.ccio_ndisks - 1 ? ')' : ',');
327 1.1 thorpej }
328 1.11 thorpej printf(", %ld blocks ", (long)ccio.ccio_size);
329 1.1 thorpej if (ccio.ccio_ileave != 0)
330 1.1 thorpej printf("interleaved at %d blocks\n", ccio.ccio_ileave);
331 1.1 thorpej else
332 1.1 thorpej printf("concatenated\n");
333 1.1 thorpej }
334 1.1 thorpej
335 1.1 thorpej free(disks);
336 1.1 thorpej return (0);
337 1.1 thorpej }
338 1.1 thorpej
339 1.1 thorpej static int
340 1.1 thorpej do_all(action)
341 1.1 thorpej int action;
342 1.1 thorpej {
343 1.1 thorpej FILE *f;
344 1.15 lukem char *line, *cp, *vp, **argv;
345 1.16 thorpej int argc, rval;
346 1.16 thorpej size_t len;
347 1.1 thorpej
348 1.10 thorpej rval = 0;
349 1.10 thorpej
350 1.17 mrg (void)setegid(getgid());
351 1.1 thorpej if ((f = fopen(ccdconf, "r")) == NULL) {
352 1.17 mrg (void)setegid(egid);
353 1.1 thorpej warn("fopen: %s", ccdconf);
354 1.1 thorpej return (1);
355 1.1 thorpej }
356 1.17 mrg (void)setegid(egid);
357 1.1 thorpej
358 1.15 lukem while ((line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL))
359 1.15 lukem != NULL) {
360 1.1 thorpej argc = 0;
361 1.1 thorpej argv = NULL;
362 1.15 lukem if (len == 0)
363 1.15 lukem goto end_of_line;
364 1.15 lukem
365 1.15 lukem for (cp = line; cp != NULL; ) {
366 1.15 lukem while ((vp = strsep(&cp, "\t ")) != NULL && *vp == '\0')
367 1.15 lukem ;
368 1.15 lukem if (vp == NULL)
369 1.15 lukem continue;
370 1.1 thorpej
371 1.1 thorpej if ((argv = realloc(argv,
372 1.1 thorpej sizeof(char *) * ++argc)) == NULL) {
373 1.1 thorpej warnx("no memory to configure ccds");
374 1.1 thorpej return (1);
375 1.1 thorpej }
376 1.15 lukem argv[argc - 1] = vp;
377 1.15 lukem
378 1.1 thorpej /*
379 1.1 thorpej * If our action is to unconfigure all, then pass
380 1.1 thorpej * just the first token to do_single() and ignore
381 1.1 thorpej * the rest. Since this will be encountered on
382 1.1 thorpej * our first pass through the line, the Right
383 1.1 thorpej * Thing will happen.
384 1.1 thorpej */
385 1.1 thorpej if (action == CCD_UNCONFIGALL) {
386 1.1 thorpej if (do_single(argc, argv, action))
387 1.1 thorpej rval = 1;
388 1.1 thorpej goto end_of_line;
389 1.1 thorpej }
390 1.1 thorpej }
391 1.1 thorpej if (argc != 0)
392 1.1 thorpej if (do_single(argc, argv, action))
393 1.1 thorpej rval = 1;
394 1.1 thorpej
395 1.1 thorpej end_of_line:
396 1.1 thorpej if (argv != NULL)
397 1.1 thorpej free(argv);
398 1.15 lukem free(line);
399 1.1 thorpej }
400 1.1 thorpej
401 1.1 thorpej (void)fclose(f);
402 1.1 thorpej return (rval);
403 1.1 thorpej }
404 1.1 thorpej
405 1.1 thorpej static int
406 1.1 thorpej checkdev(path)
407 1.1 thorpej char *path;
408 1.1 thorpej {
409 1.1 thorpej struct stat st;
410 1.1 thorpej
411 1.1 thorpej if (stat(path, &st) != 0)
412 1.1 thorpej return (errno);
413 1.1 thorpej
414 1.1 thorpej if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
415 1.1 thorpej return (EINVAL);
416 1.1 thorpej
417 1.1 thorpej return (0);
418 1.1 thorpej }
419 1.1 thorpej
420 1.1 thorpej static int
421 1.1 thorpej pathtounit(path, unitp)
422 1.1 thorpej char *path;
423 1.1 thorpej int *unitp;
424 1.1 thorpej {
425 1.1 thorpej struct stat st;
426 1.1 thorpej int maxpartitions;
427 1.1 thorpej
428 1.1 thorpej if (stat(path, &st) != 0)
429 1.1 thorpej return (errno);
430 1.1 thorpej
431 1.1 thorpej if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
432 1.1 thorpej return (EINVAL);
433 1.1 thorpej
434 1.1 thorpej if ((maxpartitions = getmaxpartitions()) < 0)
435 1.1 thorpej return (errno);
436 1.1 thorpej
437 1.1 thorpej *unitp = minor(st.st_rdev) / maxpartitions;
438 1.1 thorpej
439 1.1 thorpej return (0);
440 1.1 thorpej }
441 1.1 thorpej
442 1.1 thorpej static char *
443 1.1 thorpej resolve_ccdname(name)
444 1.1 thorpej char *name;
445 1.1 thorpej {
446 1.10 thorpej char c, *path;
447 1.1 thorpej size_t len, newlen;
448 1.1 thorpej int rawpart;
449 1.1 thorpej
450 1.1 thorpej if (name[0] == '/' || name[0] == '.') {
451 1.1 thorpej /* Assume they gave the correct pathname. */
452 1.1 thorpej return (strdup(name));
453 1.1 thorpej }
454 1.1 thorpej
455 1.1 thorpej len = strlen(name);
456 1.1 thorpej c = name[len - 1];
457 1.1 thorpej
458 1.1 thorpej newlen = len + 8;
459 1.1 thorpej if ((path = malloc(newlen)) == NULL)
460 1.1 thorpej return (NULL);
461 1.14 lukem memset(path, 0, newlen);
462 1.1 thorpej
463 1.1 thorpej if (isdigit(c)) {
464 1.1 thorpej if ((rawpart = getrawpartition()) < 0) {
465 1.1 thorpej free(path);
466 1.1 thorpej return (NULL);
467 1.1 thorpej }
468 1.9 mrg (void)snprintf(path, newlen, "/dev/%s%c", name, 'a' + rawpart);
469 1.1 thorpej } else
470 1.9 mrg (void)snprintf(path, newlen, "/dev/%s", name);
471 1.1 thorpej
472 1.1 thorpej return (path);
473 1.1 thorpej }
474 1.1 thorpej
475 1.1 thorpej static int
476 1.1 thorpej do_io(path, cmd, cciop)
477 1.1 thorpej char *path;
478 1.1 thorpej u_long cmd;
479 1.1 thorpej struct ccd_ioctl *cciop;
480 1.1 thorpej {
481 1.1 thorpej int fd;
482 1.1 thorpej char *cp;
483 1.1 thorpej
484 1.1 thorpej if ((fd = open(path, O_RDWR, 0640)) < 0) {
485 1.1 thorpej warn("open: %s", path);
486 1.1 thorpej return (1);
487 1.1 thorpej }
488 1.1 thorpej
489 1.1 thorpej if (ioctl(fd, cmd, cciop) < 0) {
490 1.1 thorpej switch (cmd) {
491 1.1 thorpej case CCDIOCSET:
492 1.1 thorpej cp = "CCDIOCSET";
493 1.1 thorpej break;
494 1.1 thorpej
495 1.1 thorpej case CCDIOCCLR:
496 1.1 thorpej cp = "CCDIOCCLR";
497 1.1 thorpej break;
498 1.1 thorpej
499 1.1 thorpej default:
500 1.1 thorpej cp = "unknown";
501 1.1 thorpej }
502 1.1 thorpej warn("ioctl (%s): %s", cp, path);
503 1.1 thorpej return (1);
504 1.1 thorpej }
505 1.1 thorpej
506 1.1 thorpej return (0);
507 1.1 thorpej }
508 1.1 thorpej
509 1.1 thorpej #define KVM_ABORT(kd, str) { \
510 1.1 thorpej (void)kvm_close((kd)); \
511 1.1 thorpej warnx((str)); \
512 1.1 thorpej warnx(kvm_geterr((kd))); \
513 1.1 thorpej return (1); \
514 1.1 thorpej }
515 1.1 thorpej
516 1.1 thorpej static int
517 1.8 thorpej dump_ccd(argc, argv, action)
518 1.1 thorpej int argc;
519 1.1 thorpej char **argv;
520 1.8 thorpej int action;
521 1.1 thorpej {
522 1.1 thorpej char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
523 1.1 thorpej struct ccd_softc *cs, *kcs;
524 1.1 thorpej size_t readsize;
525 1.1 thorpej int i, error, numccd, numconfiged = 0;
526 1.1 thorpej kvm_t *kd;
527 1.1 thorpej
528 1.14 lukem memset(errbuf, 0, sizeof(errbuf));
529 1.1 thorpej
530 1.20 mrg (void)setegid(egid);
531 1.1 thorpej if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
532 1.1 thorpej errbuf)) == NULL) {
533 1.1 thorpej warnx("can't open kvm: %s", errbuf);
534 1.1 thorpej return (1);
535 1.1 thorpej }
536 1.20 mrg (void)setgid(getgid());
537 1.1 thorpej
538 1.1 thorpej if (kvm_nlist(kd, nl))
539 1.1 thorpej KVM_ABORT(kd, "ccd-related symbols not available");
540 1.1 thorpej
541 1.1 thorpej /* Check to see how many ccds are currently configured. */
542 1.1 thorpej if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
543 1.1 thorpej sizeof(numccd)) != sizeof(numccd))
544 1.1 thorpej KVM_ABORT(kd, "can't determine number of configured ccds");
545 1.1 thorpej
546 1.1 thorpej if (numccd == 0) {
547 1.1 thorpej printf("ccd driver in kernel, but is uninitialized\n");
548 1.1 thorpej goto done;
549 1.1 thorpej }
550 1.1 thorpej
551 1.1 thorpej /* Allocate space for the configuration data. */
552 1.1 thorpej readsize = numccd * sizeof(struct ccd_softc);
553 1.1 thorpej if ((cs = malloc(readsize)) == NULL) {
554 1.1 thorpej warnx("no memory for configuration data");
555 1.1 thorpej goto bad;
556 1.1 thorpej }
557 1.14 lukem memset(cs, 0, readsize);
558 1.1 thorpej
559 1.1 thorpej /*
560 1.1 thorpej * Read the ccd configuration data from the kernel and dump
561 1.1 thorpej * it to stdout.
562 1.1 thorpej */
563 1.1 thorpej if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
564 1.1 thorpej sizeof(kcs)) != sizeof(kcs)) {
565 1.1 thorpej free(cs);
566 1.1 thorpej KVM_ABORT(kd, "can't find pointer to configuration data");
567 1.1 thorpej }
568 1.1 thorpej if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
569 1.1 thorpej free(cs);
570 1.1 thorpej KVM_ABORT(kd, "can't read configuration data");
571 1.1 thorpej }
572 1.1 thorpej
573 1.1 thorpej if (argc == 0) {
574 1.1 thorpej for (i = 0; i < numccd; ++i)
575 1.1 thorpej if (cs[i].sc_flags & CCDF_INITED) {
576 1.1 thorpej ++numconfiged;
577 1.8 thorpej if (action == CCD_STATS)
578 1.8 thorpej print_ccd_stats(&cs[i]);
579 1.8 thorpej else
580 1.8 thorpej print_ccd_info(&cs[i], kd);
581 1.1 thorpej }
582 1.1 thorpej
583 1.1 thorpej if (numconfiged == 0)
584 1.18 mycroft printf("# no concatenated disks configured\n");
585 1.1 thorpej } else {
586 1.1 thorpej while (argc) {
587 1.1 thorpej cp = *argv++; --argc;
588 1.1 thorpej if ((ccd = resolve_ccdname(cp)) == NULL) {
589 1.1 thorpej warnx("invalid ccd name: %s", cp);
590 1.1 thorpej continue;
591 1.1 thorpej }
592 1.1 thorpej if ((error = pathtounit(ccd, &i)) != 0) {
593 1.19 mrg warn("%s", ccd);
594 1.1 thorpej continue;
595 1.1 thorpej }
596 1.1 thorpej if (i >= numccd) {
597 1.1 thorpej warnx("ccd%d not configured", i);
598 1.1 thorpej continue;
599 1.1 thorpej }
600 1.8 thorpej if (cs[i].sc_flags & CCDF_INITED) {
601 1.8 thorpej if (action == CCD_STATS)
602 1.8 thorpej print_ccd_stats(&cs[i]);
603 1.8 thorpej else
604 1.8 thorpej print_ccd_info(&cs[i], kd);
605 1.8 thorpej } else
606 1.18 mycroft printf("# ccd%d not configured\n", i);
607 1.1 thorpej }
608 1.1 thorpej }
609 1.1 thorpej
610 1.1 thorpej free(cs);
611 1.1 thorpej
612 1.1 thorpej done:
613 1.1 thorpej (void)kvm_close(kd);
614 1.1 thorpej return (0);
615 1.1 thorpej
616 1.1 thorpej bad:
617 1.1 thorpej (void)kvm_close(kd);
618 1.1 thorpej return (1);
619 1.1 thorpej }
620 1.1 thorpej
621 1.1 thorpej static void
622 1.1 thorpej print_ccd_info(cs, kd)
623 1.1 thorpej struct ccd_softc *cs;
624 1.1 thorpej kvm_t *kd;
625 1.1 thorpej {
626 1.1 thorpej static int header_printed = 0;
627 1.1 thorpej struct ccdcinfo *cip;
628 1.1 thorpej size_t readsize;
629 1.1 thorpej char path[MAXPATHLEN];
630 1.1 thorpej int i;
631 1.1 thorpej
632 1.1 thorpej if (header_printed == 0 && verbose) {
633 1.1 thorpej printf("# ccd\t\tileave\tflags\tcompnent devices\n");
634 1.1 thorpej header_printed = 1;
635 1.1 thorpej }
636 1.1 thorpej
637 1.1 thorpej readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
638 1.1 thorpej if ((cip = malloc(readsize)) == NULL) {
639 1.1 thorpej warn("ccd%d: can't allocate memory for component info",
640 1.1 thorpej cs->sc_unit);
641 1.1 thorpej return;
642 1.1 thorpej }
643 1.14 lukem memset(cip, 0, readsize);
644 1.1 thorpej
645 1.1 thorpej /* Dump out softc information. */
646 1.1 thorpej printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
647 1.1 thorpej cs->sc_cflags & CCDF_USERMASK);
648 1.1 thorpej fflush(stdout);
649 1.1 thorpej
650 1.1 thorpej /* Read in the component info. */
651 1.1 thorpej if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
652 1.1 thorpej readsize) != readsize) {
653 1.1 thorpej printf("\n");
654 1.1 thorpej warnx("can't read component info");
655 1.1 thorpej warnx(kvm_geterr(kd));
656 1.1 thorpej goto done;
657 1.1 thorpej }
658 1.1 thorpej
659 1.1 thorpej /* Read component pathname and display component info. */
660 1.1 thorpej for (i = 0; i < cs->sc_nccdisks; ++i) {
661 1.1 thorpej if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
662 1.1 thorpej cip[i].ci_pathlen) != cip[i].ci_pathlen) {
663 1.1 thorpej printf("\n");
664 1.1 thorpej warnx("can't read component pathname");
665 1.1 thorpej warnx(kvm_geterr(kd));
666 1.1 thorpej goto done;
667 1.1 thorpej }
668 1.1 thorpej printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
669 1.1 thorpej fflush(stdout);
670 1.1 thorpej }
671 1.1 thorpej
672 1.1 thorpej done:
673 1.1 thorpej free(cip);
674 1.1 thorpej }
675 1.1 thorpej
676 1.8 thorpej static void
677 1.8 thorpej print_ccd_stats(cs)
678 1.8 thorpej struct ccd_softc *cs;
679 1.8 thorpej {
680 1.8 thorpej
681 1.8 thorpej printf("Statistics for %s:\n", cs->sc_xname);
682 1.8 thorpej printf("\tfreelist count: %d\n", cs->sc_freecount);
683 1.8 thorpej printf("\tfreelist hiwater: %d\n", cs->sc_hiwat);
684 1.8 thorpej printf("\tfreelist misses: %lu\n", cs->sc_nmisses);
685 1.8 thorpej printf("\tccdbuf allocations: %lu\n", cs->sc_ngetbuf);
686 1.8 thorpej }
687 1.8 thorpej
688 1.1 thorpej static int
689 1.1 thorpej flags_to_val(flags)
690 1.1 thorpej char *flags;
691 1.1 thorpej {
692 1.1 thorpej char *cp, *tok;
693 1.1 thorpej int i, tmp, val = ~CCDF_USERMASK;
694 1.1 thorpej size_t flagslen;
695 1.1 thorpej
696 1.1 thorpej /*
697 1.1 thorpej * The most common case is that of NIL flags, so check for
698 1.1 thorpej * those first.
699 1.1 thorpej */
700 1.1 thorpej if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
701 1.1 thorpej strcmp("0", flags) == 0)
702 1.1 thorpej return (0);
703 1.1 thorpej
704 1.1 thorpej flagslen = strlen(flags);
705 1.1 thorpej
706 1.1 thorpej /* Check for values represented by strings. */
707 1.1 thorpej if ((cp = strdup(flags)) == NULL)
708 1.1 thorpej err(1, "no memory to parse flags");
709 1.1 thorpej tmp = 0;
710 1.1 thorpej for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
711 1.1 thorpej for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
712 1.1 thorpej if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
713 1.1 thorpej break;
714 1.1 thorpej if (flagvaltab[i].fv_flag == NULL) {
715 1.1 thorpej free(cp);
716 1.1 thorpej goto bad_string;
717 1.1 thorpej }
718 1.1 thorpej tmp |= flagvaltab[i].fv_val;
719 1.1 thorpej }
720 1.1 thorpej
721 1.1 thorpej /* If we get here, the string was ok. */
722 1.1 thorpej free(cp);
723 1.1 thorpej val = tmp;
724 1.1 thorpej goto out;
725 1.1 thorpej
726 1.1 thorpej bad_string:
727 1.1 thorpej
728 1.1 thorpej /* Check for values represented in hex. */
729 1.1 thorpej if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
730 1.1 thorpej errno = 0; /* to check for ERANGE */
731 1.1 thorpej val = (int)strtol(&flags[2], &cp, 16);
732 1.1 thorpej if ((errno == ERANGE) || (*cp != '\0'))
733 1.1 thorpej return (-1);
734 1.1 thorpej goto out;
735 1.1 thorpej }
736 1.1 thorpej
737 1.1 thorpej /* Check for values represented in decimal. */
738 1.1 thorpej errno = 0; /* to check for ERANGE */
739 1.1 thorpej val = (int)strtol(flags, &cp, 10);
740 1.1 thorpej if ((errno == ERANGE) || (*cp != '\0'))
741 1.1 thorpej return (-1);
742 1.1 thorpej
743 1.1 thorpej out:
744 1.1 thorpej return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
745 1.1 thorpej }
746 1.1 thorpej
747 1.1 thorpej static void
748 1.1 thorpej usage()
749 1.1 thorpej {
750 1.1 thorpej
751 1.1 thorpej fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] %s\n", __progname,
752 1.1 thorpej "dev [...]");
753 1.1 thorpej fprintf(stderr, " %s -C [-v] [-f config_file]\n", __progname);
754 1.1 thorpej fprintf(stderr, " %s -u [-v] ccd [...]\n", __progname);
755 1.1 thorpej fprintf(stderr, " %s -U [-v] [-f config_file]\n", __progname);
756 1.1 thorpej fprintf(stderr, " %s -g [-M core] [-N system] %s\n", __progname,
757 1.8 thorpej "[ccd [...]]");
758 1.8 thorpej fprintf(stderr, " %s -s [-M core] [-N system] %s\n", __progname,
759 1.1 thorpej "[ccd [...]]");
760 1.1 thorpej exit(1);
761 1.1 thorpej }
762