1 /* $NetBSD: vnconfig.c,v 1.34 2005/08/19 02:09:50 christos 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 int compressed = 0; 143 char *tabname; 144 145 int config __P((char *, char *, char *, int)); 146 int getgeom __P((struct vndgeom *, char *)); 147 int main __P((int, char **)); 148 char *rawdevice __P((char *)); 149 void usage __P((void)); 150 151 int 152 main(argc, argv) 153 int argc; 154 char *argv[]; 155 { 156 int ch, rv, action = VND_CONFIG; 157 158 while ((ch = getopt(argc, argv, "Fcf:lrt:uvz")) != -1) { 159 switch (ch) { 160 case 'F': 161 force = 1; 162 break; 163 case 'c': 164 action = VND_CONFIG; 165 break; 166 case 'f': 167 if (setdisktab(optarg) == -1) 168 usage(); 169 break; 170 case 'l': 171 action = VND_GET; 172 break; 173 case 'r': 174 readonly = 1; 175 break; 176 case 't': 177 tabname = optarg; 178 break; 179 case 'u': 180 action = VND_UNCONFIG; 181 break; 182 case 'v': 183 verbose = 1; 184 break; 185 case 'z': 186 compressed = 1; 187 readonly = 1; 188 break; 189 default: 190 case '?': 191 usage(); 192 /* NOTREACHED */ 193 } 194 } 195 argc -= optind; 196 argv += optind; 197 198 if (action == VND_CONFIG) { 199 if ((argc < 2 || argc > 3) || 200 (argc == 3 && tabname != NULL)) 201 usage(); 202 rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL, 203 action); 204 } else if (action == VND_UNCONFIG) { 205 if (argc != 1 || tabname != NULL) 206 usage(); 207 rv = config(argv[0], NULL, NULL, action); 208 } else { /* VND_GET */ 209 char *vn, path[64]; 210 struct vnd_user vnu; 211 int v, n; 212 213 if (argc != 0 && argc != 1) 214 usage(); 215 216 vn = argc ? argv[0] : "vnd0"; 217 218 v = opendisk(vn, O_RDONLY, path, sizeof(path), 0); 219 if (v == -1) 220 err(1, "open: %s", vn); 221 222 for (n = 0; ; n++) { 223 vnu.vnu_unit = argc ? -1 : n; 224 rv = ioctl(v, VNDIOCGET, &vnu); 225 if (rv == -1) { 226 if (errno == ENXIO) 227 break; 228 err(1, "VNDIOCGET"); 229 } 230 231 if (vnu.vnu_ino == 0) 232 printf("vnd%d: not in use\n", 233 vnu.vnu_unit); 234 else { 235 char *dev; 236 struct statvfs *mnt = NULL; 237 int i, n; 238 239 n = 0; /* XXXGCC -Wuninitialized */ 240 241 printf("vnd%d: ", vnu.vnu_unit); 242 243 dev = devname(vnu.vnu_dev, S_IFBLK); 244 if (dev != NULL) 245 n = getmntinfo(&mnt, MNT_NOWAIT); 246 else 247 mnt = NULL; 248 if (mnt != NULL) { 249 for (i = 0; i < n; i++) { 250 if (strncmp( 251 mnt[i].f_mntfromname, 252 "/dev/", 5) == 0 && 253 strcmp( 254 mnt[i].f_mntfromname + 5, 255 dev) == 0) 256 break; 257 } 258 if (i < n) 259 printf("%s (%s) ", 260 mnt[i].f_mntonname, 261 mnt[i].f_mntfromname); 262 else 263 printf("%s ", dev); 264 } 265 else if (dev != NULL) 266 printf("%s ", dev); 267 else 268 printf("dev %d,%d ", 269 major(vnu.vnu_dev), 270 minor(vnu.vnu_dev)); 271 272 printf("inode %llu\n", 273 (unsigned long long)vnu.vnu_ino); 274 } 275 276 if (argc) 277 break; 278 } 279 close(v); 280 } 281 exit(rv); 282 } 283 284 int 285 config(dev, file, geom, action) 286 char *dev, *file, *geom; 287 int action; 288 { 289 struct vnd_ioctl vndio; 290 struct disklabel *lp; 291 char rdev[MAXPATHLEN + 1]; 292 int fd, rv; 293 294 fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0); 295 if (fd < 0) { 296 warn("%s: opendisk", rdev); 297 return (1); 298 } 299 300 memset(&vndio, 0, sizeof(vndio)); 301 #ifdef __GNUC__ 302 rv = 0; /* XXX */ 303 #endif 304 305 vndio.vnd_file = file; 306 if (geom != NULL) { 307 rv = getgeom(&vndio.vnd_geom, geom); 308 if (rv != 0) 309 errx(1, "invalid geometry: %s", geom); 310 vndio.vnd_flags = VNDIOF_HASGEOM; 311 } else if (tabname != NULL) { 312 lp = getdiskbyname(tabname); 313 if (lp == NULL) 314 errx(1, "unknown disk type: %s", tabname); 315 vndio.vnd_geom.vng_secsize = lp->d_secsize; 316 vndio.vnd_geom.vng_nsectors = lp->d_nsectors; 317 vndio.vnd_geom.vng_ntracks = lp->d_ntracks; 318 vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders; 319 vndio.vnd_flags = VNDIOF_HASGEOM; 320 } 321 322 if (readonly) 323 vndio.vnd_flags |= VNDIOF_READONLY; 324 325 if (compressed) 326 vndio.vnd_flags |= VNF_COMP; 327 328 /* 329 * Clear (un-configure) the device 330 */ 331 if (action == VND_UNCONFIG) { 332 if (force) 333 vndio.vnd_flags |= VNDIOF_FORCE; 334 rv = ioctl(fd, VNDIOCCLR, &vndio); 335 if (rv) 336 warn("%s: VNDIOCCLR", rdev); 337 else if (verbose) 338 printf("%s: cleared\n", rdev); 339 } 340 /* 341 * Configure the device 342 */ 343 if (action == VND_CONFIG) { 344 int ffd; 345 346 ffd = open(file, readonly ? O_RDONLY : O_RDWR); 347 if (ffd < 0) 348 warn("%s", file); 349 else { 350 (void) close(ffd); 351 352 rv = ioctl(fd, VNDIOCSET, &vndio); 353 if (rv) 354 warn("%s: VNDIOCSET", rdev); 355 else if (verbose) { 356 printf("%s: %d bytes on %s", rdev, 357 vndio.vnd_size, file); 358 if (vndio.vnd_flags & VNDIOF_HASGEOM) 359 printf(" using geometry %d/%d/%d/%d", 360 vndio.vnd_geom.vng_secsize, 361 vndio.vnd_geom.vng_nsectors, 362 vndio.vnd_geom.vng_ntracks, 363 vndio.vnd_geom.vng_ncylinders); 364 printf("\n"); 365 } 366 } 367 } 368 369 (void) close(fd); 370 fflush(stdout); 371 return (rv < 0); 372 } 373 374 int 375 getgeom(vng, cp) 376 struct vndgeom *vng; 377 char *cp; 378 { 379 char *secsize, *nsectors, *ntracks, *ncylinders; 380 381 #define GETARG(arg) \ 382 do { \ 383 if (cp == NULL || *cp == '\0') \ 384 return (1); \ 385 arg = strsep(&cp, "/"); \ 386 if (arg == NULL) \ 387 return (1); \ 388 } while (0) 389 390 GETARG(secsize); 391 GETARG(nsectors); 392 GETARG(ntracks); 393 GETARG(ncylinders); 394 395 #undef GETARG 396 397 /* Too many? */ 398 if (cp != NULL) 399 return (1); 400 401 #define CVTARG(str, num) \ 402 do { \ 403 num = strtol(str, &cp, 10); \ 404 if (*cp != '\0') \ 405 return (1); \ 406 } while (0) 407 408 CVTARG(secsize, vng->vng_secsize); 409 CVTARG(nsectors, vng->vng_nsectors); 410 CVTARG(ntracks, vng->vng_ntracks); 411 CVTARG(ncylinders, vng->vng_ncylinders); 412 413 #undef CVTARG 414 415 return (0); 416 } 417 418 void 419 usage() 420 { 421 422 (void)fprintf(stderr, "%s%s", 423 "usage: vnconfig [-crvz] [-f disktab] [-t typename] vnode_disk" 424 " regular-file [geomspec]\n", 425 " vnconfig -u [-Fv] vnode_disk\n" 426 " vnconfig -l [vnode_disk]\n"); 427 exit(1); 428 } 429