usched_dummy: Silence warning
[dragonfly.git] / usr.sbin / vnconfig / vnconfig.c
blob7222e0708e04e9becf5d7b318e8d443353ebe5ba
1 /*
2 * Copyright (c) 1993 University of Utah.
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
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 University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
38 * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
40 * @(#)vnconfig.c 8.1 (Berkeley) 12/15/93
41 * $FreeBSD: src/usr.sbin/vnconfig/vnconfig.c,v 1.13.2.7 2003/06/02 09:10:27 maxim Exp $
42 * $DragonFly: src/usr.sbin/vnconfig/vnconfig.c,v 1.15 2008/07/27 22:36:01 thomas Exp $
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <fcntl.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/param.h>
55 #include <sys/ioctl.h>
56 #include <sys/linker.h>
57 #include <sys/mount.h>
58 #include <sys/module.h>
59 #include <sys/stat.h>
60 #include <sys/vnioctl.h>
61 #include <vfs/ufs/ufsmount.h>
63 #define LINESIZE 1024
64 #define ZBUFSIZE 32768
66 struct vndisk {
67 char *dev;
68 char *file;
69 char *autolabel;
70 int flags;
71 int64_t size;
72 char *oarg;
73 } *vndisks;
75 #define VN_CONFIG 0x01
76 #define VN_UNCONFIG 0x02
77 #define VN_ENABLE 0x04
78 #define VN_DISABLE 0x08
79 #define VN_SWAP 0x10
80 #define VN_MOUNTRO 0x20
81 #define VN_MOUNTRW 0x40
82 #define VN_IGNORE 0x80
83 #define VN_SET 0x100
84 #define VN_RESET 0x200
85 #define VN_TRUNCATE 0x400
86 #define VN_ZERO 0x800
88 int nvndisks;
90 int all = 0;
91 int verbose = 0;
92 int global = 0;
93 int listopt = 0;
94 u_long setopt = 0;
95 u_long resetopt = 0;
96 const char *configfile;
98 int config(struct vndisk *);
99 void getoptions(struct vndisk *, const char *);
100 int getinfo(const char *vname);
101 char *rawdevice(const char *);
102 void readconfig(int);
103 static void usage(void);
104 static int64_t getsize(const char *arg);
105 static void do_autolabel(const char *dev, const char *label);
106 int what_opt(const char *, u_long *);
109 main(int argc, char *argv[])
111 int i, rv;
112 int flags = 0;
113 int64_t size = 0;
114 char *autolabel = NULL;
115 char *s;
117 configfile = _PATH_VNTAB;
118 while ((i = getopt(argc, argv, "acdef:glr:s:S:TZL:uv")) != -1)
119 switch (i) {
121 /* all -- use config file */
122 case 'a':
123 all++;
124 break;
126 /* configure */
127 case 'c':
128 flags |= VN_CONFIG;
129 flags &= ~VN_UNCONFIG;
130 break;
132 /* disable */
133 case 'd':
134 flags |= VN_DISABLE;
135 flags &= ~VN_ENABLE;
136 break;
138 /* enable */
139 case 'e':
140 flags |= (VN_ENABLE|VN_CONFIG);
141 flags &= ~(VN_DISABLE|VN_UNCONFIG);
142 break;
144 /* alternate config file */
145 case 'f':
146 configfile = optarg;
147 break;
149 /* fiddle global options */
150 case 'g':
151 global = 1 - global;
152 break;
154 /* reset options */
155 case 'r':
156 for (s = strtok(optarg, ","); s; s = strtok(NULL, ",")) {
157 if (what_opt(s, &resetopt))
158 errx(1, "invalid options '%s'", s);
160 flags |= VN_RESET;
161 break;
163 case 'l':
164 listopt = 1;
165 break;
167 /* set options */
168 case 's':
169 for (s = strtok(optarg, ","); s; s = strtok(NULL, ",")) {
170 if (what_opt(s, &setopt))
171 errx(1, "invalid options '%s'", s);
173 flags |= VN_SET;
174 break;
176 /* unconfigure */
177 case 'u':
178 flags |= (VN_DISABLE|VN_UNCONFIG);
179 flags &= ~(VN_ENABLE|VN_CONFIG);
180 break;
182 /* verbose */
183 case 'v':
184 verbose++;
185 break;
187 case 'S':
188 size = getsize(optarg);
189 flags |= VN_CONFIG;
190 flags &= ~VN_UNCONFIG;
191 break;
193 case 'T':
194 flags |= VN_TRUNCATE;
195 break;
197 case 'Z':
198 flags |= VN_ZERO;
199 break;
201 case 'L':
202 autolabel = optarg;
203 break;
205 default:
206 usage();
209 if (modfind("vn") < 0)
210 if (kldload("vn") < 0 || modfind("vn") < 0)
211 warnx( "cannot find or load \"vn\" kernel module");
213 rv = 0;
214 if (listopt) {
215 if(argc > optind)
216 while(argc > optind)
217 rv += getinfo( argv[optind++]);
218 else {
219 rv = getinfo( NULL );
221 exit(rv);
224 if (flags == 0)
225 flags = VN_CONFIG;
226 if (all) {
227 readconfig(flags);
228 } else {
229 vndisks = calloc(sizeof(struct vndisk), 1);
230 if (argc < optind + 1)
231 usage();
232 vndisks[0].dev = argv[optind++];
233 vndisks[0].file = argv[optind++]; /* may be NULL */
234 vndisks[0].flags = flags;
235 vndisks[0].size = size;
236 vndisks[0].autolabel = autolabel;
237 if (optind < argc)
238 getoptions(&vndisks[0], argv[optind]);
239 nvndisks = 1;
241 rv = 0;
242 for (i = 0; i < nvndisks; i++)
243 rv += config(&vndisks[i]);
244 exit(rv);
248 what_opt(const char *str, u_long *p)
250 if (!strcmp(str,"reserve")) { *p |= VN_RESERVE; return 0; }
251 if (!strcmp(str,"labels")) { *p |= VN_LABELS; return 0; }
252 if (!strcmp(str,"follow")) { *p |= VN_FOLLOW; return 0; }
253 if (!strcmp(str,"debug")) { *p |= VN_DEBUG; return 0; }
254 if (!strcmp(str,"io")) { *p |= VN_IO; return 0; }
255 if (!strcmp(str,"all")) { *p |= ~0; return 0; }
256 if (!strcmp(str,"none")) { *p |= 0; return 0; }
257 return 1;
262 * GETINFO
264 * Print vnode disk information to stdout for the device at
265 * path 'vname', or all existing 'vn' devices if none is given.
266 * Any 'vn' devices must exist under /dev in order to be queried.
268 * Todo: correctly use vm_secsize for swap-backed vn's ..
272 getinfo( const char *vname )
274 int i = 0, vd, printlim = 0;
275 char vnpath[PATH_MAX];
276 const char *tmp;
278 struct vn_user vnu;
279 struct stat sb;
281 if (vname == NULL) {
282 printlim = 1024;
283 } else {
284 tmp = vname;
285 while (*tmp != 0) {
286 if(isdigit(*tmp)){
287 i = atoi(tmp);
288 printlim = i + 1;
289 break;
291 tmp++;
293 if (*tmp == '\0')
294 errx(1, "unknown vn device: %s", vname);
297 snprintf(vnpath, sizeof(vnpath), "/dev/vn%d", i);
299 vd = open(vnpath, O_RDONLY);
300 if (vd < 0)
301 err(1, "open: %s", vnpath);
303 for (; i<printlim; i++) {
305 bzero(&vnpath, sizeof(vnpath));
306 bzero(&sb, sizeof(struct stat));
307 bzero(&vnu, sizeof(struct vn_user));
309 vnu.vnu_unit = i;
311 snprintf(vnpath, sizeof(vnpath), "/dev/vn%d", vnu.vnu_unit);
313 if(stat(vnpath, &sb) < 0) {
314 break;
316 else {
317 if (ioctl(vd, VNIOCGET, &vnu) == -1) {
318 if (errno != ENXIO) {
319 if (*vnu.vnu_file == '\0') {
320 fprintf(stdout,
321 "vn%d: ioctl: can't access regular file\n",
322 vnu.vnu_unit);
323 continue;
325 else {
326 close(vd);
327 err(1, "ioctl: %s", vname);
332 fprintf(stdout, "vn%d: ", vnu.vnu_unit);
334 if (vnu.vnu_file[0] == 0)
335 fprintf(stdout, "not in use\n");
336 else if ((strcmp(vnu.vnu_file, _VN_USER_SWAP)) == 0)
337 fprintf(stdout,
338 "consuming %lld VM pages\n",
339 vnu.vnu_size);
340 else
341 fprintf(stdout,
342 "covering %s on %s, inode %ju\n",
343 vnu.vnu_file,
344 devname(vnu.vnu_dev, S_IFBLK),
345 (uintmax_t)vnu.vnu_ino);
348 close(vd);
349 return 0;
353 config(struct vndisk *vnp)
355 char *dev, *file, *rdev, *oarg;
356 FILE *f;
357 struct vn_ioctl vnio;
358 int flags, pgsize, rv, status;
359 u_long l;
361 pgsize = getpagesize();
363 status = rv = 0;
366 * Prepend "/dev/" to the specified device name, if necessary.
367 * Operate on vnp->dev because it is used later.
369 if (vnp->dev[0] != '/' && vnp->dev[0] != '.')
370 asprintf(&vnp->dev, "%s%s", _PATH_DEV, vnp->dev);
371 dev = vnp->dev;
372 file = vnp->file;
373 flags = vnp->flags;
374 oarg = vnp->oarg;
376 if (flags & VN_IGNORE)
377 return(0);
380 * When a regular file has been specified, do any requested setup
381 * of the file. Truncation (also creates the file if necessary),
382 * sizing, and zeroing.
385 if (file && vnp->size != 0 && (flags & VN_CONFIG)) {
386 int fd;
387 struct stat st;
389 if (flags & VN_TRUNCATE)
390 fd = open(file, O_RDWR|O_CREAT|O_TRUNC, 0600);
391 else
392 fd = open(file, O_RDWR);
393 if (fd >= 0 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
394 if (st.st_size < vnp->size * pgsize)
395 ftruncate(fd, vnp->size * pgsize);
396 if (vnp->size != 0)
397 st.st_size = vnp->size * pgsize;
399 if (flags & VN_ZERO) {
400 char *buf = malloc(ZBUFSIZE);
401 bzero(buf, ZBUFSIZE);
402 while (st.st_size > 0) {
403 int n = (st.st_size > ZBUFSIZE) ?
404 ZBUFSIZE : (int)st.st_size;
405 if (write(fd, buf, n) != n) {
406 ftruncate(fd, 0);
407 printf("Unable to ZERO file %s\n", file);
408 return(0);
410 st.st_size -= (off_t)n;
413 close(fd);
414 } else {
415 printf("Unable to open file %s\n", file);
416 return(0);
418 } else if (file == NULL && vnp->size == 0 && (flags & VN_CONFIG)) {
419 warnx("specify regular filename or swap size");
420 return (0);
423 rdev = rawdevice(dev);
424 f = fopen(rdev, "rw");
425 if (f == NULL) {
426 warn("%s", dev);
427 return(1);
429 vnio.vn_file = file;
430 vnio.vn_size = vnp->size; /* non-zero only if swap backed */
433 * Disable the device
435 if (flags & VN_DISABLE) {
436 if (flags & (VN_MOUNTRO|VN_MOUNTRW)) {
437 rv = unmount(oarg, 0);
438 if (rv) {
439 status--;
440 if (errno == EBUSY)
441 flags &= ~VN_UNCONFIG;
442 if ((flags & VN_UNCONFIG) == 0)
443 warn("umount");
444 } else if (verbose)
445 printf("%s: unmounted\n", dev);
449 * Clear (un-configure) the device
451 if (flags & VN_UNCONFIG) {
452 rv = ioctl(fileno(f), VNIOCDETACH, &vnio);
453 if (rv) {
454 if (errno == ENODEV) {
455 if (verbose)
456 printf("%s: not configured\n", dev);
457 rv = 0;
458 } else {
459 status--;
460 warn("VNIOCDETACH");
462 } else if (verbose)
463 printf("%s: cleared\n", dev);
466 * Set specified options
468 if (flags & VN_SET) {
469 l = setopt;
470 if (global)
471 rv = ioctl(fileno(f), VNIOCGSET, &l);
472 else
473 rv = ioctl(fileno(f), VNIOCUSET, &l);
474 if (rv) {
475 status--;
476 warn("VNIO[GU]SET");
477 } else if (verbose)
478 printf("%s: flags now=%08lx\n",dev,l);
481 * Reset specified options
483 if (flags & VN_RESET) {
484 l = resetopt;
485 if (global)
486 rv = ioctl(fileno(f), VNIOCGCLEAR, &l);
487 else
488 rv = ioctl(fileno(f), VNIOCUCLEAR, &l);
489 if (rv) {
490 status--;
491 warn("VNIO[GU]CLEAR");
492 } else if (verbose)
493 printf("%s: flags now=%08lx\n",dev,l);
496 * Configure the device
498 if (flags & VN_CONFIG) {
499 rv = ioctl(fileno(f), VNIOCATTACH, &vnio);
500 if (rv) {
501 status--;
502 warn("VNIOCATTACH");
503 flags &= ~VN_ENABLE;
504 } else {
505 if (verbose) {
506 printf("%s: %s, ", dev, file);
507 if (vnp->size != 0) {
508 printf("%lld bytes mapped\n", vnio.vn_size);
509 } else {
510 printf("complete file mapped\n");
514 * autolabel
516 if (vnp->autolabel) {
517 do_autolabel(vnp->dev, vnp->autolabel);
522 * Set an option
524 if (flags & VN_SET) {
525 l = setopt;
526 if (global)
527 rv = ioctl(fileno(f), VNIOCGSET, &l);
528 else
529 rv = ioctl(fileno(f), VNIOCUSET, &l);
530 if (rv) {
531 status--;
532 warn("VNIO[GU]SET");
533 } else if (verbose)
534 printf("%s: flags now=%08lx\n",dev,l);
537 * Reset an option
539 if (flags & VN_RESET) {
540 l = resetopt;
541 if (global)
542 rv = ioctl(fileno(f), VNIOCGCLEAR, &l);
543 else
544 rv = ioctl(fileno(f), VNIOCUCLEAR, &l);
545 if (rv) {
546 status--;
547 warn("VNIO[GU]CLEAR");
548 } else if (verbose)
549 printf("%s: flags now=%08lx\n",dev,l);
553 * Close the device now, as we may want to mount it.
555 fclose(f);
558 * Enable special functions on the device
560 if (flags & VN_ENABLE) {
561 if (flags & VN_SWAP) {
562 rv = swapon(dev);
563 if (rv) {
564 status--;
565 warn("swapon");
567 else if (verbose)
568 printf("%s: swapping enabled\n", dev);
570 if (flags & (VN_MOUNTRO|VN_MOUNTRW)) {
571 struct ufs_args args;
572 int mflags;
574 args.fspec = dev;
575 mflags = (flags & VN_MOUNTRO) ? MNT_RDONLY : 0;
576 rv = mount("ufs", oarg, mflags, &args);
577 if (rv) {
578 status--;
579 warn("mount");
581 else if (verbose)
582 printf("%s: mounted on %s\n", dev, oarg);
585 /* done: */
586 fflush(stdout);
587 return(status < 0);
590 #define EOL(c) ((c) == '\0' || (c) == '\n')
591 #define WHITE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
593 void
594 readconfig(int flags)
596 char buf[LINESIZE];
597 FILE *f;
598 char *cp, *sp;
599 int ix;
600 int ax;
602 f = fopen(configfile, "r");
603 if (f == NULL)
604 err(1, "%s", configfile);
605 ix = 0; /* number of elements */
606 ax = 0; /* allocated elements */
607 while (fgets(buf, LINESIZE, f) != NULL) {
608 cp = buf;
609 if (*cp == '#')
610 continue;
611 while (!EOL(*cp) && WHITE(*cp))
612 cp++;
613 if (EOL(*cp))
614 continue;
615 sp = cp;
616 while (!EOL(*cp) && !WHITE(*cp))
617 cp++;
618 if (EOL(*cp))
619 continue;
620 *cp++ = '\0';
622 if (ix == ax) {
623 ax = ax + 16;
624 vndisks = realloc(vndisks, ax * sizeof(struct vndisk));
625 bzero(&vndisks[ix], (ax - ix) * sizeof(struct vndisk));
627 vndisks[ix].dev = malloc(cp - sp);
628 strcpy(vndisks[ix].dev, sp);
629 while (!EOL(*cp) && WHITE(*cp))
630 cp++;
631 if (EOL(*cp))
632 continue;
633 sp = cp;
634 while (!EOL(*cp) && !WHITE(*cp))
635 cp++;
636 *cp++ = '\0';
638 if (*sp == '%' && strtol(sp + 1, NULL, 0) > 0) {
639 vndisks[ix].size = getsize(sp + 1);
640 } else {
641 vndisks[ix].file = malloc(cp - sp);
642 strcpy(vndisks[ix].file, sp);
645 while (!EOL(*cp) && WHITE(*cp))
646 cp++;
647 vndisks[ix].flags = flags;
648 if (!EOL(*cp)) {
649 sp = cp;
650 while (!EOL(*cp) && !WHITE(*cp))
651 cp++;
652 *cp++ = '\0';
653 getoptions(&vndisks[ix], sp);
655 nvndisks++;
656 ix++;
660 void
661 getoptions(struct vndisk *vnp, const char *fstr)
663 int flags = 0;
664 const char *oarg = NULL;
666 if (strcmp(fstr, "swap") == 0)
667 flags |= VN_SWAP;
668 else if (strncmp(fstr, "mount=", 6) == 0) {
669 flags |= VN_MOUNTRW;
670 oarg = &fstr[6];
671 } else if (strncmp(fstr, "mountrw=", 8) == 0) {
672 flags |= VN_MOUNTRW;
673 oarg = &fstr[8];
674 } else if (strncmp(fstr, "mountro=", 8) == 0) {
675 flags |= VN_MOUNTRO;
676 oarg = &fstr[8];
677 } else if (strcmp(fstr, "ignore") == 0)
678 flags |= VN_IGNORE;
679 vnp->flags |= flags;
680 if (oarg) {
681 vnp->oarg = malloc(strlen(oarg) + 1);
682 strcpy(vnp->oarg, oarg);
683 } else
684 vnp->oarg = NULL;
687 char *
688 rawdevice(const char *dev)
690 char *rawbuf, *dp, *ep;
691 struct stat sb;
692 int len;
694 len = strlen(dev);
695 rawbuf = malloc(len + 2);
696 strcpy(rawbuf, dev);
697 if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
698 dp = strrchr(rawbuf, '/');
699 if (dp) {
700 for (ep = &rawbuf[len]; ep > dp; --ep)
701 *(ep+1) = *ep;
702 *++ep = 'r';
705 return (rawbuf);
708 static void
709 usage(void)
711 fprintf(stderr, "%s\n%s\n%s\n%s\n",
712 "usage: vnconfig [-cdeguvTZ] [-s options] [-r options]",
713 " [-S value] special_file [regular_file] [feature]",
714 " vnconfig -a [-cdeguv] [-s options] [-r options] [-f config_file]",
715 " vnconfig -l [special_file ...]");
716 exit(1);
719 static int64_t
720 getsize(const char *arg)
722 char *ptr;
723 int pgsize = getpagesize();
724 int64_t size = strtoq(arg, &ptr, 0);
726 switch(tolower(*ptr)) {
727 case 't':
729 * GULP! Terabytes. It's actually possible to create
730 * a 7.9 TB VN device, though newfs can't handle any single
731 * filesystem larger then 1 TB.
733 size *= 1024;
734 /* fall through */
735 case 'g':
736 size *= 1024;
737 /* fall through */
738 default:
739 case 'm':
740 size *= 1024;
741 /* fall through */
742 case 'k':
743 size *= 1024;
744 /* fall through */
745 case 'c':
746 break;
748 size = (size + pgsize - 1) / pgsize;
749 return(size);
753 * DO_AUTOLABEL
755 * Automatically label the device. This will wipe any preexisting
756 * label.
759 static void
760 do_autolabel(const char *dev __unused, const char *label __unused)
762 /* XXX not yet implemented */
763 fprintf(stderr, "autolabel not yet implemented, sorry\n");
764 exit(1);