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