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