Home | History | Annotate | Line # | Download | only in vnconfig
vnconfig.c revision 1.33
      1 /*	$NetBSD: vnconfig.c,v 1.33 2005/07/17 00:08:27 hubertf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 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 /*
     40  * Copyright (c) 1990, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
     72  *
     73  *	@(#)vnconfig.c	8.1 (Berkeley) 12/15/93
     74  */
     75 
     76 /*
     77  * Copyright (c) 1993 University of Utah.
     78  *
     79  * This code is derived from software contributed to Berkeley by
     80  * the Systems Programming Group of the University of Utah Computer
     81  * Science Department.
     82  *
     83  * Redistribution and use in source and binary forms, with or without
     84  * modification, are permitted provided that the following conditions
     85  * are met:
     86  * 1. Redistributions of source code must retain the above copyright
     87  *    notice, this list of conditions and the following disclaimer.
     88  * 2. Redistributions in binary form must reproduce the above copyright
     89  *    notice, this list of conditions and the following disclaimer in the
     90  *    documentation and/or other materials provided with the distribution.
     91  * 3. All advertising materials mentioning features or use of this software
     92  *    must display the following acknowledgement:
     93  *	This product includes software developed by the University of
     94  *	California, Berkeley and its contributors.
     95  * 4. Neither the name of the University nor the names of its contributors
     96  *    may be used to endorse or promote products derived from this software
     97  *    without specific prior written permission.
     98  *
     99  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    100  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    101  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    102  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    103  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    104  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    105  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    106  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    107  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    108  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    109  * SUCH DAMAGE.
    110  *
    111  * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
    112  *
    113  *	@(#)vnconfig.c	8.1 (Berkeley) 12/15/93
    114  */
    115 
    116 #include <sys/param.h>
    117 #include <sys/ioctl.h>
    118 #include <sys/mount.h>
    119 #include <sys/buf.h>
    120 #include <sys/disklabel.h>
    121 #include <sys/disk.h>
    122 
    123 #include <dev/vndvar.h>
    124 
    125 #include <disktab.h>
    126 #include <err.h>
    127 #include <errno.h>
    128 #include <fcntl.h>
    129 #include <stdio.h>
    130 #include <stdlib.h>
    131 #include <string.h>
    132 #include <unistd.h>
    133 #include <util.h>
    134 
    135 #define VND_CONFIG	1
    136 #define VND_UNCONFIG	2
    137 #define VND_GET		3
    138 
    139 int	verbose = 0;
    140 int	readonly = 0;
    141 int	force = 0;
    142 int	compressed = 0;
    143 char	*tabname;
    144 
    145 int	config __P((char *, char *, char *, int));
    146 int	getgeom __P((struct vndgeom *, char *));
    147 int	main __P((int, char **));
    148 char   *rawdevice __P((char *));
    149 void	usage __P((void));
    150 
    151 int
    152 main(argc, argv)
    153 	int argc;
    154 	char *argv[];
    155 {
    156 	int ch, rv, action = VND_CONFIG;
    157 
    158 	while ((ch = getopt(argc, argv, "Fcf:lrt:uvz")) != -1) {
    159 		switch (ch) {
    160 		case 'F':
    161 			force = 1;
    162 			break;
    163 		case 'c':
    164 			action = VND_CONFIG;
    165 			break;
    166 		case 'f':
    167 			if (setdisktab(optarg) == -1)
    168 				usage();
    169 			break;
    170 		case 'l':
    171 			action = VND_GET;
    172 			break;
    173 		case 'r':
    174 			readonly = 1;
    175 			break;
    176 		case 't':
    177 			tabname = optarg;
    178 			break;
    179 		case 'u':
    180 			action = VND_UNCONFIG;
    181 			break;
    182 		case 'v':
    183 			verbose = 1;
    184 			break;
    185 		case 'z':
    186 			compressed = 1;
    187 			readonly = 1;
    188 			break;
    189 		default:
    190 		case '?':
    191 			usage();
    192 			/* NOTREACHED */
    193 		}
    194 	}
    195 	argc -= optind;
    196 	argv += optind;
    197 
    198 	if (action == VND_CONFIG) {
    199 		if ((argc < 2 || argc > 3) ||
    200 		    (argc == 3 && tabname != NULL))
    201 			usage();
    202 		rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL,
    203 		    action);
    204 	} else if (action == VND_UNCONFIG) {
    205 		if (argc != 1 || tabname != NULL)
    206 			usage();
    207 		rv = config(argv[0], NULL, NULL, action);
    208 	} else { /* VND_GET */
    209 		char *vn, path[64];
    210 		struct vnd_user vnu;
    211 		int v, n;
    212 
    213 		if (argc != 0 && argc != 1)
    214 			usage();
    215 
    216 		vn = argc ? argv[0] : "vnd0";
    217 
    218 		v = opendisk(vn, O_RDONLY, path, sizeof(path), 0);
    219 		if (v == -1)
    220 			err(1, "open: %s", vn);
    221 
    222 		for (n = 0; ; n++) {
    223 			vnu.vnu_unit = argc ? -1 : n;
    224 			rv = ioctl(v, VNDIOCGET, &vnu);
    225 			if (rv == -1) {
    226 				if (errno == ENXIO)
    227 					break;
    228 				err(1, "VNDIOCGET");
    229 			}
    230 
    231 			if (vnu.vnu_ino == 0)
    232 				printf("vnd%d: not in use\n",
    233 				    vnu.vnu_unit);
    234 			else {
    235 				char *dev;
    236 				struct statvfs *mnt = NULL;
    237 				int i, n;
    238 
    239 				n = 0;	/* XXXGCC -Wuninitialized */
    240 
    241 				printf("vnd%d: ", vnu.vnu_unit);
    242 
    243 				dev = devname(vnu.vnu_dev, S_IFBLK);
    244 				if (dev != NULL)
    245 					n = getmntinfo(&mnt, MNT_NOWAIT);
    246 				else
    247 					mnt = NULL;
    248 				if (mnt != NULL) {
    249 					for (i = 0; i < n; i++) {
    250 						if (strncmp(
    251 						    mnt[i].f_mntfromname,
    252 						    "/dev/", 5) == 0 &&
    253 						    strcmp(
    254 						    mnt[i].f_mntfromname + 5,
    255 						    dev) == 0)
    256 							break;
    257 					}
    258 					if (i < n)
    259 						printf("%s (%s) ",
    260 						    mnt[i].f_mntonname,
    261 						    mnt[i].f_mntfromname);
    262 					else
    263 						printf("%s ", dev);
    264 				}
    265 				else if (dev != NULL)
    266 					printf("%s ", dev);
    267 				else
    268 					printf("dev %d,%d ",
    269 					    major(vnu.vnu_dev),
    270 					    minor(vnu.vnu_dev));
    271 
    272 				printf("inode %d\n", vnu.vnu_ino);
    273 			}
    274 
    275 			if (argc)
    276 				break;
    277 		}
    278 		close(v);
    279 	}
    280 	exit(rv);
    281 }
    282 
    283 int
    284 config(dev, file, geom, action)
    285 	char *dev, *file, *geom;
    286 	int action;
    287 {
    288 	struct vnd_ioctl vndio;
    289 	struct disklabel *lp;
    290 	char rdev[MAXPATHLEN + 1];
    291 	int fd, rv;
    292 
    293 	fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0);
    294 	if (fd < 0) {
    295 		warn("%s: opendisk", rdev);
    296 		return (1);
    297 	}
    298 
    299 	memset(&vndio, 0, sizeof(vndio));
    300 #ifdef __GNUC__
    301 	rv = 0;			/* XXX */
    302 #endif
    303 
    304 	vndio.vnd_file = file;
    305 	if (geom != NULL) {
    306 		rv = getgeom(&vndio.vnd_geom, geom);
    307 		if (rv != 0)
    308 			errx(1, "invalid geometry: %s", geom);
    309 		vndio.vnd_flags = VNDIOF_HASGEOM;
    310 	} else if (tabname != NULL) {
    311 		lp = getdiskbyname(tabname);
    312 		if (lp == NULL)
    313 			errx(1, "unknown disk type: %s", tabname);
    314 		vndio.vnd_geom.vng_secsize = lp->d_secsize;
    315 		vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
    316 		vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
    317 		vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
    318 		vndio.vnd_flags = VNDIOF_HASGEOM;
    319 	}
    320 
    321 	if (readonly)
    322 		vndio.vnd_flags |= VNDIOF_READONLY;
    323 
    324 	if (compressed)
    325 		vndio.vnd_flags |= VNF_COMP;
    326 
    327 	/*
    328 	 * Clear (un-configure) the device
    329 	 */
    330 	if (action == VND_UNCONFIG) {
    331 		if (force)
    332 			vndio.vnd_flags |= VNDIOF_FORCE;
    333 		rv = ioctl(fd, VNDIOCCLR, &vndio);
    334 		if (rv)
    335 			warn("%s: VNDIOCCLR", rdev);
    336 		else if (verbose)
    337 			printf("%s: cleared\n", rdev);
    338 	}
    339 	/*
    340 	 * Configure the device
    341 	 */
    342 	if (action == VND_CONFIG) {
    343 		int	ffd;
    344 
    345 		ffd = open(file, readonly ? O_RDONLY : O_RDWR);
    346 		if (ffd < 0)
    347 			warn("%s", file);
    348 		else {
    349 			(void) close(ffd);
    350 
    351 			rv = ioctl(fd, VNDIOCSET, &vndio);
    352 			if (rv)
    353 				warn("%s: VNDIOCSET", rdev);
    354 			else if (verbose) {
    355 				printf("%s: %d bytes on %s", rdev,
    356 				    vndio.vnd_size, file);
    357 				if (vndio.vnd_flags & VNDIOF_HASGEOM)
    358 					printf(" using geometry %d/%d/%d/%d",
    359 					    vndio.vnd_geom.vng_secsize,
    360 					    vndio.vnd_geom.vng_nsectors,
    361 					    vndio.vnd_geom.vng_ntracks,
    362 				    vndio.vnd_geom.vng_ncylinders);
    363 				printf("\n");
    364 			}
    365 		}
    366 	}
    367 
    368 	(void) close(fd);
    369 	fflush(stdout);
    370 	return (rv < 0);
    371 }
    372 
    373 int
    374 getgeom(vng, cp)
    375 	struct vndgeom *vng;
    376 	char *cp;
    377 {
    378 	char *secsize, *nsectors, *ntracks, *ncylinders;
    379 
    380 #define	GETARG(arg) \
    381 	do { \
    382 		if (cp == NULL || *cp == '\0') \
    383 			return (1); \
    384 		arg = strsep(&cp, "/"); \
    385 		if (arg == NULL) \
    386 			return (1); \
    387 	} while (0)
    388 
    389 	GETARG(secsize);
    390 	GETARG(nsectors);
    391 	GETARG(ntracks);
    392 	GETARG(ncylinders);
    393 
    394 #undef GETARG
    395 
    396 	/* Too many? */
    397 	if (cp != NULL)
    398 		return (1);
    399 
    400 #define	CVTARG(str, num) \
    401 	do { \
    402 		num = strtol(str, &cp, 10); \
    403 		if (*cp != '\0') \
    404 			return (1); \
    405 	} while (0)
    406 
    407 	CVTARG(secsize, vng->vng_secsize);
    408 	CVTARG(nsectors, vng->vng_nsectors);
    409 	CVTARG(ntracks, vng->vng_ntracks);
    410 	CVTARG(ncylinders, vng->vng_ncylinders);
    411 
    412 #undef CVTARG
    413 
    414 	return (0);
    415 }
    416 
    417 void
    418 usage()
    419 {
    420 
    421 	(void)fprintf(stderr, "%s%s",
    422 	    "usage: vnconfig [-crvz] [-f disktab] [-t typename] vnode_disk"
    423 		" regular-file [geomspec]\n",
    424 	    "       vnconfig -u [-Fv] vnode_disk\n"
    425 	    "       vnconfig -l [vnode_disk]\n");
    426 	exit(1);
    427 }
    428