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