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