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