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