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