vnconfig.c revision 1.29 1 /* $NetBSD: vnconfig.c,v 1.29 2003/08/07 11:25:50 agc 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 printf("vnd%d: dev %d,%d inode %d\n",
231 vnu.vnu_unit,
232 major(vnu.vnu_dev), minor(vnu.vnu_dev),
233 vnu.vnu_ino);
234
235 if (argc)
236 break;
237 }
238 close(v);
239 }
240 exit(rv);
241 }
242
243 int
244 config(dev, file, geom, action)
245 char *dev, *file, *geom;
246 int action;
247 {
248 struct vnd_ioctl vndio;
249 struct disklabel *lp;
250 char rdev[MAXPATHLEN + 1];
251 int fd, rv;
252
253 fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0);
254 if (fd < 0) {
255 warn("%s: opendisk", rdev);
256 return (1);
257 }
258
259 memset(&vndio, 0, sizeof(vndio));
260 #ifdef __GNUC__
261 rv = 0; /* XXX */
262 #endif
263
264 vndio.vnd_file = file;
265 if (geom != NULL) {
266 rv = getgeom(&vndio.vnd_geom, geom);
267 if (rv != 0)
268 errx(1, "invalid geometry: %s", geom);
269 vndio.vnd_flags = VNDIOF_HASGEOM;
270 } else if (tabname != NULL) {
271 lp = getdiskbyname(tabname);
272 if (lp == NULL)
273 errx(1, "unknown disk type: %s", tabname);
274 vndio.vnd_geom.vng_secsize = lp->d_secsize;
275 vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
276 vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
277 vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
278 vndio.vnd_flags = VNDIOF_HASGEOM;
279 }
280
281 if (readonly)
282 vndio.vnd_flags |= VNDIOF_READONLY;
283
284 /*
285 * Clear (un-configure) the device
286 */
287 if (action == VND_UNCONFIG) {
288 if (force)
289 vndio.vnd_flags |= VNDIOF_FORCE;
290 rv = ioctl(fd, VNDIOCCLR, &vndio);
291 if (rv)
292 warn("%s: VNDIOCCLR", rdev);
293 else if (verbose)
294 printf("%s: cleared\n", rdev);
295 }
296 /*
297 * Configure the device
298 */
299 if (action == VND_CONFIG) {
300 int ffd;
301
302 ffd = open(file, readonly ? O_RDONLY : O_RDWR);
303 if (ffd < 0)
304 warn("%s", file);
305 else {
306 (void) close(ffd);
307
308 rv = ioctl(fd, VNDIOCSET, &vndio);
309 if (rv)
310 warn("%s: VNDIOCSET", rdev);
311 else if (verbose) {
312 printf("%s: %d bytes on %s", rdev,
313 vndio.vnd_size, file);
314 if (vndio.vnd_flags & VNDIOF_HASGEOM)
315 printf(" using geometry %d/%d/%d/%d",
316 vndio.vnd_geom.vng_secsize,
317 vndio.vnd_geom.vng_nsectors,
318 vndio.vnd_geom.vng_ntracks,
319 vndio.vnd_geom.vng_ncylinders);
320 printf("\n");
321 }
322 }
323 }
324
325 (void) close(fd);
326 fflush(stdout);
327 return (rv < 0);
328 }
329
330 int
331 getgeom(vng, cp)
332 struct vndgeom *vng;
333 char *cp;
334 {
335 char *secsize, *nsectors, *ntracks, *ncylinders;
336
337 #define GETARG(arg) \
338 do { \
339 if (cp == NULL || *cp == '\0') \
340 return (1); \
341 arg = strsep(&cp, "/"); \
342 if (arg == NULL) \
343 return (1); \
344 } while (0)
345
346 GETARG(secsize);
347 GETARG(nsectors);
348 GETARG(ntracks);
349 GETARG(ncylinders);
350
351 #undef GETARG
352
353 /* Too many? */
354 if (cp != NULL)
355 return (1);
356
357 #define CVTARG(str, num) \
358 do { \
359 num = strtol(str, &cp, 10); \
360 if (*cp != '\0') \
361 return (1); \
362 } while (0)
363
364 CVTARG(secsize, vng->vng_secsize);
365 CVTARG(nsectors, vng->vng_nsectors);
366 CVTARG(ntracks, vng->vng_ntracks);
367 CVTARG(ncylinders, vng->vng_ncylinders);
368
369 #undef CVTARG
370
371 return (0);
372 }
373
374 void
375 usage()
376 {
377
378 (void)fprintf(stderr, "%s%s",
379 "usage: vnconfig [-crv] [-f disktab] [-t typename] vnode_disk"
380 " regular-file [geomspec]\n",
381 " vnconfig -u [-Fv] vnode_disk\n"
382 " vnconfig -l [vnode_disk]\n");
383 exit(1);
384 }
385