Home | History | Annotate | Line # | Download | only in vnconfig
vnconfig.c revision 1.30
      1 /*	$NetBSD: vnconfig.c,v 1.30 2004/01/25 21:49:04 atatat 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 char	*tabname;
    143 
    144 int	config __P((char *, char *, char *, int));
    145 int	getgeom __P((struct vndgeom *, char *));
    146 int	main __P((int, char **));
    147 char   *rawdevice __P((char *));
    148 void	usage __P((void));
    149 
    150 int
    151 main(argc, argv)
    152 	int argc;
    153 	char *argv[];
    154 {
    155 	int ch, rv, action = VND_CONFIG;
    156 
    157 	while ((ch = getopt(argc, argv, "Fcf:lrt:uv")) != -1) {
    158 		switch (ch) {
    159 		case 'F':
    160 			force = 1;
    161 			break;
    162 		case 'c':
    163 			action = VND_CONFIG;
    164 			break;
    165 		case 'f':
    166 			if (setdisktab(optarg) == -1)
    167 				usage();
    168 			break;
    169 		case 'l':
    170 			action = VND_GET;
    171 			break;
    172 		case 'r':
    173 			readonly = 1;
    174 			break;
    175 		case 't':
    176 			tabname = optarg;
    177 			break;
    178 		case 'u':
    179 			action = VND_UNCONFIG;
    180 			break;
    181 		case 'v':
    182 			verbose = 1;
    183 			break;
    184 		default:
    185 		case '?':
    186 			usage();
    187 			/* NOTREACHED */
    188 		}
    189 	}
    190 	argc -= optind;
    191 	argv += optind;
    192 
    193 	if (action == VND_CONFIG) {
    194 		if ((argc < 2 || argc > 3) ||
    195 		    (argc == 3 && tabname != NULL))
    196 			usage();
    197 		rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL,
    198 		    action);
    199 	} else if (action == VND_UNCONFIG) {
    200 		if (argc != 1 || tabname != NULL)
    201 			usage();
    202 		rv = config(argv[0], NULL, NULL, action);
    203 	} else { /* VND_GET */
    204 		char *vn, path[64];
    205 		struct vnd_user vnu;
    206 		int v, n;
    207 
    208 		if (argc != 0 && argc != 1)
    209 			usage();
    210 
    211 		vn = argc ? argv[0] : "vnd0";
    212 
    213 		v = opendisk(vn, O_RDONLY, path, sizeof(path), 0);
    214 		if (v == -1)
    215 			err(1, "open: %s", vn);
    216 
    217 		for (n = 0; ; n++) {
    218 			vnu.vnu_unit = argc ? -1 : n;
    219 			rv = ioctl(v, VNDIOCGET, &vnu);
    220 			if (rv == -1) {
    221 				if (errno == ENXIO)
    222 					break;
    223 				err(1, "VNDIOCGET");
    224 			}
    225 
    226 			if (vnu.vnu_ino == 0)
    227 				printf("vnd%d: not in use\n",
    228 				    vnu.vnu_unit);
    229 			else {
    230 				char *dev;
    231 				struct statfs *mnt = NULL;
    232 				int i, n;
    233 
    234 				printf("vnd%d: ", vnu.vnu_unit);
    235 
    236 				dev = devname(vnu.vnu_dev, S_IFBLK);
    237 				if (dev != NULL)
    238 					n = getmntinfo(&mnt, MNT_NOWAIT);
    239 				else
    240 					mnt = NULL;
    241 				if (mnt != NULL) {
    242 					for (i = 0; i < n; i++) {
    243 						if (strncmp(
    244 						    mnt[i].f_mntfromname,
    245 						    "/dev/", 5) == 0 &&
    246 						    strcmp(
    247 						    mnt[i].f_mntfromname + 5,
    248 						    dev) == 0)
    249 							break;
    250 					}
    251 					if (i < n)
    252 						printf("%s (%s) ",
    253 						    mnt[i].f_mntonname,
    254 						    mnt[i].f_mntfromname);
    255 					else
    256 						printf("%s ", dev);
    257 				}
    258 				else if (dev != NULL)
    259 					printf("%s ", dev);
    260 				else
    261 					printf("dev %d,%d ",
    262 					    major(vnu.vnu_dev),
    263 					    minor(vnu.vnu_dev));
    264 
    265 				printf("inode %d\n", vnu.vnu_ino);
    266 			}
    267 
    268 			if (argc)
    269 				break;
    270 		}
    271 		close(v);
    272 	}
    273 	exit(rv);
    274 }
    275 
    276 int
    277 config(dev, file, geom, action)
    278 	char *dev, *file, *geom;
    279 	int action;
    280 {
    281 	struct vnd_ioctl vndio;
    282 	struct disklabel *lp;
    283 	char rdev[MAXPATHLEN + 1];
    284 	int fd, rv;
    285 
    286 	fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0);
    287 	if (fd < 0) {
    288 		warn("%s: opendisk", rdev);
    289 		return (1);
    290 	}
    291 
    292 	memset(&vndio, 0, sizeof(vndio));
    293 #ifdef __GNUC__
    294 	rv = 0;			/* XXX */
    295 #endif
    296 
    297 	vndio.vnd_file = file;
    298 	if (geom != NULL) {
    299 		rv = getgeom(&vndio.vnd_geom, geom);
    300 		if (rv != 0)
    301 			errx(1, "invalid geometry: %s", geom);
    302 		vndio.vnd_flags = VNDIOF_HASGEOM;
    303 	} else if (tabname != NULL) {
    304 		lp = getdiskbyname(tabname);
    305 		if (lp == NULL)
    306 			errx(1, "unknown disk type: %s", tabname);
    307 		vndio.vnd_geom.vng_secsize = lp->d_secsize;
    308 		vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
    309 		vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
    310 		vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
    311 		vndio.vnd_flags = VNDIOF_HASGEOM;
    312 	}
    313 
    314 	if (readonly)
    315 		vndio.vnd_flags |= VNDIOF_READONLY;
    316 
    317 	/*
    318 	 * Clear (un-configure) the device
    319 	 */
    320 	if (action == VND_UNCONFIG) {
    321 		if (force)
    322 			vndio.vnd_flags |= VNDIOF_FORCE;
    323 		rv = ioctl(fd, VNDIOCCLR, &vndio);
    324 		if (rv)
    325 			warn("%s: VNDIOCCLR", rdev);
    326 		else if (verbose)
    327 			printf("%s: cleared\n", rdev);
    328 	}
    329 	/*
    330 	 * Configure the device
    331 	 */
    332 	if (action == VND_CONFIG) {
    333 		int	ffd;
    334 
    335 		ffd = open(file, readonly ? O_RDONLY : O_RDWR);
    336 		if (ffd < 0)
    337 			warn("%s", file);
    338 		else {
    339 			(void) close(ffd);
    340 
    341 			rv = ioctl(fd, VNDIOCSET, &vndio);
    342 			if (rv)
    343 				warn("%s: VNDIOCSET", rdev);
    344 			else if (verbose) {
    345 				printf("%s: %d bytes on %s", rdev,
    346 				    vndio.vnd_size, file);
    347 				if (vndio.vnd_flags & VNDIOF_HASGEOM)
    348 					printf(" using geometry %d/%d/%d/%d",
    349 					    vndio.vnd_geom.vng_secsize,
    350 					    vndio.vnd_geom.vng_nsectors,
    351 					    vndio.vnd_geom.vng_ntracks,
    352 				    vndio.vnd_geom.vng_ncylinders);
    353 				printf("\n");
    354 			}
    355 		}
    356 	}
    357 
    358 	(void) close(fd);
    359 	fflush(stdout);
    360 	return (rv < 0);
    361 }
    362 
    363 int
    364 getgeom(vng, cp)
    365 	struct vndgeom *vng;
    366 	char *cp;
    367 {
    368 	char *secsize, *nsectors, *ntracks, *ncylinders;
    369 
    370 #define	GETARG(arg) \
    371 	do { \
    372 		if (cp == NULL || *cp == '\0') \
    373 			return (1); \
    374 		arg = strsep(&cp, "/"); \
    375 		if (arg == NULL) \
    376 			return (1); \
    377 	} while (0)
    378 
    379 	GETARG(secsize);
    380 	GETARG(nsectors);
    381 	GETARG(ntracks);
    382 	GETARG(ncylinders);
    383 
    384 #undef GETARG
    385 
    386 	/* Too many? */
    387 	if (cp != NULL)
    388 		return (1);
    389 
    390 #define	CVTARG(str, num) \
    391 	do { \
    392 		num = strtol(str, &cp, 10); \
    393 		if (*cp != '\0') \
    394 			return (1); \
    395 	} while (0)
    396 
    397 	CVTARG(secsize, vng->vng_secsize);
    398 	CVTARG(nsectors, vng->vng_nsectors);
    399 	CVTARG(ntracks, vng->vng_ntracks);
    400 	CVTARG(ncylinders, vng->vng_ncylinders);
    401 
    402 #undef CVTARG
    403 
    404 	return (0);
    405 }
    406 
    407 void
    408 usage()
    409 {
    410 
    411 	(void)fprintf(stderr, "%s%s",
    412 	    "usage: vnconfig [-crv] [-f disktab] [-t typename] vnode_disk"
    413 		" regular-file [geomspec]\n",
    414 	    "       vnconfig -u [-Fv] vnode_disk\n"
    415 	    "       vnconfig -l [vnode_disk]\n");
    416 	exit(1);
    417 }
    418